2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * finish.c: Finish the update
29 * Known contributors to this file:
31 * Thomas Ruschak, 1993
33 * Markus Armbruster, 2004-2016
39 #include <sys/resource.h>
45 #include "prototypes.h"
48 static void assemble_dist_paths(double *);
53 static double *import_cost;
56 struct rusage rus1, rus2;
58 if (import_cost == NULL) {
59 logerror("First update since reboot, allocating buffer\n");
60 import_cost = malloc(WORLD_SZ() * sizeof(*import_cost));
61 if (import_cost == NULL) {
62 logerror("malloc failed in finish_sects.\n");
67 logerror("delivering...\n");
69 for (n = 0; NULL != (sp = getsectid(n)); n++) {
70 if (sp->sct_type == SCT_WATER)
74 if (nat_budget[sp->sct_own].money < 0)
78 logerror("done delivering\n");
80 logerror("assembling paths...\n");
81 getrusage(RUSAGE_SELF, &rus1);
82 assemble_dist_paths(import_cost);
83 getrusage(RUSAGE_SELF, &rus2);
84 logerror("done assembling paths %g user %g system",
85 rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
86 - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
87 rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
88 - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
90 logerror("exporting...");
91 for (n = 0; NULL != (sp = getsectid(n)); n++) {
94 if (nat_budget[sp->sct_own].money < 0)
96 dodistribute(sp, EXPORT, import_cost[n]);
98 logerror("done exporting\n");
100 logerror("importing...");
101 for (n = 0; NULL != (sp = getsectid(n)); n++) {
105 if (nat_budget[sp->sct_own].money < 0)
107 dodistribute(sp, IMPORT, import_cost[n]);
109 logerror("done importing\n");
114 distcmp(const void *p, const void *q)
118 struct sctstr *sp = (void *)empfile[EF_SECTOR].cache;
121 d = sp[b].sct_dist_y - sp[a].sct_dist_y;
124 d = sp[b].sct_dist_x - sp[a].sct_dist_x;
131 assemble_dist_paths(double *import_cost)
138 coord dx = 1, dy = 0; /* invalid */
141 job = malloc(WORLD_SZ() * sizeof(*job));
144 for (uid = 0; NULL != (sp = getsectid(uid)); uid++) {
145 import_cost[uid] = -1;
146 if (sp->sct_dist_x == sp->sct_x && sp->sct_dist_y == sp->sct_y)
151 #ifdef PATH_FIND_STATS
152 printf("dist path reuse %zu bytes, %d/%d used\n",
153 WORLD_SZ() * sizeof(*job), n, WORLD_SZ());
156 qsort(job, n, sizeof(*job), distcmp);
158 for (i = 0; i < n; i++) {
161 dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
162 if (CANT_HAPPEN(!dist))
164 if (sp->sct_own != dist->sct_own)
166 if (sp->sct_dist_x != dx || sp->sct_dist_y != dy) {
169 path_find_from(dx, dy, dist->sct_own, MOB_MOVE);
171 import_cost[uid] = path_find_to(sp->sct_x, sp->sct_y);
173 path_find_print_stats();