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