00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <config.h>
00037
00038 #include "distribute.h"
00039 #include "path.h"
00040 #include "update.h"
00041 #include <stdlib.h>
00042
00043
00044 struct distinfo {
00045 char *path;
00046 double imcost;
00047 double excost;
00048 };
00049
00050
00051
00052
00053 static struct distinfo *g_distptrs;
00054
00055
00056
00057
00058
00059
00060
00061
00062 #ifndef SAVE_FINISH_PATHS
00063 static char *finish_path = "h";
00064 #endif
00065
00066 static void assemble_dist_paths(struct distinfo *distptrs);
00067 static char *ReversePath(char *path);
00068
00069 void
00070 finish_sects(int etu)
00071 {
00072 struct sctstr *sp;
00073 struct natstr *np;
00074 int n;
00075 struct distinfo *infptr;
00076
00077 if (g_distptrs == NULL) {
00078 logerror("First update since reboot, allocating buffer\n");
00079
00080 g_distptrs = malloc(WORLD_SZ() * sizeof(*g_distptrs));
00081 if (g_distptrs == NULL) {
00082 logerror("malloc failed in finish_sects.\n");
00083 return;
00084 }
00085 }
00086
00087
00088 memset(g_distptrs, 0, WORLD_SZ() * sizeof(*g_distptrs));
00089
00090 logerror("delivering...\n");
00091
00092 for (n = 0; NULL != (sp = getsectid(n)); n++) {
00093 if (sp->sct_type == SCT_WATER)
00094 continue;
00095 if (sp->sct_own == 0)
00096 continue;
00097 np = getnatp(sp->sct_own);
00098 if (np->nat_money < 0)
00099 continue;
00100 dodeliver(sp);
00101 }
00102 logerror("done delivering\n");
00103
00104 logerror("assembling paths...\n");
00105
00106
00107 bp_enable_cachepath();
00108
00109
00110 assemble_dist_paths(g_distptrs);
00111
00112
00113 bp_disable_cachepath();
00114
00115
00116 bp_clear_cachepath();
00117
00118 logerror("done assembling paths\n");
00119
00120 logerror("exporting...");
00121 for (n = 0; NULL != (sp = getsectid(n)); n++) {
00122 if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
00123 continue;
00124 np = getnatp(sp->sct_own);
00125 if (np->nat_money < 0)
00126 continue;
00127
00128 infptr = &g_distptrs[XYOFFSET(sp->sct_x, sp->sct_y)];
00129 dodistribute(sp, EXPORT,
00130 infptr->path, infptr->imcost, infptr->excost);
00131 }
00132 logerror("done exporting\n");
00133
00134
00135 logerror("importing...");
00136 for (n = 0; NULL != (sp = getsectid(n)); n++) {
00137
00138 infptr = &g_distptrs[XYOFFSET(sp->sct_x, sp->sct_y)];
00139 if (sp->sct_type == SCT_WATER || sp->sct_own == 0) {
00140 #ifdef SAVE_FINISH_PATHS
00141 if (infptr->path)
00142 free(infptr->path);
00143 #endif
00144 continue;
00145 }
00146 np = getnatp(sp->sct_own);
00147 if (np->nat_money < 0) {
00148 #ifdef SAVE_FINISH_PATHS
00149 if (infptr->path)
00150 free(infptr->path);
00151 #endif
00152 continue;
00153 }
00154 dodistribute(sp, IMPORT,
00155 infptr->path, infptr->imcost, infptr->excost);
00156 #ifdef SAVE_FINISH_PATHS
00157 if (infptr->path)
00158 free(infptr->path);
00159 #endif
00160
00161 sp->sct_off = 0;
00162 }
00163 logerror("done importing\n");
00164
00165 }
00166
00167 static void
00168 assemble_dist_paths(struct distinfo *distptrs)
00169 {
00170 char *path, *p;
00171 double d;
00172 struct sctstr *sp;
00173 struct sctstr *dist;
00174 struct distinfo *infptr;
00175 int n;
00176 char buf[512];
00177
00178 for (n = 0; NULL != (sp = getsectid(n)); n++) {
00179 if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
00180 continue;
00181
00182 infptr = &distptrs[XYOFFSET(sp->sct_x, sp->sct_y)];
00183
00184 dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
00185 if (dist == NULL) {
00186 logerror("Bad dist sect %d,%d for %d,%d !\n",
00187 sp->sct_dist_x, sp->sct_dist_y,
00188 sp->sct_x, sp->sct_y);
00189 continue;
00190 }
00191
00192
00193
00194 path = BestDistPath(buf, dist, sp, &d);
00195
00196
00197 if (path != NULL) {
00198 #ifdef SAVE_FINISH_PATHS
00199 int len;
00200
00201 len = strlen(path);
00202 infptr->path = malloc(len);
00203 if (infptr->path == NULL) {
00204 logerror("malloc failed in assemble_dist_path!\n");
00205 return;
00206 }
00207 #endif
00208
00209 infptr->imcost = d;
00210
00211 p = ReversePath(path);
00212
00213
00214 infptr->excost = pathcost(sp, p, MOB_MOVE);
00215 #ifdef SAVE_FINISH_PATHS
00216 memcpy(infptr->path, p, len);
00217 #else
00218 infptr->path = finish_path;
00219 #endif
00220 }
00221 }
00222 }
00223
00224 static char *
00225 ReversePath(char *path)
00226 {
00227 char *patharray = "aucdefjhigklmyopqrstbvwxnz";
00228 static char new_path[512];
00229 int ind;
00230
00231 if (path == NULL)
00232 return NULL;
00233
00234 ind = strlen(path);
00235 if (ind == 0)
00236 return NULL;
00237
00238 if (path[ind - 1] == 'h')
00239 ind--;
00240
00241 new_path[ind--] = '\0';
00242 new_path[ind] = '\0';
00243
00244 while (ind >= 0) {
00245 new_path[ind--] = patharray[*(path++) - 'a'];
00246 }
00247
00248 return new_path;
00249 }