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 memset(import_cost, 0, WORLD_SZ() * sizeof(*import_cost));
67 logerror("delivering...\n");
69 for (n = 0; NULL != (sp = getsectid(n)); n++) {
70 if (sp->sct_type == SCT_WATER)
74 np = getnatp(sp->sct_own);
75 if (np->nat_money < 0)
79 logerror("done delivering\n");
81 logerror("assembling paths...\n");
82 getrusage(RUSAGE_SELF, &rus1);
84 /* First, enable the best_path cacheing */
85 bp_enable_cachepath();
87 /* Now assemble the paths */
88 assemble_dist_paths(import_cost);
90 /* Now disable the best_path cacheing */
91 bp_disable_cachepath();
93 /* Now, clear the best_path cache that may have been created */
96 getrusage(RUSAGE_SELF, &rus2);
97 logerror("done assembling paths %g user %g system",
98 rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
99 - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
100 rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
101 - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
103 logerror("exporting...");
104 for (n = 0; NULL != (sp = getsectid(n)); n++) {
105 if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
107 np = getnatp(sp->sct_own);
108 if (np->nat_money < 0)
110 dodistribute(sp, EXPORT, import_cost[n]);
112 logerror("done exporting\n");
114 logerror("importing...");
115 for (n = 0; NULL != (sp = getsectid(n)); n++) {
116 if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
118 np = getnatp(sp->sct_own);
119 if (np->nat_money < 0)
121 dodistribute(sp, IMPORT, import_cost[n]);
124 logerror("done importing\n");
129 assemble_dist_paths(double *import_cost)
138 for (n = 0; NULL != (sp = getsectid(n)); n++) {
139 if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
141 /* now, get the dist sector */
142 dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
144 logerror("Bad dist sect %d,%d for %d,%d !\n",
145 sp->sct_dist_x, sp->sct_dist_y,
146 sp->sct_x, sp->sct_y);
149 /* Now, get the best distribution path over roads */
150 /* Note we go from the dist center to the sector. This gives
151 us the import path for that sector. */
152 path = BestDistPath(buf, dist, sp, &d);