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.82 by root, Tue Jan 9 01:28:32 2007 UTC vs.
Revision 1.100 by root, Mon Apr 30 04:25:30 2007 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 * CrossFire, A Multiplayer game
3 3 *
4 Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team 4 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
5 Copyright (C) 2001-2003 Mark Wedel & Crossfire Development Team 5 * Copyright (C) 2001-2003 Mark Wedel & Crossfire Development Team
6 Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (C) 1992 Frank Tore Johansen
7 7 *
8 This program is free software; you can redistribute it and/or modify 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 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 10 * the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version. 11 * (at your option) any later version.
12 12 *
13 This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details. 16 * GNU General Public License for more details.
17 17 *
18 You should have received a copy of the GNU General Public License 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 19 * along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 21 *
22 The authors can be reached via e-mail at <crossfire@schmorp.de> 22 * The authors can be reached via e-mail at <crossfire@schmorp.de>
23*/ 23 */
24 24
25#include <global.h> 25#include <global.h>
26#include <object.h> 26#include <object.h>
27#include <tod.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 28
37#include <sproto.h> 29#include <sproto.h>
38#include <time.h> 30#include <time.h>
39 31
40#include <../random_maps/random_map.h> 32#include <../random_maps/random_map.h>
55 47
56 new_draw_info (NDI_UNIQUE, 0, op, "Authors and contributors to this program:"); 48 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>"); 49 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>"); 50 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>"); 51 new_draw_info (NDI_UNIQUE, 0, op, "Pippijn van Steenhoven <pip88nl@gmail.com>");
52 new_draw_info (NDI_UNIQUE, 0, op, "");
53 new_draw_info (NDI_UNIQUE, 0, op, "Authors and contributors to the original Crossfire:");
60 new_draw_info (NDI_UNIQUE, 0, op, "Mark Wedel <mwedel@sonic.net>"); 54 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>"); 55 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>"); 56 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>"); 57 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>"); 58 new_draw_info (NDI_UNIQUE, 0, op, "Tony Elmroth <elmroth@cd.chalmers.se>");
95 new_draw_info (NDI_UNIQUE, 0, op, "Laurent Wacrenier <Wacren@Gin.ObsPM.Fr>"); 89 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>"); 90 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>"); 91 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>"); 92 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"); 93 new_draw_info (NDI_UNIQUE, 0, op, "Chachkoff Yann <yann.chachkoff@mailandnews.com>\n");
100 94 new_draw_info (NDI_UNIQUE, 0, op, "");
101 new_draw_info (NDI_UNIQUE, 0, op, "Images and art:"); 95 new_draw_info (NDI_UNIQUE, 0, op, "Images and art:");
102 new_draw_info (NDI_UNIQUE, 0, op, "Peter Gardner"); 96 new_draw_info (NDI_UNIQUE, 0, op, "Peter Gardner");
103 new_draw_info (NDI_UNIQUE, 0, op, "David Gervais <david_eg@mail.com>"); 97 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>"); 98 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>"); 99 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>"); 100 new_draw_info (NDI_UNIQUE, 0, op, "Mårten Woxberg <maxmc@telia.com>");
101 new_draw_info (NDI_UNIQUE, 0, op, "");
107 new_draw_info (NDI_UNIQUE, 0, op, "And many more!"); 102 new_draw_info (NDI_UNIQUE, 0, op, "And many more!");
108} 103}
109 104
110/* This is a basic little function to put the player back to his 105/* This is a basic little function to put the player back to his
111 * savebed. We do some error checking - its possible that the 106 * savebed. We do some error checking - its possible that the
208 contr->maplevel = newmap->path; 203 contr->maplevel = newmap->path;
209 contr->count = 0; 204 contr->count = 0;
210 } 205 }
211 206
212 /* Update any golems */ 207 /* Update any golems */
213 if (type == PLAYER && contr->ranges[range_golem]) 208 if (type == PLAYER)
214 { 209 if (object *golem = contr->golem)
215 int i = find_free_spot (contr->ranges[range_golem], newmap,
216 x, y, 1, SIZEOFFREE);
217
218 if (i == -1)
219 { 210 {
220 contr->ranges[range_golem]->destroy (); 211 int i = find_free_spot (golem, newmap, x, y, 1, SIZEOFFREE);
221 contr->ranges[range_golem] = 0; 212
222 } 213 if (i < 0)
214 golem->destroy ();
223 else 215 else
224 {
225 for (object *tmp = contr->ranges[range_golem]; tmp; tmp = tmp->more)
226 { 216 {
227 tmp->x = x + freearr_x[i] + (tmp->arch ? 0 : tmp->arch->clone.x); 217 newmap->insert (golem, x + freearr_x[i], y + freearr_y[i]);
228 tmp->y = y + freearr_y[i] + (tmp->arch ? 0 : tmp->arch->clone.y); 218 golem->direction = find_dir_2 (x - golem->x, y - golem->y);
229 tmp->map = newmap;
230 } 219 }
231
232 insert_ob_in_map (contr->ranges[range_golem], newmap, NULL, 0);
233 contr->ranges[range_golem]->direction =
234 find_dir_2 (x - contr->ranges[range_golem]->x, y - contr->ranges[range_golem]->y);
235 } 220 }
236 }
237 221
238 /* since the players map is already loaded, we don't need to worry 222 /* since the players map is already loaded, we don't need to worry
239 * about pending objects. 223 * about pending objects.
240 */ 224 */
241 remove_all_pets (newmap); 225 remove_all_pets (newmap);
249 * is needed after the players have been updated. 233 * is needed after the players have been updated.
250 */ 234 */
251static void 235static void
252process_players1 () 236process_players1 ()
253{ 237{
254 int flag;
255
256 /* Basically, we keep looping until all the players have done their actions. */ 238 /* Basically, we keep looping until all the players have done their actions. */
257 for (flag = 1; flag != 0;) 239 for (int flag = 1; flag != 0;)
258 { 240 {
259 flag = 0; 241 flag = 0;
260 for_all_players (pl) 242 for_all_players (pl)
261 { 243 {
262 pl->refcnt_chk (); 244 pl->refcnt_chk ();
263 245
264 if (!pl->ob || !pl->ns || !pl->ob->active) 246 if (!pl->ob || !pl->ns || !pl->ob->active)
265 continue; 247 continue;
266 248
267 if (pl->ob->speed_left > 0) 249 if (pl->ob->speed_left > 0 && pl->ns)
268 if (handle_newcs_player (pl->ob)) 250 if (handle_newcs_player (pl->ob))
269 flag = 1; 251 flag = 1;
270 252
271 /* If the player is not actively playing, don't make a 253 /* If the player is not actively playing, don't make a
272 * backup save - nothing to save anyway. Plus, the 254 * backup save - nothing to save anyway. Plus, the
341 LOG (llevError, "BUG: process_events(): Free object on list\n"); 323 LOG (llevError, "BUG: process_events(): Free object on list\n");
342 op->set_speed (0); 324 op->set_speed (0);
343 continue; 325 continue;
344 } 326 }
345 327
346 /* I've seen occasional crashes due to this - the object is removed,
347 * and thus the map it points to (last map it was on) may be bogus
348 * The real bug is to try to find out the cause of this - someone
349 * is probably calling remove_ob without either an insert_ob or
350 * free_object afterwards, leaving an object dangling. But I'd
351 * rather log this and continue on instead of crashing.
352 * Don't remove players - when a player quits, the object is in
353 * sort of a limbo, of removed, but something we want to keep
354 * around.
355 */
356 if (QUERY_FLAG (op, FLAG_REMOVED) && op->type != PLAYER && op->map && op->map->in_memory != MAP_IN_MEMORY)
357 {
358 LOG (llevError, "BUG: process_events(): Removed object on list\n");
359 char *dump = dump_object (op);
360 LOG (llevError, dump);
361 free (dump);
362 op->destroy ();
363 continue;
364 }
365
366 if (!op->has_active_speed ()) 328 if (!op->has_active_speed ())
367 { 329 {
368 LOG (llevError, "BUG: process_events(): Object %s has no speed (%f), " 330 LOG (llevError, "BUG: process_events(): Object %s has no speed (%f), "
369 "but is on active list\n", op->debug_desc (), op->speed); 331 "but is on active list\n", op->debug_desc (), op->speed);
370 op->set_speed (0); 332 op->set_speed (0);
371 continue; 333 continue;
372 } 334 }
373 335
374 if (op->map == NULL && op->env == NULL && op->name && op->type != MAP) 336 if (op->flag [FLAG_REMOVED])
375 { 337 {
376 LOG (llevError, "BUG: process_events(): Object without map or " 338 LOG (llevError, "BUG: process_events(): removed object is on active list: %s\n",
377 "inventory is on active list: %s (%d)\n", &op->name, op->count); 339 op->debug_desc ());
378 op->set_speed (0); 340 op->set_speed (0);
379 continue; 341 continue;
380 } 342 }
381 343
382 /* Animate the object. Bug or feature that anim_speed 344 /* Animate the object. Bug or feature that anim_speed
383 * is based on ticks, and not the creatures speed? 345 * is based on ticks, and not the creatures speed?
384 */ 346 */
385 if (op->anim_speed && op->last_anim >= op->anim_speed) 347 if (op->anim_speed && op->last_anim >= op->anim_speed)
386 { 348 {
387 if ((op->type == PLAYER)) 349 animate_object (op, op->type == PLAYER ? op->facing : op->direction);
388 animate_object (op, op->facing);
389 else
390 animate_object (op, op->direction);
391 350
392 op->last_anim = 1; 351 op->last_anim = 1;
393 } 352 }
394 else 353 else
395 op->last_anim++; 354 op->last_anim++;
431void 390void
432emergency_save () 391emergency_save ()
433{ 392{
434 LOG (llevDebug, "emergency save begin.\n"); 393 LOG (llevDebug, "emergency save begin.\n");
435 394
436 maptile::emergency_save (); 395 cfperl_emergency_save ();
437 396
438 LOG (llevDebug, "saving book archive.\n"); 397 LOG (llevDebug, "saving book archive.\n");
439 write_book_archive (); 398 write_book_archive ();
440 399
441 LOG (llevDebug, "emergency save done.\n"); 400 LOG (llevDebug, "emergency save done.\n");
443 402
444// send all clients some informational text 403// send all clients some informational text
445static void 404static void
446cleanup_inform (const char *cause, bool make_core) 405cleanup_inform (const char *cause, bool make_core)
447{ 406{
407 int flags = NDI_UNIQUE | NDI_ALL | (make_core ? NDI_RED : NDI_GREEN);
408
448 new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_RED, 0, 0, "The server will now shutdown.\n"); 409 new_draw_info_format (flags, 0, 0, "The server will now shutdown.\n");
449 new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_RED, 0, 0, "Cause for this shtudown: %s\n", cause); 410 new_draw_info_format (flags, 0, 0, "Cause for this shutdown: %s\n", cause);
450 411
451 if (make_core) 412 if (make_core)
452 new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_RED, 0, 0, "This is considered a crash.\n"); 413 new_draw_info_format (flags, 0, 0, "This is considered a crash, but all maps and players have been saved.\n");
453 else 414 else
454 new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_RED, 0, 0, "This is considered to be a clean shutdown.\n"); 415 new_draw_info_format (flags, 0, 0, "This is considered to be a clean shutdown, and all maps and players will be saved now.\n");
455 416
456 new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_RED, 0, 0, "%s\n", CLEANUP_MESSAGE); 417 new_draw_info_format (flags, 0, 0, "%s\n", CLEANUP_MESSAGE);
457 418
458 flush_sockets (); 419 flush_sockets ();
459} 420}
460 421
461/* clean up everything before exiting */ 422/* clean up everything before exiting */
462void 423void
463cleanup (const char *cause, bool make_core) 424cleanup (const char *cause, bool make_core)
464{ 425{
426 if (make_core)
427 fork_abort (cause);
428
465 LOG (llevError, "cleanup cause: %s\n", cause); 429 LOG (llevError, "cleanup cause: %s\n", cause);
466 430
467 if (!make_core) 431 if (!make_core)
468 cleanup_inform (cause, make_core); 432 cleanup_inform (cause, make_core);
469 433
483 LOG (llevDebug, "cleanup done.\n"); 447 LOG (llevDebug, "cleanup done.\n");
484 448
485 if (make_core) 449 if (make_core)
486 { 450 {
487 cleanup_inform (cause, make_core); 451 cleanup_inform (cause, make_core);
488 abort (); 452 cfperl_cleanup (make_core);
453 _exit (1);
489 } 454 }
490 else 455 else
456 {
457 new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_GREEN, 0, 0, "Maps and players successfully saved, exiting.\n");
458 new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_GREEN, 0, 0, "And again: " CLEANUP_MESSAGE "\n");
459 flush_sockets ();
460 cfperl_cleanup (make_core);
491 _exit (0); 461 _exit (0);
462 }
492} 463}
493 464
494int 465int
495forbid_play (void) 466forbid_play (void)
496{ 467{
552 * done on the same tick, but that will happen very infrequently 523 * done on the same tick, but that will happen very infrequently
553 * 524 *
554 * I also think this code makes it easier to see how often we really are 525 * I also think this code makes it easier to see how often we really are
555 * doing the various things. 526 * doing the various things.
556 */ 527 */
557
558extern unsigned long todtick;
559
560void 528void
561do_specials (void) 529do_specials (void)
562{ 530{
563 if (!(pticks % PTICKS_PER_CLOCK)) 531 if (!(pticks % TICKS_PER_HOUR))
564 tick_the_clock (); 532 adjust_daylight ();
565 533
566 if (!(pticks % 7)) 534 if (!(pticks % 7))
567 shstr::gc (); 535 shstr::gc ();
568 536
569 if (!(pticks % 2503)) 537 if (!(pticks % 2503))
596 attachable::check_mortals (); 564 attachable::check_mortals ();
597 565
598 ++pticks; 566 ++pticks;
599} 567}
600 568
569#if 0
570// used fro benchmarking (x86/amd64-specific)
571typedef unsigned long tval;
572typedef unsigned long long stamp64;
573
574extern inline tval
575stamp (void)
576{
577 tval tsc;
578 asm volatile ("rdtsc":"=a" (tsc)::"edx");
579
580 return tsc;
581}
582
583extern inline tval
584measure (tval t)
585{
586 tval tsc;
587 asm volatile ("rdtsc":"=a" (tsc)::"edx");
588
589 if (tsc > t)
590 return tsc - t;
591 else
592 return t - tsc;
593}
594
601int 595int
602main (int argc, char **argv) 596main (int argc, char **argv)
603{ 597{
598 rand_gen rg(0);
599 tval fastest = 0x7fffffff;
600 for (int loop = 10000; loop--; )
601 {
602 tval s = stamp ();
603 volatile int i = rg.get_int(25);
604 fastest = min (fastest, measure (s));
605 }
606
607 //printf ("fastest %d\n", fastest);
608 for (int i = 0; i < 1024*1024*3; ++i)
609 {
610 char c = rg.get_int (256);
611 write (2, &c, 1);
612 }
613}
614
615#else
616
617// normal main
618int
619main (int argc, char **argv)
620{
604 settings.argc = argc; 621 settings.argc = argc;
605 settings.argv = argv; 622 settings.argv = argv;
606 623
607 init (argc, argv); 624 init (argc, argv);
608 625
609 initPlugins (); 626 initPlugins ();
610 627
611 for (;;) 628 for (;;)
612 cfperl_main (); 629 cfperl_main ();
613} 630}
631#endif
614 632

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines