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 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:
58 #define MIN(x,y) ((x) > (y) ? (y) : (x))
61 static int shiprepair(struct shpstr *, struct natstr *,
63 static void upd_ship(struct shpstr *, int,
64 struct natstr *, int *, int);
67 prod_ship(int etus, int natnum, int *bp, int build)
68 /* build = 1, maintain = 0 */
74 int lastx = 9999, lasty = 9999;
76 bp_enable_cachepath();
77 for (n = 0; NULL != (sp = getshipp(n)); n++) {
80 if (sp->shp_own != natnum)
82 np = getnatp(sp->shp_own);
83 start_money = np->nat_money;
84 if (lastx == 9999 || lasty == 9999) {
88 if (lastx != sp->shp_x || lasty != sp->shp_y) {
90 bp_disable_cachepath();
92 bp_enable_cachepath();
94 upd_ship(sp, etus, np, bp, build);
95 if (build && !player->simulation) /* make sure to only autonav once */
96 nav_ship(sp); /* autonav the ship */
97 sea_money[sp->shp_own] += np->nat_money - start_money;
98 if (!build || np->nat_money != start_money)
100 if (player->simulation)
101 np->nat_money = start_money;
103 bp_disable_cachepath();
104 bp_clear_cachepath();
107 if (build && !player->simulation) /* make sure to only sail once */
114 upd_ship(struct shpstr *sp, int etus,
115 struct natstr *np, int *bp, int build)
116 /* build = 1, maintain = 0 */
118 struct sctstr *sectp;
124 struct pchrstr *product;
133 mp = &mchr[(int)sp->shp_type];
135 if (np->nat_priorities[PRI_SBUILD] == 0 || np->nat_money < 0)
137 if (sp->shp_effic < SHIP_MINEFF || !shiprepair(sp, np, bp, etus)) {
138 makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x,
145 if (np->nat_level[NAT_TLEV] < sp->shp_tech * 0.85)
147 cost = -(mult * etus * dmin(0.0, money_ship * mp->m_cost));
148 if ((np->nat_priorities[PRI_SMAINT] == 0 || np->nat_money < cost)
149 && !player->simulation) {
150 if ((eff = sp->shp_effic - etus / 5) < SHIP_MINEFF) {
152 "%s lost to lack of maintenance\n", prship(sp));
153 makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x,
159 "%s lost %d%% to lack of maintenance\n",
160 prship(sp), sp->shp_effic - eff);
163 np->nat_money -= cost;
166 if (!player->simulation) {
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 product = &pchr[P_OIL];
174 oil_gained = roundavg(total_work(100, etus,
175 sp->shp_item[I_CIVIL],
176 sp->shp_item[I_MILIT],
179 * (double)sp->shp_effic / 100.0
180 * (double)sectp->sct_oil / 100.0
181 * prod_eff(product, sp->shp_tech));
182 max_oil = mp->m_item[I_OIL];
183 if (sp->shp_item[I_OIL] + oil_gained > max_oil)
184 oil_gained = max_oil - sp->shp_item[I_OIL];
185 if (product->p_nrdep != 0 && oil_gained > 0) {
186 resource = (u_char *)sectp + product->p_nrndx;
187 if (*resource * 100 < product->p_nrdep * oil_gained)
188 oil_gained = *resource * 100 / product->p_nrdep;
189 dep = roundavg(oil_gained * product->p_nrdep / 100.0);
190 if (CANT_HAPPEN(dep > *resource))
194 sp->shp_item[I_OIL] += oil_gained;
196 if ((mp->m_flags & M_FOOD) && sectp->sct_type == SCT_WATER) {
197 product = &pchr[P_FOOD];
199 += roundavg(total_work(100, etus,
200 sp->shp_item[I_CIVIL],
201 sp->shp_item[I_MILIT],
204 * sp->shp_effic / 100.0
205 * sectp->sct_fertil / 100.0
206 * prod_eff(product, sp->shp_tech));
208 if ((n = feed_ship(sp, etus, &needed, 1)) > 0) {
209 wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
211 nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
213 max_food = mp->m_item[I_FOOD];
214 if (sp->shp_item[I_FOOD] > max_food)
215 sp->shp_item[I_FOOD] = max_food;
217 * do plague stuff. plague can't break out on ships,
218 * but it can still kill people.
220 pstage = sp->shp_pstage;
221 ptime = sp->shp_ptime;
222 if (pstage != PLG_HEALTHY) {
223 n = plague_people(np, sp->shp_item, &pstage, &ptime, etus);
227 "PLAGUE deaths reported on %s\n", prship(sp));
228 nreport(sp->shp_own, N_DIE_PLAGUE, 0, 1);
231 wu(0, sp->shp_own, "%s battling PLAGUE\n", prship(sp));
234 /* Are we still incubating? */
236 /* Yes. Will it turn "infectious" next time? */
238 /* Yes. Report an outbreak. */
240 "Outbreak of PLAGUE on %s!\n", prship(sp));
241 nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
244 /* It has already moved on to "infectious" */
246 "%s battling PLAGUE\n", prship(sp));
250 /* Has the plague moved to "incubation" yet? */
252 /* Yes. Will it turn "infectious" next time? */
254 /* Yes. Report an outbreak. */
256 "Outbreak of PLAGUE on %s!\n", prship(sp));
257 nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
265 sp->shp_pstage = pstage;
266 sp->shp_ptime = ptime;
268 pops[sp->shp_own] += sp->shp_item[I_CIVIL];
274 * idea is: a sector full of workers can fix up eight
275 * battleships +8 % eff each etu. This will cost around
276 * 8 * 8 * $40 = $2560!
279 shiprepair(struct shpstr *ship, struct natstr *np,
287 int lcm_needed, hcm_needed;
295 mp = &mchr[(int)ship->shp_type];
296 sp = getsectp(ship->shp_x, ship->shp_y);
298 if ((sp->sct_own != ship->shp_own) && (sp->sct_own != 0)) {
299 rel = getrel(getnatp(sp->sct_own), ship->shp_own);
306 /* only military can work on a military boat */
307 if (ship->shp_glim > 0)
308 wf = etus * ship->shp_item[I_MILIT] / 2;
310 wf = etus * (ship->shp_item[I_CIVIL] / 2 + ship->shp_item[I_MILIT] / 5);
312 if (sp->sct_type != SCT_HARBR) {
316 if (!player->simulation)
317 avail = wf + sp->sct_avail * 100;
319 avail = wf + gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
322 w_p_eff = SHP_BLD_WORK(mp->m_lcm, mp->m_hcm);
324 if ((sp->sct_off) && (sp->sct_own == ship->shp_own))
328 if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
331 if (ship->shp_effic == 100) {
332 /* ship is ok; no repairs needed */
336 left = 100 - ship->shp_effic;
337 delta = roundavg((double)avail / w_p_eff);
340 if (delta > (int)((float)etus * ship_grow_scale))
341 delta = (int)((float)etus * ship_grow_scale);
345 /* delta is the max amount we can grow */
347 left = 100 - ship->shp_effic;
351 leftp = ((float)left / 100.0);
352 memset(mvec, 0, sizeof(mvec));
353 mvec[I_LCM] = lcm_needed = ldround((double)(mp->m_lcm * leftp), 1);
354 mvec[I_HCM] = hcm_needed = ldround((double)(mp->m_hcm * leftp), 1);
356 get_materials(sp, bp, mvec, 0);
358 if (mvec[I_LCM] >= lcm_needed)
361 buildp = ((float)mvec[I_LCM] / (float)mp->m_lcm);
362 if (mvec[I_HCM] < hcm_needed)
363 buildp = MIN(buildp, ((float)mvec[I_HCM] / (float)mp->m_hcm));
365 build = ldround((double)(buildp * 100.0), 1);
366 memset(mvec, 0, sizeof(mvec));
367 mvec[I_LCM] = lcm_needed = roundavg((double)(mp->m_lcm * buildp));
368 mvec[I_HCM] = hcm_needed = roundavg((double)(mp->m_hcm * buildp));
370 get_materials(sp, bp, mvec, 1);
372 if (sp->sct_type != SCT_HARBR)
374 wf -= build * w_p_eff;
377 * I didn't use roundavg here, because I want to penalize
378 * the player with a large number of ships.
380 if (!player->simulation)
381 avail = (sp->sct_avail * 100 + wf) / 100;
383 avail = (gt_bg_nmbr(bp, sp, I_MAX + 1) * 100 + wf) / 100;
386 if (!player->simulation)
387 sp->sct_avail = avail;
389 pt_bg_nmbr(bp, sp, I_MAX + 1, avail);
391 if (sp->sct_type != SCT_HARBR)
392 if ((build + ship->shp_effic) > 80) {
393 build = 80 - ship->shp_effic;
398 np->nat_money -= mult * mp->m_cost * build / 100.0;
399 if (!player->simulation)
400 ship->shp_effic += build;
405 * returns the number who starved, if any.
408 feed_ship(struct shpstr *sp, int etus, int *needed, int doit)
410 double food_eaten, land_eaten;
420 return 0; /* no food no work to do */
423 = sp->shp_item[I_CIVIL] + sp->shp_item[I_MILIT] + sp->shp_item[I_UW];
424 food_eaten = etus * eatrate * total_people;
425 ifood_eaten = (int)food_eaten;
426 if (food_eaten - ifood_eaten > 0)
430 if (!player->simulation && ifood_eaten > sp->shp_item[I_FOOD])
432 += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
433 I_FOOD, ifood_eaten - sp->shp_item[I_FOOD]);
435 /* doit - only steal food from land units during the update */
436 if (ifood_eaten > sp->shp_item[I_FOOD] && sp->shp_nland > 0 && doit) {
437 snxtitem_all(&ni, EF_LAND);
438 while ((lp = nxtitemp(&ni)) && ifood_eaten > sp->shp_item[I_FOOD]) {
439 if (lp->lnd_ship != sp->shp_uid)
441 need = ifood_eaten - sp->shp_item[I_FOOD];
442 land_eaten = etus * eatrate * lnd_getmil(lp);
443 if (lp->lnd_item[I_FOOD] - need > land_eaten) {
444 sp->shp_item[I_FOOD] += need;
445 lp->lnd_item[I_FOOD] -= need;
446 } else if (lp->lnd_item[I_FOOD] - land_eaten > 0) {
447 sp->shp_item[I_FOOD] += lp->lnd_item[I_FOOD] - land_eaten;
448 lp->lnd_item[I_FOOD] -= lp->lnd_item[I_FOOD] - land_eaten;
453 if (ifood_eaten > sp->shp_item[I_FOOD]) {
454 *needed = ifood_eaten - sp->shp_item[I_FOOD];
455 can_eat = sp->shp_item[I_FOOD] / (etus * eatrate);
456 /* only want to starve off at most 1/2 the populace. */
457 if (can_eat < total_people / 2)
458 can_eat = total_people / 2;
460 to_starve = total_people - can_eat;
461 while (to_starve && sp->shp_item[I_UW]) {
464 sp->shp_item[I_UW]--;
466 while (to_starve && sp->shp_item[I_CIVIL]) {
469 sp->shp_item[I_CIVIL]--;
471 while (to_starve && sp->shp_item[I_MILIT]) {
474 sp->shp_item[I_MILIT]--;
477 sp->shp_item[I_FOOD] = 0;
479 sp->shp_item[I_FOOD] -= (int)food_eaten;