]> 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-2008, 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 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.
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 <config.h>
36
37 #include "distribute.h"
38 #include "item.h"
39 #include "path.h"
40 #include "plague.h"
41 #include "prototypes.h"
42 #include "update.h"
43
44 #define EXPORT_BONUS 10.0
45 #define IMPORT_BONUS 10.0
46
47 int
48 dodistribute(struct sctstr *sp, int imex, char *path, double dist_i_cost,
49              double dist_e_cost)
50           /* import or export? */
51 {
52     struct ichrstr *ip;
53     struct sctstr *dist;
54     int amt;
55     int thresh;
56     int amt_dist;
57     int amt_sect;
58     i_packing sect_packing, dist_packing;
59     int pack;
60     double mcost;
61     int diff;
62     i_type item;
63     int changed;
64     int rplague;
65     int lplague;
66
67     if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
68         return 0;
69
70     if (!path) {
71         if (sp->sct_own != 0) {
72             if (imex == EXPORT) /* only want this once */
73                 wu(0, sp->sct_own, "No path to dist sector for %s\n",
74                    ownxy(sp));
75         }
76         return 0;
77     }
78
79     dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
80     dist_packing = dist->sct_effic >= 60 ? dchr[dist->sct_type].d_pkg : IPKG;
81     sect_packing = sp->sct_effic   >= 60 ? dchr[sp->sct_type].d_pkg : IPKG;
82
83     lplague = rplague = changed = 0;
84     for (item = I_NONE + 1; item <= I_MAX; item++) {
85         if (sp->sct_dist[item] == 0)
86             continue;
87         ip = &ichr[item];
88         thresh = sp->sct_dist[item];
89         /*
90          * calculate costs for importing and exporting.
91          * the mob bonus is because delivering straight through
92          * to the dist sect is cheaper than stopping at each
93          * sector along the way (processor-timewise)
94          */
95         amt_sect = sp->sct_item[item];
96         amt_dist = dist->sct_item[item];
97         diff = amt_sect - thresh;
98         if (item == I_CIVIL)
99             if (sp->sct_own != sp->sct_oldown)
100                 continue;
101         if (item == I_CIVIL)
102             if (dist->sct_own != dist->sct_oldown)
103                 continue;
104         if (diff < 0) {
105             if (imex != IMPORT)
106                 continue;
107
108             if (!military_control(dist))
109                 continue;
110
111             diff = -diff;
112             /*
113              * import.
114              * don't import if no mobility.
115              * check to make sure have enough mobility in the
116              * dist sector to import what we need.
117              */
118             if (dist->sct_mobil <= 0) {
119                 /*logerror("  dist mobil < 0"); */
120                 continue;
121             }
122             amt = diff;
123             if (item == I_CIVIL)
124                 amt_dist--;     /* Don't send your last civ */
125
126             if (amt_dist < amt) {
127                 amt = amt_dist;
128                 if (amt_dist <= 0)
129                     continue;
130             }
131             pack = ip->i_pkg[dist_packing];
132             mcost = dist_i_cost / pack * ip->i_lbs / IMPORT_BONUS;
133             if (dist->sct_mobil < mcost * amt)
134                 amt = dist->sct_mobil / mcost;
135
136             lplague++;
137             dist->sct_item[item] -= amt;
138             changed++;
139             dist->sct_mobil -= (int)(mcost * amt);
140             sp->sct_item[item] += amt;
141         } else {
142             if (imex != EXPORT)
143                 continue;
144             if (!military_control(sp))
145                 continue;
146             if ((item == I_CIVIL) && (sp->sct_work < 100))
147                 continue;
148             if ((item == I_CIVIL) && (sp->sct_own != sp->sct_oldown))
149                 continue;
150             /*
151              * export.
152              * don't export if no mobility. check to make sure we
153              * have mobility enough to do the right thing.
154              * also make sure that there's enough space in the
155              * target sector to hold the required amt.
156              */
157             if (sp->sct_mobil <= 0) {
158                 /*logerror("  sp mob is zero"); */
159                 continue;
160             }
161             amt = diff;
162             if (amt > amt_sect)
163                 amt = amt_sect;
164             pack = MAX(ip->i_pkg[sect_packing], ip->i_pkg[dist_packing]);
165             mcost = dist_e_cost / pack * ip->i_lbs / EXPORT_BONUS;
166             if (sp->sct_mobil < mcost * amt)
167                 amt = sp->sct_mobil / mcost;
168             if (amt > ITEM_MAX - amt_dist)
169                 amt = ITEM_MAX - amt_dist;
170             if (amt == 0)
171                 continue;
172
173             rplague++;
174             sp->sct_item[item] -= amt;
175             changed++;
176             sp->sct_mobil -= (int)(mcost * amt);
177             dist->sct_item[item] += amt;
178         }
179     }
180
181     if (lplague) {
182         lplague = dist->sct_pstage;
183         if (lplague == PLG_INFECT && sp->sct_pstage == PLG_HEALTHY)
184             sp->sct_pstage = PLG_EXPOSED;
185     }
186
187     if (rplague) {
188         rplague = sp->sct_pstage;
189         if (rplague == PLG_INFECT && dist->sct_pstage == PLG_HEALTHY)
190             dist->sct_pstage = PLG_EXPOSED;
191     }
192
193     return changed;
194 }