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