]> git.pond.sub.org Git - empserver/blob - src/lib/as/as_cache.c
Remove some redundant parenthesis; no functional change.
[empserver] / src / lib / as / as_cache.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  as_cache.c: Routines used to create/delete caches of A* paths.
29  *
30  *  Known contributors to this file:
31  *     Steve McClure, 1998
32  */
33
34 #include <stdlib.h>
35 #include "as.h"
36 #include "optlist.h"
37
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
47  * to x,y we want. */
48 /* These lists are dynamically created since the world is dynamically sized. */
49 /* See, I told you it was interesting. :) */
50
51 static struct as_frompath **fromhead = (struct as_frompath **)0;
52
53 /* Note that we only want to cache during updates.  Other times, it
54  * probably doesn't make much sense, but can be done. */
55
56 static int as_cachepath_on = 0; /* Default to off */
57
58 void
59 as_enable_cachepath(void)
60 {
61     as_cachepath_on = 1;
62 }
63
64 void
65 as_disable_cachepath(void)
66 {
67     as_cachepath_on = 0;
68 }
69
70 /* Note we want these to be as fast as possible */
71
72 void
73 as_add_cachepath(struct as_data *adp)
74 {
75     struct as_frompath *from;
76     struct as_topath *to = (struct as_topath *)0;
77     struct as_node *np;
78
79     /* Don't do anything if we aren't cacheing these */
80     if (as_cachepath_on == 0)
81         return;
82
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
85      * each time. */
86     if (fromhead == (struct as_frompath **)0) {
87         fromhead = calloc(1, sizeof(struct as_frompath *) * WORLD_Y);
88         if (fromhead == (struct as_frompath **)0)
89             return;
90     }
91
92     np = adp->head->np;
93     for (from = fromhead[adp->from.y]; from; from = from->next)
94         if (from->x == adp->from.x)
95             break;
96     if (from) {
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 */
100                 return;
101             }
102         }
103     } else {
104         /* We must make a new one of these */
105         from = malloc(sizeof(struct as_frompath));
106         if (from == NULL)
107             return;
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;
115     }
116     if (!to) {
117         /* We must make a new one */
118         to = malloc(sizeof(struct as_topath));
119         /* We can't, sorry */
120         if (to == NULL)
121             return;
122         /* Now set some stuff */
123         to->x = np->c.x;
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;
127     }
128     /* Now, make the path */
129     as_makepath(adp);
130     /* Now, take the path */
131     to->path = adp->path;
132     /* And clear the path in the adp */
133     adp->path = NULL;
134 }
135
136 void
137 as_clear_cachepath(void)
138 {
139     struct as_frompath *from, *from2;
140     struct as_topath *to, *to2;
141     int i, j;
142
143     /* Cache not used yet :) */
144     if (fromhead == (struct as_frompath **)0)
145         return;
146
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) {
151                     to2 = to->next;
152                     /* Free this path */
153                     as_free_path(to->path);
154                     /* Free this node */
155                     free(to);
156                 }
157             }
158             /* Now, free the list of lists */
159             free(from->tolist);
160             /* Save the next pointer */
161             from2 = from->next;
162             /* now, free this from node */
163             free(from);
164         }
165     }
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));
169 }
170
171 struct as_path *
172 as_find_cachepath(coord fx, coord fy, coord tx, coord ty)
173 {
174     struct as_frompath *from;
175     struct as_topath *to;
176
177     /* Is the cache on?  if not, return NULL */
178     if (as_cachepath_on == 0)
179         return NULL;
180
181     /* Do we have any cached? */
182     if (fromhead == (struct as_frompath **)0)
183         return NULL;
184
185     /* Yes! */
186     for (from = fromhead[fy]; from; from = from->next) {
187         if (from->x == fx) {
188             for (to = from->tolist[ty]; to; to = to->next) {
189                 if (to->x == tx)
190                     return to->path;
191             }
192         }
193     }
194     return NULL;
195 }