2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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
42 #include "distribute.h"
47 /* Used for building up distribution info */
49 s_char *path; /* path to take */
50 double imcost; /* import cost */
51 double excost; /* export cost */
54 /* This is our global buffer of distribution pointers. Note that
55 * We only malloc this once, and never again (until reboot time
56 * of course :) ) We do clear it each and every time. */
57 struct distinfo *g_distptrs = (struct distinfo *)0;
59 /* Note that even though we malloc and save the path, it is never
60 * used. Thus, this option. If you want to malloc and save every
61 * path and then free when done, just enable this. Or, if the
62 * dodistribute ever uses the path for something other than checking
63 * to see that a path exists, enable this */
64 /* #define SAVE_FINISH_PATHS */
66 #ifndef SAVE_FINISH_PATHS
67 static s_char *finish_path = "h"; /* Placeholder indicating path exists */
68 #endif /* SAVE_FINISH_PATHS */
70 static void assemble_dist_paths(struct distinfo *distptrs);
71 static s_char *ReversePath(s_char *path);
79 struct distinfo *infptr;
81 if (g_distptrs == (struct distinfo *)0) {
82 logerror("First update since reboot, allocating buffer\n");
83 /* Allocate the information buffer */
84 g_distptrs = (struct distinfo *)(malloc((WORLD_X * WORLD_Y) *
85 sizeof(struct distinfo)));
86 if (g_distptrs == (struct distinfo *)0) {
87 logerror("malloc failed in finish_sects.\n");
91 logerror("Allocated '%lu' bytes '%d' indices\n",
92 (unsigned long)(WORLD_X * WORLD_Y * sizeof(struct distinfo)),
97 memset(g_distptrs, 0, ((WORLD_X * WORLD_Y) * sizeof(struct distinfo)));
99 logerror("delivering...\n");
101 for (n = 0; NULL != (sp = getsectid(n)); n++) {
102 if (sp->sct_type == SCT_WATER)
104 if (sp->sct_own == 0)
106 np = getnatp(sp->sct_own);
107 if (np->nat_money < 0)
111 logerror("done delivering\n");
113 logerror("assembling paths...\n");
115 /* First, enable the best_path cacheing */
116 bp_enable_cachepath();
118 /* Now assemble the paths */
119 assemble_dist_paths(g_distptrs);
121 /* Now disable the best_path cacheing */
122 bp_disable_cachepath();
124 /* Now, clear the best_path cache that may have been created */
125 bp_clear_cachepath();
127 logerror("done assembling paths\n");
129 logerror("exporting...");
130 for (n = 0; NULL != (sp = getsectid(n)); n++) {
131 if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
133 np = getnatp(sp->sct_own);
134 if (np->nat_money < 0)
136 /* Get the pointer */
137 infptr = &g_distptrs[XYOFFSET(sp->sct_x, sp->sct_y)];
138 dodistribute(sp, EXPORT,
139 infptr->path, infptr->imcost, infptr->excost);
141 logerror("done exporting\n");
143 /* Note that we free the paths (if allocated) as we loop here */
144 logerror("importing...");
145 for (n = 0; NULL != (sp = getsectid(n)); n++) {
146 /* Get the pointer (we do it first so we can free if needed) */
147 infptr = &g_distptrs[XYOFFSET(sp->sct_x, sp->sct_y)];
148 if (sp->sct_type == SCT_WATER || sp->sct_own == 0) {
149 #ifdef SAVE_FINISH_PATHS
151 free((s_char *)infptr->path);
152 #endif /* SAVE_FINISH_PATHS */
155 np = getnatp(sp->sct_own);
156 if (np->nat_money < 0) {
157 #ifdef SAVE_FINISH_PATHS
159 free((s_char *)infptr->path);
160 #endif /* SAVE_FINISH_PATHS */
163 dodistribute(sp, IMPORT,
164 infptr->path, infptr->imcost, infptr->excost);
165 #ifdef SAVE_FINISH_PATHS
167 free((s_char *)infptr->path);
168 #endif /* SAVE_FINISH_PATHS */
170 logerror("done importing\n");
175 assemble_dist_paths(struct distinfo *distptrs)
181 struct distinfo *infptr;
185 for (n = 0; NULL != (sp = getsectid(n)); n++) {
186 if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
188 /* Set the pointer */
189 infptr = &distptrs[XYOFFSET(sp->sct_x, sp->sct_y)];
190 /* now, get the dist sector */
191 dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
192 if (dist == (struct sctstr *)0) {
193 logerror("Bad dist sect %d,%d for %d,%d !\n", sp->sct_dist_x,
194 sp->sct_dist_y, sp->sct_x, sp->sct_y);
197 /* Now, get the best distribution path over roads */
198 /* Note we go from the dist center to the sector. This gives
199 us the import path for that sector. */
200 path = BestDistPath(buf, dist, sp, &d, MOB_ROAD);
202 /* Now, we have a path */
203 if (path != (s_char *)0) {
204 #ifdef SAVE_FINISH_PATHS
206 /* Here we malloc a buffer and save it */
208 infptr->path = (s_char *)malloc(len);
209 if (infptr->path == (s_char *)0) {
210 logerror("malloc failed in assemble_dist_path!\n");
213 #endif /* SAVE_FINISH_PATHS */
214 /* Save the import cost */
216 /* Now, reverse the path */
217 p = ReversePath(path);
218 /* And walk the path back to the dist center to get the export
220 infptr->excost = pathcost(sp, p, MOB_ROAD);
221 #ifdef SAVE_FINISH_PATHS
222 memcpy(infptr->path, p, len);
224 infptr->path = finish_path;
225 #endif /* SAVE_FINISH_PATHS */
231 ReversePath(s_char *path)
233 s_char *patharray = "aucdefjhigklmyopqrstbvwxnz";
234 static s_char new_path[512];
237 if (path == (s_char *)0)
244 if (path[ind - 1] == 'h')
247 new_path[ind--] = '\0';
248 new_path[ind] = '\0';
251 new_path[ind--] = patharray[*(path++) - 'a'];