]> git.pond.sub.org Git - empserver/blob - src/lib/update/nav_util.c
Update copyright notice.
[empserver] / src / lib / update / nav_util.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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 "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 /* Format a ship name */
58 int
59 check_nav(struct sctstr *sect)
60 {
61     switch (dchr[sect->sct_type].d_flg & 03) {
62     case NAVOK:
63         break;
64
65     case NAV_02:
66         if (sect->sct_effic < 2)
67             return CN_CONSTRUCTION;
68         break;
69     case NAV_60:
70         if (sect->sct_effic < 60)
71             return CN_CONSTRUCTION;
72         break;
73     default:
74         return CN_LANDLOCKED;
75     }
76     return CN_NAVIGABLE;
77 }
78
79 /* load a specific ship given its 
80  * location and what field to modify.
81  * new autonav code
82  * Chad Zabel 6/1/94 
83  */
84 int
85 load_it(register struct shpstr *sp, register struct sctstr *psect, int i)
86 {
87     int comm, shipown, amount, ship_amt, sect_amt;
88     int abs_max, max_amt, transfer;
89     struct mchrstr *vship;
90
91     amount = sp->shp_lend[i];
92     shipown = sp->shp_own;
93     comm = sp->shp_tend[i];
94     if (CANT_HAPPEN((unsigned)comm > I_MAX))
95         return 0;
96
97     ship_amt = sp->shp_item[comm];
98     sect_amt = psect->sct_item[comm];
99
100     /* check for disloyal civilians */
101     if (psect->sct_oldown != shipown && comm == I_CIVIL) {
102         wu(0, shipown,
103            "Ship #%d - unable to load disloyal civilians at %s.",
104            sp->shp_uid, xyas(psect->sct_x, psect->sct_y, psect->sct_own));
105         return 0;
106     }
107     if (comm == I_CIVIL || comm == I_MILIT)
108         sect_amt--;             /* leave 1 civ or mil to hold the sector. */
109     vship = &mchr[(int)sp->shp_type];
110     abs_max = max_amt = vship->m_item[comm];
111
112     if (!abs_max)
113         return 0;               /* can't load the ship, skip to the end. */
114
115     max_amt = min(sect_amt, max_amt - ship_amt);
116     if (max_amt <= 0 && (ship_amt != abs_max)) {
117         sp->shp_autonav |= AN_LOADING;
118         return 0;
119     }
120
121
122     transfer = amount - ship_amt;
123     if (transfer > sect_amt) {  /* not enough in the   */
124         transfer = sect_amt;    /* sector to fill the  */
125         sp->shp_autonav |= AN_LOADING;  /* ship, set load flag */
126     }
127     if (ship_amt + transfer > abs_max)  /* Do not load more    */
128         transfer = abs_max - ship_amt;  /* then the max alowed */
129     /* on the ship.        */
130
131     if (transfer == 0)
132         return 0;               /* nothing to move */
133
134
135     sp->shp_item[comm] = ship_amt + transfer;
136     if (comm == I_CIVIL || comm == I_MILIT)
137         sect_amt++;             /*adjustment */
138     psect->sct_item[comm] = sect_amt - transfer;
139
140     /* deal with the plague */
141     if (psect->sct_pstage == PLG_INFECT && sp->shp_pstage == PLG_HEALTHY)
142         sp->shp_pstage = PLG_EXPOSED;
143     if (sp->shp_pstage == PLG_INFECT && psect->sct_pstage == PLG_HEALTHY)
144         psect->sct_pstage = PLG_EXPOSED;
145
146     return 1;                   /* we did someloading return 1 to keep */
147     /* our loop happy in nav_ship()        */
148
149 }
150
151 /* unload_it 
152  * A guess alot of this looks like load_it but because of its location
153  * in the autonav code I had to split the 2 procedures up.
154  * unload_it dumps all the goods from the ship to the harbor.
155  * ONLY goods in the trade fields will be unloaded.
156  * new autonav code
157  * Chad Zabel 6/1/94  
158  */
159 void
160 unload_it(register struct shpstr *sp)
161 {
162     struct sctstr *sectp;
163     int i;
164     int landowner;
165     int shipown;
166     int comm;
167     int sect_amt;
168     int ship_amt;
169     int max_amt;
170
171     sectp = getsectp(sp->shp_x, sp->shp_y);
172
173     landowner = sectp->sct_own;
174     shipown = sp->shp_own;
175
176     for (i = 0; i < TMAX; ++i) {
177         if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0)
178             continue;
179         if (landowner == 0)
180             continue;
181         if (sectp->sct_type != SCT_HARBR)
182             continue;
183
184         comm = sp->shp_tend[i];
185         if (CANT_HAPPEN((unsigned)comm > I_MAX))
186             continue;
187         ship_amt = sp->shp_item[comm];
188         sect_amt = sectp->sct_item[comm];
189
190         /* check for disloyal civilians */
191         if (sectp->sct_oldown != shipown && comm == I_CIVIL) {
192             wu(0, sp->shp_own,
193                "Ship #%d - unable to unload civilians into a disloyal sector at %s.",
194                sp->shp_uid, xyas(sectp->sct_x, sectp->sct_y,
195                                  sectp->sct_own));
196             continue;
197         }
198         if (comm == I_CIVIL)
199             ship_amt--;         /* This leaves 1 civs on board the ship */
200
201         max_amt = min(ship_amt, ITEM_MAX - sect_amt);
202         if (max_amt <= 0)
203             continue;
204
205         sp->shp_item[comm] = ship_amt - max_amt;
206         sectp->sct_item[comm] = sect_amt + max_amt;
207
208         if (sectp->sct_pstage == PLG_INFECT && sp->shp_pstage == PLG_HEALTHY)
209             sp->shp_pstage = PLG_EXPOSED;
210         if (sp->shp_pstage == PLG_INFECT && sectp->sct_pstage == PLG_HEALTHY)
211             sectp->sct_pstage = PLG_EXPOSED;
212     }
213 }
214
215 /* auto_fuel_ship 
216  * Assume a check for fuel=0 has already been made and passed.  
217  * Try to fill a ship using petro. and then oil.            
218  * new autonav code.
219  * This should be merged with the fuel command someday. 
220  * Chad Zabel 6/1/94
221  */
222
223 void
224 auto_fuel_ship(register struct shpstr *sp)
225 {
226     double d;
227     int totalfuel = 0;
228     int need;
229     int maxfuel;
230     int newfuel = 0;
231     int add_fuel = 0;
232
233     if (opt_FUEL == 0)
234         return;
235     getship(sp->shp_uid, sp);   /* refresh */
236     /* fill with petro */
237     maxfuel = mchr[(int)sp->shp_type].m_fuelc;
238     d = (double)maxfuel / 5.0;
239     if ((d - (int)d > 0.0))
240         d++;
241     need = (int)d;
242
243     newfuel = supply_commod(sp->shp_own, sp->shp_x,
244                             sp->shp_y, I_PETROL, need);
245     add_fuel += newfuel * 5;
246     if (add_fuel > maxfuel)
247         add_fuel = maxfuel;
248     sp->shp_fuel += add_fuel;
249     totalfuel += add_fuel;
250
251     if (totalfuel == maxfuel) {
252         putship(sp->shp_uid, sp);
253         return;                 /* the ship is full */
254     }
255     add_fuel = 0;
256     /* fill with oil */
257     d = (double)(maxfuel - totalfuel) / 50.0;
258     if ((d - (int)d > 0.0))
259         d++;
260     need = (int)d;
261
262     newfuel = supply_commod(sp->shp_own, sp->shp_x,
263                             sp->shp_y, I_OIL, need);
264     add_fuel = newfuel * 50;
265     if (add_fuel > maxfuel)
266         add_fuel = maxfuel;
267     sp->shp_fuel += add_fuel;
268     putship(sp->shp_uid, sp);
269 }