]> git.pond.sub.org Git - empserver/blob - src/lib/update/finish.c
Compile-time option to switch off "multiple paths same source"
[empserver] / src / lib / update / finish.c
1 /*
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
5  *
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.
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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  finish.c: Finish the update
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Thomas Ruschak, 1993
32  *     Steve McClure, 1998
33  *     Markus Armbruster, 2004-2011
34  */
35
36 #include <config.h>
37
38 #include <assert.h>
39 #include <math.h>
40 #include <stdlib.h>
41 #include <sys/resource.h>
42 #include "distribute.h"
43 #include "path.h"
44 #include "update.h"
45
46 static void assemble_dist_paths(double *);
47
48 void
49 finish_sects(int etu)
50 {
51     static double *import_cost;
52     struct sctstr *sp;
53     struct natstr *np;
54     int n;
55     struct rusage rus1, rus2;
56
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");
62             return;
63         }
64     }
65
66     logerror("delivering...\n");
67     /* Do deliveries */
68     for (n = 0; NULL != (sp = getsectid(n)); n++) {
69         if (sp->sct_type == SCT_WATER)
70             continue;
71         if (sp->sct_own == 0)
72             continue;
73         np = getnatp(sp->sct_own);
74         if (np->nat_money < 0)
75             continue;
76         dodeliver(sp);
77     }
78     logerror("done delivering\n");
79
80     logerror("assembling paths...\n");
81     getrusage(RUSAGE_SELF, &rus1);
82
83     /* First, enable the best_path cacheing */
84     bp_enable_cachepath();
85
86     /* Now assemble the paths */
87     assemble_dist_paths(import_cost);
88
89     /* Now disable the best_path cacheing */
90     bp_disable_cachepath();
91
92     /* Now, clear the best_path cache that may have been created */
93     bp_clear_cachepath();
94
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));
101
102     logerror("exporting...");
103     for (n = 0; NULL != (sp = getsectid(n)); n++) {
104         if (!sp->sct_own)
105             continue;
106         np = getnatp(sp->sct_own);
107         if (np->nat_money < 0)
108             continue;
109         dodistribute(sp, EXPORT, import_cost[n]);
110     }
111     logerror("done exporting\n");
112
113     logerror("importing...");
114     for (n = 0; NULL != (sp = getsectid(n)); n++) {
115         if (!sp->sct_own)
116             continue;
117         np = getnatp(sp->sct_own);
118         if (np->nat_money < 0)
119             continue;
120         dodistribute(sp, IMPORT, import_cost[n]);
121         sp->sct_off = 0;
122     }
123     logerror("done importing\n");
124
125 }
126
127 #if (defined(USE_PATH_FIND) || defined(TEST_PATH_FIND)) && !defined(DIST_PATH_NO_REUSE)
128 static int
129 distcmp(const void *p, const void *q)
130 {
131     int a = *(int *)p;
132     int b = *(int *)q;
133     struct sctstr *sp = (void *)empfile[EF_SECTOR].cache;
134     int d;
135
136     d = sp[b].sct_dist_y - sp[a].sct_dist_y;
137     if (d)
138         return d;
139     d = sp[b].sct_dist_x - sp[a].sct_dist_x;
140     if (d)
141         return d;
142     return b - a;
143 }
144 #endif
145
146 static void
147 assemble_dist_paths(double *import_cost)
148 {
149     struct sctstr *sp;
150     struct sctstr *dist;
151     int n;
152 #if defined(USE_PATH_FIND) || defined(TEST_PATH_FIND)
153     static int *job;
154     int uid, i;
155     coord dx = 1, dy = 0;       /* invalid */
156
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)
161             continue;
162 #else
163     if (!job)
164         job = malloc(WORLD_SZ() * sizeof(*job));
165
166     n = 0;
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)
170             continue;
171         job[n++] = uid;
172     }
173
174 #ifdef PATH_FIND_STATS
175     printf("dist path reuse %zu bytes, %d/%d used\n",
176            WORLD_SZ() * sizeof(*job), n, WORLD_SZ());
177 #endif
178
179     qsort(job, n, sizeof(*job), distcmp);
180
181     for (i = 0; i < n; i++) {
182         uid = job[i];
183 #endif  /* !DIST_PATH_NO_REUSE */
184         sp = getsectid(uid);
185         dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
186         if (CANT_HAPPEN(!dist))
187             continue;
188         if (sp->sct_own != dist->sct_own)
189             continue;
190 #ifdef DIST_PATH_NO_REUSE
191         import_cost[uid] = path_find(sp->sct_dist_x, sp->sct_dist_y,
192                                      sp->sct_x, sp->sct_y, dist->sct_own,
193                                      MOB_MOVE);
194 #else
195         if (sp->sct_dist_x != dx || sp->sct_dist_y != dy) {
196             dx = sp->sct_dist_x;
197             dy = sp->sct_dist_y;
198             path_find_from(dx, dy, dist->sct_own, MOB_MOVE);
199         }
200         import_cost[uid] = path_find_to(sp->sct_x, sp->sct_y);
201 #endif
202     }
203 #endif  /* USE_PATH_FIND || TEST_PATH_FIND */
204 #if !defined(USE_PATH_FIND) || defined(TEST_PATH_FIND)
205     char *path;
206     double d;
207     char buf[512];
208
209     for (n = 0; NULL != (sp = getsectid(n)); n++) {
210 #ifdef TEST_PATH_FIND
211         double new_imc = import_cost[n];
212 #endif
213         import_cost[n] = -1;
214         if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
215             continue;
216         dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
217         if (CANT_HAPPEN(!dist))
218             continue;
219         if (sp->sct_own != dist->sct_own)
220             continue;
221         /* Now, get the best distribution path over roads */
222         /* Note we go from the dist center to the sector.  This gives
223            us the import path for that sector. */
224         path = BestDistPath(buf, dist, sp, &d);
225         if (path)
226             import_cost[n] = d;
227 #ifdef TEST_PATH_FIND
228         if (fabs(import_cost[n] - new_imc) >= 1e-6) {
229             printf("%d,%d <- %d,%d %d: old %g, new %g, %g off\n",
230                    sp->sct_dist_x, sp->sct_dist_y,
231                    sp->sct_x, sp->sct_y, MOB_MOVE,
232                    import_cost[n], new_imc, import_cost[n] - new_imc);
233             printf("\told: %s\n", path);
234             d = path_find(sp->sct_dist_x, sp->sct_dist_y,
235                           sp->sct_x, sp->sct_y, dist->sct_own, MOB_MOVE);
236             assert(d - new_imc < 1e-6);
237             path_find_route(buf, sizeof(buf),
238                             sp->sct_dist_x, sp->sct_dist_y,
239                             sp->sct_x, sp->sct_y);
240             printf("\tnew: %s\n", buf);
241         }
242 #endif  /* TEST_PATH_FIND */
243     }
244 #endif  /* !USE_PATH_FIND || TEST_PATH_FIND */
245 #if defined(USE_PATH_FIND) || defined(TEST_PATH_FIND)
246     path_find_print_stats();
247 #endif
248 }