]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/finish.c
Remove pointless check for sea from finish_sects()
[empserver] / src / lib / update / finish.c
index acaa44a3f75cff522b92ef3c328b15dcbc002c93..605d3dd651a128efacae986fb4c624dc3f7b0cf0 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2004, 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.
  *
  *  ---
  *
  *  finish.c: Finish the update
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Thomas Ruschak, 1993
  *     Steve McClure, 1998
+ *     Markus Armbruster, 2004-2011
  */
 
-#include "misc.h"
-#include "sect.h"
-#include "nat.h"
-#include "file.h"
-#include "xy.h"
-#include "path.h"
+#include <config.h>
+
+#include <stdlib.h>
+#include <sys/resource.h>
 #include "distribute.h"
+#include "path.h"
 #include "update.h"
-#include "common.h"
-#include "optlist.h"
-
-/* Used for building up distribution info */
-struct distinfo {
-    s_char *path;              /* path to take */
-    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. */
-struct distinfo *g_distptrs = (struct distinfo *)0;
-
-/* Note that even though we malloc and save the path, it is never
- * used.  Thus, this option.  If you want to malloc and save every
- * path and then free when done, just enable this.  Or, if the
- * dodistribute ever uses the path for something other than checking
- * to see that a path exists, enable this */
-/* #define SAVE_FINISH_PATHS */
-
-#ifndef SAVE_FINISH_PATHS
-static s_char *finish_path = "h";      /* Placeholder indicating path exists */
-#endif /* SAVE_FINISH_PATHS */
-
-static void assemble_dist_paths(struct distinfo *distptrs);
-static s_char *ReversePath(s_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 distinfo *infptr;
+    struct rusage rus1, rus2;
 
-    if (g_distptrs == (struct distinfo *)0) {
+    if (import_cost == NULL) {
        logerror("First update since reboot, allocating buffer\n");
-       /* Allocate the information buffer */
-       g_distptrs = (struct distinfo *)(malloc((WORLD_X * WORLD_Y) *
-                                               sizeof(struct distinfo)));
-       if (g_distptrs == (struct distinfo *)0) {
+       import_cost = malloc(WORLD_SZ() * sizeof(*import_cost));
+       if (import_cost == NULL) {
            logerror("malloc failed in finish_sects.\n");
            return;
        }
-
-       logerror("Allocated '%lu' bytes '%d' indices\n",
-                (unsigned long)(WORLD_X * WORLD_Y * sizeof(struct distinfo)),
-                WORLD_X * WORLD_Y);
     }
 
-    /* Wipe it clean */
-    memset(g_distptrs, 0, ((WORLD_X * WORLD_Y) * sizeof(struct distinfo)));
-
     logerror("delivering...\n");
     /* Do deliveries */
     for (n = 0; NULL != (sp = getsectid(n)); n++) {
@@ -111,12 +77,13 @@ finish_sects(int etu)
     logerror("done delivering\n");
 
     logerror("assembling paths...\n");
+    getrusage(RUSAGE_SELF, &rus1);
 
     /* First, enable the best_path cacheing */
     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();
@@ -124,132 +91,62 @@ finish_sects(int etu)
     /* Now, clear the best_path cache that may have been created */
     bp_clear_cachepath();
 
-    logerror("done assembling paths\n");
+    getrusage(RUSAGE_SELF, &rus2);
+    logerror("done assembling paths %g user %g system",
+            rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
+            - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
+            rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
+            - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
 
     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[XYOFFSET(sp->sct_x, sp->sct_y)];
-       dodistribute(sp, EXPORT,
-                    infptr->path, infptr->imcost, infptr->excost);
+       dodistribute(sp, EXPORT, import_cost[n]);
     }
     logerror("done exporting\n");
 
-    /* Note that we free the paths (if allocated) as we loop here */
     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[XYOFFSET(sp->sct_x, sp->sct_y)];
-       if (sp->sct_type == SCT_WATER || sp->sct_own == 0) {
-#ifdef SAVE_FINISH_PATHS
-           if (infptr->path)
-               free((s_char *)infptr->path);
-#endif /* SAVE_FINISH_PATHS */
+       if (!sp->sct_own)
            continue;
-       }
        np = getnatp(sp->sct_own);
-       if (np->nat_money < 0) {
-#ifdef SAVE_FINISH_PATHS
-           if (infptr->path)
-               free((s_char *)infptr->path);
-#endif /* SAVE_FINISH_PATHS */
+       if (np->nat_money < 0)
            continue;
-       }
-       dodistribute(sp, IMPORT,
-                    infptr->path, infptr->imcost, infptr->excost);
-#ifdef SAVE_FINISH_PATHS
-       if (infptr->path)
-           free((s_char *)infptr->path);
-#endif /* SAVE_FINISH_PATHS */
+       dodistribute(sp, IMPORT, import_cost[n]);
+       sp->sct_off = 0;
     }
     logerror("done importing\n");
 
 }
 
 static void
-assemble_dist_paths(struct distinfo *distptrs)
+assemble_dist_paths(double *import_cost)
 {
-    s_char *path, *p;
+    char *path;
     double d;
     struct sctstr *sp;
     struct sctstr *dist;
-    struct distinfo *infptr;
     int n;
-    s_char buf[512];
+    char buf[512];
 
     for (n = 0; NULL != (sp = getsectid(n)); n++) {
+       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[XYOFFSET(sp->sct_x, sp->sct_y)];
-       /* now, get the dist sector */
        dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
-       if (dist == (struct sctstr *)0) {
-           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, MOB_ROAD);
-
-       /* Now, we have a path */
-       if (path != (s_char *)0) {
-#ifdef SAVE_FINISH_PATHS
-           int len;
-           /* Here we malloc a buffer and save it */
-           len = strlen(path);
-           infptr->path = (s_char *)malloc(len);
-           if (infptr->path == (s_char *)0) {
-               logerror("malloc failed in assemble_dist_path!\n");
-               return;
-           }
-#endif /* SAVE_FINISH_PATHS */
-           /* 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_ROAD);
-#ifdef SAVE_FINISH_PATHS
-           memcpy(infptr->path, p, len);
-#else
-           infptr->path = finish_path;
-#endif /* SAVE_FINISH_PATHS */
-       }
+       path = BestDistPath(buf, dist, sp, &d);
+       if (path)
+           import_cost[n] = d;
     }
 }
-
-static s_char *
-ReversePath(s_char *path)
-{
-    s_char *patharray = "aucdefjhigklmyopqrstbvwxnz";
-    static s_char new_path[512];
-    int ind;
-
-    if (path == (s_char *)0)
-       return (s_char *)0;
-
-    ind = strlen(path);
-    if (ind == 0)
-       return (s_char *)0;
-
-    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;
-}