]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/distribute.c
Update copyright notice
[empserver] / src / lib / update / distribute.c
index 98ce93e99e2d957af198a9aa100fc20eb79c2de5..81d946570f156d9ff93a607b262919aa0f403dce 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2013, 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,
  *  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/>.
  *
  *  ---
  *
- *  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.
  *
  *  ---
  *
  *  distribute.c: Do distribution to sectors
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Steve McClure, 1998
+ *     Markus Armbruster, 2004-2011
  */
 
-#include <stdlib.h>
-#include "misc.h"
-#include "plague.h"
-#include "nat.h"
-#include "sect.h"
+#include <config.h>
+
+#include "distribute.h"
 #include "item.h"
-#include "xy.h"
 #include "path.h"
-#include "file.h"
-#include "distribute.h"
-#include "update.h"
-#include "subs.h"
-#include "common.h"
+#include "plague.h"
 #include "prototypes.h"
+#include "update.h"
+
+#ifdef DISTRIBUTE_DEBUG
+#define DPRINTF(fmt, ...) ((void)printf(fmt , ## __VA_ARGS__))
+#else
+#define DPRINTF(fmt, ...) ((void)0)
+#endif
 
 #define EXPORT_BONUS 10.0
 #define IMPORT_BONUS 10.0
 
-#ifndef MAX
-#define MAX(x,y) ((x) < (y) ? (y) : (x))
-#endif
-
 int
-dodistribute(struct sctstr *sp, int imex, s_char *path, double dist_i_cost,
-            double dist_e_cost)
-         /* import or export? */
+dodistribute(struct sctstr *sp, int imex, double import_cost)
 {
+    double path_cost, dcc;
     struct ichrstr *ip;
     struct sctstr *dist;
     int amt;
     int thresh;
     int amt_dist;
     int amt_sect;
-    i_packing sect_packing, dist_packing;
+    enum i_packing sect_packing, dist_packing;
     int pack;
     double mcost;
     int diff;
@@ -74,22 +69,30 @@ dodistribute(struct sctstr *sp, int imex, s_char *path, double dist_i_cost,
     int rplague;
     int lplague;
 
-    if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
+    if (imex == IMPORT && import_cost < 0.0)
        return 0;
 
-    if (path == (s_char *)0) {
-       if (sp->sct_own != 0) {
-           if (imex == EXPORT) /* only want this once */
+    dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
+    if (imex == IMPORT)
+       path_cost = import_cost;
+    else {
+       dcc = sector_mcost(dist, MOB_MOVE);
+       if (import_cost < 0.0 || dcc < 0.0) {
+           if (sp->sct_own && sp->sct_uid != dist->sct_uid)
                wu(0, sp->sct_own, "No path to dist sector for %s\n",
                   ownxy(sp));
+           return 0;
        }
-       return 0;
+       path_cost = import_cost - sector_mcost(sp, MOB_MOVE) + dcc;
     }
 
-    dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
     dist_packing = dist->sct_effic >= 60 ? dchr[dist->sct_type].d_pkg : IPKG;
     sect_packing = sp->sct_effic   >= 60 ? dchr[sp->sct_type].d_pkg : IPKG;
 
+    DPRINTF("distribute: %d,%d to %d,%d pathcost %g\n",
+           sp->sct_x, sp->sct_y, sp->sct_dist_x, sp->sct_dist_y,
+           path_cost);
+
     lplague = rplague = changed = 0;
     for (item = I_NONE + 1; item <= I_MAX; item++) {
        if (sp->sct_dist[item] == 0)
@@ -125,13 +128,13 @@ dodistribute(struct sctstr *sp, int imex, s_char *path, double dist_i_cost,
             * check to make sure have enough mobility in the
             * dist sector to import what we need.
             */
-           if (dist->sct_mobil <= 0) {
-               /*logerror("  dist mobil < 0"); */
+           if (dist->sct_mobil <= 0)
                continue;
-           }
            amt = diff;
-           if (item == I_CIVIL)
-               amt_dist--;     /* Don't send your last civ */
+           /* make sure not to abandon the sector */
+           if (item == I_CIVIL ||
+               (item == I_MILIT && dist->sct_item[I_CIVIL] == 0))
+               amt_dist--;
 
            if (amt_dist < amt) {
                amt = amt_dist;
@@ -139,7 +142,7 @@ dodistribute(struct sctstr *sp, int imex, s_char *path, double dist_i_cost,
                    continue;
            }
            pack = ip->i_pkg[dist_packing];
-           mcost = dist_i_cost / pack * ip->i_lbs / IMPORT_BONUS;
+           mcost = path_cost / pack * ip->i_lbs / IMPORT_BONUS;
            if (dist->sct_mobil < mcost * amt)
                amt = dist->sct_mobil / mcost;
 
@@ -164,15 +167,13 @@ dodistribute(struct sctstr *sp, int imex, s_char *path, double dist_i_cost,
             * also make sure that there's enough space in the
             * target sector to hold the required amt.
             */
-           if (sp->sct_mobil <= 0) {
-               /*logerror("  sp mob is zero"); */
+           if (sp->sct_mobil <= 0)
                continue;
-           }
            amt = diff;
            if (amt > amt_sect)
                amt = amt_sect;
            pack = MAX(ip->i_pkg[sect_packing], ip->i_pkg[dist_packing]);
-           mcost = dist_e_cost / pack * ip->i_lbs / EXPORT_BONUS;
+           mcost = path_cost / pack * ip->i_lbs / EXPORT_BONUS;
            if (sp->sct_mobil < mcost * amt)
                amt = sp->sct_mobil / mcost;
            if (amt > ITEM_MAX - amt_dist)