2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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:
42 #include "prototypes.h"
46 static int get_minimum(struct lndstr *, i_type);
47 static int s_commod(int, int, int, i_type, int, int);
50 * We want to get enough guns to be maxed out, enough shells to
51 * fire once, one update's worth of food, enough fuel for
54 * Firts, try to forage in the sector
55 * Second look for a warehouse or headquarters to leech
56 * Third, look for a ship we own in a harbor
57 * Fourth, look for supplies in a supply unit we own
58 * (one good reason to do this last is that the supply
59 * unit will then call resupply, taking more time)
61 * May want to put code to resupply with SAMs here, later --ts
65 resupply_all(struct lndstr *lp)
68 resupply_commod(lp, I_FOOD);
69 resupply_commod(lp, I_SHELL);
71 resupply_commod(lp, I_PETROL);
75 * If the unit has less than it's minimum level of a
76 * certain commodity, fill it, to the best of our abilities.
80 resupply_commod(struct lndstr *lp, i_type type)
85 /* Ok, do we now have enough? */
86 amt = get_minimum(lp, type) - lp->lnd_item[type];
88 lp->lnd_item[type] += supply_commod(lp->lnd_own,
91 amt = get_minimum(lp, type) - lp->lnd_item[type];
93 /* Now, check again to see if we have enough. */
95 /* Are we on a ship? if so, try to get it from the ship first. */
96 if (lp->lnd_ship >= 0) {
97 getship(lp->lnd_ship, &ship);
98 /* Now, determine how much we can get */
99 if (amt > ship.shp_item[type])
100 amt = ship.shp_item[type];
101 /* Now, add and subtract */
102 lp->lnd_item[type] += amt;
103 ship.shp_item[type] -= amt;
104 putship(lp->lnd_ship, &ship);
108 if (opt_FUEL && type == I_PETROL) {
109 int fuel_needed = lchr[lp->lnd_type].l_fuelu
110 * ((float)etu_per_update * land_mob_scale) / 10.0;
112 while ((lp->lnd_fuel < fuel_needed) && lp->lnd_item[I_PETROL]) {
114 if (lp->lnd_fuel > lchr[lp->lnd_type].l_fuelc)
115 lp->lnd_fuel = lchr[lp->lnd_type].l_fuelc;
116 lp->lnd_item[I_PETROL]--;
122 * Actually get the commod
125 supply_commod(int own, int x, int y, i_type type, int total_wanted)
127 if (total_wanted <= 0)
129 return s_commod(own, x, y, type, total_wanted, !player->simulation);
133 * Just return the number you COULD get, without doing it
136 try_supply_commod(int own, int x, int y, i_type type, int total_wanted)
138 if (total_wanted <= 0)
141 return s_commod(own, x, y, type, total_wanted, 0);
144 /* Get supplies of a certain type */
146 s_commod(int own, int x, int y, i_type type, int total_wanted,
149 int wanted = total_wanted;
150 int gotten = 0, lookrange;
151 struct sctstr sect, dest;
157 /* leave at least 1 military in sectors/ships */
158 int minimum = (type == I_MILIT ? 1 : 0);
160 double move_cost, weight, mobcost;
166 /* try to get it from sector we're in */
167 getsect(x, y, &dest);
168 getsect(x, y, §);
169 if (sect.sct_own == own) {
170 if (sect.sct_item[type] - wanted >= minimum) {
171 sect.sct_item[type] -= wanted;
175 } else if (sect.sct_item[type] - minimum > 0) {
176 gotten += sect.sct_item[type] - minimum;
177 wanted -= sect.sct_item[type] - minimum;
178 sect.sct_item[type] = minimum;
183 /* look for a headquarters or warehouse */
184 lookrange = tfact(own, 10.0);
185 snxtsct_dist(&ns, x, y, lookrange);
186 while (nxtsct(&ns, §) && wanted) {
187 if (sect.sct_own != own)
189 if ((sect.sct_type != SCT_WAREH) &&
190 (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
192 if ((sect.sct_type == SCT_HEADQ) &&
193 (sect.sct_dist_x == sect.sct_x) &&
194 (sect.sct_dist_y == sect.sct_y))
196 if (sect.sct_effic < 60)
198 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
200 if (!opt_NOFOOD && type == I_FOOD)
201 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
203 if (sect.sct_item[type] <= minimum) {
204 /* Don't bother... */
208 dp = &dchr[sect.sct_type];
209 packing = ip->i_pkg[dp->d_pkg];
210 if (packing > 1 && sect.sct_effic < 60)
212 weight = (double)ip->i_lbs / packing;
213 mobcost = move_cost * weight;
215 can_move = (double)sect.sct_mobil / mobcost;
217 can_move = sect.sct_item[type] - minimum;
218 if (can_move > sect.sct_item[type] - minimum)
219 can_move = sect.sct_item[type] - minimum;
221 if (can_move >= wanted) {
224 sect.sct_item[type] -= wanted;
226 /* take off mobility for delivering sect */
227 n = roundavg(total_wanted * weight * move_cost);
230 if (n > sect.sct_mobil)
238 } else if (can_move > 0) {
242 sect.sct_item[type] -= can_move;
244 /* take off mobility for delivering sect */
245 n = roundavg(can_move * weight * move_cost);
248 if (n > sect.sct_mobil)
257 /* look for an owned ship in a harbor */
258 snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
260 while (nxtitem(&ni, &ship) && wanted) {
261 if (ship.shp_own != own)
264 if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
266 getsect(ship.shp_x, ship.shp_y, §);
267 if (sect.sct_type != SCT_HARBR)
269 if (sect.sct_effic < 2)
271 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
273 if (!opt_NOFOOD && type == I_FOOD)
274 minimum = 1 + (int)ceil(food_needed(ship.shp_item,
276 if (ship.shp_item[type] <= minimum) {
277 /* Don't bother... */
281 dp = &dchr[sect.sct_type];
282 packing = ip->i_pkg[dp->d_pkg];
283 if (packing > 1 && sect.sct_effic < 60)
285 weight = (double)ip->i_lbs / packing;
286 mobcost = move_cost * weight;
288 can_move = (double)sect.sct_mobil / mobcost;
290 can_move = ship.shp_item[type] - minimum;
291 if (can_move > ship.shp_item[type] - minimum)
292 can_move = ship.shp_item[type] - minimum;
293 if (can_move >= wanted) {
295 ship.shp_item[type] -= wanted;
297 n = roundavg(wanted * weight * move_cost);
300 if (n > sect.sct_mobil)
304 putship(ship.shp_uid, &ship);
308 } else if (can_move > 0) {
312 ship.shp_item[type] -= can_move;
314 n = roundavg(can_move * weight * move_cost);
317 if (n > sect.sct_mobil)
322 putship(ship.shp_uid, &ship);
328 /* look for an owned supply unit */
329 snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
331 while (nxtitem(&ni, &land) && wanted) {
334 if (land.lnd_own != own)
337 lcp = &lchr[(int)land.lnd_type];
338 if (!(lcp->l_flags & L_SUPPLY))
341 if (land.lnd_item[type] <= get_minimum(&land, type))
344 getsect(land.lnd_x, land.lnd_y, §);
345 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
348 if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
351 if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
354 if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
358 * Temporarily zap this unit's store, so the recursion
362 land.lnd_item[type] = 0;
363 putland(land.lnd_uid, &land);
364 save.lnd_seqno = land.lnd_seqno;
366 land.lnd_item[type] =
367 save.lnd_item[type] + s_commod(own, land.lnd_x, land.lnd_y,
368 type, wanted, actually_doit);
370 putland(land.lnd_uid, &land);
372 putland(save.lnd_uid, &save);
375 min = get_minimum(&land, type);
378 mobcost = move_cost * weight;
380 can_move = (double)land.lnd_mobil / mobcost;
382 can_move = land.lnd_item[type] - min;
383 if (can_move > land.lnd_item[type] - min)
384 can_move = land.lnd_item[type] - min;
386 if (can_move >= wanted) {
387 land.lnd_item[type] -= wanted;
389 /* resupply the supply unit */
390 resupply_commod(&land, type);
392 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
395 putland(land.lnd_uid, &land);
397 } else if (can_move > 0) {
400 land.lnd_item[type] -= can_move;
402 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
405 putland(land.lnd_uid, &land);
409 /* We've done the best we could */
410 /* return the number gotten */
416 * We want to get enough shells to fire once,
417 * one update's worth of food, enough fuel for
422 get_minimum(struct lndstr *lp, i_type type)
427 lcp = &lchr[(int)lp->lnd_type];
428 max = lcp->l_item[type];
433 return 0; /* no food reqd, get out */
434 want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
441 * return the amount of pet we'd need to get to
442 * enough fuel for 1 update
447 want = lcp->l_fuelu * ((float)etu_per_update * land_mob_scale)
449 want -= lp->lnd_fuel;
469 has_supply(struct lndstr *lp)
471 int shells_needed, shells, keepshells;
472 int food, food_needed, keepfood;
473 int fuel_needed, fuel, petrol_needed, petrol, keeppetrol;
476 food_needed = get_minimum(lp, I_FOOD);
477 food = keepfood = lp->lnd_item[I_FOOD];
478 if (food < food_needed) {
479 lp->lnd_item[I_FOOD] = 0;
480 putland(lp->lnd_uid, lp);
481 food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
482 I_FOOD, (food_needed - food));
483 lp->lnd_item[I_FOOD] = keepfood;
484 putland(lp->lnd_uid, lp);
486 if (food < food_needed)
491 shells_needed = lchr[lp->lnd_type].l_ammo;
492 shells = keepshells = lp->lnd_item[I_SHELL];
493 if (shells < shells_needed) {
494 lp->lnd_item[I_SHELL] = 0;
495 putland(lp->lnd_uid, lp);
496 shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
497 I_SHELL, (shells_needed - shells));
498 lp->lnd_item[I_SHELL] = keepshells;
499 putland(lp->lnd_uid, lp);
502 if (shells < shells_needed)
506 fuel_needed = lchr[lp->lnd_type].l_fuelu;
508 if (fuel < fuel_needed) {
510 ldround((fuel_needed - fuel) / 10.0, 1);
511 petrol = keeppetrol = lp->lnd_item[I_PETROL];
512 if (petrol < petrol_needed) {
513 lp->lnd_item[I_PETROL] = 0;
514 putland(lp->lnd_uid, lp);
515 petrol += try_supply_commod(lp->lnd_own,
516 lp->lnd_x, lp->lnd_y,
518 (petrol_needed - petrol));
519 lp->lnd_item[I_PETROL] = keeppetrol;
520 putland(lp->lnd_uid, lp);
525 if (fuel < fuel_needed)