2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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(struct shpstr *, struct natstr *,
61 static void upd_ship(struct shpstr *, int,
62 struct natstr *, int *, int);
65 prod_ship(int etus, int natnum, int *bp, int build)
66 /* build = 1, maintain = 0 */
72 int lastx = 9999, lasty = 9999;
74 bp_enable_cachepath();
75 for (n = 0; NULL != (sp = getshipp(n)); n++) {
78 if (sp->shp_own != natnum)
80 np = getnatp(sp->shp_own);
81 start_money = np->nat_money;
82 if (lastx == 9999 || lasty == 9999) {
86 if (lastx != sp->shp_x || lasty != sp->shp_y) {
88 bp_disable_cachepath();
90 bp_enable_cachepath();
92 upd_ship(sp, etus, np, bp, build);
93 if (build && !player->simulation) /* make sure to only autonav once */
94 nav_ship(sp); /* autonav the ship */
95 sea_money[sp->shp_own] += np->nat_money - start_money;
96 if ((build && (np->nat_money != start_money)) || (!build))
98 if (player->simulation)
99 np->nat_money = start_money;
101 bp_disable_cachepath();
102 bp_clear_cachepath();
105 if (build && !player->simulation) /* make sure to only sail once */
112 upd_ship(struct shpstr *sp, int etus,
113 struct natstr *np, int *bp, int build)
114 /* build = 1, maintain = 0 */
116 struct sctstr *sectp;
122 struct pchrstr *product;
131 mp = &mchr[(int)sp->shp_type];
133 if (np->nat_priorities[PRI_SBUILD] == 0 || np->nat_money < 0)
135 if (sp->shp_effic < SHIP_MINEFF || !shiprepair(sp, np, bp, etus)) {
136 makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x,
143 if (np->nat_level[NAT_TLEV] < sp->shp_tech * 0.85)
145 cost = -(mult * etus * dmin(0.0, money_ship * mp->m_cost));
146 if ((np->nat_priorities[PRI_SMAINT] == 0 || np->nat_money < cost)
147 && !player->simulation) {
148 if ((eff = sp->shp_effic - etus / 5) < SHIP_MINEFF) {
150 "%s lost to lack of maintenance\n", prship(sp));
151 makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x,
157 "%s lost %d%% to lack of maintenance\n",
158 prship(sp), sp->shp_effic - eff);
161 np->nat_money -= cost;
164 if (!player->simulation) {
165 sectp = getsectp(sp->shp_x, sp->shp_y);
167 if ((mp->m_flags & M_OIL) && sectp->sct_type == SCT_WATER) {
169 * take care of oil production
171 product = &pchr[P_OIL];
172 oil_gained = roundavg(total_work(100, etus,
173 sp->shp_item[I_CIVIL],
174 sp->shp_item[I_MILIT],
176 * (double)sp->shp_effic / 100.0
177 * (double)sectp->sct_oil / 100.0
178 * prod_eff(product, sp->shp_tech));
179 max_oil = mp->m_item[I_OIL];
180 if (sp->shp_item[I_OIL] + oil_gained > max_oil)
181 oil_gained = max_oil - sp->shp_item[I_OIL];
182 sp->shp_item[I_OIL] += oil_gained;
183 if (product->p_nrdep != 0 && oil_gained > 0) {
184 resource = (u_char *)sectp + product->p_nrndx;
185 dep = roundavg(oil_gained * product->p_nrdep / 100.0);
186 if (CANT_HAPPEN(dep > *resource))
191 if ((mp->m_flags & M_FOOD) && sectp->sct_type == SCT_WATER) {
192 product = &pchr[P_FOOD];
193 sp->shp_item[I_FOOD] += roundavg(total_work(100, etus,
194 sp->shp_item[I_CIVIL],
195 sp->shp_item[I_MILIT],
197 * (double)sp->shp_effic / 100.0
198 * (double)sectp->sct_fertil / 100.0
199 * prod_eff(product, sp->shp_tech));
201 if ((n = feed_ship(sp, etus, &needed, 1)) > 0) {
202 wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
204 nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
206 max_food = mp->m_item[I_FOOD];
207 if (sp->shp_item[I_FOOD] > max_food)
208 sp->shp_item[I_FOOD] = max_food;
210 * do plague stuff. plague can't break out on ships,
211 * but it can still kill people.
213 pstage = sp->shp_pstage;
214 ptime = sp->shp_ptime;
215 if (pstage != PLG_HEALTHY) {
216 n = plague_people(np, sp->shp_item, &pstage, &ptime, etus);
220 "PLAGUE deaths reported on %s\n", prship(sp));
221 nreport(sp->shp_own, N_DIE_PLAGUE, 0, 1);
224 wu(0, sp->shp_own, "%s battling PLAGUE\n", prship(sp));
227 /* Are we still incubating? */
229 /* Yes. Will it turn "infectious" next time? */
231 /* Yes. Report an outbreak. */
233 "Outbreak of PLAGUE on %s!\n", prship(sp));
234 nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
237 /* It has already moved on to "infectious" */
239 "%s battling PLAGUE\n", prship(sp));
243 /* Has the plague moved to "incubation" yet? */
245 /* Yes. Will it turn "infectious" next time? */
247 /* Yes. Report an outbreak. */
249 "Outbreak of PLAGUE on %s!\n", prship(sp));
250 nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
258 sp->shp_pstage = pstage;
259 sp->shp_ptime = ptime;
261 pops[sp->shp_own] += sp->shp_item[I_CIVIL];
267 * idea is: a sector full of workers can fix up eight
268 * battleships +8 % eff each etu. This will cost around
269 * 8 * 8 * $40 = $2560!
272 shiprepair(struct shpstr *ship, struct natstr *np,
280 int lcm_needed, hcm_needed;
288 mp = &mchr[(int)ship->shp_type];
289 sp = getsectp(ship->shp_x, ship->shp_y);
291 if ((sp->sct_own != ship->shp_own) && (sp->sct_own != 0)) {
292 rel = getrel(getnatp(sp->sct_own), ship->shp_own);
299 /* only military can work on a military boat */
300 if (ship->shp_glim > 0)
301 wf = etus * ship->shp_item[I_MILIT] / 2;
303 wf = etus * (ship->shp_item[I_CIVIL] / 2 + ship->shp_item[I_MILIT] / 5);
305 if (sp->sct_type != SCT_HARBR) {
309 if (!player->simulation)
310 avail = wf + sp->sct_avail * 100;
312 avail = wf + gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
315 w_p_eff = SHP_BLD_WORK(mp->m_lcm, mp->m_hcm);
317 if ((sp->sct_off) && (sp->sct_own == ship->shp_own))
321 if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
324 if (ship->shp_effic == 100) {
325 /* ship is ok; no repairs needed */
329 left = 100 - ship->shp_effic;
330 delta = roundavg((double)avail / w_p_eff);
333 if (delta > etus * ship_grow_scale)
334 delta = etus * ship_grow_scale;
338 /* delta is the max amount we can grow */
340 left = 100 - ship->shp_effic;
344 leftp = ((float)left / 100.0);
345 memset(mvec, 0, sizeof(mvec));
346 mvec[I_LCM] = lcm_needed = ldround((double)(mp->m_lcm * leftp), 1);
347 mvec[I_HCM] = hcm_needed = ldround((double)(mp->m_hcm * leftp), 1);
349 get_materials(sp, bp, mvec, 0);
351 if (mvec[I_LCM] >= lcm_needed)
354 buildp = ((float)mvec[I_LCM] / (float)mp->m_lcm);
355 if (mvec[I_HCM] < hcm_needed)
356 buildp = MIN(buildp, ((float)mvec[I_HCM] / (float)mp->m_hcm));
358 build = ldround((double)(buildp * 100.0), 1);
359 memset(mvec, 0, sizeof(mvec));
360 mvec[I_LCM] = lcm_needed = roundavg((double)(mp->m_lcm * buildp));
361 mvec[I_HCM] = hcm_needed = roundavg((double)(mp->m_hcm * buildp));
363 get_materials(sp, bp, mvec, 1);
365 if (sp->sct_type != SCT_HARBR)
367 wf -= build * w_p_eff;
370 * I didn't use roundavg here, because I want to penalize
371 * the player with a large number of ships.
373 if (!player->simulation)
374 avail = (sp->sct_avail * 100 + wf) / 100;
376 avail = (gt_bg_nmbr(bp, sp, I_MAX + 1) * 100 + wf) / 100;
379 if (!player->simulation)
380 sp->sct_avail = avail;
382 pt_bg_nmbr(bp, sp, I_MAX + 1, avail);
384 if (sp->sct_type != SCT_HARBR)
385 if ((build + ship->shp_effic) > 80) {
386 build = 80 - ship->shp_effic;
391 np->nat_money -= mult * mp->m_cost * build / 100.0;
392 if (!player->simulation)
393 ship->shp_effic += (s_char)build;
398 * returns the number who starved, if any.
401 feed_ship(struct shpstr *sp, int etus, int *needed, int doit)
403 double food_eaten, land_eaten;
413 return 0; /* no food no work to do */
416 = sp->shp_item[I_CIVIL] + sp->shp_item[I_MILIT] + sp->shp_item[I_UW];
417 food_eaten = etus * eatrate * total_people;
418 ifood_eaten = (int)food_eaten;
419 if (food_eaten - ifood_eaten > 0)
423 if (!player->simulation && ifood_eaten > sp->shp_item[I_FOOD])
425 += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
426 I_FOOD, ifood_eaten - sp->shp_item[I_FOOD]);
428 /* doit - only steal food from land units during the update */
429 if (ifood_eaten > sp->shp_item[I_FOOD] && sp->shp_nland > 0 && doit) {
430 snxtitem_all(&ni, EF_LAND);
431 while ((lp = (struct lndstr *)nxtitemp(&ni, 0)) &&
432 ifood_eaten > sp->shp_item[I_FOOD]) {
433 if (lp->lnd_ship != sp->shp_uid)
435 need = ifood_eaten - sp->shp_item[I_FOOD];
436 land_eaten = etus * eatrate * lnd_getmil(lp);
437 if (lp->lnd_item[I_FOOD] - need > land_eaten) {
438 sp->shp_item[I_FOOD] += need;
439 lp->lnd_item[I_FOOD] -= need;
440 } else if (lp->lnd_item[I_FOOD] - land_eaten > 0) {
441 sp->shp_item[I_FOOD] += lp->lnd_item[I_FOOD] - land_eaten;
442 lp->lnd_item[I_FOOD] -= lp->lnd_item[I_FOOD] - land_eaten;
447 if (ifood_eaten > sp->shp_item[I_FOOD]) {
448 *needed = ifood_eaten - sp->shp_item[I_FOOD];
449 can_eat = sp->shp_item[I_FOOD] / (etus * eatrate);
450 /* only want to starve off at most 1/2 the populace. */
451 if (can_eat < total_people / 2)
452 can_eat = total_people / 2;
454 to_starve = total_people - can_eat;
455 while (to_starve && sp->shp_item[I_UW]) {
458 sp->shp_item[I_UW]--;
460 while (to_starve && sp->shp_item[I_CIVIL]) {
463 sp->shp_item[I_CIVIL]--;
465 while (to_starve && sp->shp_item[I_MILIT]) {
468 sp->shp_item[I_MILIT]--;
471 sp->shp_item[I_FOOD] = 0;
473 sp->shp_item[I_FOOD] -= (int)food_eaten;