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