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