]> git.pond.sub.org Git - empserver/blob - src/lib/update/finish.c
25b14d8737d96122475e61c5f1b684a695aad8b5
[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)
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     if (!job)
158         job = malloc(WORLD_SZ() * sizeof(*job));
159
160     n = 0;
161     for (uid = 0; NULL != (sp = getsectid(uid)); uid++) {
162         import_cost[uid] = -1;
163         if (sp->sct_dist_x == sp->sct_x && sp->sct_dist_y == sp->sct_y)
164             continue;
165         job[n++] = uid;
166     }
167
168 #ifdef PATH_FIND_STATS
169     printf("dist path reuse %zu bytes, %d/%d used\n",
170            WORLD_SZ() * sizeof(*job), n, WORLD_SZ());
171 #endif
172
173     qsort(job, n, sizeof(*job), distcmp);
174
175     for (i = 0; i < n; i++) {
176         uid = job[i];
177         sp = getsectid(uid);
178         dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
179         if (CANT_HAPPEN(!dist))
180             continue;
181         if (sp->sct_own != dist->sct_own)
182             continue;
183         if (sp->sct_dist_x != dx || sp->sct_dist_y != dy) {
184             dx = sp->sct_dist_x;
185             dy = sp->sct_dist_y;
186             path_find_from(dx, dy, dist->sct_own, MOB_MOVE);
187         }
188         import_cost[uid] = path_find_to(sp->sct_x, sp->sct_y);
189     }
190 #endif  /* USE_PATH_FIND || TEST_PATH_FIND */
191 #if !defined(USE_PATH_FIND) || defined(TEST_PATH_FIND)
192     char *path;
193     double d;
194     char buf[512];
195
196     for (n = 0; NULL != (sp = getsectid(n)); n++) {
197 #ifdef TEST_PATH_FIND
198         double new_imc = import_cost[n];
199 #endif
200         import_cost[n] = -1;
201         if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
202             continue;
203         dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
204         if (CANT_HAPPEN(!dist))
205             continue;
206         if (sp->sct_own != dist->sct_own)
207             continue;
208         /* Now, get the best distribution path over roads */
209         /* Note we go from the dist center to the sector.  This gives
210            us the import path for that sector. */
211         path = BestDistPath(buf, dist, sp, &d);
212         if (path)
213             import_cost[n] = d;
214 #ifdef TEST_PATH_FIND
215         if (fabs(import_cost[n] - new_imc) >= 1e-6) {
216             printf("%d,%d <- %d,%d %d: old %g, new %g, %g off\n",
217                    sp->sct_dist_x, sp->sct_dist_y,
218                    sp->sct_x, sp->sct_y, MOB_MOVE,
219                    import_cost[n], new_imc, import_cost[n] - new_imc);
220             printf("\told: %s\n", path);
221             d = path_find(sp->sct_dist_x, sp->sct_dist_y,
222                           sp->sct_x, sp->sct_y, dist->sct_own, MOB_MOVE);
223             assert(d - new_imc < 1e-6);
224             path_find_route(buf, sizeof(buf),
225                             sp->sct_dist_x, sp->sct_dist_y,
226                             sp->sct_x, sp->sct_y);
227             printf("\tnew: %s\n", buf);
228         }
229 #endif  /* TEST_PATH_FIND */
230     }
231 #endif  /* !USE_PATH_FIND || TEST_PATH_FIND */
232 #if defined(USE_PATH_FIND) || defined(TEST_PATH_FIND)
233     path_find_print_stats();
234 #endif
235 }