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 extern float land_mob_scale;
127 extern int etu_per_update;
128 int fuel_needed = (lp->lnd_fuelu * (((float)etu_per_update
129 * land_mob_scale)) / 10.0);
131 while ((lp->lnd_fuel < fuel_needed) && vec[I_PETROL]) {
133 if (lp->lnd_fuel > lp->lnd_fuelc)
134 lp->lnd_fuel = lp->lnd_fuelc;
136 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
142 * Actually get the commod
145 supply_commod(int own, int x, int y, int type, int total_wanted)
147 if (total_wanted < 0)
149 return s_commod(own, x, y, type, total_wanted, !player->simulation);
153 * Just return the number you COULD get, without doing it
156 try_supply_commod(int own, int x, int y, int type, int total_wanted)
158 if (total_wanted < 0)
161 return s_commod(own, x, y, type, total_wanted, 0);
164 /* Get supplies of a certain type */
166 s_commod(int own, int x, int y, int type, int total_wanted,
169 int wanted = total_wanted;
170 int gotten = 0, lookrange;
171 struct sctstr sect, dest;
178 /* leave at least 1 military in sectors/ships */
179 int minimum = (type == I_MILIT ? 1 : 0);
181 double move_cost, weight, mobcost;
183 extern double eatrate;
184 extern int etu_per_update;
189 /* try to get it from sector we're in */
190 getsect(x, y, &dest);
191 getsect(x, y, §);
192 if (sect.sct_own == own) {
193 getvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
194 if ((vec[type] - minimum) >= wanted) {
197 putvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
201 } else if ((vec[type] - minimum) > 0) {
202 gotten += (vec[type] - minimum);
203 wanted -= (vec[type] - minimum);
206 putvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
211 /* look for a headquarters or warehouse */
212 lookrange = tfact(own, (double)10.0);
213 snxtsct_dist(&ns, x, y, lookrange);
214 while (nxtsct(&ns, §) && wanted) {
215 if (sect.sct_own != own)
217 if ((sect.sct_type != SCT_WAREH) &&
218 (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
220 if ((sect.sct_type == SCT_HEADQ) &&
221 (sect.sct_dist_x == sect.sct_x) &&
222 (sect.sct_dist_y == sect.sct_y))
224 if (sect.sct_effic < 60)
226 if (BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD) ==
229 getvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
230 if (!opt_NOFOOD && type == I_FOOD) {
231 minimum = (((double)etu_per_update * eatrate) *
232 (double)(vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW]))
235 if (vec[type] <= minimum) {
236 /* Don't bother... */
240 dp = &dchr[sect.sct_type];
241 packing = ip->i_pkg[dp->d_pkg];
242 if (packing > 1 && sect.sct_effic < 60)
244 weight = ((double)ip->i_lbs / (double)packing);
245 mobcost = move_cost * weight;
247 can_move = ((double)sect.sct_mobil / mobcost);
249 can_move = vec[type] - minimum;
250 if (can_move > (vec[type] - minimum))
251 can_move = (vec[type] - minimum);
253 if (can_move >= wanted) {
258 /* take off mobility for delivering sect */
259 n = roundavg(total_wanted * weight * move_cost);
262 if (n > sect.sct_mobil)
264 sect.sct_mobil -= (u_char)n;
267 putvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
272 } else if (can_move > 0) {
276 vec[type] -= can_move;
278 /* take off mobility for delivering sect */
279 n = roundavg(can_move * weight * move_cost);
282 if (n > sect.sct_mobil)
284 sect.sct_mobil -= (u_char)n;
287 putvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
293 /* look for an owned ship in a harbor */
294 snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
296 while (nxtitem(&ni, (s_char *)&ship) && wanted) {
297 if (ship.shp_own != own)
300 if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
302 getsect(ship.shp_x, ship.shp_y, §);
303 if (sect.sct_type != SCT_HARBR)
305 if (sect.sct_effic < 2)
307 if (BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD) ==
310 getvec(VT_ITEM, vec, (s_char *)&ship, EF_SHIP);
311 if (!opt_NOFOOD && type == I_FOOD)
312 minimum = (((double)etu_per_update * eatrate) *
313 (double)(vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW]))
315 if (vec[type] <= minimum) {
316 /* Don't bother... */
320 dp = &dchr[sect.sct_type];
321 packing = ip->i_pkg[dp->d_pkg];
322 if (packing > 1 && sect.sct_effic < 60)
324 weight = ((double)ip->i_lbs / (double)packing);
325 mobcost = move_cost * weight;
327 can_move = ((double)sect.sct_mobil / mobcost);
329 can_move = vec[type] - minimum;
330 if (can_move > (vec[type] - minimum))
331 can_move = (vec[type] - minimum);
332 if (can_move >= wanted) {
336 n = roundavg(wanted * weight * move_cost);
339 if (n > sect.sct_mobil)
341 sect.sct_mobil -= (u_char)n;
343 putvec(VT_ITEM, vec, (s_char *)&ship, EF_SHIP);
344 putship(ship.shp_uid, &ship);
348 } else if (can_move > 0) {
352 vec[type] -= can_move;
354 n = roundavg(can_move * weight * move_cost);
357 if (n > sect.sct_mobil)
359 sect.sct_mobil -= (u_char)n;
362 putvec(VT_ITEM, vec, (s_char *)&ship, EF_SHIP);
363 putship(ship.shp_uid, &ship);
369 /* look for an owned supply unit */
370 snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
372 while (nxtitem(&ni, (s_char *)&land) && wanted) {
375 if (land.lnd_own != own)
378 lcp = &lchr[(int)land.lnd_type];
379 if (!(lcp->l_flags & L_SUPPLY))
382 getvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
383 if (vec[type] <= get_minimum(&land, type))
386 getsect(land.lnd_x, land.lnd_y, §);
387 if (BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD) ==
391 if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
394 if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
397 if ((vec[type] - wanted) < get_minimum(&land, type)) {
404 putvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
406 putland(land.lnd_uid, &land);
408 s_commod(own, land.lnd_x, land.lnd_y, type, wanted,
411 putvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
412 putland(land.lnd_uid, &land);
414 putland(l2.lnd_uid, &l2);
417 getvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
419 weight = ((double)ip->i_lbs);
420 mobcost = move_cost * weight;
422 can_move = ((double)land.lnd_mobil / mobcost);
424 can_move = vec[type] - min;
425 min = get_minimum(&land, type);
426 if (can_move > (vec[type] - min))
427 can_move = (vec[type] - min);
429 if (can_move >= wanted) {
432 /* resupply the supply unit */
433 resupply_commod(&land, type);
435 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
438 putvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
439 putland(land.lnd_uid, &land);
442 } else if (can_move > 0) {
445 vec[type] -= can_move;
447 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
450 putvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
451 putland(land.lnd_uid, &land);
456 /* We've done the best we could */
457 /* return the number gotten */
466 register struct ichrstr *ip;
469 for (ip = &ichr[1]; ip->i_mnem != 0; ip++) {
470 if (t == ip->i_vtype)
477 * We want to get enough shells to fire once,
478 * one update's worth of food, enough fuel for
483 get_minimum(struct lndstr *lp, int type)
487 extern double eatrate;
488 extern int etu_per_update;
489 extern float land_mob_scale;
491 lcp = &lchr[(int)lp->lnd_type];
492 max = vl_find(V_ITEM(type), lcp->l_vtype, lcp->l_vamt, (int)lcp->l_nv);
497 return 0; /* no food reqd, get out */
498 want = (((double)etu_per_update * eatrate) *
499 (double)total_mil(lp)) + 1;
506 * return the amount of pet we'd need to get to
507 * enough fuel for 1 update
513 want = (lp->lnd_fuelu * (((float)etu_per_update *
514 land_mob_scale)) / 10.0);
515 want -= lp->lnd_fuel;
518 d = (double)want / 10.0;
537 has_supply(struct lndstr *lp)
540 int vec[I_MAX + 1], shells_needed, shells, keepshells;
541 int food, food_needed, keepfood;
542 int fuel_needed, fuel, petrol_needed, petrol, keeppetrol;
544 lcp = &lchr[(int)lp->lnd_type];
545 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
548 food_needed = get_minimum(lp, I_FOOD);
549 food = keepfood = vec[I_FOOD];
550 if (food < food_needed) {
552 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
553 food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
554 I_FOOD, (food_needed - food));
555 vec[I_FOOD] = keepfood;
556 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
558 if (food < food_needed)
563 shells_needed = lp->lnd_ammo;
564 shells = keepshells = vec[I_SHELL];
565 if (shells < shells_needed) {
567 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
568 shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
569 I_SHELL, (shells_needed - shells));
570 vec[I_SHELL] = keepshells;
571 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
574 if (shells < shells_needed)
578 fuel_needed = lp->lnd_fuelu;
582 petrol = petrol_needed = 0;
584 if (fuel < fuel_needed) {
586 ldround(((double)(fuel_needed - fuel) / 10.0), 1);
587 petrol = keeppetrol = vec[I_PETROL];
590 if (petrol < petrol_needed) {
592 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
593 petrol += try_supply_commod(lp->lnd_own,
594 lp->lnd_x, lp->lnd_y,
596 (petrol_needed - petrol));
597 vec[I_PETROL] = keeppetrol;
598 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
602 if (fuel < fuel_needed)
610 use_supply(struct lndstr *lp)
613 int vec[I_MAX + 1], shells_needed, shells, food, food_needed;
614 int fuel_needed, fuel, petrol_needed, petrol;
616 lcp = &lchr[(int)lp->lnd_type];
617 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
619 shells_needed = lp->lnd_ammo;
620 shells = vec[I_SHELL];
621 if (shells < shells_needed) {
623 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
624 shells += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_SHELL,
625 (shells_needed - shells));
626 vec[I_SHELL] = shells;
629 vec[I_SHELL] = max(vec[I_SHELL] - shells_needed, 0);
631 if (lp->lnd_frg) /* artillery */
634 food_needed = get_minimum(lp, I_FOOD);
637 if (food < food_needed) {
639 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
640 food += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_FOOD,
641 (food_needed - food));
645 vec[I_FOOD] = max(vec[I_FOOD] - food_needed, 0);
648 fuel_needed = lp->lnd_fuelu;
651 petrol = petrol_needed = 0;
653 if (fuel < fuel_needed) {
655 ldround(((double)(fuel_needed - fuel) / 10.0), 1);
656 petrol = vec[I_PETROL];
659 if (petrol < petrol_needed) {
661 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
662 petrol += supply_commod(lp->lnd_own,
663 lp->lnd_x, lp->lnd_y,
664 I_PETROL, (petrol_needed - petrol));
665 vec[I_PETROL] = petrol;
669 if (petrol >= petrol_needed) {
670 vec[I_PETROL] = max(vec[I_PETROL] - petrol_needed, 0);
671 lp->lnd_fuel += petrol_needed * 10;
673 lp->lnd_fuel += vec[I_PETROL] * 10;
678 lp->lnd_fuel = max(lp->lnd_fuel - fuel_needed, 0);
682 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
683 putland(lp->lnd_uid, lp);