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)) {
398 putvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
400 putland(land.lnd_uid, &land);
402 s_commod(own, land.lnd_x, land.lnd_y, type, wanted,
405 putvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
406 putland(land.lnd_uid, &land);
408 putland(l2.lnd_uid, &l2);
411 getvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
412 min = get_minimum(&land, type);
414 weight = ((double)ip->i_lbs);
415 mobcost = move_cost * weight;
417 can_move = ((double)land.lnd_mobil / mobcost);
419 can_move = vec[type] - min;
420 if (can_move > (vec[type] - min))
421 can_move = (vec[type] - min);
423 if (can_move >= wanted) {
426 /* resupply the supply unit */
427 resupply_commod(&land, type);
429 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
432 putvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
433 putland(land.lnd_uid, &land);
436 } else if (can_move > 0) {
439 vec[type] -= can_move;
441 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
444 putvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
445 putland(land.lnd_uid, &land);
450 /* We've done the best we could */
451 /* return the number gotten */
457 * We want to get enough shells to fire once,
458 * one update's worth of food, enough fuel for
463 get_minimum(struct lndstr *lp, int type)
468 lcp = &lchr[(int)lp->lnd_type];
469 max = vl_find(V_ITEM(type), lcp->l_vtype, lcp->l_vamt, (int)lcp->l_nv);
474 return 0; /* no food reqd, get out */
475 want = (((double)etu_per_update * eatrate) *
476 (double)total_mil(lp)) + 1;
483 * return the amount of pet we'd need to get to
484 * enough fuel for 1 update
490 want = (lp->lnd_fuelu * (((float)etu_per_update *
491 land_mob_scale)) / 10.0);
492 want -= lp->lnd_fuel;
495 d = (double)want / 10.0;
514 has_supply(struct lndstr *lp)
517 int vec[I_MAX + 1], shells_needed, shells, keepshells;
518 int food, food_needed, keepfood;
519 int fuel_needed, fuel, petrol_needed, petrol, keeppetrol;
521 lcp = &lchr[(int)lp->lnd_type];
522 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
525 food_needed = get_minimum(lp, I_FOOD);
526 food = keepfood = vec[I_FOOD];
527 if (food < food_needed) {
529 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
530 food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
531 I_FOOD, (food_needed - food));
532 vec[I_FOOD] = keepfood;
533 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
535 if (food < food_needed)
540 shells_needed = lp->lnd_ammo;
541 shells = keepshells = vec[I_SHELL];
542 if (shells < shells_needed) {
544 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
545 shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
546 I_SHELL, (shells_needed - shells));
547 vec[I_SHELL] = keepshells;
548 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
551 if (shells < shells_needed)
555 fuel_needed = lp->lnd_fuelu;
559 petrol = petrol_needed = 0;
561 if (fuel < fuel_needed) {
563 ldround(((double)(fuel_needed - fuel) / 10.0), 1);
564 petrol = keeppetrol = vec[I_PETROL];
567 if (petrol < petrol_needed) {
569 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
570 petrol += try_supply_commod(lp->lnd_own,
571 lp->lnd_x, lp->lnd_y,
573 (petrol_needed - petrol));
574 vec[I_PETROL] = keeppetrol;
575 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
579 if (fuel < fuel_needed)
587 use_supply(struct lndstr *lp)
590 int vec[I_MAX + 1], shells_needed, shells, food, food_needed;
591 int fuel_needed, fuel, petrol_needed, petrol;
593 lcp = &lchr[(int)lp->lnd_type];
594 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
596 shells_needed = lp->lnd_ammo;
597 shells = vec[I_SHELL];
598 if (shells < shells_needed) {
600 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
601 shells += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_SHELL,
602 (shells_needed - shells));
603 vec[I_SHELL] = shells;
606 vec[I_SHELL] = max(vec[I_SHELL] - shells_needed, 0);
608 if (lp->lnd_frg) /* artillery */
611 food_needed = get_minimum(lp, I_FOOD);
614 if (food < food_needed) {
616 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
617 food += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_FOOD,
618 (food_needed - food));
622 vec[I_FOOD] = max(vec[I_FOOD] - food_needed, 0);
625 fuel_needed = lp->lnd_fuelu;
628 petrol = petrol_needed = 0;
630 if (fuel < fuel_needed) {
632 ldround(((double)(fuel_needed - fuel) / 10.0), 1);
633 petrol = vec[I_PETROL];
636 if (petrol < petrol_needed) {
638 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
639 petrol += supply_commod(lp->lnd_own,
640 lp->lnd_x, lp->lnd_y,
641 I_PETROL, (petrol_needed - petrol));
642 vec[I_PETROL] = petrol;
646 if (petrol >= petrol_needed) {
647 vec[I_PETROL] = max(vec[I_PETROL] - petrol_needed, 0);
648 lp->lnd_fuel += petrol_needed * 10;
650 lp->lnd_fuel += vec[I_PETROL] * 10;
655 lp->lnd_fuel = max(lp->lnd_fuel - fuel_needed, 0);
659 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
660 putland(lp->lnd_uid, lp);