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, lp->lnd_x, lp->lnd_y,
95 amt = get_minimum(lp, type) - lp->lnd_item[type];
97 /* Now, check again to see if we have enough. */
99 /* Are we on a ship? if so, try to get it from the ship first. */
100 if (lp->lnd_ship >= 0) {
101 getship(lp->lnd_ship, &ship);
102 /* Now, determine how much we can get */
103 if (amt > ship.shp_item[type])
104 amt = ship.shp_item[type];
105 /* Now, add and subtract */
106 lp->lnd_item[type] += amt;
107 ship.shp_item[type] -= amt;
108 putship(lp->lnd_ship, &ship);
112 if (opt_FUEL && type == I_PETROL) {
113 int fuel_needed = (lp->lnd_fuelu * (((float)etu_per_update
114 * land_mob_scale)) / 10.0);
116 while ((lp->lnd_fuel < fuel_needed) && lp->lnd_item[I_PETROL]) {
118 if (lp->lnd_fuel > lp->lnd_fuelc)
119 lp->lnd_fuel = lp->lnd_fuelc;
120 lp->lnd_item[I_PETROL]--;
126 * Actually get the commod
129 supply_commod(int own, int x, int y, i_type type, int total_wanted)
131 if (total_wanted < 0)
133 return s_commod(own, x, y, type, total_wanted, !player->simulation);
137 * Just return the number you COULD get, without doing it
140 try_supply_commod(int own, int x, int y, i_type type, int total_wanted)
142 if (total_wanted < 0)
145 return s_commod(own, x, y, type, total_wanted, 0);
148 /* Get supplies of a certain type */
150 s_commod(int own, int x, int y, i_type type, int total_wanted,
153 int wanted = total_wanted;
154 int gotten = 0, lookrange;
155 struct sctstr sect, dest;
161 /* leave at least 1 military in sectors/ships */
162 int minimum = (type == I_MILIT ? 1 : 0);
164 double move_cost, weight, mobcost;
170 /* try to get it from sector we're in */
171 getsect(x, y, &dest);
172 getsect(x, y, §);
173 if (sect.sct_own == own) {
174 if (sect.sct_item[type] - wanted >= minimum) {
175 sect.sct_item[type] -= wanted;
179 } else if (sect.sct_item[type] - minimum > 0) {
180 gotten += sect.sct_item[type] - minimum;
181 wanted -= sect.sct_item[type] - minimum;
182 sect.sct_item[type] = minimum;
187 /* look for a headquarters or warehouse */
188 lookrange = tfact(own, 10.0);
189 snxtsct_dist(&ns, x, y, lookrange);
190 while (nxtsct(&ns, §) && wanted) {
191 if (sect.sct_own != own)
193 if ((sect.sct_type != SCT_WAREH) &&
194 (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
196 if ((sect.sct_type == SCT_HEADQ) &&
197 (sect.sct_dist_x == sect.sct_x) &&
198 (sect.sct_dist_y == sect.sct_y))
200 if (sect.sct_effic < 60)
202 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)
239 sect.sct_mobil -= (u_char)n;
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)
257 sect.sct_mobil -= (u_char)n;
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) ==
281 if (!opt_NOFOOD && type == I_FOOD)
282 minimum = 2 + ((etu_per_update * eatrate)
283 * (ship.shp_item[I_CIVIL] + ship.shp_item[I_MILIT]
284 + ship.shp_item[I_UW]));
285 if (ship.shp_item[type] <= minimum) {
286 /* Don't bother... */
290 dp = &dchr[sect.sct_type];
291 packing = ip->i_pkg[dp->d_pkg];
292 if (packing > 1 && sect.sct_effic < 60)
294 weight = ((double)ip->i_lbs / (double)packing);
295 mobcost = move_cost * weight;
297 can_move = ((double)sect.sct_mobil / mobcost);
299 can_move = ship.shp_item[type] - minimum;
300 if (can_move > ship.shp_item[type] - minimum)
301 can_move = ship.shp_item[type] - minimum;
302 if (can_move >= wanted) {
304 ship.shp_item[type] -= wanted;
306 n = roundavg(wanted * weight * move_cost);
309 if (n > sect.sct_mobil)
311 sect.sct_mobil -= (u_char)n;
313 putship(ship.shp_uid, &ship);
317 } else if (can_move > 0) {
321 ship.shp_item[type] -= can_move;
323 n = roundavg(can_move * weight * move_cost);
326 if (n > sect.sct_mobil)
328 sect.sct_mobil -= (u_char)n;
331 putship(ship.shp_uid, &ship);
337 /* look for an owned supply unit */
338 snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
340 while (nxtitem(&ni, &land) && wanted) {
343 if (land.lnd_own != own)
346 lcp = &lchr[(int)land.lnd_type];
347 if (!(lcp->l_flags & L_SUPPLY))
350 if (land.lnd_item[type] <= get_minimum(&land, type))
353 getsect(land.lnd_x, land.lnd_y, §);
354 if (BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD) ==
358 if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
361 if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
364 if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
368 * Temporarily zap this unit's store, so the recursion
372 land.lnd_item[type] = 0;
373 putland(land.lnd_uid, &land);
375 land.lnd_item[type] =
376 save.lnd_item[type] + s_commod(own, land.lnd_x, land.lnd_y,
377 type, wanted, actually_doit);
379 putland(land.lnd_uid, &land);
381 putland(save.lnd_uid, &save);
384 min = get_minimum(&land, type);
386 weight = ((double)ip->i_lbs);
387 mobcost = move_cost * weight;
389 can_move = ((double)land.lnd_mobil / mobcost);
391 can_move = land.lnd_item[type] - min;
392 if (can_move > land.lnd_item[type] - min)
393 can_move = land.lnd_item[type] - min;
395 if (can_move >= wanted) {
396 land.lnd_item[type] -= wanted;
398 /* resupply the supply unit */
399 resupply_commod(&land, type);
401 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
404 putland(land.lnd_uid, &land);
406 } else if (can_move > 0) {
409 land.lnd_item[type] -= can_move;
411 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
414 putland(land.lnd_uid, &land);
418 /* We've done the best we could */
419 /* return the number gotten */
425 * We want to get enough shells to fire once,
426 * one update's worth of food, enough fuel for
431 get_minimum(struct lndstr *lp, i_type type)
436 lcp = &lchr[(int)lp->lnd_type];
437 max = lcp->l_item[type];
442 return 0; /* no food reqd, get out */
443 want = (((double)etu_per_update * eatrate) *
444 (double)total_mil(lp)) + 1;
451 * return the amount of pet we'd need to get to
452 * enough fuel for 1 update
458 want = (lp->lnd_fuelu * (((float)etu_per_update *
459 land_mob_scale)) / 10.0);
460 want -= lp->lnd_fuel;
463 d = (double)want / 10.0;
482 has_supply(struct lndstr *lp)
484 int shells_needed, shells, keepshells;
485 int food, food_needed, keepfood;
486 int fuel_needed, fuel, petrol_needed, petrol, keeppetrol;
489 food_needed = get_minimum(lp, I_FOOD);
490 food = keepfood = lp->lnd_item[I_FOOD];
491 if (food < food_needed) {
492 lp->lnd_item[I_FOOD] = 0;
493 putland(lp->lnd_uid, lp);
494 food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
495 I_FOOD, (food_needed - food));
496 lp->lnd_item[I_FOOD] = keepfood;
497 putland(lp->lnd_uid, lp);
499 if (food < food_needed)
504 shells_needed = lp->lnd_ammo;
505 shells = keepshells = lp->lnd_item[I_SHELL];
506 if (shells < shells_needed) {
507 lp->lnd_item[I_SHELL] = 0;
508 putland(lp->lnd_uid, lp);
509 shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
510 I_SHELL, (shells_needed - shells));
511 lp->lnd_item[I_SHELL] = keepshells;
512 putland(lp->lnd_uid, lp);
515 if (shells < shells_needed)
519 fuel_needed = lp->lnd_fuelu;
521 if (fuel < fuel_needed) {
523 ldround(((double)(fuel_needed - fuel) / 10.0), 1);
524 petrol = keeppetrol = lp->lnd_item[I_PETROL];
525 if (petrol < petrol_needed) {
526 lp->lnd_item[I_PETROL] = 0;
527 putland(lp->lnd_uid, lp);
528 petrol += try_supply_commod(lp->lnd_own,
529 lp->lnd_x, lp->lnd_y,
531 (petrol_needed - petrol));
532 lp->lnd_item[I_PETROL] = keeppetrol;
533 putland(lp->lnd_uid, lp);
538 if (fuel < fuel_needed)
546 use_supply(struct lndstr *lp)
548 int shells_needed, shells, food, food_needed;
549 int fuel_needed, fuel, petrol_needed, petrol;
551 shells_needed = lp->lnd_ammo;
552 shells = lp->lnd_item[I_SHELL];
553 if (shells < shells_needed) {
554 lp->lnd_item[I_SHELL] = 0;
555 putland(lp->lnd_uid, lp);
556 shells += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_SHELL,
557 (shells_needed - shells));
558 lp->lnd_item[I_SHELL] = shells;
561 lp->lnd_item[I_SHELL] = MAX(lp->lnd_item[I_SHELL] - shells_needed, 0);
563 if (lp->lnd_frg) /* artillery */
566 food_needed = get_minimum(lp, I_FOOD);
567 food = lp->lnd_item[I_SHELL];
569 if (food < food_needed) {
570 lp->lnd_item[I_FOOD] = 0;
571 putland(lp->lnd_uid, lp);
572 food += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_FOOD,
573 (food_needed - food));
574 lp->lnd_item[I_FOOD] = food;
577 lp->lnd_item[I_FOOD] = MAX(lp->lnd_item[I_FOOD] - food_needed, 0);
580 fuel_needed = lp->lnd_fuelu;
583 petrol = petrol_needed = 0;
585 if (fuel < fuel_needed) {
587 ldround(((double)(fuel_needed - fuel) / 10.0), 1);
588 petrol = lp->lnd_item[I_PETROL];
591 if (petrol < petrol_needed) {
592 lp->lnd_item[I_PETROL] = 0;
593 putland(lp->lnd_uid, lp);
594 petrol += supply_commod(lp->lnd_own,
595 lp->lnd_x, lp->lnd_y,
596 I_PETROL, (petrol_needed - petrol));
597 lp->lnd_item[I_PETROL] = petrol;
601 if (petrol >= petrol_needed) {
602 lp->lnd_item[I_PETROL]
603 = MAX(lp->lnd_item[I_PETROL] - petrol_needed, 0);
604 lp->lnd_fuel += petrol_needed * 10;
606 lp->lnd_fuel += lp->lnd_item[I_PETROL] * 10;
607 lp->lnd_item[I_PETROL] = 0;
611 lp->lnd_fuel = MAX(lp->lnd_fuel - fuel_needed, 0);
615 putland(lp->lnd_uid, lp);