2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2017, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * supply.c: Supply subroutines
29 * Known contributors to this file:
30 * Markus Armbruster, 2004-2013
42 #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;
118 /* leave at least 1 military in sectors/ships */
121 double move_cost, weight, mobcost;
128 if (wanted <= vec[type])
132 /* try to get it from sector we're in */
133 if (sink->ef_type != EF_SECTOR) {
134 getsect(x, y, §);
135 if (sect.sct_own == own) {
136 if (!opt_NOFOOD && type == I_FOOD)
137 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
139 if (sect.sct_item[type] - wanted >= minimum) {
140 sect.sct_item[type] -= wanted;
144 put_empobj(sink->ef_type, sink->uid, sink);
147 } else if (sect.sct_item[type] - minimum > 0) {
148 wanted -= sect.sct_item[type] - minimum;
149 sect.sct_item[type] = minimum;
151 vec[type] += sect.sct_item[type] - minimum;
158 /* look for a headquarters or warehouse */
159 lookrange = tfact(own, 10.0);
160 snxtsct_dist(&ns, x, y, lookrange);
161 while (nxtsct(&ns, §) && wanted) {
164 if (sect.sct_own != own)
166 if ((sect.sct_type != SCT_WAREH) &&
167 (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
169 if ((sect.sct_type == SCT_HEADQ) &&
170 (sect.sct_dist_x == sect.sct_x) &&
171 (sect.sct_dist_y == sect.sct_y))
173 if (sect.sct_effic < 60)
175 move_cost = path_find(sect.sct_x, sect.sct_y, x, y, own, 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)
184 dp = &dchr[sect.sct_type];
185 packing = ip->i_pkg[dp->d_pkg];
186 if (packing > 1 && sect.sct_effic < 60)
188 weight = (double)ip->i_lbs / packing;
189 mobcost = move_cost * weight;
191 can_move = (double)sect.sct_mobil / mobcost;
193 can_move = sect.sct_item[type] - minimum;
194 if (can_move > sect.sct_item[type] - minimum)
195 can_move = sect.sct_item[type] - minimum;
197 if (can_move >= wanted) {
200 sect.sct_item[type] -= wanted;
202 /* take off mobility for delivering sect */
203 n = roundavg(wanted * weight * move_cost);
204 sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil);
208 put_empobj(sink->ef_type, sink->uid, sink);
211 } else if (can_move > 0) {
214 sect.sct_item[type] -= can_move;
216 /* take off mobility for delivering sect */
217 n = roundavg(can_move * weight * move_cost);
218 sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil);
220 vec[type] += can_move;
226 /* look for an owned ship in a harbor */
227 snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
228 while (nxtitem(&ni, &ship) && wanted) {
229 if (sink->ef_type == EF_SHIP && sink->uid == ship.shp_uid)
231 if (ship.shp_own != own)
233 if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
235 getsect(ship.shp_x, ship.shp_y, §);
236 if (sect.sct_type != SCT_HARBR)
238 if (sect.sct_effic < 2)
240 move_cost = path_find(sect.sct_x, sect.sct_y, x, y, own, MOB_MOVE);
243 if (!opt_NOFOOD && type == I_FOOD)
244 minimum = 1 + (int)ceil(food_needed(ship.shp_item,
246 if (ship.shp_item[type] <= minimum)
249 dp = &dchr[sect.sct_type];
250 packing = ip->i_pkg[dp->d_pkg];
251 if (packing > 1 && sect.sct_effic < 60)
253 weight = (double)ip->i_lbs / packing;
254 mobcost = move_cost * weight;
256 can_move = (double)sect.sct_mobil / mobcost;
258 can_move = ship.shp_item[type] - minimum;
259 if (can_move > ship.shp_item[type] - minimum)
260 can_move = ship.shp_item[type] - minimum;
261 if (can_move >= wanted) {
263 ship.shp_item[type] -= wanted;
265 n = roundavg(wanted * weight * move_cost);
266 sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil);
268 vec[type] += can_move;
269 putship(ship.shp_uid, &ship);
272 put_empobj(sink->ef_type, sink->uid, sink);
275 } else if (can_move > 0) {
278 ship.shp_item[type] -= can_move;
280 n = roundavg(can_move * weight * move_cost);
281 sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil);
283 vec[type] += can_move;
284 putship(ship.shp_uid, &ship);
291 /* look for an owned supply unit */
292 snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
293 while (nxtitem(&ni, &land) && wanted) {
296 if (sink->ef_type == EF_LAND && sink->uid == land.lnd_uid)
298 if (land.lnd_own != own)
301 lcp = &lchr[(int)land.lnd_type];
302 if (!(lcp->l_flags & L_SUPPLY))
305 if (land.lnd_item[type] <= get_minimum(&land, type))
308 if (land.lnd_ship >= 0) {
309 getsect(land.lnd_x, land.lnd_y, §);
310 if (sect.sct_type != SCT_HARBR || sect.sct_effic < 2)
314 move_cost = path_find(land.lnd_x, land.lnd_y, x, y, own, MOB_MOVE);
320 * Recursive supply is disabled for now. It can introduce
321 * cycles into the "resupplies from" relation. The code below
322 * attempts to break these cycles by temporarily zapping the
323 * commodity being supplied. That puts the land file in a
324 * funny state temporarily, risking loss of supplies when
325 * something goes wrong on the way. Worse, it increases
326 * lnd_seqno even when !actually_doit, which can lead to
327 * spurious seqno mismatch oopses in users of
328 * lnd_could_be_supplied(). I can't be bothered to clean up
329 * this mess right now, because recursive resupply is too dumb
330 * to be really useful anyway: each step uses the first source
331 * it finds, without consideration of mobility cost. If you
332 * re-enable it, don't forget to uncomment its documentation
333 * in supply.t as well.
335 if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
339 * Temporarily zap this unit's store, so the recursion
343 land.lnd_item[type] = 0;
344 putland(land.lnd_uid, &land);
345 save.lnd_seqno = land.lnd_seqno;
347 s_commod((struct empobj *)&land, land.lnd_item, type, wanted,
348 lchr[land.lnd_type].l_item[type] - wanted,
350 land.lnd_item[type] += save.lnd_item[type];
353 putland(land.lnd_uid, &land);
355 putland(save.lnd_uid, &save);
359 min = get_minimum(&land, type);
362 mobcost = move_cost * weight;
364 can_move = (double)land.lnd_mobil / mobcost;
366 can_move = land.lnd_item[type] - min;
367 if (can_move > land.lnd_item[type] - min)
368 can_move = land.lnd_item[type] - min;
370 if (can_move >= wanted) {
371 land.lnd_item[type] -= wanted;
372 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
375 putland(land.lnd_uid, &land);
376 put_empobj(sink->ef_type, sink->uid, sink);
379 } else if (can_move > 0) {
381 land.lnd_item[type] -= can_move;
382 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
384 vec[type] += can_move;
385 putland(land.lnd_uid, &land);
391 put_empobj(sink->ef_type, sink->uid, sink);
396 * We want to get enough shells to fire once,
397 * one update's worth of food.
400 get_minimum(struct lndstr *lp, i_type type)
405 lcp = &lchr[(int)lp->lnd_type];
406 max = lcp->l_item[type];
411 return 0; /* no food reqd, get out */
412 want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
428 lnd_could_be_supplied(struct lndstr *lp)
430 int res, food, food_needed, shells_needed, shells;
433 food_needed = get_minimum(lp, I_FOOD);
434 food = lp->lnd_item[I_FOOD];
435 if (food < food_needed) {
436 res = s_commod((struct empobj *)lp, lp->lnd_item,
438 lchr[lp->lnd_type].l_item[I_FOOD], 0);
439 lp->lnd_item[I_FOOD] = food;
445 shells_needed = lchr[lp->lnd_type].l_ammo;
446 shells = lp->lnd_item[I_SHELL];
447 if (shells < shells_needed) {
448 res = s_commod((struct empobj *)lp, lp->lnd_item,
449 I_SHELL, shells_needed,
450 lchr[lp->lnd_type].l_item[I_SHELL], 0);
451 lp->lnd_item[I_SHELL] = shells;