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
- set_wielded_item(item): replaces the wielded item, returns true if successful
- set_armor_groups({group1=rating, group2=rating, ...})
- set_animations({x=1,y=1}, frame_speed=15, frame_blend=0)
+- set_attachment(parent, "", {x=0,y=0,z=0}, {x=0,y=0,z=0})
+- set_detachment()
- set_bone_posrot("", {x=0,y=0,z=0}, {x=0,y=0,z=0})
- set_properties(object property table)
LuaEntitySAO-only: (no-op for other objects)
void removeFromScene(bool permanent)
{
- // bool permanent should be true when removing the object permanently and false when it's only refreshed (and comes back in a few frames)
-
- // If this object is being permanently removed, delete it from the attachments list
- if(permanent)
+ if(permanent) // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
{
+ // Detach this object's children
for(std::vector<core::vector2d<int> >::iterator ii = attachment_list.begin(); ii != attachment_list.end(); ii++)
{
- if(ii->X == this->getId()) // This is the ID of our object
+ if(ii->Y == this->getId()) // Is a child of our object
{
- attachment_list.erase(ii);
- break;
+ ii->Y = 0;
+ ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
+ if(obj)
+ obj->updateParent();
}
}
- }
-
- // If this object is being removed, either permanently or just to refresh it, then all
- // objects attached to it must be unparented else Irrlicht causes a segmentation fault.
- for(std::vector<core::vector2d<int> >::iterator ii = attachment_list.begin(); ii != attachment_list.end(); ii++)
- {
- if(ii->Y == this->getId()) // This is a child of our parent
+ // Delete this object from the attachments list
+ for(std::vector<core::vector2d<int> >::iterator ii = attachment_list.begin(); ii != attachment_list.end(); ii++)
{
- ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
- if(obj)
+ if(ii->X == this->getId()) // Is our object
{
- if(permanent)
- {
- // The parent is being permanently removed, so the child stays detached
- ii->Y = 0;
- obj->updateParent();
- }
- else
- {
- // The parent is being refreshed, detach our child enough to avoid bad memory reads
- // This only stays into effect for a few frames, as addToScene will parent its children back
- scene::IMeshSceneNode *m_child_meshnode = obj->getMeshSceneNode();
- scene::IAnimatedMeshSceneNode *m_child_animated_meshnode = obj->getAnimatedMeshSceneNode();
- scene::IBillboardSceneNode *m_child_spritenode = obj->getSpriteSceneNode();
- if(m_child_meshnode)
- m_child_meshnode->setParent(m_smgr->getRootSceneNode());
- if(m_child_animated_meshnode)
- m_child_animated_meshnode->setParent(m_smgr->getRootSceneNode());
- if(m_child_spritenode)
- m_child_spritenode->setParent(m_smgr->getRootSceneNode());
- }
+ attachment_list.erase(ii);
+ break;
}
}
}
m_smgr = smgr;
m_irr = irr;
- // If this object has attachments and is being re-added after having been refreshed, parent its children back.
- // The parent ID for this child hasn't been changed in attachment_list, so just update its attachments.
- for(std::vector<core::vector2d<int> >::iterator ii = attachment_list.begin(); ii != attachment_list.end(); ii++)
- {
- if(ii->Y == this->getId()) // This is a child of our parent
- {
- ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
- if(obj)
- obj->updateParent();
- }
- }
-
if(m_meshnode != NULL || m_animated_meshnode != NULL || m_spritenode != NULL)
return;
if(m_visuals_expired && m_smgr && m_irr){
m_visuals_expired = false;
+
+ // Attachments, part 1: All attached objects must be unparented first, or Irrlicht causes a segmentation fault
+ for(std::vector<core::vector2d<int> >::iterator ii = attachment_list.begin(); ii != attachment_list.end(); ii++)
+ {
+ if(ii->Y == this->getId()) // This is a child of our parent
+ {
+ ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
+ if(obj)
+ {
+ scene::IMeshSceneNode *m_child_meshnode = obj->getMeshSceneNode();
+ scene::IAnimatedMeshSceneNode *m_child_animated_meshnode = obj->getAnimatedMeshSceneNode();
+ scene::IBillboardSceneNode *m_child_spritenode = obj->getSpriteSceneNode();
+ if(m_child_meshnode)
+ m_child_meshnode->setParent(m_smgr->getRootSceneNode());
+ if(m_child_animated_meshnode)
+ m_child_animated_meshnode->setParent(m_smgr->getRootSceneNode());
+ if(m_child_spritenode)
+ m_child_spritenode->setParent(m_smgr->getRootSceneNode());
+ }
+ }
+ }
+
removeFromScene(false);
addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
updateAnimations();
updateBonePosRot();
updateAttachments();
- return;
- }
+ // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
+ for(std::vector<core::vector2d<int> >::iterator ii = attachment_list.begin(); ii != attachment_list.end(); ii++)
+ {
+ if(ii->Y == this->getId()) // This is a child of our parent
+ {
+ ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
+ if(obj)
+ obj->updateParent();
+ }
+ }
+ }
if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
{
// Set these for later
m_position = m_spritenode->getAbsolutePosition();
m_velocity = v3f(0,0,0);
m_acceleration = v3f(0,0,0);
+
+ if(m_is_local_player) // Update local player attachment position
+ {
+ LocalPlayer *player = m_env->getLocalPlayer();
+ player->overridePosition = getParent()->getPosition();
+ }
}
else
{
m_spritenode->setRotation(old_rotation);
m_spritenode->updateAbsolutePosition();
}
+ if(m_is_local_player)
+ {
+ LocalPlayer *player = m_env->getLocalPlayer();
+ player->isAttached = false;
+ }
}
else // Attach
{
}
}
}
+ if(m_is_local_player)
+ {
+ LocalPlayer *player = m_env->getLocalPlayer();
+ player->isAttached = true;
+ player->overridePosition = m_attachment_position;
+ }
}
}
}
else if(cmd == GENERIC_CMD_UPDATE_POSITION)
{
- // Not sent by the server if the object is an attachment
+ // Not sent by the server if this object is an attachment.
+ // We might however get here if the server notices the object being detached before the client.
m_position = readV3F1000(is);
m_velocity = readV3F1000(is);
m_acceleration = readV3F1000(is);
bool is_end_position = readU8(is);
float update_interval = readF1000(is);
- if(getParent() != NULL) // Just in case
- return;
-
// Place us a bit higher if we're physical, to not sink into
// the ground due to sucky collision detection...
if(m_prop.physical)
m_position += v3f(0,0.002,0);
-
+
+ if(getParent() != NULL) // Just in case
+ return;
+
if(do_interpolate){
if(!m_prop.physical)
pos_translator.update(m_position, is_end_position, update_interval);
if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
{
- // TODO: We shouldn't be sending this when the object is attached, but we can't check m_parent here
setBasePosition(pos_f);
m_last_sent_position = pos_f;
// Create entity from name
lua_State *L = m_env->getLua();
m_registered = scriptapi_luaentity_add(L, m_id, m_init_name.c_str());
- m_parent = NULL;
if(m_registered){
// Get properties
return sao;
}
+bool LuaEntitySAO::isAttached()
+{
+ if(!m_attachment_parent_id)
+ return false;
+ // Check if the parent still exists
+ ServerActiveObject *obj = m_env->getActiveObject(m_attachment_parent_id);
+ if(obj)
+ return true;
+ return false;
+}
+
void LuaEntitySAO::step(float dtime, bool send_recommended)
{
if(!m_properties_sent)
m_messages_out.push_back(aom);
}
+ // If attached, check that our parent is still there. If it isn't, detach.
+ if(m_attachment_parent_id && !isAttached())
+ {
+ m_attachment_parent_id = 0;
+ m_attachment_bone = "";
+ m_attachment_position = v3f(0,0,0);
+ m_attachment_rotation = v3f(0,0,0);
+ sendPosition(false, true);
+ }
+
m_last_sent_position_timer += dtime;
-
+
// 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
- if(m_parent != NULL)
+ if(isAttached())
{
- v3f pos = m_parent->getBasePosition();
+ v3f pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition();
m_base_position = pos;
m_velocity = v3f(0,0,0);
m_acceleration = v3f(0,0,0);
if(send_recommended == false)
return;
- if(m_parent != NULL)
+ if(!isAttached())
{
// TODO: force send when acceleration changes enough?
float minchange = 0.2*BS;
}
// It's best that attachments cannot be punched
- if(m_parent != NULL)
+ if(isAttached())
return 0;
ItemStack *punchitem = NULL;
void LuaEntitySAO::setPos(v3f pos)
{
- if(m_parent != NULL)
+ if(isAttached())
return;
m_base_position = pos;
sendPosition(false, true);
void LuaEntitySAO::moveTo(v3f pos, bool continuous)
{
- if(m_parent != NULL)
+ if(isAttached())
return;
m_base_position = pos;
if(!continuous)
m_animations_bone_sent = false;
}
-void LuaEntitySAO::setAttachment(ServerActiveObject *parent, std::string bone, v3f position, v3f rotation)
+void LuaEntitySAO::setAttachment(int parent_id, std::string bone, v3f position, v3f rotation)
{
// Attachments need to be handled on both the server and client.
// If we just attach on the server, we can only copy the position of the parent. Attachments
// This breaks some things so we also give the server the most accurate representation
// even if players only see the client changes.
- // Server attachment:
- m_parent = parent;
-
- // Client attachment:
- m_attachment_parent_id = parent->getId();
+ m_attachment_parent_id = parent_id;
m_attachment_bone = bone;
m_attachment_position = position;
m_attachment_rotation = rotation;
void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
{
// If the object is attached client-side, don't waste bandwidth sending its position to clients
- if(m_parent != NULL)
+ if(isAttached())
return;
m_last_sent_move_precision = m_base_position.getDistanceFrom(
m_animations_bone_sent(false),
m_attachment_sent(false),
// public
- m_teleported(false),
+ m_moved(false),
m_inventory_not_sent(false),
m_hp_not_sent(false),
m_wielded_item_not_sent(false)
{
ServerActiveObject::addedToEnvironment(dtime_s);
ServerActiveObject::setBasePosition(m_player->getPosition());
- m_parent = NULL;
m_player->setPlayerSAO(this);
m_player->peer_id = m_peer_id;
m_last_good_position = m_player->getPosition();
return "";
}
+bool PlayerSAO::isAttached()
+{
+ if(!m_attachment_parent_id)
+ return false;
+ // Check if the parent still exists
+ ServerActiveObject *obj = m_env->getActiveObject(m_attachment_parent_id);
+ if(obj)
+ return true;
+ return false;
+}
+
void PlayerSAO::step(float dtime, bool send_recommended)
{
if(!m_properties_sent)
m_messages_out.push_back(aom);
}
+ // If attached, check that our parent is still there. If it isn't, detach.
+ if(m_attachment_parent_id && !isAttached())
+ {
+ m_attachment_parent_id = 0;
+ m_attachment_bone = "";
+ m_attachment_position = v3f(0,0,0);
+ m_attachment_rotation = v3f(0,0,0);
+ m_player->setPosition(m_last_good_position);
+ m_moved = true;
+ }
+
m_time_from_last_punch += dtime;
m_nocheat_dig_time += dtime;
// 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
- if(m_parent != NULL)
+ if(isAttached())
{
- v3f pos = m_parent->getBasePosition();
+ v3f pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition();
m_last_good_position = pos;
m_last_good_position_age = 0;
m_player->setPosition(pos);
<<" moved too fast; resetting position"
<<std::endl;
m_player->setPosition(m_last_good_position);
- m_teleported = true;
+ m_moved = true;
}
m_last_good_position_age = 0;
}
return;
// If the object is attached client-side, don't waste bandwidth sending its position to clients
- if(m_position_not_sent && m_parent == NULL)
+ if(m_position_not_sent && !isAttached())
{
m_position_not_sent = false;
float update_interval = m_env->getSendRecommendedInterval();
v3f pos;
- if(m_parent != NULL) // Just in case we ever do send attachment position too
- pos = m_parent->getBasePosition();
+ if(isAttached()) // Just in case we ever do send attachment position too
+ pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition();
else
pos = m_player->getPosition() + v3f(0,BS*1,0);
std::string str = gob_cmd_update_position(
void PlayerSAO::setPos(v3f pos)
{
- if(m_parent != NULL)
+ if(isAttached())
return;
m_player->setPosition(pos);
// Movement caused by this command is always valid
m_last_good_position = pos;
m_last_good_position_age = 0;
// Force position change on client
- m_teleported = true;
+ m_moved = true;
}
void PlayerSAO::moveTo(v3f pos, bool continuous)
{
- if(m_parent != NULL)
+ if(isAttached())
return;
m_player->setPosition(pos);
// Movement caused by this command is always valid
m_last_good_position = pos;
m_last_good_position_age = 0;
// Force position change on client
- m_teleported = true;
+ m_moved = true;
}
int PlayerSAO::punch(v3f dir,
float time_from_last_punch)
{
// It's best that attachments cannot be punched
- if(m_parent != NULL)
+ if(isAttached())
return 0;
if(!toolcap)
m_animations_bone_sent = false;
}
-void PlayerSAO::setAttachment(ServerActiveObject *parent, std::string bone, v3f position, v3f rotation)
+void PlayerSAO::setAttachment(int parent_id, std::string bone, v3f position, v3f rotation)
{
// Attachments need to be handled on both the server and client.
// If we just attach on the server, we can only copy the position of the parent. Attachments
// This breaks some things so we also give the server the most accurate representation
// even if players only see the client changes.
- // Server attachment:
- m_parent = parent;
-
- // Client attachment:
- m_attachment_parent_id = parent->getId();
+ m_attachment_parent_id = parent_id;
m_attachment_bone = bone;
m_attachment_position = position;
m_attachment_rotation = rotation;
virtual void addedToEnvironment(u32 dtime_s);
static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
const std::string &data);
+ bool isAttached();
void step(float dtime, bool send_recommended);
std::string getClientInitializationData();
std::string getStaticData();
void setArmorGroups(const ItemGroupList &armor_groups);
void setAnimations(v2f frames, float frame_speed, float frame_blend);
void setBonePosRot(std::string bone, v3f position, v3f rotation);
- void setAttachment(ServerActiveObject *parent, std::string bone, v3f position, v3f rotation);
+ void setAttachment(int parent_id, std::string bone, v3f position, v3f rotation);
ObjectProperties* accessObjectProperties();
void notifyObjectPropertiesModified();
/* LuaEntitySAO-specific */
std::map<std::string, core::vector2d<v3f> > m_animation_bone;
bool m_animations_bone_sent;
-
- ServerActiveObject *m_parent;
+
int m_attachment_parent_id;
std::string m_attachment_bone;
v3f m_attachment_position;
bool unlimitedTransferDistance() const;
std::string getClientInitializationData();
std::string getStaticData();
+ bool isAttached();
void step(float dtime, bool send_recommended);
void setBasePosition(const v3f &position);
void setPos(v3f pos);
void setArmorGroups(const ItemGroupList &armor_groups);
void setAnimations(v2f frames, float frame_speed, float frame_blend);
void setBonePosRot(std::string bone, v3f position, v3f rotation);
- void setAttachment(ServerActiveObject *parent, std::string bone, v3f position, v3f rotation);
+ void setAttachment(int parent_id, std::string bone, v3f position, v3f rotation);
ObjectProperties* accessObjectProperties();
void notifyObjectPropertiesModified();
std::map<std::string, core::vector2d<v3f> > m_animation_bone; // stores position and rotation for each bone name
bool m_animations_bone_sent;
-
- ServerActiveObject *m_parent;
+
int m_attachment_parent_id;
std::string m_attachment_bone;
v3f m_attachment_position;
public:
// Some flags used by Server
- bool m_teleported;
+ bool m_moved;
bool m_inventory_not_sent;
bool m_hp_not_sent;
bool m_wielded_item_not_sent;
Draw backgrounds
*/
for(u32 i=0; i<m_backgrounds.size(); i++)
- {
const ImageDrawSpec &spec = m_backgrounds[i];
video::ITexture *texture =
m_gamedef->tsrc()->getTextureRaw(spec.name);
core::rect<s32>(core::position2d<s32>(0,0),
core::dimension2di(texture->getOriginalSize())),
NULL/*&AbsoluteClippingRect*/, colors, true);
- }
/*
Draw images
void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d,
core::list<CollisionInfo> *collision_info)
{
+ // Copy parent position if local player is attached
+ if(isAttached)
+ {
+ setPosition(overridePosition);
+ return;
+ }
+
INodeDefManager *nodemgr = m_gamedef->ndef();
v3f position = getPosition();
{
return true;
}
+
+ bool isAttached;
+
+ v3f overridePosition;
void move(f32 dtime, Map &map, f32 pos_max_d,
core::list<CollisionInfo> *collision_info);
ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0;
// Do it
-
v2f frames = v2f(1, 1);
if(!lua_isnil(L, 2))
frames = read_v2f(L, 2);
ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0;
// Do it
-
std::string bone = "";
if(!lua_isnil(L, 2))
bone = lua_tostring(L, 2);
ServerActiveObject *parent = getobject(parent_ref);
if(co == NULL) return 0;
if(parent == NULL) return 0;
+ // Do it
std::string bone = "";
if(!lua_isnil(L, 3))
bone = lua_tostring(L, 3);
v3f rotation = v3f(0, 0, 0);
if(!lua_isnil(L, 5))
rotation = read_v3f(L, 5);
- // Do it
+ co->setAttachment(parent->getId(), bone, position, rotation);
+ return 0;
+ }
- co->setAttachment(parent, bone, position, rotation);
+ // set_detachment(self)
+ static int l_set_detachment(lua_State *L)
+ {
+ ObjectRef *ref = checkobject(L, 1);
+ ServerActiveObject *co = getobject(ref);
+ if(co == NULL) return 0;
+ // Do it
+ co->setAttachment(0, "", v3f(0,0,0), v3f(0,0,0));
return 0;
}
method(ObjectRef, set_animations),
method(ObjectRef, set_bone_posrot),
method(ObjectRef, set_attachment),
+ method(ObjectRef, set_detachment),
method(ObjectRef, set_properties),
// LuaEntitySAO-only
method(ObjectRef, setvelocity),
/*
Send player inventories and HPs if necessary
*/
- if(playersao->m_teleported){
+ if(playersao->m_moved){
SendMovePlayer(client->peer_id);
- playersao->m_teleported = false;
+ playersao->m_moved = false;
}
if(playersao->m_inventory_not_sent){
UpdateCrafting(client->peer_id);
{}
virtual void setBonePosRot(std::string bone, v3f position, v3f rotation)
{}
- virtual void setAttachment(ServerActiveObject *parent, std::string bone, v3f position, v3f rotation)
+ virtual void setAttachment(int parent_id, std::string bone, v3f position, v3f rotation)
{}
virtual ObjectProperties* accessObjectProperties()
{ return NULL; }