1 |
/* |
2 |
CrossFire, A Multiplayer game for X-windows |
3 |
|
4 |
Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
5 |
Copyright (C) 1992 Frank Tore Johansen |
6 |
|
7 |
This program is free software; you can redistribute it and/or modify |
8 |
it under the terms of the GNU General Public License as published by |
9 |
the Free Software Foundation; either version 2 of the License, or |
10 |
(at your option) any later version. |
11 |
|
12 |
This program is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU General Public License |
18 |
along with this program; if not, write to the Free Software |
19 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
|
21 |
The author can be reached via e-mail to <crossfire@schmorp.de> |
22 |
*/ |
23 |
|
24 |
#include <global.h> |
25 |
#include <sproto.h> |
26 |
#include <object.h> |
27 |
|
28 |
/* This writes out information on all the temporary maps. It is called by |
29 |
* swap_map below. |
30 |
*/ |
31 |
static void |
32 |
write_map_log (void) |
33 |
{ |
34 |
FILE *fp; |
35 |
maptile *map; |
36 |
char buf[MAX_BUF]; |
37 |
long current_time = time (NULL); |
38 |
|
39 |
sprintf (buf, "%s/temp.maps", settings.localdir); |
40 |
if (!(fp = fopen (buf, "w"))) |
41 |
{ |
42 |
LOG (llevError, "Could not open %s for writing\n", buf); |
43 |
return; |
44 |
} |
45 |
for (map = first_map; map != NULL; map = map->next) |
46 |
{ |
47 |
/* If tmpname is null, it is probably a unique player map, |
48 |
* so don't save information on it. |
49 |
*/ |
50 |
if (map->in_memory != MAP_IN_MEMORY && (map->tmpname != NULL) && (strncmp (map->path, "/random", 7))) |
51 |
{ |
52 |
/* the 0 written out is a leftover from the lock number for |
53 |
* unique items and second one is from encounter maps. |
54 |
* Keep using it so that old temp files continue |
55 |
* to work. |
56 |
*/ |
57 |
fprintf (fp, "%s:%s:%ld:0:0:%d:0:%d\n", map->path, map->tmpname, |
58 |
(map->reset_time == (uint32) - 1 ? (long unsigned) -1 : map->reset_time - current_time), map->difficulty, map->darkness); |
59 |
} |
60 |
} |
61 |
fclose (fp); |
62 |
} |
63 |
|
64 |
void |
65 |
read_map_log (void) |
66 |
{ |
67 |
FILE *fp; |
68 |
maptile *map; |
69 |
char buf[MAX_BUF], *cp, *cp1; |
70 |
int do_los, darkness, lock; |
71 |
long sec = time (0); |
72 |
|
73 |
sprintf (buf, "%s/temp.maps", settings.localdir); |
74 |
if (!(fp = fopen (buf, "r"))) |
75 |
{ |
76 |
LOG (llevDebug, "Could not open %s for reading\n", buf); |
77 |
return; |
78 |
} |
79 |
while (fgets (buf, MAX_BUF, fp) != NULL) |
80 |
{ |
81 |
map = get_linked_map (); |
82 |
/* scanf doesn't work all that great on strings, so we break |
83 |
* out that manually. strdup is used for tmpname, since other |
84 |
* routines will try to free that pointer. |
85 |
*/ |
86 |
cp = strchr (buf, ':'); |
87 |
*cp++ = '\0'; |
88 |
strcpy (map->path, buf); |
89 |
cp1 = strchr (cp, ':'); |
90 |
*cp1++ = '\0'; |
91 |
map->tmpname = strdup (cp); |
92 |
|
93 |
/* Lock is left over from the lock items - we just toss it now. |
94 |
* We use it twice - second one is from encounter, but as we |
95 |
* don't care about the value, this works fine |
96 |
*/ |
97 |
sscanf (cp1, "%d:%d:%d:%hd:%d:%d\n", &map->reset_time, &lock, &lock, &map->difficulty, &do_los, &darkness); |
98 |
|
99 |
|
100 |
map->in_memory = MAP_SWAPPED; |
101 |
map->darkness = darkness; |
102 |
|
103 |
/* When the reset time is saved out, it is adjusted so that |
104 |
* the current time is subtracted (thus, it is saved as number |
105 |
* of seconds from current time that it should reset). We need |
106 |
* to add in the current seconds for this to work right. |
107 |
* On metalforge, strange behavior was observed with really high |
108 |
* reset times - I don't know how they got to that state, |
109 |
* but easy enough to do some sanity checking here. |
110 |
*/ |
111 |
map->reset_time += sec; |
112 |
if (map->reset_time > (uint32) (sec + MAP_MAXRESET)) |
113 |
map->reset_time = 0; |
114 |
|
115 |
} |
116 |
|
117 |
fclose (fp); |
118 |
} |
119 |
|
120 |
void |
121 |
swap_map (maptile *map) |
122 |
{ |
123 |
player *pl; |
124 |
|
125 |
if (map->in_memory != MAP_IN_MEMORY) |
126 |
{ |
127 |
LOG (llevError, "Tried to swap out map which was not in memory.\n"); |
128 |
return; |
129 |
} |
130 |
|
131 |
for (pl = first_player; pl; pl = pl->next) |
132 |
if (!pl->ob || (!(QUERY_FLAG (pl->ob, FLAG_REMOVED)) && pl->ob->map == map)) |
133 |
break; |
134 |
|
135 |
if (pl) |
136 |
{ |
137 |
LOG (llevDebug, "Wanted to swap out map with player.\n"); |
138 |
return; |
139 |
} |
140 |
|
141 |
remove_all_pets (map); /* Give them a chance to follow */ |
142 |
|
143 |
/* Update the reset time. Only do this is STAND_STILL is not set */ |
144 |
if (!map->fixed_resettime) |
145 |
set_map_reset_time (map); |
146 |
|
147 |
/* If it is immediate reset time, don't bother saving it - just get |
148 |
* rid of it right away. |
149 |
*/ |
150 |
if (map->reset_time <= (uint32) time (0)) |
151 |
{ |
152 |
LOG (llevDebug, "Resetting map %s.\n", map->path); |
153 |
INVOKE_MAP (RESET, map); |
154 |
delete_map (map); |
155 |
return; |
156 |
} |
157 |
|
158 |
if (new_save_map (map, 0) == -1) |
159 |
{ |
160 |
LOG (llevError, "Failed to swap map %s.\n", map->path); |
161 |
/* need to reset the in_memory flag so that delete map will also |
162 |
* free the objects with it. |
163 |
*/ |
164 |
map->in_memory = MAP_IN_MEMORY; |
165 |
delete_map (map); |
166 |
} |
167 |
else |
168 |
{ |
169 |
INVOKE_MAP (SWAPOUT, map); |
170 |
free_map (map, 1); |
171 |
} |
172 |
|
173 |
write_map_log (); |
174 |
} |
175 |
|
176 |
void |
177 |
check_active_maps (void) |
178 |
{ |
179 |
maptile *map, *next; |
180 |
|
181 |
for (map = first_map; map != NULL; map = next) |
182 |
{ |
183 |
next = map->next; |
184 |
if (map->in_memory != MAP_IN_MEMORY) |
185 |
continue; |
186 |
if (!map->timeout) |
187 |
continue; |
188 |
if (--(map->timeout) > 0) |
189 |
continue; |
190 |
/* If LWM is set, we only swap maps out when we run out of objects */ |
191 |
#ifndef MAX_OBJECTS_LWM |
192 |
swap_map (map); |
193 |
#endif |
194 |
} |
195 |
} |
196 |
|
197 |
/* |
198 |
* map_least_timeout() returns the map with the lowest timeout variable (not 0) |
199 |
*/ |
200 |
|
201 |
maptile * |
202 |
map_least_timeout (char *except_level) |
203 |
{ |
204 |
maptile *map, *chosen = NULL; |
205 |
int timeout = MAP_MAXTIMEOUT + 1; |
206 |
|
207 |
for (map = first_map; map != NULL; map = map->next) |
208 |
if (map->in_memory == MAP_IN_MEMORY && strcmp (map->path, except_level) && map->timeout && map->timeout < timeout) |
209 |
chosen = map, timeout = map->timeout; |
210 |
return chosen; |
211 |
} |
212 |
|
213 |
/* |
214 |
* players_on_map(): will be replaced by map->players when I'm satisfied |
215 |
* that the variable is always correct. |
216 |
* If show_all is true, we show everyone. If not, we don't show hidden |
217 |
* players (dms) |
218 |
*/ |
219 |
|
220 |
int |
221 |
players_on_map (maptile *m, int show_all) |
222 |
{ |
223 |
player *pl; |
224 |
int nr = 0; |
225 |
|
226 |
for (pl = first_player; pl != NULL; pl = pl->next) |
227 |
if (pl->ob != NULL && !QUERY_FLAG (pl->ob, FLAG_REMOVED) && pl->ob->map == m && (show_all || !pl->hidden)) |
228 |
nr++; |
229 |
return nr; |
230 |
} |
231 |
|
232 |
/* |
233 |
* flush_old_maps(): |
234 |
* Removes tmp-files of maps which are going to be reset next time |
235 |
* they are visited. |
236 |
* This is very useful if the tmp-disk is very full. |
237 |
*/ |
238 |
void |
239 |
flush_old_maps (void) |
240 |
{ |
241 |
|
242 |
maptile *m, *oldmap; |
243 |
uint32 sec = time (0); |
244 |
|
245 |
m = first_map; |
246 |
while (m) |
247 |
{ |
248 |
/* There can be cases (ie death) where a player leaves a map and the timeout |
249 |
* is not set so it isn't swapped out. |
250 |
*/ |
251 |
if ((m->in_memory == MAP_IN_MEMORY) && (m->timeout == 0) && !players_on_map (m, TRUE)) |
252 |
set_map_timeout (m); |
253 |
|
254 |
/* per player unique maps are never really reset. However, we do want |
255 |
* to perdiocially remove the entries in the list of active maps - this |
256 |
* generates a cleaner listing if a player issues the map commands, and |
257 |
* keeping all those swapped out per player unique maps also has some |
258 |
* memory and cpu consumption. |
259 |
* We do the cleanup here because there are lots of places that call |
260 |
* swap map, and doing it within swap map may cause problems as |
261 |
* the functions calling it may not expect the map list to change |
262 |
* underneath them. |
263 |
*/ |
264 |
if ((m->unique || m->templatemap) && m->in_memory == MAP_SWAPPED) |
265 |
{ |
266 |
LOG (llevDebug, "Resetting map %s.\n", m->path); |
267 |
oldmap = m; |
268 |
m = m->next; |
269 |
delete_map (oldmap); |
270 |
} |
271 |
else if (m->in_memory != MAP_SWAPPED || m->tmpname == NULL || sec < m->reset_time) |
272 |
{ |
273 |
m = m->next; |
274 |
} |
275 |
else |
276 |
{ |
277 |
LOG (llevDebug, "Resetting map %s.\n", m->path); |
278 |
INVOKE_MAP (RESET, m); |
279 |
clean_tmp_map (m); |
280 |
oldmap = m; |
281 |
m = m->next; |
282 |
delete_map (oldmap); |
283 |
} |
284 |
} |
285 |
} |