2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, 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:
45 /* load a specific ship given its
46 * location and what field to modify.
51 load_it(struct shpstr *sp, struct sctstr *psect, int i)
53 int shipown, amount, ship_amt, sect_amt;
54 int abs_max, max_amt, transfer;
56 struct mchrstr *vship;
58 amount = sp->shp_lend[i];
59 shipown = sp->shp_own;
60 comm = sp->shp_tend[i];
61 if (CANT_HAPPEN(comm <= I_NONE || comm > I_MAX))
64 ship_amt = sp->shp_item[comm];
65 sect_amt = psect->sct_item[comm];
67 /* check for disloyal civilians */
68 if (psect->sct_oldown != shipown && comm == I_CIVIL) {
70 "Ship #%d - unable to load disloyal civilians at %s.",
71 sp->shp_uid, xyas(psect->sct_x, psect->sct_y, psect->sct_own));
74 if (comm == I_CIVIL || comm == I_MILIT)
75 sect_amt--; /* leave 1 civ or mil to hold the sector. */
76 vship = &mchr[(int)sp->shp_type];
77 abs_max = max_amt = vship->m_item[comm];
80 return 0; /* can't load the ship, skip to the end. */
82 max_amt = MIN(sect_amt, max_amt - ship_amt);
83 if (max_amt <= 0 && (ship_amt != abs_max)) {
84 sp->shp_autonav |= AN_LOADING;
89 transfer = amount - ship_amt;
90 if (transfer > sect_amt) { /* not enough in the */
91 transfer = sect_amt; /* sector to fill the */
92 sp->shp_autonav |= AN_LOADING; /* ship, set load flag */
94 if (ship_amt + transfer > abs_max) /* Do not load more */
95 transfer = abs_max - ship_amt; /* then the max alowed */
99 return 0; /* nothing to move */
102 sp->shp_item[comm] = ship_amt + transfer;
103 if (comm == I_CIVIL || comm == I_MILIT)
104 sect_amt++; /*adjustment */
105 psect->sct_item[comm] = sect_amt - transfer;
107 /* deal with the plague */
108 if (psect->sct_pstage == PLG_INFECT && sp->shp_pstage == PLG_HEALTHY)
109 sp->shp_pstage = PLG_EXPOSED;
110 if (sp->shp_pstage == PLG_INFECT && psect->sct_pstage == PLG_HEALTHY)
111 psect->sct_pstage = PLG_EXPOSED;
113 return 1; /* we did someloading return 1 to keep */
114 /* our loop happy in nav_ship() */
119 * A guess alot of this looks like load_it but because of its location
120 * in the autonav code I had to split the 2 procedures up.
121 * unload_it dumps all the goods from the ship to the harbor.
122 * ONLY goods in the trade fields will be unloaded.
127 unload_it(struct shpstr *sp)
129 struct sctstr *sectp;
138 sectp = getsectp(sp->shp_x, sp->shp_y);
140 landowner = sectp->sct_own;
141 shipown = sp->shp_own;
143 for (i = 0; i < TMAX; ++i) {
144 if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0)
148 if (sectp->sct_type != SCT_HARBR)
151 comm = sp->shp_tend[i];
152 if (CANT_HAPPEN(comm <= I_NONE || comm > I_MAX))
154 ship_amt = sp->shp_item[comm];
155 sect_amt = sectp->sct_item[comm];
157 /* check for disloyal civilians */
158 if (sectp->sct_oldown != shipown && comm == I_CIVIL) {
160 "Ship #%d - unable to unload civilians into a disloyal sector at %s.",
161 sp->shp_uid, xyas(sectp->sct_x, sectp->sct_y,
166 ship_amt--; /* This leaves 1 civs on board the ship */
168 max_amt = MIN(ship_amt, ITEM_MAX - sect_amt);
172 sp->shp_item[comm] = ship_amt - max_amt;
173 sectp->sct_item[comm] = sect_amt + max_amt;
175 if (sectp->sct_pstage == PLG_INFECT && sp->shp_pstage == PLG_HEALTHY)
176 sp->shp_pstage = PLG_EXPOSED;
177 if (sp->shp_pstage == PLG_INFECT && sectp->sct_pstage == PLG_HEALTHY)
178 sectp->sct_pstage = PLG_EXPOSED;