2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * ship.c: Do production for ships
30 * Known contributors to this file:
33 * Ron Koenderink, 2004
34 * Markus Armbruster, 2006-2008
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 */
62 int lastx = 9999, lasty = 9999;
64 bp_enable_cachepath();
65 for (n = 0; NULL != (sp = getshipp(n)); n++) {
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 np = getnatp(sp->shp_own);
78 if (lastx == 9999 || lasty == 9999) {
82 if (lastx != sp->shp_x || lasty != sp->shp_y) {
84 bp_disable_cachepath();
86 bp_enable_cachepath();
88 start_money = np->nat_money;
89 upd_ship(sp, etus, np, bp, build);
90 if (build && !player->simulation) /* make sure to only autonav once */
91 nav_ship(sp); /* autonav the ship */
92 sea_money[sp->shp_own] += np->nat_money - start_money;
93 if (!build || np->nat_money != start_money)
95 if (player->simulation)
96 np->nat_money = start_money;
98 bp_disable_cachepath();
102 if (build && !player->simulation) /* make sure to only sail once */
109 upd_ship(struct shpstr *sp, int etus,
110 struct natstr *np, struct bp *bp, int build)
111 /* build = 1, maintain = 0 */
113 struct sctstr *sectp;
119 struct pchrstr *product;
120 unsigned char *resource;
127 mp = &mchr[(int)sp->shp_type];
129 if (!sp->shp_off && np->nat_money >= 0)
130 shiprepair(sp, np, bp, etus);
131 if (!player->simulation)
135 if (np->nat_level[NAT_TLEV] < sp->shp_tech * 0.85)
137 cost = -(mult * etus * MIN(0.0, money_ship * mp->m_cost));
138 if (np->nat_money < cost && !player->simulation) {
139 if ((eff = sp->shp_effic - etus / 5) < SHIP_MINEFF) {
141 "%s lost to lack of maintenance\n", prship(sp));
142 makelost(EF_SHIP, sp->shp_own, sp->shp_uid,
143 sp->shp_x, sp->shp_y);
148 "%s lost %d%% to lack of maintenance\n",
149 prship(sp), sp->shp_effic - eff);
152 np->nat_money -= cost;
155 if (!player->simulation) {
156 sectp = getsectp(sp->shp_x, sp->shp_y);
159 if (np->nat_money >= 0
160 && (mp->m_flags & M_OIL) && sectp->sct_type == SCT_WATER) {
161 product = &pchr[dchr[SCT_OIL].d_prd];
162 oil_gained = roundavg(total_work(100, etus,
163 sp->shp_item[I_CIVIL],
164 sp->shp_item[I_MILIT],
167 * sp->shp_effic / 100.0
168 * sectp->sct_oil / 100.0
169 * prod_eff(SCT_OIL, sp->shp_tech));
170 max_oil = mp->m_item[I_OIL];
171 if (sp->shp_item[I_OIL] + oil_gained > max_oil)
172 oil_gained = max_oil - sp->shp_item[I_OIL];
173 if (product->p_nrdep != 0 && oil_gained > 0) {
174 resource = (unsigned char *)sectp + product->p_nrndx;
175 if (*resource * 100 < product->p_nrdep * oil_gained)
176 oil_gained = *resource * 100 / product->p_nrdep;
177 dep = roundavg(oil_gained * product->p_nrdep / 100.0);
178 if (CANT_HAPPEN(dep > *resource))
182 sp->shp_item[I_OIL] += oil_gained;
185 if (np->nat_money >= 0
186 && (mp->m_flags & M_FOOD) && sectp->sct_type == SCT_WATER) {
188 += roundavg(total_work(100, etus,
189 sp->shp_item[I_CIVIL],
190 sp->shp_item[I_MILIT],
193 * sp->shp_effic / 100.0
194 * sectp->sct_fertil / 100.0
195 * prod_eff(SCT_AGRI, sp->shp_tech));
198 if ((n = feed_ship(sp, etus)) > 0) {
199 wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
201 nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
203 max_food = mp->m_item[I_FOOD];
204 if (sp->shp_item[I_FOOD] > max_food)
205 sp->shp_item[I_FOOD] = max_food;
207 * do plague stuff. plague can't break out on ships,
208 * but it can still kill people.
210 pstage = sp->shp_pstage;
211 ptime = sp->shp_ptime;
212 if (pstage != PLG_HEALTHY) {
213 n = plague_people(np, sp->shp_item, &pstage, &ptime, etus);
217 "PLAGUE deaths reported on %s\n", prship(sp));
218 nreport(sp->shp_own, N_DIE_PLAGUE, 0, 1);
221 wu(0, sp->shp_own, "%s battling PLAGUE\n", prship(sp));
224 /* Are we still incubating? */
226 /* Yes. Will it turn "infectious" next time? */
228 /* Yes. Report an outbreak. */
230 "Outbreak of PLAGUE on %s!\n", prship(sp));
231 nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
234 /* It has already moved on to "infectious" */
236 "%s battling PLAGUE\n", prship(sp));
240 /* Has the plague moved to "incubation" yet? */
242 /* Yes. Will it turn "infectious" next time? */
244 /* Yes. Report an outbreak. */
246 "Outbreak of PLAGUE on %s!\n", prship(sp));
247 nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
255 sp->shp_pstage = pstage;
256 sp->shp_ptime = ptime;
258 pops[sp->shp_own] += sp->shp_item[I_CIVIL];
264 * idea is: a sector full of workers can fix up eight
265 * battleships +8 % eff each etu. This will cost around
266 * 8 * 8 * $40 = $2560!
269 shiprepair(struct shpstr *ship, struct natstr *np, struct bp *bp, int etus)
282 mp = &mchr[(int)ship->shp_type];
283 sp = getsectp(ship->shp_x, ship->shp_y);
285 if ((sp->sct_own != ship->shp_own) && (sp->sct_own != 0)) {
286 rel = getrel(getnatp(sp->sct_own), ship->shp_own);
293 /* only military can work on a military boat */
295 wf = etus * ship->shp_item[I_MILIT] / 2;
297 wf = etus * (ship->shp_item[I_CIVIL] / 2 + ship->shp_item[I_MILIT] / 5);
299 if (sp->sct_type != SCT_HARBR) {
303 if (!player->simulation)
304 avail = wf + sp->sct_avail * 100;
306 avail = wf + bp_get_avail(bp, sp) * 100;
309 w_p_eff = SHP_BLD_WORK(mp->m_lcm, mp->m_hcm);
311 if ((sp->sct_off) && (sp->sct_own == ship->shp_own))
315 if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
318 if (ship->shp_effic == 100) {
319 /* ship is ok; no repairs needed */
323 delta = roundavg((double)avail / w_p_eff);
326 if (delta > (int)((float)etus * ship_grow_scale))
327 delta = (int)((float)etus * ship_grow_scale);
328 if (delta > 100 - ship->shp_effic)
329 delta = 100 - ship->shp_effic;
331 memset(mvec, 0, sizeof(mvec));
332 mvec[I_LCM] = mp->m_lcm;
333 mvec[I_HCM] = mp->m_hcm;
334 build = get_materials(sp, bp, mvec, delta);
336 if (sp->sct_type != SCT_HARBR)
339 wf -= build * w_p_eff;
342 * I didn't use roundavg here, because I want to penalize
343 * the player with a large number of ships.
345 if (!player->simulation)
346 avail = (sp->sct_avail * 100 + wf) / 100;
348 avail = (bp_get_avail(bp, sp) * 100 + wf) / 100;
351 if (!player->simulation)
352 sp->sct_avail = avail;
354 bp_put_avail(bp, sp, avail);
356 if (sp->sct_type != SCT_HARBR)
357 if ((build + ship->shp_effic) > 80) {
358 build = 80 - ship->shp_effic;
363 np->nat_money -= mult * mp->m_cost * build / 100.0;
364 if (!player->simulation)
365 ship->shp_effic += (signed char)build;
369 * returns the number who starved, if any.
372 feed_ship(struct shpstr *sp, int etus)
382 needed = (int)ceil(food_needed(sp->shp_item, etus));
385 if (needed > sp->shp_item[I_FOOD])
386 sp->shp_item[I_FOOD] += supply_commod(sp->shp_own,
387 sp->shp_x, sp->shp_y, I_FOOD,
388 needed - sp->shp_item[I_FOOD]);
389 if (needed > sp->shp_item[I_FOOD]) {
390 /* take from embarked land units, but don't starve them */
391 snxtitem_cargo(&ni, EF_LAND, EF_SHIP, sp->shp_uid);
392 while ((lp = nxtitemp(&ni)) && needed > sp->shp_item[I_FOOD]) {
393 give = lp->lnd_item[I_FOOD] - food_needed(lp->lnd_item, etus);
396 take = MIN((int)give, needed - sp->shp_item[I_FOOD]);
397 sp->shp_item[I_FOOD] += take;
398 lp->lnd_item[I_FOOD] -= take;
402 return feed_people(sp->shp_item, etus);