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