]> git.pond.sub.org Git - empserver/blob - src/lib/update/distribute.c
Import of Empire 4.2.12
[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
49 int
50 dodistribute(struct sctstr *sp, int imex, s_char *path, double dist_i_cost, double dist_e_cost)
51           /* import or export? */
52 {
53         struct  sctstr *getdistsp();
54         float   distpathcost();
55         struct ichrstr *ip;
56         struct sctstr *dist;
57         int     amt;
58         int     thresh;
59         int     amt_dist;
60         int     amt_sect;
61         int     packing;
62         float   imcost;
63         float   excost;
64         int     dist_packing;
65         int     diff;
66         int     item;
67         int     dists[I_MAX+1];
68         int     remote[I_MAX+1];
69         int     local[I_MAX+1];
70         int     changed;
71         int     rplague;
72         int     lplague;
73
74         getvec(VT_ITEM, local, (s_char *)sp, EF_SECTOR);
75         if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
76                 return 0;
77
78         if (getvec(VT_DIST, dists, (s_char *)sp, EF_SECTOR) <= 0)
79                 return 0;
80
81         if (path == (s_char *)0){
82                 if (sp->sct_own != 0) {
83                         if (imex == EXPORT) /* only want this once */
84                         wu(0,sp->sct_own,"No path to dist sector for %s\n",
85                                 ownxy(sp));
86                 }
87                 return 0;
88         }
89
90         dist = getsectp(sp->sct_dist_x,sp->sct_dist_y);
91         if (dist->sct_effic >= 60)
92                 dist_packing = dchr[dist->sct_type].d_pkg;
93         else
94                 dist_packing = NPKG; /* No packing */
95         
96         if (sp->sct_effic >= 60)
97                 packing = dchr[sp->sct_type].d_pkg;
98         else
99                 packing = NPKG; /* No packing */
100
101         if ((dist->sct_effic >= 60) && dchr[dist->sct_type].d_pkg == WPKG)
102                 packing = dchr[dist->sct_type].d_pkg;
103
104         getvec(VT_ITEM, remote, (s_char *)dist, EF_SECTOR);
105         lplague = rplague = changed = 0;
106         for (item = 1; item < I_MAX+1; item++) {
107                 if (dists[item] == 0)
108                         continue;
109                 ip = &ichr[item];
110                 thresh = dists[item];
111                 /*
112                  * calculate costs for importing and exporting.
113                  * the div 10.0 is because delivering straight through
114                  * to the dist sect is cheaper than stopping at each
115                  * sector along the way (processor-timewise)
116                  */
117                 excost = (dist_e_cost/ip->i_pkg[packing] * ip->i_lbs) / 10.0;
118                 imcost = (dist_i_cost/ip->i_pkg[dist_packing] *ip->i_lbs)/10.0;
119                 amt_sect = local[item];
120                 amt_dist = remote[item];
121                 diff = amt_sect - thresh;
122                 if (item == I_CIVIL)
123                         if (sp->sct_own != sp->sct_oldown)
124                                 continue;
125                 if (item == I_CIVIL)
126                         if (dist->sct_own != dist->sct_oldown)
127                                 continue;
128                 if (diff < 0){
129                         if (imex != IMPORT)
130                                 continue;
131
132                         if (!military_control(dist))
133                                 continue;
134
135                         diff = -diff;
136                         /*
137                          * import.
138                          * don't import if no mobility.
139                          * check to make sure have enough mobility in the
140                          * dist sector to import what we need.
141                          */
142                         if (dist->sct_mobil <= 0) {
143                                 /*logerror("  dist mobil < 0");*/
144                                 continue;
145                         }
146                         amt = diff;
147                         if (item == I_CIVIL)
148                                 amt_dist--;     /* Don't send your last civ */
149
150                         if (amt_dist < amt) {
151                                 amt = amt_dist;
152                                 if (amt_dist == 0)
153                                         continue;
154                         }
155                         if (dist->sct_mobil < imcost * amt)
156                                 amt = dist->sct_mobil / imcost;
157
158                         lplague++;
159                         /* XXX replace with vector assign and putvec() */
160                         remote[item] -= amt;
161                         changed++;
162                         dist->sct_mobil -= (int) (imcost * amt);
163                         local[item] += amt;
164                 } else {
165                         if (imex != EXPORT)
166                                 continue;
167                         if (!military_control(sp))
168                                 continue;
169                         if ((item == I_CIVIL)&&(sp->sct_work < 100))
170                                 continue;
171                         if ((item == I_CIVIL)&&(sp->sct_own != sp->sct_oldown))
172                                 continue;
173                         /*
174                          * export.
175                          * don't export if no mobility. check to make sure we
176                          * have mobility enough to do the right thing.
177                          * also make sure that there's enough space in the
178                          * target sector to hold the required amt.
179                          */
180                         if (sp->sct_mobil <= 0) {
181                                 /*logerror("  sp mob is zero");*/
182                                 continue;
183                         }
184                         amt = diff;
185                         if (amt > amt_sect)
186                                 amt = amt_sect;
187                         if (sp->sct_mobil < excost * amt)
188                                 amt = sp->sct_mobil / excost;
189                         if (amt + amt_dist > 9999)
190                                 amt = 9999 - amt_dist;
191                         if (amt == 0)
192                                 continue;
193                         /* XXX replace with vector assign and putvec() */
194
195                         rplague++;
196                         local[item] -= amt;
197                         changed++;
198                         sp->sct_mobil -= (int) (excost * amt);
199                         remote[item] += amt;
200                 }
201         }
202         putvec(VT_ITEM, remote, (s_char *)dist, EF_SECTOR);
203         putvec(VT_ITEM, local, (s_char *)sp, EF_SECTOR);
204
205         if (lplague) {
206           lplague = getvar(V_PSTAGE, (s_char *)dist, EF_SECTOR);
207           if (lplague == PLG_INFECT &&
208               getvar(V_PSTAGE, (s_char *)sp, EF_SECTOR) == PLG_HEALTHY) {
209             putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)sp, EF_SECTOR);
210           }
211         }
212
213         if (rplague) {
214           rplague = getvar(V_PSTAGE, (s_char *)sp, EF_SECTOR);
215           if (rplague == PLG_INFECT &&
216               getvar(V_PSTAGE, (s_char *)dist, EF_SECTOR) == PLG_HEALTHY) {
217             putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)dist, EF_SECTOR);
218           }
219         }
220
221         return changed;
222 }
223