]> 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-2007, 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 "distribute.h"
39 #include "path.h"
40 #include "update.h"
41 #include <stdlib.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 = (struct distinfo *)(malloc((WORLD_X * WORLD_Y) *
81                                                 sizeof(struct distinfo)));
82         if (g_distptrs == NULL) {
83             logerror("malloc failed in finish_sects.\n");
84             return;
85         }
86
87         logerror("Allocated '%lu' bytes '%d' indices\n",
88                  (unsigned long)(WORLD_X * WORLD_Y * sizeof(struct distinfo)),
89                  WORLD_X * WORLD_Y);
90     }
91
92     /* Wipe it clean */
93     memset(g_distptrs, 0, ((WORLD_X * WORLD_Y) * sizeof(struct distinfo)));
94
95     logerror("delivering...\n");
96     /* Do deliveries */
97     for (n = 0; NULL != (sp = getsectid(n)); n++) {
98         if (sp->sct_type == SCT_WATER)
99             continue;
100         if (sp->sct_own == 0)
101             continue;
102         np = getnatp(sp->sct_own);
103         if (np->nat_money < 0)
104             continue;
105         dodeliver(sp);
106     }
107     logerror("done delivering\n");
108
109     logerror("assembling paths...\n");
110
111     /* First, enable the best_path cacheing */
112     bp_enable_cachepath();
113
114     /* Now assemble the paths */
115     assemble_dist_paths(g_distptrs);
116
117     /* Now disable the best_path cacheing */
118     bp_disable_cachepath();
119
120     /* Now, clear the best_path cache that may have been created */
121     bp_clear_cachepath();
122
123     logerror("done assembling paths\n");
124
125     logerror("exporting...");
126     for (n = 0; NULL != (sp = getsectid(n)); n++) {
127         if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
128             continue;
129         np = getnatp(sp->sct_own);
130         if (np->nat_money < 0)
131             continue;
132         /* Get the pointer */
133         infptr = &g_distptrs[XYOFFSET(sp->sct_x, sp->sct_y)];
134         dodistribute(sp, EXPORT,
135                      infptr->path, infptr->imcost, infptr->excost);
136     }
137     logerror("done exporting\n");
138
139     /* Note that we free the paths (if allocated) as we loop here */
140     logerror("importing...");
141     for (n = 0; NULL != (sp = getsectid(n)); n++) {
142         /* Get the pointer (we do it first so we can free if needed) */
143         infptr = &g_distptrs[XYOFFSET(sp->sct_x, sp->sct_y)];
144         if (sp->sct_type == SCT_WATER || sp->sct_own == 0) {
145 #ifdef SAVE_FINISH_PATHS
146             if (infptr->path)
147                 free(infptr->path);
148 #endif /* SAVE_FINISH_PATHS */
149             continue;
150         }
151         np = getnatp(sp->sct_own);
152         if (np->nat_money < 0) {
153 #ifdef SAVE_FINISH_PATHS
154             if (infptr->path)
155                 free(infptr->path);
156 #endif /* SAVE_FINISH_PATHS */
157             continue;
158         }
159         dodistribute(sp, IMPORT,
160                      infptr->path, infptr->imcost, infptr->excost);
161 #ifdef SAVE_FINISH_PATHS
162         if (infptr->path)
163             free(infptr->path);
164 #endif /* SAVE_FINISH_PATHS */
165
166         sp->sct_off = 0;
167     }
168     logerror("done importing\n");
169
170 }
171
172 static void
173 assemble_dist_paths(struct distinfo *distptrs)
174 {
175     char *path, *p;
176     double d;
177     struct sctstr *sp;
178     struct sctstr *dist;
179     struct distinfo *infptr;
180     int n;
181     char buf[512];
182
183     for (n = 0; NULL != (sp = getsectid(n)); n++) {
184         if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
185             continue;
186         /* Set the pointer */
187         infptr = &distptrs[XYOFFSET(sp->sct_x, sp->sct_y)];
188         /* now, get the dist sector */
189         dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
190         if (dist == NULL) {
191             logerror("Bad dist sect %d,%d for %d,%d !\n",
192                      sp->sct_dist_x, sp->sct_dist_y,
193                      sp->sct_x, sp->sct_y);
194             continue;
195         }
196         /* Now, get the best distribution path over roads */
197         /* Note we go from the dist center to the sector.  This gives
198            us the import path for that sector. */
199         path = BestDistPath(buf, dist, sp, &d);
200
201         /* Now, we have a path */
202         if (path != NULL) {
203 #ifdef SAVE_FINISH_PATHS
204             int len;
205             /* Here we malloc a buffer and save it */
206             len = strlen(path);
207             infptr->path = malloc(len);
208             if (infptr->path == NULL) {
209                 logerror("malloc failed in assemble_dist_path!\n");
210                 return;
211             }
212 #endif /* SAVE_FINISH_PATHS */
213             /* Save the import cost */
214             infptr->imcost = d;
215             /* Now, reverse the path */
216             p = ReversePath(path);
217             /* And walk the path back to the dist center to get the export
218                cost */
219             infptr->excost = pathcost(sp, p, MOB_MOVE);
220 #ifdef SAVE_FINISH_PATHS
221             memcpy(infptr->path, p, len);
222 #else
223             infptr->path = finish_path;
224 #endif /* SAVE_FINISH_PATHS */
225         }
226     }
227 }
228
229 static char *
230 ReversePath(char *path)
231 {
232     char *patharray = "aucdefjhigklmyopqrstbvwxnz";
233     static char new_path[512];
234     int ind;
235
236     if (path == NULL)
237         return NULL;
238
239     ind = strlen(path);
240     if (ind == 0)
241         return NULL;
242
243     if (path[ind - 1] == 'h')
244         ind--;
245
246     new_path[ind--] = '\0';
247     new_path[ind] = '\0';
248
249     while (ind >= 0) {
250         new_path[ind--] = patharray[*(path++) - 'a'];
251     }
252
253     return new_path;
254 }