2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * nav_util.c: Utilities for autonav and sail
29 * Known contributors to this file:
44 /* load a specific ship given its
45 * location and what field to modify.
50 load_it(struct shpstr *sp, struct sctstr *psect, int i)
52 int shipown, amount, ship_amt, sect_amt;
53 int abs_max, max_amt, transfer;
55 struct mchrstr *vship;
57 amount = sp->shp_lend[i];
58 shipown = sp->shp_own;
59 comm = sp->shp_tend[i];
60 if (CANT_HAPPEN(comm <= I_NONE || comm > I_MAX))
63 ship_amt = sp->shp_item[comm];
64 sect_amt = psect->sct_item[comm];
66 /* check for disloyal civilians */
67 if (psect->sct_oldown != shipown && comm == I_CIVIL) {
69 "Ship #%d - unable to load disloyal civilians at %s.",
70 sp->shp_uid, xyas(psect->sct_x, psect->sct_y, shipown));
73 if (comm == I_CIVIL || comm == I_MILIT)
74 sect_amt--; /* leave 1 civ or mil to hold the sector. */
75 vship = &mchr[(int)sp->shp_type];
76 abs_max = max_amt = vship->m_item[comm];
79 return 0; /* can't load the ship, skip to the end. */
81 max_amt = MIN(sect_amt, max_amt - ship_amt);
82 if (max_amt <= 0 && (ship_amt != abs_max)) {
83 sp->shp_autonav |= AN_LOADING;
88 transfer = amount - ship_amt;
89 if (transfer > sect_amt) { /* not enough in the */
90 transfer = sect_amt; /* sector to fill the */
91 sp->shp_autonav |= AN_LOADING; /* ship, set load flag */
93 if (ship_amt + transfer > abs_max) /* Do not load more */
94 transfer = abs_max - ship_amt; /* then the max alowed */
98 return 0; /* nothing to move */
101 sp->shp_item[comm] = ship_amt + transfer;
102 if (comm == I_CIVIL || comm == I_MILIT)
103 sect_amt++; /*adjustment */
104 psect->sct_item[comm] = sect_amt - transfer;
106 /* deal with the plague */
107 if (psect->sct_pstage == PLG_INFECT && sp->shp_pstage == PLG_HEALTHY)
108 sp->shp_pstage = PLG_EXPOSED;
109 if (sp->shp_pstage == PLG_INFECT && psect->sct_pstage == PLG_HEALTHY)
110 psect->sct_pstage = PLG_EXPOSED;
112 return 1; /* we did someloading return 1 to keep */
113 /* our loop happy in nav_ship() */
118 * A guess alot of this looks like load_it but because of its location
119 * in the autonav code I had to split the 2 procedures up.
120 * unload_it dumps all the goods from the ship to the harbor.
121 * ONLY goods in the trade fields will be unloaded.
126 unload_it(struct shpstr *sp)
128 struct sctstr *sectp;
137 sectp = getsectp(sp->shp_x, sp->shp_y);
139 landowner = sectp->sct_own;
140 shipown = sp->shp_own;
142 for (i = 0; i < TMAX; ++i) {
143 if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0)
147 if (sectp->sct_type != SCT_HARBR)
150 comm = sp->shp_tend[i];
151 if (CANT_HAPPEN(comm <= I_NONE || comm > I_MAX))
153 ship_amt = sp->shp_item[comm];
154 sect_amt = sectp->sct_item[comm];
156 /* check for disloyal civilians */
157 if (sectp->sct_oldown != shipown && comm == I_CIVIL) {
159 "Ship #%d - unable to unload civilians into a disloyal sector at %s.",
160 sp->shp_uid, xyas(sectp->sct_x, sectp->sct_y, sp->shp_own));
164 ship_amt--; /* This leaves 1 civs on board the ship */
166 max_amt = MIN(ship_amt, ITEM_MAX - sect_amt);
170 sp->shp_item[comm] = ship_amt - max_amt;
171 sectp->sct_item[comm] = sect_amt + max_amt;
173 if (sectp->sct_pstage == PLG_INFECT && sp->shp_pstage == PLG_HEALTHY)
174 sp->shp_pstage = PLG_EXPOSED;
175 if (sp->shp_pstage == PLG_INFECT && sectp->sct_pstage == PLG_HEALTHY)
176 sectp->sct_pstage = PLG_EXPOSED;