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, 2004-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 getsect(x, y, &dest);
135 /* try to get it from sector we're in */
136 if (sink->ef_type != EF_SECTOR) {
137 getsect(x, y, §);
138 if (sect.sct_own == own) {
139 if (!opt_NOFOOD && type == I_FOOD)
140 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
142 if (sect.sct_item[type] - wanted >= minimum) {
143 sect.sct_item[type] -= wanted;
147 put_empobj(sink->ef_type, sink->uid, sink);
150 } else if (sect.sct_item[type] - minimum > 0) {
151 wanted -= sect.sct_item[type] - minimum;
152 sect.sct_item[type] = minimum;
154 vec[type] += 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) {
167 if (sect.sct_own != own)
169 if ((sect.sct_type != SCT_WAREH) &&
170 (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
172 if ((sect.sct_type == SCT_HEADQ) &&
173 (sect.sct_dist_x == sect.sct_x) &&
174 (sect.sct_dist_y == sect.sct_y))
176 if (sect.sct_effic < 60)
178 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
180 if (!opt_NOFOOD && type == I_FOOD)
181 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
183 if (sect.sct_item[type] <= minimum)
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)
214 put_empobj(sink->ef_type, sink->uid, sink);
217 } 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)
230 vec[type] += can_move;
236 /* look for an owned ship in a harbor */
237 snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
238 while (nxtitem(&ni, &ship) && wanted) {
239 if (sink->ef_type == EF_SHIP && sink->uid == ship.shp_uid)
241 if (ship.shp_own != own)
243 if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
245 getsect(ship.shp_x, ship.shp_y, §);
246 if (sect.sct_type != SCT_HARBR)
248 if (sect.sct_effic < 2)
250 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
252 if (!opt_NOFOOD && type == I_FOOD)
253 minimum = 1 + (int)ceil(food_needed(ship.shp_item,
255 if (ship.shp_item[type] <= minimum)
258 dp = &dchr[sect.sct_type];
259 packing = ip->i_pkg[dp->d_pkg];
260 if (packing > 1 && sect.sct_effic < 60)
262 weight = (double)ip->i_lbs / packing;
263 mobcost = move_cost * weight;
265 can_move = (double)sect.sct_mobil / mobcost;
267 can_move = ship.shp_item[type] - minimum;
268 if (can_move > ship.shp_item[type] - minimum)
269 can_move = ship.shp_item[type] - minimum;
270 if (can_move >= wanted) {
272 ship.shp_item[type] -= wanted;
274 n = roundavg(wanted * weight * move_cost);
277 if (n > sect.sct_mobil)
281 vec[type] += can_move;
282 putship(ship.shp_uid, &ship);
284 put_empobj(sink->ef_type, sink->uid, sink);
287 } 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)
299 vec[type] += can_move;
300 putship(ship.shp_uid, &ship);
306 /* look for an owned supply unit */
307 snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
308 while (nxtitem(&ni, &land) && wanted) {
311 if (sink->ef_type == EF_LAND && sink->uid == land.lnd_uid)
313 if (land.lnd_own != own)
316 lcp = &lchr[(int)land.lnd_type];
317 if (!(lcp->l_flags & L_SUPPLY))
320 if (land.lnd_item[type] <= get_minimum(&land, type))
323 getsect(land.lnd_x, land.lnd_y, §);
324 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
327 if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
330 if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
335 * Recursive supply is disabled for now. It can introduce
336 * cycles into the "resupplies from" relation. The code below
337 * attempts to break these cycles by temporarily zapping the
338 * commodity being supplied. That puts the land file in a
339 * funny state temporarily, risking loss of supplies when
340 * something goes wrong on the way. Worse, it increases
341 * lnd_seqno even when !actually_doit, which can lead to
342 * spurious seqno mismatch oopses in users of
343 * lnd_could_be_supplied(). I can't be bothered to clean up
344 * this mess right now, because recursive resupply is too dumb
345 * to be really useful anyway: each step uses the first source
346 * it finds, without consideration of mobility cost. If you
347 * re-enable it, don't forget to uncomment its documentation
348 * in supply.t as well.
350 if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
354 * Temporarily zap this unit's store, so the recursion
358 land.lnd_item[type] = 0;
359 putland(land.lnd_uid, &land);
360 save.lnd_seqno = land.lnd_seqno;
362 land.lnd_item[type] =
363 save.lnd_item[type] + s_commod(own, land.lnd_x, land.lnd_y,
364 type, wanted, actually_doit);
366 putland(land.lnd_uid, &land);
368 putland(save.lnd_uid, &save);
372 min = get_minimum(&land, type);
375 mobcost = move_cost * weight;
377 can_move = (double)land.lnd_mobil / mobcost;
379 can_move = land.lnd_item[type] - min;
380 if (can_move > land.lnd_item[type] - min)
381 can_move = land.lnd_item[type] - min;
383 if (can_move >= wanted) {
384 land.lnd_item[type] -= wanted;
385 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
388 putland(land.lnd_uid, &land);
389 put_empobj(sink->ef_type, sink->uid, sink);
392 } else if (can_move > 0) {
394 land.lnd_item[type] -= can_move;
395 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
397 vec[type] += can_move;
398 putland(land.lnd_uid, &land);
404 put_empobj(sink->ef_type, sink->uid, sink);
409 * We want to get enough shells to fire once,
410 * 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 res, food, food_needed, shells_needed, shells;
446 food_needed = get_minimum(lp, I_FOOD);
447 food = lp->lnd_item[I_FOOD];
448 if (food < food_needed) {
449 res = s_commod((struct empobj *)lp, lp->lnd_item,
451 lchr[lp->lnd_type].l_item[I_FOOD], 0);
452 lp->lnd_item[I_FOOD] = food;
458 shells_needed = lchr[lp->lnd_type].l_ammo;
459 shells = lp->lnd_item[I_SHELL];
460 if (shells < shells_needed) {
461 res = s_commod((struct empobj *)lp, lp->lnd_item,
462 I_SHELL, shells_needed,
463 lchr[lp->lnd_type].l_item[I_SHELL], 0);
464 lp->lnd_item[I_SHELL] = shells;