]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/supply.c
Fix return value of s_commod() when it can't draw enough
[empserver] / src / lib / subs / supply.c
index b438a6a95f607ceaff5373f390a3bd19f577e8c6..853e77fb178a7886c9dc7e9a3905e49dc081da32 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  *  supply.c: Supply subroutines
- * 
+ *
  *  Known contributors to this file:
- *  
+ *     Markus Armbruster, 2004-2009
  */
 
-#include "misc.h"
-#include "nat.h"
-#include "var.h"
-#include "retreat.h"
-#include "ship.h"
-#include "land.h"
-#include "sect.h"
-#include "news.h"
-#include "xy.h"
-#include "nsc.h"
-#include "path.h"
-#include "deity.h"
-#include "item.h"
+#include <config.h>
+
+#include <math.h>
+#include "empobj.h"
 #include "file.h"
-#include "options.h"
+#include "land.h"
+#include "nat.h"
 #include "optlist.h"
 #include "player.h"
 #include "prototypes.h"
+#include "sect.h"
+#include "ship.h"
 
-static int get_minimum(struct lndstr *, int);
-static s_char *itemname(int);
-static int s_commod(int, int, int, int, int, int);
-
-/*
- * We want to get enough guns to be maxed out, enough shells to
- *     fire once, one update's worth of food, enough fuel for
- *     one update.
- *
- * Firts, try to forage in the sector
- * Second look for a warehouse or headquarters to leech
- * Third, look for a ship we own in a harbor
- * Fourth, look for supplies in a supply unit we own
- *             (one good reason to do this last is that the supply
- *              unit will then call resupply, taking more time)
- *
- * May want to put code to resupply with SAMs here, later --ts
- *
- */
+static int s_commod(struct empobj *, short *, i_type, int, int, int);
+static int get_minimum(struct lndstr *, i_type);
 
-void
-resupply_all(struct lndstr *lp)
+int
+sct_supply(struct sctstr *sp, i_type type, int wanted)
 {
-    if (!opt_NOFOOD)
-       resupply_commod(lp, I_FOOD);
-    resupply_commod(lp, I_SHELL);
-    if (opt_FUEL)
-       resupply_commod(lp, I_PETROL);
+    return s_commod((struct empobj *)sp, sp->sct_item,
+                   type, wanted, ITEM_MAX, 1);
 }
 
-/*
- * If the unit has less than it's minimum level of a
- * certain commodity, fill it, to the best of our abilities.
- */
-
-void
-resupply_commod(struct lndstr *lp, int type)
+int
+shp_supply(struct shpstr *sp, i_type type, int wanted)
 {
-    int vec[I_MAX + 1];
-    int svec[I_MAX + 1];
-    int amt;
-    struct lchrstr *lcp;
-    struct shpstr ship;
-
-    lcp = &lchr[(int)lp->lnd_type];
-
-    getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
-    /* Ok, do we now have enough? */
-    if (vec[type] < get_minimum(lp, type)) {
-       vec[type] += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, type,
-                                  get_minimum(lp, type) - vec[type]);
-       putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
-    }
-    /* Now, check again to see if we have enough. */
-    if (vec[type] < get_minimum(lp, type)) {
-       /* Nope.  How much do we need? */
-       amt = (get_minimum(lp, type) - vec[type]);
-       /* Are we on a ship?  if so, try to get it from the ship first. */
-       if (lp->lnd_ship >= 0) {
-           getship(lp->lnd_ship, &ship);
-           getvec(VT_ITEM, svec, (s_char *)&ship, EF_SHIP);
-           /* Now, determine how much we can get */
-           amt = (amt < svec[type]) ? amt : svec[type];
-           /* Now, add and subtract */
-           vec[type] += amt;
-           svec[type] -= amt;
-           putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
-           putvec(VT_ITEM, svec, (s_char *)&ship, EF_SHIP);
-           putship(lp->lnd_ship, &ship);
-       }
-    }
+    return s_commod((struct empobj *)sp, sp->shp_item,
+                   type, wanted, mchr[sp->shp_type].m_item[type], 1);
+}
 
