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