2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * ship.c: Do production for ships
30 * Known contributors to this file:
57 #define MIN(x,y) ((x) > (y) ? (y) : (x))
60 static int shiprepair(register struct shpstr *, struct natstr *,
62 static void upd_ship(register struct shpstr *, register int,
63 struct natstr *, int *, int);
66 prod_ship(int etus, int natnum, int *bp, int build)
67 /* build = 1, maintain = 0 */
69 register struct shpstr *sp;
73 int lastx = 9999, lasty = 9999;
75 bp_enable_cachepath();
76 for (n = 0; NULL != (sp = getshipp(n)); n++) {
79 if (sp->shp_own != natnum)
81 np = getnatp(sp->shp_own);
82 start_money = np->nat_money;
83 if (lastx == 9999 || lasty == 9999) {
87 if (lastx != sp->shp_x || lasty != sp->shp_y) {
89 bp_disable_cachepath();
91 bp_enable_cachepath();
93 upd_ship(sp, etus, np, bp, build);
94 if (build && !player->simulation) /* make sure to only autonav once */
95 nav_ship(sp); /* autonav the ship */
96 sea_money[sp->shp_own] += np->nat_money - start_money;
97 if ((build && (np->nat_money != start_money)) || (!build))
99 if (player->simulation)
100 np->nat_money = start_money;
102 bp_disable_cachepath();
103 bp_clear_cachepath();
106 if (build && !player->simulation) /* make sure to only sail once */
113 upd_ship(register struct shpstr *sp, register int etus,
114 struct natstr *np, int *bp, int build)
115 /* build = 1, maintain = 0 */
117 struct sctstr *sectp;
120 u_short pstage, ptime;
124 struct pchrstr *product;
132 mp = &mchr[(int)sp->shp_type];
134 if (np->nat_priorities[PRI_SBUILD] == 0 || np->nat_money < 0)
136 if (sp->shp_effic < SHIP_MINEFF || !shiprepair(sp, np, bp, etus)) {
137 makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x,
144 if (np->nat_level[NAT_TLEV] < sp->shp_tech * 0.85)
146 cost = -(mult * etus * dmin(0.0, money_ship * mp->m_cost));
147 if ((np->nat_priorities[PRI_SMAINT] == 0 || np->nat_money < cost)
148 && !player->simulation) {
149 if ((eff = sp->shp_effic - etus / 5) < SHIP_MINEFF) {
151 "%s lost to lack of maintenance\n", prship(sp));
152 makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x,
158 "%s lost %d%% to lack of maintenance\n",
159 prship(sp), sp->shp_effic - eff);
162 np->nat_money -= cost;
165 if (!player->simulation) {
166 getvec(VT_ITEM, vec, (s_char *)sp, EF_SHIP);
167 sectp = getsectp(sp->shp_x, sp->shp_y);
169 if ((mp->m_flags & M_OIL) && sectp->sct_type == SCT_WATER) {
171 * take care of oil production
173 oil_gained = roundavg((vec[I_CIVIL] * etus / 10000.0)
175 vec[I_OIL] += oil_gained;
176 max_oil = vl_find(V_OIL, mp->m_vtype, mp->m_vamt, mp->m_nv);
177 if (vec[I_OIL] > max_oil)
178 vec[I_OIL] = max_oil;
179 product = &pchr[P_OIL];
180 if (product->p_nrdep != 0 && oil_gained > 0) {
181 resource = ((s_char *)sectp) + product->p_nrndx;
182 *resource -= roundavg(oil_gained *
183 product->p_nrdep / 100.0);
186 if ((mp->m_flags & M_FOOD) && sectp->sct_type == SCT_WATER) {
187 vec[I_FOOD] += ((vec[I_CIVIL] * etus) / 1000.0)
190 if ((n = feed_ship(sp, vec, etus, &needed, 1)) > 0) {
191 wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
193 nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
195 max_food = vl_find(V_FOOD, mp->m_vtype, mp->m_vamt, mp->m_nv);
196 if (vec[I_FOOD] > max_food)
197 vec[I_FOOD] = max_food;
199 * do plague stuff. plague can't break out on ships,
200 * but it can still kill people.
202 pstage = sp->shp_pstage;
203 ptime = sp->shp_ptime;
204 if (pstage != PLG_HEALTHY) {
205 n = plague_people(np, vec, &pstage, &ptime, etus);
209 "PLAGUE deaths reported on %s\n", prship(sp));
210 nreport(sp->shp_own, N_DIE_PLAGUE, 0, 1);
213 wu(0, sp->shp_own, "%s battling PLAGUE\n", prship(sp));
216 /* Are we still incubating? */
218 /* Yes. Will it turn "infectious" next time? */
220 /* Yes. Report an outbreak. */
222 "Outbreak of PLAGUE on %s!\n", prship(sp));
223 nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
226 /* It has already moved on to "infectious" */
228 "%s battling PLAGUE\n", prship(sp));
232 /* Has the plague moved to "incubation" yet? */
234 /* Yes. Will it turn "infectious" next time? */
236 /* Yes. Report an outbreak. */
238 "Outbreak of PLAGUE on %s!\n", prship(sp));
239 nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
247 sp->shp_pstage = pstage;
248 sp->shp_ptime = ptime;
250 putvec(VT_ITEM, vec, (s_char *)sp, EF_SHIP);
251 pops[sp->shp_own] += vec[I_CIVIL];
257 * idea is: a sector full of workers can fix up eight
258 * battleships +8 % eff each etu. This will cost around
259 * 8 * 8 * $40 = $2560!
262 shiprepair(register struct shpstr *ship, struct natstr *np,
270 int lcm_needed, hcm_needed;
278 mp = &mchr[(int)ship->shp_type];
279 sp = getsectp(ship->shp_x, ship->shp_y);
281 if ((sp->sct_own != ship->shp_own) && (sp->sct_own != 0)) {
282 rel = getrel(getnatp(sp->sct_own), ship->shp_own);
289 /* only military can work on a military boat */
290 if (ship->shp_glim > 0)
291 wf = etus * ship->shp_item[I_MILIT] / 2;
293 wf = etus * (ship->shp_item[I_CIVIL] / 2 + ship->shp_item[I_MILIT] / 5);
295 if (sp->sct_type != SCT_HARBR) {
299 if (!player->simulation)
300 avail = wf + sp->sct_avail * 100;
302 avail = wf + gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
305 w_p_eff = 20 + (mp->m_lcm + 2 * mp->m_hcm);
307 if (sp->sct_type != SCT_HARBR) {
310 if (ship->shp_glim > 0) {
311 abs_max = vl_find(V_MILIT, mp->m_vtype,
312 mp->m_vamt, (int)mp->m_nv);
313 amt = ship->shp_item[I_MILIT];
315 abs_max = vl_find(V_CIVIL, mp->m_vtype,
316 mp->m_vamt, (int)mp->m_nv);
317 amt = ship->shp_item[I_CIVIL];
319 abs_max = vl_find(V_MILIT, mp->m_vtype, mp->m_vamt,
321 amt = ship->shp_item[I_MILIT];
326 logerror("Abs max of 0 for ship %d\n", ship->shp_uid);
329 avail -= (etus * (100 - ((amt * 100) / abs_max))) / 7;
330 /* think of it as entropy in action */
334 if (!player->simulation) {
335 if (opt_SHIP_DECAY) {
336 ship->shp_effic += avail / w_p_eff;
342 if ((sp->sct_off) && (sp->sct_own == ship->shp_own))
346 if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
349 if (ship->shp_effic == 100) {
350 /* ship is ok; no repairs needed */
354 left = 100 - ship->shp_effic;
355 delta = roundavg((double)avail / w_p_eff);
358 if (delta > etus * ship_grow_scale)
359 delta = etus * ship_grow_scale;
363 /* delta is the max amount we can grow */
365 left = 100 - ship->shp_effic;
369 leftp = ((float)left / 100.0);
370 memset(mvec, 0, sizeof(mvec));
371 mvec[I_LCM] = lcm_needed = ldround((double)(mp->m_lcm * leftp), 1);
372 mvec[I_HCM] = hcm_needed = ldround((double)(mp->m_hcm * leftp), 1);
374 get_materials(sp, bp, mvec, 0);
376 if (mvec[I_LCM] >= lcm_needed)
379 buildp = ((float)mvec[I_LCM] / (float)mp->m_lcm);
380 if (mvec[I_HCM] < hcm_needed)
381 buildp = MIN(buildp, ((float)mvec[I_HCM] / (float)mp->m_hcm));
383 build = ldround((double)(buildp * 100.0), 1);
384 memset(mvec, 0, sizeof(mvec));
385 mvec[I_LCM] = lcm_needed = roundavg((double)(mp->m_lcm * buildp));
386 mvec[I_HCM] = hcm_needed = roundavg((double)(mp->m_hcm * buildp));
388 get_materials(sp, bp, mvec, 1);
390 if (sp->sct_type != SCT_HARBR)
392 wf -= build * w_p_eff;
395 * I didn't use roundavg here, because I want to penalize
396 * the player with a large number of ships.
398 if (!player->simulation)
399 avail = (sp->sct_avail * 100 + wf) / 100;
401 avail = (gt_bg_nmbr(bp, sp, I_MAX + 1) * 100 + wf) / 100;
404 if (!player->simulation)
405 sp->sct_avail = avail;
407 pt_bg_nmbr(bp, sp, I_MAX + 1, avail);
409 if (sp->sct_type != SCT_HARBR)
410 if ((build + ship->shp_effic) > 80) {
411 build = 80 - ship->shp_effic;
416 np->nat_money -= mult * mp->m_cost * build / 100.0;
417 if (!player->simulation)
418 ship->shp_effic += (s_char)build;
423 * returns the number who starved, if any.
426 feed_ship(struct shpstr *sp, register int *vec, int etus, int *needed,
429 double food_eaten, land_eaten;
439 return 0; /* no food no work to do */
442 (etus * eatrate) * (vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW]);
443 ifood_eaten = (int)food_eaten;
444 if ((food_eaten - ifood_eaten) > 0)
448 if (!player->simulation && food_eaten > vec[I_FOOD])
449 vec[I_FOOD] += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
450 I_FOOD, (ifood_eaten - vec[I_FOOD]));
452 if (food_eaten > vec[I_FOOD]) {
453 /* doit - only steal food from land units during the update */
454 if (sp->shp_nland > 0 && doit) {
455 snxtitem_all(&ni, EF_LAND);
456 while ((lp = (struct lndstr *)nxtitemp(&ni, 0)) &&
457 (food_eaten > vec[I_FOOD])) {
458 if (lp->lnd_ship != sp->shp_uid)
460 need = ifood_eaten - vec[I_FOOD];
461 land_eaten = (etus * eatrate) * (double)lnd_getmil(lp);
462 if (lp->lnd_item[I_FOOD] - need > land_eaten) {
464 lp->lnd_item[I_FOOD] -= need;
465 } else if (lp->lnd_item[I_FOOD] - land_eaten > 0) {
466 vec[I_FOOD] += lp->lnd_item[I_FOOD] - land_eaten;
467 lp->lnd_item[I_FOOD] -= lp->lnd_item[I_FOOD] - land_eaten;
473 if (food_eaten > vec[I_FOOD]) {
474 *needed = food_eaten - vec[I_FOOD];
475 if (*needed < (food_eaten - vec[I_FOOD]))
477 can_eat = (vec[I_FOOD] / (etus * eatrate));
478 total_people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
480 /* only want to starve off at most 1/2 the populace. */
481 if (can_eat < (total_people / 2))
482 can_eat = total_people / 2;
484 to_starve = total_people - can_eat;
485 while (to_starve && vec[I_UW]) {
490 while (to_starve && vec[I_CIVIL]) {
495 while (to_starve && vec[I_MILIT]) {
503 vec[I_FOOD] -= (int)food_eaten;