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
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include <config.h>
00047
00048 #include "file.h"
00049 #include "misc.h"
00050 #include "nat.h"
00051 #include "optlist.h"
00052 #include "path.h"
00053 #include "prototypes.h"
00054 #include "sect.h"
00055 #include "xy.h"
00056
00057 static int owned_and_navigable(char *, int, int, int);
00058
00059 #define MAXROUTE 100
00060 #define valid(x,y) ((((x) ^ (y)) & 1) == 0)
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 static unsigned short *mapbuf;
00071 static unsigned short **mapindex;
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 char *
00087 bestownedpath(char *bpath, char *bigmap,
00088 int x, int y, int ex, int ey, int own)
00089 {
00090 int i, j, tx, ty, markedsectors;
00091 int minx, maxx, miny, maxy, scanx, scany;
00092 unsigned routelen;
00093
00094 if (!mapbuf)
00095 mapbuf = malloc(WORLD_X * WORLD_Y * sizeof(*mapbuf));
00096 if (!mapbuf)
00097 return NULL;
00098 if (!mapindex) {
00099 mapindex = malloc(WORLD_X * sizeof(*mapindex));
00100 if (mapindex) {
00101
00102 for (i = 0; i < WORLD_X; i++)
00103 mapindex[i] = &mapbuf[WORLD_Y * i];
00104 }
00105 }
00106 if (!mapindex)
00107 return NULL;
00108
00109 x = XNORM(x);
00110 y = YNORM(y);
00111 ex = XNORM(ex);
00112 ey = YNORM(ey);
00113
00114 if (x == ex && y == ey)
00115 return "h";
00116
00117 if (!valid(x, y) || !valid(ex, ey))
00118 return NULL;
00119 if (!owned_and_navigable(bigmap, ex, ey, own))
00120 return NULL;
00121
00122 for (i = 0; i < WORLD_X; i++)
00123 for (j = 0; j < WORLD_Y; j++)
00124 mapindex[i][j] = 0xFFFF;
00125
00126 routelen = 0;
00127 mapindex[x][y] = 0;
00128 markedsectors = 1;
00129 minx = x - 2;
00130 maxx = x + 2;
00131 miny = y - 1;
00132 maxy = y + 1;
00133
00134 do {
00135 if (++routelen == MAXROUTE)
00136 return NULL;
00137 markedsectors = 0;
00138 for (scanx = minx; scanx <= maxx; scanx++) {
00139 x = XNORM(scanx);
00140 for (scany = miny; scany <= maxy; scany++) {
00141 y = YNORM(scany);
00142 if (!valid(x, y))
00143 continue;
00144 if (((mapindex[x][y] & 0x1FFF) == routelen - 1)) {
00145 for (i = DIR_FIRST; i <= DIR_LAST; i++) {
00146 tx = x + diroff[i][0];
00147 ty = y + diroff[i][1];
00148 tx = XNORM(tx);
00149 ty = YNORM(ty);
00150 if (mapindex[tx][ty] == 0xFFFF) {
00151 if (owned_and_navigable(bigmap, tx, ty, own)) {
00152 if (CANT_HAPPEN(i < DIR_FIRST || i > DIR_LAST))
00153 i = DIR_STOP;
00154 mapindex[tx][ty] =
00155 ((i - DIR_FIRST + 1) << 13) + routelen;
00156 markedsectors++;
00157 }
00158 }
00159 if (tx == ex && ty == ey) {
00160 bpath[routelen] = 0;
00161 while (routelen--) {
00162 i = (mapindex[tx][ty] >> 13)
00163 - 1 + DIR_FIRST;
00164 bpath[routelen] = dirch[i];
00165 tx = tx - diroff[i][0];
00166 ty = ty - diroff[i][1];
00167 tx = XNORM(tx);
00168 ty = YNORM(ty);
00169 }
00170 return bpath;
00171 }
00172 }
00173 }
00174 }
00175 }
00176 miny--;
00177 maxy++;
00178 minx -= 2;
00179 maxx += 2;
00180 } while (markedsectors);
00181
00182 return NULL;
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 static int
00195 owned_and_navigable(char *bigmap, int x, int y, int own)
00196 {
00197 char mapspot;
00198 struct sctstr sect;
00199
00200 if (!bigmap)
00201 return 1;
00202
00203
00204 getsect(x, y, §);
00205 if (sect.sct_own == own
00206 || (sect.sct_own && getrel(getnatp(sect.sct_own), own) == ALLIED)) {
00207
00208 switch (dchr[sect.sct_type].d_nav) {
00209 case NAVOK:
00210 return 1;
00211 case NAV_CANAL:
00212
00213 return 0;
00214 case NAV_02:
00215 return sect.sct_effic >= 2;
00216 case NAV_60:
00217 return sect.sct_effic >= 60;
00218 default:
00219 return 0;
00220 }
00221 }
00222
00223
00224 mapspot = bigmap[sctoff(x, y)];
00225 return mapspot == '.' || mapspot == ' ' || mapspot == 0;
00226 }