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
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 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.
28 * nav_util.c: Utilities for autonav and sail
30 * Known contributors to this file:
46 /* load a specific ship given its
47 * location and what field to modify.
52 load_it(struct shpstr *sp, struct sctstr *psect, int i)
54 int shipown, amount, ship_amt, sect_amt;
55 int abs_max, max_amt, transfer;
57 struct mchrstr *vship;
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))
65 ship_amt = sp->shp_item[comm];
66 sect_amt = psect->sct_item[comm];
68 /* check for disloyal civilians */
69 if (psect->sct_oldown != shipown && comm == I_CIVIL) {
71 "Ship #%d - unable to load disloyal civilians at %s.",
72 sp->shp_uid, xyas(psect->sct_x, psect->sct_y, psect->sct_own));
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];
81 return 0; /* can't load the ship, skip to the end. */
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;
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 */
95 if (ship_amt + transfer > abs_max) /* Do not load more */
96 transfer = abs_max - ship_amt; /* then the max alowed */
100 return 0; /* nothing to move */
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;
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;
114 return 1; /* we did someloading return 1 to keep */
115 /* our loop happy in nav_ship() */
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.
128 unload_it(struct shpstr *sp)
130 struct sctstr *sectp;
139 sectp = getsectp(sp->shp_x, sp->shp_y);
141 landowner = sectp->sct_own;
142 shipown = sp->shp_own;
144 for (i = 0; i < TMAX; ++i) {
145 if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0)
149 if (sectp->sct_type != SCT_HARBR)
152 comm = sp->shp_tend[i];
153 if (CANT_HAPPEN(comm <= I_NONE || comm > I_MAX))
155 ship_amt = sp->shp_item[comm];
156 sect_amt = sectp->sct_item[comm];
158 /* check for disloyal civilians */
159 if (sectp->sct_oldown != shipown && comm == I_CIVIL) {
161 "Ship #%d - unable to unload civilians into a disloyal sector at %s.",
162 sp->shp_uid, xyas(sectp->sct_x, sectp->sct_y,
167 ship_amt--; /* This leaves 1 civs on board the ship */
169 max_amt = MIN(ship_amt, ITEM_MAX - sect_amt);
173 sp->shp_item[comm] = ship_amt - max_amt;
174 sectp->sct_item[comm] = sect_amt + max_amt;
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;
184 * Assume a check for fuel=0 has already been made and passed.
185 * Try to fill a ship using petro. and then oil.
187 * This should be merged with the fuel command someday.
192 auto_fuel_ship(struct shpstr *sp)
203 getship(sp->shp_uid, sp); /* refresh */
204 /* fill with petrol */
205 maxfuel = mchr[(int)sp->shp_type].m_fuelc;
207 if (d - (int)d > 0.0)
211 newfuel = supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
213 add_fuel += newfuel * 5;
214 if (add_fuel > maxfuel)
216 sp->shp_fuel += add_fuel;
217 totalfuel += add_fuel;
219 if (totalfuel == maxfuel) {
220 putship(sp->shp_uid, sp);
221 return; /* the ship is full */
225 d = (maxfuel - totalfuel) / 50.0;
226 if (d - (int)d > 0.0)
230 newfuel = supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
232 add_fuel = newfuel * 50;
233 if (add_fuel > maxfuel)
235 sp->shp_fuel += add_fuel;
236 putship(sp->shp_uid, sp);