/* * CrossFire, A Multiplayer game * * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team * Copyright (C) 2001-2003 Mark Wedel & Crossfire Development Team * Copyright (C) 1992 Frank Tore Johansen * * This program 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 2 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, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * The authors can be reached via e-mail at */ #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) { if (op) clear_win_info (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 (ob_blocked (this, 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 && contr->ranges[range_golem]) { int i = find_free_spot (contr->ranges[range_golem], newmap, x, y, 1, SIZEOFFREE); if (i < 0) { contr->ranges[range_golem]->destroy (); contr->ranges[range_golem] = 0; } else { newmap->insert (contr->ranges[range_golem], x + freearr_x[i], y + freearr_y[i]); contr->ranges[range_golem]->direction = find_dir_2 (x - contr->ranges[range_golem]->x, y - contr->ranges[range_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 () { int flag; /* Basically, we keep looping until all the players have done their actions. */ for (flag = 1; flag != 0;) { flag = 0; for_all_players (pl) { pl->refcnt_chk (); if (!pl->ob || !pl->ns || !pl->ob->active) continue; if (pl->ob->speed_left > 0) if (handle_newcs_player (pl->ob)) flag = 1; /* If the player is not actively playing, don't make a * backup save - nothing to save anyway. Plus, the * map may not longer be valid. This can happen when the * player quits - they exist for purposes of tracking on the map, * but don't actually reside on any actual map. */ if (QUERY_FLAG (pl->ob, FLAG_REMOVED)) continue; } /* end of for loop for all the players */ } /* for flag */ for_all_players (pl) { if (!pl->ob || !pl->ns || !pl->ob->active) continue; if (settings.casting_time) { if (pl->ob->casting_time > 0) { pl->ob->casting_time--; pl->ob->start_holding = 1; } /* set spell_state so we can update the range in stats field */ if ((pl->ob->casting_time == 0) && (pl->ob->start_holding == 1)) pl->ob->start_holding = 0; } do_some_living (pl->ob); } } static void process_players2 () { /* Then check if any players should use weapon-speed instead of speed */ for_all_players (pl) { /* The code that did weapon_sp handling here was out of place - * this isn't called until after the player has finished there * actions, and is thus out of place. All we do here is bounds * checking. */ if (pl->has_hit) { if (pl->ob->speed_left > pl->weapon_sp) pl->ob->speed_left = pl->weapon_sp; /* This needs to be here - if the player is running, we need to * clear this each tick, but new commands are not being received * so execute_newserver_command() is never called */ pl->has_hit = 0; } else if (pl->ob->speed_left > pl->ob->speed) pl->ob->speed_left = pl->ob->speed; } } void process_events () { process_players1 (); for_all_actives (op) { /* Now process op */ if (QUERY_FLAG (op, FLAG_FREED)) { LOG (llevError, "BUG: process_events(): Free object on list\n"); op->set_speed (0); continue; } /* I've seen occasional crashes due to this - the object is removed, * and thus the map it points to (last map it was on) may be bogus * The real bug is to try to find out the cause of this - someone * is probably calling remove_ob without either an insert_ob or * free_object afterwards, leaving an object dangling. But I'd * rather log this and continue on instead of crashing. * Don't remove players - when a player quits, the object is in * sort of a limbo, of removed, but something we want to keep * around. */ if (QUERY_FLAG (op, FLAG_REMOVED) && op->type != PLAYER && op->map && op->map->in_memory != MAP_IN_MEMORY) { LOG (llevError, "BUG: process_events(): Removed object on list\n"); char *dump = dump_object (op); LOG (llevError, dump); free (dump); op->destroy (); continue; } if (!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 (op->map == NULL && op->env == NULL && op->name && op->type != MAP) { LOG (llevError, "BUG: process_events(): Object without map or " "inventory is on active list: %s (%d)\n", &op->name, op->count); 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) { if ((op->type == PLAYER)) animate_object (op, op->facing); else animate_object (op, op->direction); op->last_anim = 1; } else op->last_anim++; if (op->speed_left > 0) { #if 0 /* I've seen occasional crashes in move_symptom() with it * crashing because op is removed - add some debugging to * track if it is removed at this point. * This unfortunately is a bit too verbose it seems - not sure * why - I think what happens is a map is freed or something and * some objects get 'lost' - removed never to be reclaimed. * removed objects generally shouldn't exist. */ if (QUERY_FLAG (op, FLAG_REMOVED)) { LOG (llevDebug, "process_events: calling process_object with removed object %s\n", op->name ? op->name : "null"); } #endif --op->speed_left; process_object (op); if (op->destroyed ()) continue; } if (settings.casting_time == TRUE && op->casting_time > 0) op->casting_time--; if (op->speed_left <= 0) op->speed_left += FABS (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_UNIQUE | NDI_ALL | (make_core ? NDI_RED : NDI_GREEN); new_draw_info_format (flags, 0, 0, "The server will now shutdown.\n"); new_draw_info_format (flags, 0, 0, "Cause for this shutdown: %s\n", cause); if (make_core) new_draw_info_format (flags, 0, 0, "This is considered a crash, but all maps and players have been saved.\n"); else new_draw_info_format (flags, 0, 0, "This is considered to be a clean shutdown, and all maps and players have been saved.\n"); new_draw_info_format (flags, 0, 0, "%s\n", CLEANUP_MESSAGE); 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); _exit (1); } else _exit (0); } int forbid_play (void) { #if !defined(_IBMR2) && !defined(___IBMR2) && defined(PERM_FILE) char buf[MAX_BUF], day[MAX_BUF]; FILE *fp; time_t clock; struct tm *tm; int i, start, stop, forbit = 0, comp; clock = time (NULL); tm = (struct tm *) localtime (&clock); sprintf (buf, "%s/%s", settings.confdir, PERM_FILE); if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL) return 0; while (fgets (buf, MAX_BUF, fp)) { if (buf[0] == '#') continue; if (!strncmp (buf, "msg", 3)) { if (forbit) while (fgets (buf, MAX_BUF, fp)) /* print message */ fputs (buf, logfile); break; } else if (sscanf (buf, "%s %d%*c%d\n", day, &start, &stop) != 3) { LOG (llevDebug, "Warning: Incomplete line in permission file ignored.\n"); continue; } for (i = 0; i < 7; i++) { if (!strncmp (buf, days[i], 3) && (tm->tm_wday == i) && (tm->tm_hour >= start) && (tm->tm_hour < stop)) forbit = 1; } } close_and_delete (fp, comp); return forbit; #else return 0; #endif } /* * 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. */ extern unsigned long todtick; void do_specials (void) { if (!(pticks % PTICKS_PER_CLOCK)) tick_the_clock (); if (!(pticks % 7)) shstr::gc (); if (!(pticks % 2503)) fix_weight (); /* Hack to fix weightproblems caused by bugs */ if (!(pticks % 5003)) write_book_archive (); if (!(pticks % 5009)) clean_friendly_list (); if (!(pticks % 5011)) obsolete_parties (); if (!(pticks % 12503)) fix_luck (); } void server_tick () { // first do the user visible stuff doeric_server (); INVOKE_GLOBAL (CLOCK); process_events (); /* "do" something with objects with speed */ flush_sockets (); // then do some bookkeeping, should not really be here do_specials (); /* Routines called from time to time. */ attachable::check_mortals (); ++pticks; } int main (int argc, char **argv) { settings.argc = argc; settings.argv = argv; init (argc, argv); initPlugins (); for (;;) cfperl_main (); }