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