]> git.pond.sub.org Git - empserver/blob - src/lib/update/distribute.c
Update copyright notice.
[empserver] / src / lib / update / distribute.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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 #define EXPORT_BONUS 10.0
51 #define IMPORT_BONUS 10.0
52
53 #ifndef MAX
54 #define MAX(x,y) ((x) < (y) ? (y) : (x))
55 #endif
56
57 int
58 dodistribute(struct sctstr *sp, int imex, s_char *path, double dist_i_cost,
59              double dist_e_cost)
60           /* import or export? */
61 {
62     struct ichrstr *ip;
63     struct sctstr *dist;
64     int amt;
65     int thresh;
66     int amt_dist;
67     int amt_sect;
68     i_packing sect_packing, dist_packing;
69     int pack;
70     double mcost;
71     int diff;
72     int item;
73     int changed;
74     int rplague;
75     int lplague;
76
77     if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
78         return 0;
79
80     if (path == (s_char *)0) {
81         if (sp->sct_own != 0) {
82             if (imex == EXPORT) /* only want this once */
83                 wu(0, sp->sct_own, "No path to dist sector for %s\n",
84                    ownxy(sp));
85         }
86         return 0;
87     }
88
89     dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
90     dist_packing = dist->sct_effic >= 60 ? dchr[dist->sct_type].d_pkg : IPKG;
91     sect_packing = sp->sct_effic   >= 60 ? dchr[sp->sct_type].d_pkg : IPKG;
92
93     lplague = rplague = changed = 0;
94     for (item = 1; item < I_MAX + 1; item++) {
95         if (sp->sct_dist[item] == 0)
96             continue;
97         ip = &ichr[item];
98         thresh = sp->sct_dist[item];
99         /*
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)
104          */
105         amt_sect = sp->sct_item[item];
106         amt_dist = dist->sct_item[item];
107         diff = amt_sect - thresh;
108         if (item == I_CIVIL)
109             if (sp->sct_own != sp->sct_oldown)
110                 continue;
111         if (item == I_CIVIL)
112             if (dist->sct_own != dist->sct_oldown)
113                 continue;
114         if (diff < 0) {
115             if (imex != IMPORT)
116                 continue;
117
118             if (!military_control(dist))
119                 continue;
120
121             diff = -diff;
122             /*
123              * import.
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.
127              */
128             if (dist->sct_mobil <= 0) {
129                 /*logerror("  dist mobil < 0"); */
130                 continue;
131             }
132             amt = diff;
133             if (item == I_CIVIL)
134                 amt_dist--;     /* Don't send your last civ */
135
136             if (amt_dist < amt) {
137                 amt = amt_dist;
138                 if (amt_dist == 0)
139                     continue;
140             }
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;
145
146             lplague++;
147             dist->sct_item[item] -= amt;
148             changed++;
149             dist->sct_mobil -= (int)(mcost * amt);
150             sp->sct_item[item] += amt;
151         } else {
152             if (imex != EXPORT)
153                 continue;
154             if (!military_control(sp))
155                 continue;
156             if ((item == I_CIVIL) && (sp->sct_work < 100))
157                 continue;
158             if ((item == I_CIVIL) && (sp->sct_own != sp->sct_oldown))
159                 continue;
160             /*
161              * export.
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.
166              */
167             if (sp->sct_mobil <= 0) {
168                 /*logerror("  sp mob is zero"); */
169                 continue;
170             }
171             amt = diff;
172             if (amt > amt_sect)
173                 amt = amt_sect;
174             pack = MAX(ip->i_pkg[sect_packing], ip->i_pkg[dist_packing]);
175             mcost = dist_e_cost / pack * ip->i_lbs / EXPORT_BONUS;
176             if (sp->sct_mobil < mcost * amt)
177                 amt = sp->sct_mobil / mcost;
178             if (amt > ITEM_MAX - amt_dist)
179                 amt = ITEM_MAX - amt_dist;
180             if (amt == 0)
181                 continue;
182
183             rplague++;
184             sp->sct_item[item] -= amt;
185             changed++;
186             sp->sct_mobil -= (int)(mcost * amt);
187             dist->sct_item[item] += amt;
188         }
189     }
190
191     if (lplague) {
192         lplague = dist->sct_pstage;
193         if (lplague == PLG_INFECT && sp->sct_pstage == PLG_HEALTHY)
194             sp->sct_pstage = PLG_EXPOSED;
195     }
196
197     if (rplague) {
198         rplague = sp->sct_pstage;
199         if (rplague == PLG_INFECT && dist->sct_pstage == PLG_HEALTHY)
200             dist->sct_pstage = PLG_EXPOSED;
201     }
202
203     return changed;
204 }