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