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)
82 /* Ok, do we now have enough? */
83 amt = get_minimum(lp, type) - lp->lnd_item[type];
85 lp->lnd_item[type] += supply_commod(lp->lnd_own,
88 amt = get_minimum(lp, type) - lp->lnd_item[type];
90 /* Now, check again to see if we have enough. */
92 /* Are we on a ship? if so, try to get it from the ship first. */
93 if (lp->lnd_ship >= 0) {
94 getship(lp->lnd_ship, &ship);
95 /* Now, determine how much we can get */
96 if (amt > ship.shp_item[type])
97 amt = ship.shp_item[type];
98 /* Now, add and subtract */
99 lp->lnd_item[type] += amt;
100 ship.shp_item[type] -= amt;
101 putship(lp->lnd_ship, &ship);
107 * Actually get the commod
110 supply_commod(int own, int x, int y, i_type type, int total_wanted)
112 if (total_wanted <= 0)
114 return s_commod(own, x, y, type, total_wanted, !player->simulation);
118 * Just return the number you COULD get, without doing it
121 try_supply_commod(int own, int x, int y, i_type type, int total_wanted)
123 if (total_wanted <= 0)
126 return s_commod(own, x, y, type, total_wanted, 0);
129 /* Get supplies of a certain type */
131 s_commod(int own, int x, int y, i_type type, int total_wanted,
134 int wanted = total_wanted;
135 int gotten = 0, lookrange;
136 struct sctstr sect, dest;
142 /* leave at least 1 military in sectors/ships */
143 int minimum = (type == I_MILIT ? 1 : 0);
145 double move_cost, weight, mobcost;
151 /* try to get it from sector we're in */
152 getsect(x, y, &dest);
153 getsect(x, y, §);
154 if (sect.sct_own == own) {
155 if (sect.sct_item[type] - wanted >= minimum) {
156 sect.sct_item[type] -= wanted;
160 } else if (sect.sct_item[type] - minimum > 0) {
161 gotten += sect.sct_item[type] - minimum;
162 wanted -= sect.sct_item[type] - minimum;
163 sect.sct_item[type] = minimum;
168 /* look for a headquarters or warehouse */
169 lookrange = tfact(own, 10.0);
170 snxtsct_dist(&ns, x, y, lookrange);
171 while (nxtsct(&ns, §) && wanted) {
172 if (sect.sct_own != own)
174 if ((sect.sct_type != SCT_WAREH) &&
175 (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
177 if ((sect.sct_type == SCT_HEADQ) &&
178 (sect.sct_dist_x == sect.sct_x) &&
179 (sect.sct_dist_y == sect.sct_y))
181 if (sect.sct_effic < 60)
183 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
185 if (!opt_NOFOOD && type == I_FOOD)
186 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
188 if (sect.sct_item[type] <= minimum) {
189 /* Don't bother... */
193 dp = &dchr[sect.sct_type];
194 packing = ip->i_pkg[dp->d_pkg];
195 if (packing > 1 && sect.sct_effic < 60)
197 weight = (double)ip->i_lbs / packing;
198 mobcost = move_cost * weight;
200 can_move = (double)sect.sct_mobil / mobcost;
202 can_move = sect.sct_item[type] - minimum;
203 if (can_move > sect.sct_item[type] - minimum)
204 can_move = sect.sct_item[type] - minimum;
206 if (can_move >= wanted) {
209 sect.sct_item[type] -= wanted;
211 /* take off mobility for delivering sect */
212 n = roundavg(total_wanted * weight * move_cost);
215 if (n > sect.sct_mobil)
223 } else if (can_move > 0) {
227 sect.sct_item[type] -= can_move;
229 /* take off mobility for delivering sect */
230 n = roundavg(can_move * weight * move_cost);
233 if (n > sect.sct_mobil)
242 /* look for an owned ship in a harbor */
243 snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
245 while (nxtitem(&ni, &ship) && wanted) {
246 if (ship.shp_own != own)
249 if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
251 getsect(ship.shp_x, ship.shp_y, §);
252 if (sect.sct_type != SCT_HARBR)
254 if (sect.sct_effic < 2)
256 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
258 if (!opt_NOFOOD && type == I_FOOD)
259 minimum = 1 + (int)ceil(food_needed(ship.shp_item,
261 if (ship.shp_item[type] <= minimum) {
262 /* Don't bother... */
266 dp = &dchr[sect.sct_type];
267 packing = ip->i_pkg[dp->d_pkg];
268 if (packing > 1 && sect.sct_effic < 60)
270 weight = (double)ip->i_lbs / packing;
271 mobcost = move_cost * weight;
273 can_move = (double)sect.sct_mobil / mobcost;
275 can_move = ship.shp_item[type] - minimum;
276 if (can_move > ship.shp_item[type] - minimum)
277 can_move = ship.shp_item[type] - minimum;
278 if (can_move >= wanted) {
280 ship.shp_item[type] -= wanted;
282 n = roundavg(wanted * weight * move_cost);
285 if (n > sect.sct_mobil)
289 putship(ship.shp_uid, &ship);
293 } else if (can_move > 0) {
297 ship.shp_item[type] -= can_move;
299 n = roundavg(can_move * weight * move_cost);
302 if (n > sect.sct_mobil)
307 putship(ship.shp_uid, &ship);
313 /* look for an owned supply unit */
314 snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
316 while (nxtitem(&ni, &land) && wanted) {
319 if (land.lnd_own != own)
322 lcp = &lchr[(int)land.lnd_type];
323 if (!(lcp->l_flags & L_SUPPLY))
326 if (land.lnd_item[type] <= get_minimum(&land, type))
329 getsect(land.lnd_x, land.lnd_y, §);
330 if (!BestLandPath(buf, &dest, §, &move_cost, MOB_MOVE))
333 if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
336 if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
339 if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
343 * Temporarily zap this unit's store, so the recursion
347 land.lnd_item[type] = 0;
348 putland(land.lnd_uid, &land);
349 save.lnd_seqno = land.lnd_seqno;
351 land.lnd_item[type] =
352 save.lnd_item[type] + s_commod(own, land.lnd_x, land.lnd_y,
353 type, wanted, actually_doit);
355 putland(land.lnd_uid, &land);
357 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;
374 /* resupply the supply unit */
375 resupply_commod(&land, type);
377 land.lnd_mobil -= roundavg(wanted * weight * move_cost);
380 putland(land.lnd_uid, &land);
382 } else if (can_move > 0) {
385 land.lnd_item[type] -= can_move;
387 land.lnd_mobil -= roundavg(can_move * weight * move_cost);
390 putland(land.lnd_uid, &land);
394 /* We've done the best we could */
395 /* return the number gotten */
401 * We want to get enough shells to fire once,
402 * one update's worth of food.
406 get_minimum(struct lndstr *lp, i_type type)
411 lcp = &lchr[(int)lp->lnd_type];
412 max = lcp->l_item[type];
417 return 0; /* no food reqd, get out */
418 want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
434 has_supply(struct lndstr *lp)
436 int shells_needed, shells, keepshells;
437 int food, food_needed, keepfood;
440 food_needed = get_minimum(lp, I_FOOD);
441 food = keepfood = lp->lnd_item[I_FOOD];
442 if (food < food_needed) {
443 lp->lnd_item[I_FOOD] = 0;
444 putland(lp->lnd_uid, lp);
445 food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
446 I_FOOD, (food_needed - food));
447 lp->lnd_item[I_FOOD] = keepfood;
448 putland(lp->lnd_uid, lp);
450 if (food < food_needed)
455 shells_needed = lchr[lp->lnd_type].l_ammo;
456 shells = keepshells = lp->lnd_item[I_SHELL];
457 if (shells < shells_needed) {
458 lp->lnd_item[I_SHELL] = 0;
459 putland(lp->lnd_uid, lp);
460 shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
461 I_SHELL, (shells_needed - shells));
462 lp->lnd_item[I_SHELL] = keepshells;
463 putland(lp->lnd_uid, lp);
466 if (shells < shells_needed)