* Server/Client Environments now have an helper to cast Player object in the right type to use it
* Server: use RemotePlayer everywhere and remove previous added casts
* Client: use LocalPlayer where needed
* Environment: remove unused functions (getPlayers(), getRandomConnectedPlayer(), getNearestConnectedPlayer())
break;
case InventoryLocation::PLAYER:
{
- Player *player = m_env.getPlayer(loc.name.c_str());
+ LocalPlayer *player = m_env.getPlayer(loc.name.c_str());
if(!player)
return NULL;
return &player->inventory;
return NULL;
}
-Player * Environment::getRandomConnectedPlayer()
-{
- std::vector<Player*> connected_players = getPlayers(true);
- u32 chosen_one = myrand() % connected_players.size();
- u32 j = 0;
- for(std::vector<Player*>::iterator
- i = connected_players.begin();
- i != connected_players.end(); ++i) {
- if(j == chosen_one) {
- Player *player = *i;
- return player;
- }
- j++;
- }
- return NULL;
-}
-
-Player * Environment::getNearestConnectedPlayer(v3f pos)
-{
- std::vector<Player*> connected_players = getPlayers(true);
- f32 nearest_d = 0;
- Player *nearest_player = NULL;
- for(std::vector<Player*>::iterator
- i = connected_players.begin();
- i != connected_players.end(); ++i) {
- Player *player = *i;
- f32 d = player->getPosition().getDistanceFrom(pos);
- if(d < nearest_d || nearest_player == NULL) {
- nearest_d = d;
- nearest_player = player;
- }
- }
- return nearest_player;
-}
-
-std::vector<Player*> Environment::getPlayers()
-{
- return m_players;
-}
-
-std::vector<Player*> Environment::getPlayers(bool ignore_disconnected)
-{
- std::vector<Player*> newlist;
- for(std::vector<Player*>::iterator
- i = m_players.begin();
- i != m_players.end(); ++i) {
- Player *player = *i;
-
- if(ignore_disconnected) {
- // Ignore disconnected players
- if(player->peer_id == 0)
- continue;
- }
-
- newlist.push_back(player);
- }
- return newlist;
-}
-
u32 Environment::getDayNightRatio()
{
MutexAutoLock lock(this->m_time_lock);
m_time_of_day_speed = speed;
}
-float Environment::getTimeOfDaySpeed()
-{
- return m_time_of_day_speed;
-}
-
void Environment::setDayNightRatioOverride(bool enable, u32 value)
{
MutexAutoLock lock(this->m_time_lock);
return *m_map;
}
+RemotePlayer *ServerEnvironment::getPlayer(const u16 peer_id)
+{
+ return dynamic_cast<RemotePlayer *>(Environment::getPlayer(peer_id));
+}
+
+RemotePlayer *ServerEnvironment::getPlayer(const char* name)
+{
+ return dynamic_cast<RemotePlayer *>(Environment::getPlayer(name));
+}
+
bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16 *p)
{
float distance = pos1.getDistanceFrom(pos2);
return *m_map;
}
+LocalPlayer *ClientEnvironment::getPlayer(const u16 peer_id)
+{
+ return dynamic_cast<LocalPlayer *>(Environment::getPlayer(peer_id));
+}
+
+LocalPlayer *ClientEnvironment::getPlayer(const char* name)
+{
+ return dynamic_cast<LocalPlayer *>(Environment::getPlayer(name));
+}
+
void ClientEnvironment::addPlayer(Player *player)
{
DSTACK(FUNCTION_NAME);
virtual void addPlayer(Player *player);
void removePlayer(Player *player);
- Player * getPlayer(u16 peer_id);
- Player * getPlayer(const char *name);
- Player * getRandomConnectedPlayer();
- Player * getNearestConnectedPlayer(v3f pos);
- std::vector<Player*> getPlayers();
- std::vector<Player*> getPlayers(bool ignore_disconnected);
u32 getDayNightRatio();
void stepTimeOfDay(float dtime);
void setTimeOfDaySpeed(float speed);
- float getTimeOfDaySpeed();
void setDayNightRatioOverride(bool enable, u32 value);
u32 m_added_objects;
protected:
+ Player * getPlayer(u16 peer_id);
+ Player * getPlayer(const char *name);
+
// peer_ids in here should be unique, except that there may be many 0s
std::vector<Player*> m_players;
void setStaticForActiveObjectsInBlock(v3s16 blockpos,
bool static_exists, v3s16 static_block=v3s16(0,0,0));
+ RemotePlayer *getPlayer(const u16 peer_id);
+ RemotePlayer *getPlayer(const char* name);
private:
/*
{ m_player_names.remove(name); }
void updateCameraOffset(v3s16 camera_offset)
{ m_camera_offset = camera_offset; }
- v3s16 getCameraOffset()
- { return m_camera_offset; }
+ v3s16 getCameraOffset() const { return m_camera_offset; }
+
+ LocalPlayer *getPlayer(const u16 peer_id);
+ LocalPlayer *getPlayer(const char* name);
private:
ClientMap *m_map;
pitch = modulo360f(pitch);
yaw = modulo360f(yaw);
- RemotePlayer *player =
- dynamic_cast<RemotePlayer *>(m_env->getPlayer(pkt->getPeerId()));
+ RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
void Server::handleCommand_InventoryAction(NetworkPacket* pkt)
{
- RemotePlayer *player =
- dynamic_cast<RemotePlayer *>(m_env->getPlayer(pkt->getPeerId()));
+ RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
*pkt >> damage;
- RemotePlayer *player =
- dynamic_cast<RemotePlayer *>(m_env->getPlayer(pkt->getPeerId()));
+ RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
*pkt >> breath;
- RemotePlayer *player =
- dynamic_cast<RemotePlayer *>(m_env->getPlayer(pkt->getPeerId()));
+ RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
if (pkt->getSize() < 2)
return;
- RemotePlayer *player =
- dynamic_cast<RemotePlayer *>(m_env->getPlayer(pkt->getPeerId()));
+ RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
verbosestream << "TOSERVER_INTERACT: action=" << (int)action << ", item="
<< item_i << ", pointed=" << pointed.dump() << std::endl;
- RemotePlayer *player =
- dynamic_cast<RemotePlayer *>(m_env->getPlayer(pkt->getPeerId()));
+ RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
fields[fieldname] = pkt->readLongString();
}
- RemotePlayer *player =
- dynamic_cast<RemotePlayer *>(m_env->getPlayer(pkt->getPeerId()));
+ RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
fields[fieldname] = pkt->readLongString();
}
- RemotePlayer *player =
- dynamic_cast<RemotePlayer *>(m_env->getPlayer(pkt->getPeerId()));
+ RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: "
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);
}
-void InvRef::createPlayer(lua_State *L, Player *player)
+void InvRef::createPlayer(lua_State *L, RemotePlayer *player)
{
NO_MAP_LOCK_REQUIRED;
InventoryLocation loc;
#include "inventory.h"
#include "inventorymanager.h"
-class Player;
+class RemotePlayer;
/*
InvRef
// Creates an InvRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.
static void create(lua_State *L, const InventoryLocation &loc);
- static void createPlayer(lua_State *L, Player *player);
+ static void createPlayer(lua_State *L, RemotePlayer *player);
static void createNodeMeta(lua_State *L, v3s16 p);
static void Register(lua_State *L);
};
static int l_get_inventory(lua_State *L);
- static void inventory_set_list_from_lua(Inventory *inv, const char *name,
- lua_State *L, int tableindex, int forcesize);
- static void inventory_get_list_to_lua(Inventory *inv, const char *name,
- lua_State *L);
-
public:
static void Initialize(lua_State *L, int top);
};
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
- Player *player = getplayer(ref);
+ RemotePlayer *player = getplayer(ref);
if (player == NULL)
return 0;
// Do it
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
- Player *player = getplayer(ref);
+ RemotePlayer *player = getplayer(ref);
if (player == NULL)
return 0;
// Do it
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
- Player *player = getplayer(ref);
+ RemotePlayer *player = getplayer(ref);
if (player == NULL)
return 0;
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
- Player *player = getplayer(ref);
+ RemotePlayer *player = getplayer(ref);
if (player == NULL)
return 0;
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
- Player *player = getplayer(ref);
+ RemotePlayer *player = getplayer(ref);
if (player == NULL)
return 0;
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
- Player *player = getplayer(ref);
+ RemotePlayer *player = getplayer(ref);
if (player == NULL)
return 0;
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
- Player *player = getplayer(ref);
+ RemotePlayer *player = getplayer(ref);
if (player == NULL)
return 0;
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
- Player *player = getplayer(ref);
+ RemotePlayer *player = getplayer(ref);
if (player == NULL)
return 0;
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
- Player *player = getplayer(ref);
+ RemotePlayer *player = getplayer(ref);
if (player == NULL)
return 0;
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
- Player *player = getplayer(ref);
+ RemotePlayer *player = getplayer(ref);
if (player == NULL)
return 0;
class ServerActiveObject;
class LuaEntitySAO;
class PlayerSAO;
-class Player;
class RemotePlayer;
/*
{
NO_MAP_LOCK_REQUIRED;
const char * name = luaL_checkstring(L, 1);
- Player *player = getEnv(L)->getPlayer(name);
+ RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
if(player == NULL)
{
lua_pushnil(L); // no such player
NO_MAP_LOCK_REQUIRED;
const char * name = luaL_checkstring(L, 1);
- Player *player = getEnv(L)->getPlayer(name);
- if(player == NULL)
- {
+ RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
+ if (player == NULL) {
lua_pushnil(L); // no such player
return 1;
}
{
NO_MAP_LOCK_REQUIRED;
const char * name = luaL_checkstring(L, 1);
- Player *player = getEnv(L)->getPlayer(name);
- if(player == NULL)
- {
+ RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
+ if (player == NULL) {
lua_pushboolean(L, false); // no such player
return 1;
}
try
{
- Address addr = getServer(L)->getPeerAddress(getEnv(L)->getPlayer(name)->peer_id);
+ Address addr = getServer(L)->getPeerAddress(
+ dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name)->peer_id);
std::string ip_str = addr.serializeString();
getServer(L)->setIpBanned(ip_str, name);
}
{
message = "Kicked.";
}
- Player *player = getEnv(L)->getPlayer(name);
- if (player == NULL)
- {
+
+ RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
+ if (player == NULL) {
lua_pushboolean(L, false); // No such player
return 1;
}
++i;
}
- RemotePlayer *player = dynamic_cast<RemotePlayer *>(m_env->getPlayer(peer_id));
+ RemotePlayer *player = m_env->getPlayer(peer_id);
/* Run scripts and remove from environment */
if(player != NULL) {
PlayerSAO* Server::getPlayerSAO(u16 peer_id)
{
- RemotePlayer *player = dynamic_cast<RemotePlayer *>(m_env->getPlayer(peer_id));
+ RemotePlayer *player = m_env->getPlayer(peer_id);
if (player == NULL)
return NULL;
return player->getPlayerSAO();
return true;
}
-u32 Server::hudAdd(Player *player, HudElement *form)
+u32 Server::hudAdd(RemotePlayer *player, HudElement *form)
{
if (!player)
return -1;
return id;
}
-bool Server::hudRemove(Player *player, u32 id) {
+bool Server::hudRemove(RemotePlayer *player, u32 id) {
if (!player)
return false;
return true;
}
-bool Server::hudChange(Player *player, u32 id, HudElementStat stat, void *data)
+bool Server::hudChange(RemotePlayer *player, u32 id, HudElementStat stat, void *data)
{
if (!player)
return false;
return true;
}
-void Server::hudSetHotbarImage(Player *player, std::string name)
+void Server::hudSetHotbarImage(RemotePlayer *player, std::string name)
{
if (!player)
return;
SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_IMAGE, name);
}
-std::string Server::hudGetHotbarImage(Player *player)
+std::string Server::hudGetHotbarImage(RemotePlayer *player)
{
if (!player)
return "";
return player->getHotbarImage();
}
-void Server::hudSetHotbarSelectedImage(Player *player, std::string name)
+void Server::hudSetHotbarSelectedImage(RemotePlayer *player, std::string name)
{
if (!player)
return;
SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_SELECTED_IMAGE, name);
}
-std::string Server::hudGetHotbarSelectedImage(Player *player)
+std::string Server::hudGetHotbarSelectedImage(RemotePlayer *player)
{
if (!player)
return "";
return player->getHotbarSelectedImage();
}
-bool Server::setLocalPlayerAnimations(Player *player,
- v2s32 animation_frames[4], f32 frame_speed)
+bool Server::setLocalPlayerAnimations(RemotePlayer *player,
+ v2s32 animation_frames[4], f32 frame_speed)
{
if (!player)
return false;
return true;
}
-bool Server::setPlayerEyeOffset(Player *player, v3f first, v3f third)
+bool Server::setPlayerEyeOffset(RemotePlayer *player, v3f first, v3f third)
{
if (!player)
return false;
return true;
}
-bool Server::setSky(Player *player, const video::SColor &bgcolor,
+bool Server::setSky(RemotePlayer *player, const video::SColor &bgcolor,
const std::string &type, const std::vector<std::string> ¶ms)
{
if (!player)
Map & getMap() { return m_env->getMap(); }
ServerEnvironment & getEnv() { return *m_env; }
- u32 hudAdd(Player *player, HudElement *element);
- bool hudRemove(Player *player, u32 id);
- bool hudChange(Player *player, u32 id, HudElementStat stat, void *value);
+ u32 hudAdd(RemotePlayer *player, HudElement *element);
+ bool hudRemove(RemotePlayer *player, u32 id);
+ bool hudChange(RemotePlayer *player, u32 id, HudElementStat stat, void *value);
bool hudSetFlags(RemotePlayer *player, u32 flags, u32 mask);
bool hudSetHotbarItemcount(RemotePlayer *player, s32 hotbar_itemcount);
s32 hudGetHotbarItemcount(RemotePlayer *player) const
{ return player->getHotbarItemcount(); }
- void hudSetHotbarImage(Player *player, std::string name);
- std::string hudGetHotbarImage(Player *player);
- void hudSetHotbarSelectedImage(Player *player, std::string name);
- std::string hudGetHotbarSelectedImage(Player *player);
+ void hudSetHotbarImage(RemotePlayer *player, std::string name);
+ std::string hudGetHotbarImage(RemotePlayer *player);
+ void hudSetHotbarSelectedImage(RemotePlayer *player, std::string name);
+ std::string hudGetHotbarSelectedImage(RemotePlayer *player);
inline Address getPeerAddress(u16 peer_id)
{ return m_con.GetPeerAddress(peer_id); }
- bool setLocalPlayerAnimations(Player *player, v2s32 animation_frames[4], f32 frame_speed);
- bool setPlayerEyeOffset(Player *player, v3f first, v3f third);
+ bool setLocalPlayerAnimations(RemotePlayer *player, v2s32 animation_frames[4],
+ f32 frame_speed);
+ bool setPlayerEyeOffset(RemotePlayer *player, v3f first, v3f third);
- bool setSky(Player *player, const video::SColor &bgcolor,
+ bool setSky(RemotePlayer *player, const video::SColor &bgcolor,
const std::string &type, const std::vector<std::string> ¶ms);
bool overrideDayNightRatio(RemotePlayer *player, bool do_override, float brightness);
class ServerEnvironment;
struct ItemStack;
-class Player;
struct ToolCapabilities;
struct ObjectProperties;
// environment
virtual bool environmentDeletes() const
{ return true; }
-
+
// Create a certain type of ServerActiveObject
static ServerActiveObject* create(ActiveObjectType type,
ServerEnvironment *env, u16 id, v3f pos,
const std::string &data);
-
+
/*
Some simple getters/setters
*/
v3f getBasePosition(){ return m_base_position; }
void setBasePosition(v3f pos){ m_base_position = pos; }
ServerEnvironment* getEnv(){ return m_env; }
-
+
/*
Some more dynamic interface
*/
-
+
virtual void setPos(v3f pos)
{ setBasePosition(pos); }
// continuous: if true, object does not stop immediately at pos
virtual float getMinimumSavedMovement();
virtual std::string getDescription(){return "SAO";}
-
+
/*
Step object in time.
Messages added to messages are sent to client over network.
packet.
*/
virtual void step(float dtime, bool send_recommended){}
-
+
/*
The return value of this is passed to the client-side object
when it is created
*/
virtual std::string getClientInitializationData(u16 protocol_version){return "";}
-
+
/*
The return value of this is passed to the server-side object
when it is created (converted from static to active - actually
*/
virtual bool isStaticAllowed() const
{return true;}
-
+
// Returns tool wear
virtual int punch(v3f dir,
const ToolCapabilities *toolcap=NULL,
- This can be set to true by anything else too.
*/
bool m_removed;
-
+
/*
This is set to true when an object should be removed from the active
object list but couldn't be removed because the id has to be
list.
*/
bool m_pending_deactivation;
-
+
/*
Whether the object's static data has been stored to a block
*/
a copy of the static data resides.
*/
v3s16 m_static_block;
-
+
/*
Queue of messages to be sent to the client
*/
std::queue<ActiveObjectMessage> m_messages_out;
-
+
protected:
// Used for creating objects based on type
typedef ServerActiveObject* (*Factory)