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