]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/supply.c
Update copyright notice
[empserver] / src / lib / subs / supply.c
index 57a60a4203c75e15ed4d0dd4b3ba960270ab4746..7b679cae4fc4a75728b3993811312cc621755232 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2017, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *  supply.c: Supply subroutines
  *
  *  Known contributors to this file:
- *     Markus Armbruster, 2009
+ *     Markus Armbruster, 2004-2013
  */
 
 #include <config.h>
 
 #include <math.h>
+#include "chance.h"
 #include "empobj.h"
-#include "file.h"
 #include "land.h"
 #include "nat.h"
 #include "optlist.h"
-#include "player.h"
+#include "path.h"
 #include "prototypes.h"
 #include "sect.h"
 #include "ship.h"
+#include "update.h"
 
 static int s_commod(struct empobj *, short *, i_type, int, int, int);
 static int get_minimum(struct lndstr *, i_type);
@@ -109,7 +109,7 @@ s_commod(struct empobj *sink, short *vec,
     coord x = sink->x;
     coord y = sink->y;
     int lookrange;
-    struct sctstr sect, dest;
+    struct sctstr sect;
     struct nstr_sect ns;
     struct nstr_item ni;
     struct lchrstr *lcp;
@@ -122,7 +122,6 @@ s_commod(struct empobj *sink, short *vec,
     int packing;
     struct dchrstr *dp;
     struct ichrstr *ip;
-    char buf[1024];
 
     if (wanted > limit)
        wanted = limit;
@@ -131,33 +130,37 @@ s_commod(struct empobj *sink, short *vec,
     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) {
-       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);
+    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) &&
@@ -169,15 +172,14 @@ s_commod(struct empobj *sink, short *vec,
            continue;
        if (sect.sct_effic < 60)
            continue;
-       if (!BestLandPath(buf, &dest, &sect, &move_cost, MOB_MOVE))
+       move_cost = path_find(sect.sct_x, sect.sct_y, x, y, own, MOB_MOVE);
+       if (move_cost < 0)
            continue;
        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];
@@ -199,11 +201,7 @@ s_commod(struct empobj *sink, short *vec,
 
            /* take off mobility for delivering sect */
            n = roundavg(wanted * weight * move_cost);
-           if (n < 0)
-               n = 0;
-           if (n > sect.sct_mobil)
-               n = sect.sct_mobil;
-           sect.sct_mobil -= n;
+           sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil);
            if (actually_doit) {
                vec[type] += wanted;
                putsect(&sect);
@@ -217,11 +215,7 @@ s_commod(struct empobj *sink, short *vec,
 
            /* take off mobility for delivering sect */
            n = roundavg(can_move * weight * move_cost);
-           if (n < 0)
-               n = 0;
-           if (n > sect.sct_mobil)
-               n = sect.sct_mobil;
-           sect.sct_mobil -= n;
+           sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil);
            if (actually_doit) {
                vec[type] += can_move;
                putsect(&sect);
@@ -231,11 +225,11 @@ s_commod(struct empobj *sink, short *vec,
 
     /* 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);
@@ -243,15 +237,14 @@ s_commod(struct empobj *sink, short *vec,
            continue;
        if (sect.sct_effic < 2)
            continue;
-       if (!BestLandPath(buf, &dest, &sect, &move_cost, MOB_MOVE))
+       move_cost = path_find(sect.sct_x, sect.sct_y, x, y, own, MOB_MOVE);
+       if (move_cost < 0)
            continue;
        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];
@@ -270,15 +263,12 @@ s_commod(struct empobj *sink, short *vec,
            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 -= n;
+           sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil);
            if (actually_doit) {
                vec[type] += can_move;
                putship(ship.shp_uid, &ship);
-               putsect(&sect);
+               if (n)
+                   putsect(&sect);
                put_empobj(sink->ef_type, sink->uid, sink);
            }
            return 1;
@@ -288,25 +278,23 @@ s_commod(struct empobj *sink, short *vec,
            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 -= n;
+           sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil);
            if (actually_doit) {
                vec[type] += can_move;
                putship(ship.shp_uid, &ship);
-               putsect(&sect);
+               if (n)
+                   putsect(&sect);
            }
        }
     }
 
     /* 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;
 
@@ -317,14 +305,14 @@ s_commod(struct empobj *sink, short *vec,
        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_MOVE))
-           continue;
-
-       if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
-           continue;
+       if (land.lnd_ship >= 0) {
+           getsect(land.lnd_x, land.lnd_y, &sect);
+           if (sect.sct_type != SCT_HARBR || sect.sct_effic < 2)
+               continue;
+       }
 
-       if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
+       move_cost = path_find(land.lnd_x, land.lnd_y, x, y, own, MOB_MOVE);
+       if (move_cost < 0)
            continue;
 
 #if 0
@@ -356,9 +344,11 @@ s_commod(struct empobj *sink, short *vec,
            putland(land.lnd_uid, &land);
            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);
+           s_commod((struct empobj *)&land, land.lnd_item, type, wanted,
+                    lchr[land.lnd_type].l_item[type] - wanted,
+                    actually_doit);
+           land.lnd_item[type] += save.lnd_item[type];
+
            if (actually_doit)
                putland(land.lnd_uid, &land);
            else
@@ -399,7 +389,7 @@ s_commod(struct empobj *sink, short *vec,
 
     if (actually_doit)
        put_empobj(sink->ef_type, sink->uid, sink);
-    return wanted <= vec[type];
+    return 0;
 }
 
 /*
@@ -443,13 +433,10 @@ lnd_could_be_supplied(struct lndstr *lp)
        food_needed = get_minimum(lp, I_FOOD);
        food = lp->lnd_item[I_FOOD];
        if (food < food_needed) {
-           lp->lnd_item[I_FOOD] = 0;
-           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;
-           putland(lp->lnd_uid, lp);
            if (!res)
                return 0;
        }
@@ -458,13 +445,10 @@ lnd_could_be_supplied(struct lndstr *lp)
     shells_needed = lchr[lp->lnd_type].l_ammo;
     shells = lp->lnd_item[I_SHELL];
     if (shells < shells_needed) {
-       lp->lnd_item[I_SHELL] = 0;
-       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;
-       putland(lp->lnd_uid, lp);
        if (!res)
            return 0;
     }