2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, 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-2011
41 #include <sys/resource.h>
42 #include "distribute.h"
46 static void assemble_dist_paths(double *);
51 static double *import_cost;
55 struct rusage rus1, rus2;
57 if (import_cost == NULL) {
58 logerror("First update since reboot, allocating buffer\n");
59 import_cost = malloc(WORLD_SZ() * sizeof(*import_cost));
60 if (import_cost == NULL) {
61 logerror("malloc failed in finish_sects.\n");
66 logerror("delivering...\n");
68 for (n = 0; NULL != (sp = getsectid(n)); n++) {
69 if (sp->sct_type == SCT_WATER)
73 np = getnatp(sp->sct_own);
74 if (np->nat_money < 0)
78 logerror("done delivering\n");
80 logerror("assembling paths...\n");
81 getrusage(RUSAGE_SELF, &rus1);
83 /* First, enable the best_path cacheing */
84 bp_enable_cachepath();
86 /* Now assemble the paths */
87 assemble_dist_paths(import_cost);
89 /* Now disable the best_path cacheing */
90 bp_disable_cachepath();
92 /* Now, clear the best_path cache that may have been created */
95 getrusage(RUSAGE_SELF, &rus2);
96 logerror("done assembling paths %g user %g system",
97 rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
98 - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
99 rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
100 - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
102 logerror("exporting...");
103 for (n = 0; NULL != (sp = getsectid(n)); n++) {
106 np = getnatp(sp->sct_own);
107 if (np->nat_money < 0)
109 dodistribute(sp, EXPORT, import_cost[n]);
111 logerror("done exporting\n");
113 logerror("importing...");
114 for (n = 0; NULL != (sp = getsectid(n)); n++) {
117 np = getnatp(sp->sct_own);
118 if (np->nat_money < 0)
120 dodistribute(sp, IMPORT, import_cost[n]);
123 logerror("done importing\n");
127 #if (defined(USE_PATH_FIND) || defined(TEST_PATH_FIND)) && !defined(DIST_PATH_NO_REUSE)
129 distcmp(const void *p, const void *q)
133 struct sctstr *sp = (void *)empfile[EF_SECTOR].cache;
136 d = sp[b].sct_dist_y - sp[a].sct_dist_y;
139 d = sp[b].sct_dist_x - sp[a].sct_dist_x;
147 assemble_dist_paths(double *import_cost)
152 #if defined(USE_PATH_FIND) || defined(TEST_PATH_FIND)
155 coord dx = 1, dy = 0; /* invalid */
157 #ifdef DIST_PATH_NO_REUSE
158 for (uid = 0; NULL != (sp = getsectid(uid)); uid++) {
159 import_cost[uid] = -1;
160 if (sp->sct_dist_x == sp->sct_x && sp->sct_dist_y == sp->sct_y)
164 job = malloc(WORLD_SZ() * sizeof(*job));
167 for (uid = 0; NULL != (sp = getsectid(uid)); uid++) {
168 import_cost[uid] = -1;
169 if (sp->sct_dist_x == sp->sct_x && sp->sct_dist_y == sp->sct_y)
174 #ifdef PATH_FIND_STATS
175 printf("dist path reuse %zu bytes, %d/%d used\n",
176 WORLD_SZ() * sizeof(*job), n, WORLD_SZ());
179 qsort(job, n, sizeof(*job), distcmp);
181 for (i = 0; i < n; i++) {
183 #endif /* !DIST_PATH_NO_REUSE */
185 dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
186 if (CANT_HAPPEN(!dist))
188 if (sp->sct_own != dist->sct_own)
190 #ifdef DIST_PATH_NO_REUSE
191 #if DIST_PATH_NO_REUSE == 1
192 import_cost[uid] = path_find(sp->sct_dist_x, sp->sct_dist_y,
193 sp->sct_x, sp->sct_y, dist->sct_own,
196 path_find_from(sp->sct_dist_x, sp->sct_dist_y,
197 dist->sct_own, MOB_MOVE);
198 import_cost[uid] = path_find_to(sp->sct_x, sp->sct_y);
201 if (sp->sct_dist_x != dx || sp->sct_dist_y != dy) {
204 path_find_from(dx, dy, dist->sct_own, MOB_MOVE);
206 import_cost[uid] = path_find_to(sp->sct_x, sp->sct_y);
209 #endif /* USE_PATH_FIND || TEST_PATH_FIND */
210 #if !defined(USE_PATH_FIND) || defined(TEST_PATH_FIND)
215 for (n = 0; NULL != (sp = getsectid(n)); n++) {
216 #ifdef TEST_PATH_FIND
217 double new_imc = import_cost[n];
220 if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
222 dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
223 if (CANT_HAPPEN(!dist))
225 if (sp->sct_own != dist->sct_own)
227 /* Now, get the best distribution path over roads */
228 /* Note we go from the dist center to the sector. This gives
229 us the import path for that sector. */
230 path = BestDistPath(buf, dist, sp, &d);
233 #ifdef TEST_PATH_FIND
234 if (fabs(import_cost[n] - new_imc) >= 1e-6) {
235 printf("%d,%d <- %d,%d %d: old %g, new %g, %g off\n",
236 sp->sct_dist_x, sp->sct_dist_y,
237 sp->sct_x, sp->sct_y, MOB_MOVE,
238 import_cost[n], new_imc, import_cost[n] - new_imc);
239 printf("\told: %s\n", path);
240 d = path_find(sp->sct_dist_x, sp->sct_dist_y,
241 sp->sct_x, sp->sct_y, dist->sct_own, MOB_MOVE);
242 assert(d - new_imc < 1e-6);
243 path_find_route(buf, sizeof(buf),
244 sp->sct_dist_x, sp->sct_dist_y,
245 sp->sct_x, sp->sct_y);
246 printf("\tnew: %s\n", buf);
248 #endif /* TEST_PATH_FIND */
250 #endif /* !USE_PATH_FIND || TEST_PATH_FIND */
251 #if defined(USE_PATH_FIND) || defined(TEST_PATH_FIND)
252 path_find_print_stats();