/*
* This file is part of Deliantra, the Roguelike Realtime MMORPG.
*
* Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
* Copyright (©) 2001-2003,2007 Mark Wedel & Crossfire Development Team
* Copyright (©) 1992,2007 Frank Tore Johansen
*
* Deliantra is free software: you can redistribute it and/or modify it under
* the terms of the Affero 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 Affero GNU General Public License
* and 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
#include <../random_maps/random_map.h>
#include <../random_maps/rproto.h>
#include "path.h"
void
version (object *op)
{
new_draw_info_format (NDI_UNIQUE, 0, op, "This is Deliantra v%s", VERSION);
}
/* 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)
{
op->player_goto (op->contr->savebed_map, op->contr->bed_x, op->contr->bed_y);
}
/*
* 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_ACTIVE)
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->drop_and_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;
// process_objects destroys the speed_left value
pl->speed_left_save = ob->speed_left;
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)
{
// restore speed_left value saved by process_players1
pl->ob->speed_left = pl->speed_left_save;
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);
}
}
static void
process_events ()
{
process_players1 ();
for_all_actives (op)
{
// try to prefetch some stuff we expect to need
// obviously, it should be grouped into the same cacheline.
// preliminary results indicate that this gives halves the speed
// used for the inner loop
if (_i < actives.size ()) // HACK, rely on _i :/
{
object *next = actives [_i + 1];
prefetch (&next->flag , 0, 1);
prefetch (&next->speed , 0, 1);
prefetch (&next->anim_speed, 0, 1);
prefetch (&next->contr , 0, 1);
}
/* 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;
}
//TODO: remove soon
if (expect_false (op->speed < 0.))
{
LOG (llevError, "BUG: process_events(): Object %s has negative speed (%f), "
"but is on active list\n", op->debug_desc (), op->speed);
op->speed = -op->speed;
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 (expect_false (op->speed_left > 0.f))
{
--op->speed_left;
process_object (op);
}
// this will destroy the speed_left value for players, but
// process_players1 and ..2 save/restore the real value,
// so we can avoid a costly test here.
op->speed_left = min (op->speed, op->speed_left + 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");
log_cleanup ();
if (make_core)
cleanup_inform (cause, make_core);
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 (make_core);
}
/*
* 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.
*/
static void
do_specials (void)
{
shstr::gc ();
archetype::gc ();
if (expect_false (!(pticks % TICKS_PER_HOUR)))
maptile::adjust_daylight ();
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;
}
// normal main
int
main (int argc, char **argv)
{
settings.argc = argc;
settings.argv = argv;
rndm.seed (time (0));
g_thread_init (0); // for the slice allocator only
init (argc, argv);
for (;;)
cfperl_main ();
}