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