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