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 * as_cache.c: Routines used to create/delete caches of A* paths.
30 * Known contributors to this file:
38 /* The way this works is interesting. :) */
39 /* We keep a pointer to a list of pointers. The index into this list
40 * is the y coordinate of the from sector. This member points to a list
41 * of from sectors on that y coordinate. So, we march that list checking
42 * the x value to find the from x,y we want. */
43 /* Once we find the from x,y, that node has the same type of pointer to
44 * a list of pointers. The index into this list is the y coordinate of
45 * the to sector. This member points to a list of to sectors on that y
46 * coordinate. So, we march that list checking the x value to find the
48 /* These lists are dynamically created since the world is dynamically sized. */
49 /* See, I told you it was interesting. :) */
51 static struct as_frompath **fromhead = (struct as_frompath **)0;
53 /* Note that we only want to cache during updates. Other times, it
54 * probably doesn't make much sense, but can be done. */
56 static int as_cachepath_on = 0; /* Default to off */
59 as_enable_cachepath(void)
65 as_disable_cachepath(void)
70 /* Note we want these to be as fast as possible */
73 as_add_cachepath(struct as_data *adp)
75 struct as_frompath *from;
76 struct as_topath *to = (struct as_topath *)0;
79 /* Don't do anything if we aren't cacheing these */
80 if (as_cachepath_on == 0)
83 /* Note we will only allocate this once. Afterwards, we just keep
84 * zeroing it since it's rather small and we don't need to re-allocate
86 if (fromhead == (struct as_frompath **)0) {
87 fromhead = calloc(1, sizeof(struct as_frompath *) * WORLD_Y);
88 if (fromhead == (struct as_frompath **)0)
93 for (from = fromhead[adp->from.y]; from; from = from->next)
94 if (from->x == adp->from.x)
97 for (to = from->tolist[np->c.y]; to; to = to->next) {
98 if (to->x == np->c.x) {
99 /* It is already here! Don't bother adding it again */
104 /* We must make a new one of these */
105 from = (struct as_frompath *)malloc(sizeof(struct as_frompath));
108 /* And set some stuff */
109 from->x = adp->from.x;
110 /* Here we malloc a whole bunch of tolist pointers. */
111 from->tolist = calloc(1, sizeof(struct as_topath *) * WORLD_Y);
112 /* Now, add from to the global list */
113 from->next = fromhead[adp->from.y];
114 fromhead[adp->from.y] = from;
117 /* We must make a new one */
118 to = (struct as_topath *)malloc(sizeof(struct as_topath));
119 /* We can't, sorry */
122 /* Now set some stuff */
124 /* Now add it to the list we are in */
125 to->next = from->tolist[np->c.y];
126 from->tolist[np->c.y] = to;
128 /* Now, make the path */
130 /* Now, take the path */
131 to->path = adp->path;
132 /* And clear the path in the adp */
137 as_clear_cachepath(void)
139 struct as_frompath *from, *from2;
140 struct as_topath *to, *to2;
143 /* Cache not used yet :) */
144 if (fromhead == (struct as_frompath **)0)
147 for (j = 0; j < WORLD_Y; j++) {
148 for (from = fromhead[j]; from; from = from2) {
149 for (i = 0; i < WORLD_Y; i++) {
150 for (to = from->tolist[i]; to; to = to2) {
153 as_free_path(to->path);
158 /* Now, free the list of lists */
159 free((s_char *)from->tolist);
160 /* Save the next pointer */
162 /* now, free this from node */
163 free((s_char *)from);
166 /* Note we don't free the fromhead here, we just zero it. That way,
167 we can use it next time without mallocing int */
168 memset(fromhead, 0, (sizeof(struct as_frompath *) * WORLD_Y));
172 as_find_cachepath(coord fx, coord fy, coord tx, coord ty)
174 struct as_frompath *from;
175 struct as_topath *to;
177 /* Is the cache on? if not, return NULL */
178 if (as_cachepath_on == 0)
181 /* Do we have any cached? */
182 if (fromhead == (struct as_frompath **)0)
186 for (from = fromhead[fy]; from; from = from->next) {
188 for (to = from->tolist[ty]; to; to = to->next) {