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
39 #include <sys/resource.h>
40 #include "distribute.h"
44 static void assemble_dist_paths(double *);
49 static double *import_cost;
53 struct rusage rus1, rus2;
55 if (import_cost == NULL) {
56 logerror("First update since reboot, allocating buffer\n");
57 import_cost = malloc(WORLD_SZ() * sizeof(*import_cost));
58 if (import_cost == NULL) {
59 logerror("malloc failed in finish_sects.\n");
64 logerror("delivering...\n");
66 for (n = 0; NULL != (sp = getsectid(n)); n++) {
67 if (sp->sct_type == SCT_WATER)
71 np = getnatp(sp->sct_own);
72 if (np->nat_money < 0)
76 logerror("done delivering\n");
78 logerror("assembling paths...\n");
79 getrusage(RUSAGE_SELF, &rus1);
81 /* First, enable the best_path cacheing */
82 bp_enable_cachepath();
84 /* Now assemble the paths */
85 assemble_dist_paths(import_cost);
87 /* Now disable the best_path cacheing */
88 bp_disable_cachepath();
90 /* Now, clear the best_path cache that may have been created */
93 getrusage(RUSAGE_SELF, &rus2);
94 logerror("done assembling paths %g user %g system",
95 rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
96 - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
97 rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
98 - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
100 logerror("exporting...");
101 for (n = 0; NULL != (sp = getsectid(n)); n++) {
104 np = getnatp(sp->sct_own);
105 if (np->nat_money < 0)
107 dodistribute(sp, EXPORT, import_cost[n]);
109 logerror("done exporting\n");
111 logerror("importing...");
112 for (n = 0; NULL != (sp = getsectid(n)); n++) {
115 np = getnatp(sp->sct_own);
116 if (np->nat_money < 0)
118 dodistribute(sp, IMPORT, import_cost[n]);
121 logerror("done importing\n");
126 assemble_dist_paths(double *import_cost)
135 for (n = 0; NULL != (sp = getsectid(n)); n++) {
137 if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
139 dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
140 if (CANT_HAPPEN(!dist))
142 if (sp->sct_own != dist->sct_own)
144 /* Now, get the best distribution path over roads */
145 /* Note we go from the dist center to the sector. This gives
146 us the import path for that sector. */
147 path = BestDistPath(buf, dist, sp, &d);