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:
31 * Markus Armbruster, 2009
42 #include "prototypes.h"
46 static int supply_commod(int, int, int, i_type, int);
47 static int s_commod(int, int, int, i_type, int, int);
48 static int get_minimum(struct lndstr *, i_type);
51 sct_supply(struct sctstr *sp, i_type type, int wanted)
53 if (sp->sct_item[type] < wanted) {
54 sp->sct_item[type] += supply_commod(sp->sct_own,
55 sp->sct_x, sp->sct_y, type,
56 wanted - sp->sct_item[type]);
59 return sp->sct_item[type] >= wanted;
63 shp_supply(struct shpstr *sp, i_type type, int wanted)
65 if (sp->shp_item[type] < wanted) {
66 sp->shp_item[type] += supply_commod(sp->shp_own,
67 sp->shp_x, sp->shp_y, type,
68 wanted - sp->shp_item[type]);
69 putship(sp->shp_uid, sp);
71 return sp->shp_item[type] >= wanted;
75 lnd_supply(struct lndstr *lp, i_type type, int wanted)
77 if (lp->lnd_item[type] < wanted) {
78 lp->lnd_item[type] += supply_commod(lp->lnd_own,
79 lp->lnd_x, lp->lnd_y, type,
80 wanted - lp->lnd_item[type]);
81 putland(lp->lnd_uid, lp);
83 return lp->lnd_item[type] >= wanted;
87 lnd_in_supply(struct lndstr *lp)
90 if (lp->lnd_item[I_FOOD] < get_minimum(lp, I_FOOD))
93 return lp->lnd_item[I_SHELL] >= get_minimum(lp, I_SHELL);
97 lnd_supply_all(struct lndstr *lp)
102 fail |= !lnd_supply(lp, I_FOOD, get_minimum(lp, I_FOOD));
103 fail |= !lnd_supply(lp, I_SHELL, get_minimum(lp, I_SHELL));
108 * Actually get the commod
111 supply_commod(int own, int x, int y, i_type type, int total_wanted)
113 if (total_wanted <= 0)
115 return s_commod(own, x, y, type, total_wanted, !player->simulation);
119 * Just return the number you COULD get, without doing it
122 try_supply_commod(int own, int x, int y, i_type type, int total_wanted)
124 if (total_wanted <= 0)
127 return s_commod(own, x, y, type, total_wanted, 0);
131 * Actually get the commod
133 * First, try to forage in the sector
134 * Second look for a warehouse or headquarters to leech
135 * Third, look for a ship we own in a harbor
136 * Fourth, look for supplies in a supply unit we own
137 * (one good reason to do this last is that the supply
138 * unit will then call resupply, taking more time)
140 * May want to put code to resupply with SAMs here, later --ts
143 s_commod(int own, int x, int y, i_type type, int total_wanted,
146 int wanted = total_wanted;
147 int gotten = 0, lookrange;
148 struct sctstr sect, dest;
154 /* leave at least 1 military in sectors/ships */
157 double move_cost, weight, mobcost;
163 /* try to get it from sector we're in */
164 getsect(x, y, &dest);
165 getsect(x, y, §);
166 if (sect.sct_own == own) {
167 if (!opt_NOFOOD && type == I_FOOD)
168 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
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(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))
356 * Recursive supply is disabled for now. It can introduce
357 * cycles into the "resupplies from" relation. The code below
358 * attempts to break these cycles by temporarily zapping the
359 * commodity being supplied. That puts the land file in a
360 * funny state temporarily, risking loss of supplies when
361 * something goes wrong on the way. Worse, it increases
362 * lnd_seqno even when !actually_doit, which can lead to
363 * spurious seqno mismatch oopses in users of
364 * lnd_could_be_supplied(). I can't be bothered to clean up
365 * this mess right now, because recursive resupply is too dumb
366 * to be really useful anyway: each step uses the first source
367 * it finds, without consideration of mobility cost. If you
368 * re-enable it, don't forget to uncomment its documentation
369 * in supply.t as well.
371 if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
375 * Temporarily zap this unit's store, so the recursion
379 land.lnd_item[type] = 0;
380 putland(land.lnd_uid, &land);
381 save.lnd_seqno = land.lnd_seqno;
383 land.lnd_item[type] =
384 save.lnd_item[type] + s_commod(own, land.lnd_x, land.lnd_y,
385 type, wanted, actually_doit);
387 putland(land.lnd_uid, &land);
389 putland(save.lnd_uid, &save);
393 min = get_minimum(&land, type);
396 mobcost = move_cost * weight;
398 can_move = (double)land.lnd_mobil / mobcost;
400 can_move = land.lnd_item[type] - min;
401 if (can_move > land.lnd_item[type] - min)
402 can_move = land.lnd_item[type] - min;
404 if (can_move >= wanted) {
405 land.lnd_item[type] -= wanted;
406 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
409 putland(land.lnd_uid, &land);
411 } else if (can_move > 0) {
414 land.lnd_item[type] -= can_move;
416 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
419 putland(land.lnd_uid, &land);
423 /* We've done the best we could */
424 /* return the number gotten */
430 * We want to get enough shells to fire once,
431 * one update's worth of food.
435 get_minimum(struct lndstr *lp, i_type type)
440 lcp = &lchr[(int)lp->lnd_type];
441 max = lcp->l_item[type];
446 return 0; /* no food reqd, get out */
447 want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
463 lnd_could_be_supplied(struct lndstr *lp)
465 int shells_needed, shells, keepshells;
466 int food, food_needed, keepfood;
469 food_needed = get_minimum(lp, I_FOOD);
470 food = keepfood = lp->lnd_item[I_FOOD];
471 if (food < food_needed) {
472 lp->lnd_item[I_FOOD] = 0;
473 putland(lp->lnd_uid, lp);
474 food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
475 I_FOOD, (food_needed - food));
476 lp->lnd_item[I_FOOD] = keepfood;
477 putland(lp->lnd_uid, lp);
479 if (food < food_needed)
484 shells_needed = lchr[lp->lnd_type].l_ammo;
485 shells = keepshells = lp->lnd_item[I_SHELL];
486 if (shells < shells_needed) {
487 lp->lnd_item[I_SHELL] = 0;
488 putland(lp->lnd_uid, lp);
489 shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
490 I_SHELL, (shells_needed - shells));
491 lp->lnd_item[I_SHELL] = keepshells;
492 putland(lp->lnd_uid, lp);
495 if (shells < shells_needed)