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:
50 #include "prototypes.h"
52 static int get_minimum(struct lndstr *, int);
53 static int s_commod(int, int, int, int, int, int);
56 * We want to get enough guns to be maxed out, enough shells to
57 * fire once, one update's worth of food, enough fuel for
60 * Firts, try to forage in the sector
61 * Second look for a warehouse or headquarters to leech
62 * Third, look for a ship we own in a harbor
63 * Fourth, look for supplies in a supply unit we own
64 * (one good reason to do this last is that the supply
65 * unit will then call resupply, taking more time)
67 * May want to put code to resupply with SAMs here, later --ts
72 resupply_all(struct lndstr *lp)
75 resupply_commod(lp, I_FOOD);
76 resupply_commod(lp, I_SHELL);
78 resupply_commod(lp, I_PETROL);
82 * If the unit has less than it's minimum level of a
83 * certain commodity, fill it, to the best of our abilities.
87 resupply_commod(struct lndstr *lp, int type)
95 lcp = &lchr[(int)lp->lnd_type];
97 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
98 /* Ok, do we now have enough? */
99 if (vec[type] < get_minimum(lp, type)) {
100 vec[type] += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, type,
101 get_minimum(lp, type) - vec[type]);
102 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
104 /* Now, check again to see if we have enough. */
105 if (vec[type] < get_minimum(lp, type)) {
106 /* Nope. How much do we need? */
107 amt = (get_minimum(lp, type) - vec[type]);
108 /* Are we on a ship? if so, try to get it from the ship first. */
109 if (lp->lnd_ship >= 0) {
110 getship(lp->lnd_ship, &ship);
111 getvec(VT_ITEM, svec, (s_char *)&ship, EF_SHIP);
112 /* Now, determine how much we can get */
113 amt = (amt < svec[type]) ? amt : svec[type];
114 /* Now, add and subtract */
117 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
118 putvec(VT_ITEM, svec, (s_char *)&ship, EF_SHIP);
119 putship(lp->lnd_ship, &ship);
123 if (opt_FUEL && type == I_PETROL) {
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;
183 /* try to get it from sector we're in */
184 getsect(x, y, &dest);
185 getsect(x, y, §);
186 if (sect.sct_own == own) {
187 getvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
188 if ((vec[type] - minimum) >= wanted) {
191 putvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
195 } else if ((vec[type] - minimum) > 0) {
196 gotten += (vec[type] - minimum);
197 wanted -= (vec[type] - minimum);
200 putvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
205 /* look for a headquarters or warehouse */
206 lookrange = tfact(own, (double)10.0);
207 snxtsct_dist(&ns, x, y, lookrange);
208 while (nxtsct(&ns, §) && wanted) {
209 if (sect.sct_own != own)
211 if ((sect.sct_type != SCT_WAREH) &&
212 (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
214 if ((sect.sct_type == SCT_HEADQ) &&
215 (sect.sct_dist_x == sect.sct_x) &&
216 (sect.sct_dist_y == sect.sct_y))
218 if (sect.sct_effic < 60)
220 if (BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD) ==
223 getvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
224 if (!opt_NOFOOD && type == I_FOOD) {
225 minimum = (((double)etu_per_update * eatrate) *
226 (double)(vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW]))
229 if (vec[type] <= minimum) {
230 /* Don't bother... */
234 dp = &dchr[sect.sct_type];
235 packing = ip->i_pkg[dp->d_pkg];
236 if (packing > 1 && sect.sct_effic < 60)
238 weight = ((double)ip->i_lbs / (double)packing);
239 mobcost = move_cost * weight;
241 can_move = ((double)sect.sct_mobil / mobcost);
243 can_move = vec[type] - minimum;
244 if (can_move > (vec[type] - minimum))
245 can_move = (vec[type] - minimum);
247 if (can_move >= wanted) {
252 /* take off mobility for delivering sect */
253 n = roundavg(total_wanted * weight * move_cost);
256 if (n > sect.sct_mobil)
258 sect.sct_mobil -= (u_char)n;
261 putvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
266 } else if (can_move > 0) {
270 vec[type] -= can_move;
272 /* take off mobility for delivering sect */
273 n = roundavg(can_move * weight * move_cost);
276 if (n > sect.sct_mobil)
278 sect.sct_mobil -= (u_char)n;
281 putvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
287 /* look for an owned ship in a harbor */
288 snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
290 while (nxtitem(&ni, (s_char *)&ship) && wanted) {
291 if (ship.shp_own != own)
294 if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
296 getsect(ship.shp_x, ship.shp_y, §);
297 if (sect.sct_type != SCT_HARBR)
299 if (sect.sct_effic < 2)
301 if (BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD) ==
304 getvec(VT_ITEM, vec, (s_char *)&ship, EF_SHIP);
305 if (!opt_NOFOOD && type == I_FOOD)
306 minimum = (((double)etu_per_update * eatrate) *
307 (double)(vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW]))
309 if (vec[type] <= minimum) {
310 /* Don't bother... */
314 dp = &dchr[sect.sct_type];
315 packing = ip->i_pkg[dp->d_pkg];
316 if (packing > 1 && sect.sct_effic < 60)
318 weight = ((double)ip->i_lbs / (double)packing);
319 mobcost = move_cost * weight;
321 can_move = ((double)sect.sct_mobil / mobcost);
323 can_move = vec[type] - minimum;
324 if (can_move > (vec[type] - minimum))
325 can_move = (vec[type] - minimum);
326 if (can_move >= wanted) {
330 n = roundavg(wanted * weight * move_cost);
333 if (n > sect.sct_mobil)
335 sect.sct_mobil -= (u_char)n;
337 putvec(VT_ITEM, vec, (s_char *)&ship, EF_SHIP);
338 putship(ship.shp_uid, &ship);
342 } else if (can_move > 0) {
346 vec[type] -= can_move;
348 n = roundavg(can_move * weight * move_cost);
351 if (n > sect.sct_mobil)
353 sect.sct_mobil -= (u_char)n;
356 putvec(VT_ITEM, vec, (s_char *)&ship, EF_SHIP);
357 putship(ship.shp_uid, &ship);
363 /* look for an owned supply unit */
364 snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
366 while (nxtitem(&ni, (s_char *)&land) && wanted) {
369 if (land.lnd_own != own)
372 lcp = &lchr[(int)land.lnd_type];
373 if (!(lcp->l_flags & L_SUPPLY))
376 getvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
377 if (vec[type] <= get_minimum(&land, type))
380 getsect(land.lnd_x, land.lnd_y, §);
381 if (BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD) ==
385 if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
388 if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
391 if ((vec[type] - wanted) < get_minimum(&land, type)) {
396 * Temporarily zap this unit's store, so the recursion
402 putvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
404 putland(land.lnd_uid, &land);
406 hold += 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(save.lnd_uid, &save);
417 getvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
418 min = get_minimum(&land, type);
420 weight = ((double)ip->i_lbs);
421 mobcost = move_cost * weight;
423 can_move = ((double)land.lnd_mobil / mobcost);
425 can_move = vec[type] - min;
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 */
463 * We want to get enough shells to fire once,
464 * one update's worth of food, enough fuel for
469 get_minimum(struct lndstr *lp, int type)
474 lcp = &lchr[(int)lp->lnd_type];
475 max = vl_find(V_ITEM(type), lcp->l_vtype, lcp->l_vamt, (int)lcp->l_nv);
480 return 0; /* no food reqd, get out */
481 want = (((double)etu_per_update * eatrate) *
482 (double)total_mil(lp)) + 1;
489 * return the amount of pet we'd need to get to
490 * enough fuel for 1 update
496 want = (lp->lnd_fuelu * (((float)etu_per_update *
497 land_mob_scale)) / 10.0);
498 want -= lp->lnd_fuel;
501 d = (double)want / 10.0;
520 has_supply(struct lndstr *lp)
523 int vec[I_MAX + 1], shells_needed, shells, keepshells;
524 int food, food_needed, keepfood;
525 int fuel_needed, fuel, petrol_needed, petrol, keeppetrol;
527 lcp = &lchr[(int)lp->lnd_type];
528 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
531 food_needed = get_minimum(lp, I_FOOD);
532 food = keepfood = vec[I_FOOD];
533 if (food < food_needed) {
535 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
536 food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
537 I_FOOD, (food_needed - food));
538 vec[I_FOOD] = keepfood;
539 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
541 if (food < food_needed)
546 shells_needed = lp->lnd_ammo;
547 shells = keepshells = vec[I_SHELL];
548 if (shells < shells_needed) {
550 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
551 shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
552 I_SHELL, (shells_needed - shells));
553 vec[I_SHELL] = keepshells;
554 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
557 if (shells < shells_needed)
561 fuel_needed = lp->lnd_fuelu;
565 petrol = petrol_needed = 0;
567 if (fuel < fuel_needed) {
569 ldround(((double)(fuel_needed - fuel) / 10.0), 1);
570 petrol = keeppetrol = vec[I_PETROL];
573 if (petrol < petrol_needed) {
575 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
576 petrol += try_supply_commod(lp->lnd_own,
577 lp->lnd_x, lp->lnd_y,
579 (petrol_needed - petrol));
580 vec[I_PETROL] = keeppetrol;
581 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
585 if (fuel < fuel_needed)
593 use_supply(struct lndstr *lp)
596 int vec[I_MAX + 1], shells_needed, shells, food, food_needed;
597 int fuel_needed, fuel, petrol_needed, petrol;
599 lcp = &lchr[(int)lp->lnd_type];
600 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
602 shells_needed = lp->lnd_ammo;
603 shells = vec[I_SHELL];
604 if (shells < shells_needed) {
606 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
607 shells += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_SHELL,
608 (shells_needed - shells));
609 vec[I_SHELL] = shells;
612 vec[I_SHELL] = max(vec[I_SHELL] - shells_needed, 0);
614 if (lp->lnd_frg) /* artillery */
617 food_needed = get_minimum(lp, I_FOOD);
620 if (food < food_needed) {
622 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
623 food += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_FOOD,
624 (food_needed - food));
628 vec[I_FOOD] = max(vec[I_FOOD] - food_needed, 0);
631 fuel_needed = lp->lnd_fuelu;
634 petrol = petrol_needed = 0;
636 if (fuel < fuel_needed) {
638 ldround(((double)(fuel_needed - fuel) / 10.0), 1);
639 petrol = vec[I_PETROL];
642 if (petrol < petrol_needed) {
644 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
645 petrol += supply_commod(lp->lnd_own,
646 lp->lnd_x, lp->lnd_y,
647 I_PETROL, (petrol_needed - petrol));
648 vec[I_PETROL] = petrol;
652 if (petrol >= petrol_needed) {
653 vec[I_PETROL] = max(vec[I_PETROL] - petrol_needed, 0);
654 lp->lnd_fuel += petrol_needed * 10;
656 lp->lnd_fuel += vec[I_PETROL] * 10;
661 lp->lnd_fuel = max(lp->lnd_fuel - fuel_needed, 0);
665 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
666 putland(lp->lnd_uid, lp);