ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/main.C
Revision: 1.81
Committed: Sun Jan 7 02:39:14 2007 UTC (17 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.80: +3 -68 lines
Log Message:
""

File Contents

# Content
1 /*
2 CrossFire, A Multiplayer game for X-windows
3
4 Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
5 Copyright (C) 2001-2003 Mark Wedel & Crossfire Development Team
6 Copyright (C) 1992 Frank Tore Johansen
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your 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 GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 The authors can be reached via e-mail at <crossfire@schmorp.de>
23 */
24
25 #include <global.h>
26 #include <object.h>
27 #include <tod.h>
28
29 #ifdef HAVE_DES_H
30 # include <des.h>
31 #else
32 # ifdef HAVE_CRYPT_H
33 # include <crypt.h>
34 # endif
35 #endif
36
37 #include <sproto.h>
38 #include <time.h>
39
40 #include <../random_maps/random_map.h>
41 #include <../random_maps/rproto.h>
42 #include "path.h"
43
44 static char days[7][4] = {
45 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
46 };
47
48 void
49 version (object *op)
50 {
51 if (op)
52 clear_win_info (op);
53
54 new_draw_info_format (NDI_UNIQUE, 0, op, "This is Crossfire+ v%s", VERSION);
55
56 new_draw_info (NDI_UNIQUE, 0, op, "Authors and contributors to this program:");
57 new_draw_info (NDI_UNIQUE, 0, op, "Marc A. Lehmann <pcg@goof.com>");
58 new_draw_info (NDI_UNIQUE, 0, op, "Robin Redeker <elmex@x-paste.de>");
59 new_draw_info (NDI_UNIQUE, 0, op, "Pippijn van Steenhoven <pip88nl@gmail.com>");
60 new_draw_info (NDI_UNIQUE, 0, op, "Mark Wedel <mwedel@sonic.net>");
61 new_draw_info (NDI_UNIQUE, 0, op, "Frank Tore Johansen <frankj@ifi.uio.no>");
62 new_draw_info (NDI_UNIQUE, 0, op, "Kjetil Torgrim Homme <kjetilho@ifi.uio.no>");
63 new_draw_info (NDI_UNIQUE, 0, op, "Tyler Van Gorder <tvangod@ecst.csuchico.edu>");
64 new_draw_info (NDI_UNIQUE, 0, op, "Tony Elmroth <elmroth@cd.chalmers.se>");
65 new_draw_info (NDI_UNIQUE, 0, op, "Dougal Scott <dougal.scott@fcit.monasu.edu.au>");
66 new_draw_info (NDI_UNIQUE, 0, op, "William <wchuang@athena.mit.edu>");
67 new_draw_info (NDI_UNIQUE, 0, op, "Geoff Bailey <ftww@cs.su.oz.au>");
68 new_draw_info (NDI_UNIQUE, 0, op, "Kjetil Wiekhorst Jørgensen <jorgens@flipper.pvv.unit.no>");
69 new_draw_info (NDI_UNIQUE, 0, op, "Cameron Blackwood <c.blackwood@rdt.monash.edu.au>");
70 new_draw_info (NDI_UNIQUE, 0, op, "Joseph L. Traub <jtraub+@cmu.edu>");
71 new_draw_info (NDI_UNIQUE, 0, op, "Rupert G. Goldie <rgg@aaii.oz.au>");
72 new_draw_info (NDI_UNIQUE, 0, op, "Eric A. Anderson <eanders+@cmu.edu>");
73 new_draw_info (NDI_UNIQUE, 0, op, "Rickard Eneqvist <eneq@Prag.DoCS.UU.SE>");
74 new_draw_info (NDI_UNIQUE, 0, op, "Jarkko Sonninen <Jarkko.Sonninen@lut.fi>");
75 new_draw_info (NDI_UNIQUE, 0, op, "Karl Holland <kholland@sunlab.cit.cornell.du>");
76 new_draw_info (NDI_UNIQUE, 0, op, "Mikael Lundgren <vick@bern.docs.uu.se>");
77 new_draw_info (NDI_UNIQUE, 0, op, "Mikael Olsson <mol@meryl.csd.uu.se>");
78 new_draw_info (NDI_UNIQUE, 0, op, "Tero Haatanen <Tero.Haatanen@lut.fi>");
79 new_draw_info (NDI_UNIQUE, 0, op, "Lasse Ylitalo <ylitalo@student.docs.uu.se>");
80 new_draw_info (NDI_UNIQUE, 0, op, "Niilo Neuvo <anipa@guru.magic.fi>");
81 new_draw_info (NDI_UNIQUE, 0, op, "Markku J{rvinen <mta@modeemi.cs.tut.fi>");
82 new_draw_info (NDI_UNIQUE, 0, op, "Sylvain Meunier <meunier@inf.enst.fr>");
83 new_draw_info (NDI_UNIQUE, 0, op, "Jason Fosback <jfosback@darmok.uoregon.edu>");
84 new_draw_info (NDI_UNIQUE, 0, op, "Carl Edman <cedman@capitalist.princeton.edu>");
85 new_draw_info (NDI_UNIQUE, 0, op, "Charles Henrich <henrich@crh.cl.msu.edu>");
86 new_draw_info (NDI_UNIQUE, 0, op, "Gregor Schmid <schmid@fb3-s7.math.tu-berlin.de>");
87 new_draw_info (NDI_UNIQUE, 0, op, "Raphael Quinet <quinet@montefiore.ulg.ac.be>");
88 new_draw_info (NDI_UNIQUE, 0, op, "Jari Vanhala <jam@modeemi.cs.tut.fi>");
89 new_draw_info (NDI_UNIQUE, 0, op, "Tero Kivinen <kivinen@joker.cs.hut.fi>");
90 new_draw_info (NDI_UNIQUE, 0, op, "Peter Mardahl <peterm@soda.berkeley.edu>");
91 new_draw_info (NDI_UNIQUE, 0, op, "Matthew Zeher <matt@cs.odu.edu>");
92 new_draw_info (NDI_UNIQUE, 0, op, "Scott R. Turner <srt@sun-dimas.aero.org>");
93 new_draw_info (NDI_UNIQUE, 0, op, "Ben Fennema <huma@netcom.com>");
94 new_draw_info (NDI_UNIQUE, 0, op, "Nick Williams <njw@cs.city.ac.uk>");
95 new_draw_info (NDI_UNIQUE, 0, op, "Laurent Wacrenier <Wacren@Gin.ObsPM.Fr>");
96 new_draw_info (NDI_UNIQUE, 0, op, "Brian Thomas <thomas@astro.psu.edu>");
97 new_draw_info (NDI_UNIQUE, 0, op, "John Steven Moerk <jsm@axon.ksc.nasa.gov>");
98 new_draw_info (NDI_UNIQUE, 0, op, "Delbecq David <david.delbecq@mailandnews.com>");
99 new_draw_info (NDI_UNIQUE, 0, op, "Chachkoff Yann <yann.chachkoff@mailandnews.com>\n");
100
101 new_draw_info (NDI_UNIQUE, 0, op, "Images and art:");
102 new_draw_info (NDI_UNIQUE, 0, op, "Peter Gardner");
103 new_draw_info (NDI_UNIQUE, 0, op, "David Gervais <david_eg@mail.com>");
104 new_draw_info (NDI_UNIQUE, 0, op, "Mitsuhiro Itakura <ita@gold.koma.jaeri.go.jp>");
105 new_draw_info (NDI_UNIQUE, 0, op, "Hansjoerg Malthaner <hansjoerg.malthaner@danet.de>");
106 new_draw_info (NDI_UNIQUE, 0, op, "Mårten Woxberg <maxmc@telia.com>");
107 new_draw_info (NDI_UNIQUE, 0, op, "And many more!");
108 }
109
110 /* This is a basic little function to put the player back to his
111 * savebed. We do some error checking - its possible that the
112 * savebed map may no longer exist, so we make sure the player
113 * goes someplace.
114 */
115 void
116 enter_player_savebed (object *op)
117 {
118 object *tmp = object::create ();
119 EXIT_PATH (tmp) = op->contr->savebed_map;
120 EXIT_X (tmp) = op->contr->bed_x;
121 EXIT_Y (tmp) = op->contr->bed_y;
122 op->enter_exit (tmp);
123 tmp->destroy ();
124 }
125
126 /*
127 * enter_map(): Moves the player and pets from current map (if any) to
128 * new map. map, x, y must be set. map is the map we are moving the
129 * player to - it could be the map he just came from if the load failed for
130 * whatever reason. If default map coordinates are to be used, then
131 * the function that calls this should figure them out.
132 */
133 void
134 object::enter_map (maptile *newmap, int x, int y)
135 {
136 if (destroyed () || !newmap || newmap->in_memory != MAP_IN_MEMORY)
137 return;
138
139 if (out_of_map (newmap, x, y))
140 {
141 LOG (llevError, "enter_map: supplied coordinates are not within the map! (%s: %d, %d)\n", &newmap->path, x, y);
142 x = newmap->enter_x;
143 y = newmap->enter_y;
144 if (out_of_map (newmap, x, y))
145 {
146 LOG (llevError, "enter_map: map %s provides invalid default enter location (%d, %d) > (%d, %d)\n",
147 &newmap->path, x, y, newmap->width, newmap->height);
148 new_draw_info (NDI_UNIQUE, 0, this, "The exit is closed");
149 return;
150 }
151 }
152
153 if (contr && map != newmap && map)
154 if (INVOKE_MAP (LEAVE, map, ARG_PLAYER (contr)))
155 return;
156
157 /* If it is a player login, he has yet to be inserted anyplace.
158 * otherwise, we need to deal with removing the player here.
159 */
160 remove ();
161
162 /* try to find a spot for the player */
163 if (ob_blocked (this, newmap, x, y))
164 { /* First choice blocked */
165 /* We try to find a spot for the player, starting closest in.
166 * We could use find_first_free_spot, but that doesn't randomize it at all,
167 * So for example, if the north space is free, you would always end up there even
168 * if other spaces around are available.
169 * Note that for the second and third calls, we could start at a position other
170 * than one, but then we could end up on the other side of walls and so forth.
171 */
172 int i = find_free_spot (this, newmap, x, y, 1, SIZEOFFREE1 + 1);
173
174 if (i == -1)
175 {
176 i = find_free_spot (this, newmap, x, y, 1, SIZEOFFREE2 + 1);
177 if (i == -1)
178 i = find_free_spot (this, newmap, x, y, 1, SIZEOFFREE);
179 }
180
181 if (i != -1)
182 {
183 x += freearr_x[i];
184 y += freearr_y[i];
185 }
186 else
187 /* not much we can do in this case. */
188 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);
189 }
190
191 if (contr && map != newmap)
192 {
193 if (INVOKE_PLAYER (MAP_CHANGE, contr, ARG_MAP (newmap), ARG_INT (x), ARG_INT (y)))
194 return;
195
196 if (INVOKE_MAP (ENTER, newmap, ARG_PLAYER (contr), ARG_INT (x), ARG_INT (y)))
197 return;
198 }
199
200 this->x = x;
201 this->y = y;
202 map = newmap;
203
204 insert_ob_in_map (this, map, 0, INS_NO_WALK_ON);
205
206 enemy = NULL;
207
208 if (contr)
209 {
210 contr->maplevel = newmap->path;
211 contr->count = 0;
212 }
213
214 /* Update any golems */
215 if (type == PLAYER && contr->ranges[range_golem])
216 {
217 int i = find_free_spot (contr->ranges[range_golem], newmap,
218 x, y, 1, SIZEOFFREE);
219
220 contr->ranges[range_golem]->remove ();
221
222 if (i == -1)
223 {
224 remove_friendly_object (contr->ranges[range_golem]);
225 contr->ranges[range_golem]->destroy ();
226 contr->ranges[range_golem] = 0;
227 }
228 else
229 {
230 for (object *tmp = contr->ranges[range_golem]; tmp != NULL; tmp = tmp->more)
231 {
232 tmp->x = x + freearr_x[i] + (tmp->arch ? 0 : tmp->arch->clone.x);
233 tmp->y = y + freearr_y[i] + (tmp->arch ? 0 : tmp->arch->clone.y);
234 tmp->map = newmap;
235 }
236
237 insert_ob_in_map (contr->ranges[range_golem], newmap, NULL, 0);
238 contr->ranges[range_golem]->direction =
239 find_dir_2 (x - contr->ranges[range_golem]->x, y - contr->ranges[range_golem]->y);
240 }
241 }
242
243 /* since the players map is already loaded, we don't need to worry
244 * about pending objects.
245 */
246 remove_all_pets (newmap);
247 }
248
249 /* process_players1 and process_players2 do all the player related stuff.
250 * I moved it out of process events and process_map. This was to some
251 * extent for debugging as well as to get a better idea of the time used
252 * by the various functions. process_players1() does the processing before
253 * objects have been updated, process_players2() does the processing that
254 * is needed after the players have been updated.
255 */
256 static void
257 process_players1 ()
258 {
259 int flag;
260
261 /* Basically, we keep looping until all the players have done their actions. */
262 for (flag = 1; flag != 0;)
263 {
264 flag = 0;
265 for_all_players (pl)
266 {
267 pl->refcnt_chk ();
268
269 if (!pl->ob || !pl->ns || !pl->ob->active)
270 continue;
271
272 if (pl->ob->speed_left > 0)
273 if (handle_newcs_player (pl->ob))
274 flag = 1;
275
276 /* If the player is not actively playing, don't make a
277 * backup save - nothing to save anyway. Plus, the
278 * map may not longer be valid. This can happen when the
279 * player quits - they exist for purposes of tracking on the map,
280 * but don't actually reside on any actual map.
281 */
282 if (QUERY_FLAG (pl->ob, FLAG_REMOVED))
283 continue;
284 } /* end of for loop for all the players */
285 } /* for flag */
286
287 for_all_players (pl)
288 {
289 if (!pl->ob || !pl->ns || !pl->ob->active)
290 continue;
291
292 if (settings.casting_time)
293 {
294 if (pl->ob->casting_time > 0)
295 {
296 pl->ob->casting_time--;
297 pl->ob->start_holding = 1;
298 }
299
300 /* set spell_state so we can update the range in stats field */
301 if ((pl->ob->casting_time == 0) && (pl->ob->start_holding == 1))
302 pl->ob->start_holding = 0;
303 }
304
305 do_some_living (pl->ob);
306 }
307 }
308
309 static void
310 process_players2 ()
311 {
312 /* Then check if any players should use weapon-speed instead of speed */
313 for_all_players (pl)
314 {
315 /* The code that did weapon_sp handling here was out of place -
316 * this isn't called until after the player has finished there
317 * actions, and is thus out of place. All we do here is bounds
318 * checking.
319 */
320 if (pl->has_hit)
321 {
322 if (pl->ob->speed_left > pl->weapon_sp)
323 pl->ob->speed_left = pl->weapon_sp;
324
325 /* This needs to be here - if the player is running, we need to
326 * clear this each tick, but new commands are not being received
327 * so execute_newserver_command() is never called
328 */
329 pl->has_hit = 0;
330 }
331 else if (pl->ob->speed_left > pl->ob->speed)
332 pl->ob->speed_left = pl->ob->speed;
333 }
334 }
335
336 void
337 process_events ()
338 {
339 process_players1 ();
340
341 for_all_actives (op)
342 {
343 /* Now process op */
344 if (QUERY_FLAG (op, FLAG_FREED))
345 {
346 LOG (llevError, "BUG: process_events(): Free object on list\n");
347 op->set_speed (0);
348 continue;
349 }
350
351 /* I've seen occasional crashes due to this - the object is removed,
352 * and thus the map it points to (last map it was on) may be bogus
353 * The real bug is to try to find out the cause of this - someone
354 * is probably calling remove_ob without either an insert_ob or
355 * free_object afterwards, leaving an object dangling. But I'd
356 * rather log this and continue on instead of crashing.
357 * Don't remove players - when a player quits, the object is in
358 * sort of a limbo, of removed, but something we want to keep
359 * around.
360 */
361 if (QUERY_FLAG (op, FLAG_REMOVED) && op->type != PLAYER && op->map && op->map->in_memory != MAP_IN_MEMORY)
362 {
363 LOG (llevError, "BUG: process_events(): Removed object on list\n");
364 char *dump = dump_object (op);
365 LOG (llevError, dump);
366 free (dump);
367 op->destroy ();
368 continue;
369 }
370
371 if (!op->has_active_speed ())
372 {
373 LOG (llevError, "BUG: process_events(): Object %s has no speed (%f), "
374 "but is on active list\n", op->debug_desc (), op->speed);
375 op->set_speed (0);
376 continue;
377 }
378
379 if (op->map == NULL && op->env == NULL && op->name && op->type != MAP)
380 {
381 LOG (llevError, "BUG: process_events(): Object without map or "
382 "inventory is on active list: %s (%d)\n", &op->name, op->count);
383 op->set_speed (0);
384 continue;
385 }
386
387 /* Animate the object. Bug or feature that anim_speed
388 * is based on ticks, and not the creatures speed?
389 */
390 if (op->anim_speed && op->last_anim >= op->anim_speed)
391 {
392 if ((op->type == PLAYER))
393 animate_object (op, op->facing);
394 else
395 animate_object (op, op->direction);
396
397 op->last_anim = 1;
398 }
399 else
400 op->last_anim++;
401
402 if (op->speed_left > 0)
403 {
404 #if 0
405 /* I've seen occasional crashes in move_symptom() with it
406 * crashing because op is removed - add some debugging to
407 * track if it is removed at this point.
408 * This unfortunately is a bit too verbose it seems - not sure
409 * why - I think what happens is a map is freed or something and
410 * some objects get 'lost' - removed never to be reclaimed.
411 * removed objects generally shouldn't exist.
412 */
413 if (QUERY_FLAG (op, FLAG_REMOVED))
414 {
415 LOG (llevDebug, "process_events: calling process_object with removed object %s\n", op->name ? op->name : "null");
416 }
417 #endif
418 --op->speed_left;
419 process_object (op);
420
421 if (op->destroyed ())
422 continue;
423 }
424
425 if (settings.casting_time == TRUE && op->casting_time > 0)
426 op->casting_time--;
427
428 if (op->speed_left <= 0)
429 op->speed_left += FABS (op->speed);
430 }
431
432 process_players2 ();
433 }
434
435 /* clean up everything before exiting */
436 void
437 emergency_save ()
438 {
439 LOG (llevDebug, "emergency save begin.\n");
440
441 maptile::emergency_save ();
442
443 LOG (llevDebug, "saving book archive.\n");
444 write_book_archive ();
445
446 LOG (llevDebug, "emergency save done.\n");
447 }
448
449 // send all clients some informational text
450 static void
451 cleanup_inform (const char *cause, bool make_core)
452 {
453 new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_RED, 0, 0, "The server will now shutdown.\n");
454 new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_RED, 0, 0, "Cause for this shtudown: %s\n", cause);
455
456 if (make_core)
457 new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_RED, 0, 0, "This is considered a crash.\n");
458 else
459 new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_RED, 0, 0, "This is considered to be a clean shutdown.\n");
460
461 new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_RED, 0, 0, "%s\n", CLEANUP_MESSAGE);
462
463 flush_sockets ();
464 }
465
466 /* clean up everything before exiting */
467 void
468 cleanup (const char *cause, bool make_core)
469 {
470 LOG (llevError, "cleanup cause: %s\n", cause);
471
472 if (!make_core)
473 cleanup_inform (cause, make_core);
474
475 LOG (llevDebug, "cleanup begin.\n");
476
477 if (init_done && !in_cleanup)
478 {
479 in_cleanup = true;
480 emergency_save ();
481 }
482 else
483 in_cleanup = true;
484
485 LOG (llevDebug, "running cleanup handlers.\n");
486 INVOKE_GLOBAL (CLEANUP);
487
488 LOG (llevDebug, "cleanup done.\n");
489
490 if (make_core)
491 {
492 cleanup_inform (cause, make_core);
493 abort ();
494 }
495 else
496 _exit (0);
497 }
498
499 int
500 forbid_play (void)
501 {
502 #if !defined(_IBMR2) && !defined(___IBMR2) && defined(PERM_FILE)
503 char buf[MAX_BUF], day[MAX_BUF];
504 FILE *fp;
505 time_t clock;
506 struct tm *tm;
507 int i, start, stop, forbit = 0, comp;
508
509 clock = time (NULL);
510 tm = (struct tm *) localtime (&clock);
511
512 sprintf (buf, "%s/%s", settings.confdir, PERM_FILE);
513 if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL)
514 return 0;
515
516 while (fgets (buf, MAX_BUF, fp))
517 {
518 if (buf[0] == '#')
519 continue;
520
521 if (!strncmp (buf, "msg", 3))
522 {
523 if (forbit)
524 while (fgets (buf, MAX_BUF, fp)) /* print message */
525 fputs (buf, logfile);
526 break;
527 }
528 else if (sscanf (buf, "%s %d%*c%d\n", day, &start, &stop) != 3)
529 {
530 LOG (llevDebug, "Warning: Incomplete line in permission file ignored.\n");
531 continue;
532 }
533
534 for (i = 0; i < 7; i++)
535 {
536 if (!strncmp (buf, days[i], 3) && (tm->tm_wday == i) && (tm->tm_hour >= start) && (tm->tm_hour < stop))
537 forbit = 1;
538 }
539 }
540
541 close_and_delete (fp, comp);
542
543 return forbit;
544 #else
545 return 0;
546 #endif
547 }
548
549 /*
550 * do_specials() is a collection of functions to call from time to time.
551 * Modified 2000-1-14 MSW to use the global pticks count to determine how
552 * often to do things. This will allow us to spred them out more often.
553 * I use prime numbers for the factor count - in that way, it is less likely
554 * these actions will fall on the same tick (compared to say using 500/2500/15000
555 * which would mean on that 15,000 tick count a whole bunch of stuff gets
556 * done). Of course, there can still be times where multiple specials are
557 * done on the same tick, but that will happen very infrequently
558 *
559 * I also think this code makes it easier to see how often we really are
560 * doing the various things.
561 */
562
563 extern unsigned long todtick;
564
565 void
566 do_specials (void)
567 {
568 if (!(pticks % PTICKS_PER_CLOCK))
569 tick_the_clock ();
570
571 if (!(pticks % 7))
572 shstr::gc ();
573
574 if (!(pticks % 2503))
575 fix_weight (); /* Hack to fix weightproblems caused by bugs */
576
577 if (!(pticks % 5003))
578 write_book_archive ();
579
580 if (!(pticks % 5009))
581 clean_friendly_list ();
582
583 if (!(pticks % 5011))
584 obsolete_parties ();
585
586 if (!(pticks % 12503))
587 fix_luck ();
588 }
589
590 void
591 server_tick ()
592 {
593 // first do the user visible stuff
594 doeric_server ();
595 INVOKE_GLOBAL (CLOCK);
596 process_events (); /* "do" something with objects with speed */
597 flush_sockets ();
598
599 // then do some bookkeeping, should not really be here
600 do_specials (); /* Routines called from time to time. */
601 attachable::check_mortals ();
602
603 ++pticks;
604 }
605
606 int
607 main (int argc, char **argv)
608 {
609 settings.argc = argc;
610 settings.argv = argv;
611
612 init (argc, argv);
613
614 initPlugins ();
615
616 for (;;)
617 cfperl_main ();
618 }
619