]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/finish.c
Compile-time option to use A* for distribution
[empserver] / src / lib / update / finish.c
index 43e035557f40008ec7eddf9bd2ec6d28675e5474..16c9872d9fe86b799e2e2d5d87e4f19f5edc3ba4 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-2011, 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
  */
 
 #include <config.h>
 
+#include <assert.h>
+#include <math.h>
 #include <stdlib.h>
 #include <sys/resource.h>
 #include "distribute.h"
 #include "path.h"
 #include "update.h"
 
-/* Used for building up distribution info */
-struct distinfo {
-    double imcost;             /* import cost */
-    double excost;             /* export cost */
-};
-
-/* This is our global buffer of distribution pointers.  Note that
- * We only malloc this once, and never again (until reboot time
- * of course :) ) We do clear it each and every time. */
-static struct distinfo *g_distptrs;
-
-static void assemble_dist_paths(struct distinfo *distptrs);
-static char *ReversePath(char *path);
+static void assemble_dist_paths(double *);
 
 void
 finish_sects(int etu)
 {
+    static double *import_cost;
     struct sctstr *sp;
     struct natstr *np;
     int n;
     struct rusage rus1, rus2;
-    struct distinfo *infptr;
 
-    if (g_distptrs == NULL) {
+    if (import_cost == NULL) {
        logerror("First update since reboot, allocating buffer\n");
-       /* Allocate the information buffer */
-       g_distptrs = malloc(WORLD_SZ() * sizeof(*g_distptrs));
-       if (g_distptrs == NULL) {
+       import_cost = malloc(WORLD_SZ() * sizeof(*import_cost));
+       if (import_cost == NULL) {
            logerror("malloc failed in finish_sects.\n");
            return;
        }
     }
 
-    /* Wipe it clean */
-    memset(g_distptrs, 0, WORLD_SZ() * sizeof(*g_distptrs));
-
     logerror("delivering...\n");
     /* Do deliveries */
     for (n = 0; NULL != (sp = getsectid(n)); n++) {
@@ -98,7 +84,7 @@ finish_sects(int etu)
     bp_enable_cachepath();
 
     /* Now assemble the paths */
-    assemble_dist_paths(g_distptrs);
+    assemble_dist_paths(import_cost);
 
     /* Now disable the best_path cacheing */
     bp_disable_cachepath();
@@ -115,100 +101,154 @@ 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)
            continue;
        np = getnatp(sp->sct_own);
        if (np->nat_money < 0)
            continue;
-       /* Get the pointer */
-       infptr = &g_distptrs[sp->sct_uid];
-       dodistribute(sp, EXPORT, infptr->imcost, infptr->excost);
+       dodistribute(sp, EXPORT, import_cost[n]);
     }
     logerror("done exporting\n");
 
     logerror("importing...");
     for (n = 0; NULL != (sp = getsectid(n)); n++) {
-       /* Get the pointer (we do it first so we can free if needed) */
-       infptr = &g_distptrs[sp->sct_uid];
-       if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
+       if (!sp->sct_own)
            continue;
        np = getnatp(sp->sct_own);
        if (np->nat_money < 0)
            continue;
-       dodistribute(sp, IMPORT, infptr->imcost, infptr->excost);
+       dodistribute(sp, IMPORT, import_cost[n]);
        sp->sct_off = 0;
     }
     logerror("done importing\n");
 
 }
 
+#if (defined(USE_PATH_FIND) || defined(TEST_PATH_FIND)) && !defined(DIST_PATH_NO_REUSE)
+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;
+}
+#endif
+
 static void
