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"
54 * We want to get enough guns to be maxed out, enough shells to
55 * fire once, one update's worth of food, enough fuel for
58 * Firts, try to forage in the sector
59 * Second look for a warehouse or headquarters to leech
60 * Third, look for a ship we own in a harbor
61 * Fourth, look for supplies in a supply unit we own
62 * (one good reason to do this last is that the supply
63 * unit will then call resupply, taking more time)
65 * May want to put code to resupply with SAMs here, later --ts
70 resupply_all(struct lndstr *lp)
73 resupply_commod(lp, I_FOOD);
74 resupply_commod(lp, I_SHELL);
76 resupply_commod(lp, I_PETROL);
80 * If the unit has less than it's minimum level of a
81 * certain commodity, fill it, to the best of our abilities.
85 resupply_commod(struct lndstr *lp, int type)
93 lcp = &lchr[(int)lp->lnd_type];
95 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
96 /* Ok, do we now have enough? */
97 if (vec[type] < get_minimum(lp, type)) {
98 vec[type] += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, type,
99 get_minimum(lp, type) - vec[type]);
100 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
102 /* Now, check again to see if we have enough. */
103 if (vec[type] < get_minimum(lp, type)) {
104 /* Nope. How much do we need? */
105 amt = (get_minimum(lp, type) - vec[type]);
106 /* Are we on a ship? if so, try to get it from the ship first. */
107 if (lp->lnd_ship >= 0) {
108 getship(lp->lnd_ship, &ship);
109 getvec(VT_ITEM, svec, (s_char *)&ship, EF_SHIP);
110 /* Now, determine how much we can get */
111 amt = (amt < svec[type]) ? amt : svec[type];
112 /* Now, add and subtract */
115 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
116 putvec(VT_ITEM, svec, (s_char *)&ship, EF_SHIP);
117 putship(lp->lnd_ship, &ship);
121 if (opt_FUEL && type == I_PETROL) {
122 extern float land_mob_scale;
123 extern int etu_per_update;
124 int fuel_needed = (lp->lnd_fuelu * (((float)etu_per_update
125 * land_mob_scale)) / 10.0);
127 while ((lp->lnd_fuel < fuel_needed) && vec[I_PETROL]) {
129 if (lp->lnd_fuel > lp->lnd_fuelc)
130 lp->lnd_fuel = lp->lnd_fuelc;
132 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
138 * Actually get the commod
141 supply_commod(int own, int x, int y, int type, int total_wanted)
143 if (total_wanted < 0)
145 return s_commod(own, x, y, type, total_wanted, !player->simulation);
149 * Just return the number you COULD get, without doing it
152 try_supply_commod(int own, int x, int y, int type, int total_wanted)
154 if (total_wanted < 0)
157 return s_commod(own, x, y, type, total_wanted, 0);
160 /* Get supplies of a certain type */
162 s_commod(int own, int x, int y, int type, int total_wanted,
165 int wanted = total_wanted;
166 int gotten = 0, lookrange;
167 struct sctstr sect, dest;
174 /* leave at least 1 military in sectors/ships */
175 int minimum = (type == I_MILIT ? 1 : 0);
177 double move_cost, weight, mobcost;
179 extern double eatrate;
180 extern int etu_per_update;
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)) {
397 bcopy((s_char *)&land, (s_char *)&l2, sizeof(struct lndstr));
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 at_minimum(struct lndstr *lp, int type)
477 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
478 if (vec[type] <= get_minimum(lp, type))
485 * We want to get enough shells to fire once,
486 * one update's worth of food, enough fuel for
491 get_minimum(struct lndstr *lp, int type)
495 extern double eatrate;
496 extern int etu_per_update;
497 extern float land_mob_scale;
499 lcp = &lchr[(int)lp->lnd_type];
500 max = vl_find(V_ITEM(type), lcp->l_vtype, lcp->l_vamt, (int)lcp->l_nv);
505 return 0; /* no food reqd, get out */
506 want = (((double)etu_per_update * eatrate) *
507 (double)total_mil(lp)) + 1;
514 * return the amount of pet we'd need to get to
515 * enough fuel for 1 update
521 want = (lp->lnd_fuelu * (((float)etu_per_update *
522 land_mob_scale)) / 10.0);
523 want -= lp->lnd_fuel;
526 d = (double)want / 10.0;
545 has_supply(struct lndstr *lp)
548 int vec[I_MAX + 1], shells_needed, shells, keepshells;
549 int food, food_needed, keepfood;
550 int fuel_needed, fuel, petrol_needed, petrol, keeppetrol;
552 lcp = &lchr[(int)lp->lnd_type];
553 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
556 food_needed = get_minimum(lp, I_FOOD);
557 food = keepfood = vec[I_FOOD];
558 if (food < food_needed) {
560 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
561 food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
562 I_FOOD, (food_needed - food));
563 vec[I_FOOD] = keepfood;
564 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
566 if (food < food_needed)
571 shells_needed = lp->lnd_ammo;
572 shells = keepshells = vec[I_SHELL];
573 if (shells < shells_needed) {
575 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
576 shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
577 I_SHELL, (shells_needed - shells));
578 vec[I_SHELL] = keepshells;
579 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
582 if (shells < shells_needed)
586 fuel_needed = lp->lnd_fuelu;
590 petrol = petrol_needed = 0;
592 if (fuel < fuel_needed) {
594 ldround(((double)(fuel_needed - fuel) / 10.0), 1);
595 petrol = keeppetrol = vec[I_PETROL];
598 if (petrol < petrol_needed) {
600 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
601 petrol += try_supply_commod(lp->lnd_own,
602 lp->lnd_x, lp->lnd_y,
604 (petrol_needed - petrol));
605 vec[I_PETROL] = keeppetrol;
606 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
610 if (fuel < fuel_needed)
618 use_supply(struct lndstr *lp)
621 int vec[I_MAX + 1], shells_needed, shells, food, food_needed;
622 int fuel_needed, fuel, petrol_needed, petrol;
624 lcp = &lchr[(int)lp->lnd_type];
625 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
627 shells_needed = lp->lnd_ammo;
628 shells = vec[I_SHELL];
629 if (shells < shells_needed) {
631 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
632 shells += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_SHELL,
633 (shells_needed - shells));
634 vec[I_SHELL] = shells;
637 vec[I_SHELL] = max(vec[I_SHELL] - shells_needed, 0);
639 if (lp->lnd_frg) /* artillery */
642 food_needed = get_minimum(lp, I_FOOD);
645 if (food < food_needed) {
647 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
648 food += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_FOOD,
649 (food_needed - food));
653 vec[I_FOOD] = max(vec[I_FOOD] - food_needed, 0);
656 fuel_needed = lp->lnd_fuelu;
659 petrol = petrol_needed = 0;
661 if (fuel < fuel_needed) {
663 ldround(((double)(fuel_needed - fuel) / 10.0), 1);
664 petrol = vec[I_PETROL];
667 if (petrol < petrol_needed) {
669 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
670 petrol += supply_commod(lp->lnd_own,
671 lp->lnd_x, lp->lnd_y,
672 I_PETROL, (petrol_needed - petrol));
673 vec[I_PETROL] = petrol;
677 if (petrol >= petrol_needed) {
678 vec[I_PETROL] = max(vec[I_PETROL] - petrol_needed, 0);
679 lp->lnd_fuel += petrol_needed * 10;
681 lp->lnd_fuel += vec[I_PETROL] * 10;
686 lp->lnd_fuel = max(lp->lnd_fuel - fuel_needed, 0);
690 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
691 putland(lp->lnd_uid, lp);