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
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.
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.
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
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.
28 * distribute.c: Do distribution to sectors
30 * Known contributors to this file:
37 #include "distribute.h"
41 #include "prototypes.h"
44 #ifdef DISTRIBUTE_DEBUG
45 #define DPRINTF(fmt, ...) ((void)printf(fmt , ## __VA_ARGS__))
47 #define DPRINTF(fmt, ...) ((void)0)
50 #define EXPORT_BONUS 10.0
51 #define IMPORT_BONUS 10.0
54 dodistribute(struct sctstr *sp, int imex, char *path, double dist_i_cost,
56 /* import or export? */
64 enum i_packing sect_packing, dist_packing;
73 if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
77 if (sp->sct_own != 0) {
78 if (imex == EXPORT) /* only want this once */
79 wu(0, sp->sct_own, "No path to dist sector for %s\n",
85 dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
86 dist_packing = dist->sct_effic >= 60 ? dchr[dist->sct_type].d_pkg : IPKG;
87 sect_packing = sp->sct_effic >= 60 ? dchr[sp->sct_type].d_pkg : IPKG;
89 DPRINTF("distribute: %d,%d to %d,%d pathcost %g\n",
90 sp->sct_x, sp->sct_y, sp->sct_dist_x, sp->sct_dist_y,
91 imex == IMPORT ? dist_i_cost : dist_e_cost);
93 lplague = rplague = changed = 0;
94 for (item = I_NONE + 1; item <= I_MAX; item++) {
95 if (sp->sct_dist[item] == 0)
98 thresh = sp->sct_dist[item];
100 * calculate costs for importing and exporting.
101 * the mob bonus is because delivering straight through
102 * to the dist sect is cheaper than stopping at each
103 * sector along the way (processor-timewise)
105 amt_sect = sp->sct_item[item];
106 amt_dist = dist->sct_item[item];
107 diff = amt_sect - thresh;
109 if (sp->sct_own != sp->sct_oldown)
112 if (dist->sct_own != dist->sct_oldown)
118 if (!military_control(dist))
124 * don't import if no mobility.
125 * check to make sure have enough mobility in the
126 * dist sector to import what we need.
128 if (dist->sct_mobil <= 0)
131 /* make sure not to abandon the sector */
132 if (item == I_CIVIL ||
133 (item == I_MILIT && dist->sct_item[I_CIVIL] == 0))
136 if (amt_dist < amt) {
141 pack = ip->i_pkg[dist_packing];
142 mcost = dist_i_cost / pack * ip->i_lbs / IMPORT_BONUS;
143 if (dist->sct_mobil < mcost * amt)
144 amt = dist->sct_mobil / mcost;
147 dist->sct_item[item] -= amt;
149 dist->sct_mobil -= (int)(mcost * amt);
150 sp->sct_item[item] += amt;
154 if (!military_control(sp))
156 if ((item == I_CIVIL) && (sp->sct_work < 100))
158 if ((item == I_CIVIL) && (sp->sct_own != sp->sct_oldown))
162 * don't export if no mobility. check to make sure we
163 * have mobility enough to do the right thing.
164 * also make sure that there's enough space in the
165 * target sector to hold the required amt.
167 if (sp->sct_mobil <= 0)
172 pack = MAX(ip->i_pkg[sect_packing], ip->i_pkg[dist_packing]);
173 mcost = dist_e_cost / pack * ip->i_lbs / EXPORT_BONUS;
174 if (sp->sct_mobil < mcost * amt)
175 amt = sp->sct_mobil / mcost;
176 if (amt > ITEM_MAX - amt_dist)
177 amt = ITEM_MAX - amt_dist;
182 sp->sct_item[item] -= amt;
184 sp->sct_mobil -= (int)(mcost * amt);
185 dist->sct_item[item] += amt;
190 lplague = dist->sct_pstage;
191 if (lplague == PLG_INFECT && sp->sct_pstage == PLG_HEALTHY)
192 sp->sct_pstage = PLG_EXPOSED;
196 rplague = sp->sct_pstage;
197 if (rplague == PLG_INFECT && dist->sct_pstage == PLG_HEALTHY)
198 dist->sct_pstage = PLG_EXPOSED;