-    if (opt_FUEL && type == I_PETROL) {
-       extern float land_mob_scale;
-       extern int etu_per_update;
-       int fuel_needed = (lp->lnd_fuelu * (((float)etu_per_update
-                                            * land_mob_scale)) / 10.0);
-
-       while ((lp->lnd_fuel < fuel_needed) && vec[I_PETROL]) {
-           lp->lnd_fuel += 10;
-           if (lp->lnd_fuel > lp->lnd_fuelc)
-               lp->lnd_fuel = lp->lnd_fuelc;
-           vec[I_PETROL]--;
-           putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
-       }
-    }
+int
+lnd_supply(struct lndstr *lp, i_type type, int wanted)
+{
+    return s_commod((struct empobj *)lp, lp->lnd_item,
+                   type, wanted, lchr[lp->lnd_type].l_item[type], 1);
 }
 
-/*
- * Actually get the commod
- */
 int
-supply_commod(int own, int x, int y, int type, int total_wanted)
+lnd_in_supply(struct lndstr *lp)
 {
-    if (total_wanted < 0)
-       return 0;
-    return s_commod(own, x, y, type, total_wanted, !player->simulation);
+    if (!opt_NOFOOD) {
+       if (lp->lnd_item[I_FOOD] < get_minimum(lp, I_FOOD))
+           return 0;
+    }
+    return lp->lnd_item[I_SHELL] >= get_minimum(lp, I_SHELL);
 }
 
-/*
- * Just return the number you COULD get, without doing it
- */
 int
-try_supply_commod(int own, int x, int y, int type, int total_wanted)
+lnd_supply_all(struct lndstr *lp)
 {
-    if (total_wanted < 0)
-       return 0;
+    int fail = 0;
 
-    return s_commod(own, x, y, type, total_wanted, 0);
+    if (!opt_NOFOOD)
+       fail |= !lnd_supply(lp, I_FOOD, get_minimum(lp, I_FOOD));
+    fail |= !lnd_supply(lp, I_SHELL, get_minimum(lp, I_SHELL));
+    return !fail;
 }
 
-/* Get supplies of a certain type */
+/*
+ * Actually get the commod
+ *
+ * First, try to forage in the sector
+ * Second look for a warehouse or headquarters to leech
+ * Third, look for a ship we own in a harbor
+ * Fourth, look for supplies in a supply unit we own
+ *             (one good reason to do this last is that the supply
+ *              unit will then call resupply, taking more time)
+ *
+ * May want to put code to resupply with SAMs here, later --ts
+ */
 static int
-s_commod(int own, int x, int y, int type, int total_wanted,
-        int actually_doit)
+s_commod(struct empobj *sink, short *vec,
+        i_type type, int wanted, int limit, int actually_doit)
 {
-    int wanted = total_wanted;
-    int gotten = 0, lookrange;
+    natid own = sink->own;
+    coord x = sink->x;
+    coord y = sink->y;
+    int lookrange;
     struct sctstr sect, dest;
-    int vec[I_MAX + 1];
     struct nstr_sect ns;
     struct nstr_item ni;
     struct lchrstr *lcp;
     struct shpstr ship;
     struct lndstr land;
     /* leave at least 1 military in sectors/ships */
-    int minimum = (type == I_MILIT ? 1 : 0);
+    int minimum = 0;
     int can_move;
     double move_cost, weight, mobcost;
     int packing;
-    extern double eatrate;
-    extern int etu_per_update;
     struct dchrstr *dp;
     struct ichrstr *ip;
-    s_char buf[1024];
+    char buf[1024];
+
+    if (wanted > limit)
+       wanted = limit;
+    if (wanted <= vec[type])
+       return 1;
+    wanted -= vec[type];
 
-    /* try to get it from sector we're in */
     getsect(x, y, &dest);
-    getsect(x, y, &sect);
-    if (sect.sct_own == own) {
-       getvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
-       if ((vec[type] - minimum) >= wanted) {
-           vec[type] -= wanted;
-           if (actually_doit) {
-               putvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
-               putsect(&sect);
-           }
-           return total_wanted;
-       } else if ((vec[type] - minimum) > 0) {
-           gotten += (vec[type] - minimum);
-           wanted -= (vec[type] - minimum);
-           vec[type] = minimum;
-           if (actually_doit) {
-               putvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
-               putsect(&sect);
+
+    /* try to get it from sector we're in */
+    if (sink->ef_type != EF_SECTOR) {
+       getsect(x, y, &sect);
+       if (sect.sct_own == own) {
+           if (!opt_NOFOOD && type == I_FOOD)
+               minimum = 1 + (int)ceil(food_needed(sect.sct_item,
+                                                   etu_per_update));
+           if (sect.sct_item[type] - wanted >= minimum) {
+               sect.sct_item[type] -= wanted;
+               if (actually_doit) {
+                   vec[type] += wanted;
+                   putsect(&sect);
+                   put_empobj(sink->ef_type, sink->uid, sink);
+               }
+               return 1;
+           } else if (sect.sct_item[type] - minimum > 0) {
+               wanted -= sect.sct_item[type] - minimum;
+               sect.sct_item[type] = minimum;
+               if (actually_doit) {
+                   vec[type] += sect.sct_item[type] - minimum;
+                   putsect(&sect);
+               }
            }
        }
     }
+
     /* look for a headquarters or warehouse */
-    lookrange = tfact(own, (double)10.0);
+    lookrange = tfact(own, 10.0);
     snxtsct_dist(&ns, x, y, lookrange);
     while (nxtsct(&ns, &sect) && wanted) {
+       if (ns.curdist == 0)
+           continue;
        if (sect.sct_own != own)
            continue;
        if ((sect.sct_type != SCT_WAREH) &&
@@ -223,57 +175,49 @@ s_commod(int own, int x, int y, int type, int total_wanted,
            continue;
        if (sect.sct_effic < 60)
            continue;
-       if (BestLandPath(buf, &dest, &sect, &move_cost, MOB_ROAD) ==
-           (s_char *)0)
+       if (!BestLandPath(buf, &dest, &sect, &move_cost, MOB_MOVE))
            continue;
-       getvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
-       if (!opt_NOFOOD && type == I_FOOD) {
-           minimum = (((double)etu_per_update * eatrate) *
-                      (double)(vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW]))
-               + 2;
-       }
-       if (vec[type] <= minimum) {
-           /* Don't bother... */
+       if (!opt_NOFOOD && type == I_FOOD)
+           minimum = 1 + (int)ceil(food_needed(sect.sct_item,
+                                               etu_per_update));
+       if (sect.sct_item[type] <= minimum)
            continue;
-       }
        ip = &ichr[type];
        dp = &dchr[sect.sct_type];
        packing = ip->i_pkg[dp->d_pkg];
        if (packing > 1 && sect.sct_effic < 60)
            packing = 1;
-       weight = ((double)ip->i_lbs / (double)packing);
+       weight = (double)ip->i_lbs / packing;
        mobcost = move_cost * weight;
        if (mobcost > 0)
-           can_move = ((double)sect.sct_mobil / mobcost);
+           can_move = (double)sect.sct_mobil / mobcost;
        else
-           can_move = vec[type] - minimum;
-       if (can_move > (vec[type] - minimum))
-           can_move = (vec[type] - minimum);
+           can_move = sect.sct_item[type] - minimum;
+       if (can_move > sect.sct_item[type] - minimum)
+           can_move = sect.sct_item[type] - minimum;
 
        if (can_move >= wanted) {
            int n;
 
-           vec[type] -= wanted;
+           sect.sct_item[type] -= wanted;
 
            /* take off mobility for delivering sect */
-           n = roundavg(total_wanted * weight * move_cost);
+           n = roundavg(wanted * weight * move_cost);
            if (n < 0)
                n = 0;
            if (n > sect.sct_mobil)
                n = sect.sct_mobil;
-           sect.sct_mobil -= (u_char)n;
-
+           sect.sct_mobil -= n;
            if (actually_doit) {
-               putvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
+               vec[type] += wanted;
                putsect(&sect);
+               put_empobj(sink->ef_type, sink->uid, sink);
            }
-
-           return total_wanted;
+           return 1;
        } else if (can_move > 0) {
            int n;
-           gotten += can_move;
            wanted -= can_move;
-           vec[type] -= can_move;
+           sect.sct_item[type] -= can_move;
 
            /* take off mobility for delivering sect */
            n = roundavg(can_move * weight * move_cost);
@@ -281,10 +225,9 @@ s_commod(int own, int x, int y, int type, int total_wanted,
                n = 0;
            if (n > sect.sct_mobil)
                n = sect.sct_mobil;
-           sect.sct_mobil -= (u_char)n;
-
+           sect.sct_mobil -= n;
            if (actually_doit) {
-               putvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
+               vec[type] += can_move;
                putsect(&sect);
            }
        }
@@ -292,11 +235,11 @@ s_commod(int own, int x, int y, int type, int total_wanted,
 
     /* look for an owned ship in a harbor */
     snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
-
-    while (nxtitem(&ni, (s_char *)&ship) && wanted) {
+    while (nxtitem(&ni, &ship) && wanted) {
+       if (sink->ef_type == EF_SHIP && sink->uid == ship.shp_uid)
+           continue;
        if (ship.shp_own != own)
            continue;
-
        if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
            continue;
        getsect(ship.shp_x, ship.shp_y, &sect);
@@ -304,62 +247,56 @@ s_commod(int own, int x, int y, int type, int total_wanted,
            continue;
        if (sect.sct_effic < 2)
            continue;
-       if (BestLandPath(buf, &dest, &sect, &move_cost, MOB_ROAD) ==
-           (s_char *)0)
+       if (!BestLandPath(buf, &dest, &sect, &move_cost, MOB_MOVE))
            continue;
-       getvec(VT_ITEM, vec, (s_char *)&ship, EF_SHIP);
        if (!opt_NOFOOD && type == I_FOOD)
-           minimum = (((double)etu_per_update * eatrate) *
-                      (double)(vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW]))
-               + 2;
-       if (vec[type] <= minimum) {
-           /* Don't bother... */
+           minimum = 1 + (int)ceil(food_needed(ship.shp_item,
+                                               etu_per_update));
+       if (ship.shp_item[type] <= minimum)
            continue;
-       }
        ip = &ichr[type];
        dp = &dchr[sect.sct_type];
        packing = ip->i_pkg[dp->d_pkg];
        if (packing > 1 && sect.sct_effic < 60)
            packing = 1;
-       weight = ((double)ip->i_lbs / (double)packing);
+       weight = (double)ip->i_lbs / packing;
        mobcost = move_cost * weight;
        if (mobcost > 0)
-           can_move = ((double)sect.sct_mobil / mobcost);
+           can_move = (double)sect.sct_mobil / mobcost;
        else
-           can_move = vec[type] - minimum;
-       if (can_move > (vec[type] - minimum))
-           can_move = (vec[type] - minimum);
+           can_move = ship.shp_item[type] - minimum;
+       if (can_move > ship.shp_item[type] - minimum)
+           can_move = ship.shp_item[type] - minimum;
        if (can_move >= wanted) {
            int n;
-           vec[type] -= wanted;
+           ship.shp_item[type] -= wanted;
 
            n = roundavg(wanted * weight * move_cost);
            if (n < 0)
                n = 0;
            if (n > sect.sct_mobil)
                n = sect.sct_mobil;
-           sect.sct_mobil -= (u_char)n;
+           sect.sct_mobil -= n;
            if (actually_doit) {
-               putvec(VT_ITEM, vec, (s_char *)&ship, EF_SHIP);
+               vec[type] += can_move;
                putship(ship.shp_uid, &ship);
                putsect(&sect);
+               put_empobj(sink->ef_type, sink->uid, sink);
            }
-           return total_wanted;
+           return 1;
        } else if (can_move > 0) {
            int n;
-           gotten += can_move;
            wanted -= can_move;
-           vec[type] -= can_move;
+           ship.shp_item[type] -= can_move;
 
            n = roundavg(can_move * weight * move_cost);
            if (n < 0)
                n = 0;
            if (n > sect.sct_mobil)
                n = sect.sct_mobil;
-           sect.sct_mobil -= (u_char)n;
-
+           sect.sct_mobil -= n;
            if (actually_doit) {
-               putvec(VT_ITEM, vec, (s_char *)&ship, EF_SHIP);
+               vec[type] += can_move;
                putship(ship.shp_uid, &ship);
                putsect(&sect);
            }
@@ -368,10 +305,11 @@ s_commod(int own, int x, int y, int type, int total_wanted,
 
     /* look for an owned supply unit */
     snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
-
-    while (nxtitem(&ni, (s_char *)&land) && wanted) {
+    while (nxtitem(&ni, &land) && wanted) {
        int min;
 
+       if (sink->ef_type == EF_LAND && sink->uid == land.lnd_uid)
+           continue;
        if (land.lnd_own != own)
            continue;
 
@@ -379,13 +317,11 @@ s_commod(int own, int x, int y, int type, int total_wanted,
        if (!(lcp->l_flags & L_SUPPLY))
            continue;
 
-       getvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
-       if (vec[type] <= get_minimum(&land, type))
+       if (land.lnd_item[type] <= get_minimum(&land, type))
            continue;
 
        getsect(land.lnd_x, land.lnd_y, &sect);
-       if (BestLandPath(buf, &dest, &sect, &move_cost, MOB_ROAD) ==
-           (s_char *)0)
+       if (!BestLandPath(buf, &dest, &sect, &move_cost, MOB_MOVE))
            continue;
 
        if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
@@ -394,134 +330,102 @@ s_commod(int own, int x, int y, int type, int total_wanted,
        if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
            continue;
 
-       if ((vec[type] - wanted) < get_minimum(&land, type)) {
-           int hold;
-           struct lndstr l2;
-
-           l2 = land;
-           hold = vec[type];
-           vec[type] = 0;
-           putvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
-           land.lnd_fuel = 0;
-           putland(land.lnd_uid, &land);
-           hold +=
-               s_commod(own, land.lnd_x, land.lnd_y, type, wanted,
-                        actually_doit);
-           vec[type] = hold;
-           putvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
+#if 0
+       /*
+        * Recursive supply is disabled for now.  It can introduce
+        * cycles into the "resupplies from" relation.  The code below
+        * attempts to break these cycles by temporarily zapping the
+        * commodity being supplied.  That puts the land file in a
+        * funny state temporarily, risking loss of supplies when
+        * something goes wrong on the way.  Worse, it increases
+        * lnd_seqno even when !actually_doit, which can lead to
+        * spurious seqno mismatch oopses in users of
+        * lnd_could_be_supplied().  I can't be bothered to clean up
+        * this mess right now, because recursive resupply is too dumb
+        * to be really useful anyway: each step uses the first source
+        * it finds, without consideration of mobility cost.  If you
+        * re-enable it, don't forget to uncomment its documentation
+        * in supply.t as well.
+        */
+       if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
+           struct lndstr save;
+
+           /*
+            * Temporarily zap this unit's store, so the recursion
+            * avoids it.
+            */
+           save = land;
+           land.lnd_item[type] = 0;
            putland(land.lnd_uid, &land);
-           if (!actually_doit)
-               putland(l2.lnd_uid, &l2);
+           save.lnd_seqno = land.lnd_seqno;
+
+           land.lnd_item[type] =
+               save.lnd_item[type] + s_commod(own, land.lnd_x, land.lnd_y,
+                                              type, wanted, actually_doit);
+           if (actually_doit)
+               putland(land.lnd_uid, &land);
+           else
+               putland(save.lnd_uid, &save);
        }
+#endif
 
-       getvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
+       min = get_minimum(&land, type);
        ip = &ichr[type];
-       weight = ((double)ip->i_lbs);
+       weight = ip->i_lbs;
        mobcost = move_cost * weight;
        if (mobcost > 0)
-           can_move = ((double)land.lnd_mobil / mobcost);
+           can_move = (double)land.lnd_mobil / mobcost;
        else
-           can_move = vec[type] - min;
-       min = get_minimum(&land, type);
-       if (can_move > (vec[type] - min))
-           can_move = (vec[type] - min);
+           can_move = land.lnd_item[type] - min;
+       if (can_move > land.lnd_item[type] - min)
+           can_move = land.lnd_item[type] - min;
 
        if (can_move >= wanted) {
-           vec[type] -= wanted;
-
-           /* resupply the supply unit */
-           resupply_commod(&land, type);
-
+           land.lnd_item[type] -= wanted;
            land.lnd_mobil -= roundavg(wanted * weight * move_cost);
-
            if (actually_doit) {
-               putvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
+               vec[type] += wanted;
                putland(land.lnd_uid, &land);
+               put_empobj(sink->ef_type, sink->uid, sink);
            }
-           return total_wanted;
+           return 1;
        } else if (can_move > 0) {
-           gotten += can_move;
            wanted -= can_move;
-           vec[type] -= can_move;
-
+           land.lnd_item[type] -= can_move;
            land.lnd_mobil -= roundavg(can_move * weight * move_cost);
-
            if (actually_doit) {
-               putvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
+               vec[type] += can_move;
                putland(land.lnd_uid, &land);
            }
        }
     }
 
-    /* We've done the best we could */
-    /* return the number gotten */
-    return gotten;
-}
-
-
-static s_char *
-itemname(int type)
-{
-    register int t;
-    register struct ichrstr *ip;
-
-    t = V_ITEM(type);
-    for (ip = &ichr[1]; ip->i_mnem != 0; ip++) {
-       if (t == ip->i_vtype)
-           return ip->i_name;
-    }
+    if (actually_doit)
+       put_empobj(sink->ef_type, sink->uid, sink);
     return 0;
 }
 
 /*
  * We want to get enough shells to fire once,
- * one update's worth of food, enough fuel for
- * one update.
+ * one update's worth of food.
  */
-
 static int
-get_minimum(struct lndstr *lp, int type)
+get_minimum(struct lndstr *lp, i_type type)
 {
     struct lchrstr *lcp;
     int max, want = 0;
-    extern double eatrate;
-    extern int etu_per_update;
-    extern float land_mob_scale;
 
     lcp = &lchr[(int)lp->lnd_type];
-    max = vl_find(V_ITEM(type), lcp->l_vtype, lcp->l_vamt, (int)lcp->l_nv);
+    max = lcp->l_item[type];
 
     switch (type) {
     case I_FOOD:
        if (opt_NOFOOD)
            return 0;           /* no food reqd, get out */
-       want = (((double)etu_per_update * eatrate) *
-               (double)total_mil(lp)) + 1;
+       want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
        break;
     case I_SHELL:
-       want = lp->lnd_ammo;
-       break;
-
-       /*
-        * return the amount of pet we'd need to get to 
-        * enough fuel for 1 update
-        *
-        */
-    case I_PETROL:
-       if (opt_FUEL == 0)
-           return 0;
-       want = (lp->lnd_fuelu * (((float)etu_per_update *
-                                 land_mob_scale)) / 10.0);
-       want -= lp->lnd_fuel;
-       if (want > 0) {
-           double d;
-           d = (double)want / 10.0;
-           want = (int)d;
-           if (want == 0)
-               want++;
-       }
-
-       max = want;
+       want = lcp->l_ammo;
        break;
     default:
        return 0;
@@ -534,152 +438,33 @@ get_minimum(struct lndstr *lp, int type)
 }
 
 int
-has_supply(struct lndstr *lp)
+lnd_could_be_supplied(struct lndstr *lp)
 {
-    struct lchrstr *lcp;
-    int vec[I_MAX + 1], shells_needed, shells, keepshells;
-    int food, food_needed, keepfood;
-    int fuel_needed, fuel, petrol_needed, petrol, keeppetrol;
-
-    lcp = &lchr[(int)lp->lnd_type];
-    getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
+    int res, food, food_needed, shells_needed, shells;
 
     if (!opt_NOFOOD) {
        food_needed = get_minimum(lp, I_FOOD);
-       food = keepfood = vec[I_FOOD];
+       food = lp->lnd_item[I_FOOD];
        if (food < food_needed) {
-           vec[I_FOOD] = 0;
-           putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
-           food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
-                                     I_FOOD, (food_needed - food));
-           vec[I_FOOD] = keepfood;
-           putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
+           res = s_commod((struct empobj *)lp, lp->lnd_item,
+                          I_FOOD, food_needed,
+                          lchr[lp->lnd_type].l_item[I_FOOD], 0);
+           lp->lnd_item[I_FOOD] = food;
+           if (!res)
+               return 0;
        }
-       if (food < food_needed)
-           return 0;
-
     }
 
-    shells_needed = lp->lnd_ammo;
-    shells = keepshells = vec[I_SHELL];
+    shells_needed = lchr[lp->lnd_type].l_ammo;
+    shells = lp->lnd_item[I_SHELL];
     if (shells < shells_needed) {
-       vec[I_SHELL] = 0;
-       putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
-       shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
-                                   I_SHELL, (shells_needed - shells));
-       vec[I_SHELL] = keepshells;
-       putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
-    }
-
-    if (shells < shells_needed)
-       return 0;
-
-    if (opt_FUEL) {
-       fuel_needed = lp->lnd_fuelu;
-
-       fuel = lp->lnd_fuel;
-
-       petrol = petrol_needed = 0;
-
-       if (fuel < fuel_needed) {
-           petrol_needed =
-               ldround(((double)(fuel_needed - fuel) / 10.0), 1);
-           petrol = keeppetrol = vec[I_PETROL];
-       }
-
-       if (petrol < petrol_needed) {
-           vec[I_PETROL] = 0;
-           putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
-           petrol += try_supply_commod(lp->lnd_own,
-                                       lp->lnd_x, lp->lnd_y,
-                                       I_PETROL,
-                                       (petrol_needed - petrol));
-           vec[I_PETROL] = keeppetrol;
-           putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
-           fuel += petrol * 10;
-       }
-
-       if (fuel < fuel_needed)
+       res = s_commod((struct empobj *)lp, lp->lnd_item,
+                      I_SHELL, shells_needed,
+                      lchr[lp->lnd_type].l_item[I_SHELL], 0);
+       lp->lnd_item[I_SHELL] = shells;
+       if (!res)
            return 0;
     }
-    /* end opt_FUEL */
-    return 1;
-}
-
-int
-use_supply(struct lndstr *lp)
-{
-    struct lchrstr *lcp;
-    int vec[I_MAX + 1], shells_needed, shells, food, food_needed;
-    int fuel_needed, fuel, petrol_needed, petrol;
-
-    lcp = &lchr[(int)lp->lnd_type];
-    getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
-
-    shells_needed = lp->lnd_ammo;
-    shells = vec[I_SHELL];
-    if (shells < shells_needed) {
-       vec[I_SHELL] = 0;
-       putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
-       shells += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_SHELL,
-                               (shells_needed - shells));
-       vec[I_SHELL] = shells;
-    }
-
-    vec[I_SHELL] = max(vec[I_SHELL] - shells_needed, 0);
-
-    if (lp->lnd_frg)           /* artillery */
-       goto artillery;
-
-    food_needed = get_minimum(lp, I_FOOD);
-    food = vec[I_SHELL];
-
-    if (food < food_needed) {
-       vec[I_FOOD] = 0;
-       putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
-       food += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_FOOD,
-                             (food_needed - food));
-       vec[I_FOOD] = food;
-    }
-
-    vec[I_FOOD] = max(vec[I_FOOD] - food_needed, 0);
-
-    if (opt_FUEL) {
-       fuel_needed = lp->lnd_fuelu;
-       fuel = lp->lnd_fuel;
 
-       petrol = petrol_needed = 0;
-
-       if (fuel < fuel_needed) {
-           petrol_needed =
-               ldround(((double)(fuel_needed - fuel) / 10.0), 1);
-           petrol = vec[I_PETROL];
-       }
-
-       if (petrol < petrol_needed) {
-           vec[I_PETROL] = 0;
-           putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
-           petrol += supply_commod(lp->lnd_own,
-                                   lp->lnd_x, lp->lnd_y,
-                                   I_PETROL, (petrol_needed - petrol));
-           vec[I_PETROL] = petrol;
-       }
-
-       if (petrol_needed) {
-           if (petrol >= petrol_needed) {
-               vec[I_PETROL] = max(vec[I_PETROL] - petrol_needed, 0);
-               lp->lnd_fuel += petrol_needed * 10;
-           } else {
-               lp->lnd_fuel += vec[I_PETROL] * 10;
-               vec[I_PETROL] = 0;
-           }
-       }
-
-       lp->lnd_fuel = max(lp->lnd_fuel - fuel_needed, 0);
-    }
-    /* end opt_FUEL */
-  artillery:
-    putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
-    putland(lp->lnd_uid, lp);
     return 1;
 }