2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * distribute.c: Do distribution to sectors
30 * Known contributors to this file:
44 #include "distribute.h"
50 dodistribute(struct sctstr *sp, int imex, s_char *path, double dist_i_cost,
52 /* import or export? */
54 struct sctstr *getdistsp();
69 int remote[I_MAX + 1];
75 getvec(VT_ITEM, local, (s_char *)sp, EF_SECTOR);
76 if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
79 if (getvec(VT_DIST, dists, (s_char *)sp, EF_SECTOR) <= 0)
82 if (path == (s_char *)0) {
83 if (sp->sct_own != 0) {
84 if (imex == EXPORT) /* only want this once */
85 wu(0, sp->sct_own, "No path to dist sector for %s\n",
91 dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
92 if (dist->sct_effic >= 60)
93 dist_packing = dchr[dist->sct_type].d_pkg;
95 dist_packing = NPKG; /* No packing */
97 if (sp->sct_effic >= 60)
98 packing = dchr[sp->sct_type].d_pkg;
100 packing = NPKG; /* No packing */
102 if ((dist->sct_effic >= 60) && dchr[dist->sct_type].d_pkg == WPKG)
103 packing = dchr[dist->sct_type].d_pkg;
105 getvec(VT_ITEM, remote, (s_char *)dist, EF_SECTOR);
106 lplague = rplague = changed = 0;
107 for (item = 1; item < I_MAX + 1; item++) {
108 if (dists[item] == 0)
111 thresh = dists[item];
113 * calculate costs for importing and exporting.
114 * the div 10.0 is because delivering straight through
115 * to the dist sect is cheaper than stopping at each
116 * sector along the way (processor-timewise)
118 excost = (dist_e_cost / ip->i_pkg[packing] * ip->i_lbs) / 10.0;
120 (dist_i_cost / ip->i_pkg[dist_packing] * ip->i_lbs) / 10.0;
121 amt_sect = local[item];
122 amt_dist = remote[item];
123 diff = amt_sect - thresh;
125 if (sp->sct_own != sp->sct_oldown)
128 if (dist->sct_own != dist->sct_oldown)
134 if (!military_control(dist))
140 * don't import if no mobility.
141 * check to make sure have enough mobility in the
142 * dist sector to import what we need.
144 if (dist->sct_mobil <= 0) {
145 /*logerror(" dist mobil < 0"); */
150 amt_dist--; /* Don't send your last civ */
152 if (amt_dist < amt) {
157 if (dist->sct_mobil < imcost * amt)
158 amt = dist->sct_mobil / imcost;
161 /* XXX replace with vector assign and putvec() */
164 dist->sct_mobil -= (int)(imcost * amt);
169 if (!military_control(sp))
171 if ((item == I_CIVIL) && (sp->sct_work < 100))
173 if ((item == I_CIVIL) && (sp->sct_own != sp->sct_oldown))
177 * don't export if no mobility. check to make sure we
178 * have mobility enough to do the right thing.
179 * also make sure that there's enough space in the
180 * target sector to hold the required amt.
182 if (sp->sct_mobil <= 0) {
183 /*logerror(" sp mob is zero"); */
189 if (sp->sct_mobil < excost * amt)
190 amt = sp->sct_mobil / excost;
191 if (amt + amt_dist > 9999)
192 amt = 9999 - amt_dist;
195 /* XXX replace with vector assign and putvec() */
200 sp->sct_mobil -= (int)(excost * amt);
204 putvec(VT_ITEM, remote, (s_char *)dist, EF_SECTOR);
205 putvec(VT_ITEM, local, (s_char *)sp, EF_SECTOR);
208 lplague = getvar(V_PSTAGE, (s_char *)dist, EF_SECTOR);
209 if (lplague == PLG_INFECT &&
210 getvar(V_PSTAGE, (s_char *)sp, EF_SECTOR) == PLG_HEALTHY) {
211 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)sp, EF_SECTOR);
216 rplague = getvar(V_PSTAGE, (s_char *)sp, EF_SECTOR);
217 if (rplague == PLG_INFECT &&
218 getvar(V_PSTAGE, (s_char *)dist, EF_SECTOR) == PLG_HEALTHY) {
219 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)dist, EF_SECTOR);