2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2016, 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
43 #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;
119 /* leave at least 1 military in sectors/ships */
122 double move_cost, weight, mobcost;
129 if (wanted <= vec[type])
133 /* try to get it from sector we're in */
134 if (sink->ef_type != EF_SECTOR) {
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;
159 /* look for a headquarters or warehouse */
160 lookrange = tfact(own, 10.0);
161 snxtsct_dist(&ns, x, y, lookrange);
162 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 move_cost = path_find(sect.sct_x, sect.sct_y, x, y, own, MOB_MOVE);
179 if (!opt_NOFOOD && type == I_FOOD)
180 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
182 if (sect.sct_item[type] <= minimum)
185 dp = &dchr[sect.sct_type];
186 packing = ip->i_pkg[dp->d_pkg];
187 if (packing > 1 && sect.sct_effic < 60)
189 weight = (double)ip->i_lbs / packing;
190 mobcost = move_cost * weight;
192 can_move = (double)sect.sct_mobil / mobcost;
194 can_move = sect.sct_item[type] - minimum;
195 if (can_move > sect.sct_item[type] - minimum)
196 can_move = sect.sct_item[type] - minimum;
198 if (can_move >= wanted) {
201 sect.sct_item[type] -= wanted;
203 /* take off mobility for delivering sect */
204 n = roundavg(wanted * weight * move_cost);
205 sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil);
209 put_empobj(sink->ef_type, sink->uid, sink);
212 } else if (can_move > 0) {
215 sect.sct_item[type] -= can_move;
217 /* take off mobility for delivering sect */
218 n = roundavg(can_move * weight * move_cost);
219 sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil);
221 vec[type] += can_move;
227 /* look for an owned ship in a harbor */
228 snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
229 while (nxtitem(&ni, &ship) && wanted) {
230 if (sink->ef_type == EF_SHIP && sink->uid == ship.shp_uid)
232 if (ship.shp_own != own)
234 if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
236 getsect(ship.shp_x, ship.shp_y, §);
237 if (sect.sct_type != SCT_HARBR)
239 if (sect.sct_effic < 2)
241 move_cost = path_find(sect.sct_x, sect.sct_y, x, y, own, MOB_MOVE);
244 if (!opt_NOFOOD && type == I_FOOD)
245 minimum = 1 + (int)ceil(food_needed(ship.shp_item,
247 if (ship.shp_item[type] <= minimum)
250 dp = &dchr[sect.sct_type];
251 packing = ip->i_pkg[dp->d_pkg];
252 if (packing > 1 && sect.sct_effic < 60)
254 weight = (double)ip->i_lbs / packing;
255 mobcost = move_cost * weight;
257 can_move = (double)sect.sct_mobil / mobcost;
259 can_move = ship.shp_item[type] - minimum;
260 if (can_move > ship.shp_item[type] - minimum)
261 can_move = ship.shp_item[type] - minimum;
262 if (can_move >= wanted) {
264 ship.shp_item[type] -= wanted;
266 n = roundavg(wanted * weight * move_cost);
267 sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil);
269 vec[type] += can_move;
270 putship(ship.shp_uid, &ship);
273 put_empobj(sink->ef_type, sink->uid, sink);
276 } else if (can_move > 0) {
279 ship.shp_item[type] -= can_move;
281 n = roundavg(can_move * weight * move_cost);
282 sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil);
284 vec[type] += can_move;
285 putship(ship.shp_uid, &ship);
292 /* look for an owned supply unit */
293 snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
294 while (nxtitem(&ni, &land) && wanted) {
297 if (sink->ef_type == EF_LAND && sink->uid == land.lnd_uid)
299 if (land.lnd_own != own)
302 lcp = &lchr[(int)land.lnd_type];
303 if (!(lcp->l_flags & L_SUPPLY))
306 if (land.lnd_item[type] <= get_minimum(&land, type))
309 if (land.lnd_ship >= 0) {
310 getsect(land.lnd_x, land.lnd_y, §);
311 if (sect.sct_type != SCT_HARBR || sect.sct_effic < 2)
315 move_cost = path_find(land.lnd_x, land.lnd_y, x, y, own, MOB_MOVE);
321 * Recursive supply is disabled for now. It can introduce
322 * cycles into the "resupplies from" relation. The code below
323 * attempts to break these cycles by temporarily zapping the
324 * commodity being supplied. That puts the land file in a
325 * funny state temporarily, risking loss of supplies when
326 * something goes wrong on the way. Worse, it increases
327 * lnd_seqno even when !actually_doit, which can lead to
328 * spurious seqno mismatch oopses in users of
329 * lnd_could_be_supplied(). I can't be bothered to clean up
330 * this mess right now, because recursive resupply is too dumb
331 * to be really useful anyway: each step uses the first source
332 * it finds, without consideration of mobility cost. If you
333 * re-enable it, don't forget to uncomment its documentation
334 * in supply.t as well.
336 if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
340 * Temporarily zap this unit's store, so the recursion
344 land.lnd_item[type] = 0;
345 putland(land.lnd_uid, &land);
346 save.lnd_seqno = land.lnd_seqno;
348 s_commod((struct empobj *)&land, land.lnd_item, type, wanted,
349 lchr[land.lnd_type].l_item[type] - wanted,
351 land.lnd_item[type] += save.lnd_item[type];
354 putland(land.lnd_uid, &land);
356 putland(save.lnd_uid, &save);
360 min = get_minimum(&land, type);
363 mobcost = move_cost * weight;
365 can_move = (double)land.lnd_mobil / mobcost;
367 can_move = land.lnd_item[type] - min;
368 if (can_move > land.lnd_item[type] - min)
369 can_move = land.lnd_item[type] - min;
371 if (can_move >= wanted) {
372 land.lnd_item[type] -= wanted;
373 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
376 putland(land.lnd_uid, &land);
377 put_empobj(sink->ef_type, sink->uid, sink);
380 } else if (can_move > 0) {
382 land.lnd_item[type] -= can_move;
383 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
385 vec[type] += can_move;
386 putland(land.lnd_uid, &land);
392 put_empobj(sink->ef_type, sink->uid, sink);
397 * We want to get enough shells to fire once,
398 * one update's worth of food.
401 get_minimum(struct lndstr *lp, i_type type)
406 lcp = &lchr[(int)lp->lnd_type];
407 max = lcp->l_item[type];
412 return 0; /* no food reqd, get out */
413 want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
429 lnd_could_be_supplied(struct lndstr *lp)
431 int res, food, food_needed, shells_needed, shells;
434 food_needed = get_minimum(lp, I_FOOD);
435 food = lp->lnd_item[I_FOOD];
436 if (food < food_needed) {
437 res = s_commod((struct empobj *)lp, lp->lnd_item,
439 lchr[lp->lnd_type].l_item[I_FOOD], 0);
440 lp->lnd_item[I_FOOD] = food;
446 shells_needed = lchr[lp->lnd_type].l_ammo;
447 shells = lp->lnd_item[I_SHELL];
448 if (shells < shells_needed) {
449 res = s_commod((struct empobj *)lp, lp->lnd_item,
450 I_SHELL, shells_needed,
451 lchr[lp->lnd_type].l_item[I_SHELL], 0);
452 lp->lnd_item[I_SHELL] = shells;