]> git.pond.sub.org Git - empserver/blob - src/lib/update/finish.c
Merge branch 'pathfind' into pathfind-test
[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 <stdlib.h>
39 #include <sys/resource.h>
40 #include "distribute.h"
41 #include "path.h"
42 #include "update.h"
43
44 static void assemble_dist_paths(double *);
45
46 void
47 finish_sects(int etu)
48 {
49     static double *import_cost;
50     struct sctstr *sp;
51     struct natstr *np;
52     int n;
53     struct rusage rus1, rus2;
54
55     if (import_cost == NULL) {
56         logerror("First update since reboot, allocating buffer\n");
57         import_cost = malloc(WORLD_SZ() * sizeof(*import_cost));
58         if (import_cost == NULL) {
59             logerror("malloc failed in finish_sects.\n");
60             return;
61         }
62     }
63
64     logerror("delivering...\n");
65     /* Do deliveries */
66     for (n = 0; NULL != (sp = getsectid(n)); n++) {
67         if (sp->sct_type == SCT_WATER)
68             continue;
69         if (sp->sct_own == 0)
70             continue;
71         np = getnatp(sp->sct_own);
72         if (np->nat_money < 0)
73             continue;
74         dodeliver(sp);
75     }
76     logerror("done delivering\n");
77
78     logerror("assembling paths...\n");
79     getrusage(RUSAGE_SELF, &rus1);
80
81     /* First, enable the best_path cacheing */
82     bp_enable_cachepath();
83
84     /* Now assemble the paths */
85     assemble_dist_paths(import_cost);
86
87     /* Now disable the best_path cacheing */
88     bp_disable_cachepath();
89
90     /* Now, clear the best_path cache that may have been created */
91     bp_clear_cachepath();
92
93     getrusage(RUSAGE_SELF, &rus2);
94     logerror("done assembling paths %g user %g system",
95              rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
96              - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
97              rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
98              - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
99
100     logerror("exporting...");
101     for (n = 0; NULL != (sp = getsectid(n)); n++) {
102         if (!sp->sct_own)
103             continue;
104         np = getnatp(sp->sct_own);
105         if (np->nat_money < 0)
106             continue;
107         dodistribute(sp, EXPORT, import_cost[n]);
108     }
109     logerror("done exporting\n");
110
111     logerror("importing...");
112     for (n = 0; NULL != (sp = getsectid(n)); n++) {
113         if (!sp->sct_own)
114             continue;
115         np = getnatp(sp->sct_own);
116         if (np->nat_money < 0)
117             continue;
118         dodistribute(sp, IMPORT, import_cost[n]);
119         sp->sct_off = 0;
120     }
121     logerror("done importing\n");
122
123 }
124
125 #ifdef USE_PATH_FIND
126 static int
127 distcmp(const void *p, const void *q)
128 {
129     int a = *(int *)p;
130     int b = *(int *)q;
131     struct sctstr *sp = (void *)empfile[EF_SECTOR].cache;
132     int d;
133
134     d = sp[b].sct_dist_y - sp[a].sct_dist_y;
135     if (d)
136         return d;
137     d = sp[b].sct_dist_x - sp[a].sct_dist_x;
138     if (d)
139         return d;
140     return b - a;
141 }
142 #endif
143
144 static void
145 assemble_dist_paths(double *import_cost)
146 {
147     struct sctstr *sp;
148     struct sctstr *dist;
149     int n;
150 #ifdef USE_PATH_FIND
151     static int *job;
152     int uid, i;
153     coord dx = 1, dy = 0;       /* invalid */
154
155     if (!job)
156         job = malloc(WORLD_SZ() * sizeof(*job));
157
158     n = 0;
159     for (uid = 0; NULL != (sp = getsectid(uid)); uid++) {
160         import_cost[uid] = -1;
161         if (sp->sct_dist_x == sp->sct_x && sp->sct_dist_y == sp->sct_y)
162             continue;
163         job[n++] = uid;
164     }
165
166 #ifdef PATH_FIND_STATS
167     printf("dist path reuse %zu bytes, %d/%d used\n",
168            WORLD_SZ() * sizeof(*job), n, WORLD_SZ());
169 #endif
170
171     qsort(job, n, sizeof(*job), distcmp);
172
173     for (i = 0; i < n; i++) {
174         uid = job[i];
175         sp = getsectid(uid);
176         dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
177         if (CANT_HAPPEN(!dist))
178             continue;
179         if (sp->sct_own != dist->sct_own)
180             continue;
181         if (sp->sct_dist_x != dx || sp->sct_dist_y != dy) {
182             dx = sp->sct_dist_x;
183             dy = sp->sct_dist_y;
184             path_find_from(dx, dy, dist->sct_own, MOB_MOVE);
185         }
186         import_cost[uid] = path_find_to(sp->sct_x, sp->sct_y);
187     }
188     path_find_print_stats();
189 #else   /* !USE_PATH_FIND */
190     char *path;
191     double d;
192     char buf[512];
193
194     for (n = 0; NULL != (sp = getsectid(n)); n++) {
195         import_cost[n] = -1;
196         if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
197             continue;
198         dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
199         if (CANT_HAPPEN(!dist))
200             continue;
201         if (sp->sct_own != dist->sct_own)
202             continue;
203         /* Now, get the best distribution path over roads */
204         /* Note we go from the dist center to the sector.  This gives
205            us the import path for that sector. */
206         path = BestDistPath(buf, dist, sp, &d);
207         if (path)
208             import_cost[n] = d;
209     }
210 #endif  /* !USE_PATH_FIND */
211 }