2011-06-26 14:48:56 +02:00
|
|
|
/*
|
2013-02-24 18:40:43 +01:00
|
|
|
Minetest
|
2013-02-24 19:38:45 +01:00
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2020-04-11 11:22:15 +02:00
|
|
|
Copyright (C) 2013-2020 Minetest core developers & community
|
2011-06-26 14:48:56 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
2012-06-05 16:56:56 +02:00
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
2011-06-26 14:48:56 +02:00
|
|
|
(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
|
2012-06-05 16:56:56 +02:00
|
|
|
GNU Lesser General Public License for more details.
|
2011-06-26 14:48:56 +02:00
|
|
|
|
2012-06-05 16:56:56 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
2011-06-26 14:48:56 +02:00
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2020-04-11 11:22:15 +02:00
|
|
|
#include "luaentity_sao.h"
|
2011-06-26 14:48:56 +02:00
|
|
|
#include "collision.h"
|
2020-04-11 11:22:15 +02:00
|
|
|
#include "constants.h"
|
|
|
|
#include "player_sao.h"
|
2017-04-25 19:38:08 +02:00
|
|
|
#include "scripting_server.h"
|
2020-04-11 11:22:15 +02:00
|
|
|
#include "server.h"
|
|
|
|
#include "serverenvironment.h"
|
2011-11-11 18:33:17 +01:00
|
|
|
|
2020-04-11 11:22:15 +02:00
|
|
|
LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos, const std::string &data)
|
|
|
|
: UnitSAO(env, pos)
|
2011-11-11 18:33:17 +01:00
|
|
|
{
|
2012-03-04 20:08:03 +01:00
|
|
|
std::string name;
|
|
|
|
std::string state;
|
2019-02-11 00:03:26 +01:00
|
|
|
u16 hp = 1;
|
2012-03-04 20:08:03 +01:00
|
|
|
v3f velocity;
|
2018-11-28 09:38:50 +01:00
|
|
|
v3f rotation;
|
2018-12-24 00:22:27 +01:00
|
|
|
|
|
|
|
while (!data.empty()) { // breakable, run for one iteration
|
2012-03-10 00:38:48 +01:00
|
|
|
std::istringstream is(data, std::ios::binary);
|
2018-12-24 00:22:27 +01:00
|
|
|
// 'version' does not allow to incrementally extend the parameter list thus
|
|
|
|
// we need another variable to build on top of 'version=1'. Ugly hack but works™
|
|
|
|
u8 version2 = 0;
|
2012-03-10 00:38:48 +01:00
|
|
|
u8 version = readU8(is);
|
2018-12-24 00:22:27 +01:00
|
|
|
|
|
|
|
name = deSerializeString(is);
|
|
|
|
state = deSerializeLongString(is);
|
|
|
|
|
|
|
|
if (version < 1)
|
|
|
|
break;
|
|
|
|
|
2019-02-11 00:03:26 +01:00
|
|
|
hp = readU16(is);
|
2018-12-24 00:22:27 +01:00
|
|
|
velocity = readV3F1000(is);
|
|
|
|
// yaw must be yaw to be backwards-compatible
|
|
|
|
rotation.Y = readF1000(is);
|
|
|
|
|
|
|
|
if (is.good()) // EOF for old formats
|
|
|
|
version2 = readU8(is);
|
|
|
|
|
|
|
|
if (version2 < 1) // PROTOCOL_VERSION < 37
|
|
|
|
break;
|
|
|
|
|
|
|
|
// version2 >= 1
|
|
|
|
rotation.X = readF1000(is);
|
|
|
|
rotation.Z = readF1000(is);
|
|
|
|
|
|
|
|
// if (version2 < 2)
|
|
|
|
// break;
|
|
|
|
// <read new values>
|
|
|
|
break;
|
2012-03-04 20:08:03 +01:00
|
|
|
}
|
2011-11-11 18:33:17 +01:00
|
|
|
// create object
|
2018-12-24 00:22:27 +01:00
|
|
|
infostream << "LuaEntitySAO::create(name=\"" << name << "\" state=\""
|
|
|
|
<< state << "\")" << std::endl;
|
2020-04-11 11:22:15 +02:00
|
|
|
|
|
|
|
m_init_name = name;
|
|
|
|
m_init_state = state;
|
|
|
|
m_hp = hp;
|
|
|
|
m_velocity = velocity;
|
|
|
|
m_rotation = rotation;
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaEntitySAO::~LuaEntitySAO()
|
|
|
|
{
|
|
|
|
if(m_registered){
|
|
|
|
m_env->getScriptIface()->luaentity_Remove(m_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (u32 attached_particle_spawner : m_attached_particle_spawners) {
|
|
|
|
m_env->deleteParticleSpawner(attached_particle_spawner, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaEntitySAO::addedToEnvironment(u32 dtime_s)
|
|
|
|
{
|
|
|
|
ServerActiveObject::addedToEnvironment(dtime_s);
|
|
|
|
|
|
|
|
// Create entity from name
|
|
|
|
m_registered = m_env->getScriptIface()->
|
|
|
|
luaentity_Add(m_id, m_init_name.c_str());
|
|
|
|
|
|
|
|
if(m_registered){
|
|
|
|
// Get properties
|
|
|
|
m_env->getScriptIface()->
|
|
|
|
luaentity_GetProperties(m_id, this, &m_prop);
|
|
|
|
// Initialize HP from properties
|
|
|
|
m_hp = m_prop.hp_max;
|
|
|
|
// Activate entity, supplying serialized state
|
|
|
|
m_env->getScriptIface()->
|
|
|
|
luaentity_Activate(m_id, m_init_state, dtime_s);
|
|
|
|
} else {
|
|
|
|
m_prop.infotext = m_init_name;
|
|
|
|
}
|
2011-11-11 18:33:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LuaEntitySAO::step(float dtime, bool send_recommended)
|
|
|
|
{
|
2012-03-29 16:46:21 +02:00
|
|
|
if(!m_properties_sent)
|
|
|
|
{
|
|
|
|
m_properties_sent = true;
|
|
|
|
std::string str = getPropertyPacket();
|
|
|
|
// create message and add to list
|
2020-05-26 17:38:31 +02:00
|
|
|
m_messages_out.emplace(getId(), true, str);
|
2012-03-29 16:46:21 +02:00
|
|
|
}
|
|
|
|
|
Update attachments at the ending of the addToScene function for parents. And with this... *drum roll* Client-side attachments are at last functional and stick visibly.
Fix the last segmentation fault (apparently). So far attachments seem to be fully functional, although removing the parent causes children to go to origin 0,0,0 and possibly still cause such a fault (though this should already be addressed)
Fix a bug in falling code where entities get stuck
Also check if the parent has been removed server-side, and detach the child if so. Fixes children going to origin 0,0,0 when their parent is removed.
Unset all attachment properties when permanently detaching (on both the client and server). Also store less data we don't need
Create a separate function for detaching, and also update lua api documentation
When a child is detached, update its position from the server to clients. This WILL cause it to get positioned slightly differently client side, as the server attachment system only copies parent origin and knows not about mesh / bone transformation. This prevents different clients seeing the object detached in different spots which is most correct
Update the position of attached players to clients. An attached player will see himself move, but this is currently VERY ugly and laggy as it is done by the server (it probably must stay this way too)
Use a different approach for locally attached players. This allows for smooth positio transitions to work, as well at the player turning around freely. Still buggy however
2012-11-07 17:42:38 +01:00
|
|
|
// If attached, check that our parent is still there. If it isn't, detach.
|
2020-06-04 19:31:46 +02:00
|
|
|
if (m_attachment_parent_id && !isAttached()) {
|
|
|
|
// This is handled when objects are removed from the map
|
|
|
|
warningstream << "LuaEntitySAO::step() id=" << m_id <<
|
|
|
|
" is attached to nonexistent parent. This is a bug." << std::endl;
|
|
|
|
clearParentAttachment();
|
Update attachments at the ending of the addToScene function for parents. And with this... *drum roll* Client-side attachments are at last functional and stick visibly.
Fix the last segmentation fault (apparently). So far attachments seem to be fully functional, although removing the parent causes children to go to origin 0,0,0 and possibly still cause such a fault (though this should already be addressed)
Fix a bug in falling code where entities get stuck
Also check if the parent has been removed server-side, and detach the child if so. Fixes children going to origin 0,0,0 when their parent is removed.
Unset all attachment properties when permanently detaching (on both the client and server). Also store less data we don't need
Create a separate function for detaching, and also update lua api documentation
When a child is detached, update its position from the server to clients. This WILL cause it to get positioned slightly differently client side, as the server attachment system only copies parent origin and knows not about mesh / bone transformation. This prevents different clients seeing the object detached in different spots which is most correct
Update the position of attached players to clients. An attached player will see himself move, but this is currently VERY ugly and laggy as it is done by the server (it probably must stay this way too)
Use a different approach for locally attached players. This allows for smooth positio transitions to work, as well at the player turning around freely. Still buggy however
2012-11-07 17:42:38 +01:00
|
|
|
sendPosition(false, true);
|
|
|
|
}
|
|
|
|
|
2011-11-12 11:12:15 +01:00
|
|
|
m_last_sent_position_timer += dtime;
|
Update attachments at the ending of the addToScene function for parents. And with this... *drum roll* Client-side attachments are at last functional and stick visibly.
Fix the last segmentation fault (apparently). So far attachments seem to be fully functional, although removing the parent causes children to go to origin 0,0,0 and possibly still cause such a fault (though this should already be addressed)
Fix a bug in falling code where entities get stuck
Also check if the parent has been removed server-side, and detach the child if so. Fixes children going to origin 0,0,0 when their parent is removed.
Unset all attachment properties when permanently detaching (on both the client and server). Also store less data we don't need
Create a separate function for detaching, and also update lua api documentation
When a child is detached, update its position from the server to clients. This WILL cause it to get positioned slightly differently client side, as the server attachment system only copies parent origin and knows not about mesh / bone transformation. This prevents different clients seeing the object detached in different spots which is most correct
Update the position of attached players to clients. An attached player will see himself move, but this is currently VERY ugly and laggy as it is done by the server (it probably must stay this way too)
Use a different approach for locally attached players. This allows for smooth positio transitions to work, as well at the player turning around freely. Still buggy however
2012-11-07 17:42:38 +01:00
|
|
|
|
2020-04-14 14:11:33 +02:00
|
|
|
collisionMoveResult moveresult, *moveresult_p = nullptr;
|
|
|
|
|
2012-10-28 16:07:11 +01:00
|
|
|
// Each frame, parent position is copied if the object is attached, otherwise it's calculated normally
|
|
|
|
// If the object gets detached this comes into effect automatically from the last known origin
|
Update attachments at the ending of the addToScene function for parents. And with this... *drum roll* Client-side attachments are at last functional and stick visibly.
Fix the last segmentation fault (apparently). So far attachments seem to be fully functional, although removing the parent causes children to go to origin 0,0,0 and possibly still cause such a fault (though this should already be addressed)
Fix a bug in falling code where entities get stuck
Also check if the parent has been removed server-side, and detach the child if so. Fixes children going to origin 0,0,0 when their parent is removed.
Unset all attachment properties when permanently detaching (on both the client and server). Also store less data we don't need
Create a separate function for detaching, and also update lua api documentation
When a child is detached, update its position from the server to clients. This WILL cause it to get positioned slightly differently client side, as the server attachment system only copies parent origin and knows not about mesh / bone transformation. This prevents different clients seeing the object detached in different spots which is most correct
Update the position of attached players to clients. An attached player will see himself move, but this is currently VERY ugly and laggy as it is done by the server (it probably must stay this way too)
Use a different approach for locally attached players. This allows for smooth positio transitions to work, as well at the player turning around freely. Still buggy however
2012-11-07 17:42:38 +01:00
|
|
|
if(isAttached())
|
2012-10-27 14:14:24 +02:00
|
|
|
{
|
Update attachments at the ending of the addToScene function for parents. And with this... *drum roll* Client-side attachments are at last functional and stick visibly.
Fix the last segmentation fault (apparently). So far attachments seem to be fully functional, although removing the parent causes children to go to origin 0,0,0 and possibly still cause such a fault (though this should already be addressed)
Fix a bug in falling code where entities get stuck
Also check if the parent has been removed server-side, and detach the child if so. Fixes children going to origin 0,0,0 when their parent is removed.
Unset all attachment properties when permanently detaching (on both the client and server). Also store less data we don't need
Create a separate function for detaching, and also update lua api documentation
When a child is detached, update its position from the server to clients. This WILL cause it to get positioned slightly differently client side, as the server attachment system only copies parent origin and knows not about mesh / bone transformation. This prevents different clients seeing the object detached in different spots which is most correct
Update the position of attached players to clients. An attached player will see himself move, but this is currently VERY ugly and laggy as it is done by the server (it probably must stay this way too)
Use a different approach for locally attached players. This allows for smooth positio transitions to work, as well at the player turning around freely. Still buggy however
2012-11-07 17:42:38 +01:00
|
|
|
v3f pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition();
|
2012-10-28 16:07:11 +01:00
|
|
|
m_base_position = pos;
|
2012-10-27 14:14:24 +02:00
|
|
|
m_velocity = v3f(0,0,0);
|
|
|
|
m_acceleration = v3f(0,0,0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(m_prop.physical){
|
2016-02-11 15:21:21 +01:00
|
|
|
aabb3f box = m_prop.collisionbox;
|
2012-10-27 14:14:24 +02:00
|
|
|
box.MinEdge *= BS;
|
|
|
|
box.MaxEdge *= BS;
|
|
|
|
f32 pos_max_d = BS*0.25; // Distance per iteration
|
|
|
|
v3f p_pos = m_base_position;
|
|
|
|
v3f p_velocity = m_velocity;
|
|
|
|
v3f p_acceleration = m_acceleration;
|
2017-01-09 20:39:22 +01:00
|
|
|
moveresult = collisionMoveSimple(m_env, m_env->getGameDef(),
|
2013-07-22 18:54:30 +02:00
|
|
|
pos_max_d, box, m_prop.stepheight, dtime,
|
2016-01-25 00:06:01 +01:00
|
|
|
&p_pos, &p_velocity, p_acceleration,
|
2013-06-14 14:04:46 +02:00
|
|
|
this, m_prop.collideWithObjects);
|
2020-04-14 14:11:33 +02:00
|
|
|
moveresult_p = &moveresult;
|
2013-07-22 18:54:30 +02:00
|
|
|
|
2012-10-27 14:14:24 +02:00
|
|
|
// Apply results
|
|
|
|
m_base_position = p_pos;
|
|
|
|
m_velocity = p_velocity;
|
|
|
|
m_acceleration = p_acceleration;
|
|
|
|
} else {
|
|
|
|
m_base_position += dtime * m_velocity + 0.5 * dtime
|
|
|
|
* dtime * m_acceleration;
|
|
|
|
m_velocity += dtime * m_acceleration;
|
|
|
|
}
|
2013-07-31 17:29:25 +02:00
|
|
|
|
2017-11-26 15:37:55 +01:00
|
|
|
if (m_prop.automatic_face_movement_dir &&
|
|
|
|
(fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001)) {
|
|
|
|
float target_yaw = atan2(m_velocity.Z, m_velocity.X) * 180 / M_PI
|
|
|
|
+ m_prop.automatic_face_movement_dir_offset;
|
2019-04-04 18:53:55 +02:00
|
|
|
float max_rotation_per_sec =
|
|
|
|
m_prop.automatic_face_movement_max_rotation_per_sec;
|
2019-04-10 21:33:54 +02:00
|
|
|
|
2019-04-04 18:53:55 +02:00
|
|
|
if (max_rotation_per_sec > 0) {
|
|
|
|
m_rotation.Y = wrapDegrees_0_360(m_rotation.Y);
|
2019-04-10 21:33:54 +02:00
|
|
|
wrappedApproachShortest(m_rotation.Y, target_yaw,
|
|
|
|
dtime * max_rotation_per_sec, 360.f);
|
|
|
|
} else {
|
|
|
|
// Negative values of max_rotation_per_sec mean disabled.
|
2019-04-04 18:53:55 +02:00
|
|
|
m_rotation.Y = target_yaw;
|
2019-04-10 21:33:54 +02:00
|
|
|
}
|
2013-07-31 17:29:25 +02:00
|
|
|
}
|
2011-11-21 13:36:21 +01:00
|
|
|
}
|
2011-11-21 10:15:15 +01:00
|
|
|
|
2020-04-14 14:11:33 +02:00
|
|
|
if(m_registered) {
|
|
|
|
m_env->getScriptIface()->luaentity_Step(m_id, dtime, moveresult_p);
|
2011-11-11 18:33:17 +01:00
|
|
|
}
|
2011-11-12 10:59:56 +01:00
|
|
|
|
2017-08-17 23:02:50 +02:00
|
|
|
if (!send_recommended)
|
2011-11-12 10:59:56 +01:00
|
|
|
return;
|
2012-10-28 16:07:11 +01:00
|
|
|
|
Update attachments at the ending of the addToScene function for parents. And with this... *drum roll* Client-side attachments are at last functional and stick visibly.
Fix the last segmentation fault (apparently). So far attachments seem to be fully functional, although removing the parent causes children to go to origin 0,0,0 and possibly still cause such a fault (though this should already be addressed)
Fix a bug in falling code where entities get stuck
Also check if the parent has been removed server-side, and detach the child if so. Fixes children going to origin 0,0,0 when their parent is removed.
Unset all attachment properties when permanently detaching (on both the client and server). Also store less data we don't need
Create a separate function for detaching, and also update lua api documentation
When a child is detached, update its position from the server to clients. This WILL cause it to get positioned slightly differently client side, as the server attachment system only copies parent origin and knows not about mesh / bone transformation. This prevents different clients seeing the object detached in different spots which is most correct
Update the position of attached players to clients. An attached player will see himself move, but this is currently VERY ugly and laggy as it is done by the server (it probably must stay this way too)
Use a different approach for locally attached players. This allows for smooth positio transitions to work, as well at the player turning around freely. Still buggy however
2012-11-07 17:42:38 +01:00
|
|
|
if(!isAttached())
|
2012-10-28 16:07:11 +01:00
|
|
|
{
|
|
|
|
// TODO: force send when acceleration changes enough?
|
|
|
|
float minchange = 0.2*BS;
|
|
|
|
if(m_last_sent_position_timer > 1.0){
|
|
|
|
minchange = 0.01*BS;
|
|
|
|
} else if(m_last_sent_position_timer > 0.2){
|
|
|
|
minchange = 0.05*BS;
|
|
|
|
}
|
|
|
|
float move_d = m_base_position.getDistanceFrom(m_last_sent_position);
|
|
|
|
move_d += m_last_sent_move_precision;
|
|
|
|
float vel_d = m_velocity.getDistanceFrom(m_last_sent_velocity);
|
2018-04-03 18:16:17 +02:00
|
|
|
if (move_d > minchange || vel_d > minchange ||
|
2018-11-28 09:38:50 +01:00
|
|
|
std::fabs(m_rotation.X - m_last_sent_rotation.X) > 1.0f ||
|
|
|
|
std::fabs(m_rotation.Y - m_last_sent_rotation.Y) > 1.0f ||
|
|
|
|
std::fabs(m_rotation.Z - m_last_sent_rotation.Z) > 1.0f) {
|
|
|
|
|
2012-10-28 16:07:11 +01:00
|
|
|
sendPosition(true, false);
|
|
|
|
}
|
2011-11-12 10:59:56 +01:00
|
|
|
}
|
2012-03-09 19:46:56 +01:00
|
|
|
|
2020-06-04 19:31:46 +02:00
|
|
|
sendOutdatedData();
|
2011-11-11 18:33:17 +01:00
|
|
|
}
|
|
|
|
|
2012-11-26 23:47:03 +01:00
|
|
|
std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version)
|
2011-11-11 18:33:17 +01:00
|
|
|
{
|
|
|
|
std::ostringstream os(std::ios::binary);
|
2012-11-04 22:54:50 +01:00
|
|
|
|
2018-12-24 00:22:27 +01:00
|
|
|
// PROTOCOL_VERSION >= 37
|
2017-03-19 08:44:29 +01:00
|
|
|
writeU8(os, 1); // version
|
|
|
|
os << serializeString(""); // name
|
|
|
|
writeU8(os, 0); // is_player
|
2019-02-11 00:03:26 +01:00
|
|
|
writeU16(os, getId()); //id
|
2019-01-03 17:04:26 +01:00
|
|
|
writeV3F32(os, m_base_position);
|
|
|
|
writeV3F32(os, m_rotation);
|
2019-02-11 00:03:26 +01:00
|
|
|
writeU16(os, m_hp);
|
2017-01-14 19:32:10 +01:00
|
|
|
|
2017-03-19 08:44:29 +01:00
|
|
|
std::ostringstream msg_os(std::ios::binary);
|
|
|
|
msg_os << serializeLongString(getPropertyPacket()); // message 1
|
2020-04-10 19:49:20 +02:00
|
|
|
msg_os << serializeLongString(generateUpdateArmorGroupsCommand()); // 2
|
|
|
|
msg_os << serializeLongString(generateUpdateAnimationCommand()); // 3
|
2020-06-04 19:31:46 +02:00
|
|
|
for (const auto &bone_pos : m_bone_position) {
|
|
|
|
msg_os << serializeLongString(generateUpdateBonePositionCommand(
|
|
|
|
bone_pos.first, bone_pos.second.X, bone_pos.second.Y)); // m_bone_position.size
|
2012-11-26 23:47:03 +01:00
|
|
|
}
|
2020-04-10 19:49:20 +02:00
|
|
|
msg_os << serializeLongString(generateUpdateAttachmentCommand()); // 4
|
2020-06-04 19:31:46 +02:00
|
|
|
|
2017-03-19 08:44:29 +01:00
|
|
|
int message_count = 4 + m_bone_position.size();
|
2020-06-04 19:31:46 +02:00
|
|
|
|
|
|
|
for (const auto &id : getAttachmentChildIds()) {
|
|
|
|
if (ServerActiveObject *obj = m_env->getActiveObject(id)) {
|
2017-03-19 08:44:29 +01:00
|
|
|
message_count++;
|
2020-06-04 19:31:46 +02:00
|
|
|
msg_os << serializeLongString(obj->generateUpdateInfantCommand(
|
|
|
|
id, protocol_version));
|
2017-03-19 08:44:29 +01:00
|
|
|
}
|
2012-11-04 22:54:50 +01:00
|
|
|
}
|
|
|
|
|
2020-04-10 19:49:20 +02:00
|
|
|
msg_os << serializeLongString(generateSetTextureModCommand());
|
2017-03-19 08:44:29 +01:00
|
|
|
message_count++;
|
|
|
|
|
|
|
|
writeU8(os, message_count);
|
2020-06-04 19:31:46 +02:00
|
|
|
std::string serialized = msg_os.str();
|
|
|
|
os.write(serialized.c_str(), serialized.size());
|
2017-03-19 08:44:29 +01:00
|
|
|
|
2011-11-12 09:39:44 +01:00
|
|
|
// return result
|
2011-11-11 18:33:17 +01:00
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2017-01-11 22:48:14 +01:00
|
|
|
void LuaEntitySAO::getStaticData(std::string *result) const
|
2011-11-11 18:33:17 +01:00
|
|
|
{
|
2015-10-14 08:39:30 +02:00
|
|
|
verbosestream<<FUNCTION_NAME<<std::endl;
|
2011-11-11 18:33:17 +01:00
|
|
|
std::ostringstream os(std::ios::binary);
|
2018-12-24 00:22:27 +01:00
|
|
|
// version must be 1 to keep backwards-compatibility. See version2
|
2012-03-04 20:08:03 +01:00
|
|
|
writeU8(os, 1);
|
2011-11-11 18:33:17 +01:00
|
|
|
// name
|
|
|
|
os<<serializeString(m_init_name);
|
|
|
|
// state
|
2011-11-11 19:50:09 +01:00
|
|
|
if(m_registered){
|
2013-08-11 04:09:45 +02:00
|
|
|
std::string state = m_env->getScriptIface()->
|
|
|
|
luaentity_GetStaticdata(m_id);
|
2011-11-11 19:50:09 +01:00
|
|
|
os<<serializeLongString(state);
|
|
|
|
} else {
|
|
|
|
os<<serializeLongString(m_init_state);
|
|
|
|
}
|
2019-02-11 00:03:26 +01:00
|
|
|
writeU16(os, m_hp);
|
2012-03-04 20:08:03 +01:00
|
|
|
writeV3F1000(os, m_velocity);
|
2018-12-24 00:22:27 +01:00
|
|
|
// yaw
|
|
|
|
writeF1000(os, m_rotation.Y);
|
|
|
|
|
|
|
|
// version2. Increase this variable for new values
|
|
|
|
writeU8(os, 1); // PROTOCOL_VERSION >= 37
|
|
|
|
|
|
|
|
writeF1000(os, m_rotation.X);
|
|
|
|
writeF1000(os, m_rotation.Z);
|
|
|
|
|
|
|
|
// <write new values>
|
2018-11-28 09:38:50 +01:00
|
|
|
|
2017-01-11 22:48:14 +01:00
|
|
|
*result = os.str();
|
2011-11-11 18:33:17 +01:00
|
|
|
}
|
|
|
|
|
2019-09-22 22:12:21 +02:00
|
|
|
u16 LuaEntitySAO::punch(v3f dir,
|
2012-03-04 20:08:03 +01:00
|
|
|
const ToolCapabilities *toolcap,
|
|
|
|
ServerActiveObject *puncher,
|
|
|
|
float time_from_last_punch)
|
2011-11-12 01:25:30 +01:00
|
|
|
{
|
2017-09-15 12:19:01 +02:00
|
|
|
if (!m_registered) {
|
2011-12-04 15:25:27 +01:00
|
|
|
// Delete unknown LuaEntities when punched
|
2017-09-15 12:19:01 +02:00
|
|
|
m_pending_removal = true;
|
2012-03-04 20:08:03 +01:00
|
|
|
return 0;
|
2011-12-04 15:25:27 +01:00
|
|
|
}
|
2012-10-27 14:14:24 +02:00
|
|
|
|
2019-06-09 11:25:41 +02:00
|
|
|
FATAL_ERROR_IF(!puncher, "Punch action called without SAO");
|
|
|
|
|
|
|
|
s32 old_hp = getHP();
|
2019-09-21 11:44:24 +02:00
|
|
|
ItemStack selected_item, hand_item;
|
|
|
|
ItemStack tool_item = puncher->getWieldedItem(&selected_item, &hand_item);
|
2012-03-04 20:08:03 +01:00
|
|
|
|
2012-03-05 00:30:55 +01:00
|
|
|
PunchDamageResult result = getPunchDamage(
|
|
|
|
m_armor_groups,
|
|
|
|
toolcap,
|
2019-09-21 11:44:24 +02:00
|
|
|
&tool_item,
|
2012-03-04 20:08:03 +01:00
|
|
|
time_from_last_punch);
|
2015-01-19 05:29:19 +01:00
|
|
|
|
2017-01-21 15:58:07 +01:00
|
|
|
bool damage_handled = m_env->getScriptIface()->luaentity_Punch(m_id, puncher,
|
|
|
|
time_from_last_punch, toolcap, dir, result.did_punch ? result.damage : 0);
|
2015-01-19 05:29:19 +01:00
|
|
|
|
2017-01-21 15:58:07 +01:00
|
|
|
if (!damage_handled) {
|
|
|
|
if (result.did_punch) {
|
2019-02-11 00:03:26 +01:00
|
|
|
setHP((s32)getHP() - result.damage,
|
2020-03-04 19:13:26 +01:00
|
|
|
PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
|
2013-05-05 01:44:55 +02:00
|
|
|
|
2017-01-21 15:58:07 +01:00
|
|
|
// create message and add to list
|
2020-04-10 19:49:20 +02:00
|
|
|
sendPunchCommand();
|
2017-01-21 15:58:07 +01:00
|
|
|
}
|
2012-03-04 20:08:03 +01:00
|
|
|
}
|
|
|
|
|
2018-04-30 18:43:49 +02:00
|
|
|
if (getHP() == 0 && !isGone()) {
|
|
|
|
clearParentAttachment();
|
|
|
|
clearChildAttachments();
|
2017-07-27 11:32:35 +02:00
|
|
|
m_env->getScriptIface()->luaentity_on_death(m_id, puncher);
|
2020-03-04 19:13:26 +01:00
|
|
|
m_pending_removal = true;
|
2017-07-27 11:32:35 +02:00
|
|
|
}
|
2015-03-11 20:52:47 +01:00
|
|
|
|
2019-06-09 11:25:41 +02:00
|
|
|
actionstream << puncher->getDescription() << " (id=" << puncher->getId() <<
|
|
|
|
", hp=" << puncher->getHP() << ") punched " <<
|
|
|
|
getDescription() << " (id=" << m_id << ", hp=" << m_hp <<
|
|
|
|
"), damage=" << (old_hp - (s32)getHP()) <<
|
|
|
|
(damage_handled ? " (handled by Lua)" : "") << std::endl;
|
|
|
|
|
2020-03-04 19:13:26 +01:00
|
|
|
// TODO: give Lua control over wear
|
2012-03-05 00:30:55 +01:00
|
|
|
return result.wear;
|
2011-11-12 01:25:30 +01:00
|
|
|
}
|
|
|
|
|
2011-11-12 16:37:14 +01:00
|
|
|
void LuaEntitySAO::rightClick(ServerActiveObject *clicker)
|
2011-11-12 01:25:30 +01:00
|
|
|
{
|
2015-03-11 20:52:47 +01:00
|
|
|
if (!m_registered)
|
2011-11-12 01:25:30 +01:00
|
|
|
return;
|
2018-04-30 18:43:49 +02:00
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
m_env->getScriptIface()->luaentity_Rightclick(m_id, clicker);
|
2011-11-12 01:25:30 +01:00
|
|
|
}
|
2011-11-11 18:33:17 +01:00
|
|
|
|
2016-10-30 16:12:09 +01:00
|
|
|
void LuaEntitySAO::setPos(const v3f &pos)
|
2011-11-12 10:59:56 +01:00
|
|
|
{
|
Update attachments at the ending of the addToScene function for parents. And with this... *drum roll* Client-side attachments are at last functional and stick visibly.
Fix the last segmentation fault (apparently). So far attachments seem to be fully functional, although removing the parent causes children to go to origin 0,0,0 and possibly still cause such a fault (though this should already be addressed)
Fix a bug in falling code where entities get stuck
Also check if the parent has been removed server-side, and detach the child if so. Fixes children going to origin 0,0,0 when their parent is removed.
Unset all attachment properties when permanently detaching (on both the client and server). Also store less data we don't need
Create a separate function for detaching, and also update lua api documentation
When a child is detached, update its position from the server to clients. This WILL cause it to get positioned slightly differently client side, as the server attachment system only copies parent origin and knows not about mesh / bone transformation. This prevents different clients seeing the object detached in different spots which is most correct
Update the position of attached players to clients. An attached player will see himself move, but this is currently VERY ugly and laggy as it is done by the server (it probably must stay this way too)
Use a different approach for locally attached players. This allows for smooth positio transitions to work, as well at the player turning around freely. Still buggy however
2012-11-07 17:42:38 +01:00
|
|
|
if(isAttached())
|
2012-10-27 14:14:24 +02:00
|
|
|
return;
|
2011-11-12 10:59:56 +01:00
|
|
|
m_base_position = pos;
|
2011-11-12 12:14:44 +01:00
|
|
|
sendPosition(false, true);
|
2011-11-12 10:59:56 +01:00
|
|
|
}
|
|
|
|
|
2011-11-12 12:59:56 +01:00
|
|
|
void LuaEntitySAO::moveTo(v3f pos, bool continuous)
|
2011-11-12 10:59:56 +01:00
|
|
|
{
|
Update attachments at the ending of the addToScene function for parents. And with this... *drum roll* Client-side attachments are at last functional and stick visibly.
Fix the last segmentation fault (apparently). So far attachments seem to be fully functional, although removing the parent causes children to go to origin 0,0,0 and possibly still cause such a fault (though this should already be addressed)
Fix a bug in falling code where entities get stuck
Also check if the parent has been removed server-side, and detach the child if so. Fixes children going to origin 0,0,0 when their parent is removed.
Unset all attachment properties when permanently detaching (on both the client and server). Also store less data we don't need
Create a separate function for detaching, and also update lua api documentation
When a child is detached, update its position from the server to clients. This WILL cause it to get positioned slightly differently client side, as the server attachment system only copies parent origin and knows not about mesh / bone transformation. This prevents different clients seeing the object detached in different spots which is most correct
Update the position of attached players to clients. An attached player will see himself move, but this is currently VERY ugly and laggy as it is done by the server (it probably must stay this way too)
Use a different approach for locally attached players. This allows for smooth positio transitions to work, as well at the player turning around freely. Still buggy however
2012-11-07 17:42:38 +01:00
|
|
|
if(isAttached())
|
2012-10-27 14:14:24 +02:00
|
|
|
return;
|
2011-11-12 10:59:56 +01:00
|
|
|
m_base_position = pos;
|
2011-11-12 12:59:56 +01:00
|
|
|
if(!continuous)
|
|
|
|
sendPosition(true, true);
|
2011-11-12 10:59:56 +01:00
|
|
|
}
|
|
|
|
|
2011-11-12 14:14:24 +01:00
|
|
|
float LuaEntitySAO::getMinimumSavedMovement()
|
|
|
|
{
|
|
|
|
return 0.1 * BS;
|
|
|
|
}
|
|
|
|
|
2012-03-04 20:08:03 +01:00
|
|
|
std::string LuaEntitySAO::getDescription()
|
|
|
|
{
|
2020-04-08 20:13:23 +02:00
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "LuaEntitySAO \"" << m_init_name << "\" ";
|
|
|
|
auto pos = floatToInt(m_base_position, BS);
|
|
|
|
oss << "at " << PP(pos);
|
|
|
|
return oss.str();
|
2012-03-04 20:08:03 +01:00
|
|
|
}
|
|
|
|
|
2019-02-11 00:03:26 +01:00
|
|
|
void LuaEntitySAO::setHP(s32 hp, const PlayerHPChangeReason &reason)
|
2012-03-29 15:10:11 +02:00
|
|
|
{
|
2019-02-11 00:03:26 +01:00
|
|
|
m_hp = rangelim(hp, 0, U16_MAX);
|
2012-03-29 15:10:11 +02:00
|
|
|
}
|
|
|
|
|
2019-02-11 00:03:26 +01:00
|
|
|
u16 LuaEntitySAO::getHP() const
|
2012-03-29 15:10:11 +02:00
|
|
|
{
|
|
|
|
return m_hp;
|
|
|
|
}
|
|
|
|
|
2011-11-21 10:15:15 +01:00
|
|
|
void LuaEntitySAO::setVelocity(v3f velocity)
|
|
|
|
{
|
|
|
|
m_velocity = velocity;
|
|
|
|
}
|
|
|
|
|
2012-01-02 15:19:52 +01:00
|
|
|
v3f LuaEntitySAO::getVelocity()
|
|
|
|
{
|
|
|
|
return m_velocity;
|
|
|
|
}
|
|
|
|
|
2011-11-21 10:15:15 +01:00
|
|
|
void LuaEntitySAO::setAcceleration(v3f acceleration)
|
|
|
|
{
|
|
|
|
m_acceleration = acceleration;
|
|
|
|
}
|
|
|
|
|
2011-11-27 03:31:05 +01:00
|
|
|
v3f LuaEntitySAO::getAcceleration()
|
|
|
|
{
|
|
|
|
return m_acceleration;
|
|
|
|
}
|
|
|
|
|
2011-11-26 11:35:30 +01:00
|
|
|
void LuaEntitySAO::setTextureMod(const std::string &mod)
|
|
|
|
{
|
2017-01-14 19:32:10 +01:00
|
|
|
m_current_texture_modifier = mod;
|
2011-11-26 11:35:30 +01:00
|
|
|
// create message and add to list
|
2020-04-10 19:49:20 +02:00
|
|
|
m_messages_out.emplace(getId(), true, generateSetTextureModCommand());
|
2011-11-27 03:31:05 +01:00
|
|
|
}
|
|
|
|
|
2017-01-14 19:32:10 +01:00
|
|
|
std::string LuaEntitySAO::getTextureMod() const
|
|
|
|
{
|
|
|
|
return m_current_texture_modifier;
|
|
|
|
}
|
|
|
|
|
2020-04-10 19:49:20 +02:00
|
|
|
|
|
|
|
std::string LuaEntitySAO::generateSetTextureModCommand() const
|
|
|
|
{
|
|
|
|
std::ostringstream os(std::ios::binary);
|
|
|
|
// command
|
|
|
|
writeU8(os, AO_CMD_SET_TEXTURE_MOD);
|
|
|
|
// parameters
|
|
|
|
os << serializeString(m_current_texture_modifier);
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string LuaEntitySAO::generateSetSpriteCommand(v2s16 p, u16 num_frames,
|
|
|
|
f32 framelength, bool select_horiz_by_yawpitch)
|
|
|
|
{
|
|
|
|
std::ostringstream os(std::ios::binary);
|
|
|
|
// command
|
|
|
|
writeU8(os, AO_CMD_SET_SPRITE);
|
|
|
|
// parameters
|
|
|
|
writeV2S16(os, p);
|
|
|
|
writeU16(os, num_frames);
|
|
|
|
writeF32(os, framelength);
|
|
|
|
writeU8(os, select_horiz_by_yawpitch);
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2011-11-27 03:31:05 +01:00
|
|
|
void LuaEntitySAO::setSprite(v2s16 p, int num_frames, float framelength,
|
|
|
|
bool select_horiz_by_yawpitch)
|
|
|
|
{
|
2020-04-10 19:49:20 +02:00
|
|
|
std::string str = generateSetSpriteCommand(
|
2012-03-29 16:46:21 +02:00
|
|
|
p,
|
|
|
|
num_frames,
|
|
|
|
framelength,
|
|
|
|
select_horiz_by_yawpitch
|
|
|
|
);
|
2011-11-27 03:31:05 +01:00
|
|
|
// create message and add to list
|
2020-04-10 19:49:20 +02:00
|
|
|
m_messages_out.emplace(getId(), true, str);
|
2011-11-26 11:35:30 +01:00
|
|
|
}
|
|
|
|
|
2012-01-02 12:12:56 +01:00
|
|
|
std::string LuaEntitySAO::getName()
|
|
|
|
{
|
|
|
|
return m_init_name;
|
|
|
|
}
|
|
|
|
|
2012-03-29 16:46:21 +02:00
|
|
|
std::string LuaEntitySAO::getPropertyPacket()
|
|
|
|
{
|
2020-04-10 19:49:20 +02:00
|
|
|
return generateSetPropertiesCommand(m_prop);
|
2012-03-29 16:46:21 +02:00
|
|
|
}
|
|
|
|
|
2011-11-12 12:14:44 +01:00
|
|
|
void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
|
2011-11-12 10:59:56 +01:00
|
|
|
{
|
2012-10-28 16:07:11 +01:00
|
|
|
// If the object is attached client-side, don't waste bandwidth sending its position to clients
|
Update attachments at the ending of the addToScene function for parents. And with this... *drum roll* Client-side attachments are at last functional and stick visibly.
Fix the last segmentation fault (apparently). So far attachments seem to be fully functional, although removing the parent causes children to go to origin 0,0,0 and possibly still cause such a fault (though this should already be addressed)
Fix a bug in falling code where entities get stuck
Also check if the parent has been removed server-side, and detach the child if so. Fixes children going to origin 0,0,0 when their parent is removed.
Unset all attachment properties when permanently detaching (on both the client and server). Also store less data we don't need
Create a separate function for detaching, and also update lua api documentation
When a child is detached, update its position from the server to clients. This WILL cause it to get positioned slightly differently client side, as the server attachment system only copies parent origin and knows not about mesh / bone transformation. This prevents different clients seeing the object detached in different spots which is most correct
Update the position of attached players to clients. An attached player will see himself move, but this is currently VERY ugly and laggy as it is done by the server (it probably must stay this way too)
Use a different approach for locally attached players. This allows for smooth positio transitions to work, as well at the player turning around freely. Still buggy however
2012-11-07 17:42:38 +01:00
|
|
|
if(isAttached())
|
2012-10-27 14:14:24 +02:00
|
|
|
return;
|
2015-01-19 05:29:19 +01:00
|
|
|
|
2011-11-12 12:59:56 +01:00
|
|
|
m_last_sent_move_precision = m_base_position.getDistanceFrom(
|
|
|
|
m_last_sent_position);
|
|
|
|
m_last_sent_position_timer = 0;
|
2011-11-12 10:59:56 +01:00
|
|
|
m_last_sent_position = m_base_position;
|
2011-11-21 13:36:21 +01:00
|
|
|
m_last_sent_velocity = m_velocity;
|
2011-11-21 10:15:15 +01:00
|
|
|
//m_last_sent_acceleration = m_acceleration;
|
2018-11-28 09:38:50 +01:00
|
|
|
m_last_sent_rotation = m_rotation;
|
2011-11-12 12:59:56 +01:00
|
|
|
|
|
|
|
float update_interval = m_env->getSendRecommendedInterval();
|
2011-11-12 10:59:56 +01:00
|
|
|
|
2020-04-10 19:49:20 +02:00
|
|
|
std::string str = generateUpdatePositionCommand(
|
2012-03-29 16:46:21 +02:00
|
|
|
m_base_position,
|
|
|
|
m_velocity,
|
|
|
|
m_acceleration,
|
2018-11-28 09:38:50 +01:00
|
|
|
m_rotation,
|
2012-03-29 16:46:21 +02:00
|
|
|
do_interpolate,
|
|
|
|
is_movement_end,
|
|
|
|
update_interval
|
|
|
|
);
|
2011-11-12 10:59:56 +01:00
|
|
|
// create message and add to list
|
2020-04-10 19:49:20 +02:00
|
|
|
m_messages_out.emplace(getId(), false, str);
|
2011-11-12 10:59:56 +01:00
|
|
|
}
|
|
|
|
|
2017-01-09 20:39:45 +01:00
|
|
|
bool LuaEntitySAO::getCollisionBox(aabb3f *toset) const
|
|
|
|
{
|
2013-01-12 18:59:19 +01:00
|
|
|
if (m_prop.physical)
|
|
|
|
{
|
|
|
|
//update collision box
|
|
|
|
toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
|
|
|
|
toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
|
|
|
|
|
|
|
|
toset->MinEdge += m_base_position;
|
|
|
|
toset->MaxEdge += m_base_position;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-23 21:11:20 +02:00
|
|
|
bool LuaEntitySAO::getSelectionBox(aabb3f *toset) const
|
|
|
|
{
|
2017-08-24 10:01:16 +02:00
|
|
|
if (!m_prop.is_visible || !m_prop.pointable) {
|
2016-07-23 21:11:20 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-24 10:01:16 +02:00
|
|
|
toset->MinEdge = m_prop.selectionbox.MinEdge * BS;
|
|
|
|
toset->MaxEdge = m_prop.selectionbox.MaxEdge * BS;
|
2016-07-23 21:11:20 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-09 20:39:45 +01:00
|
|
|
bool LuaEntitySAO::collideWithObjects() const
|
|
|
|
{
|
2013-06-14 14:04:46 +02:00
|
|
|
return m_prop.collideWithObjects;
|
|
|
|
}
|