]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/finish.c
Update copyright notice
[empserver] / src / lib / update / finish.c
index c8d85dbaee695da535f3820471b0ac6e0f3805ca..965a87432f8eadd905e3e7b03ef5a3c7f0dd9c86 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
  *     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 *);
@@ -48,7 +50,6 @@ finish_sects(int etu)
 {
     static double *import_cost;
     struct sctstr *sp;
-    struct natstr *np;
     int n;
     struct rusage rus1, rus2;
 
@@ -64,12 +65,9 @@ finish_sects(int etu)
     logerror("delivering...\n");
     /* Do deliveries */
     for (n = 0; NULL != (sp = getsectid(n)); n++) {
-       if (sp->sct_type == SCT_WATER)
+       if (!sp->sct_own || sp->sct_type == SCT_SANCT)
            continue;
-       if (sp->sct_own == 0)
-           continue;
-       np = getnatp(sp->sct_own);
-       if (np->nat_money < 0)
+       if (nat_budget[sp->sct_own].money < 0)
            continue;
        dodeliver(sp);
     }
@@ -87,10 +85,9 @@ finish_sects(int etu)
 
     logerror("exporting...");
     for (n = 0; NULL != (sp = getsectid(n)); n++) {
-       if (!sp->sct_own)
+       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]);
     }
@@ -98,30 +95,65 @@ finish_sects(int etu)
 
     logerror("importing...");
     for (n = 0; NULL != (sp = getsectid(n)); n++) {
-       if (!sp->sct_own)
+       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)
 {
     struct sctstr *sp;
     struct sctstr *dist;
     int n;
+    static int *job;
+    int uid, i;
     coord dx = 1, dy = 0;      /* invalid */
 
-    for (n = 0; NULL != (sp = getsectid(n)); n++) {
-       import_cost[n] = -1;
+    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];
+       sp = getsectid(uid);
        dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
        if (CANT_HAPPEN(!dist))
            continue;
@@ -132,7 +164,7 @@ assemble_dist_paths(double *import_cost)
            dy = sp->sct_dist_y;
            path_find_from(dx, dy, dist->sct_own, MOB_MOVE);
        }
-       import_cost[n] = path_find_to(sp->sct_x, sp->sct_y);
+       import_cost[uid] = path_find_to(sp->sct_x, sp->sct_y);
     }
     path_find_print_stats();
 }