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:
56 #define MIN(x,y) ((x) > (y) ? (y) : (x))
59 static int shiprepair(register struct shpstr *, int *, struct natstr *,
61 static void upd_ship(register struct shpstr *, int, register int,
62 struct natstr *, int *, int);
65 prod_ship(int etus, int natnum, int *bp, int build)
66 /* build = 1, maintain = 0 */
68 register struct shpstr *sp;
71 extern long sea_money[MAXNOC];
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, n, 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, int shipno, register int etus,
114 struct natstr *np, int *bp, int build)
115 /* build = 1, maintain = 0 */
118 struct sctstr *sectp;
125 struct pchrstr *product;
129 extern double money_ship;
134 mp = &mchr[(int)sp->shp_type];
135 getvec(VT_ITEM, vec, (s_char *)sp, EF_SHIP);
137 if (np->nat_priorities[PRI_SBUILD] == 0 || np->nat_money < 0)
139 if (sp->shp_effic < SHIP_MINEFF ||
140 !shiprepair(sp, vec, np, bp, etus)) {
141 makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x,
148 if (np->nat_level[NAT_TLEV] < sp->shp_tech * 0.85)
150 cost = -(mult * etus * dmin(0.0, money_ship * mp->m_cost));
151 if ((np->nat_priorities[PRI_SMAINT] == 0 ||
152 np->nat_money < cost) && !player->simulation) {
153 if ((eff = sp->shp_effic - etus / 5) < SHIP_MINEFF) {
155 "%s lost to lack of maintenance\n", prship(sp));
156 makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x,
162 "%s lost %d%% to lack of maintenance\n",
163 prship(sp), sp->shp_effic - eff);
166 np->nat_money -= cost;
169 sectp = getsectp(sp->shp_x, sp->shp_y);
170 if (((mp->m_flags & M_OIL) && (sectp->sct_type == SCT_WATER))
171 && !player->simulation) {
173 * take care of oil production
175 oil_gained = roundavg((vec[I_CIVIL] * etus / 10000.0)
177 vec[I_OIL] += oil_gained;
178 max_oil = vl_find(V_OIL, mp->m_vtype, mp->m_vamt, mp->m_nv);
179 if (vec[I_OIL] > max_oil)
180 vec[I_OIL] = max_oil;
181 product = &pchr[P_OIL];
182 if (product->p_nrdep != 0 && oil_gained > 0) {
183 resource = ((s_char *)sectp) + product->p_nrndx;
184 *resource -= roundavg(oil_gained *
185 product->p_nrdep / 100.0);
188 if (((mp->m_flags & M_FOOD) && (sectp->sct_type == SCT_WATER))
189 && !player->simulation) {
190 sectp = getsectp(sp->shp_x, sp->shp_y);
191 vec[I_FOOD] += ((vec[I_CIVIL] * etus) / 1000.0)
194 /* Military costs are now part of regular military costs, not ship costs */
195 /* np->nat_money += (int) (etus * vec[I_MILIT] * money_mil);*/
196 if (!player->simulation) {
197 if ((n = feed_ship(sp, vec, etus, &needed, 1)) > 0) {
198 wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
200 nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
202 max_food = vl_find(V_FOOD, mp->m_vtype, mp->m_vamt, mp->m_nv);
203 if (vec[I_FOOD] > max_food)
204 vec[I_FOOD] = max_food;
206 * do plague stuff. plague can't break out on ships,
207 * but it can still kill people.
209 getvec(VT_COND, cvec, (s_char *)sp, EF_SHIP);
210 if (cvec[C_PSTAGE] > 0) {
211 n = plague_people(np, vec, cvec, 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? */
223 if (n == cvec[C_PSTAGE]) {
224 /* Yes. Will it turn "infectious" next time? */
225 if (cvec[C_PTIME] <= etus) {
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? */
239 if (n != cvec[C_PSTAGE]) {
240 /* Yes. Will it turn "infectious" next time? */
241 if (cvec[C_PTIME] <= etus) {
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 putvec(VT_COND, cvec, (s_char *)sp, EF_SHIP);
255 putvec(VT_ITEM, vec, (s_char *)sp, EF_SHIP);
256 pops[sp->shp_own] += vec[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(register struct shpstr *ship, int *vec, struct natstr *np,
270 extern int ship_grow_scale;
276 int lcm_needed, hcm_needed;
285 mp = &mchr[(int)ship->shp_type];
286 sp = getsectp(ship->shp_x, ship->shp_y);
288 if ((sp->sct_own != ship->shp_own) && (sp->sct_own != 0)) {
289 rel = getrel(getnatp(sp->sct_own), ship->shp_own);
296 /* only military can work on a military boat */
297 if (ship->shp_glim > 0)
298 wf = etus * vec[I_MILIT] / 2;
300 wf = etus * (vec[I_CIVIL] / 2 + vec[I_MILIT] / 5);
302 if (sp->sct_type != SCT_HARBR) {
306 if (!player->simulation)
307 avail = wf + sp->sct_avail * 100;
309 avail = wf + gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
312 w_p_eff = 20 + (mp->m_lcm + 2 * mp->m_hcm);
314 if (sp->sct_type != SCT_HARBR) {
317 if (ship->shp_glim > 0) {
318 abs_max = vl_find(V_MILIT, mp->m_vtype,
319 mp->m_vamt, (int)mp->m_nv);
322 abs_max = vl_find(V_CIVIL, mp->m_vtype,
323 mp->m_vamt, (int)mp->m_nv);
326 abs_max = vl_find(V_MILIT, mp->m_vtype, mp->m_vamt,
333 logerror("Abs max of 0 for ship %d\n", ship->shp_uid);
336 avail -= (etus * (100 - ((amt * 100) / abs_max))) / 7;
337 /* think of it as entropy in action */
341 if (!player->simulation) {
342 if (opt_SHIP_DECAY) {
343 ship->shp_effic += avail / w_p_eff;
349 if ((sp->sct_off) && (sp->sct_own == ship->shp_own))
352 getvec(VT_ITEM, svec, (s_char *)sp, EF_SECTOR);
355 if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
358 if (ship->shp_effic == 100) {
359 /* ship is ok; no repairs needed */
363 left = 100 - ship->shp_effic;
364 delta = roundavg((double)avail / w_p_eff);
367 if (delta > etus * ship_grow_scale)
368 delta = etus * ship_grow_scale;
372 /* delta is the max amount we can grow */
374 left = 100 - ship->shp_effic;
378 leftp = ((float)left / 100.0);
379 memset(mvec, 0, sizeof(mvec));
380 mvec[I_LCM] = lcm_needed = ldround((double)(mp->m_lcm * leftp), 1);
381 mvec[I_HCM] = hcm_needed = ldround((double)(mp->m_hcm * leftp), 1);
383 get_materials(sp, bp, mvec, 0);
385 if (mvec[I_LCM] >= lcm_needed)
388 buildp = ((float)mvec[I_LCM] / (float)mp->m_lcm);
389 if (mvec[I_HCM] < hcm_needed)
390 buildp = MIN(buildp, ((float)mvec[I_HCM] / (float)mp->m_hcm));
392 build = ldround((double)(buildp * 100.0), 1);
393 memset(mvec, 0, sizeof(mvec));
394 mvec[I_LCM] = lcm_needed = roundavg((double)(mp->m_lcm * buildp));
395 mvec[I_HCM] = hcm_needed = roundavg((double)(mp->m_hcm * buildp));
397 get_materials(sp, bp, mvec, 1);
399 if (sp->sct_type != SCT_HARBR)
401 wf -= build * w_p_eff;
404 * I didn't use roundavg here, because I want to penalize
405 * the player with a large number of ships.
407 if (!player->simulation)
408 avail = (sp->sct_avail * 100 + wf) / 100;
410 avail = (gt_bg_nmbr(bp, sp, I_MAX + 1) * 100 + wf) / 100;
413 if (!player->simulation)
414 sp->sct_avail = avail;
416 pt_bg_nmbr(bp, sp, I_MAX + 1, avail);
418 if (sp->sct_type != SCT_HARBR)
419 if ((build + ship->shp_effic) > 80) {
420 build = 80 - ship->shp_effic;
425 np->nat_money -= mult * mp->m_cost * build / 100.0;
426 if (!player->simulation)
427 ship->shp_effic += (s_char)build;
432 * returns the number who starved, if any.
435 feed_ship(struct shpstr *sp, register int *vec, int etus, int *needed,
438 extern double eatrate;
439 double food_eaten, land_eaten;
445 int starved, lvec[I_MAX + 1];
448 s_char *nxtitemp(struct nstr_item *np, int owner);
451 return 0; /* no food no work to do */
454 (etus * eatrate) * (vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW]);
455 ifood_eaten = (int)food_eaten;
456 if ((food_eaten - ifood_eaten) > 0)
460 if (!player->simulation && food_eaten > vec[I_FOOD])
461 vec[I_FOOD] += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
462 I_FOOD, (ifood_eaten - vec[I_FOOD]));
464 if (food_eaten > vec[I_FOOD]) {
465 /* doit - only steal food from land units during the update */
466 if (sp->shp_nland > 0 && doit) {
467 snxtitem_all(&ni, EF_LAND);
468 while ((lp = (struct lndstr *)nxtitemp(&ni, 0)) &&
469 (food_eaten > vec[I_FOOD])) {
470 if (lp->lnd_ship != sp->shp_uid)
472 need = ifood_eaten - vec[I_FOOD];
473 getvec(VT_ITEM, lvec, (s_char *)lp, EF_LAND);
474 land_eaten = (etus * eatrate) * (double)lnd_getmil(lp);
475 if (lvec[I_FOOD] - need > land_eaten) {
477 lvec[I_FOOD] -= need;
478 } else if ((lvec[I_FOOD] - land_eaten) > 0) {
479 vec[I_FOOD] += (lvec[I_FOOD] - land_eaten);
480 lvec[I_FOOD] -= (lvec[I_FOOD] - land_eaten);
482 putvec(VT_ITEM, lvec, (s_char *)lp, EF_LAND);
487 if (food_eaten > vec[I_FOOD]) {
488 *needed = food_eaten - vec[I_FOOD];
489 if (*needed < (food_eaten - vec[I_FOOD]))
491 if (opt_NEW_STARVE) {
492 can_eat = (vec[I_FOOD] / (etus * eatrate));
493 total_people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
495 /* only want to starve off at most 1/2 the populace. */
496 if (can_eat < (total_people / 2))
497 can_eat = total_people / 2;
499 to_starve = total_people - can_eat;
500 while (to_starve && vec[I_UW]) {
505 while (to_starve && vec[I_CIVIL]) {
510 while (to_starve && vec[I_MILIT]) {
517 } else { /* ! opt_NEW_STARVE */
518 people_left = (vec[I_FOOD] + 0.01) / (food_eaten + 0.01);
519 starved = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
520 /* only want to starve off at most 1/2 the populace. */
521 if (people_left < 0.5)
523 vec[I_CIVIL] = (int)(vec[I_CIVIL] * people_left);
524 vec[I_MILIT] = (int)(vec[I_MILIT] * people_left);
525 vec[I_UW] = (int)(vec[I_UW] * people_left);
526 starved -= vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
530 vec[I_FOOD] -= (int)food_eaten;