2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, 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
44 #include "prototypes.h"
48 static int s_commod(struct empobj *, short *, i_type, int, int, int);
49 static int get_minimum(struct lndstr *, i_type);
52 sct_supply(struct sctstr *sp, i_type type, int wanted)
54 return s_commod((struct empobj *)sp, sp->sct_item,
55 type, wanted, ITEM_MAX, 1);
59 shp_supply(struct shpstr *sp, i_type type, int wanted)
61 return s_commod((struct empobj *)sp, sp->shp_item,
62 type, wanted, mchr[sp->shp_type].m_item[type], 1);
66 lnd_supply(struct lndstr *lp, i_type type, int wanted)
68 return s_commod((struct empobj *)lp, lp->lnd_item,
69 type, wanted, lchr[lp->lnd_type].l_item[type], 1);
73 lnd_in_supply(struct lndstr *lp)
76 if (lp->lnd_item[I_FOOD] < get_minimum(lp, I_FOOD))
79 return lp->lnd_item[I_SHELL] >= get_minimum(lp, I_SHELL);
83 lnd_supply_all(struct lndstr *lp)
88 fail |= !lnd_supply(lp, I_FOOD, get_minimum(lp, I_FOOD));
89 fail |= !lnd_supply(lp, I_SHELL, get_minimum(lp, I_SHELL));
94 * Actually get the commod
96 * First, try to forage in the sector
97 * Second look for a warehouse or headquarters to leech
98 * Third, look for a ship we own in a harbor
99 * Fourth, look for supplies in a supply unit we own
100 * (one good reason to do this last is that the supply
101 * unit will then call resupply, taking more time)
103 * May want to put code to resupply with SAMs here, later --ts
106 s_commod(struct empobj *sink, short *vec,
107 i_type type, int wanted, int limit, int actually_doit)
109 natid own = sink->own;
113 struct sctstr sect, dest;
119 /* leave at least 1 military in sectors/ships */
122 double move_cost, weight, mobcost;
130 if (wanted <= vec[type])
134 getsect(x, y, &dest);
136 /* try to get it from sector we're in */
137 if (sink->ef_type != EF_SECTOR) {
138 getsect(x, y, §);
139 if (sect.sct_own == own) {
140 if (!opt_NOFOOD && type == I_FOOD)
141 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
143 if (sect.sct_item[type] - wanted >= minimum) {
144 sect.sct_item[type] -= wanted;
148 put_empobj(sink->ef_type, sink->uid, sink);
151 } else if (sect.sct_item[type] - minimum > 0) {
152 wanted -= sect.sct_item[type] - minimum;
153 sect.sct_item[type] = minimum;
155 vec[type] += sect.sct_item[type] - minimum;
162 /* look for a headquarters or warehouse */
163 lookrange = tfact(own, 10.0);
164 snxtsct_dist(&ns, x, y, lookrange);
165 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)
187 dp = &dchr[sect.sct_type];
188 packing = ip->i_pkg[dp->d_pkg];
189 if (packing > 1 && sect.sct_effic < 60)
191 weight = (double)ip->i_lbs / packing;
192 mobcost = move_cost * weight;
194 can_move = (double)sect.sct_mobil / mobcost;
196 can_move = sect.sct_item[type] - minimum;
197 if (can_move > sect.sct_item[type] - minimum)
198 can_move = sect.sct_item[type] - minimum;
200 if (can_move >= wanted) {
203 sect.sct_item[type] -= wanted;
205 /* take off mobility for delivering sect */
206 n = roundavg(wanted * weight * move_cost);
209 if (n > sect.sct_mobil)
215 put_empobj(sink->ef_type, sink->uid, sink);
218 } else if (can_move > 0) {
221 sect.sct_item[type] -= can_move;
223 /* take off mobility for delivering sect */
224 n = roundavg(can_move * weight * move_cost);
227 if (n > sect.sct_mobil)
231 vec[type] += can_move;
237 /* look for an owned ship in a harbor */
238 snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
239 while (nxtitem(&ni, &ship) && wanted) {
240 if (sink->ef_type == EF_SHIP && sink->uid == ship.shp_uid)
242 if (ship.shp_own != own)
244 if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
246 getsect(ship.shp_x, ship.shp_y, §);
247 if (sect.sct_type != SCT_HARBR)
249 if (sect.sct_effic < 2)
251 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
253 if (!opt_NOFOOD && type == I_FOOD)
254 minimum = 1 + (int)ceil(food_needed(ship.shp_item,
256 if (ship.shp_item[type] <= minimum)
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 vec[type] += can_move;
283 putship(ship.shp_uid, &ship);
285 put_empobj(sink->ef_type, sink->uid, sink);
288 } else if (can_move > 0) {
291 ship.shp_item[type] -= can_move;
293 n = roundavg(can_move * weight * move_cost);
296 if (n > sect.sct_mobil)
300 vec[type] += can_move;
301 putship(ship.shp_uid, &ship);
307 /* look for an owned supply unit */
308 snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
309 while (nxtitem(&ni, &land) && wanted) {
312 if (sink->ef_type == EF_LAND && sink->uid == land.lnd_uid)
314 if (land.lnd_own != own)
317 lcp = &lchr[(int)land.lnd_type];
318 if (!(lcp->l_flags & L_SUPPLY))
321 if (land.lnd_item[type] <= get_minimum(&land, type))
324 getsect(land.lnd_x, land.lnd_y, §);
325 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
328 if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
331 if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
336 * Recursive supply is disabled for now. It can introduce
337 * cycles into the "resupplies from" relation. The code below
338 * attempts to break these cycles by temporarily zapping the
339 * commodity being supplied. That puts the land file in a
340 * funny state temporarily, risking loss of supplies when
341 * something goes wrong on the way. Worse, it increases
342 * lnd_seqno even when !actually_doit, which can lead to
343 * spurious seqno mismatch oopses in users of
344 * lnd_could_be_supplied(). I can't be bothered to clean up
345 * this mess right now, because recursive resupply is too dumb
346 * to be really useful anyway: each step uses the first source
347 * it finds, without consideration of mobility cost. If you
348 * re-enable it, don't forget to uncomment its documentation
349 * in supply.t as well.
351 if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
355 * Temporarily zap this unit's store, so the recursion
359 land.lnd_item[type] = 0;
360 putland(land.lnd_uid, &land);
361 save.lnd_seqno = land.lnd_seqno;
363 s_commod((struct empobj *)&land, land.lnd_item, type, wanted,
364 lchr[land.lnd_type].l_item[type] - wanted,
366 land.lnd_item[type] += save.lnd_item[type];
369 putland(land.lnd_uid, &land);
371 putland(save.lnd_uid, &save);
375 min = get_minimum(&land, type);
378 mobcost = move_cost * weight;
380 can_move = (double)land.lnd_mobil / mobcost;
382 can_move = land.lnd_item[type] - min;
383 if (can_move > land.lnd_item[type] - min)
384 can_move = land.lnd_item[type] - min;
386 if (can_move >= wanted) {
387 land.lnd_item[type] -= wanted;
388 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
391 putland(land.lnd_uid, &land);
392 put_empobj(sink->ef_type, sink->uid, sink);
395 } else if (can_move > 0) {
397 land.lnd_item[type] -= can_move;
398 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
400 vec[type] += can_move;
401 putland(land.lnd_uid, &land);
407 put_empobj(sink->ef_type, sink->uid, sink);
412 * We want to get enough shells to fire once,
413 * 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 res, food, food_needed, shells_needed, shells;
449 food_needed = get_minimum(lp, I_FOOD);
450 food = lp->lnd_item[I_FOOD];
451 if (food < food_needed) {
452 res = s_commod((struct empobj *)lp, lp->lnd_item,
454 lchr[lp->lnd_type].l_item[I_FOOD], 0);
455 lp->lnd_item[I_FOOD] = food;
461 shells_needed = lchr[lp->lnd_type].l_ammo;
462 shells = lp->lnd_item[I_SHELL];
463 if (shells < shells_needed) {
464 res = s_commod((struct empobj *)lp, lp->lnd_item,
465 I_SHELL, shells_needed,
466 lchr[lp->lnd_type].l_item[I_SHELL], 0);
467 lp->lnd_item[I_SHELL] = shells;