2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2016, 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 * ship.c: Do production for ships
29 * Known contributors to this file:
32 * Ron Koenderink, 2004
33 * Markus Armbruster, 2006-2016
50 static void shiprepair(struct shpstr *, struct natstr *, struct bp *, int);
51 static void upd_ship(struct shpstr *, int, struct natstr *, struct bp *, int);
52 static int feed_ship(struct shpstr *, int);
55 prod_ship(int etus, int natnum, struct bp *bp, int build)
56 /* build = 1, maintain = 0 */
63 for (n = 0; NULL != (sp = getshipp(n)); n++) {
66 if (sp->shp_own != natnum)
68 if (sp->shp_effic < SHIP_MINEFF) {
69 makelost(EF_SHIP, sp->shp_own, sp->shp_uid,
70 sp->shp_x, sp->shp_y);
75 np = getnatp(sp->shp_own);
76 start_money = np->nat_money;
77 upd_ship(sp, etus, np, bp, build);
78 sea_money[sp->shp_own] += np->nat_money - start_money;
79 if (!build || np->nat_money != start_money)
81 if (player->simulation)
82 np->nat_money = start_money;
89 upd_ship(struct shpstr *sp, int etus,
90 struct natstr *np, struct bp *bp, int build)
91 /* build = 1, maintain = 0 */
99 struct pchrstr *product;
100 unsigned char *resource;
102 int n, mult, cost, eff_lost;
104 mp = &mchr[(int)sp->shp_type];
106 if (!sp->shp_off && np->nat_money >= 0)
107 shiprepair(sp, np, bp, etus);
108 if (!player->simulation)
112 if (np->nat_level[NAT_TLEV] < sp->shp_tech * 0.85)
114 cost = -(mult * etus * MIN(0.0, money_ship * mp->m_cost));
115 if (np->nat_money < cost && !player->simulation) {
117 if (sp->shp_effic - eff_lost < SHIP_MINEFF)
118 eff_lost = sp->shp_effic - SHIP_MINEFF;
120 wu(0, sp->shp_own, "%s lost %d%% to lack of maintenance\n",
121 prship(sp), eff_lost);
122 sp->shp_effic -= eff_lost;
125 np->nat_money -= cost;
128 if (!player->simulation) {
129 sectp = getsectp(sp->shp_x, sp->shp_y);
132 if (np->nat_money >= 0
133 && (mp->m_flags & M_OIL) && sectp->sct_type == SCT_WATER) {
134 product = &pchr[dchr[SCT_OIL].d_prd];
135 oil_gained = roundavg(total_work(100, etus,
136 sp->shp_item[I_CIVIL],
137 sp->shp_item[I_MILIT],
140 * sp->shp_effic / 100.0
141 * sectp->sct_oil / 100.0
142 * prod_eff(SCT_OIL, sp->shp_tech));
143 max_oil = mp->m_item[I_OIL];
144 if (sp->shp_item[I_OIL] + oil_gained > max_oil)
145 oil_gained = max_oil - sp->shp_item[I_OIL];
146 if (product->p_nrdep != 0 && oil_gained > 0) {
147 resource = (unsigned char *)sectp + product->p_nrndx;
148 if (*resource * 100 < product->p_nrdep * oil_gained)
149 oil_gained = *resource * 100 / product->p_nrdep;
150 dep = roundavg(oil_gained * product->p_nrdep / 100.0);
151 if (CANT_HAPPEN(dep > *resource))
155 sp->shp_item[I_OIL] += oil_gained;
158 if (np->nat_money >= 0
159 && (mp->m_flags & M_FOOD) && sectp->sct_type == SCT_WATER) {
161 += roundavg(total_work(100, etus,
162 sp->shp_item[I_CIVIL],
163 sp->shp_item[I_MILIT],
166 * sp->shp_effic / 100.0
167 * sectp->sct_fertil / 100.0
168 * prod_eff(SCT_AGRI, sp->shp_tech));
171 if ((n = feed_ship(sp, etus)) > 0) {
172 wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
174 nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
176 max_food = mp->m_item[I_FOOD];
177 if (sp->shp_item[I_FOOD] > max_food)
178 sp->shp_item[I_FOOD] = max_food;
180 * do plague stuff. plague can't break out on ships,
181 * but it can still kill people.
183 pstage = sp->shp_pstage;
184 ptime = sp->shp_ptime;
185 if (pstage != PLG_HEALTHY) {
186 n = plague_people(np, sp->shp_item, &pstage, &ptime, etus);
190 "PLAGUE deaths reported on %s\n", prship(sp));
191 nreport(sp->shp_own, N_DIE_PLAGUE, 0, 1);
194 wu(0, sp->shp_own, "%s battling PLAGUE\n", prship(sp));
197 /* Are we still incubating? */
199 /* Yes. Will it turn "infectious" next time? */
201 /* Yes. Report an outbreak. */
203 "Outbreak of PLAGUE on %s!\n", prship(sp));
204 nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
207 /* It has already moved on to "infectious" */
209 "%s battling PLAGUE\n", prship(sp));
213 /* Has the plague moved to "incubation" yet? */
215 /* Yes. Will it turn "infectious" next time? */
217 /* Yes. Report an outbreak. */
219 "Outbreak of PLAGUE on %s!\n", prship(sp));
220 nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
228 sp->shp_pstage = pstage;
229 sp->shp_ptime = ptime;
231 pops[sp->shp_own] += sp->shp_item[I_CIVIL];
237 * idea is: a sector full of workers can fix up eight
238 * battleships +8 % eff each etu. This will cost around
239 * 8 * 8 * $40 = $2560!
242 shiprepair(struct shpstr *ship, struct natstr *np, struct bp *bp, int etus)
252 mp = &mchr[(int)ship->shp_type];
253 sp = getsectp(ship->shp_x, ship->shp_y);
256 && relations_with(sp->sct_own, ship->shp_own) < FRIENDLY)
259 /* only military can work on a military boat */
261 wf = etus * ship->shp_item[I_MILIT] / 2;
263 wf = etus * (ship->shp_item[I_CIVIL] / 2 + ship->shp_item[I_MILIT] / 5);
265 if (sp->sct_type != SCT_HARBR) {
269 if (!player->simulation)
270 avail = wf + sp->sct_avail * 100;
272 avail = wf + bp_get_avail(bp, sp) * 100;
275 if ((sp->sct_off) && (sp->sct_own == ship->shp_own))
279 if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
282 if (ship->shp_effic == 100) {
283 /* ship is ok; no repairs needed */
287 delta = roundavg((double)avail / mp->m_bwork);
290 if (delta > (int)((float)etus * ship_grow_scale))
291 delta = (int)((float)etus * ship_grow_scale);
292 if (delta > 100 - ship->shp_effic)
293 delta = 100 - ship->shp_effic;
295 build = get_materials(sp, bp, mp->m_mat, delta);
297 if (sp->sct_type != SCT_HARBR)
300 wf -= build * mp->m_bwork;
303 * I didn't use roundavg here, because I want to penalize
304 * the player with a large number of ships.
306 if (!player->simulation)
307 avail = (sp->sct_avail * 100 + wf) / 100;
309 avail = (bp_get_avail(bp, sp) * 100 + wf) / 100;
312 if (!player->simulation)
313 sp->sct_avail = avail;
315 bp_put_avail(bp, sp, avail);
317 if (sp->sct_type != SCT_HARBR)
318 if ((build + ship->shp_effic) > 80) {
319 build = 80 - ship->shp_effic;
324 np->nat_money -= mult * mp->m_cost * build / 100.0;
325 if (!player->simulation)
326 ship->shp_effic += (signed char)build;
330 * returns the number who starved, if any.
333 feed_ship(struct shpstr *sp, int etus)
343 needed = (int)ceil(food_needed(sp->shp_item, etus));
346 if (needed > sp->shp_item[I_FOOD]) {
347 /* take from embarked land units, but don't starve them */
348 snxtitem_cargo(&ni, EF_LAND, EF_SHIP, sp->shp_uid);
349 while ((lp = nxtitemp(&ni)) && needed > sp->shp_item[I_FOOD]) {
350 give = lp->lnd_item[I_FOOD] - food_needed(lp->lnd_item, etus);
353 take = MIN((int)give, needed - sp->shp_item[I_FOOD]);
354 sp->shp_item[I_FOOD] += take;
355 lp->lnd_item[I_FOOD] -= take;
359 return feed_people(sp->shp_item, etus);