ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/swap.C
Revision: 1.18
Committed: Wed Dec 27 09:28:02 2006 UTC (17 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.17: +26 -34 lines
Log Message:
introduce for_all_maps

File Contents

# User Rev Content
1 elmex 1.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 root 1.7 The author can be reached via e-mail to <crossfire@schmorp.de>
22 elmex 1.1 */
23    
24     #include <global.h>
25 root 1.11 #include <sproto.h>
26 elmex 1.1 #include <object.h>
27    
28     /* This writes out information on all the temporary maps. It is called by
29     * swap_map below.
30     */
31 root 1.6 static void
32     write_map_log (void)
33 elmex 1.1 {
34 root 1.6 FILE *fp;
35 root 1.8 maptile *map;
36 root 1.6 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 root 1.12
46 root 1.18 for_all_maps (map)
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)
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 root 1.12
61 root 1.6 fclose (fp);
62 elmex 1.1 }
63    
64 root 1.6 void
65     read_map_log (void)
66 elmex 1.1 {
67 root 1.6 FILE *fp;
68 root 1.8 maptile *map;
69 root 1.6 char buf[MAX_BUF], *cp, *cp1;
70     int do_los, darkness, lock;
71 root 1.9 long sec = time (0);
72 elmex 1.1
73 root 1.6 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 elmex 1.1 }
79 root 1.13
80     while (fgets (buf, MAX_BUF, fp))
81 root 1.6 {
82     map = get_linked_map ();
83     /* scanf doesn't work all that great on strings, so we break
84     * out that manually. strdup is used for tmpname, since other
85     * routines will try to free that pointer.
86     */
87     cp = strchr (buf, ':');
88     *cp++ = '\0';
89     strcpy (map->path, buf);
90     cp1 = strchr (cp, ':');
91     *cp1++ = '\0';
92 root 1.10 map->tmpname = strdup (cp);
93 root 1.6
94     /* Lock is left over from the lock items - we just toss it now.
95     * We use it twice - second one is from encounter, but as we
96     * don't care about the value, this works fine
97     */
98     sscanf (cp1, "%d:%d:%d:%hd:%d:%d\n", &map->reset_time, &lock, &lock, &map->difficulty, &do_los, &darkness);
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 root 1.11
116 root 1.6 fclose (fp);
117 elmex 1.1 }
118    
119 root 1.6 void
120 root 1.8 swap_map (maptile *map)
121 root 1.6 {
122     if (map->in_memory != MAP_IN_MEMORY)
123     {
124     LOG (llevError, "Tried to swap out map which was not in memory.\n");
125     return;
126     }
127 root 1.11
128 root 1.17 // cannot (yet) swap out maps with players
129 root 1.14 for_all_players (pl)
130 root 1.15 if (pl->ob && pl->ob->map == map && !pl->ob->flag [FLAG_REMOVED])
131 root 1.17 return;
132 root 1.15
133     remove_all_pets (map); /* Give them a chance to follow */
134 root 1.6
135 root 1.16 /* Update the reset time. Only do this if STAND_STILL is not set */
136 root 1.15 if (!map->fixed_resettime)
137     set_map_reset_time (map);
138    
139     /* If it is immediate reset time, don't bother saving it - just get
140     * rid of it right away.
141     */
142     if (map->reset_time <= (uint32) time (0))
143     {
144     LOG (llevDebug, "Resetting map %s.\n", map->path);
145     INVOKE_MAP (RESET, map);
146     delete_map (map);
147     return;
148     }
149 root 1.14
150 root 1.15 if (new_save_map (map, 0) == -1)
151     {
152     LOG (llevError, "Failed to swap map %s.\n", map->path);
153     /* need to reset the in_memory flag so that delete map will also
154     * free the objects with it.
155     */
156     map->in_memory = MAP_IN_MEMORY;
157     delete_map (map);
158     }
159     else
160     {
161     INVOKE_MAP (SWAPOUT, map);
162     free_map (map, 1);
163     }
164 root 1.6
165 root 1.15 write_map_log ();
166 elmex 1.1 }
167    
168 root 1.6 void
169     check_active_maps (void)
170     {
171 root 1.18 for_all_maps (map)
172     if (map->in_memory == MAP_IN_MEMORY
173     && map->timeout
174     && !--map->timeout)
175     {
176     swap_map (map);
177     // only swap one map per tick
178     break;
179     }
180 elmex 1.1 }
181    
182     /*
183     * map_least_timeout() returns the map with the lowest timeout variable (not 0)
184     */
185 root 1.8 maptile *
186 root 1.6 map_least_timeout (char *except_level)
187     {
188 root 1.18 maptile *chosen = NULL;
189 elmex 1.1 int timeout = MAP_MAXTIMEOUT + 1;
190 root 1.6
191 root 1.18 for_all_maps (map)
192 root 1.6 if (map->in_memory == MAP_IN_MEMORY && strcmp (map->path, except_level) && map->timeout && map->timeout < timeout)
193     chosen = map, timeout = map->timeout;
194 root 1.18
195 elmex 1.1 return chosen;
196     }
197    
198     /*
199     * players_on_map(): will be replaced by map->players when I'm satisfied
200     * that the variable is always correct.
201     * If show_all is true, we show everyone. If not, we don't show hidden
202     * players (dms)
203     */
204 root 1.6 int
205 root 1.8 players_on_map (maptile *m, int show_all)
206 root 1.6 {
207     int nr = 0;
208    
209 root 1.14 for_all_players (pl)
210 root 1.6 if (pl->ob != NULL && !QUERY_FLAG (pl->ob, FLAG_REMOVED) && pl->ob->map == m && (show_all || !pl->hidden))
211 elmex 1.1 nr++;
212 root 1.14
213 elmex 1.1 return nr;
214     }
215    
216     /*
217     * flush_old_maps():
218     * Removes tmp-files of maps which are going to be reset next time
219     * they are visited.
220     * This is very useful if the tmp-disk is very full.
221     */
222 root 1.6 void
223     flush_old_maps (void)
224     {
225 root 1.8 maptile *m, *oldmap;
226 root 1.9 uint32 sec = time (0);
227 elmex 1.1
228 root 1.6 m = first_map;
229     while (m)
230     {
231     /* There can be cases (ie death) where a player leaves a map and the timeout
232     * is not set so it isn't swapped out.
233     */
234     if ((m->in_memory == MAP_IN_MEMORY) && (m->timeout == 0) && !players_on_map (m, TRUE))
235 root 1.9 set_map_timeout (m);
236 root 1.3
237 root 1.6 /* per player unique maps are never really reset. However, we do want
238     * to perdiocially remove the entries in the list of active maps - this
239     * generates a cleaner listing if a player issues the map commands, and
240     * keeping all those swapped out per player unique maps also has some
241     * memory and cpu consumption.
242     * We do the cleanup here because there are lots of places that call
243     * swap map, and doing it within swap map may cause problems as
244     * the functions calling it may not expect the map list to change
245     * underneath them.
246     */
247     if ((m->unique || m->templatemap) && m->in_memory == MAP_SWAPPED)
248     {
249     LOG (llevDebug, "Resetting map %s.\n", m->path);
250     oldmap = m;
251     m = m->next;
252     delete_map (oldmap);
253 root 1.3 }
254 root 1.6 else if (m->in_memory != MAP_SWAPPED || m->tmpname == NULL || sec < m->reset_time)
255 root 1.17 m = m->next;
256 root 1.6 else
257     {
258     LOG (llevDebug, "Resetting map %s.\n", m->path);
259     INVOKE_MAP (RESET, m);
260     clean_tmp_map (m);
261     oldmap = m;
262     m = m->next;
263     delete_map (oldmap);
264 root 1.3 }
265 elmex 1.1 }
266     }