]> git.pond.sub.org Git - empserver/blob - src/lib/update/nav_util.c
License upgrade to GPL version 3 or later
[empserver] / src / lib / update / nav_util.c
1 /*
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
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  nav_util.c: Utilities for autonav and sail
28  *
29  *  Known contributors to this file:
30  *
31  */
32
33 #include <config.h>
34
35 #include "item.h"
36 #include "land.h"
37 #include "nsc.h"
38 #include "path.h"
39 #include "plague.h"
40 #include "plane.h"
41 #include "ship.h"
42 #include "update.h"
43
44 /* load a specific ship given its
45  * location and what field to modify.
46  * new autonav code
47  * Chad Zabel 6/1/94
48  */
49 int
50 load_it(struct shpstr *sp, struct sctstr *psect, int i)
51 {
52     int shipown, amount, ship_amt, sect_amt;
53     int abs_max, max_amt, transfer;
54     i_type comm;
55     struct mchrstr *vship;
56
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))
61         return 0;
62
63     ship_amt = sp->shp_item[comm];
64     sect_amt = psect->sct_item[comm];
65
66     /* check for disloyal civilians */
67     if (psect->sct_oldown != shipown && comm == I_CIVIL) {
68         wu(0, shipown,
69            "Ship #%d - unable to load disloyal civilians at %s.",
70            sp->shp_uid, xyas(psect->sct_x, psect->sct_y, shipown));
71         return 0;
72     }
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];
77
78     if (!abs_max)
79         return 0;               /* can't load the ship, skip to the end. */
80
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;
84         return 0;
85     }
86
87
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 */
92     }
93     if (ship_amt + transfer > abs_max)  /* Do not load more    */
94         transfer = abs_max - ship_amt;  /* then the max alowed */
95     /* on the ship.        */
96
97     if (transfer <= 0)
98         return 0;               /* nothing to move */
99
100
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;
105
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;
111
112     return 1;                   /* we did someloading return 1 to keep */
113     /* our loop happy in nav_ship()        */
114
115 }
116
117 /* unload_it
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.
122  * new autonav code
123  * Chad Zabel 6/1/94
124  */
125 void
126 unload_it(struct shpstr *sp)
127 {
128     struct sctstr *sectp;
129     int i;
130     int landowner;
131     int shipown;
132     i_type comm;
133     int sect_amt;
134     int ship_amt;
135     int max_amt;
136
137     sectp = getsectp(sp->shp_x, sp->shp_y);
138
139     landowner = sectp->sct_own;
140     shipown = sp->shp_own;
141
142     for (i = 0; i < TMAX; ++i) {
143         if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0)
144             continue;
145         if (landowner == 0)
146             continue;
147         if (sectp->sct_type != SCT_HARBR)
148             continue;
149
150         comm = sp->shp_tend[i];
151         if (CANT_HAPPEN(comm <= I_NONE || comm > I_MAX))
152             continue;
153         ship_amt = sp->shp_item[comm];
154         sect_amt = sectp->sct_item[comm];
155
156         /* check for disloyal civilians */
157         if (sectp->sct_oldown != shipown && comm == I_CIVIL) {
158             wu(0, sp->shp_own,
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));
161             continue;
162         }
163         if (comm == I_CIVIL)
164             ship_amt--;         /* This leaves 1 civs on board the ship */
165
166         max_amt = MIN(ship_amt, ITEM_MAX - sect_amt);
167         if (max_amt <= 0)
168             continue;
169
170         sp->shp_item[comm] = ship_amt - max_amt;
171         sectp->sct_item[comm] = sect_amt + max_amt;
172
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;
177     }
178 }