]> git.pond.sub.org Git - empserver/blob - src/lib/update/finish.c
SAVE_FINISH_PATHS hasn't been used since 4.2.2, remove it
[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 static char *finish_path = "h"; /* Placeholder indicating path exists */
57
58 static void assemble_dist_paths(struct distinfo *distptrs);
59 static char *ReversePath(char *path);
60
61 void
62 finish_sects(int etu)
63 {
64     struct sctstr *sp;
65     struct natstr *np;
66     int n;
67     struct rusage rus1, rus2;
68     struct distinfo *infptr;
69
70     if (g_distptrs == NULL) {
71         logerror("First update since reboot, allocating buffer\n");
72         /* Allocate the information buffer */
73         g_distptrs = malloc(WORLD_SZ() * sizeof(*g_distptrs));
74         if (g_distptrs == NULL) {
75             logerror("malloc failed in finish_sects.\n");
76             return;
77         }
78     }
79
80     /* Wipe it clean */
81     memset(g_distptrs, 0, WORLD_SZ() * sizeof(*g_distptrs));
82
83     logerror("delivering...\n");
84     /* Do deliveries */
85     for (n = 0; NULL != (sp = getsectid(n)); n++) {
86         if (sp->sct_type == SCT_WATER)
87             continue;
88         if (sp->sct_own == 0)
89             continue;
90         np = getnatp(sp->sct_own);
91         if (np->nat_money < 0)
92             continue;
93         dodeliver(sp);
94     }
95     logerror("done delivering\n");
96
97     logerror("assembling paths...\n");
98     getrusage(RUSAGE_SELF, &rus1);
99
100     /* First, enable the best_path cacheing */
101     bp_enable_cachepath();
102
103     /* Now assemble the paths */
104     assemble_dist_paths(g_distptrs);
105
106     /* Now disable the best_path cacheing */
107     bp_disable_cachepath();
108
109     /* Now, clear the best_path cache that may have been created */
110     bp_clear_cachepath();
111
112     getrusage(RUSAGE_SELF, &rus2);
113     logerror("done assembling paths %g user %g system",
114              rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
115              - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
116              rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
117              - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
118
119     logerror("exporting...");
120     for (n = 0; NULL != (sp = getsectid(n)); n++) {
121         if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
122             continue;
123         np = getnatp(sp->sct_own);
124         if (np->nat_money < 0)
125             continue;
126         /* Get the pointer */
127         infptr = &g_distptrs[sp->sct_uid];
128         dodistribute(sp, EXPORT,
129                      infptr->path, infptr->imcost, infptr->excost);
130     }
131     logerror("done exporting\n");
132
133     logerror("importing...");
134     for (n = 0; NULL != (sp = getsectid(n)); n++) {
135         /* Get the pointer (we do it first so we can free if needed) */
136         infptr = &g_distptrs[sp->sct_uid];
137         if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
138             continue;
139         np = getnatp(sp->sct_own);
140         if (np->nat_money < 0)
141             continue;
142         dodistribute(sp, IMPORT,
143                      infptr->path, infptr->imcost, infptr->excost);
144         sp->sct_off = 0;
145     }
146     logerror("done importing\n");
147
148 }
149
150 static void
151 assemble_dist_paths(struct distinfo *distptrs)
152 {
153     char *path, *p;
154     double d;
155     struct sctstr *sp;
156     struct sctstr *dist;
157     struct distinfo *infptr;
158     int n;
159     char buf[512];
160
161     for (n = 0; NULL != (sp = getsectid(n)); n++) {
162         if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
163             continue;
164         /* Set the pointer */
165         infptr = &distptrs[sp->sct_uid];
166         /* now, get the dist sector */
167         dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
168         if (dist == NULL) {
169             logerror("Bad dist sect %d,%d for %d,%d !\n",
170                      sp->sct_dist_x, sp->sct_dist_y,
171                      sp->sct_x, sp->sct_y);
172             continue;
173         }
174         /* Now, get the best distribution path over roads */
175         /* Note we go from the dist center to the sector.  This gives
176            us the import path for that sector. */
177         path = BestDistPath(buf, dist, sp, &d);
178
179         /* Now, we have a path */
180         if (path != NULL) {
181             /* Save the import cost */
182             infptr->imcost = d;
183             /* Now, reverse the path */
184             p = ReversePath(path);
185             /* And walk the path back to the dist center to get the export
186                cost */
187             infptr->excost = pathcost(sp, p, MOB_MOVE);
188             infptr->path = finish_path;
189         }
190     }
191 }
192
193 static char *
194 ReversePath(char *path)
195 {
196     char *patharray = "aucdefjhigklmyopqrstbvwxnz";
197     static char new_path[512];
198     int ind;
199
200     if (path == NULL)
201         return NULL;
202
203     ind = strlen(path);
204     if (ind == 0)
205         return NULL;
206
207     if (path[ind - 1] == 'h')
208         ind--;
209
210     new_path[ind--] = '\0';
211     new_path[ind] = '\0';
212
213     while (ind >= 0) {
214         new_path[ind--] = patharray[*(path++) - 'a'];
215     }
216
217     return new_path;
218 }