]> git.pond.sub.org Git - empserver/blob - src/lib/update/finish.c
Compile-time option to use A* for distribution
[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 #if DIST_PATH_NO_REUSE == 1
192         import_cost[uid] = path_find(sp->sct_dist_x, sp->sct_dist_y,
193                                      sp->sct_x, sp->sct_y, dist->sct_own,
194                                      MOB_MOVE);
195 #else
196         path_find_from(sp->sct_dist_x, sp->sct_dist_y,
197                        dist->sct_own, MOB_MOVE);
198         import_cost[uid] = path_find_to(sp->sct_x, sp->sct_y);
199 #endif
200 #else
201         if (sp->sct_dist_x != dx || sp->sct_dist_y != dy) {
202             dx = sp->sct_dist_x;
203             dy = sp->sct_dist_y;
204             path_find_from(dx, dy, dist->sct_own, MOB_MOVE);
205         }
206         import_cost[uid] = path_find_to(sp->sct_x, sp->sct_y);
207 #endif
208     }
209 #endif  /* USE_PATH_FIND || TEST_PATH_FIND */
210 #if !defined(USE_PATH_FIND) || defined(TEST_PATH_FIND)
211     char *path;
212     double d;
213     char buf[512];
214
215     for (n = 0; NULL != (sp = getsectid(n)); n++) {
216 #ifdef TEST_PATH_FIND
217         double new_imc = import_cost[n];
218 #endif
219         import_cost[n] = -1;
220         if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
221             continue;
222         dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
223         if (CANT_HAPPEN(!dist))
224             continue;
225         if (sp->sct_own != dist->sct_own)
226             continue;
227         /* Now, get the best distribution path over roads */
228         /* Note we go from the dist center to the sector.  This gives
229            us the import path for that sector. */
230         path = BestDistPath(buf, dist, sp, &d);
231         if (path)
232             import_cost[n] = d;
233 #ifdef TEST_PATH_FIND
234         if (fabs(import_cost[n] - new_imc) >= 1e-6) {
235             printf("%d,%d <- %d,%d %d: old %g, new %g, %g off\n",
236                    sp->sct_dist_x, sp->sct_dist_y,
237                    sp->sct_x, sp->sct_y, MOB_MOVE,
238                    import_cost[n], new_imc, import_cost[n] - new_imc);
239             printf("\told: %s\n", path);
240             d = path_find(sp->sct_dist_x, sp->sct_dist_y,
241                           sp->sct_x, sp->sct_y, dist->sct_own, MOB_MOVE);
242             assert(d - new_imc < 1e-6);
243             path_find_route(buf, sizeof(buf),
244                             sp->sct_dist_x, sp->sct_dist_y,
245                             sp->sct_x, sp->sct_y);
246             printf("\tnew: %s\n", buf);
247         }
248 #endif  /* TEST_PATH_FIND */
249     }
250 #endif  /* !USE_PATH_FIND || TEST_PATH_FIND */
251 #if defined(USE_PATH_FIND) || defined(TEST_PATH_FIND)
252     path_find_print_stats();
253 #endif
254 }