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 */
136 int minimum = (type == I_MILIT ? 1 : 0);
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 (sect.sct_item[type] - wanted >= minimum) {
149 sect.sct_item[type] -= wanted;
153 } else if (sect.sct_item[type] - minimum > 0) {
154 gotten += sect.sct_item[type] - minimum;
155 wanted -= sect.sct_item[type] - minimum;
156 sect.sct_item[type] = minimum;
161 /* look for a headquarters or warehouse */
162 lookrange = tfact(own, 10.0);
163 snxtsct_dist(&ns, x, y, lookrange);
164 while (nxtsct(&ns, §) && wanted) {
165 if (sect.sct_own != own)
167 if ((sect.sct_type != SCT_WAREH) &&
168 (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
170 if ((sect.sct_type == SCT_HEADQ) &&
171 (sect.sct_dist_x == sect.sct_x) &&
172 (sect.sct_dist_y == sect.sct_y))
174 if (sect.sct_effic < 60)
176 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
178 if (!opt_NOFOOD && type == I_FOOD)
179 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
181 if (sect.sct_item[type] <= minimum) {
182 /* Don't bother... */
186 dp = &dchr[sect.sct_type];
187 packing = ip->i_pkg[dp->d_pkg];
188 if (packing > 1 && sect.sct_effic < 60)
190 weight = (double)ip->i_lbs / packing;
191 mobcost = move_cost * weight;
193 can_move = (double)sect.sct_mobil / mobcost;
195 can_move = sect.sct_item[type] - minimum;
196 if (can_move > sect.sct_item[type] - minimum)
197 can_move = sect.sct_item[type] - minimum;
199 if (can_move >= wanted) {
202 sect.sct_item[type] -= wanted;
204 /* take off mobility for delivering sect */
205 n = roundavg(wanted * weight * move_cost);
208 if (n > sect.sct_mobil)
216 } else if (can_move > 0) {
220 sect.sct_item[type] -= can_move;
222 /* take off mobility for delivering sect */
223 n = roundavg(can_move * weight * move_cost);
226 if (n > sect.sct_mobil)
235 /* look for an owned ship in a harbor */
236 snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
238 while (nxtitem(&ni, &ship) && wanted) {
239 if (ship.shp_own != own)
242 if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
244 getsect(ship.shp_x, ship.shp_y, §);
245 if (sect.sct_type != SCT_HARBR)
247 if (sect.sct_effic < 2)
249 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
251 if (!opt_NOFOOD && type == I_FOOD)
252 minimum = 1 + (int)ceil(food_needed(ship.shp_item,
254 if (ship.shp_item[type] <= minimum) {
255 /* Don't bother... */
259 dp = &dchr[sect.sct_type];
260 packing = ip->i_pkg[dp->d_pkg];
261 if (packing > 1 && sect.sct_effic < 60)
263 weight = (double)ip->i_lbs / packing;
264 mobcost = move_cost * weight;
266 can_move = (double)sect.sct_mobil / mobcost;
268 can_move = ship.shp_item[type] - minimum;
269 if (can_move > ship.shp_item[type] - minimum)
270 can_move = ship.shp_item[type] - minimum;
271 if (can_move >= wanted) {
273 ship.shp_item[type] -= wanted;
275 n = roundavg(wanted * weight * move_cost);
278 if (n > sect.sct_mobil)
282 putship(ship.shp_uid, &ship);
286 } else if (can_move > 0) {
290 ship.shp_item[type] -= can_move;
292 n = roundavg(can_move * weight * move_cost);
295 if (n > sect.sct_mobil)
300 putship(ship.shp_uid, &ship);
306 /* look for an owned supply unit */
307 snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
309 while (nxtitem(&ni, &land) && wanted) {
312 if (land.lnd_own != own)
315 lcp = &lchr[(int)land.lnd_type];
316 if (!(lcp->l_flags & L_SUPPLY))
319 if (land.lnd_item[type] <= get_minimum(&land, type))
322 getsect(land.lnd_x, land.lnd_y, §);
323 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
326 if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
329 if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
334 * Recursive supply is disabled for now. It can introduce
335 * cycles into the "resupplies from" relation. The code below
336 * attempts to break these cycles by temporarily zapping the
337 * commodity being supplied. That puts the land file in a
338 * funny state temporarily, risking loss of supplies when
339 * something goes wrong on the way. Worse, it increases
340 * lnd_seqno even when !actually_doit, which can lead to
341 * spurious seqno mismatch oopses in users of
342 * lnd_could_be_supplied(). I can't be bothered to clean up
343 * this mess right now, because recursive resupply is too dumb
344 * to be really useful anyway: each step uses the first source
345 * it finds, without consideration of mobility cost. If you
346 * re-enable it, don't forget to uncomment its documentation
347 * in supply.t as well.
349 if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
353 * Temporarily zap this unit's store, so the recursion
357 land.lnd_item[type] = 0;
358 putland(land.lnd_uid, &land);
359 save.lnd_seqno = land.lnd_seqno;
361 land.lnd_item[type] =
362 save.lnd_item[type] + s_commod(own, land.lnd_x, land.lnd_y,
363 type, wanted, actually_doit);
365 putland(land.lnd_uid, &land);
367 putland(save.lnd_uid, &save);
371 min = get_minimum(&land, type);
374 mobcost = move_cost * weight;
376 can_move = (double)land.lnd_mobil / mobcost;
378 can_move = land.lnd_item[type] - min;
379 if (can_move > land.lnd_item[type] - min)
380 can_move = land.lnd_item[type] - min;
382 if (can_move >= wanted) {
383 land.lnd_item[type] -= wanted;
384 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
387 putland(land.lnd_uid, &land);
389 } else if (can_move > 0) {
392 land.lnd_item[type] -= can_move;
394 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
397 putland(land.lnd_uid, &land);
401 /* We've done the best we could */
402 /* return the number gotten */
408 * We want to get enough shells to fire once,
409 * one update's worth of food.
413 get_minimum(struct lndstr *lp, i_type type)
418 lcp = &lchr[(int)lp->lnd_type];
419 max = lcp->l_item[type];
424 return 0; /* no food reqd, get out */
425 want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
441 lnd_could_be_supplied(struct lndstr *lp)
443 int shells_needed, shells, keepshells;
444 int food, food_needed, keepfood;
447 food_needed = get_minimum(lp, I_FOOD);
448 food = keepfood = lp->lnd_item[I_FOOD];
449 if (food < food_needed) {
450 lp->lnd_item[I_FOOD] = 0;
451 putland(lp->lnd_uid, lp);
452 food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
453 I_FOOD, (food_needed - food));
454 lp->lnd_item[I_FOOD] = keepfood;
455 putland(lp->lnd_uid, lp);
457 if (food < food_needed)
462 shells_needed = lchr[lp->lnd_type].l_ammo;
463 shells = keepshells = lp->lnd_item[I_SHELL];
464 if (shells < shells_needed) {
465 lp->lnd_item[I_SHELL] = 0;
466 putland(lp->lnd_uid, lp);
467 shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
468 I_SHELL, (shells_needed - shells));
469 lp->lnd_item[I_SHELL] = keepshells;
470 putland(lp->lnd_uid, lp);
473 if (shells < shells_needed)