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 #include <config.h>
00035
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include "as.h"
00039 #include "optlist.h"
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 static struct as_frompath **fromhead = (struct as_frompath **)0;
00055
00056
00057
00058
00059 static int as_cachepath_on = 0;
00060
00061 void
00062 as_enable_cachepath(void)
00063 {
00064 as_cachepath_on = 1;
00065 }
00066
00067 void
00068 as_disable_cachepath(void)
00069 {
00070 as_cachepath_on = 0;
00071 }
00072
00073
00074
00075 void
00076 as_add_cachepath(struct as_data *adp)
00077 {
00078 struct as_frompath *from;
00079 struct as_topath *to = (struct as_topath *)0;
00080 struct as_node *np;
00081
00082
00083 if (as_cachepath_on == 0)
00084 return;
00085
00086
00087
00088
00089 if (fromhead == NULL) {
00090 fromhead = calloc(1, sizeof(struct as_frompath *) * WORLD_Y);
00091 if (fromhead == NULL)
00092 return;
00093 }
00094
00095 np = adp->head->np;
00096 for (from = fromhead[adp->from.y]; from; from = from->next)
00097 if (from->x == adp->from.x)
00098 break;
00099 if (from) {
00100 for (to = from->tolist[np->c.y]; to; to = to->next) {
00101 if (to->x == np->c.x) {
00102
00103 return;
00104 }
00105 }
00106 } else {
00107
00108 from = malloc(sizeof(struct as_frompath));
00109 if (from == NULL)
00110 return;
00111
00112 from->x = adp->from.x;
00113
00114 from->tolist = calloc(1, sizeof(struct as_topath *) * WORLD_Y);
00115
00116 from->next = fromhead[adp->from.y];
00117 fromhead[adp->from.y] = from;
00118 }
00119 if (!to) {
00120
00121 to = malloc(sizeof(struct as_topath));
00122
00123 if (to == NULL)
00124 return;
00125
00126 to->x = np->c.x;
00127
00128 to->next = from->tolist[np->c.y];
00129 from->tolist[np->c.y] = to;
00130 }
00131
00132 as_makepath(adp);
00133
00134 to->path = adp->path;
00135
00136 adp->path = NULL;
00137 }
00138
00139 void
00140 as_clear_cachepath(void)
00141 {
00142 struct as_frompath *from, *from2;
00143 struct as_topath *to, *to2;
00144 int i, j;
00145
00146
00147 if (fromhead == NULL)
00148 return;
00149
00150 for (j = 0; j < WORLD_Y; j++) {
00151 for (from = fromhead[j]; from; from = from2) {
00152 for (i = 0; i < WORLD_Y; i++) {
00153 for (to = from->tolist[i]; to; to = to2) {
00154 to2 = to->next;
00155
00156 as_free_path(to->path);
00157
00158 free(to);
00159 }
00160 }
00161
00162 free(from->tolist);
00163
00164 from2 = from->next;
00165
00166 free(from);
00167 }
00168 }
00169
00170
00171 memset(fromhead, 0, (sizeof(struct as_frompath *) * WORLD_Y));
00172 }
00173
00174 struct as_path *
00175 as_find_cachepath(coord fx, coord fy, coord tx, coord ty)
00176 {
00177 struct as_frompath *from;
00178 struct as_topath *to;
00179
00180
00181 if (as_cachepath_on == 0)
00182 return NULL;
00183
00184
00185 if (fromhead == NULL)
00186 return NULL;
00187
00188
00189 for (from = fromhead[fy]; from; from = from->next) {
00190 if (from->x == fx) {
00191 for (to = from->tolist[ty]; to; to = to->next) {
00192 if (to->x == tx)
00193 return to->path;
00194 }
00195 }
00196 }
00197 return NULL;
00198 }