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
49 #include "prototypes.h"
53 static void upd_ship(struct shpstr *, int, struct bp *, int);
54 static void shiprepair(struct shpstr *, struct natstr *, struct bp *,
55 int, struct budget *);
56 static int feed_ship(struct shpstr *, int);
59 prod_ship(int etus, int natnum, struct bp *bp, int build)
60 /* build = 1, maintain = 0 */
65 for (i = 0; (sp = getshipp(i)); i++) {
68 if (sp->shp_own != natnum)
70 if (sp->shp_effic < SHIP_MINEFF) {
71 makelost(EF_SHIP, sp->shp_own, sp->shp_uid,
72 sp->shp_x, sp->shp_y);
77 upd_ship(sp, etus, bp, build);
82 upd_ship(struct shpstr *sp, int etus, struct bp *bp, int build)
83 /* build = 1, maintain = 0 */
85 struct budget *budget = &nat_budget[sp->shp_own];
86 struct mchrstr *mp = &mchr[sp->shp_type];
87 struct natstr *np = getnatp(sp->shp_own);
93 struct pchrstr *product;
94 unsigned char *resource;
96 int n, mult, eff_lost;
100 if (!sp->shp_off && budget->money >= 0)
101 shiprepair(sp, np, bp, etus, budget);
102 if (!player->simulation)
105 budget->oldowned_civs += sp->shp_item[I_CIVIL];
107 if (np->nat_level[NAT_TLEV] < sp->shp_tech * 0.85)
109 budget->bm[BUDG_SHP_MAINT].count++;
110 cost = mult * etus * -money_ship * mp->m_cost;
111 if (budget->money < cost && !player->simulation) {
113 if (sp->shp_effic - eff_lost < SHIP_MINEFF)
114 eff_lost = sp->shp_effic - SHIP_MINEFF;
116 wu(0, sp->shp_own, "%s lost %d%% to lack of maintenance\n",
117 prship(sp), eff_lost);
118 sp->shp_effic -= eff_lost;
121 budget->bm[BUDG_SHP_MAINT].money -= cost;
122 budget->money -= cost;
125 if (!player->simulation) {
126 sectp = getsectp(sp->shp_x, sp->shp_y);
129 if (budget->money >= 0
130 && (mp->m_flags & M_OIL) && sectp->sct_type == SCT_WATER) {
131 product = &pchr[dchr[SCT_OIL].d_prd];
132 oil_gained = roundavg(total_work(100, etus,
133 sp->shp_item[I_CIVIL],
134 sp->shp_item[I_MILIT],
137 * sp->shp_effic / 100.0
138 * sectp->sct_oil / 100.0
139 * prod_eff(SCT_OIL, sp->shp_tech));
140 max_oil = mp->m_item[I_OIL];
141 if (sp->shp_item[I_OIL] + oil_gained > max_oil)
142 oil_gained = max_oil - sp->shp_item[I_OIL];
143 if (product->p_nrdep != 0 && oil_gained > 0) {
144 resource = (unsigned char *)sectp + product->p_nrndx;
145 if (*resource * 100 < product->p_nrdep * oil_gained)
146 oil_gained = *resource * 100 / product->p_nrdep;
147 dep = roundavg(oil_gained * product->p_nrdep / 100.0);
148 if (CANT_HAPPEN(dep > *resource))
152 sp->shp_item[I_OIL] += oil_gained;
155 if (budget->money >= 0
156 && (mp->m_flags & M_FOOD) && sectp->sct_type == SCT_WATER) {
158 += roundavg(total_work(100, etus,
159 sp->shp_item[I_CIVIL],
160 sp->shp_item[I_MILIT],
163 * sp->shp_effic / 100.0
164 * sectp->sct_fertil / 100.0
165 * prod_eff(SCT_AGRI, sp->shp_tech));
168 if ((n = feed_ship(sp, etus)) > 0) {
169 wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
171 nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
173 max_food = mp->m_item[I_FOOD];
174 if (sp->shp_item[I_FOOD] > max_food)
175 sp->shp_item[I_FOOD] = max_food;
177 * do plague stuff. plague can't break out on ships,
178 * but it can still kill people.
180 pstage = sp->shp_pstage;
181 ptime = sp->shp_ptime;
182 if (pstage != PLG_HEALTHY) {
183 n = plague_people(np, sp->shp_item, &pstage, &ptime, etus);
187 "PLAGUE deaths reported on %s\n", prship(sp));
188 nreport(sp->shp_own, N_DIE_PLAGUE, 0, 1);
191 wu(0, sp->shp_own, "%s battling PLAGUE\n", prship(sp));
194 /* Are we still incubating? */
196 /* Yes. Will it turn "infectious" next time? */
198 /* Yes. Report an outbreak. */
200 "Outbreak of PLAGUE on %s!\n", prship(sp));
201 nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
204 /* It has already moved on to "infectious" */
206 "%s battling PLAGUE\n", prship(sp));
210 /* Has the plague moved to "incubation" yet? */
212 /* Yes. Will it turn "infectious" next time? */
214 /* Yes. Report an outbreak. */
216 "Outbreak of PLAGUE on %s!\n", prship(sp));
217 nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
225 sp->shp_pstage = pstage;
226 sp->shp_ptime = ptime;
233 * idea is: a sector full of workers can fix up eight
234 * battleships +8 % eff each etu. This will cost around
235 * 8 * 8 * $40 = $2560!
238 shiprepair(struct shpstr *ship, struct natstr *np, struct bp *bp, int etus,
239 struct budget *budget)
241 struct mchrstr *mp = &mchr[(int)ship->shp_type];
243 struct sctstr *sp, scratch_sect;
250 if (ship->shp_effic == 100)
253 sp = getsectp(ship->shp_x, ship->shp_y);
258 && relations_with(sp->sct_own, ship->shp_own) < FRIENDLY)
261 if (player->simulation) {
263 bp_to_sect(bp, &scratch_sect);
268 if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
271 /* only military can work on a military boat */
273 wf = etus * ship->shp_item[I_MILIT] / 2;
275 wf = etus * (ship->shp_item[I_CIVIL] / 2 + ship->shp_item[I_MILIT] / 5);
277 if (sp->sct_type != SCT_HARBR) {
281 avail = wf + sp->sct_avail * 100;
283 delta = avail / mp->m_bwork;
286 if (delta > (int)((float)etus * ship_grow_scale))
287 delta = (int)((float)etus * ship_grow_scale);
288 if (delta > 100 - ship->shp_effic)
289 delta = 100 - ship->shp_effic;
291 build = get_materials(sp, mp->m_mat, delta);
293 if (sp->sct_type != SCT_HARBR)
296 wf -= build * mp->m_bwork;
298 avail = roundavg((sp->sct_avail * 100 + wf) / 100.0);
301 sp->sct_avail = avail;
303 if (sp->sct_type != SCT_HARBR)
304 if ((build + ship->shp_effic) > 80) {
305 build = 80 - ship->shp_effic;
310 bp_set_from_sect(bp, sp);
311 cost = mult * mp->m_cost * build / 100.0;
312 budget->bm[BUDG_SHP_BUILD].count += !!build;
313 budget->bm[BUDG_SHP_BUILD].money -= cost;
314 budget->money -= cost;
315 if (!player->simulation)
316 ship->shp_effic += (signed char)build;
320 * returns the number who starved, if any.
323 feed_ship(struct shpstr *sp, int etus)
333 needed = (int)ceil(food_needed(sp->shp_item, etus));
336 if (needed > sp->shp_item[I_FOOD]) {
337 /* take from embarked land units, but don't starve them */
338 snxtitem_cargo(&ni, EF_LAND, EF_SHIP, sp->shp_uid);
339 while ((lp = nxtitemp(&ni)) && needed > sp->shp_item[I_FOOD]) {
340 give = lp->lnd_item[I_FOOD] - food_needed(lp->lnd_item, etus);
343 take = MIN((int)give, needed - sp->shp_item[I_FOOD]);
344 sp->shp_item[I_FOOD] += take;
345 lp->lnd_item[I_FOOD] -= take;
349 return feed_people(sp->shp_item, etus);