]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/distribute.c
Update copyright notice.
[empserver] / src / lib / update / distribute.c
index 9f9ec8ee2734a81e2d3f2a6b94bca50ac886468f..fba6ec5ef17b6b0b7c82b6a25e2d2ddd2c6c17bf 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-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
 #include "common.h"
 #include "prototypes.h"
 
+#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)
@@ -58,26 +65,18 @@ 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;
+    i_packing sect_packing, dist_packing;
+    int pack;
+    double mcost;
     int diff;
     int item;
-    int dists[I_MAX + 1];
-    int remote[I_MAX + 1];
-    int local[I_MAX + 1];
     int changed;
     int rplague;
     int lplague;
 
-    getvec(VT_ITEM, local, (s_char *)sp, EF_SECTOR);
     if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
        return 0;
 
-    if (getvec(VT_DIST, dists, (s_char *)sp, EF_SECTOR) <= 0)
-       return 0;
-
     if (path == (s_char *)0) {
        if (sp->sct_own != 0) {
            if (imex == EXPORT) /* only want this once */
@@ -88,37 +87,23 @@ 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 */
+    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 (sp->sct_effic >= 60)
-       packing = dchr[sp->sct_type].d_pkg;
-    else
-       packing = NPKG;         /* No packing */
-
-    if ((dist->sct_effic >= 60) && dchr[dist->sct_type].d_pkg == WPKG)
-       packing = dchr[dist->sct_type].d_pkg;
-
-    getvec(VT_ITEM, remote, (s_char *)dist, EF_SECTOR);
     lplague = rplague = changed = 0;
     for (item = 1; item < I_MAX + 1; item++) {
-       if (dists[item] == 0)
+       if (sp->sct_dist[item] == 0)
            continue;
        ip = &ichr[item];
-       thresh = dists[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 = local[item];
-       amt_dist = remote[item];
+       amt_sect = sp->sct_item[item];
+       amt_dist = dist->sct_item[item];
        diff = amt_sect - thresh;
        if (item == I_CIVIL)
            if (sp->sct_own != sp->sct_oldown)
@@ -153,15 +138,16 @@ dodistribute(struct sctstr *sp, int imex, s_char *path, double dist_i_cost,
                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() */
-           remote[item] -= amt;
+           dist->sct_item[item] -= amt;
            changed++;
-           dist->sct_mobil -= (int)(imcost * amt);
-           local[item] += amt;
+           dist->sct_mobil -= (int)(mcost * amt);
+           sp->sct_item[item] += amt;
        } else {
            if (imex != EXPORT)
                continue;
@@ -185,38 +171,33 @@ dodistribute(struct sctstr *sp, int imex, s_char *path, double dist_i_cost,
            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++;
-           local[item] -= amt;
+           sp->sct_item[item] -= amt;
            changed++;
-           sp->sct_mobil -= (int)(excost * amt);
-           remote[item] += amt;
+           sp->sct_mobil -= (int)(mcost * amt);
+           dist->sct_item[item] += amt;
        }
     }
-    putvec(VT_ITEM, remote, (s_char *)dist, EF_SECTOR);
-    putvec(VT_ITEM, local, (s_char *)sp, EF_SECTOR);
 
     if (lplague) {
-       lplague = getvar(V_PSTAGE, (s_char *)dist, EF_SECTOR);
-       if (lplague == PLG_INFECT &&
-           getvar(V_PSTAGE, (s_char *)sp, EF_SECTOR) == PLG_HEALTHY) {
-           putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)sp, EF_SECTOR);
-       }
+       lplague = dist->sct_pstage;
+       if (lplague == PLG_INFECT && sp->sct_pstage == PLG_HEALTHY)
+           sp->sct_pstage = PLG_EXPOSED;
     }
 
     if (rplague) {
-       rplague = getvar(V_PSTAGE, (s_char *)sp, EF_SECTOR);
-       if (rplague == PLG_INFECT &&
-           getvar(V_PSTAGE, (s_char *)dist, EF_SECTOR) == PLG_HEALTHY) {
-           putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)dist, EF_SECTOR);
-       }
+       rplague = sp->sct_pstage;
+       if (rplague == PLG_INFECT && dist->sct_pstage == PLG_HEALTHY)
+           dist->sct_pstage = PLG_EXPOSED;
     }
 
     return changed;