]> git.pond.sub.org Git - empserver/blob - src/lib/update/distribute.c
production: Use update code instead of duplicating it
[empserver] / src / lib / update / distribute.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  distribute.c: Do distribution to sectors
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Steve McClure, 1998
32  *     Markus Armbruster, 2004-2011
33  */
34
35 #include <config.h>
36
37 #include "file.h"
38 #include "item.h"
39 #include "optlist.h"
40 #include "path.h"
41 #include "plague.h"
42 #include "prototypes.h"
43 #include "sect.h"
44 #include "update.h"
45
46 #ifdef DISTRIBUTE_DEBUG
47 #define DPRINTF(fmt, ...) ((void)printf(fmt , ## __VA_ARGS__))
48 #else
49 #define DPRINTF(fmt, ...) ((void)0)
50 #endif
51
52 #define EXPORT_BONUS 10.0
53 #define IMPORT_BONUS 10.0
54
55 int
56 dodistribute(struct sctstr *sp, int imex, double import_cost)
57 {
58     double path_cost, dcc;
59     struct ichrstr *ip;
60     struct sctstr *dist;
61     int amt;
62     int thresh;
63     int amt_dist;
64     int amt_sect;
65     enum i_packing sect_packing, dist_packing;
66     int pack;
67     double mcost;
68     int diff;
69     i_type item;
70     int changed;
71     int rplague;
72     int lplague;
73
74     if (imex == IMPORT && import_cost < 0.0)
75         return 0;
76
77     dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
78     if (imex == IMPORT)
79         path_cost = import_cost;
80     else {
81         dcc = sector_mcost(dist, MOB_MOVE);
82         if (import_cost < 0.0 || dcc < 0.0) {
83             if (sp->sct_own && sp->sct_uid != dist->sct_uid)
84                 wu(0, sp->sct_own, "No path to dist sector for %s\n",
85                    ownxy(sp));
86             return 0;
87         }
88         path_cost = import_cost - sector_mcost(sp, MOB_MOVE) + dcc;
89     }
90
91     dist_packing = dist->sct_effic >= 60 ? dchr[dist->sct_type].d_pkg : IPKG;
92     sect_packing = sp->sct_effic   >= 60 ? dchr[sp->sct_type].d_pkg : IPKG;
93
94     DPRINTF("distribute: %d,%d to %d,%d pathcost %g\n",
95             sp->sct_x, sp->sct_y, sp->sct_dist_x, sp->sct_dist_y,
96             path_cost);
97
98     lplague = rplague = changed = 0;
99     for (item = I_NONE + 1; item <= I_MAX; item++) {
100         if (sp->sct_dist[item] == 0)
101             continue;
102         ip = &ichr[item];
103         thresh = sp->sct_dist[item];
104         /*
105          * calculate costs for importing and exporting.
106          * the mob bonus is because delivering straight through
107          * to the dist sect is cheaper than stopping at each
108          * sector along the way (processor-timewise)
109          */
110         amt_sect = sp->sct_item[item];
111         amt_dist = dist->sct_item[item];
112         diff = amt_sect - thresh;
113         if (item == I_CIVIL)
114             if (sp->sct_own != sp->sct_oldown)
115                 continue;
116         if (item == I_CIVIL)
117             if (dist->sct_own != dist->sct_oldown)
118                 continue;
119         if (diff < 0) {
120             if (imex != IMPORT)
121                 continue;
122
123             if (!military_control(dist))
124                 continue;
125
126             diff = -diff;
127             /*
128              * import.
129              * don't import if no mobility.
130              * check to make sure have enough mobility in the
131              * dist sector to import what we need.
132              */
133             if (dist->sct_mobil <= 0)
134                 continue;
135             amt = diff;
136             /* make sure not to abandon the sector */
137             if (item == I_CIVIL ||
138                 (item == I_MILIT && dist->sct_item[I_CIVIL] == 0))
139                 amt_dist--;
140
141             if (amt_dist < amt) {
142                 amt = amt_dist;
143                 if (amt_dist <= 0)
144                     continue;
145             }
146             pack = ip->i_pkg[dist_packing];
147             mcost = path_cost / pack * ip->i_lbs / IMPORT_BONUS;
148             if (dist->sct_mobil < mcost * amt)
149                 amt = dist->sct_mobil / mcost;
150
151             lplague++;
152             dist->sct_item[item] -= amt;
153             changed++;
154             dist->sct_mobil -= (int)(mcost * amt);
155             sp->sct_item[item] += amt;
156         } else {
157             if (imex != EXPORT)
158                 continue;
159             if (!military_control(sp))
160                 continue;
161             if ((item == I_CIVIL) && (sp->sct_work < 100))
162                 continue;
163             if ((item == I_CIVIL) && (sp->sct_own != sp->sct_oldown))
164                 continue;
165             /*
166              * export.
167              * don't export if no mobility. check to make sure we
168              * have mobility enough to do the right thing.
169              * also make sure that there's enough space in the
170              * target sector to hold the required amt.
171              */
172             if (sp->sct_mobil <= 0)
173                 continue;
174             amt = diff;
175             if (amt > amt_sect)
176                 amt = amt_sect;
177             pack = MAX(ip->i_pkg[sect_packing], ip->i_pkg[dist_packing]);
178             mcost = path_cost / pack * ip->i_lbs / EXPORT_BONUS;
179             if (sp->sct_mobil < mcost * amt)
180                 amt = sp->sct_mobil / mcost;
181             if (amt > ITEM_MAX - amt_dist)
182                 amt = ITEM_MAX - amt_dist;
183             if (amt == 0)
184                 continue;
185
186             rplague++;
187             sp->sct_item[item] -= amt;
188             changed++;
189             sp->sct_mobil -= (int)(mcost * amt);
190             dist->sct_item[item] += amt;
191         }
192     }
193
194     if (lplague) {
195         lplague = dist->sct_pstage;
196         if (lplague == PLG_INFECT && sp->sct_pstage == PLG_HEALTHY)
197             sp->sct_pstage = PLG_EXPOSED;
198     }
199
200     if (rplague) {
201         rplague = sp->sct_pstage;
202         if (rplague == PLG_INFECT && dist->sct_pstage == PLG_HEALTHY)
203             dist->sct_pstage = PLG_EXPOSED;
204     }
205
206     return changed;
207 }