2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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:
37 /* The way this works is interesting. :) */
38 /* We keep a pointer to a list of pointers. The index into this list
39 * is the y coordinate of the from sector. This member points to a list
40 * of from sectors on that y coordinate. So, we march that list checking
41 * the x value to find the from x,y we want. */
42 /* Once we find the from x,y, that node has the same type of pointer to
43 * a list of pointers. The index into this list is the y coordinate of
44 * the to sector. This member points to a list of to sectors on that y
45 * coordinate. So, we march that list checking the x value to find the
47 /* These lists are dynamically created since the world is dynamically sized. */
48 /* See, I told you it was interesting. :) */
50 static struct as_frompath **fromhead = (struct as_frompath **)0;
52 /* Note that we only want to cache during updates. Other times, it
53 * probably doesn't make much sense, but can be done. */
55 static int as_cachepath_on = 0; /* Default to off */
58 as_enable_cachepath(void)
64 as_disable_cachepath(void)
69 /* Note we want these to be as fast as possible */
72 as_add_cachepath(struct as_data *adp)
74 struct as_frompath *from;
75 struct as_topath *to = (struct as_topath *)0;
78 /* Don't do anything if we aren't cacheing these */
79 if (as_cachepath_on == 0)
82 /* Note we will only allocate this once. Afterwards, we just keep
83 * zeroing it since it's rather small and we don't need to re-allocate
85 if (fromhead == (struct as_frompath **)0) {
86 fromhead = calloc(1, sizeof(struct as_frompath *) * WORLD_Y);
87 if (fromhead == (struct as_frompath **)0)
92 for (from = fromhead[adp->from.y]; from; from = from->next)
93 if (from->x == adp->from.x)
96 for (to = from->tolist[np->c.y]; to; to = to->next) {
97 if (to->x == np->c.x) {
98 /* It is already here! Don't bother adding it again */
103 /* We must make a new one of these */
104 from = (struct as_frompath *)malloc(sizeof(struct as_frompath));
107 /* And set some stuff */
108 from->x = adp->from.x;
109 /* Here we malloc a whole bunch of tolist pointers. */
110 from->tolist = calloc(1, sizeof(struct as_topath *) * WORLD_Y);
111 /* Now, add from to the global list */
112 from->next = fromhead[adp->from.y];
113 fromhead[adp->from.y] = from;
116 /* We must make a new one */
117 to = (struct as_topath *)malloc(sizeof(struct as_topath));
118 /* We can't, sorry */
121 /* Now set some stuff */
123 /* Now add it to the list we are in */
124 to->next = from->tolist[np->c.y];
125 from->tolist[np->c.y] = to;
127 /* Now, make the path */
129 /* Now, take the path */
130 to->path = adp->path;
131 /* And clear the path in the adp */
136 as_clear_cachepath(void)
138 struct as_frompath *from, *from2;
139 struct as_topath *to, *to2;
142 /* Cache not used yet :) */
143 if (fromhead == (struct as_frompath **)0)
146 for (j = 0; j < WORLD_Y; j++) {
147 for (from = fromhead[j]; from; from = from2) {
148 for (i = 0; i < WORLD_Y; i++) {
149 for (to = from->tolist[i]; to; to = to2) {
152 as_free_path(to->path);
157 /* Now, free the list of lists */
158 free((s_char *)from->tolist);
159 /* Save the next pointer */
161 /* now, free this from node */
162 free((s_char *)from);
165 /* Note we don't free the fromhead here, we just zero it. That way,
166 we can use it next time without mallocing int */
167 memset(fromhead, 0, (sizeof(struct as_frompath *) * WORLD_Y));
171 as_find_cachepath(coord fx, coord fy, coord tx, coord ty)
173 struct as_frompath *from;
174 struct as_topath *to;
176 /* Is the cache on? if not, return NULL */
177 if (as_cachepath_on == 0)
180 /* Do we have any cached? */
181 if (fromhead == (struct as_frompath **)0)
185 for (from = fromhead[fy]; from; from = from->next) {
187 for (to = from->tolist[ty]; to; to = to->next) {