2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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 files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * supply.c: Supply subroutines
30 * Known contributors to this file:
48 #include "prototypes.h"
50 static int get_minimum(struct lndstr *, i_type);
51 static int s_commod(int, int, int, i_type, int, int);
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, i_type type)
90 /* Ok, do we now have enough? */
91 amt = get_minimum(lp, type) - lp->lnd_item[type];
93 lp->lnd_item[type] += supply_commod(lp->lnd_own,
96 amt = get_minimum(lp, type) - lp->lnd_item[type];
98 /* Now, check again to see if we have enough. */
100 /* Are we on a ship? if so, try to get it from the ship first. */
101 if (lp->lnd_ship >= 0) {
102 getship(lp->lnd_ship, &ship);
103 /* Now, determine how much we can get */
104 if (amt > ship.shp_item[type])
105 amt = ship.shp_item[type];
106 /* Now, add and subtract */
107 lp->lnd_item[type] += amt;
108 ship.shp_item[type] -= amt;
109 putship(lp->lnd_ship, &ship);
113 if (opt_FUEL && type == I_PETROL) {
114 int fuel_needed = (lp->lnd_fuelu * (((float)etu_per_update
115 * land_mob_scale)) / 10.0);
117 while ((lp->lnd_fuel < fuel_needed) && lp->lnd_item[I_PETROL]) {
119 if (lp->lnd_fuel > lp->lnd_fuelc)
120 lp->lnd_fuel = lp->lnd_fuelc;
121 lp->lnd_item[I_PETROL]--;
127 * Actually get the commod
130 supply_commod(int own, int x, int y, i_type type, int total_wanted)
132 if (total_wanted < 0)
134 return s_commod(own, x, y, type, total_wanted, !player->simulation);
138 * Just return the number you COULD get, without doing it
141 try_supply_commod(int own, int x, int y, i_type type, int total_wanted)
143 if (total_wanted < 0)
146 return s_commod(own, x, y, type, total_wanted, 0);
149 /* Get supplies of a certain type */
151 s_commod(int own, int x, int y, i_type type, int total_wanted,
154 int wanted = total_wanted;
155 int gotten = 0, lookrange;
156 struct sctstr sect, dest;
162 /* leave at least 1 military in sectors/ships */
163 int minimum = (type == I_MILIT ? 1 : 0);
165 double move_cost, weight, mobcost;
171 /* try to get it from sector we're in */
172 getsect(x, y, &dest);
173 getsect(x, y, §);
174 if (sect.sct_own == own) {
175 if (sect.sct_item[type] - wanted >= minimum) {
176 sect.sct_item[type] -= wanted;
180 } else if (sect.sct_item[type] - minimum > 0) {
181 gotten += sect.sct_item[type] - minimum;
182 wanted -= sect.sct_item[type] - minimum;
183 sect.sct_item[type] = minimum;
188 /* look for a headquarters or warehouse */
189 lookrange = tfact(own, 10.0);
190 snxtsct_dist(&ns, x, y, lookrange);
191 while (nxtsct(&ns, §) && wanted) {
192 if (sect.sct_own != own)
194 if ((sect.sct_type != SCT_WAREH) &&
195 (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
197 if ((sect.sct_type == SCT_HEADQ) &&
198 (sect.sct_dist_x == sect.sct_x) &&
199 (sect.sct_dist_y == sect.sct_y))
201 if (sect.sct_effic < 60)
203 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD))
205 if (!opt_NOFOOD && type == I_FOOD) {
206 minimum = 2 + ((etu_per_update * eatrate)
207 * (sect.sct_item[I_CIVIL] + sect.sct_item[I_MILIT]
208 + sect.sct_item[I_UW]));
210 if (sect.sct_item[type] <= minimum) {
211 /* Don't bother... */
215 dp = &dchr[sect.sct_type];
216 packing = ip->i_pkg[dp->d_pkg];
217 if (packing > 1 && sect.sct_effic < 60)
219 weight = ((double)ip->i_lbs / (double)packing);
220 mobcost = move_cost * weight;
222 can_move = ((double)sect.sct_mobil / mobcost);
224 can_move = sect.sct_item[type] - minimum;
225 if (can_move > sect.sct_item[type] - minimum)
226 can_move = sect.sct_item[type] - minimum;
228 if (can_move >= wanted) {
231 sect.sct_item[type] -= wanted;
233 /* take off mobility for delivering sect */
234 n = roundavg(total_wanted * weight * move_cost);
237 if (n > sect.sct_mobil)
245 } else if (can_move > 0) {
249 sect.sct_item[type] -= can_move;
251 /* take off mobility for delivering sect */
252 n = roundavg(can_move * weight * move_cost);
255 if (n > sect.sct_mobil)
264 /* look for an owned ship in a harbor */
265 snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
267 while (nxtitem(&ni, &ship) && wanted) {
268 if (ship.shp_own != own)
271 if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
273 getsect(ship.shp_x, ship.shp_y, §);
274 if (sect.sct_type != SCT_HARBR)
276 if (sect.sct_effic < 2)
278 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD))
280 if (!opt_NOFOOD && type == I_FOOD)
281 minimum = 2 + ((etu_per_update * eatrate)
282 * (ship.shp_item[I_CIVIL] + ship.shp_item[I_MILIT]
283 + ship.shp_item[I_UW]));
284 if (ship.shp_item[type] <= minimum) {
285 /* Don't bother... */
289 dp = &dchr[sect.sct_type];
290 packing = ip->i_pkg[dp->d_pkg];
291 if (packing > 1 && sect.sct_effic < 60)
293 weight = ((double)ip->i_lbs / (double)packing);
294 mobcost = move_cost * weight;
296 can_move = ((double)sect.sct_mobil / mobcost);
298 can_move = ship.shp_item[type] - minimum;
299 if (can_move > ship.shp_item[type] - minimum)
300 can_move = ship.shp_item[type] - minimum;
301 if (can_move >= wanted) {
303 ship.shp_item[type] -= wanted;
305 n = roundavg(wanted * weight * move_cost);
308 if (n > sect.sct_mobil)
312 putship(ship.shp_uid, &ship);
316 } else if (can_move > 0) {
320 ship.shp_item[type] -= can_move;
322 n = roundavg(can_move * weight * move_cost);
325 if (n > sect.sct_mobil)
330 putship(ship.shp_uid, &ship);
336 /* look for an owned supply unit */
337 snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
339 while (nxtitem(&ni, &land) && wanted) {
342 if (land.lnd_own != own)
345 lcp = &lchr[(int)land.lnd_type];
346 if (!(lcp->l_flags & L_SUPPLY))
349 if (land.lnd_item[type] <= get_minimum(&land, type))
352 getsect(land.lnd_x, land.lnd_y, §);
353 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD))
356 if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
359 if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
362 if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
366 * Temporarily zap this unit's store, so the recursion
370 land.lnd_item[type] = 0;
371 putland(land.lnd_uid, &land);
373 land.lnd_item[type] =
374 save.lnd_item[type] + s_commod(own, land.lnd_x, land.lnd_y,
375 type, wanted, actually_doit);
377 putland(land.lnd_uid, &land);
379 putland(save.lnd_uid, &save);
382 min = get_minimum(&land, type);
384 weight = ((double)ip->i_lbs);
385 mobcost = move_cost * weight;
387 can_move = ((double)land.lnd_mobil / mobcost);
389 can_move = land.lnd_item[type] - min;
390 if (can_move > land.lnd_item[type] - min)
391 can_move = land.lnd_item[type] - min;
393 if (can_move >= wanted) {
394 land.lnd_item[type] -= wanted;
396 /* resupply the supply unit */
397 resupply_commod(&land, type);
399 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
402 putland(land.lnd_uid, &land);
404 } else if (can_move > 0) {
407 land.lnd_item[type] -= can_move;
409 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
412 putland(land.lnd_uid, &land);
416 /* We've done the best we could */
417 /* return the number gotten */
423 * We want to get enough shells to fire once,
424 * one update's worth of food, enough fuel for
429 get_minimum(struct lndstr *lp, i_type type)
434 lcp = &lchr[(int)lp->lnd_type];
435 max = lcp->l_item[type];
440 return 0; /* no food reqd, get out */
441 want = (((double)etu_per_update * eatrate) *
442 (double)lp->lnd_item[I_MILIT]) + 1;
449 * return the amount of pet we'd need to get to
450 * enough fuel for 1 update
456 want = (lp->lnd_fuelu * (((float)etu_per_update *
457 land_mob_scale)) / 10.0);
458 want -= lp->lnd_fuel;
461 d = (double)want / 10.0;
480 has_supply(struct lndstr *lp)
482 int shells_needed, shells, keepshells;
483 int food, food_needed, keepfood;
484 int fuel_needed, fuel, petrol_needed, petrol, keeppetrol;
487 food_needed = get_minimum(lp, I_FOOD);
488 food = keepfood = lp->lnd_item[I_FOOD];
489 if (food < food_needed) {
490 lp->lnd_item[I_FOOD] = 0;
491 putland(lp->lnd_uid, lp);
492 food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
493 I_FOOD, (food_needed - food));
494 lp->lnd_item[I_FOOD] = keepfood;
495 putland(lp->lnd_uid, lp);
497 if (food < food_needed)
502 shells_needed = lp->lnd_ammo;
503 shells = keepshells = lp->lnd_item[I_SHELL];
504 if (shells < shells_needed) {
505 lp->lnd_item[I_SHELL] = 0;
506 putland(lp->lnd_uid, lp);
507 shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
508 I_SHELL, (shells_needed - shells));
509 lp->lnd_item[I_SHELL] = keepshells;
510 putland(lp->lnd_uid, lp);
513 if (shells < shells_needed)
517 fuel_needed = lp->lnd_fuelu;
519 if (fuel < fuel_needed) {
521 ldround(((double)(fuel_needed - fuel) / 10.0), 1);
522 petrol = keeppetrol = lp->lnd_item[I_PETROL];
523 if (petrol < petrol_needed) {
524 lp->lnd_item[I_PETROL] = 0;
525 putland(lp->lnd_uid, lp);
526 petrol += try_supply_commod(lp->lnd_own,
527 lp->lnd_x, lp->lnd_y,
529 (petrol_needed - petrol));
530 lp->lnd_item[I_PETROL] = keeppetrol;
531 putland(lp->lnd_uid, lp);
536 if (fuel < fuel_needed)
544 use_supply(struct lndstr *lp)
546 int shells_needed, shells, food, food_needed;
547 int fuel_needed, fuel, petrol_needed, petrol;
549 shells_needed = lp->lnd_ammo;
550 shells = lp->lnd_item[I_SHELL];
551 if (shells < shells_needed) {
552 lp->lnd_item[I_SHELL] = 0;
553 putland(lp->lnd_uid, lp);
554 shells += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
555 I_SHELL, shells_needed - shells);
556 lp->lnd_item[I_SHELL] = shells;
559 lp->lnd_item[I_SHELL] = MAX(lp->lnd_item[I_SHELL] - shells_needed, 0);
561 if (lp->lnd_frg) /* artillery */
564 food_needed = get_minimum(lp, I_FOOD);
565 food = lp->lnd_item[I_SHELL];
567 if (food < food_needed) {
568 lp->lnd_item[I_FOOD] = 0;
569 putland(lp->lnd_uid, lp);
570 food += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
571 I_FOOD, food_needed - food);
572 lp->lnd_item[I_FOOD] = food;
575 lp->lnd_item[I_FOOD] = MAX(lp->lnd_item[I_FOOD] - food_needed, 0);
578 fuel_needed = lp->lnd_fuelu;
581 petrol = petrol_needed = 0;
583 if (fuel < fuel_needed) {
585 ldround(((double)(fuel_needed - fuel) / 10.0), 1);
586 petrol = lp->lnd_item[I_PETROL];
589 if (petrol < petrol_needed) {
590 lp->lnd_item[I_PETROL] = 0;
591 putland(lp->lnd_uid, lp);
592 petrol += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
593 I_PETROL, petrol_needed - petrol);
594 lp->lnd_item[I_PETROL] = petrol;
598 if (petrol >= petrol_needed) {
599 lp->lnd_item[I_PETROL]
600 = MAX(lp->lnd_item[I_PETROL] - petrol_needed, 0);
601 lp->lnd_fuel += petrol_needed * 10;
603 lp->lnd_fuel += lp->lnd_item[I_PETROL] * 10;
604 lp->lnd_item[I_PETROL] = 0;
608 lp->lnd_fuel = MAX(lp->lnd_fuel - fuel_needed, 0);
612 putland(lp->lnd_uid, lp);