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 lnd_in_supply(struct lndstr *lp)
93 if (lp->lnd_item[I_FOOD] < get_minimum(lp, I_FOOD))
96 return lp->lnd_item[I_SHELL] >= get_minimum(lp, I_SHELL);
100 * Actually get the commod
103 supply_commod(int own, int x, int y, i_type type, int total_wanted)
105 if (total_wanted <= 0)
107 return s_commod(own, x, y, type, total_wanted, !player->simulation);
111 * Just return the number you COULD get, without doing it
114 try_supply_commod(int own, int x, int y, i_type type, int total_wanted)
116 if (total_wanted <= 0)
119 return s_commod(own, x, y, type, total_wanted, 0);
122 /* Get supplies of a certain type */
124 s_commod(int own, int x, int y, i_type type, int total_wanted,
127 int wanted = total_wanted;
128 int gotten = 0, lookrange;
129 struct sctstr sect, dest;
135 /* leave at least 1 military in sectors/ships */
138 double move_cost, weight, mobcost;
144 /* try to get it from sector we're in */
145 getsect(x, y, &dest);
146 getsect(x, y, §);
147 if (sect.sct_own == own) {
148 if (!opt_NOFOOD && type == I_FOOD)
149 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
151 if (sect.sct_item[type] - wanted >= minimum) {
152 sect.sct_item[type] -= wanted;
156 } else if (sect.sct_item[type] - minimum > 0) {
157 gotten += sect.sct_item[type] - minimum;
158 wanted -= sect.sct_item[type] - minimum;
159 sect.sct_item[type] = minimum;
164 /* look for a headquarters or warehouse */
165 lookrange = tfact(own, 10.0);
166 snxtsct_dist(&ns, x, y, lookrange);
167 while (nxtsct(&ns, §) && wanted) {
168 if (sect.sct_own != own)
170 if ((sect.sct_type != SCT_WAREH) &&
171 (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
173 if ((sect.sct_type == SCT_HEADQ) &&
174 (sect.sct_dist_x == sect.sct_x) &&
175 (sect.sct_dist_y == sect.sct_y))
177 if (sect.sct_effic < 60)
179 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
181 if (!opt_NOFOOD && type == I_FOOD)
182 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
184 if (sect.sct_item[type] <= minimum) {
185 /* Don't bother... */
189 dp = &dchr[sect.sct_type];
190 packing = ip->i_pkg[dp->d_pkg];
191 if (packing > 1 && sect.sct_effic < 60)
193 weight = (double)ip->i_lbs / packing;
194 mobcost = move_cost * weight;
196 can_move = (double)sect.sct_mobil / mobcost;
198 can_move = sect.sct_item[type] - minimum;
199 if (can_move > sect.sct_item[type] - minimum)
200 can_move = sect.sct_item[type] - minimum;
202 if (can_move >= wanted) {
205 sect.sct_item[type] -= wanted;
207 /* take off mobility for delivering sect */
208 n = roundavg(wanted * weight * move_cost);
211 if (n > sect.sct_mobil)
219 } else if (can_move > 0) {
223 sect.sct_item[type] -= can_move;
225 /* take off mobility for delivering sect */
226 n = roundavg(can_move * weight * move_cost);
229 if (n > sect.sct_mobil)
238 /* look for an owned ship in a harbor */
239 snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
241 while (nxtitem(&ni, &ship) && wanted) {
242 if (ship.shp_own != own)
245 if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
247 getsect(ship.shp_x, ship.shp_y, §);
248 if (sect.sct_type != SCT_HARBR)
250 if (sect.sct_effic < 2)
252 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
254 if (!opt_NOFOOD && type == I_FOOD)
255 minimum = 1 + (int)ceil(food_needed(ship.shp_item,
257 if (ship.shp_item[type] <= minimum) {
258 /* Don't bother... */
262 dp = &dchr[sect.sct_type];
263 packing = ip->i_pkg[dp->d_pkg];
264 if (packing > 1 && sect.sct_effic < 60)
266 weight = (double)ip->i_lbs / packing;
267 mobcost = move_cost * weight;
269 can_move = (double)sect.sct_mobil / mobcost;
271 can_move = ship.shp_item[type] - minimum;
272 if (can_move > ship.shp_item[type] - minimum)
273 can_move = ship.shp_item[type] - minimum;
274 if (can_move >= wanted) {
276 ship.shp_item[type] -= wanted;
278 n = roundavg(wanted * weight * move_cost);
281 if (n > sect.sct_mobil)
285 putship(ship.shp_uid, &ship);
289 } else if (can_move > 0) {
293 ship.shp_item[type] -= can_move;
295 n = roundavg(can_move * weight * move_cost);
298 if (n > sect.sct_mobil)
303 putship(ship.shp_uid, &ship);
309 /* look for an owned supply unit */
310 snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
312 while (nxtitem(&ni, &land) && wanted) {
315 if (land.lnd_own != own)
318 lcp = &lchr[(int)land.lnd_type];
319 if (!(lcp->l_flags & L_SUPPLY))
322 if (land.lnd_item[type] <= get_minimum(&land, type))
325 getsect(land.lnd_x, land.lnd_y, §);
326 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
329 if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
332 if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
337 * Recursive supply is disabled for now. It can introduce
338 * cycles into the "resupplies from" relation. The code below
339 * attempts to break these cycles by temporarily zapping the
340 * commodity being supplied. That puts the land file in a
341 * funny state temporarily, risking loss of supplies when
342 * something goes wrong on the way. Worse, it increases
343 * lnd_seqno even when !actually_doit, which can lead to
344 * spurious seqno mismatch oopses in users of
345 * lnd_could_be_supplied(). I can't be bothered to clean up
346 * this mess right now, because recursive resupply is too dumb
347 * to be really useful anyway: each step uses the first source
348 * it finds, without consideration of mobility cost. If you
349 * re-enable it, don't forget to uncomment its documentation
350 * in supply.t as well.
352 if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
356 * Temporarily zap this unit's store, so the recursion
360 land.lnd_item[type] = 0;
361 putland(land.lnd_uid, &land);
362 save.lnd_seqno = land.lnd_seqno;
364 land.lnd_item[type] =
365 save.lnd_item[type] + s_commod(own, land.lnd_x, land.lnd_y,
366 type, wanted, actually_doit);
368 putland(land.lnd_uid, &land);
370 putland(save.lnd_uid, &save);
374 min = get_minimum(&land, type);
377 mobcost = move_cost * weight;
379 can_move = (double)land.lnd_mobil / mobcost;
381 can_move = land.lnd_item[type] - min;
382 if (can_move > land.lnd_item[type] - min)
383 can_move = land.lnd_item[type] - min;
385 if (can_move >= wanted) {
386 land.lnd_item[type] -= wanted;
387 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
390 putland(land.lnd_uid, &land);
392 } else if (can_move > 0) {
395 land.lnd_item[type] -= can_move;
397 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
400 putland(land.lnd_uid, &land);
404 /* We've done the best we could */
405 /* return the number gotten */
411 * We want to get enough shells to fire once,
412 * one update's worth of food.
416 get_minimum(struct lndstr *lp, i_type type)
421 lcp = &lchr[(int)lp->lnd_type];
422 max = lcp->l_item[type];
427 return 0; /* no food reqd, get out */
428 want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
444 lnd_could_be_supplied(struct lndstr *lp)
446 int shells_needed, shells, keepshells;
447 int food, food_needed, keepfood;
450 food_needed = get_minimum(lp, I_FOOD);
451 food = keepfood = lp->lnd_item[I_FOOD];
452 if (food < food_needed) {
453 lp->lnd_item[I_FOOD] = 0;
454 putland(lp->lnd_uid, lp);
455 food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
456 I_FOOD, (food_needed - food));
457 lp->lnd_item[I_FOOD] = keepfood;
458 putland(lp->lnd_uid, lp);
460 if (food < food_needed)
465 shells_needed = lchr[lp->lnd_type].l_ammo;
466 shells = keepshells = lp->lnd_item[I_SHELL];
467 if (shells < shells_needed) {
468 lp->lnd_item[I_SHELL] = 0;
469 putland(lp->lnd_uid, lp);
470 shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
471 I_SHELL, (shells_needed - shells));
472 lp->lnd_item[I_SHELL] = keepshells;
473 putland(lp->lnd_uid, lp);
476 if (shells < shells_needed)