ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/main.C
(Generate patch)

Comparing deliantra/server/server/main.C (file contents):
Revision 1.21 by pippijn, Thu Sep 7 10:01:58 2006 UTC vs.
Revision 1.155 by root, Wed Apr 21 06:45:57 2010 UTC

1/* 1/*
2 * static char *rcsid_main_c = 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * "$Id: main.C,v 1.21 2006/09/07 10:01:58 pippijn Exp $"; 3 *
4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2001-2003 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 Frank Tore Johansen
7 *
8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * the terms of the Affero GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the Affero GNU General Public License
19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 *
22 * The authors can be reached via e-mail to <support@deliantra.net>
4 */ 23 */
5
6/*
7 CrossFire, A Multiplayer game for X-windows
8
9 Copyright (C) 2001-2003 Mark Wedel & Crossfire Development Team
10 Copyright (C) 1992 Frank Tore Johansen
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 The authors can be reached via e-mail at crossfire-devel@real-time.com
27*/
28 24
29#include <global.h> 25#include <global.h>
30#include <object.h> 26#include <object.h>
31#include <tod.h> 27#include <tod.h>
32 28
33#ifdef HAVE_DES_H
34#include <des.h>
35#else
36# ifdef HAVE_CRYPT_H
37# include <crypt.h>
38# endif
39#endif
40
41#ifndef __CEXTRACT__
42#include <sproto.h> 29#include <sproto.h>
43#endif
44
45#ifdef HAVE_TIME_H
46#include <time.h> 30#include <time.h>
47#endif 31
32#include <glib.h>
48 33
49#include <../random_maps/random_map.h> 34#include <../random_maps/random_map.h>
50#include <../random_maps/rproto.h> 35#include <../random_maps/rproto.h>
51#include "path.h" 36#include "path.h"
52 37
53static char days[7][4] = {
54 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
55};
56
57void 38void
58version (object * op) 39version (object *op)
59{ 40{
60 if (op != NULL)
61 clear_win_info (op);
62
63 new_draw_info_format (NDI_UNIQUE, 0, op, "This is Crossfire v%s", VERSION); 41 new_draw_info_format (NDI_UNIQUE, 0, op, "This is Deliantra v%s", VERSION);
64
65/* If in a socket, don't print out the list of authors. It confuses the
66 * crossclient program.
67 */
68 if (op == NULL)
69 return;
70 new_draw_info (NDI_UNIQUE, 0, op, "Authors and contributors to this program:");
71 new_draw_info (NDI_UNIQUE, 0, op, "mwedel@sonic.net (Mark Wedel)");
72 new_draw_info (NDI_UNIQUE, 0, op, "frankj@ifi.uio.no (Frank Tore Johansen)");
73 new_draw_info (NDI_UNIQUE, 0, op, "kjetilho@ifi.uio.no (Kjetil Torgrim Homme)");
74 new_draw_info (NDI_UNIQUE, 0, op, "tvangod@ecst.csuchico.edu (Tyler Van Gorder)");
75 new_draw_info (NDI_UNIQUE, 0, op, "elmroth@cd.chalmers.se (Tony Elmroth)");
76 new_draw_info (NDI_UNIQUE, 0, op, "dougal.scott@fcit.monasu.edu.au (Dougal Scott)");
77 new_draw_info (NDI_UNIQUE, 0, op, "wchuang@athena.mit.edu (William)");
78 new_draw_info (NDI_UNIQUE, 0, op, "ftww@cs.su.oz.au (Geoff Bailey)");
79 new_draw_info (NDI_UNIQUE, 0, op, "jorgens@flipper.pvv.unit.no (Kjetil Wiekhorst Jxrgensen)");
80 new_draw_info (NDI_UNIQUE, 0, op, "c.blackwood@rdt.monash.edu.au (Cameron Blackwood)");
81 new_draw_info (NDI_UNIQUE, 0, op, "jtraub+@cmu.edu (Joseph L. Traub)");
82 new_draw_info (NDI_UNIQUE, 0, op, "rgg@aaii.oz.au (Rupert G. Goldie)");
83 new_draw_info (NDI_UNIQUE, 0, op, "eanders+@cmu.edu (Eric A. Anderson)");
84 new_draw_info (NDI_UNIQUE, 0, op, "eneq@Prag.DoCS.UU.SE (Rickard Eneqvist)");
85 new_draw_info (NDI_UNIQUE, 0, op, "Jarkko.Sonninen@lut.fi (Jarkko Sonninen)");
86 new_draw_info (NDI_UNIQUE, 0, op, "kholland@sunlab.cit.cornell.du (Karl Holland)");
87 new_draw_info (NDI_UNIQUE, 0, op, "vick@bern.docs.uu.se (Mikael Lundgren)");
88 new_draw_info (NDI_UNIQUE, 0, op, "mol@meryl.csd.uu.se (Mikael Olsson)");
89 new_draw_info (NDI_UNIQUE, 0, op, "Tero.Haatanen@lut.fi (Tero Haatanen)");
90 new_draw_info (NDI_UNIQUE, 0, op, "ylitalo@student.docs.uu.se (Lasse Ylitalo)");
91 new_draw_info (NDI_UNIQUE, 0, op, "anipa@guru.magic.fi (Niilo Neuvo)");
92 new_draw_info (NDI_UNIQUE, 0, op, "mta@modeemi.cs.tut.fi (Markku J{rvinen)");
93 new_draw_info (NDI_UNIQUE, 0, op, "meunier@inf.enst.fr (Sylvain Meunier)");
94 new_draw_info (NDI_UNIQUE, 0, op, "jfosback@darmok.uoregon.edu (Jason Fosback)");
95 new_draw_info (NDI_UNIQUE, 0, op, "cedman@capitalist.princeton.edu (Carl Edman)");
96 new_draw_info (NDI_UNIQUE, 0, op, "henrich@crh.cl.msu.edu (Charles Henrich)");
97 new_draw_info (NDI_UNIQUE, 0, op, "schmid@fb3-s7.math.tu-berlin.de (Gregor Schmid)");
98 new_draw_info (NDI_UNIQUE, 0, op, "quinet@montefiore.ulg.ac.be (Raphael Quinet)");
99 new_draw_info (NDI_UNIQUE, 0, op, "jam@modeemi.cs.tut.fi (Jari Vanhala)");
100 new_draw_info (NDI_UNIQUE, 0, op, "kivinen@joker.cs.hut.fi (Tero Kivinen)");
101 new_draw_info (NDI_UNIQUE, 0, op, "peterm@soda.berkeley.edu (Peter Mardahl)");
102 new_draw_info (NDI_UNIQUE, 0, op, "matt@cs.odu.edu (Matthew Zeher)");
103 new_draw_info (NDI_UNIQUE, 0, op, "srt@sun-dimas.aero.org (Scott R. Turner)");
104 new_draw_info (NDI_UNIQUE, 0, op, "huma@netcom.com (Ben Fennema)");
105 new_draw_info (NDI_UNIQUE, 0, op, "njw@cs.city.ac.uk (Nick Williams)");
106 new_draw_info (NDI_UNIQUE, 0, op, "Wacren@Gin.ObsPM.Fr (Laurent Wacrenier)");
107 new_draw_info (NDI_UNIQUE, 0, op, "thomas@astro.psu.edu (Brian Thomas)");
108 new_draw_info (NDI_UNIQUE, 0, op, "jsm@axon.ksc.nasa.gov (John Steven Moerk)");
109 new_draw_info (NDI_UNIQUE, 0, op, "Delbecq David [david.delbecq@mailandnews.com]");
110 new_draw_info (NDI_UNIQUE, 0, op, "Chachkoff Yann [yann.chachkoff@mailandnews.com]\n");
111 new_draw_info (NDI_UNIQUE, 0, op, "Images and art:");
112 new_draw_info (NDI_UNIQUE, 0, op, "Peter Gardner");
113 new_draw_info (NDI_UNIQUE, 0, op, "David Gervais [david_eg@mail.com]");
114 new_draw_info (NDI_UNIQUE, 0, op, "Mitsuhiro Itakura [ita@gold.koma.jaeri.go.jp]");
115 new_draw_info (NDI_UNIQUE, 0, op, "Hansjoerg Malthaner [hansjoerg.malthaner@danet.de]");
116 new_draw_info (NDI_UNIQUE, 0, op, "Mårten Woxberg [maxmc@telia.com]");
117 new_draw_info (NDI_UNIQUE, 0, op, "And many more!");
118}
119
120void
121info_keys (object * op)
122{
123 clear_win_info (op);
124 new_draw_info (NDI_UNIQUE, 0, op, "Push `hjklynub' to walk in a direction.");
125 new_draw_info (NDI_UNIQUE, 0, op, "Shift + dir = fire, Ctrl + dir = run");
126 new_draw_info (NDI_UNIQUE, 0, op, "(To fire at yourself, hit `.'");
127 new_draw_info (NDI_UNIQUE, 0, op, "To attack, walk into the monsters.");
128 new_draw_info (NDI_UNIQUE, 0, op, "\" = speak ' = extended command");
129 new_draw_info (NDI_UNIQUE, 0, op, "i = inventory , = get : = look");
130 new_draw_info (NDI_UNIQUE, 0, op, "<> = rotate d = drop ? = help");
131 new_draw_info (NDI_UNIQUE, 0, op, "a = apply A = apply below t = throw");
132 new_draw_info (NDI_UNIQUE, 0, op, "e = examine E = exa below @ = autopick");
133 new_draw_info (NDI_UNIQUE, 0, op, "C = configure s = brace v = version");
134 new_draw_info (NDI_UNIQUE, 0, op, "+- = change range <tab> = browse spells");
135 new_draw_info (NDI_UNIQUE, 0, op, "x = change inventory type");
136 new_draw_info (NDI_UNIQUE, 0, op, "Mouse: L = examine, M = apply, R = drop/get");
137 new_draw_info (NDI_UNIQUE, 0, op, "'help = info about extended commands.");
138 new_draw_info (NDI_UNIQUE, 0, op, "Ctrl-R = refresh Ctrl-C = clear");
139 new_draw_info (NDI_UNIQUE, 0, op, "You can type a number before most commands.");
140 new_draw_info (NDI_UNIQUE, 0, op, "(For instance 3d drops 3 items.)");
141}
142
143void
144start_info (object * op)
145{
146 char buf[MAX_BUF];
147
148 sprintf (buf, "Welcome to Crossfire, v%s!", VERSION);
149 new_draw_info (NDI_UNIQUE, 0, op, buf);
150 new_draw_info (NDI_UNIQUE, 0, op, "Press `?' for help");
151 new_draw_info (NDI_UNIQUE, 0, op, " ");
152 new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_DK_ORANGE, 5, op, "%s entered the game.", &op->name);
153 if (!op->contr->name_changed)
154 {
155 new_draw_info (NDI_UNIQUE, 0, op, "Note that you must set your name with the name");
156 new_draw_info (NDI_UNIQUE, 0, op, "command to enter the highscore list.");
157 new_draw_info (NDI_UNIQUE, 0, op, "(You can also use the crossfire.name X-resource.)");
158 }
159}
160
161/* Really, there is no reason to crypt the passwords any system. But easier
162 * to just leave this enabled for backward compatibility. Put the
163 * simple case at top - no encryption - makes it easier to read.
164 */
165char *
166crypt_string (char *str, char *salt)
167{
168#if defined(WIN32) || (defined(__FreeBSD__) && !defined(HAVE_LIBDES))
169 return (str);
170#else
171 static char *c = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
172 char s[2];
173
174 if (salt == NULL)
175 s[0] = c[RANDOM () % (int) strlen (c)], s[1] = c[RANDOM () % (int) strlen (c)];
176 else
177 s[0] = salt[0], s[1] = salt[1];
178
179# ifdef HAVE_LIBDES
180 return (char *) des_crypt (str, s);
181# endif
182 /* Default case - just use crypt */
183 return (char *) crypt (str, s);
184#endif
185}
186
187int
188check_password (char *typed, char *crypted)
189{
190 return !strcmp (crypt_string (typed, crypted), crypted);
191} 42}
192 43
193/* This is a basic little function to put the player back to his 44/* This is a basic little function to put the player back to his
194 * savebed. We do some error checking - its possible that the 45 * savebed. We do some error checking - its possible that the
195 * savebed map may no longer exist, so we make sure the player 46 * savebed map may no longer exist, so we make sure the player
196 * goes someplace. 47 * goes someplace.
197 */ 48 */
198void 49void
199enter_player_savebed (object * op) 50enter_player_savebed (object *op)
200{ 51{
201 mapstruct *oldmap = op->map; 52 op->player_goto (op->contr->savebed_map, op->contr->bed_x, op->contr->bed_y);
202 object *tmp;
203
204 tmp = get_object ();
205
206 EXIT_PATH (tmp) = op->contr->savebed_map;
207 EXIT_X (tmp) = op->contr->bed_x;
208 EXIT_Y (tmp) = op->contr->bed_y;
209 enter_exit (op, tmp);
210 /* If the player has not changed maps and the name does not match
211 * that of the savebed, his savebed map is gone. Lets go back
212 * to the emergency path. Update what the players savebed is
213 * while we're at it.
214 */
215 if (oldmap == op->map && strcmp (op->contr->savebed_map, oldmap->path))
216 {
217 LOG (llevDebug, "Player %s savebed location %s is invalid - going to emergency location (%s)\n",
218 settings.emergency_mapname, &op->name, op->contr->savebed_map);
219 strcpy (op->contr->savebed_map, settings.emergency_mapname);
220 op->contr->bed_x = settings.emergency_x;
221 op->contr->bed_y = settings.emergency_y;
222 EXIT_PATH (tmp) = op->contr->savebed_map;
223 EXIT_X (tmp) = op->contr->bed_x;
224 EXIT_Y (tmp) = op->contr->bed_y;
225 enter_exit (op, tmp);
226 }
227 free_object (tmp);
228}
229
230/* All this really is is a glorified remove_object that also updates
231 * the counts on the map if needed.
232 */
233void
234leave_map (object * op)
235{
236 mapstruct *oldmap = op->map;
237
238 remove_ob (op);
239
240 if (oldmap)
241 {
242 if (!op->contr->hidden)
243 oldmap->players--;
244 if (oldmap->players <= 0)
245 { /* can be less than zero due to errors in tracking this */
246 set_map_timeout (oldmap);
247 }
248 }
249} 53}
250 54
251/* 55/*
252 * enter_map(): Moves the player and pets from current map (if any) to 56 * enter_map(): Moves the player and pets from current map (if any) to
253 * new map. map, x, y must be set. map is the map we are moving the 57 * new map. map, x, y must be set. map is the map we are moving the
254 * player to - it could be the map he just came from if the load failed for 58 * player to - it could be the map he just came from if the load failed for
255 * whatever reason. If default map coordinates are to be used, then 59 * whatever reason. If default map coordinates are to be used, then
256 * the function that calls this should figure them out. 60 * the function that calls this should figure them out.
257 */ 61 */
258static void 62void
259enter_map (object * op, mapstruct * newmap, int x, int y) 63object::enter_map (maptile *newmap, int x, int y)
260{ 64{
261 mapstruct *oldmap = op->map; 65 if (destroyed () || !newmap || newmap->in_memory != MAP_ACTIVE)
66 return;
262 67
263 if (out_of_map (newmap, x, y)) 68 if (out_of_map (newmap, x, y))
264 { 69 {
265 LOG (llevError, "enter_map: supplied coordinates are not within the map! (%s: %d, %d)\n", newmap->path, x, y); 70 LOG (llevError, "enter_map: supplied coordinates are not within the map! (%s: %d, %d)\n", &newmap->path, x, y);
266 x = MAP_ENTER_X (newmap); 71 x = newmap->enter_x;
267 y = MAP_ENTER_Y (newmap); 72 y = newmap->enter_y;
268 if (out_of_map (newmap, x, y)) 73 if (out_of_map (newmap, x, y))
269 { 74 {
270 LOG (llevError, "enter_map: map %s provides invalid default enter location (%d, %d) > (%d, %d)\n", 75 LOG (llevError, "enter_map: map %s provides invalid default enter location (%d, %d) > (%d, %d)\n",
271 newmap->path, x, y, MAP_WIDTH (newmap), MAP_HEIGHT (newmap)); 76 &newmap->path, x, y, newmap->width, newmap->height);
272 new_draw_info (NDI_UNIQUE, 0, op, "The exit is closed"); 77 new_draw_info (NDI_UNIQUE, 0, this, "The exit is closed");
273 return; 78 return;
274 } 79 }
275 } 80 }
81
82 if (contr && map != newmap && map)
83 if (INVOKE_MAP (LEAVE, map, ARG_PLAYER (contr)))
84 return;
85
86 // remove, so stupid blocked does not trigger a failure
87 remove ();
88
276 /* try to find a spot for the player */ 89 /* try to find a spot for the player */
277 if (ob_blocked (op, newmap, x, y)) 90 if (blocked (newmap, x, y))
278 { /* First choice blocked */ 91 { /* First choice blocked */
279 /* We try to find a spot for the player, starting closest in. 92 /* We try to find a spot for the player, starting closest in.
280 * We could use find_first_free_spot, but that doesn't randomize it at all, 93 * We could use find_first_free_spot, but that doesn't randomize it at all,
281 * So for example, if the north space is free, you would always end up there even 94 * So for example, if the north space is free, you would always end up there even
282 * if other spaces around are available. 95 * if other spaces around are available.
283 * Note that for the second and third calls, we could start at a position other 96 * Note that for the second and third calls, we could start at a position other
284 * than one, but then we could end up on the other side of walls and so forth. 97 * than one, but then we could end up on the other side of walls and so forth.
285 */ 98 */
286 int i = find_free_spot (op, newmap, x, y, 1, SIZEOFFREE1 + 1); 99 int i = find_free_spot (this, newmap, x, y, 1, SIZEOFFREE1 + 1);
100
287 if (i == -1) 101 if (i < 0)
288 { 102 {
289 i = find_free_spot (op, newmap, x, y, 1, SIZEOFFREE2 + 1); 103 i = find_free_spot (this, newmap, x, y, 1, SIZEOFFREE2 + 1);
290 if (i == -1) 104 if (i < 0)
291 i = find_free_spot (op, newmap, x, y, 1, SIZEOFFREE); 105 i = find_free_spot (this, newmap, x, y, 1, SIZEOFFREE);
292 } 106 }
107
293 if (i != -1) 108 if (i >= 0)
294 { 109 {
295 x += freearr_x[i]; 110 x += freearr_x[i];
296 y += freearr_y[i]; 111 y += freearr_y[i];
297 } 112 }
298 else 113 else
299 {
300 /* not much we can do in this case. */ 114 /* not much we can do in this case. */
301 LOG (llevInfo, "enter_map: Could not find free spot for player - will dump on top of object (%s: %d, %d)\n", newmap->path, x, y); 115 LOG (llevInfo, "enter_map: Could not find free spot for player - will dump on top of object (%s: %d, %d)\n", &newmap->path, x, y);
302 }
303 } /* end if looking for free spot */
304
305 if (op->map != NULL)
306 { 116 }
307 INVOKE_PLAYER (MAP_CHANGE, op->contr, ARG_MAP (op->map), ARG_MAP (newmap)); 117
308 INVOKE_MAP (LEAVE, op->map, ARG_PLAYER (op->contr)); 118 if (contr && map != newmap)
309 } 119 {
120 if (INVOKE_PLAYER (MAP_CHANGE, contr, ARG_MAP (newmap), ARG_INT (x), ARG_INT (y)))
121 return;
310 122
311 /* If it is a player login, he has yet to be inserted anyplace. 123 if (INVOKE_MAP (ENTER, newmap, ARG_PLAYER (contr), ARG_INT (x), ARG_INT (y)))
312 * otherwise, we need to deal with removing the player here. 124 return;
313 */ 125 }
314 if (!QUERY_FLAG (op, FLAG_REMOVED))
315 remove_ob (op);
316 126
317 /* remove_ob clears these so they must be reset after the remove_ob call */ 127 newmap->insert (this, x, y, 0, INS_NO_WALK_ON);
318 op->x = x;
319 op->y = y;
320 op->map = newmap;
321 insert_ob_in_map (op, op->map, NULL, INS_NO_WALK_ON);
322 128
323 INVOKE_MAP (ENTER, op->map, ARG_PLAYER (op->contr)); 129 enemy = 0;
324 130
325 if (!op->contr->hidden)
326 newmap->players++;
327
328 newmap->timeout = 0;
329 op->enemy = NULL;
330
331 if (op->contr) 131 if (contr)
332 { 132 {
333 strcpy (op->contr->maplevel, newmap->path); 133 contr->maplevel = newmap->path;
334 op->contr->count = 0; 134 contr->count = 0;
335 } 135 }
336 136
337 /* Update any golems */ 137 /* Update any golems */
338 if (op->type == PLAYER && op->contr->ranges[range_golem] != NULL) 138 if (type == PLAYER)
339 { 139 if (object *golem = contr->golem)
340 int i = find_free_spot (op->contr->ranges[range_golem], newmap,
341 x, y, 1, SIZEOFFREE);
342 remove_ob (op->contr->ranges[range_golem]);
343 if (i == -1)
344 { 140 {
345 remove_friendly_object (op->contr->ranges[range_golem]); 141 int i = find_free_spot (golem, newmap, x, y, 1, SIZEOFFREE);
346 free_object (op->contr->ranges[range_golem]); 142
347 op->contr->ranges[range_golem] = NULL; 143 if (i < 0)
348 op->contr->golem_count = 0; 144 golem->drop_and_destroy ();
349 }
350 else 145 else
351 {
352 object *tmp;
353 for (tmp = op->contr->ranges[range_golem]; tmp != NULL; tmp = tmp->more)
354 { 146 {
355 tmp->x = x + freearr_x[i] + (tmp->arch == NULL ? 0 : tmp->arch->clone.x); 147 newmap->insert (golem, x + freearr_x[i], y + freearr_y[i]);
356 tmp->y = y + freearr_y[i] + (tmp->arch == NULL ? 0 : tmp->arch->clone.y); 148 golem->direction = find_dir_2 (golem->x - x, golem->y - y);
357 tmp->map = newmap;
358 } 149 }
359 insert_ob_in_map (op->contr->ranges[range_golem], newmap, NULL, 0);
360 op->contr->ranges[range_golem]->direction =
361 find_dir_2 (op->x - op->contr->ranges[range_golem]->x, op->y - op->contr->ranges[range_golem]->y);
362 } 150 }
363 }
364 op->direction = 0;
365 151
366 /* since the players map is already loaded, we don't need to worry 152 /* since the players map is already loaded, we don't need to worry
367 * about pending objects. 153 * about pending objects.
368 */ 154 */
369 remove_all_pets (newmap); 155 remove_all_pets (newmap);
370
371 /* If the player is changing maps, we need to do some special things
372 * Do this after the player is on the new map - otherwise the force swap of the
373 * old map does not work.
374 */
375 if (oldmap != newmap)
376 {
377 if (oldmap) /* adjust old map */
378 {
379 oldmap->players--;
380
381 if (oldmap->players <= 0) /* can be less than zero due to errors in tracking this */
382 set_map_timeout (oldmap);
383 }
384 }
385} 156}
386
387void
388set_map_timeout (mapstruct * oldmap)
389{
390#if MAP_MAXTIMEOUT
391 oldmap->timeout = MAP_TIMEOUT (oldmap);
392 /* Do MINTIMEOUT first, so that MAXTIMEOUT is used if that is
393 * lower than the min value.
394 */
395#if MAP_MINTIMEOUT
396 if (oldmap->timeout < MAP_MINTIMEOUT)
397 {
398 oldmap->timeout = MAP_MINTIMEOUT;
399 }
400#endif
401 if (oldmap->timeout > MAP_MAXTIMEOUT)
402 {
403 oldmap->timeout = MAP_MAXTIMEOUT;
404 }
405#else
406 /* save out the map */
407 swap_map (oldmap);
408#endif /* MAP_MAXTIMEOUT */
409}
410
411
412/* clean_path takes a path and replaces all / with _
413 * We do a strcpy so that we do not change the original string.
414 */
415char *
416clean_path (const char *file)
417{
418 static char newpath[MAX_BUF], *cp;
419
420 strncpy (newpath, file, MAX_BUF - 1);
421 newpath[MAX_BUF - 1] = '\0';
422 for (cp = newpath; *cp != '\0'; cp++)
423 {
424 if (*cp == '/')
425 *cp = '_';
426 }
427 return newpath;
428}
429
430
431/* unclean_path takes a path and replaces all _ with /
432 * This basically undoes clean path.
433 * We do a strcpy so that we do not change the original string.
434 * We are smart enough to start after the last / in case we
435 * are getting passed a string that points to a unique map
436 * path.
437 */
438char *
439unclean_path (const char *src)
440{
441 static char newpath[MAX_BUF], *cp;
442
443 cp = strrchr (src, '/');
444 if (cp)
445 strncpy (newpath, cp + 1, MAX_BUF - 1);
446 else
447 strncpy (newpath, src, MAX_BUF - 1);
448 newpath[MAX_BUF - 1] = '\0';
449
450 for (cp = newpath; *cp != '\0'; cp++)
451 {
452 if (*cp == '_')
453 *cp = '/';
454 }
455 return newpath;
456}
457
458
459/* The player is trying to enter a randomly generated map. In this case, generate the
460 * random map as needed.
461 */
462
463static void
464enter_random_map (object * pl, object * exit_ob)
465{
466 mapstruct *new_map;
467 char newmap_name[HUGE_BUF], *cp;
468 static int reference_number = 0;
469 RMParms rp;
470
471 memset (&rp, 0, sizeof (RMParms));
472 rp.Xsize = -1;
473 rp.Ysize = -1;
474 rp.region = get_region_by_map (exit_ob->map);
475 if (exit_ob->msg)
476 set_random_map_variable (&rp, exit_ob->msg);
477 rp.origin_x = exit_ob->x;
478 rp.origin_y = exit_ob->y;
479 strcpy (rp.origin_map, pl->map->path);
480
481 /* If we have a final_map, use it as a base name to give some clue
482 * as where the player is. Otherwise, use the origin map.
483 * Take the last component (after the last slash) to give
484 * shorter names without bogus slashes.
485 */
486 if (rp.final_map[0])
487 {
488 cp = strrchr (rp.final_map, '/');
489 if (!cp)
490 cp = rp.final_map;
491 }
492 else
493 {
494 char buf[HUGE_BUF];
495
496 cp = strrchr (rp.origin_map, '/');
497 if (!cp)
498 cp = rp.origin_map;
499 /* Need to strip of any trailing digits, if it has them */
500 strcpy (buf, cp);
501 while (isdigit (buf[strlen (buf) - 1]))
502 buf[strlen (buf) - 1] = 0;
503 cp = buf;
504 }
505
506 sprintf (newmap_name, "/random/%s%04d", cp + 1, reference_number++);
507
508 /* now to generate the actual map. */
509 new_map = generate_random_map (newmap_name, &rp);
510
511 /* Update the exit_ob so it now points directly at the newly created
512 * random maps. Not that it is likely to happen, but it does mean that a
513 * exit in a unique map leading to a random map will not work properly.
514 * It also means that if the created random map gets reset before
515 * the exit leading to it, that the exit will no longer work.
516 */
517 if (new_map)
518 {
519 int x, y;
520 x = EXIT_X (exit_ob) = MAP_ENTER_X (new_map);
521 y = EXIT_Y (exit_ob) = MAP_ENTER_Y (new_map);
522 EXIT_PATH (exit_ob) = newmap_name;
523 strcpy (new_map->path, newmap_name);
524 enter_map (pl, new_map, x, y);
525 }
526}
527
528/* The player is trying to enter a non-randomly generated template map. In this
529 * case, use a map file for a template
530 */
531
532static void
533enter_fixed_template_map (object * pl, object * exit_ob)
534{
535 mapstruct *new_map;
536 char tmpnum[32], exitpath[HUGE_BUF], resultname[HUGE_BUF], tmpstring[HUGE_BUF], *sourcemap;
537 const char *new_map_name;
538
539 /* Split the exit path string into two parts, one
540 * for where to store the map, and one for were
541 * to generate the map from.
542 */
543 snprintf (exitpath, sizeof (exitpath), "%s", EXIT_PATH (exit_ob) + 2);
544 sourcemap = strchr (exitpath, '!');
545 if (!sourcemap)
546 {
547 new_draw_info_format (NDI_UNIQUE, 0, pl, "The %s is closed.", &exit_ob->name);
548 /* Should only occur when no source map is set.
549 */
550 LOG (llevError, "enter_fixed_template_map: Exit %s (%d,%d) on map %s has no source template.\n",
551 &exit_ob->name, exit_ob->x, exit_ob->y, exit_ob->map->path);
552 return;
553 }
554 *sourcemap++ = '\0';
555
556 /* If we are not coming from a template map, we can use relative directories
557 * for the map to generate from.
558 */
559 if (!exit_ob->map->templatemap)
560 {
561 sourcemap = path_combine_and_normalize (exit_ob->map->path, sourcemap);
562 }
563
564 /* Do replacement of %x, %y, and %n to the x coord of the exit, the y coord
565 * of the exit, and the name of the map the exit is on, respectively.
566 */
567 sprintf (tmpnum, "%d", exit_ob->x);
568 replace (exitpath, "%x", tmpnum, resultname, sizeof (resultname));
569
570 sprintf (tmpnum, "%d", exit_ob->y);
571 sprintf (tmpstring, "%s", resultname);
572 replace (tmpstring, "%y", tmpnum, resultname, sizeof (resultname));
573
574 sprintf (tmpstring, "%s", resultname);
575 replace (tmpstring, "%n", exit_ob->map->name, resultname, sizeof (resultname));
576
577 /* If we are coming from another template map, use reletive paths unless
578 * indicated otherwise.
579 */
580 if (exit_ob->map->templatemap && (resultname[0] != '/'))
581 {
582 new_map_name = path_combine_and_normalize (exit_ob->map->path, resultname);
583 }
584 else
585 {
586 new_map_name = create_template_pathname (resultname);
587 }
588
589 /* Attempt to load the map, if unable to, then
590 * create the map from the template.
591 */
592 new_map = ready_map_name (new_map_name, MAP_PLAYER_UNIQUE);
593 if (!new_map)
594 {
595 new_map = load_original_map (create_pathname (sourcemap), MAP_PLAYER_UNIQUE);
596 if (new_map)
597 fix_auto_apply (new_map);
598 }
599
600 if (new_map)
601 {
602 /* set the path of the map to where it should be
603 * so we don't just save over the source map.
604 */
605 strcpy (new_map->path, new_map_name);
606 new_map->templatemap = 1;
607 enter_map (pl, new_map, EXIT_X (exit_ob), EXIT_Y (exit_ob));
608 }
609 else
610 {
611 new_draw_info_format (NDI_UNIQUE, 0, pl, "The %s is closed.", &exit_ob->name);
612 /* Should only occur when an invalid source map is set.
613 */
614 LOG (llevDebug, "enter_fixed_template_map: Exit %s (%d,%d) on map %s leads no where.\n",
615 &exit_ob->name, exit_ob->x, exit_ob->y, exit_ob->map->path);
616 }
617}
618
619
620/* The player is trying to enter a randomly generated template map. In this
621 * case, generate the map as needed.
622 */
623
624static void
625enter_random_template_map (object * pl, object * exit_ob)
626{
627 mapstruct *new_map;
628 char tmpnum[32], resultname[HUGE_BUF], tmpstring[HUGE_BUF];
629 const char *new_map_name;
630 RMParms rp;
631
632 /* Do replacement of %x, %y, and %n to the x coord of the exit, the y coord
633 * of the exit, and the name of the map the exit is on, respectively.
634 */
635 sprintf (tmpnum, "%d", exit_ob->x);
636 replace (EXIT_PATH (exit_ob) + 3, "%x", tmpnum, resultname, sizeof (resultname));
637
638 sprintf (tmpnum, "%d", exit_ob->y);
639 sprintf (tmpstring, "%s", resultname);
640 replace (tmpstring, "%y", tmpnum, resultname, sizeof (resultname));
641
642 sprintf (tmpstring, "%s", resultname);
643 replace (tmpstring, "%n", exit_ob->map->name, resultname, sizeof (resultname));
644
645 /* If we are coming from another template map, use reletive paths unless
646 * indicated otherwise.
647 */
648 if (exit_ob->map->templatemap && (resultname[0] != '/'))
649 {
650 new_map_name = path_combine_and_normalize (exit_ob->map->path, resultname);
651 }
652 else
653 {
654 new_map_name = create_template_pathname (resultname);
655 }
656
657 new_map = ready_map_name (new_map_name, MAP_PLAYER_UNIQUE);
658 if (!new_map)
659 {
660 memset (&rp, 0, sizeof (RMParms));
661 rp.Xsize = -1;
662 rp.Ysize = -1;
663 rp.region = get_region_by_map (exit_ob->map);
664 if (exit_ob->msg)
665 set_random_map_variable (&rp, exit_ob->msg);
666 rp.origin_x = exit_ob->x;
667 rp.origin_y = exit_ob->y;
668 strcpy (rp.origin_map, pl->map->path);
669
670 /* now to generate the actual map. */
671 new_map = generate_random_map (new_map_name, &rp);
672 }
673
674
675 /* Update the exit_ob so it now points directly at the newly created
676 * random maps. Not that it is likely to happen, but it does mean that a
677 * exit in a unique map leading to a random map will not work properly.
678 * It also means that if the created random map gets reset before
679 * the exit leading to it, that the exit will no longer work.
680 */
681 if (new_map)
682 {
683 int x, y;
684 x = EXIT_X (exit_ob) = MAP_ENTER_X (new_map);
685 y = EXIT_Y (exit_ob) = MAP_ENTER_Y (new_map);
686 new_map->templatemap = 1;
687 enter_map (pl, new_map, x, y);
688 }
689}
690
691
692/* Code to enter/detect a character entering a unique map.
693 */
694static void
695enter_unique_map (object * op, object * exit_ob)
696{
697 char apartment[HUGE_BUF];
698 mapstruct *newmap;
699
700 if (EXIT_PATH (exit_ob)[0] == '/')
701 {
702 sprintf (apartment, "%s/%s/%s/%s", settings.localdir, settings.playerdir, &op->name, clean_path (EXIT_PATH (exit_ob)));
703 newmap = ready_map_name (apartment, MAP_PLAYER_UNIQUE);
704 if (!newmap)
705 {
706 newmap = load_original_map (create_pathname (EXIT_PATH (exit_ob)), MAP_PLAYER_UNIQUE);
707 if (newmap)
708 fix_auto_apply (newmap);
709 }
710 }
711 else
712 { /* relative directory */
713 char reldir[HUGE_BUF], tmpc[HUGE_BUF], *cp;
714
715 if (exit_ob->map->unique)
716 {
717
718 strcpy (reldir, unclean_path (exit_ob->map->path));
719
720 /* Need to copy this over, as clean_path only has one static return buffer */
721 strcpy (tmpc, clean_path (reldir));
722 /* Remove final component, if any */
723 if ((cp = strrchr (tmpc, '_')) != NULL)
724 *cp = 0;
725
726 sprintf (apartment, "%s/%s/%s/%s_%s", settings.localdir, settings.playerdir, &op->name, tmpc, clean_path (EXIT_PATH (exit_ob)));
727
728 newmap = ready_map_name (apartment, MAP_PLAYER_UNIQUE);
729 if (!newmap)
730 {
731 newmap = load_original_map (create_pathname (path_combine_and_normalize (reldir, EXIT_PATH (exit_ob))), MAP_PLAYER_UNIQUE);
732 if (newmap)
733 fix_auto_apply (newmap);
734 }
735 }
736 else
737 {
738 /* The exit is unique, but the map we are coming from is not unique. So
739 * use the basic logic - don't need to demangle the path name
740 */
741 sprintf (apartment, "%s/%s/%s/%s", settings.localdir,
742 settings.playerdir, &op->name, clean_path (path_combine_and_normalize (exit_ob->map->path, EXIT_PATH (exit_ob))));
743 newmap = ready_map_name (apartment, MAP_PLAYER_UNIQUE);
744 if (!newmap)
745 {
746 newmap = ready_map_name (path_combine_and_normalize (exit_ob->map->path, EXIT_PATH (exit_ob)), 0);
747 if (newmap)
748 fix_auto_apply (newmap);
749 }
750 }
751 }
752
753 if (newmap)
754 {
755 strcpy (newmap->path, apartment);
756 newmap->unique = 1;
757 enter_map (op, newmap, EXIT_X (exit_ob), EXIT_Y (exit_ob));
758 }
759 else
760 {
761 new_draw_info_format (NDI_UNIQUE, 0, op, "The %s is closed.", &exit_ob->name);
762 /* Perhaps not critical, but I would think that the unique maps
763 * should be new enough this does not happen. This also creates
764 * a strange situation where some players could perhaps have visited
765 * such a map before it was removed, so they have the private
766 * map, but other players can't get it anymore.
767 */
768 LOG (llevDebug, "enter_unique_map: Exit %s (%d,%d) on map %s is leads no where.\n",
769 &exit_ob->name, exit_ob->x, exit_ob->y, exit_ob->map->path);
770 }
771
772}
773
774
775/* Tries to move 'op' to exit_ob. op is the character or monster that is
776 * using the exit, where exit_ob is the exit object (boat, door, teleporter,
777 * etc.) if exit_ob is null, then op->contr->maplevel contains that map to
778 * move the object to. This is used when loading the player.
779 *
780 * Largely redone by MSW 2001-01-21 - this function was overly complex
781 * and had some obscure bugs.
782 */
783
784void
785enter_exit (object * op, object * exit_ob)
786{
787#define PORTAL_DESTINATION_NAME "Town portal destination" /* this one should really be in a header file */
788 object *tmp;
789 /* It may be nice to support other creatures moving across
790 * exits, but right now a lot of the code looks at op->contr,
791 * so thta is an RFE.
792 */
793 if (op->type != PLAYER)
794 return;
795
796 /* First, lets figure out what map the player is going to go to */
797 if (exit_ob)
798 {
799
800 /* check to see if we make a template map */
801 if (EXIT_PATH (exit_ob) && EXIT_PATH (exit_ob)[1] == '@')
802 {
803 if (EXIT_PATH (exit_ob)[2] == '!')
804 {
805 /* generate a template map randomly */
806 enter_random_template_map (op, exit_ob);
807 }
808 else
809 {
810 /* generate a template map from a fixed template */
811 enter_fixed_template_map (op, exit_ob);
812 }
813 }
814 /* check to see if we make a randomly generated map */
815 else if (EXIT_PATH (exit_ob) && EXIT_PATH (exit_ob)[1] == '!')
816 {
817 enter_random_map (op, exit_ob);
818 }
819 else if (QUERY_FLAG (exit_ob, FLAG_UNIQUE))
820 {
821 enter_unique_map (op, exit_ob);
822 }
823 else
824 {
825 int x = EXIT_X (exit_ob), y = EXIT_Y (exit_ob);
826 /* 'Normal' exits that do not do anything special
827 * Simple enough we don't need another routine for it.
828 */
829 mapstruct *newmap;
830 if (exit_ob->map)
831 {
832 newmap = ready_map_name (path_combine_and_normalize (exit_ob->map->path, EXIT_PATH (exit_ob)), 0);
833 /* Random map was previously generated, but is no longer about. Lets generate a new
834 * map.
835 */
836 if (!newmap && !strncmp (EXIT_PATH (exit_ob), "/random/", 8))
837 {
838 /* Maps that go down have a message set. However, maps that go
839 * up, don't. If the going home has reset, there isn't much
840 * point generating a random map, because it won't match the maps.
841 */
842 if (exit_ob->msg)
843 {
844 enter_random_map (op, exit_ob);
845 }
846 else
847 {
848 new_draw_info_format (NDI_UNIQUE, 0, op, "The %s is closed.", &exit_ob->name);
849 return;
850 }
851
852 /* For exits that cause damages (like pits). Don't know if any
853 * random maps use this or not.
854 */
855 if (exit_ob->stats.dam && op->type == PLAYER)
856 hit_player (op, exit_ob->stats.dam, exit_ob, exit_ob->attacktype, 1);
857 return;
858 }
859 }
860 else
861 {
862 /* For word of recall and other force objects
863 * They contain the full pathname of the map to go back to,
864 * so we don't need to normalize it.
865 * But we do need to see if it is unique or not
866 */
867 if (!strncmp (EXIT_PATH (exit_ob), settings.localdir, strlen (settings.localdir)))
868 newmap = ready_map_name (EXIT_PATH (exit_ob), MAP_PLAYER_UNIQUE);
869 else
870 newmap = ready_map_name (EXIT_PATH (exit_ob), 0);
871 }
872 if (!newmap)
873 {
874 if (exit_ob->name)
875 new_draw_info_format (NDI_UNIQUE, 0, op, "The %s is closed.", &exit_ob->name);
876 /* don't cry to momma if name is not set - as in tmp objects
877 * used by the savebed code and character creation */
878 return;
879 }
880
881 /* This supports the old behaviour, but it really should not be used.
882 * I will note for example that with this method, it is impossible to
883 * set 0,0 destination coordinates. Really, if we want to support
884 * using the new maps default coordinates, the exit ob should use
885 * something like -1, -1 so it is clear to do that.
886 */
887 if (x == 0 && y == 0)
888 {
889 x = MAP_ENTER_X (newmap);
890 y = MAP_ENTER_Y (newmap);
891 LOG (llevDebug, "enter_exit: Exit %s (%d,%d) on map %s is 0 destination coordinates\n",
892 &exit_ob->name, exit_ob->x, exit_ob->y, exit_ob->map ? exit_ob->map->path : "<nil>");
893 }
894
895 /* mids 02/13/2002 if exit is damned, update players death & WoR home-position and delete town portal */
896 if (QUERY_FLAG (exit_ob, FLAG_DAMNED))
897 {
898 /* remove an old force with a slaying field == PORTAL_DESTINATION_NAME */
899 for (tmp = op->inv; tmp != NULL; tmp = tmp->below)
900 {
901 if (tmp->type == FORCE && tmp->slaying && !strcmp (tmp->slaying, PORTAL_DESTINATION_NAME))
902 break;
903 }
904 if (tmp)
905 {
906 remove_ob (tmp);
907 free_object (tmp);
908 }
909
910 strcpy (op->contr->savebed_map, path_combine_and_normalize (exit_ob->map->path, EXIT_PATH (exit_ob)));
911 op->contr->bed_x = EXIT_X (exit_ob), op->contr->bed_y = EXIT_Y (exit_ob);
912 save_player (op, 1);
913 /* LOG(llevDebug,"enter_exit: Taking damned exit %s to (%d,%d) on map %s\n",
914 * exit_ob->name?exit_ob->name:"(none)", exit_ob->x, exit_ob->y,
915 * path_combine_and_normalize(exit_ob->map->path, EXIT_PATH(exit_ob))); */
916 }
917
918 enter_map (op, newmap, x, y);
919 }
920 /* For exits that cause damages (like pits) */
921 if (exit_ob->stats.dam && op->type == PLAYER)
922 hit_player (op, exit_ob->stats.dam, exit_ob, exit_ob->attacktype, 1);
923 }
924 else
925 {
926 int flags = 0;
927 mapstruct *newmap;
928
929
930 /* Hypothetically, I guess its possible that a standard map matches
931 * the localdir, but that seems pretty unlikely - unlikely enough that
932 * I'm not going to attempt to try to deal with that possibility.
933 * We use the fact that when a player saves on a unique map, it prepends
934 * the localdir to that name. So its an easy way to see of the map is
935 * unique or not.
936 */
937 if (!strncmp (op->contr->maplevel, settings.localdir, strlen (settings.localdir)))
938 flags = MAP_PLAYER_UNIQUE;
939
940 /* newmap returns the map (if already loaded), or loads it for
941 * us.
942 */
943 newmap = ready_map_name (op->contr->maplevel, flags);
944 if (!newmap)
945 {
946 LOG (llevError, "enter_exit: Pathname to map does not exist! (%s)\n", op->contr->maplevel);
947 newmap = ready_map_name (settings.emergency_mapname, 0);
948 op->x = settings.emergency_x;
949 op->y = settings.emergency_y;
950 /* If we can't load the emergency map, something is probably really
951 * screwed up, so bail out now.
952 */
953 if (!newmap)
954 {
955 LOG (llevError, "enter_exit: could not load emergency map? Fatal error\n");
956 abort ();
957 }
958 }
959 enter_map (op, newmap, op->x, op->y);
960 }
961}
962
963/*
964 * process_active_maps(): Works like process_events(), but it only
965 * processes maps which a player is on.
966 *
967 */
968
969#if 0 // dead code, schmorp
970void
971process_active_maps ()
972{
973 for (mapstruct * map = first_map; map != NULL; map = map->next)
974 if (map->in_memory == MAP_IN_MEMORY)
975 if (players_on_map (map, TRUE))
976 process_events (map);
977}
978#endif
979 157
980/* process_players1 and process_players2 do all the player related stuff. 158/* process_players1 and process_players2 do all the player related stuff.
981 * I moved it out of process events and process_map. This was to some 159 * I moved it out of process events and process_map. This was to some
982 * extent for debugging as well as to get a better idea of the time used 160 * extent for debugging as well as to get a better idea of the time used
983 * by the various functions. process_players1() does the processing before 161 * by the various functions. process_players1() does the processing before
984 * objects have been updated, process_players2() does the processing that 162 * objects have been updated, process_players2() does the processing that
985 * is needed after the players have been updated. 163 * is needed after the players have been updated.
986 */ 164 */
987 165static void
988void 166process_players1 ()
989process_players1 (mapstruct * map)
990{ 167{
991 int flag;
992 player *pl, *plnext;
993
994 /* Basically, we keep looping until all the players have done their actions. */ 168 /* Basically, we keep looping until all the players have done their actions. */
995 for (flag = 1; flag != 0;) 169 for (int flag = 1; flag != 0;)
996 { 170 {
997 flag = 0; 171 flag = 0;
998 for (pl = first_player; pl != NULL; pl = plnext) 172 for_all_players (pl)
999 { 173 {
1000 plnext = pl->next; /* In case a player exits the game in handle_player() */ 174 pl->refcnt_chk ();
1001 175
1002 if (pl->ob == NULL) 176 if (expect_false (!pl->ob || !pl->ns || !pl->ob->active))
1003 continue; 177 continue;
1004 178
1005 if (map != NULL && pl->ob->map != map)
1006 continue;
1007
1008 if (pl->ob->speed_left > 0)
1009 {
1010 if (handle_newcs_player (pl->ob)) 179 if (handle_newcs_player (pl->ob))
1011 flag = 1; 180 flag = 1;
1012 } /* end if player has speed left */
1013
1014 /* If the player is not actively playing, don't make a
1015 * backup save - nothing to save anyway. Plus, the
1016 * map may not longer be valid. This can happen when the
1017 * player quits - they exist for purposes of tracking on the map,
1018 * but don't actually reside on any actual map.
1019 */
1020 if (QUERY_FLAG (pl->ob, FLAG_REMOVED))
1021 continue;
1022
1023#ifdef AUTOSAVE
1024 /* check for ST_PLAYING state so that we don't try to save off when
1025 * the player is logging in.
1026 */
1027 if ((pl->last_save_tick + AUTOSAVE) < (uint32) pticks && pl->state == ST_PLAYING)
1028 {
1029 /* Don't save the player on unholy ground. Instead, increase the
1030 * tick time so it will be about 10 seconds before we try and save
1031 * again.
1032 */
1033// if (get_map_flags(pl->ob->map, NULL, pl->ob->x, pl->ob->y, NULL, NULL) & P_NO_CLERIC) {
1034// pl->last_save_tick += 100;
1035// } else {
1036 save_player (pl->ob, 1);
1037 pl->last_save_tick = pticks;
1038// }
1039 } 181 }
1040#endif
1041 } /* end of for loop for all the players */
1042 } /* for flag */
1043 for (pl = first_player; pl != NULL; pl = pl->next)
1044 { 182 }
1045 if (map != NULL && (pl->ob == NULL || pl->ob->map != map)) 183
184 for_all_players (pl)
185 {
186 object *ob = pl->ob;
187
188 // process_objects destroys the speed_left value
189 pl->speed_left_save = ob->speed_left;
190
191 if (expect_false (!ob || !pl->ns || !ob->active))
1046 continue; 192 continue;
1047 if (settings.casting_time == TRUE) 193
1048 {
1049 if (pl->ob->casting_time > 0)
1050 {
1051 pl->ob->casting_time--;
1052 pl->ob->start_holding = 1;
1053 }
1054 /* set spell_state so we can update the range in stats field */
1055 if ((pl->ob->casting_time == 0) && (pl->ob->start_holding == 1))
1056 {
1057 pl->ob->start_holding = 0;
1058 }
1059 }
1060 do_some_living (pl->ob); 194 do_some_living (ob);
1061 /* draw(pl->ob);*//* updated in socket code */
1062 } 195 }
1063} 196}
1064 197
1065void 198static void
1066process_players2 (mapstruct * map) 199process_players2 ()
1067{ 200{
1068 player *pl;
1069
1070 /* Then check if any players should use weapon-speed instead of speed */ 201 /* Then check if any players should use weapon-speed instead of speed */
1071 for (pl = first_player; pl != NULL; pl = pl->next) 202 for_all_players (pl)
1072 { 203 {
1073 if (map != NULL) 204 // restore speed_left value saved by process_players1
1074 {
1075 if (pl->ob == NULL || QUERY_FLAG (pl->ob, FLAG_REMOVED))
1076 continue;
1077 else if (pl->loading != NULL) /* Player is blocked */
1078 pl->ob->speed_left -= pl->ob->speed;
1079 if (pl->ob->map != map)
1080 continue;
1081 }
1082
1083 /* The code that did weapon_sp handling here was out of place -
1084 * this isn't called until after the player has finished there
1085 * actions, and is thus out of place. All we do here is bounds
1086 * checking.
1087 */
1088 if (pl->has_hit)
1089 {
1090 if (pl->ob->speed_left > pl->weapon_sp)
1091 pl->ob->speed_left = pl->weapon_sp;
1092
1093 /* This needs to be here - if the player is running, we need to
1094 * clear this each tick, but new commands are not being received
1095 * so execute_newserver_command() is never called
1096 */
1097 pl->has_hit = 0;
1098
1099 }
1100 else if (pl->ob->speed_left > pl->ob->speed)
1101 pl->ob->speed_left = pl->ob->speed; 205 pl->ob->speed_left = pl->speed_left_save;
1102 }
1103}
1104 206
1105void 207 pl->weapon_sp_left = min (pl->weapon_sp, pl->weapon_sp_left + pl->weapon_sp);
1106process_events (mapstruct * map) 208 pl->ob->speed_left = min (pl->ob->speed, pl->ob->speed_left + pl->ob->speed);
1107{ 209 }
1108 object *op; 210}
1109 object *marker = get_object ();
1110 tag_t tag;
1111 211
212static void
213process_events ()
214{
1112 process_players1 (map); 215 process_players1 ();
1113 216
1114 marker->active_next = active_objects; 217 for_all_actives (op)
1115
1116 if (marker->active_next)
1117 marker->active_next->active_prev = marker;
1118
1119 marker->active_prev = NULL;
1120 active_objects = marker;
1121
1122 while (marker->active_next)
1123 { 218 {
1124 op = marker->active_next; 219 // try to prefetch some stuff we expect to need
1125 tag = op->count; 220 // obviously, it should be grouped into the same cacheline.
221 // preliminary results indicate that this gives halves the speed
222 // used for the inner loop
223 if (_i < actives.size ()) // HACK, rely on _i :/
224 {
225 object *next = actives [_i + 1];
1126 226
1127 /* Move marker forward - swap op and marker */ 227 prefetch (&next->flag , 0, 1);
1128 op->active_prev = marker->active_prev; 228 prefetch (&next->speed , 0, 1);
1129 229 prefetch (&next->anim_speed, 0, 1);
1130 if (op->active_prev) 230 prefetch (&next->contr , 0, 1);
1131 op->active_prev->active_next = op; 231 }
1132 else
1133 active_objects = op;
1134
1135 marker->active_next = op->active_next;
1136
1137 if (marker->active_next)
1138 marker->active_next->active_prev = marker;
1139
1140 marker->active_prev = op;
1141 op->active_next = marker;
1142 232
1143 /* Now process op */ 233 /* Now process op */
1144 if (QUERY_FLAG (op, FLAG_FREED)) 234 if (expect_false (op->flag [FLAG_FREED]))
1145 { 235 {
1146 LOG (llevError, "BUG: process_events(): Free object on list\n"); 236 LOG (llevError, "BUG: process_events(): Free object on list\n");
1147 op->speed = 0; 237 op->set_speed (0);
1148 update_ob_speed (op);
1149 continue; 238 continue;
1150 } 239 }
1151 240
1152 /* I've seen occasional crashes due to this - the object is removed, 241 if (expect_false (!op->has_active_speed ()))
1153 * and thus the map it points to (last map it was on) may be bogus
1154 * The real bug is to try to find out the cause of this - someone
1155 * is probably calling remove_ob without either an insert_ob or
1156 * free_object afterwards, leaving an object dangling. But I'd
1157 * rather log this and continue on instead of crashing.
1158 * Don't remove players - when a player quits, the object is in
1159 * sort of a limbo, of removed, but something we want to keep
1160 * around.
1161 */ 242 {
1162 if (QUERY_FLAG (op, FLAG_REMOVED) && op->type != PLAYER && op->map && op->map->in_memory != MAP_IN_MEMORY)
1163 {
1164 LOG (llevError, "BUG: process_events(): Removed object on list\n"); 243 LOG (llevError, "BUG: process_events(): Object %s has no speed (%f), "
1165 dump_object (op); 244 "but is on active list\n", op->debug_desc (), op->speed);
1166 LOG (llevError, errmsg); 245 op->set_speed (0);
1167 free_object (op);
1168 continue; 246 continue;
1169 } 247 }
1170 248
1171 if (!op->speed) 249 if (expect_false (op->flag [FLAG_REMOVED]))
1172 { 250 {
1173 LOG (llevError, "BUG: process_events(): Object %s has no speed, " "but is on active list\n", &op->arch->name); 251 LOG (llevError, "BUG: process_events(): removed object is on active list: %s\n",
1174 update_ob_speed (op); 252 op->debug_desc ());
253 op->set_speed (0);
1175 continue; 254 continue;
1176 } 255 }
1177 256
1178 if (op->map == NULL && op->env == NULL && op->name && op->type != MAP && map == NULL)
1179 {
1180 LOG (llevError, "BUG: process_events(): Object without map or " "inventory is on active list: %s (%d)\n", &op->name, op->count);
1181 op->speed = 0;
1182 update_ob_speed (op);
1183 continue;
1184 }
1185
1186 if (map != NULL && op->map != map)
1187 continue;
1188
1189 /* Animate the object. Bug of feature that andim_speed 257 /* Animate the object. Bug or feature that anim_speed
1190 * is based on ticks, and not the creatures speed? 258 * is based on ticks, and not the creatures speed?
1191 */ 259 */
1192 if (op->anim_speed && op->last_anim >= op->anim_speed) 260 if (op->anim_speed && op->last_anim >= op->anim_speed)
1193 { 261 {
1194 if ((op->type == PLAYER) || (op->type == MONSTER))
1195 animate_object (op, op->facing);
1196 else
1197 animate_object (op, op->direction); 262 animate_object (op, op->contr ? op->facing : op->direction);
1198
1199 op->last_anim = 1; 263 op->last_anim = 1;
1200 } 264 }
1201 else 265 else
1202 op->last_anim++; 266 ++op->last_anim;
1203 267
1204 if (op->speed_left > 0) 268 if (expect_false (op->speed_left > 0.f))
1205 {
1206#if 0
1207 /* I've seen occasional crashes in move_symptom() with it
1208 * crashing because op is removed - add some debugging to
1209 * track if it is removed at this point.
1210 * This unfortunately is a bit too verbose it seems - not sure
1211 * why - I think what happens is a map is freed or something and
1212 * some objects get 'lost' - removed never to be reclaimed.
1213 * removed objects generally shouldn't exist.
1214 */
1215 if (QUERY_FLAG (op, FLAG_REMOVED))
1216 { 269 {
1217 LOG (llevDebug, "process_events: calling process_object with removed object %s\n", op->name ? op->name : "null");
1218 }
1219#endif
1220 --op->speed_left; 270 --op->speed_left;
1221 process_object (op); 271 process_object (op);
1222 if (was_destroyed (op, tag))
1223 continue;
1224 } 272 }
1225 if (settings.casting_time == TRUE && op->casting_time > 0)
1226 op->casting_time--;
1227 if (op->speed_left <= 0)
1228 op->speed_left += FABS (op->speed);
1229 }
1230 273
1231 /* Remove marker object from active list */ 274 // this will destroy the speed_left value for players, but
1232 if (marker->active_prev != NULL) 275 // process_players1 and ..2 save/restore the real value,
1233 marker->active_prev->active_next = NULL; 276 // so we can avoid a costly test here.
277 op->speed_left = min (op->speed, op->speed_left + op->speed);
278 }
279
280 process_players2 ();
281}
282
283/* clean up everything before exiting */
284void
285emergency_save ()
286{
287 LOG (llevDebug, "emergency save begin.\n");
288
289 cfperl_emergency_save ();
290
291 LOG (llevDebug, "saving book archive.\n");
292 write_book_archive ();
293
294 LOG (llevDebug, "emergency save done.\n");
295}
296
297// send all clients some informational text
298static void
299cleanup_inform (const char *cause, bool make_core)
300{
301 int flags = NDI_REPLY | NDI_UNIQUE | NDI_ALL | (make_core ? NDI_RED : NDI_GREEN);
302
303 new_draw_info_format (flags, 0, 0, "The server will now shutdown.");
304 new_draw_info_format (flags, 0, 0, "Cause for this shutdown: %s", cause);
305
306 if (make_core)
307 new_draw_info_format (flags, 0, 0, "This is considered a crash, but all maps and players have been saved.");
1234 else 308 else
1235 active_objects = NULL; 309 new_draw_info_format (flags, 0, 0, "This is considered to be a clean shutdown, and all maps and players will be saved now.");
1236 310
1237 process_players2 (map); 311 new_draw_info_format (flags, 0, 0, "%s", CLEANUP_MESSAGE);
1238 312
1239 free_object (marker); 313 client::flush_sockets ();
1240}
1241
1242void
1243clean_tmp_files (void)
1244{
1245 mapstruct *m, *next;
1246
1247 LOG (llevInfo, "Cleaning up...\n");
1248
1249 /* We save the maps - it may not be intuitive why, but if there are unique
1250 * items, we need to save the map so they get saved off. Perhaps we should
1251 * just make a special function that only saves the unique items.
1252 */
1253 for (m = first_map; m != NULL; m = next)
1254 {
1255 next = m->next;
1256 if (m->in_memory == MAP_IN_MEMORY)
1257 {
1258 /* If we want to reuse the temp maps, swap it out (note that will also
1259 * update the log file. Otherwise, save the map (mostly for unique item
1260 * stuff). Note that the clean_tmp_map is called after the end of
1261 * the for loop but is in the #else bracket. IF we are recycling the maps,
1262 * we certainly don't want the temp maps removed.
1263 */
1264
1265 /* XXX The above comment is dead wrong */
1266 if (settings.recycle_tmp_maps == TRUE)
1267 swap_map (m);
1268 else
1269 {
1270 new_save_map (m, 0); /* note we save here into a overlay map */
1271 clean_tmp_map (m);
1272 }
1273 }
1274 }
1275 write_todclock (); /* lets just write the clock here */
1276} 314}
1277 315
1278/* clean up everything before exiting */ 316/* clean up everything before exiting */
1279void 317void
1280cleanup (void) 318cleanup (const char *cause, bool make_core)
1281{ 319{
320 if (make_core)
321 fork_abort (cause);
322
323 LOG (llevError, "cleanup cause: %s\n", cause);
324
325 if (!make_core)
326 cleanup_inform (cause, make_core);
327
1282 LOG (llevDebug, "Cleanup called. freeing data.\n"); 328 LOG (llevDebug, "cleanup begin.\n");
1283 clean_tmp_files ();
1284 write_book_archive ();
1285#ifdef MEMORY_DEBUG
1286 free_all_maps ();
1287 free_style_maps ();
1288 free_all_object_data ();
1289 free_all_archs ();
1290 free_all_treasures ();
1291 free_all_images ();
1292 free_all_newserver ();
1293 free_all_recipes ();
1294 free_all_readable ();
1295 free_all_god ();
1296 free_all_anim ();
1297 /* See what the string data that is out there that hasn't been freed. */
1298/* LOG(llevDebug, ss_dump_table(0xff));*/
1299#endif
1300 exit (0);
1301}
1302 329
1303void 330 if (init_done && !in_cleanup)
1304leave (player * pl, int draw_exit)
1305{
1306 if (pl != NULL)
1307 {
1308 /* We do this so that the socket handling routine can do the final
1309 * cleanup. We also leave that loop to actually handle the freeing
1310 * of the data.
1311 */
1312 if (pl->ob->type != DEAD_OBJECT)
1313 {
1314 pl->socket.status = Ns_Dead;
1315
1316 /* If a hidden dm dropped connection do not create
1317 * inconsistencies by showing that they have left the game
1318 */
1319 if (!(QUERY_FLAG (pl->ob, FLAG_WIZ) && pl->ob->contr->hidden)
1320 && draw_exit && (pl->state != ST_GET_NAME && pl->state != ST_GET_PASSWORD && pl->state != ST_CONFIRM_PASSWORD))
1321 {
1322 if (pl->ob->map)
1323 {
1324 INVOKE_PLAYER (LOGOUT, pl);
1325 LOG (llevInfo, "LOGOUT: Player named %s from ip %s\n", &pl->ob->name, pl->socket.host);
1326 }
1327
1328 char buf[MAX_BUF];
1329 sprintf (buf, "%s left the game.", &pl->ob->name);
1330 new_draw_info (NDI_UNIQUE | NDI_ALL | NDI_DK_ORANGE, 5, NULL, buf);
1331 }
1332
1333 if (!QUERY_FLAG (pl->ob, FLAG_REMOVED))
1334 leave_map (pl->ob);
1335
1336 pl->ob->type = DEAD_OBJECT; /* To avoid problems with inventory window */
1337 }
1338 } 331 {
1339} 332 in_cleanup = true;
1340 333 emergency_save ();
1341int
1342forbid_play (void)
1343{
1344#if !defined(_IBMR2) && !defined(___IBMR2) && defined(PERM_FILE)
1345 char buf[MAX_BUF], day[MAX_BUF];
1346 FILE *fp;
1347 time_t clock;
1348 struct tm *tm;
1349 int i, start, stop, forbit = 0, comp;
1350
1351 clock = time (NULL);
1352 tm = (struct tm *) localtime (&clock);
1353
1354 sprintf (buf, "%s/%s", settings.confdir, PERM_FILE);
1355 if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL)
1356 return 0;
1357
1358 while (fgets (buf, MAX_BUF, fp))
1359 { 334 }
1360 if (buf[0] == '#') 335 else
1361 continue; 336 in_cleanup = true;
1362 if (!strncmp (buf, "msg", 3))
1363 {
1364 if (forbit)
1365 while (fgets (buf, MAX_BUF, fp)) /* print message */
1366 fputs (buf, logfile);
1367 break;
1368 337
1369 } 338 LOG (llevDebug, "running cleanup handlers.\n");
1370 else if (sscanf (buf, "%s %d%*c%d\n", day, &start, &stop) != 3) 339 INVOKE_GLOBAL (CLEANUP);
1371 {
1372 LOG (llevDebug, "Warning: Incomplete line in permission file ignored.\n");
1373 continue;
1374 }
1375 340
1376 for (i = 0; i < 7; i++) 341 LOG (llevDebug, "cleanup done.\n");
1377 { 342
1378 if (!strncmp (buf, days[i], 3) && (tm->tm_wday == i) && (tm->tm_hour >= start) && (tm->tm_hour < stop)) 343 log_cleanup ();
1379 forbit = 1; 344
1380 } 345 if (make_core)
346 cleanup_inform (cause, make_core);
347 else
1381 } 348 {
349 new_draw_info_format (NDI_REPLY | NDI_UNIQUE | NDI_ALL | NDI_GREEN, 0, 0, "Maps and players successfully saved, exiting.");
350 new_draw_info_format (NDI_REPLY | NDI_UNIQUE | NDI_ALL | NDI_GREEN, 0, 0, "And again: " CLEANUP_MESSAGE);
351 client::flush_sockets ();
352 }
1382 353
1383 close_and_delete (fp, comp); 354 cfperl_cleanup (make_core);
1384 355 _exit (make_core);
1385 return forbit;
1386#else
1387 return 0;
1388#endif
1389} 356}
1390 357
1391/* 358/*
1392 * do_specials() is a collection of functions to call from time to time. 359 * do_specials() is a collection of functions to call from time to time.
1393 * Modified 2000-1-14 MSW to use the global pticks count to determine how 360 * Modified 2000-1-14 MSW to use the global pticks count to determine how
1394 * often to do things. This will allow us to spred them out more often. 361 * often to do things. This will allow us to spred them out more often.
1395 * I use prime numbers for the factor count - in that way, it is less likely 362 * I use prime numbers for the factor count - in that way, it is less likely
1396 * these actions will fall on the same tick (compared to say using 500/2500/15000 363 * these actions will fall on the same tick (compared to say using 500/2500/15000
1397 * which would mean on that 15,000 tick count a whole bunch of stuff gets 364 * which would mean on that 15,000 tick count a whole bunch of stuff gets
1399 * done on the same tick, but that will happen very infrequently 366 * done on the same tick, but that will happen very infrequently
1400 * 367 *
1401 * I also think this code makes it easier to see how often we really are 368 * I also think this code makes it easier to see how often we really are
1402 * doing the various things. 369 * doing the various things.
1403 */ 370 */
1404 371static void
1405extern unsigned long todtick;
1406
1407void
1408do_specials (void) 372do_specials ()
1409{ 373{
1410
1411#ifdef WATCHDOG
1412 if (!(pticks % 503))
1413 watchdog ();
1414#endif
1415
1416 if (!(pticks % PTICKS_PER_CLOCK))
1417 tick_the_clock ();
1418
1419 if (!(pticks % 7))
1420 shstr::gc (); 374 shstr::gc ();
375 archetype::gc ();
1421 376
1422 if (!(pticks % 79)) 377 if (expect_false (!(pticks % TICKS_PER_HOUR)))
1423 flush_old_maps (); /* Clears the tmp-files of maps which have reset */ 378 maptile::adjust_daylight ();
1424 379
1425 if (!(pticks % 2503)) 380 if (expect_false (!(pticks % 2503)))
1426 fix_weight (); /* Hack to fix weightproblems caused by bugs */ 381 fix_weight (); /* Hack to fix weightproblems caused by bugs */
1427 382
1428 if (!(pticks % 2521))
1429 metaserver_update (); /* 2500 ticks is about 5 minutes */
1430
1431 if (!(pticks % 5003)) 383 if (expect_false (!(pticks % 5003)))
1432 write_book_archive (); 384 write_book_archive ();
1433 385
1434 if (!(pticks % 5009)) 386 if (expect_false (!(pticks % 5009)))
1435 clean_friendly_list (); 387 clean_friendly_list ();
1436 388
1437 if (!(pticks % 5011)) 389 if (expect_false (!(pticks % 5011)))
1438 obsolete_parties (); 390 obsolete_parties ();
1439 391
1440 if (!(pticks % 12503)) 392 if (expect_false (!(pticks % 12503)))
1441 fix_luck (); 393 fix_luck ();
1442} 394}
1443 395
1444void 396void
1445server_tick () 397server_tick ()
1446{ 398{
1447 nroferrors = 0; 399 // first do the user visible stuff
1448
1449 doeric_server ();
1450 INVOKE_GLOBAL (CLOCK); 400 INVOKE_GLOBAL (CLOCK);
1451 process_events (NULL); /* "do" something with objects with speed */ 401 process_events (); // "do" something with objects with speed
1452 flush_sockets (); 402 client::clock (); // draw client maps etc.
1453 check_active_maps (); /* Removes unused maps after a certain timeout */ 403
404 // then do some bookkeeping, should not really be here
1454 do_specials (); /* Routines called from time to time. */ 405 do_specials (); /* Routines called from time to time. */
406 attachable::check_mortals ();
1455 407
1456 ++pticks; 408 ++pticks;
1457}
1458 409
410 if (object::object_count >= RESTART_COUNT)
411 cleanup ("running out of protocol ID values - need full restart");
412}
413
414// normal main
1459int 415int
1460main (int argc, char **argv) 416main (int argc, char **argv)
1461{ 417{
1462 settings.argc = argc; 418 settings.argc = argc;
1463 settings.argv = argv; 419 settings.argv = argv;
1464 420
1465 cfperl_init (); 421 rndm.seed (time (0));
422 g_thread_init (0); // for the slice allocator only
1466 423
1467 init (argc, argv); 424 init (argc, argv);
1468
1469 initPlugins ();
1470 425
1471 for (;;) 426 for (;;)
1472 cfperl_main (); 427 cfperl_main ();
1473
1474 // unreached
1475 emergency_save (0);
1476 cleanup ();
1477
1478 return 0;
1479} 428}
429

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines