2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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)
93 lcp = &lchr[(int)lp->lnd_type];
95 /* Ok, do we now have enough? */
96 amt = get_minimum(lp, type) - lp->lnd_item[type];
98 lp->lnd_item[type] += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
100 amt = get_minimum(lp, type) - lp->lnd_item[type];
102 /* Now, check again to see if we have enough. */
104 /* Are we on a ship? if so, try to get it from the ship first. */
105 if (lp->lnd_ship >= 0) {
106 getship(lp->lnd_ship, &ship);
107 /* Now, determine how much we can get */
108 if (amt > ship.shp_item[type])
109 amt = ship.shp_item[type];
110 /* Now, add and subtract */
111 lp->lnd_item[type] += amt;
112 ship.shp_item[type] -= amt;
113 putship(lp->lnd_ship, &ship);
117 if (opt_FUEL && type == I_PETROL) {
118 int fuel_needed = (lp->lnd_fuelu * (((float)etu_per_update
119 * land_mob_scale)) / 10.0);
121 while ((lp->lnd_fuel < fuel_needed) && lp->lnd_item[I_PETROL]) {
123 if (lp->lnd_fuel > lp->lnd_fuelc)
124 lp->lnd_fuel = lp->lnd_fuelc;
125 lp->lnd_item[I_PETROL]--;
131 * Actually get the commod
134 supply_commod(int own, int x, int y, int type, int total_wanted)
136 if (total_wanted < 0)
138 return s_commod(own, x, y, type, total_wanted, !player->simulation);
142 * Just return the number you COULD get, without doing it
145 try_supply_commod(int own, int x, int y, int type, int total_wanted)
147 if (total_wanted < 0)
150 return s_commod(own, x, y, type, total_wanted, 0);
153 /* Get supplies of a certain type */
155 s_commod(int own, int x, int y, int type, int total_wanted,
158 int wanted = total_wanted;
159 int gotten = 0, lookrange;
160 struct sctstr sect, dest;
166 /* leave at least 1 military in sectors/ships */
167 int minimum = (type == I_MILIT ? 1 : 0);
169 double move_cost, weight, mobcost;
175 /* try to get it from sector we're in */
176 getsect(x, y, &dest);
177 getsect(x, y, §);
178 if (sect.sct_own == own) {
179 if (sect.sct_item[type] - wanted >= minimum) {
180 sect.sct_item[type] -= wanted;
184 } else if (sect.sct_item[type] - minimum > 0) {
185 gotten += sect.sct_item[type] - minimum;
186 wanted -= sect.sct_item[type] - minimum;
187 sect.sct_item[type] = minimum;
192 /* look for a headquarters or warehouse */
193 lookrange = tfact(own, 10.0);
194 snxtsct_dist(&ns, x, y, lookrange);
195 while (nxtsct(&ns, §) && wanted) {
196 if (sect.sct_own != own)
198 if ((sect.sct_type != SCT_WAREH) &&
199 (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
201 if ((sect.sct_type == SCT_HEADQ) &&
202 (sect.sct_dist_x == sect.sct_x) &&
203 (sect.sct_dist_y == sect.sct_y))
205 if (sect.sct_effic < 60)
207 if (BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD) ==
210 if (!opt_NOFOOD && type == I_FOOD) {
211 minimum = 2 + ((etu_per_update * eatrate)
212 * (sect.sct_item[I_CIVIL] + sect.sct_item[I_MILIT]
213 + sect.sct_item[I_UW]));
215 if (sect.sct_item[type] <= minimum) {
216 /* Don't bother... */
220 dp = &dchr[sect.sct_type];
221 packing = ip->i_pkg[dp->d_pkg];
222 if (packing > 1 && sect.sct_effic < 60)
224 weight = ((double)ip->i_lbs / (double)packing);
225 mobcost = move_cost * weight;
227 can_move = ((double)sect.sct_mobil / mobcost);
229 can_move = sect.sct_item[type] - minimum;
230 if (can_move > sect.sct_item[type] - minimum)
231 can_move = sect.sct_item[type] - minimum;
233 if (can_move >= wanted) {
236 sect.sct_item[type] -= wanted;
238 /* take off mobility for delivering sect */
239 n = roundavg(total_wanted * weight * move_cost);
242 if (n > sect.sct_mobil)
244 sect.sct_mobil -= (u_char)n;
250 } else if (can_move > 0) {
254 sect.sct_item[type] -= can_move;
256 /* take off mobility for delivering sect */
257 n = roundavg(can_move * weight * move_cost);
260 if (n > sect.sct_mobil)
262 sect.sct_mobil -= (u_char)n;
269 /* look for an owned ship in a harbor */
270 snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
272 while (nxtitem(&ni, (s_char *)&ship) && wanted) {
273 if (ship.shp_own != own)
276 if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
278 getsect(ship.shp_x, ship.shp_y, §);
279 if (sect.sct_type != SCT_HARBR)
281 if (sect.sct_effic < 2)
283 if (BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD) ==
286 if (!opt_NOFOOD && type == I_FOOD)
287 minimum = 2 + ((etu_per_update * eatrate)
288 * (ship.shp_item[I_CIVIL] + ship.shp_item[I_MILIT]
289 + ship.shp_item[I_UW]));
290 if (ship.shp_item[type] <= minimum) {
291 /* Don't bother... */
295 dp = &dchr[sect.sct_type];
296 packing = ip->i_pkg[dp->d_pkg];
297 if (packing > 1 && sect.sct_effic < 60)
299 weight = ((double)ip->i_lbs / (double)packing);
300 mobcost = move_cost * weight;
302 can_move = ((double)sect.sct_mobil / mobcost);
304 can_move = ship.shp_item[type] - minimum;
305 if (can_move > ship.shp_item[type] - minimum)
306 can_move = ship.shp_item[type] - minimum;
307 if (can_move >= wanted) {
309 ship.shp_item[type] -= wanted;
311 n = roundavg(wanted * weight * move_cost);
314 if (n > sect.sct_mobil)
316 sect.sct_mobil -= (u_char)n;
318 putship(ship.shp_uid, &ship);
322 } else if (can_move > 0) {
326 ship.shp_item[type] -= can_move;
328 n = roundavg(can_move * weight * move_cost);
331 if (n > sect.sct_mobil)
333 sect.sct_mobil -= (u_char)n;
336 putship(ship.shp_uid, &ship);
342 /* look for an owned supply unit */
343 snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
345 while (nxtitem(&ni, (s_char *)&land) && wanted) {
348 if (land.lnd_own != own)
351 lcp = &lchr[(int)land.lnd_type];
352 if (!(lcp->l_flags & L_SUPPLY))
355 if (land.lnd_item[type] <= get_minimum(&land, type))
358 getsect(land.lnd_x, land.lnd_y, §);
359 if (BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD) ==
363 if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
366 if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
369 if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
373 * Temporarily zap this unit's store, so the recursion
377 land.lnd_item[type] = 0;
378 putland(land.lnd_uid, &land);
380 land.lnd_item[type] =
381 save.lnd_item[type] + s_commod(own, land.lnd_x, land.lnd_y,
382 type, wanted, actually_doit);
384 putland(land.lnd_uid, &land);
386 putland(save.lnd_uid, &save);
389 min = get_minimum(&land, type);
391 weight = ((double)ip->i_lbs);
392 mobcost = move_cost * weight;
394 can_move = ((double)land.lnd_mobil / mobcost);
396 can_move = land.lnd_item[type] - min;
397 if (can_move > land.lnd_item[type] - min)
398 can_move = land.lnd_item[type] - min;
400 if (can_move >= wanted) {
401 land.lnd_item[type] -= wanted;
403 /* resupply the supply unit */
404 resupply_commod(&land, type);
406 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
409 putland(land.lnd_uid, &land);
411 } else if (can_move > 0) {
414 land.lnd_item[type] -= can_move;
416 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
419 putland(land.lnd_uid, &land);
423 /* We've done the best we could */
424 /* return the number gotten */
430 * We want to get enough shells to fire once,
431 * one update's worth of food, enough fuel for
436 get_minimum(struct lndstr *lp, int type)
441 lcp = &lchr[(int)lp->lnd_type];
442 max = lcp->l_item[type];
447 return 0; /* no food reqd, get out */
448 want = (((double)etu_per_update * eatrate) *
449 (double)total_mil(lp)) + 1;
456 * return the amount of pet we'd need to get to
457 * enough fuel for 1 update
463 want = (lp->lnd_fuelu * (((float)etu_per_update *
464 land_mob_scale)) / 10.0);
465 want -= lp->lnd_fuel;
468 d = (double)want / 10.0;
487 has_supply(struct lndstr *lp)
490 int shells_needed, shells, keepshells;
491 int food, food_needed, keepfood;
492 int fuel_needed, fuel, petrol_needed, petrol, keeppetrol;
494 lcp = &lchr[(int)lp->lnd_type];
497 food_needed = get_minimum(lp, I_FOOD);
498 food = keepfood = lp->lnd_item[I_FOOD];
499 if (food < food_needed) {
500 lp->lnd_item[I_FOOD] = 0;
501 putland(lp->lnd_uid, lp);
502 food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
503 I_FOOD, (food_needed - food));
504 lp->lnd_item[I_FOOD] = keepfood;
505 putland(lp->lnd_uid, lp);
507 if (food < food_needed)
512 shells_needed = lp->lnd_ammo;
513 shells = keepshells = lp->lnd_item[I_SHELL];
514 if (shells < shells_needed) {
515 lp->lnd_item[I_SHELL] = 0;
516 putland(lp->lnd_uid, lp);
517 shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
518 I_SHELL, (shells_needed - shells));
519 lp->lnd_item[I_SHELL] = keepshells;
520 putland(lp->lnd_uid, lp);
523 if (shells < shells_needed)
527 fuel_needed = lp->lnd_fuelu;
529 if (fuel < fuel_needed) {
531 ldround(((double)(fuel_needed - fuel) / 10.0), 1);
532 petrol = keeppetrol = lp->lnd_item[I_PETROL];
533 if (petrol < petrol_needed) {
534 lp->lnd_item[I_PETROL] = 0;
535 putland(lp->lnd_uid, lp);
536 petrol += try_supply_commod(lp->lnd_own,
537 lp->lnd_x, lp->lnd_y,
539 (petrol_needed - petrol));
540 lp->lnd_item[I_PETROL] = keeppetrol;
541 putland(lp->lnd_uid, lp);
546 if (fuel < fuel_needed)
554 use_supply(struct lndstr *lp)
557 int shells_needed, shells, food, food_needed;
558 int fuel_needed, fuel, petrol_needed, petrol;
560 lcp = &lchr[(int)lp->lnd_type];
562 shells_needed = lp->lnd_ammo;
563 shells = lp->lnd_item[I_SHELL];
564 if (shells < shells_needed) {
565 lp->lnd_item[I_SHELL] = 0;
566 putland(lp->lnd_uid, lp);
567 shells += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_SHELL,
568 (shells_needed - shells));
569 lp->lnd_item[I_SHELL] = shells;
572 lp->lnd_item[I_SHELL] = max(lp->lnd_item[I_SHELL] - shells_needed, 0);
574 if (lp->lnd_frg) /* artillery */
577 food_needed = get_minimum(lp, I_FOOD);
578 food = lp->lnd_item[I_SHELL];
580 if (food < food_needed) {
581 lp->lnd_item[I_FOOD] = 0;
582 putland(lp->lnd_uid, lp);
583 food += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_FOOD,
584 (food_needed - food));
585 lp->lnd_item[I_FOOD] = food;
588 lp->lnd_item[I_FOOD] = max(lp->lnd_item[I_FOOD] - food_needed, 0);
591 fuel_needed = lp->lnd_fuelu;
594 petrol = petrol_needed = 0;
596 if (fuel < fuel_needed) {
598 ldround(((double)(fuel_needed - fuel) / 10.0), 1);
599 petrol = lp->lnd_item[I_PETROL];
602 if (petrol < petrol_needed) {
603 lp->lnd_item[I_PETROL] = 0;
604 putland(lp->lnd_uid, lp);
605 petrol += supply_commod(lp->lnd_own,
606 lp->lnd_x, lp->lnd_y,
607 I_PETROL, (petrol_needed - petrol));
608 lp->lnd_item[I_PETROL] = petrol;
612 if (petrol >= petrol_needed) {
613 lp->lnd_item[I_PETROL]
614 = max(lp->lnd_item[I_PETROL] - petrol_needed, 0);
615 lp->lnd_fuel += petrol_needed * 10;
617 lp->lnd_fuel += lp->lnd_item[I_PETROL] * 10;
618 lp->lnd_item[I_PETROL] = 0;
622 lp->lnd_fuel = max(lp->lnd_fuel - fuel_needed, 0);
626 putland(lp->lnd_uid, lp);