2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, 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.
53 * Firts, try to forage in the sector
54 * Second look for a warehouse or headquarters to leech
55 * Third, look for a ship we own in a harbor
56 * Fourth, look for supplies in a supply unit we own
57 * (one good reason to do this last is that the supply
58 * unit will then call resupply, taking more time)
60 * May want to put code to resupply with SAMs here, later --ts
64 resupply_all(struct lndstr *lp)
67 resupply_commod(lp, I_FOOD);
68 resupply_commod(lp, I_SHELL);
72 * If the unit has less than it's minimum level of a
73 * certain commodity, fill it, to the best of our abilities.
77 resupply_commod(struct lndstr *lp, i_type type)
81 amt = get_minimum(lp, type) - lp->lnd_item[type];
83 lp->lnd_item[type] += supply_commod(lp->lnd_own,
90 * Actually get the commod
93 supply_commod(int own, int x, int y, i_type type, int total_wanted)
95 if (total_wanted <= 0)
97 return s_commod(own, x, y, type, total_wanted, !player->simulation);
101 * Just return the number you COULD get, without doing it
104 try_supply_commod(int own, int x, int y, i_type type, int total_wanted)
106 if (total_wanted <= 0)
109 return s_commod(own, x, y, type, total_wanted, 0);
112 /* Get supplies of a certain type */
114 s_commod(int own, int x, int y, i_type type, int total_wanted,
117 int wanted = total_wanted;
118 int gotten = 0, lookrange;
119 struct sctstr sect, dest;
125 /* leave at least 1 military in sectors/ships */
126 int minimum = (type == I_MILIT ? 1 : 0);
128 double move_cost, weight, mobcost;
134 /* try to get it from sector we're in */
135 getsect(x, y, &dest);
136 getsect(x, y, §);
137 if (sect.sct_own == own) {
138 if (sect.sct_item[type] - wanted >= minimum) {
139 sect.sct_item[type] -= wanted;
143 } else if (sect.sct_item[type] - minimum > 0) {
144 gotten += sect.sct_item[type] - minimum;
145 wanted -= sect.sct_item[type] - minimum;
146 sect.sct_item[type] = minimum;
151 /* look for a headquarters or warehouse */
152 lookrange = tfact(own, 10.0);
153 snxtsct_dist(&ns, x, y, lookrange);
154 while (nxtsct(&ns, §) && wanted) {
155 if (sect.sct_own != own)
157 if ((sect.sct_type != SCT_WAREH) &&
158 (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
160 if ((sect.sct_type == SCT_HEADQ) &&
161 (sect.sct_dist_x == sect.sct_x) &&
162 (sect.sct_dist_y == sect.sct_y))
164 if (sect.sct_effic < 60)
166 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
168 if (!opt_NOFOOD && type == I_FOOD)
169 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
171 if (sect.sct_item[type] <= minimum) {
172 /* Don't bother... */
176 dp = &dchr[sect.sct_type];
177 packing = ip->i_pkg[dp->d_pkg];
178 if (packing > 1 && sect.sct_effic < 60)
180 weight = (double)ip->i_lbs / packing;
181 mobcost = move_cost * weight;
183 can_move = (double)sect.sct_mobil / mobcost;
185 can_move = sect.sct_item[type] - minimum;
186 if (can_move > sect.sct_item[type] - minimum)
187 can_move = sect.sct_item[type] - minimum;
189 if (can_move >= wanted) {
192 sect.sct_item[type] -= wanted;
194 /* take off mobility for delivering sect */
195 n = roundavg(total_wanted * weight * move_cost);
198 if (n > sect.sct_mobil)
206 } else if (can_move > 0) {
210 sect.sct_item[type] -= can_move;
212 /* take off mobility for delivering sect */
213 n = roundavg(can_move * weight * move_cost);
216 if (n > sect.sct_mobil)
225 /* look for an owned ship in a harbor */
226 snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
228 while (nxtitem(&ni, &ship) && wanted) {
229 if (ship.shp_own != own)
232 if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
234 getsect(ship.shp_x, ship.shp_y, §);
235 if (sect.sct_type != SCT_HARBR)
237 if (sect.sct_effic < 2)
239 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
241 if (!opt_NOFOOD && type == I_FOOD)
242 minimum = 1 + (int)ceil(food_needed(ship.shp_item,
244 if (ship.shp_item[type] <= minimum) {
245 /* Don't bother... */
249 dp = &dchr[sect.sct_type];
250 packing = ip->i_pkg[dp->d_pkg];
251 if (packing > 1 && sect.sct_effic < 60)
253 weight = (double)ip->i_lbs / packing;
254 mobcost = move_cost * weight;
256 can_move = (double)sect.sct_mobil / mobcost;
258 can_move = ship.shp_item[type] - minimum;
259 if (can_move > ship.shp_item[type] - minimum)
260 can_move = ship.shp_item[type] - minimum;
261 if (can_move >= wanted) {
263 ship.shp_item[type] -= wanted;
265 n = roundavg(wanted * weight * move_cost);
268 if (n > sect.sct_mobil)
272 putship(ship.shp_uid, &ship);
276 } else if (can_move > 0) {
280 ship.shp_item[type] -= can_move;
282 n = roundavg(can_move * weight * move_cost);
285 if (n > sect.sct_mobil)
290 putship(ship.shp_uid, &ship);
296 /* look for an owned supply unit */
297 snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
299 while (nxtitem(&ni, &land) && wanted) {
302 if (land.lnd_own != own)
305 lcp = &lchr[(int)land.lnd_type];
306 if (!(lcp->l_flags & L_SUPPLY))
309 if (land.lnd_item[type] <= get_minimum(&land, type))
312 getsect(land.lnd_x, land.lnd_y, §);
313 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
316 if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
319 if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
322 if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
326 * Temporarily zap this unit's store, so the recursion
330 land.lnd_item[type] = 0;
331 putland(land.lnd_uid, &land);
332 save.lnd_seqno = land.lnd_seqno;
334 land.lnd_item[type] =
335 save.lnd_item[type] + s_commod(own, land.lnd_x, land.lnd_y,
336 type, wanted, actually_doit);
338 putland(land.lnd_uid, &land);
340 putland(save.lnd_uid, &save);
343 min = get_minimum(&land, type);
346 mobcost = move_cost * weight;
348 can_move = (double)land.lnd_mobil / mobcost;
350 can_move = land.lnd_item[type] - min;
351 if (can_move > land.lnd_item[type] - min)
352 can_move = land.lnd_item[type] - min;
354 if (can_move >= wanted) {
355 land.lnd_item[type] -= wanted;
356 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
359 putland(land.lnd_uid, &land);
361 } else if (can_move > 0) {
364 land.lnd_item[type] -= can_move;
366 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
369 putland(land.lnd_uid, &land);
373 /* We've done the best we could */
374 /* return the number gotten */
380 * We want to get enough shells to fire once,
381 * one update's worth of food.
385 get_minimum(struct lndstr *lp, i_type type)
390 lcp = &lchr[(int)lp->lnd_type];
391 max = lcp->l_item[type];
396 return 0; /* no food reqd, get out */
397 want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
413 has_supply(struct lndstr *lp)
415 int shells_needed, shells, keepshells;
416 int food, food_needed, keepfood;
419 food_needed = get_minimum(lp, I_FOOD);
420 food = keepfood = lp->lnd_item[I_FOOD];
421 if (food < food_needed) {
422 lp->lnd_item[I_FOOD] = 0;
423 putland(lp->lnd_uid, lp);
424 food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
425 I_FOOD, (food_needed - food));
426 lp->lnd_item[I_FOOD] = keepfood;
427 putland(lp->lnd_uid, lp);
429 if (food < food_needed)
434 shells_needed = lchr[lp->lnd_type].l_ammo;
435 shells = keepshells = lp->lnd_item[I_SHELL];
436 if (shells < shells_needed) {
437 lp->lnd_item[I_SHELL] = 0;
438 putland(lp->lnd_uid, lp);
439 shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
440 I_SHELL, (shells_needed - shells));
441 lp->lnd_item[I_SHELL] = keepshells;
442 putland(lp->lnd_uid, lp);
445 if (shells < shells_needed)