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:
57 /* Format a ship name */
59 check_nav(struct sctstr *sect)
61 switch (dchr[sect->sct_type].d_flg & 03) {
66 if (sect->sct_effic < 2)
67 return CN_CONSTRUCTION;
70 if (sect->sct_effic < 60)
71 return CN_CONSTRUCTION;
79 /* load a specific ship given its
80 * location and what field to modify.
85 load_it(register struct shpstr *sp, register struct sctstr *psect, int i)
87 int comm, shipown, amount, ship_amt, sect_amt,
88 abs_max, max_amt, transfer;
90 struct mchrstr *vship;
92 amount = sp->shp_lend[i];
93 shipown = sp->shp_own;
94 item = sp->shp_tend[i]; /* commodity */
95 comm = com_num(&item);
97 ship_amt = sp->shp_item[comm];
98 sect_amt = psect->sct_item[comm];
100 /* check for disloyal civilians */
101 if (psect->sct_oldown != shipown && comm == V_CIVIL) {
103 "Ship #%d - unable to load disloyal civilians at %s.",
104 sp->shp_uid, xyas(psect->sct_x, psect->sct_y, psect->sct_own));
107 if (comm == V_CIVIL || comm == V_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 = (vl_find(comm, vship->m_vtype,
111 vship->m_vamt, (int)vship->m_nv));
114 return 0; /* can't load the ship, skip to the end. */
116 max_amt = min(sect_amt, max_amt - ship_amt);
117 if (max_amt <= 0 && (ship_amt != abs_max)) {
118 sp->shp_autonav |= AN_LOADING;
123 transfer = amount - ship_amt;
124 if (transfer > sect_amt) { /* not enough in the */
125 transfer = sect_amt; /* sector to fill the */
126 sp->shp_autonav |= AN_LOADING; /* ship, set load flag */
128 if (ship_amt + transfer > abs_max) /* Do not load more */
129 transfer = abs_max - ship_amt; /* then the max alowed */
133 return 0; /* nothing to move */
136 sp->shp_item[comm] = ship_amt + transfer;
137 if (comm == V_CIVIL || comm == V_MILIT)
138 sect_amt++; /*adjustment */
139 psect->sct_item[comm] = sect_amt - transfer;
141 /* deal with the plague */
142 if (psect->sct_pstage == PLG_INFECT && sp->shp_pstage == PLG_HEALTHY)
143 sp->shp_pstage = PLG_EXPOSED;
144 if (sp->shp_pstage == PLG_INFECT && psect->sct_pstage == PLG_HEALTHY)
145 psect->sct_pstage = PLG_EXPOSED;
147 return 1; /* we did someloading return 1 to keep */
148 /* our loop happy in nav_ship() */
153 * A guess alot of this looks like load_it but because of its location
154 * in the autonav code I had to split the 2 procedures up.
155 * unload_it dumps all the goods from the ship to the harbor.
156 * ONLY goods in the trade fields will be unloaded.
161 unload_it(register struct shpstr *sp)
163 struct sctstr *sectp;
171 int abs_max = 99999; /* max amount a sector can hold. */
176 sectp = getsectp(sp->shp_x, sp->shp_y);
178 landowner = sectp->sct_own;
179 shipown = sp->shp_own;
181 for (i = 0; i < TMAX; ++i) {
182 item = sp->shp_tend[i];
183 level = sp->shp_lend[i];
185 if (item == ' ' || level == 0)
189 if (sectp->sct_type != SCT_HARBR)
192 comm = com_num(&item);
193 ship_amt = sp->shp_item[comm];
194 sect_amt = sectp->sct_item[comm];
196 /* check for disloyal civilians */
197 if (sectp->sct_oldown != shipown && comm == V_CIVIL) {
199 "Ship #%d - unable to unload civilians into a disloyal sector at %s.",
200 sp->shp_uid, xyas(sectp->sct_x, sectp->sct_y,
205 ship_amt--; /* This leaves 1 civs on board the ship */
207 if (sect_amt >= abs_max)
208 continue; /* The sector is full. */
210 max_amt = min(ship_amt, abs_max - sect_amt);
215 sp->shp_item[comm] = ship_amt - max_amt;
216 sectp->sct_item[comm] = sect_amt + max_amt;
218 if (sectp->sct_pstage == PLG_INFECT && sp->shp_pstage == PLG_HEALTHY)
219 sp->shp_pstage = PLG_EXPOSED;
220 if (sp->shp_pstage == PLG_INFECT && sectp->sct_pstage == PLG_HEALTHY)
221 sectp->sct_pstage = PLG_EXPOSED;
226 * This small but useful bit of code runs through the list
227 * of commodities and return the integer value of the
228 * commodity it finds if possible.
229 * Basicly its a hacked version of whatitem.c found in the
240 for (ip = &ichr[1]; ip->i_mnem != 0; ip++) {
241 if (*ptr == ip->i_mnem)
244 return 0; /*NOTREACHED*/
250 * Assume a check for fuel=0 has already been made and passed.
251 * Try to fill a ship using petro. and then oil.
253 * This should be merged with the fuel command someday.
258 auto_fuel_ship(register struct shpstr *sp)
269 getship(sp->shp_uid, sp); /* refresh */
270 /* fill with petro */
271 maxfuel = mchr[(int)sp->shp_type].m_fuelc;
272 d = (double)maxfuel / 5.0;
273 if ((d - (int)d > 0.0))
277 newfuel = supply_commod(sp->shp_own, sp->shp_x,
278 sp->shp_y, I_PETROL, need);
279 add_fuel += newfuel * 5;
280 if (add_fuel > maxfuel)
282 sp->shp_fuel += add_fuel;
283 totalfuel += add_fuel;
285 if (totalfuel == maxfuel) {
286 putship(sp->shp_uid, sp);
287 return; /* the ship is full */
291 d = (double)(maxfuel - totalfuel) / 50.0;
292 if ((d - (int)d > 0.0))
296 newfuel = supply_commod(sp->shp_own, sp->shp_x,
297 sp->shp_y, I_OIL, need);
298 add_fuel = newfuel * 50;
299 if (add_fuel > maxfuel)
301 sp->shp_fuel += add_fuel;
302 putship(sp->shp_uid, sp);