2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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
50 static void shiprepair(struct shpstr *, struct natstr *, int *, int);
51 static void upd_ship(struct shpstr *, int, struct natstr *, int *, int);
52 static int feed_ship(struct shpstr *, int);
55 prod_ship(int etus, int natnum, int *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, int *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 ((mp->m_flags & M_OIL) && sectp->sct_type == SCT_WATER) {
160 product = &pchr[dchr[SCT_OIL].d_prd];
161 oil_gained = 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_oil / 100.0
168 * prod_eff(SCT_OIL, sp->shp_tech));
169 max_oil = mp->m_item[I_OIL];
170 if (sp->shp_item[I_OIL] + oil_gained > max_oil)
171 oil_gained = max_oil - sp->shp_item[I_OIL];
172 if (product->p_nrdep != 0 && oil_gained > 0) {
173 resource = (unsigned char *)sectp + product->p_nrndx;
174 if (*resource * 100 < product->p_nrdep * oil_gained)
175 oil_gained = *resource * 100 / product->p_nrdep;
176 dep = roundavg(oil_gained * product->p_nrdep / 100.0);
177 if (CANT_HAPPEN(dep > *resource))
181 sp->shp_item[I_OIL] += oil_gained;
184 if ((mp->m_flags & M_FOOD) && sectp->sct_type == SCT_WATER) {
186 += roundavg(total_work(100, etus,
187 sp->shp_item[I_CIVIL],
188 sp->shp_item[I_MILIT],
191 * sp->shp_effic / 100.0
192 * sectp->sct_fertil / 100.0
193 * prod_eff(SCT_AGRI, sp->shp_tech));
196 if ((n = feed_ship(sp, etus)) > 0) {
197 wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
199 nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
201 max_food = mp->m_item[I_FOOD];
202 if (sp->shp_item[I_FOOD] > max_food)
203 sp->shp_item[I_FOOD] = max_food;
205 * do plague stuff. plague can't break out on ships,
206 * but it can still kill people.
208 pstage = sp->shp_pstage;
209 ptime = sp->shp_ptime;
210 if (pstage != PLG_HEALTHY) {
211 n = plague_people(np, sp->shp_item, &pstage, &ptime, etus);
215 "PLAGUE deaths reported on %s\n", prship(sp));
216 nreport(sp->shp_own, N_DIE_PLAGUE, 0, 1);
219 wu(0, sp->shp_own, "%s battling PLAGUE\n", prship(sp));
222 /* Are we still incubating? */
224 /* Yes. Will it turn "infectious" next time? */
226 /* Yes. Report an outbreak. */
228 "Outbreak of PLAGUE on %s!\n", prship(sp));
229 nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
232 /* It has already moved on to "infectious" */
234 "%s battling PLAGUE\n", prship(sp));
238 /* Has the plague moved to "incubation" yet? */
240 /* Yes. Will it turn "infectious" next time? */
242 /* Yes. Report an outbreak. */
244 "Outbreak of PLAGUE on %s!\n", prship(sp));
245 nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
253 sp->shp_pstage = pstage;
254 sp->shp_ptime = ptime;
256 pops[sp->shp_own] += sp->shp_item[I_CIVIL];
262 * idea is: a sector full of workers can fix up eight
263 * battleships +8 % eff each etu. This will cost around
264 * 8 * 8 * $40 = $2560!
267 shiprepair(struct shpstr *ship, struct natstr *np, int *bp, int etus)
274 int lcm_needed, hcm_needed;
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 */
294 if (ship->shp_glim > 0)
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 + gt_bg_nmbr(bp, sp, I_MAX + 1) * 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 left = 100 - ship->shp_effic;
324 delta = roundavg((double)avail / w_p_eff);
327 if (delta > (int)((float)etus * ship_grow_scale))
328 delta = (int)((float)etus * ship_grow_scale);
332 /* delta is the max amount we can grow */
334 left = 100 - ship->shp_effic;
338 leftp = left / 100.0;
339 memset(mvec, 0, sizeof(mvec));
340 mvec[I_LCM] = lcm_needed = ldround(mp->m_lcm * leftp, 1);
341 mvec[I_HCM] = hcm_needed = ldround(mp->m_hcm * leftp, 1);
342 get_materials(sp, bp, mvec, 0);
345 if (mvec[I_LCM] < lcm_needed)
346 buildp = MIN(buildp, (float)mvec[I_LCM] / (float)mp->m_lcm);
347 if (mvec[I_HCM] < hcm_needed)
348 buildp = MIN(buildp, (float)mvec[I_HCM] / (float)mp->m_hcm);
350 build = ldround(buildp * 100.0, 1);
351 memset(mvec, 0, sizeof(mvec));
352 mvec[I_LCM] = roundavg(mp->m_lcm * buildp);
353 mvec[I_HCM] = roundavg(mp->m_hcm * buildp);
354 get_materials(sp, bp, mvec, 1);
356 if (sp->sct_type != SCT_HARBR)
358 wf -= build * w_p_eff;
361 * I didn't use roundavg here, because I want to penalize
362 * the player with a large number of ships.
364 if (!player->simulation)
365 avail = (sp->sct_avail * 100 + wf) / 100;
367 avail = (gt_bg_nmbr(bp, sp, I_MAX + 1) * 100 + wf) / 100;
370 if (!player->simulation)
371 sp->sct_avail = avail;
373 pt_bg_nmbr(bp, sp, I_MAX + 1, avail);
375 if (sp->sct_type != SCT_HARBR)
376 if ((build + ship->shp_effic) > 80) {
377 build = 80 - ship->shp_effic;
382 np->nat_money -= mult * mp->m_cost * build / 100.0;
383 if (!player->simulation)
384 ship->shp_effic += (signed char)build;
388 * returns the number who starved, if any.
391 feed_ship(struct shpstr *sp, int etus)
401 needed = (int)ceil(food_needed(sp->shp_item, etus));
404 if (needed > sp->shp_item[I_FOOD])
405 sp->shp_item[I_FOOD] += supply_commod(sp->shp_own,
406 sp->shp_x, sp->shp_y, I_FOOD,
407 needed - sp->shp_item[I_FOOD]);
408 if (needed > sp->shp_item[I_FOOD]) {
409 /* take from embarked land units, but don't starve them */
410 snxtitem_all(&ni, EF_LAND);
411 while ((lp = nxtitemp(&ni)) && needed > sp->shp_item[I_FOOD]) {
412 if (lp->lnd_ship != sp->shp_uid)
414 give = lp->lnd_item[I_FOOD] - food_needed(lp->lnd_item, etus);
417 take = MIN((int)give, needed - sp->shp_item[I_FOOD]);
418 sp->shp_item[I_FOOD] += take;
419 lp->lnd_item[I_FOOD] -= take;
423 return feed_people(sp->shp_item, etus);