]> 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 dbe725720dc79da39c686a451141b3fa26b69eb5..853e77fb178a7886c9dc7e9a3905e49dc081da32 100644 (file)
  *  supply.c: Supply subroutines
  *
  *  Known contributors to this file:
- *
+ *     Markus Armbruster, 2004-2009
  */
 
 #include <config.h>
 
 #include <math.h>
+#include "empobj.h"
 #include "file.h"
 #include "land.h"
 #include "nat.h"
 #include "sect.h"
 #include "ship.h"
 
+static int s_commod(struct empobj *, short *, i_type, int, int, int);
 static int get_minimum(struct lndstr *, i_type);
-static int s_commod(int, int, int, i_type, int, int);
-
-/*
- * We want to get enough guns to be maxed out, enough shells to
- *     fire once, one update's worth of food.
- *
- * 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
- */
 
-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);
+    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, i_type type)
+int
+shp_supply(struct shpstr *sp, i_type type, int wanted)
 {
-    int amt;
+    return s_commod((struct empobj *)sp, sp->shp_item,
+                   type, wanted, mchr[sp->shp_type].m_item[type], 1);
+}
 
-    amt = get_minimum(lp, type) - lp->lnd_item[type];
-    if (amt > 0) {
-       lp->lnd_item[type] += supply_commod(lp->lnd_own,
-                                           lp->lnd_x, lp->lnd_y,
-                                           type, amt);
-    }
+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);
 }
 
 int
@@ -96,36 +78,37 @@ lnd_in_supply(struct lndstr *lp)
     return lp->lnd_item[I_SHELL] >= get_minimum(lp, I_SHELL);
 }
 
-/*
- * Actually get the commod
- */
 int
-supply_commod(int own, int x, int y, i_type type, int total_wanted)
+lnd_supply_all(struct lndstr *lp)
 {
-    if (total_wanted <= 0)
-       return 0;
-    return s_commod(own, x, y, type, total_wanted, !player->simulation);
+    int fail = 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;
 }
 
 /*
- * Just return the number you COULD get, without doing it
+ * 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
-try_supply_commod(int own, int x, int y, i_type type, int total_wanted)
+s_commod(struct empobj *sink, short *vec,
+        i_type type, int wanted, int limit, int actually_doit)
 {
-    if (total_wanted <= 0)
-       return 0;
-
-    return s_commod(own, x, y, type, total_wanted, 0);
-}
-
-/* Get supplies of a certain type */
-static int
-s_commod(int own, int x, int y, i_type type, int total_wanted,
-        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;
     struct nstr_sect ns;
     struct nstr_item ni;
@@ -141,30 +124,46 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
     struct ichrstr *ip;
     char buf[1024];
 
-    /* try to get it from sector we're in */
+    if (wanted > limit)
+       wanted = limit;
+    if (wanted <= vec[type])
+       return 1;
+    wanted -= vec[type];
+
     getsect(x, y, &dest);
-    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)
-               putsect(&sect);
-           return total_wanted;
-       } else if (sect.sct_item[type] - minimum > 0) {
-           gotten += sect.sct_item[type] - minimum;
-           wanted -= sect.sct_item[type] - minimum;
-           sect.sct_item[type] = minimum;
-           if (actually_doit)
-               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, 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) &&
@@ -181,10 +180,8 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
        if (!opt_NOFOOD && type == I_FOOD)
            minimum = 1 + (int)ceil(food_needed(sect.sct_item,
                                                etu_per_update));
-       if (sect.sct_item[type] <= minimum) {
-           /* Don't bother... */
+       if (sect.sct_item[type] <= minimum)
            continue;
-       }
        ip = &ichr[type];
        dp = &dchr[sect.sct_type];
        packing = ip->i_pkg[dp->d_pkg];
@@ -211,14 +208,14 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
            if (n > sect.sct_mobil)
                n = sect.sct_mobil;
            sect.sct_mobil -= n;
