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:
42 #include "prototypes.h"
46 static int get_minimum(struct lndstr *, i_type);
47 static int s_commod(int, int, int, i_type, int, int);
50 * We want to get enough guns to be maxed out, enough shells to
51 * fire once, one update's worth of food.
53 * Firts, try to forage in the sector
54 * Second look for a warehouse or headquarters to leech
55 * Third, look for a ship we own in a harbor
56 * Fourth, look for supplies in a supply unit we own
57 * (one good reason to do this last is that the supply
58 * unit will then call resupply, taking more time)
60 * May want to put code to resupply with SAMs here, later --ts
64 resupply_all(struct lndstr *lp)
67 resupply_commod(lp, I_FOOD);
68 resupply_commod(lp, I_SHELL);
72 * If the unit has less than it's minimum level of a
73 * certain commodity, fill it, to the best of our abilities.
77 resupply_commod(struct lndstr *lp, i_type type)
81 amt = get_minimum(lp, type) - lp->lnd_item[type];
83 lp->lnd_item[type] += supply_commod(lp->lnd_own,
90 lnd_in_supply(struct lndstr *lp)
93 if (lp->lnd_item[I_FOOD] < get_minimum(lp, I_FOOD))
96 return lp->lnd_item[I_SHELL] >= get_minimum(lp, I_SHELL);
100 * Actually get the commod
103 supply_commod(int own, int x, int y, i_type type, int total_wanted)
105 if (total_wanted <= 0)
107 return s_commod(own, x, y, type, total_wanted, !player->simulation);
111 * Just return the number you COULD get, without doing it
114 try_supply_commod(int own, int x, int y, i_type type, int total_wanted)
116 if (total_wanted <= 0)
119 return s_commod(own, x, y, type, total_wanted, 0);
122 /* Get supplies of a certain type */
124 s_commod(int own, int x, int y, i_type type, int total_wanted,
127 int wanted = total_wanted;
128 int gotten = 0, lookrange;
129 struct sctstr sect, dest;
135 /* leave at least 1 military in sectors/ships */
136 int minimum = (type == I_MILIT ? 1 : 0);
138 double move_cost, weight, mobcost;
144 /* try to get it from sector we're in */
145 getsect(x, y, &dest);
146 getsect(x, y, §);
147 if (sect.sct_own == own) {
148 if (sect.sct_item[type] - wanted >= minimum) {
149 sect.sct_item[type] -= wanted;
153 } else if (sect.sct_item[type] - minimum > 0) {
154 gotten += sect.sct_item[type] - minimum;
155 wanted -= sect.sct_item[type] - minimum;
156 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) {
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 if (!BestLandPath(buf, &dest, §, &move_cost, 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) {
182 /* Don't bother... */
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(total_wanted * weight * move_cost);
208 if (n > sect.sct_mobil)
216 } 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)
235 /* look for an owned ship in a harbor */
236 snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
238 while (nxtitem(&ni, &ship) && wanted) {
239 if (ship.shp_own != own)
242 if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
244 getsect(ship.shp_x, ship.shp_y, §);
245 if (sect.sct_type != SCT_HARBR)
247 if (sect.sct_effic < 2)
249 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
251 if (!opt_NOFOOD && type == I_FOOD)
252 minimum = 1 + (int)ceil(food_needed(ship.shp_item,
254 if (ship.shp_item[type] <= minimum) {
255 /* Don't bother... */
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 putship(ship.shp_uid, &ship);
286 } 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)
300 putship(ship.shp_uid, &ship);
306 /* look for an owned supply unit */
307 snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
309 while (nxtitem(&ni, &land) && wanted) {
312 if (land.lnd_own != own)
315 lcp = &lchr[(int)land.lnd_type];
316 if (!(lcp->l_flags & L_SUPPLY))
319 if (land.lnd_item[type] <= get_minimum(&land, type))
322 getsect(land.lnd_x, land.lnd_y, §);
323 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
326 if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
329 if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
332 if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
336 * Temporarily zap this unit's store, so the recursion
340 land.lnd_item[type] = 0;
341 putland(land.lnd_uid, &land);
342 save.lnd_seqno = land.lnd_seqno;
344 land.lnd_item[type] =
345 save.lnd_item[type] + s_commod(own, land.lnd_x, land.lnd_y,
346 type, wanted, actually_doit);
348 putland(land.lnd_uid, &land);
350 putland(save.lnd_uid, &save);
353 min = get_minimum(&land, type);
356 mobcost = move_cost * weight;
358 can_move = (double)land.lnd_mobil / mobcost;
360 can_move = land.lnd_item[type] - min;
361 if (can_move > land.lnd_item[type] - min)
362 can_move = land.lnd_item[type] - min;
364 if (can_move >= wanted) {
365 land.lnd_item[type] -= wanted;
366 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
369 putland(land.lnd_uid, &land);
371 } else if (can_move > 0) {
374 land.lnd_item[type] -= can_move;
376 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
379 putland(land.lnd_uid, &land);
383 /* We've done the best we could */
384 /* return the number gotten */
390 * We want to get enough shells to fire once,
391 * one update's worth of food.
395 get_minimum(struct lndstr *lp, i_type type)
400 lcp = &lchr[(int)lp->lnd_type];
401 max = lcp->l_item[type];
406 return 0; /* no food reqd, get out */
407 want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
423 lnd_could_be_supplied(struct lndstr *lp)
425 int shells_needed, shells, keepshells;
426 int food, food_needed, keepfood;
429 food_needed = get_minimum(lp, I_FOOD);
430 food = keepfood = lp->lnd_item[I_FOOD];
431 if (food < food_needed) {
432 lp->lnd_item[I_FOOD] = 0;
433 putland(lp->lnd_uid, lp);
434 food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
435 I_FOOD, (food_needed - food));
436 lp->lnd_item[I_FOOD] = keepfood;
437 putland(lp->lnd_uid, lp);
439 if (food < food_needed)
444 shells_needed = lchr[lp->lnd_type].l_ammo;
445 shells = keepshells = lp->lnd_item[I_SHELL];
446 if (shells < shells_needed) {
447 lp->lnd_item[I_SHELL] = 0;
448 putland(lp->lnd_uid, lp);
449 shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
450 I_SHELL, (shells_needed - shells));
451 lp->lnd_item[I_SHELL] = keepshells;
452 putland(lp->lnd_uid, lp);
455 if (shells < shells_needed)