]> git.pond.sub.org Git - empserver/blob - src/lib/update/distribute.c
ef147b9eaaf5aa18fc7fb43a7d63a5b174f02aaa
[empserver] / src / lib / update / distribute.c
1 /*
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
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 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.
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 <stdlib.h>
36 #include "misc.h"
37 #include "var.h"
38 #include "nat.h"
39 #include "sect.h"
40 #include "item.h"
41 #include "xy.h"
42 #include "path.h"
43 #include "file.h"
44 #include "distribute.h"
45 #include "update.h"
46 #include "subs.h"
47 #include "common.h"
48 #include "prototypes.h"
49
50 int
51 dodistribute(struct sctstr *sp, int imex, s_char *path, double dist_i_cost,
52              double dist_e_cost)
53           /* import or export? */
54 {
55     struct ichrstr *ip;
56     struct sctstr *dist;
57     int amt;
58     int thresh;
59     int amt_dist;
60     int amt_sect;
61     i_packing packing;
62     float imcost;
63     float excost;
64     i_packing dist_packing;
65     int diff;
66     int item;
67     int changed;
68     int rplague;
69     int lplague;
70
71     if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
72         return 0;
73
74     if (path == (s_char *)0) {
75         if (sp->sct_own != 0) {
76             if (imex == EXPORT) /* only want this once */
77                 wu(0, sp->sct_own, "No path to dist sector for %s\n",
78                    ownxy(sp));
79         }
80         return 0;
81     }
82
83     dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
84     if (dist->sct_effic >= 60)
85         dist_packing = dchr[dist->sct_type].d_pkg;
86     else
87         dist_packing = NPKG;    /* No packing */
88
89     if (sp->sct_effic >= 60)
90         packing = dchr[sp->sct_type].d_pkg;
91     else
92         packing = NPKG;         /* No packing */
93
94     if ((dist->sct_effic >= 60) && dchr[dist->sct_type].d_pkg == WPKG)
95         packing = dchr[dist->sct_type].d_pkg;
96
97     lplague = rplague = changed = 0;
98     for (item = 1; item < I_MAX + 1; 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 div 10.0 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         excost = (dist_e_cost / ip->i_pkg[packing] * ip->i_lbs) / 10.0;
110         imcost = (dist_i_cost / ip->i_pkg[dist_packing] * ip->i_lbs) / 10.0;
111         amt_sect = sp->sct_item[item];
112         amt_dist = dist->sct_item[item];
113         diff = amt_sect - thresh;
114         if (item == I_CIVIL)
115             if (sp->sct_own != sp->sct_oldown)
116                 continue;
117         if (item == I_CIVIL)
118             if (dist->sct_own != dist->sct_oldown)
119                 continue;
120         if (diff < 0) {
121             if (imex != IMPORT)
122                 continue;
123
124             if (!military_control(dist))
125                 continue;
126
127             diff = -diff;
128             /*
129              * import.
130              * don't import if no mobility.
131              * check to make sure have enough mobility in the
132              * dist sector to import what we need.
133              */
134             if (dist->sct_mobil <= 0) {
135                 /*logerror("  dist mobil < 0"); */
136                 continue;
137             }
138             amt = diff;
139             if (item == I_CIVIL)
140                 amt_dist--;     /* Don't send your last civ */
141
142             if (amt_dist < amt) {
143                 amt = amt_dist;
144                 if (amt_dist == 0)
145                     continue;
146             }
147             if (dist->sct_mobil < imcost * amt)
148                 amt = dist->sct_mobil / imcost;
149
150             lplague++;
151             /* XXX replace with vector assign and putvec() */
152             dist->sct_item[item] -= amt;
153             changed++;
154             dist->sct_mobil -= (int)(imcost * amt);
155             sp->sct_item[item] += amt;
156         } else {
157             if (imex != EXPORT)
158                 continue;
159             if (!military_control(sp))
160                 continue;
161             if ((item == I_CIVIL) && (sp->sct_work < 100))
162                 continue;
163             if ((item == I_CIVIL) && (sp->sct_own != sp->sct_oldown))
164                 continue;
165             /*
166              * export.
167              * don't export if no mobility. check to make sure we
168              * have mobility enough to do the right thing.
169              * also make sure that there's enough space in the
170              * target sector to hold the required amt.
171              */
172             if (sp->sct_mobil <= 0) {
173                 /*logerror("  sp mob is zero"); */
174                 continue;
175             }
176             amt = diff;
177             if (amt > amt_sect)
178                 amt = amt_sect;
179             if (sp->sct_mobil < excost * amt)
180                 amt = sp->sct_mobil / excost;
181             if (amt > ITEM_MAX - amt_dist)
182                 amt = ITEM_MAX - amt_dist;
183             if (amt == 0)
184                 continue;
185             /* XXX replace with vector assign and putvec() */
186
187             rplague++;
188             sp->sct_item[item] -= amt;
189             changed++;
190             sp->sct_mobil -= (int)(excost * amt);
191             dist->sct_item[item] += amt;
192         }
193     }
194
195     if (lplague) {
196         lplague = dist->sct_pstage;
197         if (lplague == PLG_INFECT && sp->sct_pstage == PLG_HEALTHY)
198             sp->sct_pstage = PLG_EXPOSED;
199     }
200
201     if (rplague) {
202         rplague = sp->sct_pstage;
203         if (rplague == PLG_INFECT && dist->sct_pstage == PLG_HEALTHY)
204             dist->sct_pstage = PLG_EXPOSED;
205     }
206
207     return changed;
208 }