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