]> git.pond.sub.org Git - empserver/blob - src/lib/update/nav_util.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / update / nav_util.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  *  nav_util.c: Utilities for autonav and sail
29  * 
30  *  Known contributors to this file:
31  *     
32  */
33
34 #include <config.h>
35
36 #include "misc.h"
37
38 #include <ctype.h>
39 #include "plague.h"
40 #include "ship.h"
41 #include "plane.h"
42 #include "land.h"
43 #include "sect.h"
44 #include "xy.h"
45 #include "nsc.h"
46 #include "nat.h"
47 #include "path.h"
48 #include "file.h"
49 #include "item.h"
50 #include "optlist.h"
51 #include "player.h"
52 #include "update.h"
53 #include "subs.h"
54 #include "common.h"
55 #include "gen.h"
56
57 /* load a specific ship given its 
58  * location and what field to modify.
59  * new autonav code
60  * Chad Zabel 6/1/94 
61  */
62 int
63 load_it(struct shpstr *sp, struct sctstr *psect, int i)
64 {
65     int shipown, amount, ship_amt, sect_amt;
66     int abs_max, max_amt, transfer;
67     i_type comm;
68     struct mchrstr *vship;
69
70     amount = sp->shp_lend[i];
71     shipown = sp->shp_own;
72     comm = sp->shp_tend[i];
73     if (CANT_HAPPEN(comm <= I_NONE || comm > I_MAX))
74         return 0;
75
76     ship_amt = sp->shp_item[comm];
77     sect_amt = psect->sct_item[comm];
78
79     /* check for disloyal civilians */
80     if (psect->sct_oldown != shipown && comm == I_CIVIL) {
81         wu(0, shipown,
82            "Ship #%d - unable to load disloyal civilians at %s.",
83            sp->shp_uid, xyas(psect->sct_x, psect->sct_y, psect->sct_own));
84         return 0;
85     }
86     if (comm == I_CIVIL || comm == I_MILIT)
87         sect_amt--;             /* leave 1 civ or mil to hold the sector. */
88     vship = &mchr[(int)sp->shp_type];
89     abs_max = max_amt = vship->m_item[comm];
90
91     if (!abs_max)
92         return 0;               /* can't load the ship, skip to the end. */
93
94     max_amt = MIN(sect_amt, max_amt - ship_amt);
95     if (max_amt <= 0 && (ship_amt != abs_max)) {
96         sp->shp_autonav |= AN_LOADING;
97         return 0;
98     }
99
100
101     transfer = amount - ship_amt;
102     if (transfer > sect_amt) {  /* not enough in the   */
103         transfer = sect_amt;    /* sector to fill the  */
104         sp->shp_autonav |= AN_LOADING;  /* ship, set load flag */
105     }
106     if (ship_amt + transfer > abs_max)  /* Do not load more    */
107         transfer = abs_max - ship_amt;  /* then the max alowed */
108     /* on the ship.        */
109
110     if (transfer <= 0)
111         return 0;               /* nothing to move */
112
113
114     sp->shp_item[comm] = ship_amt + transfer;
115     if (comm == I_CIVIL || comm == I_MILIT)
116         sect_amt++;             /*adjustment */
117     psect->sct_item[comm] = sect_amt - transfer;
118
119     /* deal with the plague */
120     if (psect->sct_pstage == PLG_INFECT && sp->shp_pstage == PLG_HEALTHY)
121         sp->shp_pstage = PLG_EXPOSED;
122     if (sp->shp_pstage == PLG_INFECT && psect->sct_pstage == PLG_HEALTHY)
123         psect->sct_pstage = PLG_EXPOSED;
124
125     return 1;                   /* we did someloading return 1 to keep */
126     /* our loop happy in nav_ship()        */
127
128 }
129
130 /* unload_it 
131  * A guess alot of this looks like load_it but because of its location
132  * in the autonav code I had to split the 2 procedures up.
133  * unload_it dumps all the goods from the ship to the harbor.
134  * ONLY goods in the trade fields will be unloaded.
135  * new autonav code
136  * Chad Zabel 6/1/94  
137  */
138 void
139 unload_it(struct shpstr *sp)
140 {
141     struct sctstr *sectp;
142     int i;
143     int landowner;
144     int shipown;
145     i_type comm;
146     int sect_amt;
147     int ship_amt;
148     int max_amt;
149
150     sectp = getsectp(sp->shp_x, sp->shp_y);
151
152     landowner = sectp->sct_own;
153     shipown = sp->shp_own;
154
155     for (i = 0; i < TMAX; ++i) {
156         if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0)
157             continue;
158         if (landowner == 0)
159             continue;
160         if (sectp->sct_type != SCT_HARBR)
161             continue;
162
163         comm = sp->shp_tend[i];
164         if (CANT_HAPPEN(comm <= I_NONE || comm > I_MAX))
165             continue;
166         ship_amt = sp->shp_item[comm];
167         sect_amt = sectp->sct_item[comm];
168
169         /* check for disloyal civilians */
170         if (sectp->sct_oldown != shipown && comm == I_CIVIL) {
171             wu(0, sp->shp_own,
172                "Ship #%d - unable to unload civilians into a disloyal sector at %s.",
173                sp->shp_uid, xyas(sectp->sct_x, sectp->sct_y,
174                                  sectp->sct_own));
175             continue;
176         }
177         if (comm == I_CIVIL)
178             ship_amt--;         /* This leaves 1 civs on board the ship */
179
180         max_amt = MIN(ship_amt, ITEM_MAX - sect_amt);
181         if (max_amt <= 0)
182             continue;
183
184         sp->shp_item[comm] = ship_amt - max_amt;
185         sectp->sct_item[comm] = sect_amt + max_amt;
186
187         if (sectp->sct_pstage == PLG_INFECT && sp->shp_pstage == PLG_HEALTHY)
188             sp->shp_pstage = PLG_EXPOSED;
189         if (sp->shp_pstage == PLG_INFECT && sectp->sct_pstage == PLG_HEALTHY)
190             sectp->sct_pstage = PLG_EXPOSED;
191     }
192 }
193
194 /* auto_fuel_ship 
195  * Assume a check for fuel=0 has already been made and passed.  
196  * Try to fill a ship using petro. and then oil.            
197  * new autonav code.
198  * This should be merged with the fuel command someday. 
199  * Chad Zabel 6/1/94
200  */
201
202 void
203 auto_fuel_ship(struct shpstr *sp)
204 {
205     double d;
206     int totalfuel = 0;
207     int need;
208     int maxfuel;
209     int newfuel = 0;
210     int add_fuel = 0;
211
212     if (opt_FUEL == 0)
213         return;
214     getship(sp->shp_uid, sp);   /* refresh */
215     /* fill with petro */
216     maxfuel = mchr[(int)sp->shp_type].m_fuelc;
217     d = (double)maxfuel / 5.0;
218     if ((d - (int)d > 0.0))
219         d++;
220     need = (int)d;
221
222     newfuel = supply_commod(sp->shp_own, sp->shp_x,
223                             sp->shp_y, I_PETROL, need);
224     add_fuel += newfuel * 5;
225     if (add_fuel > maxfuel)
226         add_fuel = maxfuel;
227     sp->shp_fuel += add_fuel;
228     totalfuel += add_fuel;
229
230     if (totalfuel == maxfuel) {
231         putship(sp->shp_uid, sp);
232         return;                 /* the ship is full */
233     }
234     add_fuel = 0;
235     /* fill with oil */
236     d = (double)(maxfuel - totalfuel) / 50.0;
237     if ((d - (int)d > 0.0))
238         d++;
239     need = (int)d;
240
241     newfuel = supply_commod(sp->shp_own, sp->shp_x,
242                             sp->shp_y, I_OIL, need);
243     add_fuel = newfuel * 50;
244     if (add_fuel > maxfuel)
245         add_fuel = maxfuel;
246     sp->shp_fuel += add_fuel;
247     putship(sp->shp_uid, sp);
248 }