2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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:
40 /* The way this works is interesting. :) */
41 /* We keep a pointer to a list of pointers. The index into this list
42 * is the y coordinate of the from sector. This member points to a list
43 * of from sectors on that y coordinate. So, we march that list checking
44 * the x value to find the from x,y we want. */
45 /* Once we find the from x,y, that node has the same type of pointer to
46 * a list of pointers. The index into this list is the y coordinate of
47 * the to sector. This member points to a list of to sectors on that y
48 * coordinate. So, we march that list checking the x value to find the
50 /* These lists are dynamically created since the world is dynamically sized. */
51 /* See, I told you it was interesting. :) */
53 static struct as_frompath **fromhead = (struct as_frompath **)0;
55 /* Note that we only want to cache during updates. Other times, it
56 * probably doesn't make much sense, but can be done. */
58 static int as_cachepath_on = 0; /* Default to off */
61 as_enable_cachepath(void)
67 as_disable_cachepath(void)
72 /* Note we want these to be as fast as possible */
75 as_add_cachepath(struct as_data *adp)
77 struct as_frompath *from;
78 struct as_topath *to = (struct as_topath *)0;
81 /* Don't do anything if we aren't cacheing these */
82 if (as_cachepath_on == 0)
85 /* Note we will only allocate this once. Afterwards, we just keep
86 * zeroing it since it's rather small and we don't need to re-allocate
88 if (fromhead == (struct as_frompath **)0) {
89 fromhead = calloc(1, sizeof(struct as_frompath *) * WORLD_Y);
90 if (fromhead == (struct as_frompath **)0)
95 for (from = fromhead[adp->from.y]; from; from = from->next)
96 if (from->x == adp->from.x)
99 for (to = from->tolist[np->c.y]; to; to = to->next) {
100 if (to->x == np->c.x) {
101 /* It is already here! Don't bother adding it again */
106 /* We must make a new one of these */
107 from = malloc(sizeof(struct as_frompath));
110 /* And set some stuff */
111 from->x = adp->from.x;
112 /* Here we malloc a whole bunch of tolist pointers. */
113 from->tolist = calloc(1, sizeof(struct as_topath *) * WORLD_Y);
114 /* Now, add from to the global list */
115 from->next = fromhead[adp->from.y];
116 fromhead[adp->from.y] = from;
119 /* We must make a new one */
120 to = malloc(sizeof(struct as_topath));
121 /* We can't, sorry */
124 /* Now set some stuff */
126 /* Now add it to the list we are in */
127 to->next = from->tolist[np->c.y];
128 from->tolist[np->c.y] = to;
130 /* Now, make the path */
132 /* Now, take the path */
133 to->path = adp->path;
134 /* And clear the path in the adp */
139 as_clear_cachepath(void)
141 struct as_frompath *from, *from2;
142 struct as_topath *to, *to2;
145 /* Cache not used yet :) */
146 if (fromhead == (struct as_frompath **)0)
149 for (j = 0; j < WORLD_Y; j++) {
150 for (from = fromhead[j]; from; from = from2) {
151 for (i = 0; i < WORLD_Y; i++) {
152 for (to = from->tolist[i]; to; to = to2) {
155 as_free_path(to->path);
160 /* Now, free the list of lists */
162 /* Save the next pointer */
164 /* now, free this from node */
168 /* Note we don't free the fromhead here, we just zero it. That way,
169 we can use it next time without mallocing int */
170 memset(fromhead, 0, (sizeof(struct as_frompath *) * WORLD_Y));
174 as_find_cachepath(coord fx, coord fy, coord tx, coord ty)
176 struct as_frompath *from;
177 struct as_topath *to;
179 /* Is the cache on? if not, return NULL */
180 if (as_cachepath_on == 0)
183 /* Do we have any cached? */
184 if (fromhead == (struct as_frompath **)0)
188 for (from = fromhead[fy]; from; from = from->next) {
190 for (to = from->tolist[ty]; to; to = to->next) {