-assemble_dist_paths(struct distinfo *distptrs)
+assemble_dist_paths(double *import_cost)
 {
-    char *path, *p;
-    double d;
     struct sctstr *sp;
     struct sctstr *dist;
-    struct distinfo *infptr;
     int n;
+#if defined(USE_PATH_FIND) || defined(TEST_PATH_FIND)
+    static int *job;
+    int uid, i;
+    coord dx = 1, dy = 0;      /* invalid */
+
+#ifdef DIST_PATH_NO_REUSE
+    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;
+#else
+    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;
+       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];
+#endif /* !DIST_PATH_NO_REUSE */
+       sp = getsectid(uid);
+       dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
+       if (CANT_HAPPEN(!dist))
+           continue;
+       if (sp->sct_own != dist->sct_own)
+           continue;
+#ifdef DIST_PATH_NO_REUSE
+#if DIST_PATH_NO_REUSE == 1
+       import_cost[uid] = path_find(sp->sct_dist_x, sp->sct_dist_y,
+                                    sp->sct_x, sp->sct_y, dist->sct_own,
+                                    MOB_MOVE);
+#else
+       path_find_from(sp->sct_dist_x, sp->sct_dist_y,
+                      dist->sct_own, MOB_MOVE);
+       import_cost[uid] = path_find_to(sp->sct_x, sp->sct_y);
+#endif
+#else
+       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);
+       }
+       import_cost[uid] = path_find_to(sp->sct_x, sp->sct_y);
+#endif
+    }
+#endif /* USE_PATH_FIND || TEST_PATH_FIND */
+#if !defined(USE_PATH_FIND) || defined(TEST_PATH_FIND)
+    char *path;
+    double d;
     char buf[512];
 
     for (n = 0; NULL != (sp = getsectid(n)); n++) {
+#ifdef TEST_PATH_FIND
+       double new_imc = import_cost[n];
+#endif
+       import_cost[n] = -1;
        if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
            continue;
-       /* Set the pointer */
-       infptr = &distptrs[sp->sct_uid];
-       /* now, get the dist sector */
        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;
-       }
        /* 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);
-
-       /* Now, we have a path */
-       if (!path)
-           infptr->imcost = infptr->excost = -1.0;
-       else {
-           /* Save the import cost */
-           infptr->imcost = d;
-           /* Now, reverse the path */
-           p = ReversePath(path);
-           /* And walk the path back to the dist center to get the export
-              cost */
-           infptr->excost = pathcost(sp, p, MOB_MOVE);
+       if (path)
+           import_cost[n] = d;
+#ifdef TEST_PATH_FIND
+       if (fabs(import_cost[n] - new_imc) >= 1e-6) {
+           printf("%d,%d <- %d,%d %d: old %g, new %g, %g off\n",
+                  sp->sct_dist_x, sp->sct_dist_y,
+                  sp->sct_x, sp->sct_y, MOB_MOVE,
+                  import_cost[n], new_imc, import_cost[n] - new_imc);
+           printf("\told: %s\n", path);
+           d = path_find(sp->sct_dist_x, sp->sct_dist_y,
+                         sp->sct_x, sp->sct_y, dist->sct_own, MOB_MOVE);
+           assert(d - new_imc < 1e-6);
+           path_find_route(buf, sizeof(buf),
+                           sp->sct_dist_x, sp->sct_dist_y,
+                           sp->sct_x, sp->sct_y);
+           printf("\tnew: %s\n", buf);
        }
+#endif /* TEST_PATH_FIND */
     }
-}
-
-static char *
-ReversePath(char *path)
-{
-    char *patharray = "aucdefjhigklmyopqrstbvwxnz";
-    static char new_path[512];
-    int ind;
-
-    if (path == NULL)
-       return NULL;
-
-    ind = strlen(path);
-    if (ind == 0)
-       return NULL;
-
-    if (path[ind - 1] == 'h')
-       ind--;
-
-    new_path[ind--] = '\0';
-    new_path[ind] = '\0';
-
-    while (ind >= 0) {
-       new_path[ind--] = patharray[*(path++) - 'a'];
-    }
-
-    return new_path;
+#endif /* !USE_PATH_FIND || TEST_PATH_FIND */
+#if defined(USE_PATH_FIND) || defined(TEST_PATH_FIND)
+    path_find_print_stats();
+#endif
 }