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;
119 u_short pstage, ptime;
123 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 oil_gained = roundavg((sp->shp_item[I_CIVIL] * etus / 10000.0)
173 max_oil = vl_find(V_OIL, mp->m_vtype, mp->m_vamt, mp->m_nv);
174 if (sp->shp_item[I_OIL] > max_oil)
175 oil_gained = max_oil - sp->shp_item[I_OIL];
176 sp->shp_item[I_OIL] += oil_gained;
\r
177 product = &pchr[P_OIL];
178 if (product->p_nrdep != 0 && oil_gained > 0) {
179 resource = ((s_char *)sectp) + product->p_nrndx;
180 *resource -= roundavg(oil_gained *
181 product->p_nrdep / 100.0);
184 if ((mp->m_flags & M_FOOD) && sectp->sct_type == SCT_WATER) {
185 sp->shp_item[I_FOOD] += ((sp->shp_item[I_CIVIL] * etus) / 1000.0)
188 if ((n = feed_ship(sp, etus, &needed, 1)) > 0) {
189 wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
191 nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
193 max_food = vl_find(V_FOOD, mp->m_vtype, mp->m_vamt, mp->m_nv);
194 if (sp->shp_item[I_FOOD] > max_food)
195 sp->shp_item[I_FOOD] = max_food;
197 * do plague stuff. plague can't break out on ships,
198 * but it can still kill people.
200 pstage = sp->shp_pstage;
201 ptime = sp->shp_ptime;
202 if (pstage != PLG_HEALTHY) {
203 n = plague_people(np, sp->shp_item, &pstage, &ptime, etus);
207 "PLAGUE deaths reported on %s\n", prship(sp));
208 nreport(sp->shp_own, N_DIE_PLAGUE, 0, 1);
211 wu(0, sp->shp_own, "%s battling PLAGUE\n", prship(sp));
214 /* Are we still incubating? */
216 /* Yes. Will it turn "infectious" next time? */
218 /* Yes. Report an outbreak. */
220 "Outbreak of PLAGUE on %s!\n", prship(sp));
221 nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
224 /* It has already moved on to "infectious" */
226 "%s battling PLAGUE\n", prship(sp));
230 /* Has the plague moved to "incubation" yet? */
232 /* Yes. Will it turn "infectious" next time? */
234 /* Yes. Report an outbreak. */
236 "Outbreak of PLAGUE on %s!\n", prship(sp));
237 nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
245 sp->shp_pstage = pstage;
246 sp->shp_ptime = ptime;
248 pops[sp->shp_own] += sp->shp_item[I_CIVIL];
254 * idea is: a sector full of workers can fix up eight
255 * battleships +8 % eff each etu. This will cost around
256 * 8 * 8 * $40 = $2560!
259 shiprepair(register struct shpstr *ship, struct natstr *np,
267 int lcm_needed, hcm_needed;
275 mp = &mchr[(int)ship->shp_type];
276 sp = getsectp(ship->shp_x, ship->shp_y);
278 if ((sp->sct_own != ship->shp_own) && (sp->sct_own != 0)) {
279 rel = getrel(getnatp(sp->sct_own), ship->shp_own);
286 /* only military can work on a military boat */
287 if (ship->shp_glim > 0)
288 wf = etus * ship->shp_item[I_MILIT] / 2;
290 wf = etus * (ship->shp_item[I_CIVIL] / 2 + ship->shp_item[I_MILIT] / 5);
292 if (sp->sct_type != SCT_HARBR) {
296 if (!player->simulation)
297 avail = wf + sp->sct_avail * 100;
299 avail = wf + gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
302 w_p_eff = SHP_BLD_WORK(mp->m_lcm, mp->m_hcm);
304 if ((sp->sct_off) && (sp->sct_own == ship->shp_own))
308 if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
311 if (ship->shp_effic == 100) {
312 /* ship is ok; no repairs needed */
316 left = 100 - ship->shp_effic;
317 delta = roundavg((double)avail / w_p_eff);
320 if (delta > etus * ship_grow_scale)
321 delta = etus * ship_grow_scale;
325 /* delta is the max amount we can grow */
327 left = 100 - ship->shp_effic;
331 leftp = ((float)left / 100.0);
332 memset(mvec, 0, sizeof(mvec));
333 mvec[I_LCM] = lcm_needed = ldround((double)(mp->m_lcm * leftp), 1);
334 mvec[I_HCM] = hcm_needed = ldround((double)(mp->m_hcm * leftp), 1);
336 get_materials(sp, bp, mvec, 0);
338 if (mvec[I_LCM] >= lcm_needed)
341 buildp = ((float)mvec[I_LCM] / (float)mp->m_lcm);
342 if (mvec[I_HCM] < hcm_needed)
343 buildp = MIN(buildp, ((float)mvec[I_HCM] / (float)mp->m_hcm));
345 build = ldround((double)(buildp * 100.0), 1);
346 memset(mvec, 0, sizeof(mvec));
347 mvec[I_LCM] = lcm_needed = roundavg((double)(mp->m_lcm * buildp));
348 mvec[I_HCM] = hcm_needed = roundavg((double)(mp->m_hcm * buildp));
350 get_materials(sp, bp, mvec, 1);
352 if (sp->sct_type != SCT_HARBR)
354 wf -= build * w_p_eff;
357 * I didn't use roundavg here, because I want to penalize
358 * the player with a large number of ships.
360 if (!player->simulation)
361 avail = (sp->sct_avail * 100 + wf) / 100;
363 avail = (gt_bg_nmbr(bp, sp, I_MAX + 1) * 100 + wf) / 100;
366 if (!player->simulation)
367 sp->sct_avail = avail;
369 pt_bg_nmbr(bp, sp, I_MAX + 1, avail);
371 if (sp->sct_type != SCT_HARBR)
372 if ((build + ship->shp_effic) > 80) {
373 build = 80 - ship->shp_effic;
378 np->nat_money -= mult * mp->m_cost * build / 100.0;
379 if (!player->simulation)
380 ship->shp_effic += (s_char)build;
385 * returns the number who starved, if any.
388 feed_ship(struct shpstr *sp, int etus, int *needed, int doit)
390 double food_eaten, land_eaten;
400 return 0; /* no food no work to do */
403 = sp->shp_item[I_CIVIL] + sp->shp_item[I_MILIT] + sp->shp_item[I_UW];
404 food_eaten = etus * eatrate * total_people;
405 ifood_eaten = (int)food_eaten;
406 if (food_eaten - ifood_eaten > 0)
410 if (!player->simulation && ifood_eaten > sp->shp_item[I_FOOD])
412 += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
413 I_FOOD, ifood_eaten - sp->shp_item[I_FOOD]);
415 /* doit - only steal food from land units during the update */
416 if (ifood_eaten > sp->shp_item[I_FOOD] && sp->shp_nland > 0 && doit) {
417 snxtitem_all(&ni, EF_LAND);
418 while ((lp = (struct lndstr *)nxtitemp(&ni, 0)) &&
419 ifood_eaten > sp->shp_item[I_FOOD]) {
420 if (lp->lnd_ship != sp->shp_uid)
422 need = ifood_eaten - sp->shp_item[I_FOOD];
423 land_eaten = etus * eatrate * lnd_getmil(lp);
424 if (lp->lnd_item[I_FOOD] - need > land_eaten) {
425 sp->shp_item[I_FOOD] += need;
426 lp->lnd_item[I_FOOD] -= need;
427 } else if (lp->lnd_item[I_FOOD] - land_eaten > 0) {
428 sp->shp_item[I_FOOD] += lp->lnd_item[I_FOOD] - land_eaten;
429 lp->lnd_item[I_FOOD] -= lp->lnd_item[I_FOOD] - land_eaten;
434 if (ifood_eaten > sp->shp_item[I_FOOD]) {
435 *needed = ifood_eaten - sp->shp_item[I_FOOD];
436 can_eat = sp->shp_item[I_FOOD] / (etus * eatrate);
437 /* only want to starve off at most 1/2 the populace. */
438 if (can_eat < total_people / 2)
439 can_eat = total_people / 2;
441 to_starve = total_people - can_eat;
442 while (to_starve && sp->shp_item[I_UW]) {
445 sp->shp_item[I_UW]--;
447 while (to_starve && sp->shp_item[I_CIVIL]) {
450 sp->shp_item[I_CIVIL]--;
452 while (to_starve && sp->shp_item[I_MILIT]) {
455 sp->shp_item[I_MILIT]--;
458 sp->shp_item[I_FOOD] = 0;
460 sp->shp_item[I_FOOD] -= (int)food_eaten;