]> git.pond.sub.org Git - empserver/blob - src/lib/update/distribute.c
Update copyright notice
[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 "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 import_cost)
55 {
56     double path_cost, dcc;
57     struct ichrstr *ip;
58     struct sctstr *dist;
59     int amt;
60     int thresh;
61     int amt_dist;
62     int amt_sect;
63     enum i_packing sect_packing, dist_packing;
64     int pack;
65     double mcost;
66     int diff;
67     i_type item;
68     int changed;
69     int rplague;
70     int lplague;
71
72     if (imex == IMPORT && import_cost < 0.0)
73         return 0;
74
75     dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
76     if (imex == IMPORT)
77         path_cost = import_cost;
78     else {
79         dcc = sector_mcost(dist, MOB_MOVE);
80         if (import_cost < 0.0 || dcc < 0.0) {
81             if (sp->sct_own && sp->sct_uid != dist->sct_uid)
82                 wu(0, sp->sct_own, "No path to dist sector for %s\n",
83                    ownxy(sp));
84             return 0;
85         }
86         path_cost = import_cost - sector_mcost(sp, MOB_MOVE) + dcc;
87     }
88
89     dist_packing = dist->sct_effic >= 60 ? dchr[dist->sct_type].d_pkg : IPKG;
90     sect_packing = sp->sct_effic   >= 60 ? dchr[sp->sct_type].d_pkg : IPKG;
91
92     DPRINTF("distribute: %d,%d to %d,%d pathcost %g\n",
93             sp->sct_x, sp->sct_y, sp->sct_dist_x, sp->sct_dist_y,
94             path_cost);
95
96     lplague = rplague = changed = 0;
97     for (item = I_NONE + 1; item <= I_MAX; item++) {
98         if (sp->sct_dist[item] == 0)
99             continue;
100         ip = &ichr[item];
101         thresh = sp->sct_dist[item];
102         /*
103          * calculate costs for importing and exporting.
104          * the mob bonus is because delivering straight through
105          * to the dist sect is cheaper than stopping at each
106          * sector along the way (processor-timewise)
107          */
108         amt_sect = sp->sct_item[item];
109         amt_dist = dist->sct_item[item];
110         diff = amt_sect - thresh;
111         if (item == I_CIVIL)
112             if (sp->sct_own != sp->sct_oldown)
113                 continue;
114         if (item == I_CIVIL)
115             if (dist->sct_own != dist->sct_oldown)
116                 continue;
117         if (diff < 0) {
118             if (imex != IMPORT)
119                 continue;
120
121             if (!military_control(dist))
122                 continue;
123
124             diff = -diff;
125             /*
126              * import.
127              * don't import if no mobility.
128              * check to make sure have enough mobility in the
129              * dist sector to import what we need.
130              */
131             if (dist->sct_mobil <= 0)
132                 continue;
133             amt = diff;
134             /* make sure not to abandon the sector */
135             if (item == I_CIVIL ||
136                 (item == I_MILIT && dist->sct_item[I_CIVIL] == 0))
137                 amt_dist--;
138
139             if (amt_dist < amt) {
140                 amt = amt_dist;
141                 if (amt_dist <= 0)
142                     continue;
143             }
144             pack = ip->i_pkg[dist_packing];
145             mcost = path_cost / pack * ip->i_lbs / IMPORT_BONUS;
146             if (dist->sct_mobil < mcost * amt)
147                 amt = dist->sct_mobil / mcost;
148
149             lplague++;
150             dist->sct_item[item] -= amt;
151             changed++;
152             dist->sct_mobil -= (int)(mcost * amt);
153             sp->sct_item[item] += amt;
154         } else {
155             if (imex != EXPORT)
156                 continue;
157             if (!military_control(sp))
158                 continue;
159             if ((item == I_CIVIL) && (sp->sct_work < 100))
160                 continue;
161             if ((item == I_CIVIL) && (sp->sct_own != sp->sct_oldown))
162                 continue;
163             /*
164              * export.
165              * don't export if no mobility. check to make sure we
166              * have mobility enough to do the right thing.
167              * also make sure that there's enough space in the
168              * target sector to hold the required amt.
169              */
170             if (sp->sct_mobil <= 0)
171                 continue;
172             amt = diff;
173             if (amt > amt_sect)
174                 amt = amt_sect;
175             pack = MAX(ip->i_pkg[sect_packing], ip->i_pkg[dist_packing]);
176             mcost = path_cost / pack * ip->i_lbs / EXPORT_BONUS;
177             if (sp->sct_mobil < mcost * amt)
178                 amt = sp->sct_mobil / mcost;
179             if (amt > ITEM_MAX - amt_dist)
180                 amt = ITEM_MAX - amt_dist;
181             if (amt == 0)
182                 continue;
183
184             rplague++;
185             sp->sct_item[item] -= amt;
186             changed++;
187             sp->sct_mobil -= (int)(mcost * amt);
188             dist->sct_item[item] += amt;
189         }
190     }
191
192     if (lplague) {
193         lplague = dist->sct_pstage;
194         if (lplague == PLG_INFECT && sp->sct_pstage == PLG_HEALTHY)
195             sp->sct_pstage = PLG_EXPOSED;
196     }
197
198     if (rplague) {
199         rplague = sp->sct_pstage;
200         if (rplague == PLG_INFECT && dist->sct_pstage == PLG_HEALTHY)
201             dist->sct_pstage = PLG_EXPOSED;
202     }
203
204     return changed;
205 }