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
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.
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.
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
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.
28 * finish.c: Finish the update
30 * Known contributors to this file:
32 * Thomas Ruschak, 1993
46 #include "distribute.h"
51 /* Used for building up distribution info */
53 s_char *path; /* path to take */
54 double imcost; /* import cost */
55 double excost; /* export cost */
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;
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 */
70 #ifndef SAVE_FINISH_PATHS
71 static s_char *finish_path = "h"; /* Placeholder indicating path exists */
72 #endif /* SAVE_FINISH_PATHS */
74 static void assemble_dist_paths(struct distinfo *distptrs);
75 s_char *BestDistPath();
76 s_char *ReversePath(s_char *path);
82 register struct sctstr *sp;
87 struct distinfo *infptr;
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");
99 logerror("Allocated '%d' bytes '%d' indices\n",
100 ((WORLD_X * WORLD_Y) * sizeof(struct distinfo)),
101 (WORLD_X * WORLD_Y));
106 bzero((s_char *)g_distptrs, ((WORLD_X * WORLD_Y) *
107 sizeof(struct distinfo)));
109 logerror("delivering...\n");
111 for (n=0; NULL != (sp = getsectid(n)); n++) {
112 if (sp->sct_type == SCT_WATER)
114 if (sp->sct_own == 0)
116 np = getnatp(sp->sct_own);
117 if (np->nat_money < 0)
120 if (getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR) > 0)
121 changed += dodeliver(sp, vec);
123 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
125 logerror("done delivering\n");
127 logerror("assembling paths...\n");
129 /* First, enable the best_path cacheing */
130 bp_enable_cachepath();
132 /* Now assemble the paths */
133 assemble_dist_paths(g_distptrs);
135 /* Now disable the best_path cacheing */
136 bp_disable_cachepath();
138 /* Now, clear the best_path cache that may have been created */
139 bp_clear_cachepath();
141 logerror("done assembling paths\n");
143 logerror("exporting...");
144 for (n=0; NULL != (sp = getsectid(n)); n++) {
145 if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
147 np = getnatp(sp->sct_own);
148 if (np->nat_money < 0)
150 /* Get the pointer */
151 infptr = &g_distptrs[XYOFFSET(sp->sct_x, sp->sct_y)];
152 dodistribute(sp, EXPORT,
153 infptr->path, infptr->imcost, infptr->excost);
155 logerror("done exporting\n");
157 /* Note that we free the paths (if allocated) as we loop here */
158 logerror("importing...");
159 for (n=0; NULL != (sp = getsectid(n)); n++) {
160 /* Get the pointer (we do it first so we can free if needed) */
161 infptr = &g_distptrs[XYOFFSET(sp->sct_x, sp->sct_y)];
162 if (sp->sct_type == SCT_WATER || sp->sct_own == 0) {
163 #ifdef SAVE_FINISH_PATHS
165 free((s_char *)infptr->path);
166 #endif /* SAVE_FINISH_PATHS */
169 np = getnatp(sp->sct_own);
170 if (np->nat_money < 0) {
171 #ifdef SAVE_FINISH_PATHS
173 free((s_char *)infptr->path);
174 #endif /* SAVE_FINISH_PATHS */
177 dodistribute(sp, IMPORT,
178 infptr->path, infptr->imcost, infptr->excost);
179 #ifdef SAVE_FINISH_PATHS
181 free((s_char *)infptr->path);
182 #endif /* SAVE_FINISH_PATHS */
184 logerror("done importing\n");
189 assemble_dist_paths(struct distinfo *distptrs)
195 struct distinfo *infptr;
199 for (n=0; NULL != (sp = getsectid(n)); n++) {
200 if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
202 /* Set the pointer */
203 infptr = &distptrs[XYOFFSET(sp->sct_x, sp->sct_y)];
204 /* now, get the dist sector */
205 dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
206 if (dist == (struct sctstr *)0){
207 logerror("Bad dist sect %d,%d for %d,%d !\n",sp->sct_dist_x,
208 sp->sct_dist_y,sp->sct_x,sp->sct_y);
211 /* Now, get the best distribution path over roads */
212 /* Note we go from the dist center to the sector. This gives
213 us the import path for that sector. */
214 path = BestDistPath(buf, dist, sp, &d, MOB_ROAD);
216 /* Now, we have a path */
217 if (path != (s_char *)0) {
218 #ifdef SAVE_FINISH_PATHS
220 /* Here we malloc a buffer and save it */
222 infptr->path = (s_char *)malloc(len);
223 if (infptr->path == (s_char *)0) {
224 logerror("malloc failed in assemble_dist_path!\n");
227 #endif /* SAVE_FINISH_PATHS */
228 /* Save the import cost */
230 /* Now, reverse the path */
231 p = ReversePath(path);
232 /* And walk the path back to the dist center to get the export
234 infptr->excost = pathcost(sp, p, MOB_ROAD);
235 #ifdef SAVE_FINISH_PATHS
236 bcopy(p, infptr->path, len);
238 infptr->path = finish_path;
239 #endif /* SAVE_FINISH_PATHS */
245 *ReversePath(s_char *path)
247 s_char *patharray = "aucdefjhigklmyopqrstbvwxnz";
248 static s_char new_path[512];
251 if (path == (s_char *)0)
258 if (path[ind - 1] == 'h')
261 new_path[ind--] = '\0';
262 new_path[ind] = '\0';
265 new_path[ind--] = patharray[*(path++) - 'a'];