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
43 #include "prototypes.h"
47 static int s_commod(struct empobj *, short *, i_type, int, int, int);
48 static int get_minimum(struct lndstr *, i_type);
51 sct_supply(struct sctstr *sp, i_type type, int wanted)
53 return s_commod((struct empobj *)sp, sp->sct_item,
54 type, wanted, ITEM_MAX, 1);
58 shp_supply(struct shpstr *sp, i_type type, int wanted)
60 return s_commod((struct empobj *)sp, sp->shp_item,
61 type, wanted, mchr[sp->shp_type].m_item[type], 1);
65 lnd_supply(struct lndstr *lp, i_type type, int wanted)
67 return s_commod((struct empobj *)lp, lp->lnd_item,
68 type, wanted, lchr[lp->lnd_type].l_item[type], 1);
72 lnd_in_supply(struct lndstr *lp)
75 if (lp->lnd_item[I_FOOD] < get_minimum(lp, I_FOOD))
78 return lp->lnd_item[I_SHELL] >= get_minimum(lp, I_SHELL);
82 lnd_supply_all(struct lndstr *lp)
87 fail |= !lnd_supply(lp, I_FOOD, get_minimum(lp, I_FOOD));
88 fail |= !lnd_supply(lp, I_SHELL, get_minimum(lp, I_SHELL));
93 * Actually get the commod
95 * First, try to forage in the sector
96 * Second look for a warehouse or headquarters to leech
97 * Third, look for a ship we own in a harbor
98 * Fourth, look for supplies in a supply unit we own
99 * (one good reason to do this last is that the supply
100 * unit will then call resupply, taking more time)
102 * May want to put code to resupply with SAMs here, later --ts
105 s_commod(struct empobj *sink, short *vec,
106 i_type type, int wanted, int limit, int actually_doit)
108 natid own = sink->own;
112 struct sctstr sect, dest;
118 /* leave at least 1 military in sectors/ships */
121 double move_cost, weight, mobcost;
129 if (wanted <= vec[type])
133 /* try to get it from sector we're in */
134 getsect(x, y, &dest);
135 getsect(x, y, §);
136 if (sect.sct_own == own) {
137 if (!opt_NOFOOD && type == I_FOOD)
138 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
140 if (sect.sct_item[type] - wanted >= minimum) {
141 sect.sct_item[type] -= wanted;
145 put_empobj(sink->ef_type, sink->uid, sink);
148 } else if (sect.sct_item[type] - minimum > 0) {
149 wanted -= sect.sct_item[type] - minimum;
150 sect.sct_item[type] = minimum;
152 vec[type] += sect.sct_item[type] - minimum;
157 /* look for a headquarters or warehouse */
158 lookrange = tfact(own, 10.0);
159 snxtsct_dist(&ns, x, y, lookrange);
160 while (nxtsct(&ns, §) && wanted) {
161 if (sect.sct_own != own)
163 if ((sect.sct_type != SCT_WAREH) &&
164 (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
166 if ((sect.sct_type == SCT_HEADQ) &&
167 (sect.sct_dist_x == sect.sct_x) &&
168 (sect.sct_dist_y == sect.sct_y))
170 if (sect.sct_effic < 60)
172 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
174 if (!opt_NOFOOD && type == I_FOOD)
175 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
177 if (sect.sct_item[type] <= minimum) {
178 /* Don't bother... */
182 dp = &dchr[sect.sct_type];
183 packing = ip->i_pkg[dp->d_pkg];
184 if (packing > 1 && sect.sct_effic < 60)
186 weight = (double)ip->i_lbs / packing;
187 mobcost = move_cost * weight;
189 can_move = (double)sect.sct_mobil / mobcost;
191 can_move = sect.sct_item[type] - minimum;
192 if (can_move > sect.sct_item[type] - minimum)
193 can_move = sect.sct_item[type] - minimum;
195 if (can_move >= wanted) {
198 sect.sct_item[type] -= wanted;
200 /* take off mobility for delivering sect */
201 n = roundavg(wanted * weight * move_cost);
204 if (n > sect.sct_mobil)
210 put_empobj(sink->ef_type, sink->uid, sink);
213 } else if (can_move > 0) {
216 sect.sct_item[type] -= can_move;
218 /* take off mobility for delivering sect */
219 n = roundavg(can_move * weight * move_cost);
222 if (n > sect.sct_mobil)
226 vec[type] += can_move;
232 /* look for an owned ship in a harbor */
233 snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
235 while (nxtitem(&ni, &ship) && wanted) {
236 if (ship.shp_own != own)
239 if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
241 getsect(ship.shp_x, ship.shp_y, §);
242 if (sect.sct_type != SCT_HARBR)
244 if (sect.sct_effic < 2)
246 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
248 if (!opt_NOFOOD && type == I_FOOD)
249 minimum = 1 + (int)ceil(food_needed(ship.shp_item,
251 if (ship.shp_item[type] <= minimum) {
252 /* Don't bother... */
256 dp = &dchr[sect.sct_type];
257 packing = ip->i_pkg[dp->d_pkg];
258 if (packing > 1 && sect.sct_effic < 60)
260 weight = (double)ip->i_lbs / packing;
261 mobcost = move_cost * weight;
263 can_move = (double)sect.sct_mobil / mobcost;
265 can_move = ship.shp_item[type] - minimum;
266 if (can_move > ship.shp_item[type] - minimum)
267 can_move = ship.shp_item[type] - minimum;
268 if (can_move >= wanted) {
270 ship.shp_item[type] -= wanted;
272 n = roundavg(wanted * weight * move_cost);
275 if (n > sect.sct_mobil)
279 vec[type] += can_move;
280 putship(ship.shp_uid, &ship);
282 put_empobj(sink->ef_type, sink->uid, sink);
285 } else if (can_move > 0) {
288 ship.shp_item[type] -= can_move;
290 n = roundavg(can_move * weight * move_cost);
293 if (n > sect.sct_mobil)
297 vec[type] += can_move;
298 putship(ship.shp_uid, &ship);
304 /* look for an owned supply unit */
305 snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
307 while (nxtitem(&ni, &land) && wanted) {
310 if (land.lnd_own != own)
313 lcp = &lchr[(int)land.lnd_type];
314 if (!(lcp->l_flags & L_SUPPLY))
317 if (land.lnd_item[type] <= get_minimum(&land, type))
320 getsect(land.lnd_x, land.lnd_y, §);
321 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
324 if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
327 if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
332 * Recursive supply is disabled for now. It can introduce
333 * cycles into the "resupplies from" relation. The code below
334 * attempts to break these cycles by temporarily zapping the
335 * commodity being supplied. That puts the land file in a
336 * funny state temporarily, risking loss of supplies when
337 * something goes wrong on the way. Worse, it increases
338 * lnd_seqno even when !actually_doit, which can lead to
339 * spurious seqno mismatch oopses in users of
340 * lnd_could_be_supplied(). I can't be bothered to clean up
341 * this mess right now, because recursive resupply is too dumb
342 * to be really useful anyway: each step uses the first source
343 * it finds, without consideration of mobility cost. If you
344 * re-enable it, don't forget to uncomment its documentation
345 * in supply.t as well.
347 if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
351 * Temporarily zap this unit's store, so the recursion
355 land.lnd_item[type] = 0;
356 putland(land.lnd_uid, &land);
357 save.lnd_seqno = land.lnd_seqno;
359 land.lnd_item[type] =
360 save.lnd_item[type] + s_commod(own, land.lnd_x, land.lnd_y,
361 type, wanted, actually_doit);
363 putland(land.lnd_uid, &land);
365 putland(save.lnd_uid, &save);
369 min = get_minimum(&land, type);
372 mobcost = move_cost * weight;
374 can_move = (double)land.lnd_mobil / mobcost;
376 can_move = land.lnd_item[type] - min;
377 if (can_move > land.lnd_item[type] - min)
378 can_move = land.lnd_item[type] - min;
380 if (can_move >= wanted) {
381 land.lnd_item[type] -= wanted;
382 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
385 putland(land.lnd_uid, &land);
386 put_empobj(sink->ef_type, sink->uid, sink);
389 } else if (can_move > 0) {
391 land.lnd_item[type] -= can_move;
392 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
394 vec[type] += can_move;
395 putland(land.lnd_uid, &land);
401 put_empobj(sink->ef_type, sink->uid, sink);
402 return wanted <= vec[type];
406 * We want to get enough shells to fire once,
407 * one update's worth of food.
410 get_minimum(struct lndstr *lp, i_type type)
415 lcp = &lchr[(int)lp->lnd_type];
416 max = lcp->l_item[type];
421 return 0; /* no food reqd, get out */
422 want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
438 lnd_could_be_supplied(struct lndstr *lp)
440 int res, food, food_needed, shells_needed, shells;
443 food_needed = get_minimum(lp, I_FOOD);
444 food = lp->lnd_item[I_FOOD];
445 if (food < food_needed) {
446 lp->lnd_item[I_FOOD] = 0;
447 putland(lp->lnd_uid, lp);
448 res = s_commod((struct empobj *)lp, lp->lnd_item,
450 lchr[lp->lnd_type].l_item[I_FOOD], 0);
451 lp->lnd_item[I_FOOD] = food;
452 putland(lp->lnd_uid, lp);
458 shells_needed = lchr[lp->lnd_type].l_ammo;
459 shells = lp->lnd_item[I_SHELL];
460 if (shells < shells_needed) {
461 lp->lnd_item[I_SHELL] = 0;
462 putland(lp->lnd_uid, lp);
463 res = s_commod((struct empobj *)lp, lp->lnd_item,
464 I_SHELL, shells_needed,
465 lchr[lp->lnd_type].l_item[I_SHELL], 0);
466 lp->lnd_item[I_SHELL] = shells;
467 putland(lp->lnd_uid, lp);