2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2015, 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-2011
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)
254 mp = &mchr[(int)ship->shp_type];
255 sp = getsectp(ship->shp_x, ship->shp_y);
258 && relations_with(sp->sct_own, ship->shp_own) < FRIENDLY)
261 /* only military can work on a military boat */
263 wf = etus * ship->shp_item[I_MILIT] / 2;
265 wf = etus * (ship->shp_item[I_CIVIL] / 2 + ship->shp_item[I_MILIT] / 5);
267 if (sp->sct_type != SCT_HARBR) {
271 if (!player->simulation)
272 avail = wf + sp->sct_avail * 100;
274 avail = wf + bp_get_avail(bp, sp) * 100;
277 w_p_eff = SHP_BLD_WORK(mp->m_lcm, mp->m_hcm);
279 if ((sp->sct_off) && (sp->sct_own == ship->shp_own))
283 if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
286 if (ship->shp_effic == 100) {
287 /* ship is ok; no repairs needed */
291 delta = roundavg((double)avail / w_p_eff);
294 if (delta > (int)((float)etus * ship_grow_scale))
295 delta = (int)((float)etus * ship_grow_scale);
296 if (delta > 100 - ship->shp_effic)
297 delta = 100 - ship->shp_effic;
299 memset(mvec, 0, sizeof(mvec));
300 mvec[I_LCM] = mp->m_lcm;
301 mvec[I_HCM] = mp->m_hcm;
302 build = get_materials(sp, bp, mvec, delta);
304 if (sp->sct_type != SCT_HARBR)
307 wf -= build * w_p_eff;
310 * I didn't use roundavg here, because I want to penalize
311 * the player with a large number of ships.
313 if (!player->simulation)
314 avail = (sp->sct_avail * 100 + wf) / 100;
316 avail = (bp_get_avail(bp, sp) * 100 + wf) / 100;
319 if (!player->simulation)
320 sp->sct_avail = avail;
322 bp_put_avail(bp, sp, avail);
324 if (sp->sct_type != SCT_HARBR)
325 if ((build + ship->shp_effic) > 80) {
326 build = 80 - ship->shp_effic;
331 np->nat_money -= mult * mp->m_cost * build / 100.0;
332 if (!player->simulation)
333 ship->shp_effic += (signed char)build;
337 * returns the number who starved, if any.
340 feed_ship(struct shpstr *sp, int etus)
350 needed = (int)ceil(food_needed(sp->shp_item, etus));
353 if (needed > sp->shp_item[I_FOOD]) {
354 /* take from embarked land units, but don't starve them */
355 snxtitem_cargo(&ni, EF_LAND, EF_SHIP, sp->shp_uid);
356 while ((lp = nxtitemp(&ni)) && needed > sp->shp_item[I_FOOD]) {
357 give = lp->lnd_item[I_FOOD] - food_needed(lp->lnd_item, etus);
360 take = MIN((int)give, needed - sp->shp_item[I_FOOD]);
361 sp->shp_item[I_FOOD] += take;
362 lp->lnd_item[I_FOOD] -= take;
366 return feed_people(sp->shp_item, etus);