2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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 */
64 as_disable_cachepath()
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 = (struct as_topath **)calloc(1,
111 sizeof(struct as_topath
113 /* Now, add from to the global list */
114 from->next = fromhead[adp->from.y];
115 fromhead[adp->from.y] = from;
118 /* We must make a new one */
119 to = (struct as_topath *)malloc(sizeof(struct as_topath));
120 /* We can't, sorry */
123 /* Now set some stuff */
125 /* Now add it to the list we are in */
126 to->next = from->tolist[np->c.y];
127 from->tolist[np->c.y] = to;
129 /* Now, make the path */
131 /* Now, take the path */
132 to->path = adp->path;
133 /* And clear the path in the adp */
140 struct as_frompath *from, *from2;
141 struct as_topath *to, *to2;
144 /* Cache not used yet :) */
145 if (fromhead == (struct as_frompath **)0)
148 for (j = 0; j < WORLD_Y; j++) {
149 for (from = fromhead[j]; from; from = from2) {
150 for (i = 0; i < WORLD_Y; i++) {
151 for (to = from->tolist[i]; to; to = to2) {
154 as_free_path(to->path);
159 /* Now, free the list of lists */
160 free((s_char *)from->tolist);
161 /* Save the next pointer */
163 /* now, free this from node */
164 free((s_char *)from);
167 /* Note we don't free the fromhead here, we just zero it. That way,
168 we can use it next time without mallocing int */
169 memset(fromhead, 0, (sizeof(struct as_frompath *) * WORLD_Y));
173 as_find_cachepath(coord fx, coord fy, coord tx, coord ty)
175 struct as_frompath *from;
176 struct as_topath *to;
178 /* Is the cache on? if not, return NULL */
179 if (as_cachepath_on == 0)
182 /* Do we have any cached? */
183 if (fromhead == (struct as_frompath **)0)
187 for (from = fromhead[fy]; from; from = from->next) {
189 for (to = from->tolist[ty]; to; to = to->next) {