]> git.pond.sub.org Git - empserver/blob - src/lib/update/nav_util.c
Supply prototypes where possible. This uncovered type errors with
[empserver] / src / lib / update / nav_util.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  *  nav_util.c: Utilities for autonav and sail
29  * 
30  *  Known contributors to this file:
31  *     
32  */
33
34 #include "misc.h"
35
36 #include <ctype.h>
37 #include "var.h"
38 #include "ship.h"
39 #include "plane.h"
40 #include "land.h"
41 #include "nuke.h"
42 #include "sect.h"
43 #include "news.h"
44 #include "xy.h"
45 #include "nsc.h"
46 #include "nat.h"
47 #include "path.h"
48 #include "deity.h"
49 #include "file.h"
50 #include "item.h"
51 #include "optlist.h"
52 #include "player.h"
53 #include "update.h"
54 #include "subs.h"
55 #include "common.h"
56 #include "gen.h"
57
58 /* Format a ship name */
59 int
60 check_nav(struct sctstr *sect)
61 {
62     switch (dchr[sect->sct_type].d_flg & 03) {
63     case NAVOK:
64         break;
65
66     case NAV_02:
67         if (sect->sct_effic < 2)
68             return CN_CONSTRUCTION;
69         break;
70     case NAV_60:
71         if (sect->sct_effic < 60)
72             return CN_CONSTRUCTION;
73         break;
74     default:
75         return CN_LANDLOCKED;
76     }
77     return CN_NAVIGABLE;
78 }
79
80 /* load a specific ship given its 
81  * location and what field to modify.
82  * new autonav code
83  * Chad Zabel 6/1/94 
84  */
85 int
86 load_it(register struct shpstr *sp, register struct sctstr *psect, int i)
87 {
88     int comm, shipown, amount, ship_amt, sect_amt,
89         abs_max, max_amt, transfer;
90     s_char item;
91     struct mchrstr *vship;
92
93     amount = sp->shp_lend[i];
94     shipown = sp->shp_own;
95     item = sp->shp_tend[i];     /* commodity */
96     comm = com_num(&item);
97
98     ship_amt = getvar(comm, (s_char *)sp, EF_SHIP);
99     sect_amt = getvar(comm, (s_char *)psect, EF_SECTOR);
100
101     /* check for disloyal civilians */
102     if (psect->sct_oldown != shipown && comm == V_CIVIL) {
103         wu(0, shipown,
104            "Ship #%d - unable to load disloyal civilians at %s.",
105            sp->shp_uid, xyas(psect->sct_x, psect->sct_y, psect->sct_own));
106         return 0;
107     }
108     if (comm == V_CIVIL || comm == V_MILIT)
109         sect_amt--;             /* leave 1 civ or mil to hold the sector. */
110     vship = &mchr[(int)sp->shp_type];
111     abs_max = max_amt = (vl_find(comm, vship->m_vtype,
112                                  vship->m_vamt, (int)vship->m_nv));
113
114     if (!abs_max)
115         return 0;               /* can't load the ship, skip to the end. */
116
117     max_amt = min(sect_amt, max_amt - ship_amt);
118     if (max_amt <= 0 && (ship_amt != abs_max)) {
119         sp->shp_autonav |= AN_LOADING;
120         return 0;
121     }
122
123
124     transfer = amount - ship_amt;
125     if (transfer > sect_amt) {  /* not enough in the   */
126         transfer = sect_amt;    /* sector to fill the  */
127         sp->shp_autonav |= AN_LOADING;  /* ship, set load flag */
128     }
129     if (ship_amt + transfer > abs_max)  /* Do not load more    */
130         transfer = abs_max - ship_amt;  /* then the max alowed */
131     /* on the ship.        */
132
133     if (transfer == 0)
134         return 0;               /* nothing to move */
135
136
137     putvar(comm, ship_amt + transfer, (s_char *)sp, EF_SHIP);
138     if (comm == V_CIVIL || comm == V_MILIT)
139         sect_amt++;             /*adjustment */
140     putvar(comm, sect_amt - transfer, (s_char *)psect, EF_SECTOR);
141
142     /* deal with the plague */
143     if (getvar(V_PSTAGE, (s_char *)psect, EF_SECTOR) == PLG_INFECT &&
144         getvar(V_PSTAGE, (s_char *)sp, EF_SHIP) == PLG_HEALTHY)
145         putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)sp, EF_SHIP);
146     if (getvar(V_PSTAGE, (s_char *)sp, EF_SHIP) == PLG_INFECT &&
147         getvar(V_PSTAGE, (s_char *)psect, EF_SECTOR) == PLG_HEALTHY)
148         putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)psect, EF_SECTOR);
149
150     return 1;                   /* we did someloading return 1 to keep */
151     /* our loop happy in nav_ship()        */
152
153 }
154
155 /* unload_it 
156  * A guess alot of this looks like load_it but because of its location
157  * in the autonav code I had to split the 2 procedures up.
158  * unload_it dumps all the goods from the ship to the harbor.
159  * ONLY goods in the trade fields will be unloaded.
160  * new autonav code
161  * Chad Zabel 6/1/94  
162  */
163 void
164 unload_it(register struct shpstr *sp)
165 {
166     struct sctstr *sectp;
167     s_char item;
168     int i;
169     int landowner;
170     int shipown;
171     int comm;
172     int sect_amt;
173     int ship_amt;
174     int abs_max = 99999;        /* max amount a sector can hold. */
175     int max_amt;
176     int level;
177
178
179     sectp = getsectp(sp->shp_x, sp->shp_y);
180
181     landowner = sectp->sct_own;
182     shipown = sp->shp_own;
183
184     for (i = 0; i < TMAX; ++i) {
185         item = sp->shp_tend[i];
186         level = sp->shp_lend[i];
187
188         if (item == ' ' || level == 0)
189             continue;
190         if (landowner == 0)
191             continue;
192         if (sectp->sct_type != SCT_HARBR)
193             continue;
194
195         comm = com_num(&item);
196         ship_amt = getvar(comm, (s_char *)sp, EF_SHIP);
197         sect_amt = getvar(comm, (s_char *)sectp, EF_SECTOR);
198
199         /* check for disloyal civilians */
200         if (sectp->sct_oldown != shipown && comm == V_CIVIL) {
201             wu(0, sp->shp_own,
202                "Ship #%d - unable to unload civilians into a disloyal sector at %s.",
203                sp->shp_uid, xyas(sectp->sct_x, sectp->sct_y,
204                                  sectp->sct_own));
205             continue;
206         }
207         if (comm == V_CIVIL)
208             ship_amt--;         /* This leaves 1 civs on board the ship */
209
210         if (sect_amt >= abs_max)
211             continue;           /* The sector is full. */
212
213         max_amt = min(ship_amt, abs_max - sect_amt);
214
215         if (max_amt <= 0)
216             continue;
217
218         putvar(comm, ship_amt - max_amt, (s_char *)sp, EF_SHIP);
219         putvar(comm, sect_amt + max_amt, (s_char *)sectp, EF_SECTOR);
220
221         if (getvar(V_PSTAGE, (s_char *)sectp, EF_SECTOR) == PLG_INFECT &&
222             getvar(V_PSTAGE, (s_char *)sp, EF_SHIP) == PLG_HEALTHY)
223             putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)sp, EF_SHIP);
224
225         if (getvar(V_PSTAGE, (s_char *)sp, EF_SHIP) == PLG_INFECT &&
226             getvar(V_PSTAGE, (s_char *)sectp, EF_SECTOR) == PLG_HEALTHY)
227             putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)sectp, EF_SECTOR);
228
229     }
230
231 }
232
233 /* com_num
234  * This small but useful bit of code runs through the list
235  * of commodities and return the integer value of the 
236  * commodity it finds if possible.  Very handy when using getvar().  
237  * Basicly its a hacked version of whatitem.c found in the
238  * /player directory.
239  * new autonav code.
240  * Chad Zabel 6/1/94
241  */
242
243 int
244 com_num(s_char *ptr)
245 {
246     struct ichrstr *ip;
247
248     for (ip = &ichr[1]; ip->i_mnem != 0; ip++) {
249         if (*ptr == ip->i_mnem)
250             return ip->i_vtype;
251     }
252     return 0;                   /*NOTREACHED*/
253 }
254
255
256
257 /* auto_fuel_ship 
258  * Assume a check for fuel=0 has already been made and passed.  
259  * Try to fill a ship using petro. and then oil.            
260  * new autonav code.
261  * This should be merged with the fuel command someday. 
262  * Chad Zabel 6/1/94
263  */
264
265 void
266 auto_fuel_ship(register struct shpstr *sp)
267 {
268     double d;
269     int totalfuel = 0;
270     int need;
271     int maxfuel;
272     int newfuel = 0;
273     int add_fuel = 0;
274
275     if (opt_FUEL == 0)
276         return;
277     getship(sp->shp_uid, sp);   /* refresh */
278     /* fill with petro */
279     maxfuel = mchr[(int)sp->shp_type].m_fuelc;
280     d = (double)maxfuel / 5.0;
281     if ((d - (int)d > 0.0))
282         d++;
283     need = (int)d;
284
285     newfuel = supply_commod(sp->shp_own, sp->shp_x,
286                             sp->shp_y, I_PETROL, need);
287     add_fuel += newfuel * 5;
288     if (add_fuel > maxfuel)
289         add_fuel = maxfuel;
290     sp->shp_fuel += add_fuel;
291     totalfuel += add_fuel;
292
293     if (totalfuel == maxfuel) {
294         putship(sp->shp_uid, sp);
295         return;                 /* the ship is full */
296     }
297     add_fuel = 0;
298     /* fill with oil */
299     d = (double)(maxfuel - totalfuel) / 50.0;
300     if ((d - (int)d > 0.0))
301         d++;
302     need = (int)d;
303
304     newfuel = supply_commod(sp->shp_own, sp->shp_x,
305                             sp->shp_y, I_OIL, need);
306     add_fuel = newfuel * 50;
307     if (add_fuel > maxfuel)
308         add_fuel = maxfuel;
309     sp->shp_fuel += add_fuel;
310     putship(sp->shp_uid, sp);
311 }