]> git.pond.sub.org Git - empserver/blob - src/lib/update/distribute.c
Merge dodistribute() parameters dist_i_cost, dist_e_cost
[empserver] / src / lib / update / distribute.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  distribute.c: Do distribution to sectors
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Steve McClure, 1998
33  */
34
35 #include <config.h>
36
37 #include "distribute.h"
38 #include "item.h"
39 #include "path.h"
40 #include "plague.h"
41 #include "prototypes.h"
42 #include "update.h"
43
44 #ifdef DISTRIBUTE_DEBUG
45 #define DPRINTF(fmt, ...) ((void)printf(fmt , ## __VA_ARGS__))
46 #else
47 #define DPRINTF(fmt, ...) ((void)0)
48 #endif
49
50 #define EXPORT_BONUS 10.0
51 #define IMPORT_BONUS 10.0
52
53 int
54 dodistribute(struct sctstr *sp, int imex, double path_cost)
55 {
56     struct ichrstr *ip;
57     struct sctstr *dist;
58     int amt;
59     int thresh;
60     int amt_dist;
61     int amt_sect;
62     enum i_packing sect_packing, dist_packing;
63     int pack;
64     double mcost;
65     int diff;
66     i_type item;
67     int changed;
68     int rplague;
69     int lplague;
70
71     if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
72         return 0;
73
74     if (path_cost < 0.0) {
75         if (sp->sct_own != 0) {
76             if (imex == EXPORT) /* only want this once */
77                 wu(0, sp->sct_own, "No path to dist sector for %s\n",
78                    ownxy(sp));
79         }
80         return 0;
81     }
82
83     dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
84     dist_packing = dist->sct_effic >= 60 ? dchr[dist->sct_type].d_pkg : IPKG;
85     sect_packing = sp->sct_effic   >= 60 ? dchr[sp->sct_type].d_pkg : IPKG;
86
87     DPRINTF("distribute: %d,%d to %d,%d pathcost %g\n",
88             sp->sct_x, sp->sct_y, sp->sct_dist_x, sp->sct_dist_y,
89             path_cost);
90
91     lplague = rplague = changed = 0;
92     for (item = I_NONE + 1; item <= I_MAX; item++) {
93         if (sp->sct_dist[item] == 0)
94             continue;
95         ip = &ichr[item];
96         thresh = sp->sct_dist[item];
97         /*
98          * calculate costs for importing and exporting.
99          * the mob bonus is because delivering straight through
100          * to the dist sect is cheaper than stopping at each
101          * sector along the way (processor-timewise)
102          */
103         amt_sect = sp->sct_item[item];
104         amt_dist = dist->sct_item[item];
105         diff = amt_sect - thresh;
106         if (item == I_CIVIL)
107             if (sp->sct_own != sp->sct_oldown)
108                 continue;
109         if (item == I_CIVIL)
110             if (dist->sct_own != dist->sct_oldown)
111                 continue;
112         if (diff < 0) {
113             if (imex != IMPORT)
114                 continue;
115
116             if (!military_control(dist))
117                 continue;
118
119             diff = -diff;
120             /*
121              * import.
122              * don't import if no mobility.
123              * check to make sure have enough mobility in the
124              * dist sector to import what we need.
125              */
126             if (dist->sct_mobil <= 0)
127                 continue;
128             amt = diff;
129             /* make sure not to abandon the sector */
130             if (item == I_CIVIL ||
131                 (item == I_MILIT && dist->sct_item[I_CIVIL] == 0))
132                 amt_dist--;
133
134             if (amt_dist < amt) {
135                 amt = amt_dist;
136                 if (amt_dist <= 0)
137                     continue;
138             }
139             pack = ip->i_pkg[dist_packing];
140             mcost = path_cost / pack * ip->i_lbs / IMPORT_BONUS;
141             if (dist->sct_mobil < mcost * amt)
142                 amt = dist->sct_mobil / mcost;
143
144             lplague++;
145             dist->sct_item[item] -= amt;
146             changed++;
147             dist->sct_mobil -= (int)(mcost * amt);
148             sp->sct_item[item] += amt;
149         } else {
150             if (imex != EXPORT)
151                 continue;
152             if (!military_control(sp))
153                 continue;
154             if ((item == I_CIVIL) && (sp->sct_work < 100))
155                 continue;
156             if ((item == I_CIVIL) && (sp->sct_own != sp->sct_oldown))
157                 continue;
158             /*
159              * export.
160              * don't export if no mobility. check to make sure we
161              * have mobility enough to do the right thing.
162              * also make sure that there's enough space in the
163              * target sector to hold the required amt.
164              */
165             if (sp->sct_mobil <= 0)
166                 continue;
167             amt = diff;
168             if (amt > amt_sect)
169                 amt = amt_sect;
170             pack = MAX(ip->i_pkg[sect_packing], ip->i_pkg[dist_packing]);
171             mcost = path_cost / pack * ip->i_lbs / EXPORT_BONUS;
172             if (sp->sct_mobil < mcost * amt)
173                 amt = sp->sct_mobil / mcost;
174             if (amt > ITEM_MAX - amt_dist)
175                 amt = ITEM_MAX - amt_dist;
176             if (amt == 0)
177                 continue;
178
179             rplague++;
180             sp->sct_item[item] -= amt;
181             changed++;
182             sp->sct_mobil -= (int)(mcost * amt);
183             dist->sct_item[item] += amt;
184         }
185     }
186
187     if (lplague) {
188         lplague = dist->sct_pstage;
189         if (lplague == PLG_INFECT && sp->sct_pstage == PLG_HEALTHY)
190             sp->sct_pstage = PLG_EXPOSED;
191     }
192
193     if (rplague) {
194         rplague = sp->sct_pstage;
195         if (rplague == PLG_INFECT && dist->sct_pstage == PLG_HEALTHY)
196             dist->sct_pstage = PLG_EXPOSED;
197     }
198
199     return changed;
200 }