]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/distribute.c
Remove dodistribute() parameter path
[empserver] / src / lib / update / distribute.c
index 1b518d8c0aeaaf3e905297e07c4ddf6c4681a2c2..7f86179e71d319bce58e146e7c7563b1dbcccba9 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-2010, 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.
  *
  *  ---
  *
  *  distribute.c: Do distribution to sectors
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Steve McClure, 1998
  */
 
-#include <stdlib.h>
-#include "misc.h"
-#include "var.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
 
 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 dist_i_cost, double dist_e_cost)
 {
     struct ichrstr *ip;
     struct sctstr *dist;
@@ -58,12 +60,11 @@ dodistribute(struct sctstr *sp, int imex, s_char *path, double dist_i_cost,
     int thresh;
     int amt_dist;
     int amt_sect;
-    int packing;
-    float imcost;
-    float excost;
-    int dist_packing;
+    enum i_packing sect_packing, dist_packing;
+    int pack;
+    double mcost;
     int diff;
-    int item;
+    i_type item;
     int changed;
     int rplague;
     int lplague;
@@ -71,7 +72,7 @@ dodistribute(struct sctstr *sp, int imex, s_char *path, double dist_i_cost,
     if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
        return 0;
 
-    if (path == (s_char *)0) {
+    if (dist_e_cost < 0.0) {
        if (sp->sct_own != 0) {
            if (imex == EXPORT) /* only want this once */
                wu(0, sp->sct_own, "No path to dist sector for %s\n",
@@ -81,33 +82,25 @@ dodistribute(struct sctstr *sp, int imex, s_char *path, double dist_i_cost,
     }
 
     dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
-    if (dist->sct_effic >= 60)
-       dist_packing = dchr[dist->sct_type].d_pkg;
-    else
-       dist_packing = NPKG;    /* No packing */
-
-    if (sp->sct_effic >= 60)
-       packing = dchr[sp->sct_type].d_pkg;
-    else
-       packing = NPKG;         /* No packing */
+    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;
 
-    if ((dist->sct_effic >= 60) && dchr[dist->sct_type].d_pkg == WPKG)
-       packing = dchr[dist->sct_type].d_pkg;
+    DPRINTF("distribute: %d,%d to %d,%d pathcost %g\n",
+           sp->sct_x, sp->sct_y, sp->sct_dist_x, sp->sct_dist_y,
+           imex == IMPORT ? dist_i_cost : dist_e_cost);
 
     lplague = rplague = changed = 0;
-    for (item = 1; item < I_MAX + 1; item++) {
+    for (item = I_NONE + 1; item <= I_MAX; item++) {
        if (sp->sct_dist[item] == 0)
            continue;
        ip = &ichr[item];
        thresh = sp->sct_dist[item];
        /*
         * calculate costs for importing and exporting.
-        * the div 10.0 is because delivering straight through
+        * the mob bonus is because delivering straight through
         * to the dist sect is cheaper than stopping at each
         * sector along the way (processor-timewise)
         */
-       excost = (dist_e_cost / ip->i_pkg[packing] * ip->i_lbs) / 10.0;
-       imcost = (dist_i_cost / ip->i_pkg[dist_packing] * ip->i_lbs) / 10.0;
        amt_sect = sp->sct_item[item];
        amt_dist = dist->sct_item[item];
        diff = amt_sect - thresh;
@@ -131,27 +124,28 @@ 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;
-               if (amt_dist == 0)
+               if (amt_dist <= 0)
                    continue;
            }
-           if (dist->sct_mobil < imcost * amt)
-               amt = dist->sct_mobil / imcost;
+           pack = ip->i_pkg[dist_packing];
+           mcost = dist_i_cost / pack * ip->i_lbs / IMPORT_BONUS;
+           if (dist->sct_mobil < mcost * amt)
+               amt = dist->sct_mobil / mcost;
 
            lplague++;
-           /* XXX replace with vector assign and putvec() */
            dist->sct_item[item] -= amt;
            changed++;
-           dist->sct_mobil -= (int)(imcost * amt);
+           dist->sct_mobil -= (int)(mcost * amt);
            sp->sct_item[item] += amt;
        } else {
            if (imex != EXPORT)
@@ -169,25 +163,24 @@ 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;
-           if (sp->sct_mobil < excost * amt)
-               amt = sp->sct_mobil / excost;
-           if (amt + amt_dist > 9999)
-               amt = 9999 - amt_dist;
+           pack = MAX(ip->i_pkg[sect_packing], ip->i_pkg[dist_packing]);
+           mcost = dist_e_cost / pack * ip->i_lbs / EXPORT_BONUS;
+           if (sp->sct_mobil < mcost * amt)
+               amt = sp->sct_mobil / mcost;
+           if (amt > ITEM_MAX - amt_dist)
+               amt = ITEM_MAX - amt_dist;
            if (amt == 0)
                continue;
-           /* XXX replace with vector assign and putvec() */
 
            rplague++;
            sp->sct_item[item] -= amt;
            changed++;
-           sp->sct_mobil -= (int)(excost * amt);
+           sp->sct_mobil -= (int)(mcost * amt);
            dist->sct_item[item] += amt;
        }
     }