]> git.pond.sub.org Git - empserver/blob - src/lib/update/finish.c
7284097845817036a6c3733d34ade06743c91b33
[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  */
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 /* Used for building up distribution info */
45 struct distinfo {
46     char *path;                 /* path to take */
47     double imcost;              /* import cost */
48     double excost;              /* export cost */
49 };
50
51 /* This is our global buffer of distribution pointers.  Note that
52  * We only malloc this once, and never again (until reboot time
53  * of course :) ) We do clear it each and every time. */
54 static struct distinfo *g_distptrs;
55
56 /* Note that even though we malloc and save the path, it is never
57  * used.  Thus, this option.  If you want to malloc and save every
58  * path and then free when done, just enable this.  Or, if the
59  * dodistribute ever uses the path for something other than checking
60  * to see that a path exists, enable this */
61 /* #define SAVE_FINISH_PATHS */
62
63 #ifndef SAVE_FINISH_PATHS
64 static char *finish_path = "h"; /* Placeholder indicating path exists */
65 #endif /* SAVE_FINISH_PATHS */
66
67 static void assemble_dist_paths(struct distinfo *distptrs);
68 static char *ReversePath(char *path);
69
70 void
71 finish_sects(int etu)
72 {
73     struct sctstr *sp;
74     struct natstr *np;
75     int n;
76     struct rusage rus1, rus2;
77     struct distinfo *infptr;
78
79     if (g_distptrs == NULL) {
80         logerror("First update since reboot, allocating buffer\n");
81         /* Allocate the information buffer */
82         g_distptrs = malloc(WORLD_SZ() * sizeof(*g_distptrs));
83         if (g_distptrs == NULL) {
84             logerror("malloc failed in finish_sects.\n");
85             return;
86         }
87     }
88
89     /* Wipe it clean */
90     memset(g_distptrs, 0, WORLD_SZ() * sizeof(*g_distptrs));
91
92     logerror("delivering...\n");
93     /* Do deliveries */
94     for (n = 0; NULL != (sp = getsectid(n)); n++) {
95         if (sp->sct_type == SCT_WATER)
96             continue;
97         if (sp->sct_own == 0)
98             continue;
99         np = getnatp(sp->sct_own);
100         if (np->nat_money < 0)
101             continue;
102         dodeliver(sp);
103     }
104     logerror("done delivering\n");
105
106     logerror("assembling paths...\n");
107     getrusage(RUSAGE_SELF, &rus1);
108
109     /* First, enable the best_path cacheing */
110     bp_enable_cachepath();
111
112     /* Now assemble the paths */
113     assemble_dist_paths(g_distptrs);
114
115     /* Now disable the best_path cacheing */
116     bp_disable_cachepath();
117
118     /* Now, clear the best_path cache that may have been created */
119     bp_clear_cachepath();
120
121     getrusage(RUSAGE_SELF, &rus2);
122     logerror("done assembling paths %g user %g system",
123              rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
124              - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
125              rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
126              - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
127
128     logerror("exporting...");
129     for (n = 0; NULL != (sp = getsectid(n)); n++) {
130         if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
131             continue;
132         np = getnatp(sp->sct_own);
133         if (np->nat_money < 0)
134             continue;
135         /* Get the pointer */
136         infptr = &g_distptrs[sp->sct_uid];
137         dodistribute(sp, EXPORT,
138                      infptr->path, infptr->imcost, infptr->excost);
139     }
140     logerror("done exporting\n");
141
142     /* Note that we free the paths (if allocated) as we loop here */
143     logerror("importing...");
144     for (n = 0; NULL != (sp = getsectid(n)); n++) {
145         /* Get the pointer (we do it first so we can free if needed) */
146         infptr = &g_distptrs[sp->sct_uid];
147         if (sp->sct_type == SCT_WATER || sp->sct_own == 0) {
148 #ifdef SAVE_FINISH_PATHS
149             if (infptr->path)
150                 free(infptr->path);
151 #endif /* SAVE_FINISH_PATHS */
152             continue;
153         }
154         np = getnatp(sp->sct_own);
155         if (np->nat_money < 0) {
156 #ifdef SAVE_FINISH_PATHS
157             if (infptr->path)
158                 free(infptr->path);
159 #endif /* SAVE_FINISH_PATHS */
160             continue;
161         }
162         dodistribute(sp, IMPORT,
163                      infptr->path, infptr->imcost, infptr->excost);
164 #ifdef SAVE_FINISH_PATHS
165         if (infptr->path)
166             free(infptr->path);
167 #endif /* SAVE_FINISH_PATHS */
168
169         sp->sct_off = 0;
170     }
171     logerror("done importing\n");
172
173 }
174
175 static void
176 assemble_dist_paths(struct distinfo *distptrs)
177 {
178     char *path, *p;
179     double d;
180     struct sctstr *sp;
181     struct sctstr *dist;
182     struct distinfo *infptr;
183     int n;
184     char buf[512];
185
186     for (n = 0; NULL != (sp = getsectid(n)); n++) {
187         if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
188             continue;
189         /* Set the pointer */
190         infptr = &distptrs[sp->sct_uid];
191         /* now, get the dist sector */
192         dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
193         if (dist == NULL) {
194             logerror("Bad dist sect %d,%d for %d,%d !\n",
195                      sp->sct_dist_x, sp->sct_dist_y,
196                      sp->sct_x, sp->sct_y);
197             continue;
198         }
199         /* Now, get the best distribution path over roads */
200         /* Note we go from the dist center to the sector.  This gives
201            us the import path for that sector. */
202         path = BestDistPath(buf, dist, sp, &d);
203
204         /* Now, we have a path */
205         if (path != NULL) {
206 #ifdef SAVE_FINISH_PATHS
207             int len;
208             /* Here we malloc a buffer and save it */
209             len = strlen(path);
210             infptr->path = malloc(len);
211             if (infptr->path == NULL) {
212                 logerror("malloc failed in assemble_dist_path!\n");
213                 return;
214             }
215 #endif /* SAVE_FINISH_PATHS */
216             /* Save the import cost */
217             infptr->imcost = d;
218             /* Now, reverse the path */
219             p = ReversePath(path);
220             /* And walk the path back to the dist center to get the export
221                cost */
222             infptr->excost = pathcost(sp, p, MOB_MOVE);
223 #ifdef SAVE_FINISH_PATHS
224             memcpy(infptr->path, p, len);
225 #else
226             infptr->path = finish_path;
227 #endif /* SAVE_FINISH_PATHS */
228         }
229     }
230 }
231
232 static char *
233 ReversePath(char *path)
234 {
235     char *patharray = "aucdefjhigklmyopqrstbvwxnz";
236     static char new_path[512];
237     int ind;
238
239     if (path == NULL)
240         return NULL;
241
242     ind = strlen(path);
243     if (ind == 0)
244         return NULL;
245
246     if (path[ind - 1] == 'h')
247         ind--;
248
249     new_path[ind--] = '\0';
250     new_path[ind] = '\0';
251
252     while (ind >= 0) {
253         new_path[ind--] = patharray[*(path++) - 'a'];
254     }
255
256     return new_path;
257 }