ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/swap.C
Revision: 1.15
Committed: Sun Dec 24 12:28:30 2006 UTC (17 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.14: +35 -35 lines
Log Message:
broken logic before, broken code after - fixed

File Contents

# Content
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
46 for (map = first_map; map != NULL; map = map->next)
47 {
48 /* If tmpname is null, it is probably a unique player map,
49 * so don't save information on it.
50 */
51 if (map->in_memory != MAP_IN_MEMORY && map->tmpname)
52 {
53 /* the 0 written out is a leftover from the lock number for
54 * unique items and second one is from encounter maps.
55 * Keep using it so that old temp files continue
56 * to work.
57 */
58 fprintf (fp, "%s:%s:%ld:0:0:%d:0:%d\n", map->path, map->tmpname,
59 (map->reset_time == (uint32) - 1 ? (long unsigned) -1 : map->reset_time - current_time), map->difficulty, map->darkness);
60 }
61 }
62
63 fclose (fp);
64 }
65
66 void
67 read_map_log (void)
68 {
69 FILE *fp;
70 maptile *map;
71 char buf[MAX_BUF], *cp, *cp1;
72 int do_los, darkness, lock;
73 long sec = time (0);
74
75 sprintf (buf, "%s/temp.maps", settings.localdir);
76 if (!(fp = fopen (buf, "r")))
77 {
78 LOG (llevDebug, "Could not open %s for reading\n", buf);
79 return;
80 }
81
82 while (fgets (buf, MAX_BUF, fp))
83 {
84 map = get_linked_map ();
85 /* scanf doesn't work all that great on strings, so we break
86 * out that manually. strdup is used for tmpname, since other
87 * routines will try to free that pointer.
88 */
89 cp = strchr (buf, ':');
90 *cp++ = '\0';
91 strcpy (map->path, buf);
92 cp1 = strchr (cp, ':');
93 *cp1++ = '\0';
94 map->tmpname = strdup (cp);
95
96 /* Lock is left over from the lock items - we just toss it now.
97 * We use it twice - second one is from encounter, but as we
98 * don't care about the value, this works fine
99 */
100 sscanf (cp1, "%d:%d:%d:%hd:%d:%d\n", &map->reset_time, &lock, &lock, &map->difficulty, &do_los, &darkness);
101
102 map->in_memory = MAP_SWAPPED;
103 map->darkness = darkness;
104
105 /* When the reset time is saved out, it is adjusted so that
106 * the current time is subtracted (thus, it is saved as number
107 * of seconds from current time that it should reset). We need
108 * to add in the current seconds for this to work right.
109 * On metalforge, strange behavior was observed with really high
110 * reset times - I don't know how they got to that state,
111 * but easy enough to do some sanity checking here.
112 */
113 map->reset_time += sec;
114 if (map->reset_time > (uint32) (sec + MAP_MAXRESET))
115 map->reset_time = 0;
116 }
117
118 fclose (fp);
119 }
120
121 void
122 swap_map (maptile *map)
123 {
124 if (map->in_memory != MAP_IN_MEMORY)
125 {
126 LOG (llevError, "Tried to swap out map which was not in memory.\n");
127 return;
128 }
129
130 for_all_players (pl)
131 if (pl->ob && pl->ob->map == map && !pl->ob->flag [FLAG_REMOVED])
132 {
133 LOG (llevDebug, "Wanted to swap out map with player.\n");
134 return;
135 }
136
137 remove_all_pets (map); /* Give them a chance to follow */
138
139 /* Update the reset time. Only do this is STAND_STILL is not set */
140 if (!map->fixed_resettime)
141 set_map_reset_time (map);
142
143 /* If it is immediate reset time, don't bother saving it - just get
144 * rid of it right away.
145 */
146 if (map->reset_time <= (uint32) time (0))
147 {
148 LOG (llevDebug, "Resetting map %s.\n", map->path);
149 INVOKE_MAP (RESET, map);
150 delete_map (map);
151 return;
152 }
153
154 if (new_save_map (map, 0) == -1)
155 {
156 LOG (llevError, "Failed to swap map %s.\n", map->path);
157 /* need to reset the in_memory flag so that delete map will also
158 * free the objects with it.
159 */
160 map->in_memory = MAP_IN_MEMORY;
161 delete_map (map);
162 }
163 else
164 {
165 INVOKE_MAP (SWAPOUT, map);
166 free_map (map, 1);
167 }
168
169 write_map_log ();
170 }
171
172 void
173 check_active_maps (void)
174 {
175 maptile *map, *next;
176
177 for (map = first_map; map != NULL; map = next)
178 {
179 next = map->next;
180 if (map->in_memory != MAP_IN_MEMORY)
181 continue;
182 if (!map->timeout)
183 continue;
184 if (--(map->timeout) > 0)
185 continue;
186 /* If LWM is set, we only swap maps out when we run out of objects */
187 #ifndef MAX_OBJECTS_LWM
188 swap_map (map);
189 #endif
190 }
191 }
192
193 /*
194 * map_least_timeout() returns the map with the lowest timeout variable (not 0)
195 */
196
197 maptile *
198 map_least_timeout (char *except_level)
199 {
200 maptile *map, *chosen = NULL;
201 int timeout = MAP_MAXTIMEOUT + 1;
202
203 for (map = first_map; map != NULL; map = map->next)
204 if (map->in_memory == MAP_IN_MEMORY && strcmp (map->path, except_level) && map->timeout && map->timeout < timeout)
205 chosen = map, timeout = map->timeout;
206 return chosen;
207 }
208
209 /*
210 * players_on_map(): will be replaced by map->players when I'm satisfied
211 * that the variable is always correct.
212 * If show_all is true, we show everyone. If not, we don't show hidden
213 * players (dms)
214 */
215
216 int
217 players_on_map (maptile *m, int show_all)
218 {
219 int nr = 0;
220
221 for_all_players (pl)
222 if (pl->ob != NULL && !QUERY_FLAG (pl->ob, FLAG_REMOVED) && pl->ob->map == m && (show_all || !pl->hidden))
223 nr++;
224
225 return nr;
226 }
227
228 /*
229 * flush_old_maps():
230 * Removes tmp-files of maps which are going to be reset next time
231 * they are visited.
232 * This is very useful if the tmp-disk is very full.
233 */
234 void
235 flush_old_maps (void)
236 {
237
238 maptile *m, *oldmap;
239 uint32 sec = time (0);
240
241 m = first_map;
242 while (m)
243 {
244 /* There can be cases (ie death) where a player leaves a map and the timeout
245 * is not set so it isn't swapped out.
246 */
247 if ((m->in_memory == MAP_IN_MEMORY) && (m->timeout == 0) && !players_on_map (m, TRUE))
248 set_map_timeout (m);
249
250 /* per player unique maps are never really reset. However, we do want
251 * to perdiocially remove the entries in the list of active maps - this
252 * generates a cleaner listing if a player issues the map commands, and
253 * keeping all those swapped out per player unique maps also has some
254 * memory and cpu consumption.
255 * We do the cleanup here because there are lots of places that call
256 * swap map, and doing it within swap map may cause problems as
257 * the functions calling it may not expect the map list to change
258 * underneath them.
259 */
260 if ((m->unique || m->templatemap) && m->in_memory == MAP_SWAPPED)
261 {
262 LOG (llevDebug, "Resetting map %s.\n", m->path);
263 oldmap = m;
264 m = m->next;
265 delete_map (oldmap);
266 }
267 else if (m->in_memory != MAP_SWAPPED || m->tmpname == NULL || sec < m->reset_time)
268 {
269 m = m->next;
270 }
271 else
272 {
273 LOG (llevDebug, "Resetting map %s.\n", m->path);
274 INVOKE_MAP (RESET, m);
275 clean_tmp_map (m);
276 oldmap = m;
277 m = m->next;
278 delete_map (oldmap);
279 }
280 }
281 }