2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2013, 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:
41 /* load a specific ship given its
42 * location and what field to modify.
47 load_it(struct shpstr *sp, struct sctstr *psect, int i)
49 int shipown, amount, ship_amt, sect_amt;
50 int abs_max, max_amt, transfer;
52 struct mchrstr *vship;
54 amount = sp->shp_lend[i];
55 shipown = sp->shp_own;
56 comm = sp->shp_tend[i];
57 if (CANT_HAPPEN(comm <= I_NONE || comm > I_MAX))
60 ship_amt = sp->shp_item[comm];
61 sect_amt = psect->sct_item[comm];
63 /* check for disloyal civilians */
64 if (psect->sct_oldown != shipown && comm == I_CIVIL) {
66 "Ship #%d - unable to load disloyal civilians at %s.",
67 sp->shp_uid, xyas(psect->sct_x, psect->sct_y, shipown));
70 if (comm == I_CIVIL || comm == I_MILIT)
71 sect_amt--; /* leave 1 civ or mil to hold the sector. */
72 vship = &mchr[(int)sp->shp_type];
73 abs_max = max_amt = vship->m_item[comm];
76 return 0; /* can't load the ship, skip to the end. */
78 max_amt = MIN(sect_amt, max_amt - ship_amt);
79 if (max_amt <= 0 && (ship_amt != abs_max)) {
80 sp->shp_autonav |= AN_LOADING;
85 transfer = amount - ship_amt;
86 if (transfer > sect_amt) { /* not enough in the */
87 transfer = sect_amt; /* sector to fill the */
88 sp->shp_autonav |= AN_LOADING; /* ship, set load flag */
90 if (ship_amt + transfer > abs_max) /* Do not load more */
91 transfer = abs_max - ship_amt; /* then the max alowed */
95 return 0; /* nothing to move */
98 sp->shp_item[comm] = ship_amt + transfer;
99 if (comm == I_CIVIL || comm == I_MILIT)
100 sect_amt++; /*adjustment */
101 psect->sct_item[comm] = sect_amt - transfer;
103 /* deal with the plague */
104 if (psect->sct_pstage == PLG_INFECT && sp->shp_pstage == PLG_HEALTHY)
105 sp->shp_pstage = PLG_EXPOSED;
106 if (sp->shp_pstage == PLG_INFECT && psect->sct_pstage == PLG_HEALTHY)
107 psect->sct_pstage = PLG_EXPOSED;
109 return 1; /* we did someloading return 1 to keep */
110 /* our loop happy in nav_ship() */
115 * A guess alot of this looks like load_it but because of its location
116 * in the autonav code I had to split the 2 procedures up.
117 * unload_it dumps all the goods from the ship to the harbor.
118 * ONLY goods in the trade fields will be unloaded.
123 unload_it(struct shpstr *sp)
125 struct sctstr *sectp;
134 sectp = getsectp(sp->shp_x, sp->shp_y);
136 landowner = sectp->sct_own;
137 shipown = sp->shp_own;
139 for (i = 0; i < TMAX; ++i) {
140 if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0)
144 if (sectp->sct_type != SCT_HARBR)
147 comm = sp->shp_tend[i];
148 if (CANT_HAPPEN(comm <= I_NONE || comm > I_MAX))
150 ship_amt = sp->shp_item[comm];
151 sect_amt = sectp->sct_item[comm];
153 /* check for disloyal civilians */
154 if (sectp->sct_oldown != shipown && comm == I_CIVIL) {
156 "Ship #%d - unable to unload civilians into a disloyal sector at %s.",
157 sp->shp_uid, xyas(sectp->sct_x, sectp->sct_y, sp->shp_own));
161 ship_amt--; /* This leaves 1 civs on board the ship */
163 max_amt = MIN(ship_amt, ITEM_MAX - sect_amt);
167 sp->shp_item[comm] = ship_amt - max_amt;
168 sectp->sct_item[comm] = sect_amt + max_amt;
170 if (sectp->sct_pstage == PLG_INFECT && sp->shp_pstage == PLG_HEALTHY)
171 sp->shp_pstage = PLG_EXPOSED;
172 if (sp->shp_pstage == PLG_INFECT && sectp->sct_pstage == PLG_HEALTHY)
173 sectp->sct_pstage = PLG_EXPOSED;