2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * finish.c: Finish the update
30 * Known contributors to this file:
32 * Thomas Ruschak, 1993
34 * Markus Armbruster, 2004-2011
40 #include <sys/resource.h>
41 #include "distribute.h"
45 static void assemble_dist_paths(double *);
50 static double *import_cost;
54 struct rusage rus1, rus2;
56 if (import_cost == NULL) {
57 logerror("First update since reboot, allocating buffer\n");
58 import_cost = malloc(WORLD_SZ() * sizeof(*import_cost));
59 if (import_cost == NULL) {
60 logerror("malloc failed in finish_sects.\n");
65 logerror("delivering...\n");
67 for (n = 0; NULL != (sp = getsectid(n)); n++) {
68 if (sp->sct_type == SCT_WATER)
72 np = getnatp(sp->sct_own);
73 if (np->nat_money < 0)
77 logerror("done delivering\n");
79 logerror("assembling paths...\n");
80 getrusage(RUSAGE_SELF, &rus1);
82 /* First, enable the best_path cacheing */
83 bp_enable_cachepath();
85 /* Now assemble the paths */
86 assemble_dist_paths(import_cost);
88 /* Now disable the best_path cacheing */
89 bp_disable_cachepath();
91 /* Now, clear the best_path cache that may have been created */
94 getrusage(RUSAGE_SELF, &rus2);
95 logerror("done assembling paths %g user %g system",
96 rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
97 - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
98 rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
99 - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
101 logerror("exporting...");
102 for (n = 0; NULL != (sp = getsectid(n)); n++) {
103 if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
105 np = getnatp(sp->sct_own);
106 if (np->nat_money < 0)
108 dodistribute(sp, EXPORT, import_cost[n]);
110 logerror("done exporting\n");
112 logerror("importing...");
113 for (n = 0; NULL != (sp = getsectid(n)); n++) {
114 if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
116 np = getnatp(sp->sct_own);
117 if (np->nat_money < 0)
119 dodistribute(sp, IMPORT, import_cost[n]);
122 logerror("done importing\n");
127 assemble_dist_paths(double *import_cost)
136 for (n = 0; NULL != (sp = getsectid(n)); n++) {
138 if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
140 dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
141 if (CANT_HAPPEN(!dist))
143 /* Now, get the best distribution path over roads */
144 /* Note we go from the dist center to the sector. This gives
145 us the import path for that sector. */
146 path = BestDistPath(buf, dist, sp, &d);