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