]> git.pond.sub.org Git - empserver/blob - src/lib/update/finish.c
Speed up export cost calculation in assemble_dist_paths()
[empserver] / src / lib / update / finish.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  finish.c: Finish the update
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Thomas Ruschak, 1993
33  *     Steve McClure, 1998
34  *     Markus Armbruster, 2004-2011
35  */
36
37 #include <config.h>
38
39 #include <stdlib.h>
40 #include <sys/resource.h>
41 #include "distribute.h"
42 #include "path.h"
43 #include "update.h"
44
45 static void assemble_dist_paths(double *);
46
47 void
48 finish_sects(int etu)
49 {
50     static double *import_cost;
51     struct sctstr *sp;
52     struct natstr *np;
53     int n;
54     struct rusage rus1, rus2;
55
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");
61             return;
62         }
63     }
64
65     memset(import_cost, 0, WORLD_SZ() * sizeof(*import_cost));
66
67     logerror("delivering...\n");
68     /* Do deliveries */
69     for (n = 0; NULL != (sp = getsectid(n)); n++) {
70         if (sp->sct_type == SCT_WATER)
71             continue;
72         if (sp->sct_own == 0)
73             continue;
74         np = getnatp(sp->sct_own);
75         if (np->nat_money < 0)
76             continue;
77         dodeliver(sp);
78     }
79     logerror("done delivering\n");
80
81     logerror("assembling paths...\n");
82     getrusage(RUSAGE_SELF, &rus1);
83
84     /* First, enable the best_path cacheing */
85     bp_enable_cachepath();
86
87     /* Now assemble the paths */
88     assemble_dist_paths(import_cost);
89
90     /* Now disable the best_path cacheing */
91     bp_disable_cachepath();
92
93     /* Now, clear the best_path cache that may have been created */
94     bp_clear_cachepath();
95
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));
102
103     logerror("exporting...");
104     for (n = 0; NULL != (sp = getsectid(n)); n++) {
105         if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
106             continue;
107         np = getnatp(sp->sct_own);
108         if (np->nat_money < 0)
109             continue;
110         dodistribute(sp, EXPORT, import_cost[n]);
111     }
112     logerror("done exporting\n");
113
114     logerror("importing...");
115     for (n = 0; NULL != (sp = getsectid(n)); n++) {
116         if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
117             continue;
118         np = getnatp(sp->sct_own);
119         if (np->nat_money < 0)
120             continue;
121         dodistribute(sp, IMPORT, import_cost[n]);
122         sp->sct_off = 0;
123     }
124     logerror("done importing\n");
125
126 }
127
128 static void
129 assemble_dist_paths(double *import_cost)
130 {
131     char *path;
132     double d;
133     struct sctstr *sp;
134     struct sctstr *dist;
135     int n;
136     char buf[512];
137
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))
140             continue;
141         /* now, get the dist sector */
142         dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
143         if (dist == NULL) {
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);
147             continue;
148         }
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);
153         if (path)
154             import_cost[n] = d;
155         else
156             import_cost[n] = -1;
157     }
158 }