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
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.
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.
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
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.
28 * nav_util.c: Utilities for autonav and sail
30 * Known contributors to this file:
58 /* Format a ship name */
60 check_nav(struct sctstr *sect)
62 extern struct dchrstr dchr[];
64 switch (dchr[sect->sct_type].d_flg & 03) {
69 if (sect->sct_effic < 2)
70 return CN_CONSTRUCTION;
73 if (sect->sct_effic < 60)
74 return CN_CONSTRUCTION;
82 /* load a specific ship given its
83 * location and what field to modify.
88 load_it(register struct shpstr *sp, register struct sctstr *psect, int i)
90 int comm, shipown, amount, ship_amt, sect_amt,
91 abs_max, max_amt, transfer;
93 struct mchrstr *vship;
95 amount = sp->shp_lend[i];
96 shipown = sp->shp_own;
97 item = sp->shp_tend[i]; /* commodity */
98 comm = com_num(&item);
100 ship_amt = getvar(comm,(s_char *) sp , EF_SHIP);
101 sect_amt = getvar(comm,(s_char *) psect, EF_SECTOR);
103 /* check for disloyal civilians */
104 if (psect->sct_oldown != shipown && comm == V_CIVIL)
105 { wu(0,shipown,"Ship #%d - unable to load disloyal civilians at %s.",
106 sp->shp_uid, xyas(psect->sct_x,psect->sct_y,psect->sct_own));
109 if (comm == V_CIVIL || comm == V_MILIT)
110 sect_amt--; /* leave 1 civ or mil to hold the sector. */
111 vship = &mchr[(int)sp->shp_type];
112 abs_max = max_amt = (vl_find(comm,vship->m_vtype,
113 vship->m_vamt,(int) vship->m_nv));
116 return 0; /* can't load the ship, skip to the end. */
118 max_amt = min (sect_amt, max_amt - ship_amt);
119 if (max_amt <= 0 && (ship_amt != abs_max)) {
120 sp->shp_autonav |= AN_LOADING;
125 transfer = amount - ship_amt;
126 if (transfer > sect_amt) { /* not enough in the */
127 transfer = sect_amt; /* sector to fill the */
128 sp->shp_autonav |= AN_LOADING; /* ship, set load flag */
130 if (ship_amt + transfer > abs_max) /* Do not load more */
131 transfer = abs_max-ship_amt; /* then the max alowed */
135 return 0; /* nothing to move */
138 putvar(comm, ship_amt + transfer, (s_char *)sp, EF_SHIP);
139 if (comm == V_CIVIL || comm == V_MILIT)
140 sect_amt++; /*adjustment*/
141 putvar(comm, sect_amt - transfer, (s_char *)psect, EF_SECTOR);
143 /* deal with the plague */
144 if (getvar(V_PSTAGE, (s_char *)psect, EF_SECTOR) == PLG_INFECT &&
145 getvar(V_PSTAGE, (s_char *)sp , EF_SHIP) == PLG_HEALTHY)
146 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)sp, EF_SHIP);
147 if (getvar(V_PSTAGE, (s_char *)sp, EF_SHIP) == PLG_INFECT &&
148 getvar(V_PSTAGE, (s_char *)psect, EF_SECTOR) == PLG_HEALTHY)
149 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)psect, EF_SECTOR);
151 return 1; /* we did someloading return 1 to keep */
152 /* our loop happy in nav_ship() */
157 * A guess alot of this looks like load_it but because of its location
158 * in the autonav code I had to split the 2 procedures up.
159 * unload_it dumps all the goods from the ship to the harbor.
160 * ONLY goods in the trade fields will be unloaded.
165 unload_it(register struct shpstr *sp)
167 struct sctstr *sectp;
175 int abs_max = 99999; /* max amount a sector can hold. */
180 sectp = getsectp(sp->shp_x,sp->shp_y);
182 landowner = sectp->sct_own;
183 shipown = sp->shp_own;
185 for(i=0;i<TMAX;++i) {
186 item = sp->shp_tend[i];
187 level = sp->shp_lend[i];
189 if (item == ' ' || level == 0)
193 if (sectp->sct_type != SCT_HARBR)
196 comm = com_num(&item);
197 ship_amt = getvar(comm,(s_char *) sp ,EF_SHIP);
198 sect_amt = getvar(comm,(s_char *) sectp ,EF_SECTOR);
200 /* check for disloyal civilians */
201 if (sectp->sct_oldown != shipown && comm == V_CIVIL)
203 wu(0,sp->shp_own,"Ship #%d - unable to unload civilians into a disloyal sector at %s.",
204 sp->shp_uid, xyas(sectp->sct_x,sectp->sct_y,sectp->sct_own));
208 ship_amt--; /* This leaves 1 civs on board the ship */
210 if (sect_amt >= abs_max)
211 continue; /* The sector is full. */
213 max_amt = min (ship_amt,abs_max - sect_amt);
218 putvar(comm, ship_amt - max_amt, (s_char *) sp ,EF_SHIP);
219 putvar(comm, sect_amt + max_amt, (s_char *) sectp,EF_SECTOR);
221 if (getvar(V_PSTAGE, (s_char *)sectp,EF_SECTOR) == PLG_INFECT &&
222 getvar(V_PSTAGE, (s_char *)sp ,EF_SHIP ) == PLG_HEALTHY)
223 putvar(V_PSTAGE, PLG_EXPOSED,(s_char *)sp, EF_SHIP);
225 if (getvar(V_PSTAGE, (s_char *)sp ,EF_SHIP ) == PLG_INFECT &&
226 getvar(V_PSTAGE, (s_char *)sectp,EF_SECTOR) == PLG_HEALTHY)
227 putvar(V_PSTAGE, PLG_EXPOSED,(s_char *)sectp, EF_SECTOR);
233 * This small but useful bit of code runs through the list
234 * of commodities and return the integer value of the
235 * commodity it finds if possible. Very handy when using getvar().
236 * Basicly its a hacked version of whatitem.c found in the
247 for(ip = &ichr[1];ip->i_mnem != 0; ip++) {
248 if (*ptr == ip->i_mnem)
251 return 0; /*NOTREACHED*/
257 * Assume a check for fuel=0 has already been made and passed.
258 * Try to fill a ship using petro. and then oil.
260 * This should be merged with the fuel command someday.
265 auto_fuel_ship(register struct shpstr *sp)
274 if (opt_FUEL == 0) return;
275 getship(sp->shp_uid,sp); /* refresh */
276 /* fill with petro */
277 maxfuel = mchr[(int)sp->shp_type].m_fuelc;
278 d = (double) maxfuel / 5.0;
279 if (( d-(int)d > 0.0 ))
283 newfuel = supply_commod(sp->shp_own,sp->shp_x,
284 sp->shp_y,I_PETROL,need);
285 add_fuel += newfuel * 5;
286 if (add_fuel > maxfuel)
288 sp->shp_fuel += add_fuel;
289 totalfuel += add_fuel;
291 if (totalfuel == maxfuel) {
292 putship(sp->shp_uid,sp);
293 return; /* the ship is full */
297 d = (double) (maxfuel - totalfuel) / 50.0;
298 if ((d-(int)d > 0.0))
302 newfuel = supply_commod(sp->shp_own,sp->shp_x,
303 sp->shp_y,I_OIL,need);
304 add_fuel = newfuel * 50;
305 if (add_fuel > maxfuel)
307 sp->shp_fuel += add_fuel;
308 putship(sp->shp_uid,sp);