]> git.pond.sub.org Git - empserver/blob - src/lib/update/nav_util.c
Clean up superfluous includes
[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 "nsc.h"
37 #include "plague.h"
38 #include "ship.h"
39 #include "update.h"
40
41 /* load a specific ship given its
42  * location and what field to modify.
43  * new autonav code
44  * Chad Zabel 6/1/94
45  */
46 int
47 load_it(struct shpstr *sp, struct sctstr *psect, int i)
48 {
49     int shipown, amount, ship_amt, sect_amt;
50     int abs_max, max_amt, transfer;
51     i_type comm;
52     struct mchrstr *vship;
53
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))
58         return 0;
59
60     ship_amt = sp->shp_item[comm];
61     sect_amt = psect->sct_item[comm];
62
63     /* check for disloyal civilians */
64     if (psect->sct_oldown != shipown && comm == I_CIVIL) {
65         wu(0, shipown,
66            "Ship #%d - unable to load disloyal civilians at %s.",
67            sp->shp_uid, xyas(psect->sct_x, psect->sct_y, shipown));
68         return 0;
69     }
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];
74
75     if (!abs_max)
76         return 0;               /* can't load the ship, skip to the end. */
77
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;
81         return 0;
82     }
83
84
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 */
89     }
90     if (ship_amt + transfer > abs_max)  /* Do not load more    */
91         transfer = abs_max - ship_amt;  /* then the max alowed */
92     /* on the ship.        */
93
94     if (transfer <= 0)
95         return 0;               /* nothing to move */
96
97
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;
102
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;
108
109     return 1;                   /* we did someloading return 1 to keep */
110     /* our loop happy in nav_ship()        */
111
112 }
113
114 /* unload_it
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.
119  * new autonav code
120  * Chad Zabel 6/1/94
121  */
122 void
123 unload_it(struct shpstr *sp)
124 {
125     struct sctstr *sectp;
126     int i;
127     int landowner;
128     int shipown;
129     i_type comm;
130     int sect_amt;
131     int ship_amt;
132     int max_amt;
133
134     sectp = getsectp(sp->shp_x, sp->shp_y);
135
136     landowner = sectp->sct_own;
137     shipown = sp->shp_own;
138
139     for (i = 0; i < TMAX; ++i) {
140         if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0)
141             continue;
142         if (landowner == 0)
143             continue;
144         if (sectp->sct_type != SCT_HARBR)
145             continue;
146
147         comm = sp->shp_tend[i];
148         if (CANT_HAPPEN(comm <= I_NONE || comm > I_MAX))
149             continue;
150         ship_amt = sp->shp_item[comm];
151         sect_amt = sectp->sct_item[comm];
152
153         /* check for disloyal civilians */
154         if (sectp->sct_oldown != shipown && comm == I_CIVIL) {
155             wu(0, sp->shp_own,
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));
158             continue;
159         }
160         if (comm == I_CIVIL)
161             ship_amt--;         /* This leaves 1 civs on board the ship */
162
163         max_amt = MIN(ship_amt, ITEM_MAX - sect_amt);
164         if (max_amt <= 0)
165             continue;
166
167         sp->shp_item[comm] = ship_amt - max_amt;
168         sectp->sct_item[comm] = sect_amt + max_amt;
169
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;
174     }
175 }