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