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 * supply.c: Supply subroutines
30 * Known contributors to this file:
51 #include "prototypes.h"
53 static int get_minimum(struct lndstr *, int);
54 static s_char *itemname(int);
55 static int s_commod(int, int, int, int, int, int);
58 * We want to get enough guns to be maxed out, enough shells to
59 * fire once, one update's worth of food, enough fuel for
62 * Firts, try to forage in the sector
63 * Second look for a warehouse or headquarters to leech
64 * Third, look for a ship we own in a harbor
65 * Fourth, look for supplies in a supply unit we own
66 * (one good reason to do this last is that the supply
67 * unit will then call resupply, taking more time)
69 * May want to put code to resupply with SAMs here, later --ts
74 resupply_all(struct lndstr *lp)
77 resupply_commod(lp, I_FOOD);
78 resupply_commod(lp, I_SHELL);
80 resupply_commod(lp, I_PETROL);
84 * If the unit has less than it's minimum level of a
85 * certain commodity, fill it, to the best of our abilities.
89 resupply_commod(struct lndstr *lp, int type)
97 lcp = &lchr[(int)lp->lnd_type];
99 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
100 /* Ok, do we now have enough? */
101 if (vec[type] < get_minimum(lp, type)) {
102 vec[type] += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, type,
103 get_minimum(lp, type) - vec[type]);
104 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
106 /* Now, check again to see if we have enough. */
107 if (vec[type] < get_minimum(lp, type)) {
108 /* Nope. How much do we need? */
109 amt = (get_minimum(lp, type) - vec[type]);
110 /* Are we on a ship? if so, try to get it from the ship first. */
111 if (lp->lnd_ship >= 0) {
112 getship(lp->lnd_ship, &ship);
113 getvec(VT_ITEM, svec, (s_char *)&ship, EF_SHIP);
114 /* Now, determine how much we can get */
115 amt = (amt < svec[type]) ? amt : svec[type];
116 /* Now, add and subtract */
119 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
120 putvec(VT_ITEM, svec, (s_char *)&ship, EF_SHIP);
121 putship(lp->lnd_ship, &ship);
125 if (opt_FUEL && type == I_PETROL) {
126 int fuel_needed = (lp->lnd_fuelu * (((float)etu_per_update
127 * land_mob_scale)) / 10.0);
129 while ((lp->lnd_fuel < fuel_needed) && vec[I_PETROL]) {
131 if (lp->lnd_fuel > lp->lnd_fuelc)
132 lp->lnd_fuel = lp->lnd_fuelc;
134 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
140 * Actually get the commod
143 supply_commod(int own, int x, int y, int type, int total_wanted)
145 if (total_wanted < 0)
147 return s_commod(own, x, y, type, total_wanted, !player->simulation);
151 * Just return the number you COULD get, without doing it
154 try_supply_commod(int own, int x, int y, int type, int total_wanted)
156 if (total_wanted < 0)
159 return s_commod(own, x, y, type, total_wanted, 0);
162 /* Get supplies of a certain type */
164 s_commod(int own, int x, int y, int type, int total_wanted,
167 int wanted = total_wanted;
168 int gotten = 0, lookrange;
169 struct sctstr sect, dest;
176 /* leave at least 1 military in sectors/ships */
177 int minimum = (type == I_MILIT ? 1 : 0);
179 double move_cost, weight, mobcost;
185 /* try to get it from sector we're in */
186 getsect(x, y, &dest);
187 getsect(x, y, §);
188 if (sect.sct_own == own) {
189 getvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
190 if ((vec[type] - minimum) >= wanted) {
193 putvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
197 } else if ((vec[type] - minimum) > 0) {
198 gotten += (vec[type] - minimum);
199 wanted -= (vec[type] - minimum);
202 putvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
207 /* look for a headquarters or warehouse */
208 lookrange = tfact(own, (double)10.0);
209 snxtsct_dist(&ns, x, y, lookrange);
210 while (nxtsct(&ns, §) && wanted) {
211 if (sect.sct_own != own)
213 if ((sect.sct_type != SCT_WAREH) &&
214 (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
216 if ((sect.sct_type == SCT_HEADQ) &&
217 (sect.sct_dist_x == sect.sct_x) &&
218 (sect.sct_dist_y == sect.sct_y))
220 if (sect.sct_effic < 60)
222 if (BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD) ==
225 getvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
226 if (!opt_NOFOOD && type == I_FOOD) {
227 minimum = (((double)etu_per_update * eatrate) *
228 (double)(vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW]))
231 if (vec[type] <= minimum) {
232 /* Don't bother... */
236 dp = &dchr[sect.sct_type];
237 packing = ip->i_pkg[dp->d_pkg];
238 if (packing > 1 && sect.sct_effic < 60)
240 weight = ((double)ip->i_lbs / (double)packing);
241 mobcost = move_cost * weight;
243 can_move = ((double)sect.sct_mobil / mobcost);
245 can_move = vec[type] - minimum;
246 if (can_move > (vec[type] - minimum))
247 can_move = (vec[type] - minimum);
249 if (can_move >= wanted) {
254 /* take off mobility for delivering sect */
255 n = roundavg(total_wanted * weight * move_cost);
258 if (n > sect.sct_mobil)
260 sect.sct_mobil -= (u_char)n;
263 putvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
268 } else if (can_move > 0) {
272 vec[type] -= can_move;
274 /* take off mobility for delivering sect */
275 n = roundavg(can_move * weight * move_cost);
278 if (n > sect.sct_mobil)
280 sect.sct_mobil -= (u_char)n;
283 putvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
289 /* look for an owned ship in a harbor */
290 snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
292 while (nxtitem(&ni, (s_char *)&ship) && wanted) {
293 if (ship.shp_own != own)
296 if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
298 getsect(ship.shp_x, ship.shp_y, §);
299 if (sect.sct_type != SCT_HARBR)
301 if (sect.sct_effic < 2)
303 if (BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD) ==
306 getvec(VT_ITEM, vec, (s_char *)&ship, EF_SHIP);
307 if (!opt_NOFOOD && type == I_FOOD)
308 minimum = (((double)etu_per_update * eatrate) *
309 (double)(vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW]))
311 if (vec[type] <= minimum) {
312 /* Don't bother... */
316 dp = &dchr[sect.sct_type];
317 packing = ip->i_pkg[dp->d_pkg];
318 if (packing > 1 && sect.sct_effic < 60)
320 weight = ((double)ip->i_lbs / (double)packing);
321 mobcost = move_cost * weight;
323 can_move = ((double)sect.sct_mobil / mobcost);
325 can_move = vec[type] - minimum;
326 if (can_move > (vec[type] - minimum))
327 can_move = (vec[type] - minimum);
328 if (can_move >= wanted) {
332 n = roundavg(wanted * weight * move_cost);
335 if (n > sect.sct_mobil)
337 sect.sct_mobil -= (u_char)n;
339 putvec(VT_ITEM, vec, (s_char *)&ship, EF_SHIP);
340 putship(ship.shp_uid, &ship);
344 } else if (can_move > 0) {
348 vec[type] -= can_move;
350 n = roundavg(can_move * weight * move_cost);
353 if (n > sect.sct_mobil)
355 sect.sct_mobil -= (u_char)n;
358 putvec(VT_ITEM, vec, (s_char *)&ship, EF_SHIP);
359 putship(ship.shp_uid, &ship);
365 /* look for an owned supply unit */
366 snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
368 while (nxtitem(&ni, (s_char *)&land) && wanted) {
371 if (land.lnd_own != own)
374 lcp = &lchr[(int)land.lnd_type];
375 if (!(lcp->l_flags & L_SUPPLY))
378 getvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
379 if (vec[type] <= get_minimum(&land, type))
382 getsect(land.lnd_x, land.lnd_y, §);
383 if (BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD) ==
387 if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
390 if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
393 if ((vec[type] - wanted) < get_minimum(&land, type)) {
400 putvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
402 putland(land.lnd_uid, &land);
404 s_commod(own, land.lnd_x, land.lnd_y, type, wanted,
407 putvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
408 putland(land.lnd_uid, &land);
410 putland(l2.lnd_uid, &l2);
413 getvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
415 weight = ((double)ip->i_lbs);
416 mobcost = move_cost * weight;
418 can_move = ((double)land.lnd_mobil / mobcost);
420 can_move = vec[type] - min;
421 min = get_minimum(&land, type);
422 if (can_move > (vec[type] - min))
423 can_move = (vec[type] - min);
425 if (can_move >= wanted) {
428 /* resupply the supply unit */
429 resupply_commod(&land, type);
431 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
434 putvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
435 putland(land.lnd_uid, &land);
438 } else if (can_move > 0) {
441 vec[type] -= can_move;
443 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
446 putvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
447 putland(land.lnd_uid, &land);
452 /* We've done the best we could */
453 /* return the number gotten */
462 register struct ichrstr *ip;
465 for (ip = &ichr[1]; ip->i_mnem != 0; ip++) {
466 if (t == ip->i_vtype)
473 * We want to get enough shells to fire once,
474 * one update's worth of food, enough fuel for
479 get_minimum(struct lndstr *lp, int type)
484 lcp = &lchr[(int)lp->lnd_type];
485 max = vl_find(V_ITEM(type), lcp->l_vtype, lcp->l_vamt, (int)lcp->l_nv);
490 return 0; /* no food reqd, get out */
491 want = (((double)etu_per_update * eatrate) *
492 (double)total_mil(lp)) + 1;
499 * return the amount of pet we'd need to get to
500 * enough fuel for 1 update
506 want = (lp->lnd_fuelu * (((float)etu_per_update *
507 land_mob_scale)) / 10.0);
508 want -= lp->lnd_fuel;
511 d = (double)want / 10.0;
530 has_supply(struct lndstr *lp)
533 int vec[I_MAX + 1], shells_needed, shells, keepshells;
534 int food, food_needed, keepfood;
535 int fuel_needed, fuel, petrol_needed, petrol, keeppetrol;
537 lcp = &lchr[(int)lp->lnd_type];
538 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
541 food_needed = get_minimum(lp, I_FOOD);
542 food = keepfood = vec[I_FOOD];
543 if (food < food_needed) {
545 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
546 food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
547 I_FOOD, (food_needed - food));
548 vec[I_FOOD] = keepfood;
549 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
551 if (food < food_needed)
556 shells_needed = lp->lnd_ammo;
557 shells = keepshells = vec[I_SHELL];
558 if (shells < shells_needed) {
560 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
561 shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
562 I_SHELL, (shells_needed - shells));
563 vec[I_SHELL] = keepshells;
564 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
567 if (shells < shells_needed)
571 fuel_needed = lp->lnd_fuelu;
575 petrol = petrol_needed = 0;
577 if (fuel < fuel_needed) {
579 ldround(((double)(fuel_needed - fuel) / 10.0), 1);
580 petrol = keeppetrol = vec[I_PETROL];
583 if (petrol < petrol_needed) {
585 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
586 petrol += try_supply_commod(lp->lnd_own,
587 lp->lnd_x, lp->lnd_y,
589 (petrol_needed - petrol));
590 vec[I_PETROL] = keeppetrol;
591 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
595 if (fuel < fuel_needed)
603 use_supply(struct lndstr *lp)
606 int vec[I_MAX + 1], shells_needed, shells, food, food_needed;
607 int fuel_needed, fuel, petrol_needed, petrol;
609 lcp = &lchr[(int)lp->lnd_type];
610 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
612 shells_needed = lp->lnd_ammo;
613 shells = vec[I_SHELL];
614 if (shells < shells_needed) {
616 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
617 shells += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_SHELL,
618 (shells_needed - shells));
619 vec[I_SHELL] = shells;
622 vec[I_SHELL] = max(vec[I_SHELL] - shells_needed, 0);
624 if (lp->lnd_frg) /* artillery */
627 food_needed = get_minimum(lp, I_FOOD);
630 if (food < food_needed) {
632 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
633 food += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_FOOD,
634 (food_needed - food));
638 vec[I_FOOD] = max(vec[I_FOOD] - food_needed, 0);
641 fuel_needed = lp->lnd_fuelu;
644 petrol = petrol_needed = 0;
646 if (fuel < fuel_needed) {
648 ldround(((double)(fuel_needed - fuel) / 10.0), 1);
649 petrol = vec[I_PETROL];
652 if (petrol < petrol_needed) {
654 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
655 petrol += supply_commod(lp->lnd_own,
656 lp->lnd_x, lp->lnd_y,
657 I_PETROL, (petrol_needed - petrol));
658 vec[I_PETROL] = petrol;
662 if (petrol >= petrol_needed) {
663 vec[I_PETROL] = max(vec[I_PETROL] - petrol_needed, 0);
664 lp->lnd_fuel += petrol_needed * 10;
666 lp->lnd_fuel += vec[I_PETROL] * 10;
671 lp->lnd_fuel = max(lp->lnd_fuel - fuel_needed, 0);
675 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
676 putland(lp->lnd_uid, lp);