]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/finish.c
Update copyright notice
[empserver] / src / lib / update / finish.c
index 528fce7abc324c17525c2fed11857b53354e8b2f..965a87432f8eadd905e3e7b03ef5a3c7f0dd9c86 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2020, 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/>.
  *
  *  ---
  *
  *     Dave Pare, 1986
  *     Thomas Ruschak, 1993
  *     Steve McClure, 1998
- *     Markus Armbruster, 2004-2011
+ *     Markus Armbruster, 2004-2016
  */
 
 #include <config.h>
 
 #include <stdlib.h>
 #include <sys/resource.h>
-#include "distribute.h"
+#include "optlist.h"
 #include "path.h"
+#include "sect.h"
+#include "prototypes.h"
 #include "update.h"
 
 static void assemble_dist_paths(double *);
@@ -49,7 +50,6 @@ finish_sects(int etu)
 {
     static double *import_cost;
     struct sctstr *sp;
-    struct natstr *np;
     int n;
     struct rusage rus1, rus2;
 
@@ -62,17 +62,12 @@ finish_sects(int etu)
        }
     }
 
-    memset(import_cost, 0, WORLD_SZ() * sizeof(*import_cost));
-
     logerror("delivering...\n");
     /* Do deliveries */
     for (n = 0; NULL != (sp = getsectid(n)); n++) {
-       if (sp->sct_type == SCT_WATER)
-           continue;
-       if (sp->sct_own == 0)
+       if (!sp->sct_own || sp->sct_type == SCT_SANCT)
            continue;
-       np = getnatp(sp->sct_own);
-       if (np->nat_money < 0)
+       if (nat_budget[sp->sct_own].money < 0)
            continue;
        dodeliver(sp);
     }
@@ -80,19 +75,7 @@ finish_sects(int etu)
 
     logerror("assembling paths...\n");
     getrusage(RUSAGE_SELF, &rus1);
-
-    /* First, enable the best_path cacheing */
-    bp_enable_cachepath();
-
-    /* Now assemble the paths */
     assemble_dist_paths(import_cost);
-
-    /* Now disable the best_path cacheing */
-    bp_disable_cachepath();
-
-    /* Now, clear the best_path cache that may have been created */
-    bp_clear_cachepath();
-
     getrusage(RUSAGE_SELF, &rus2);
     logerror("done assembling paths %g user %g system",
             rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
@@ -102,10 +85,9 @@ finish_sects(int etu)
 
     logerror("exporting...");
     for (n = 0; NULL != (sp = getsectid(n)); n++) {
-       if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
+       if (!sp->sct_own || sp->sct_type == SCT_SANCT)
            continue;
-       np = getnatp(sp->sct_own);
-       if (np->nat_money < 0)
+       if (nat_budget[sp->sct_own].money < 0)
            continue;
        dodistribute(sp, EXPORT, import_cost[n]);
     }
@@ -113,46 +95,76 @@ finish_sects(int etu)
 
     logerror("importing...");
     for (n = 0; NULL != (sp = getsectid(n)); n++) {
-       if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
+       sp->sct_off = 0;
+       if (!sp->sct_own || sp->sct_type == SCT_SANCT)
            continue;
-       np = getnatp(sp->sct_own);
-       if (np->nat_money < 0)
+       if (nat_budget[sp->sct_own].money < 0)
            continue;
        dodistribute(sp, IMPORT, import_cost[n]);
-       sp->sct_off = 0;
     }
     logerror("done importing\n");
 
 }
 
+static int
+distcmp(const void *p, const void *q)
+{
+    int a = *(int *)p;
+    int b = *(int *)q;
+    struct sctstr *sp = (void *)empfile[EF_SECTOR].cache;
+    int d;
+
+    d = sp[b].sct_dist_y - sp[a].sct_dist_y;
+    if (d)
+       return d;
+    d = sp[b].sct_dist_x - sp[a].sct_dist_x;
+    if (d)
+       return d;
+    return b - a;
+}
+
 static void
 assemble_dist_paths(double *import_cost)
 {
-    char *path;
-    double d;
     struct sctstr *sp;
     struct sctstr *dist;
     int n;
-    char buf[512];
+    static int *job;
+    int uid, i;
+    coord dx = 1, dy = 0;      /* invalid */
 
-    for (n = 0; NULL != (sp = getsectid(n)); n++) {
-       if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
+    if (!job)
+       job = malloc(WORLD_SZ() * sizeof(*job));
+
+    n = 0;
+    for (uid = 0; NULL != (sp = getsectid(uid)); uid++) {
+       import_cost[uid] = -1;
+       if (sp->sct_dist_x == sp->sct_x && sp->sct_dist_y == sp->sct_y)
            continue;
-       /* now, get the dist sector */
+       job[n++] = uid;
+    }
+
+#ifdef PATH_FIND_STATS
+    printf("dist path reuse %zu bytes, %d/%d used\n",
+          WORLD_SZ() * sizeof(*job), n, WORLD_SZ());
+#endif
+
+    qsort(job, n, sizeof(*job), distcmp);
+
+    for (i = 0; i < n; i++) {
+       uid = job[i];
+       sp = getsectid(uid);
        dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
-       if (dist == NULL) {
-           logerror("Bad dist sect %d,%d for %d,%d !\n",
-                    sp->sct_dist_x, sp->sct_dist_y,
-                    sp->sct_x, sp->sct_y);
+       if (CANT_HAPPEN(!dist))
+           continue;
+       if (sp->sct_own != dist->sct_own)
            continue;
+       if (sp->sct_dist_x != dx || sp->sct_dist_y != dy) {
+           dx = sp->sct_dist_x;
+           dy = sp->sct_dist_y;
+           path_find_from(dx, dy, dist->sct_own, MOB_MOVE);
        }
-       /* Now, get the best distribution path over roads */
-       /* Note we go from the dist center to the sector.  This gives
-          us the import path for that sector. */
-       path = BestDistPath(buf, dist, sp, &d);
-       if (path)
-           import_cost[n] = d;
-       else
-           import_cost[n] = -1;
+       import_cost[uid] = path_find_to(sp->sct_x, sp->sct_y);
     }
+    path_find_print_stats();
 }