/* * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. * * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team * Copyright (©) 2001-2003,2007 Mark Wedel & Crossfire Development Team * Copyright (©) 1992,2007 Frank Tore Johansen * * Crossfire TRT is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * The authors can be reached via e-mail to */ #include #include #include #include #include #include <../random_maps/random_map.h> #include <../random_maps/rproto.h> #include "path.h" static char days[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; void version (object *op) { new_draw_info_format (NDI_UNIQUE, 0, op, "This is Crossfire+ v%s", VERSION); new_draw_info (NDI_UNIQUE, 0, op, "Authors and contributors to this program:"); new_draw_info (NDI_UNIQUE, 0, op, "Marc A. Lehmann "); new_draw_info (NDI_UNIQUE, 0, op, "Robin Redeker "); new_draw_info (NDI_UNIQUE, 0, op, "Pippijn van Steenhoven "); new_draw_info (NDI_UNIQUE, 0, op, ""); new_draw_info (NDI_UNIQUE, 0, op, "Authors and contributors to the original Crossfire:"); new_draw_info (NDI_UNIQUE, 0, op, "Mark Wedel "); new_draw_info (NDI_UNIQUE, 0, op, "Frank Tore Johansen "); new_draw_info (NDI_UNIQUE, 0, op, "Kjetil Torgrim Homme "); new_draw_info (NDI_UNIQUE, 0, op, "Tyler Van Gorder "); new_draw_info (NDI_UNIQUE, 0, op, "Tony Elmroth "); new_draw_info (NDI_UNIQUE, 0, op, "Dougal Scott "); new_draw_info (NDI_UNIQUE, 0, op, "William "); new_draw_info (NDI_UNIQUE, 0, op, "Geoff Bailey "); new_draw_info (NDI_UNIQUE, 0, op, "Kjetil Wiekhorst Jørgensen "); new_draw_info (NDI_UNIQUE, 0, op, "Cameron Blackwood "); new_draw_info (NDI_UNIQUE, 0, op, "Joseph L. Traub "); new_draw_info (NDI_UNIQUE, 0, op, "Rupert G. Goldie "); new_draw_info (NDI_UNIQUE, 0, op, "Eric A. Anderson "); new_draw_info (NDI_UNIQUE, 0, op, "Rickard Eneqvist "); new_draw_info (NDI_UNIQUE, 0, op, "Jarkko Sonninen "); new_draw_info (NDI_UNIQUE, 0, op, "Karl Holland "); new_draw_info (NDI_UNIQUE, 0, op, "Mikael Lundgren "); new_draw_info (NDI_UNIQUE, 0, op, "Mikael Olsson "); new_draw_info (NDI_UNIQUE, 0, op, "Tero Haatanen "); new_draw_info (NDI_UNIQUE, 0, op, "Lasse Ylitalo "); new_draw_info (NDI_UNIQUE, 0, op, "Niilo Neuvo "); new_draw_info (NDI_UNIQUE, 0, op, "Markku J{rvinen "); new_draw_info (NDI_UNIQUE, 0, op, "Sylvain Meunier "); new_draw_info (NDI_UNIQUE, 0, op, "Jason Fosback "); new_draw_info (NDI_UNIQUE, 0, op, "Carl Edman "); new_draw_info (NDI_UNIQUE, 0, op, "Charles Henrich "); new_draw_info (NDI_UNIQUE, 0, op, "Gregor Schmid "); new_draw_info (NDI_UNIQUE, 0, op, "Raphael Quinet "); new_draw_info (NDI_UNIQUE, 0, op, "Jari Vanhala "); new_draw_info (NDI_UNIQUE, 0, op, "Tero Kivinen "); new_draw_info (NDI_UNIQUE, 0, op, "Peter Mardahl "); new_draw_info (NDI_UNIQUE, 0, op, "Matthew Zeher "); new_draw_info (NDI_UNIQUE, 0, op, "Scott R. Turner "); new_draw_info (NDI_UNIQUE, 0, op, "Ben Fennema "); new_draw_info (NDI_UNIQUE, 0, op, "Nick Williams "); new_draw_info (NDI_UNIQUE, 0, op, "Laurent Wacrenier "); new_draw_info (NDI_UNIQUE, 0, op, "Brian Thomas "); new_draw_info (NDI_UNIQUE, 0, op, "John Steven Moerk "); new_draw_info (NDI_UNIQUE, 0, op, "Delbecq David "); new_draw_info (NDI_UNIQUE, 0, op, "Chachkoff Yann \n"); new_draw_info (NDI_UNIQUE, 0, op, ""); new_draw_info (NDI_UNIQUE, 0, op, "Images and art:"); new_draw_info (NDI_UNIQUE, 0, op, "Peter Gardner"); new_draw_info (NDI_UNIQUE, 0, op, "David Gervais "); new_draw_info (NDI_UNIQUE, 0, op, "Mitsuhiro Itakura "); new_draw_info (NDI_UNIQUE, 0, op, "Hansjoerg Malthaner "); new_draw_info (NDI_UNIQUE, 0, op, "Mårten Woxberg "); new_draw_info (NDI_UNIQUE, 0, op, ""); new_draw_info (NDI_UNIQUE, 0, op, "And many more!"); } /* This is a basic little function to put the player back to his * savebed. We do some error checking - its possible that the * savebed map may no longer exist, so we make sure the player * goes someplace. */ void enter_player_savebed (object *op) { object *tmp = object::create (); EXIT_PATH (tmp) = op->contr->savebed_map; EXIT_X (tmp) = op->contr->bed_x; EXIT_Y (tmp) = op->contr->bed_y; op->enter_exit (tmp); tmp->destroy (); } /* * enter_map(): Moves the player and pets from current map (if any) to * new map. map, x, y must be set. map is the map we are moving the * player to - it could be the map he just came from if the load failed for * whatever reason. If default map coordinates are to be used, then * the function that calls this should figure them out. */ void object::enter_map (maptile *newmap, int x, int y) { if (destroyed () || !newmap || newmap->in_memory != MAP_IN_MEMORY) return; if (out_of_map (newmap, x, y)) { LOG (llevError, "enter_map: supplied coordinates are not within the map! (%s: %d, %d)\n", &newmap->path, x, y); x = newmap->enter_x; y = newmap->enter_y; if (out_of_map (newmap, x, y)) { LOG (llevError, "enter_map: map %s provides invalid default enter location (%d, %d) > (%d, %d)\n", &newmap->path, x, y, newmap->width, newmap->height); new_draw_info (NDI_UNIQUE, 0, this, "The exit is closed"); return; } } if (contr && map != newmap && map) if (INVOKE_MAP (LEAVE, map, ARG_PLAYER (contr))) return; // remove, so stupid ob_locked does not trigger a failure remove (); /* try to find a spot for the player */ if (blocked (newmap, x, y)) { /* First choice blocked */ /* We try to find a spot for the player, starting closest in. * We could use find_first_free_spot, but that doesn't randomize it at all, * So for example, if the north space is free, you would always end up there even * if other spaces around are available. * Note that for the second and third calls, we could start at a position other * than one, but then we could end up on the other side of walls and so forth. */ int i = find_free_spot (this, newmap, x, y, 1, SIZEOFFREE1 + 1); if (i == -1) { i = find_free_spot (this, newmap, x, y, 1, SIZEOFFREE2 + 1); if (i == -1) i = find_free_spot (this, newmap, x, y, 1, SIZEOFFREE); } if (i != -1) { x += freearr_x[i]; y += freearr_y[i]; } else /* not much we can do in this case. */ 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); } if (contr && map != newmap) { if (INVOKE_PLAYER (MAP_CHANGE, contr, ARG_MAP (newmap), ARG_INT (x), ARG_INT (y))) return; if (INVOKE_MAP (ENTER, newmap, ARG_PLAYER (contr), ARG_INT (x), ARG_INT (y))) return; } this->x = x; this->y = y; map = newmap; insert_ob_in_map (this, map, 0, INS_NO_WALK_ON); enemy = 0; if (contr) { contr->maplevel = newmap->path; contr->count = 0; } /* Update any golems */ if (type == PLAYER) if (object *golem = contr->golem) { int i = find_free_spot (golem, newmap, x, y, 1, SIZEOFFREE); if (i < 0) golem->destroy (); else { newmap->insert (golem, x + freearr_x[i], y + freearr_y[i]); golem->direction = find_dir_2 (x - golem->x, y - golem->y); } } /* since the players map is already loaded, we don't need to worry * about pending objects. */ remove_all_pets (newmap); } /* process_players1 and process_players2 do all the player related stuff. * I moved it out of process events and process_map. This was to some * extent for debugging as well as to get a better idea of the time used * by the various functions. process_players1() does the processing before * objects have been updated, process_players2() does the processing that * is needed after the players have been updated. */ static void process_players1 () { /* Basically, we keep looping until all the players have done their actions. */ for (int flag = 1; flag != 0;) { flag = 0; for_all_players (pl) { pl->refcnt_chk (); if (expect_false (!pl->ob || !pl->ns || !pl->ob->active)) continue; if (handle_newcs_player (pl->ob)) flag = 1; } } for_all_players (pl) { object *ob = pl->ob; if (expect_false (!ob || !pl->ns || !ob->active)) continue; do_some_living (ob); } } static void process_players2 () { /* Then check if any players should use weapon-speed instead of speed */ for_all_players (pl) { pl->weapon_sp_left = min (pl->weapon_sp, pl->weapon_sp_left + pl->weapon_sp); pl->ob->speed_left = min (pl->ob->speed, pl->ob->speed_left + pl->ob->speed); } } void process_events () { process_players1 (); for_all_actives (op) { /* Now process op */ if (expect_false (QUERY_FLAG (op, FLAG_FREED))) { LOG (llevError, "BUG: process_events(): Free object on list\n"); op->set_speed (0); continue; } if (expect_false (!op->has_active_speed ())) { LOG (llevError, "BUG: process_events(): Object %s has no speed (%f), " "but is on active list\n", op->debug_desc (), op->speed); op->set_speed (0); continue; } if (expect_false (op->flag [FLAG_REMOVED])) { LOG (llevError, "BUG: process_events(): removed object is on active list: %s\n", op->debug_desc ()); op->set_speed (0); continue; } /* Animate the object. Bug or feature that anim_speed * is based on ticks, and not the creatures speed? */ if (op->anim_speed && op->last_anim >= op->anim_speed) { animate_object (op, op->contr ? op->facing : op->direction); op->last_anim = 1; } else ++op->last_anim; if (op->speed_left > 0.f) { --op->speed_left; process_object (op); if (op->destroyed ()) continue; } if (!op->contr) op->speed_left = min (abs (op->speed), op->speed_left + abs (op->speed)); } process_players2 (); } /* clean up everything before exiting */ void emergency_save () { LOG (llevDebug, "emergency save begin.\n"); cfperl_emergency_save (); LOG (llevDebug, "saving book archive.\n"); write_book_archive (); LOG (llevDebug, "emergency save done.\n"); } // send all clients some informational text static void cleanup_inform (const char *cause, bool make_core) { int flags = NDI_REPLY | NDI_UNIQUE | NDI_ALL | (make_core ? NDI_RED : NDI_GREEN); new_draw_info_format (flags, 0, 0, "The server will now shutdown."); new_draw_info_format (flags, 0, 0, "Cause for this shutdown: %s", cause); if (make_core) new_draw_info_format (flags, 0, 0, "This is considered a crash, but all maps and players have been saved."); else new_draw_info_format (flags, 0, 0, "This is considered to be a clean shutdown, and all maps and players will be saved now."); new_draw_info_format (flags, 0, 0, "%s", CLEANUP_MESSAGE); client::flush_sockets (); } /* clean up everything before exiting */ void cleanup (const char *cause, bool make_core) { if (make_core) fork_abort (cause); LOG (llevError, "cleanup cause: %s\n", cause); if (!make_core) cleanup_inform (cause, make_core); LOG (llevDebug, "cleanup begin.\n"); if (init_done && !in_cleanup) { in_cleanup = true; emergency_save (); } else in_cleanup = true; LOG (llevDebug, "running cleanup handlers.\n"); INVOKE_GLOBAL (CLEANUP); LOG (llevDebug, "cleanup done.\n"); if (make_core) { cleanup_inform (cause, make_core); cfperl_cleanup (make_core); _exit (1); } else { new_draw_info_format (NDI_REPLY | NDI_UNIQUE | NDI_ALL | NDI_GREEN, 0, 0, "Maps and players successfully saved, exiting."); new_draw_info_format (NDI_REPLY | NDI_UNIQUE | NDI_ALL | NDI_GREEN, 0, 0, "And again: " CLEANUP_MESSAGE); client::flush_sockets (); cfperl_cleanup (make_core); _exit (0); } } /* * do_specials() is a collection of functions to call from time to time. * Modified 2000-1-14 MSW to use the global pticks count to determine how * often to do things. This will allow us to spred them out more often. * I use prime numbers for the factor count - in that way, it is less likely * these actions will fall on the same tick (compared to say using 500/2500/15000 * which would mean on that 15,000 tick count a whole bunch of stuff gets * done). Of course, there can still be times where multiple specials are * done on the same tick, but that will happen very infrequently * * I also think this code makes it easier to see how often we really are * doing the various things. */ void do_specials (void) { if (expect_false (!(pticks % TICKS_PER_HOUR))) adjust_daylight (); if (expect_false (!(pticks % 89))) shstr::gc (); if (expect_false (!(pticks % 2503))) fix_weight (); /* Hack to fix weightproblems caused by bugs */ if (expect_false (!(pticks % 5003))) write_book_archive (); if (expect_false (!(pticks % 5009))) clean_friendly_list (); if (expect_false (!(pticks % 5011))) obsolete_parties (); if (expect_false (!(pticks % 12503))) fix_luck (); } void server_tick () { // first do the user visible stuff INVOKE_GLOBAL (CLOCK); process_events (); // "do" something with objects with speed client::clock (); // draw client maps etc. // then do some bookkeeping, should not really be here do_specials (); /* Routines called from time to time. */ attachable::check_mortals (); ++pticks; } #if 0 // used fro benchmarking (x86/amd64-specific) typedef unsigned long tval; typedef unsigned long long stamp64; extern inline tval stamp (void) { tval tsc; asm volatile ("rdtsc":"=a" (tsc)::"edx"); return tsc; } extern inline tval measure (tval t) { tval tsc; asm volatile ("rdtsc":"=a" (tsc)::"edx"); if (tsc > t) return tsc - t; else return t - tsc; } int main (int argc, char **argv) { rand_gen rg(0); tval fastest = 0x7fffffff; for (int loop = 10000; loop--; ) { tval s = stamp (); volatile int i = rg.get_int(25); fastest = min (fastest, measure (s)); } //printf ("fastest %d\n", fastest); for (int i = 0; i < 1024*1024*3; ++i) { char c = rg.get_int (256); write (2, &c, 1); } } #else // normal main int main (int argc, char **argv) { settings.argc = argc; settings.argv = argv; init (argc, argv); for (;;) cfperl_main (); } #endif #if 0 int xyzzy () { return rndm(8); } void xyzzy (object_ptr &a, object_ptr &o) { asm volatile ("int3"); a = o; asm volatile ("int3"); } #endif