-
-           if (actually_doit)
+           if (actually_doit) {
+               vec[type] += wanted;
                putsect(&sect);
-
-           return total_wanted;
+               put_empobj(sink->ef_type, sink->uid, sink);
+           }
+           return 1;
        } else if (can_move > 0) {
            int n;
-           gotten += can_move;
            wanted -= can_move;
            sect.sct_item[type] -= can_move;
 
@@ -229,19 +226,20 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
            if (n > sect.sct_mobil)
                n = sect.sct_mobil;
            sect.sct_mobil -= n;
-
-           if (actually_doit)
+           if (actually_doit) {
+               vec[type] += can_move;
                putsect(&sect);
+           }
        }
     }
 
     /* look for an owned ship in a harbor */
     snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
-
     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);
@@ -254,10 +252,8 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
        if (!opt_NOFOOD && type == I_FOOD)
            minimum = 1 + (int)ceil(food_needed(ship.shp_item,
                                                etu_per_update));
-       if (ship.shp_item[type] <= minimum) {
-           /* Don't bother... */
+       if (ship.shp_item[type] <= minimum)
            continue;
-       }
        ip = &ichr[type];
        dp = &dchr[sect.sct_type];
        packing = ip->i_pkg[dp->d_pkg];
@@ -282,13 +278,14 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
                n = sect.sct_mobil;
            sect.sct_mobil -= n;
            if (actually_doit) {
+               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;
            ship.shp_item[type] -= can_move;
 
@@ -298,8 +295,8 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
            if (n > sect.sct_mobil)
                n = sect.sct_mobil;
            sect.sct_mobil -= n;
-
            if (actually_doit) {
+               vec[type] += can_move;
                putship(ship.shp_uid, &ship);
                putsect(&sect);
            }
@@ -308,10 +305,11 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
 
     /* look for an owned supply unit */
     snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
-
     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;
 
@@ -385,33 +383,32 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
        if (can_move >= wanted) {
            land.lnd_item[type] -= wanted;
            land.lnd_mobil -= roundavg(wanted * weight * move_cost);
-
-           if (actually_doit)
+           if (actually_doit) {
+               vec[type] += wanted;
                putland(land.lnd_uid, &land);
-           return total_wanted;
+               put_empobj(sink->ef_type, sink->uid, sink);
+           }
+           return 1;
        } else if (can_move > 0) {
-           gotten += can_move;
            wanted -= can_move;
            land.lnd_item[type] -= can_move;
-
            land.lnd_mobil -= roundavg(can_move * weight * move_cost);
-
-           if (actually_doit)
+           if (actually_doit) {
+               vec[type] += can_move;
                putland(land.lnd_uid, &land);
+           }
        }
     }
 
-    /* We've done the best we could */
-    /* return the number gotten */
-    return gotten;
+    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.
  */
-
 static int
 get_minimum(struct lndstr *lp, i_type type)
 {
@@ -443,38 +440,31 @@ get_minimum(struct lndstr *lp, i_type type)
 int
 lnd_could_be_supplied(struct lndstr *lp)
 {
-    int shells_needed, shells, keepshells;
-    int food, food_needed, keepfood;
+    int res, food, food_needed, shells_needed, shells;
 
     if (!opt_NOFOOD) {
        food_needed = get_minimum(lp, I_FOOD);
-       food = keepfood = lp->lnd_item[I_FOOD];
+       food = lp->lnd_item[I_FOOD];
        if (food < food_needed) {
-           lp->lnd_item[I_FOOD] = 0;
-           putland(lp->lnd_uid, lp);
-           food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
-                                     I_FOOD, (food_needed - food));
-           lp->lnd_item[I_FOOD] = keepfood;
-           putland(lp->lnd_uid, lp);
+           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 = lchr[lp->lnd_type].l_ammo;
-    shells = keepshells = lp->lnd_item[I_SHELL];
+    shells = lp->lnd_item[I_SHELL];
     if (shells < shells_needed) {
-       lp->lnd_item[I_SHELL] = 0;
-       putland(lp->lnd_uid, lp);
-       shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
-                                   I_SHELL, (shells_needed - shells));
-       lp->lnd_item[I_SHELL] = keepshells;
-       putland(lp->lnd_uid, lp);
+       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;
     }
 
-    if (shells < shells_needed)
-       return 0;
-
     return 1;
 }