2011-12-02 03:44:20 -05:00
|
|
|
/*
|
|
|
|
Minetest-c55
|
|
|
|
Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
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.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "serverremoteplayer.h"
|
|
|
|
#include "main.h" // For g_settings
|
|
|
|
#include "settings.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "gamedef.h"
|
2012-01-12 00:10:39 -05:00
|
|
|
#include "inventory.h"
|
2011-12-02 03:44:20 -05:00
|
|
|
#include "environment.h"
|
2012-02-28 12:45:23 -05:00
|
|
|
#include "tool.h"
|
2011-12-02 03:44:20 -05:00
|
|
|
|
|
|
|
ServerRemotePlayer::ServerRemotePlayer(ServerEnvironment *env):
|
|
|
|
Player(env->getGameDef()),
|
|
|
|
ServerActiveObject(env, v3f(0,0,0)),
|
|
|
|
m_last_good_position(0,0,0),
|
|
|
|
m_last_good_position_age(0),
|
2012-01-12 00:10:39 -05:00
|
|
|
m_wield_index(0),
|
2011-12-02 03:44:20 -05:00
|
|
|
m_inventory_not_sent(false),
|
|
|
|
m_hp_not_sent(false),
|
|
|
|
m_is_in_environment(false),
|
2011-12-04 07:43:01 -05:00
|
|
|
m_time_from_last_punch(0),
|
2011-12-02 03:44:20 -05:00
|
|
|
m_position_not_sent(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
ServerRemotePlayer::ServerRemotePlayer(ServerEnvironment *env, v3f pos_, u16 peer_id_,
|
|
|
|
const char *name_):
|
|
|
|
Player(env->getGameDef()),
|
|
|
|
ServerActiveObject(env, pos_),
|
2011-12-04 07:43:01 -05:00
|
|
|
m_last_good_position(0,0,0),
|
|
|
|
m_last_good_position_age(0),
|
2012-01-14 05:05:24 -05:00
|
|
|
m_wield_index(0),
|
2011-12-02 03:44:20 -05:00
|
|
|
m_inventory_not_sent(false),
|
|
|
|
m_hp_not_sent(false),
|
|
|
|
m_is_in_environment(false),
|
2011-12-04 07:43:01 -05:00
|
|
|
m_time_from_last_punch(0),
|
2011-12-02 03:44:20 -05:00
|
|
|
m_position_not_sent(false)
|
|
|
|
{
|
|
|
|
setPosition(pos_);
|
|
|
|
peer_id = peer_id_;
|
|
|
|
updateName(name_);
|
|
|
|
}
|
|
|
|
ServerRemotePlayer::~ServerRemotePlayer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerRemotePlayer::setPosition(const v3f &position)
|
|
|
|
{
|
|
|
|
Player::setPosition(position);
|
|
|
|
ServerActiveObject::setBasePosition(position);
|
|
|
|
m_position_not_sent = true;
|
|
|
|
}
|
|
|
|
|
2012-01-12 00:10:39 -05:00
|
|
|
Inventory* ServerRemotePlayer::getInventory()
|
2011-12-02 03:44:20 -05:00
|
|
|
{
|
2012-01-12 00:10:39 -05:00
|
|
|
return &inventory;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Inventory* ServerRemotePlayer::getInventory() const
|
|
|
|
{
|
|
|
|
return &inventory;
|
|
|
|
}
|
|
|
|
|
|
|
|
InventoryLocation ServerRemotePlayer::getInventoryLocation() const
|
|
|
|
{
|
|
|
|
InventoryLocation loc;
|
|
|
|
loc.setPlayer(getName());
|
|
|
|
return loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerRemotePlayer::setInventoryModified()
|
|
|
|
{
|
|
|
|
m_inventory_not_sent = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ServerRemotePlayer::getWieldList() const
|
|
|
|
{
|
|
|
|
return "main";
|
|
|
|
}
|
|
|
|
|
|
|
|
int ServerRemotePlayer::getWieldIndex() const
|
|
|
|
{
|
|
|
|
return m_wield_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerRemotePlayer::setWieldIndex(int i)
|
|
|
|
{
|
|
|
|
m_wield_index = i;
|
2011-12-02 03:44:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ServerActiveObject interface */
|
|
|
|
|
|
|
|
void ServerRemotePlayer::addedToEnvironment()
|
|
|
|
{
|
|
|
|
assert(!m_is_in_environment);
|
|
|
|
m_is_in_environment = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerRemotePlayer::removingFromEnvironment()
|
|
|
|
{
|
|
|
|
assert(m_is_in_environment);
|
|
|
|
m_is_in_environment = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ServerRemotePlayer::unlimitedTransferDistance() const
|
|
|
|
{
|
2011-12-02 04:22:09 -05:00
|
|
|
return g_settings->getBool("unlimited_player_transfer_distance");
|
2011-12-02 03:44:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServerRemotePlayer::step(float dtime, bool send_recommended)
|
|
|
|
{
|
2011-12-04 07:43:01 -05:00
|
|
|
m_time_from_last_punch += dtime;
|
|
|
|
|
2011-12-02 03:44:20 -05:00
|
|
|
if(send_recommended == false)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(m_position_not_sent)
|
|
|
|
{
|
|
|
|
m_position_not_sent = false;
|
|
|
|
|
|
|
|
std::ostringstream os(std::ios::binary);
|
|
|
|
// command (0 = update position)
|
|
|
|
writeU8(os, 0);
|
|
|
|
// pos
|
|
|
|
writeV3F1000(os, getPosition());
|
|
|
|
// yaw
|
|
|
|
writeF1000(os, getYaw());
|
|
|
|
// create message and add to list
|
|
|
|
ActiveObjectMessage aom(getId(), false, os.str());
|
|
|
|
m_messages_out.push_back(aom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ServerRemotePlayer::getClientInitializationData()
|
|
|
|
{
|
|
|
|
std::ostringstream os(std::ios::binary);
|
|
|
|
// version
|
|
|
|
writeU8(os, 0);
|
|
|
|
// name
|
|
|
|
os<<serializeString(getName());
|
|
|
|
// pos
|
|
|
|
writeV3F1000(os, getPosition());
|
|
|
|
// yaw
|
|
|
|
writeF1000(os, getYaw());
|
2012-01-23 18:00:26 -05:00
|
|
|
// dead
|
|
|
|
writeU8(os, getHP() == 0);
|
2011-12-02 03:44:20 -05:00
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ServerRemotePlayer::getStaticData()
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerRemotePlayer::punch(ServerActiveObject *puncher,
|
|
|
|
float time_from_last_punch)
|
|
|
|
{
|
|
|
|
if(!puncher)
|
|
|
|
return;
|
|
|
|
|
2011-12-02 04:25:47 -05:00
|
|
|
// No effect if PvP disabled
|
|
|
|
if(g_settings->getBool("enable_pvp") == false){
|
|
|
|
if(puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-28 12:45:23 -05:00
|
|
|
// "Material" groups of the player
|
|
|
|
std::map<std::string, int> groups;
|
|
|
|
groups["snappy"] = 1;
|
|
|
|
groups["choppy"] = 2;
|
2011-12-02 03:44:20 -05:00
|
|
|
|
2012-01-12 00:10:39 -05:00
|
|
|
IItemDefManager *idef = m_env->getGameDef()->idef();
|
|
|
|
ItemStack punchitem = puncher->getWieldedItem();
|
2012-02-28 12:45:23 -05:00
|
|
|
ToolCapabilities tp = punchitem.getToolCapabilities(idef);
|
2011-12-02 03:44:20 -05:00
|
|
|
|
2012-02-28 12:45:23 -05:00
|
|
|
HitParams hitparams = getHitParams(groups, &tp, time_from_last_punch);
|
2011-12-02 03:44:20 -05:00
|
|
|
|
2011-12-04 07:43:01 -05:00
|
|
|
actionstream<<"Player "<<getName()<<" punched by "
|
2012-02-28 12:45:23 -05:00
|
|
|
<<puncher->getDescription()<<", damage "<<hitparams.hp
|
2011-12-04 07:43:01 -05:00
|
|
|
<<" HP"<<std::endl;
|
|
|
|
|
2012-02-28 12:45:23 -05:00
|
|
|
setHP(getHP() - hitparams.hp);
|
|
|
|
punchitem.addWear(hitparams.wear, idef);
|
2012-01-12 00:10:39 -05:00
|
|
|
puncher->setWieldedItem(punchitem);
|
2011-12-04 07:43:01 -05:00
|
|
|
|
2012-02-28 12:45:23 -05:00
|
|
|
if(hitparams.hp != 0)
|
2011-12-02 03:57:40 -05:00
|
|
|
{
|
|
|
|
std::ostringstream os(std::ios::binary);
|
|
|
|
// command (1 = punched)
|
|
|
|
writeU8(os, 1);
|
|
|
|
// damage
|
2012-02-28 12:45:23 -05:00
|
|
|
writeS16(os, hitparams.hp);
|
2011-12-02 03:57:40 -05:00
|
|
|
// create message and add to list
|
|
|
|
ActiveObjectMessage aom(getId(), false, os.str());
|
|
|
|
m_messages_out.push_back(aom);
|
|
|
|
}
|
2011-12-02 03:44:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServerRemotePlayer::rightClick(ServerActiveObject *clicker)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerRemotePlayer::setPos(v3f pos)
|
|
|
|
{
|
|
|
|
setPosition(pos);
|
|
|
|
// Movement caused by this command is always valid
|
|
|
|
m_last_good_position = pos;
|
|
|
|
m_last_good_position_age = 0;
|
|
|
|
}
|
|
|
|
void ServerRemotePlayer::moveTo(v3f pos, bool continuous)
|
|
|
|
{
|
|
|
|
setPosition(pos);
|
|
|
|
// Movement caused by this command is always valid
|
|
|
|
m_last_good_position = pos;
|
|
|
|
m_last_good_position_age = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerRemotePlayer::setHP(s16 hp_)
|
|
|
|
{
|
|
|
|
s16 oldhp = hp;
|
|
|
|
|
|
|
|
// FIXME: don't hardcode maximum HP, make configurable per object
|
|
|
|
if(hp_ < 0)
|
|
|
|
hp_ = 0;
|
|
|
|
else if(hp_ > 20)
|
|
|
|
hp_ = 20;
|
|
|
|
hp = hp_;
|
|
|
|
|
|
|
|
if(hp != oldhp)
|
|
|
|
m_hp_not_sent = true;
|
2012-01-23 18:00:26 -05:00
|
|
|
|
|
|
|
// On death or reincarnation send an active object message
|
|
|
|
if((hp == 0) != (oldhp == 0))
|
|
|
|
{
|
|
|
|
std::ostringstream os(std::ios::binary);
|
|
|
|
// command (2 = update death state)
|
|
|
|
writeU8(os, 2);
|
|
|
|
// dead?
|
|
|
|
writeU8(os, hp == 0);
|
|
|
|
// create message and add to list
|
|
|
|
ActiveObjectMessage aom(getId(), false, os.str());
|
|
|
|
m_messages_out.push_back(aom);
|
|
|
|
}
|
2011-12-02 03:44:20 -05:00
|
|
|
}
|
|
|
|
s16 ServerRemotePlayer::getHP()
|
|
|
|
{
|
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|