Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add support for limiting rotation of automatic face movement dir entitys
  • Loading branch information
Sapier authored and Sapier committed Dec 19, 2015
1 parent 0663220 commit 70ea5d5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/content_cao.cpp
Expand Up @@ -1260,8 +1260,18 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
(fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
{
m_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
float optimal_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
+ m_prop.automatic_face_movement_dir_offset;
float max_rotation_delta =
dtime * m_prop.automatic_face_movement_max_rotation_per_sec;

if ((m_prop.automatic_face_movement_max_rotation_per_sec > 0) &&
(fabs(m_yaw - optimal_yaw) > max_rotation_delta)) {

m_yaw = optimal_yaw < m_yaw ? m_yaw - max_rotation_delta : m_yaw + max_rotation_delta;
} else {
m_yaw = optimal_yaw;
}
updateNodePos();
}
}
Expand Down
16 changes: 14 additions & 2 deletions src/content_sao.cpp
Expand Up @@ -283,8 +283,20 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
}

if((m_prop.automatic_face_movement_dir) &&
(fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001)){
m_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI + m_prop.automatic_face_movement_dir_offset;
(fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
{
float optimal_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
+ m_prop.automatic_face_movement_dir_offset;
float max_rotation_delta =
dtime * m_prop.automatic_face_movement_max_rotation_per_sec;

if ((m_prop.automatic_face_movement_max_rotation_per_sec > 0) &&
(fabs(m_yaw - optimal_yaw) > max_rotation_delta)) {

m_yaw = optimal_yaw < m_yaw ? m_yaw - max_rotation_delta : m_yaw + max_rotation_delta;
} else {
m_yaw = optimal_yaw;
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/object_properties.cpp
Expand Up @@ -45,7 +45,8 @@ ObjectProperties::ObjectProperties():
automatic_face_movement_dir_offset(0.0),
backface_culling(true),
nametag(""),
nametag_color(255, 255, 255, 255)
nametag_color(255, 255, 255, 255),
automatic_face_movement_max_rotation_per_sec(-1)
{
textures.push_back("unknown_object.png");
colors.push_back(video::SColor(255,255,255,255));
Expand Down Expand Up @@ -116,6 +117,8 @@ void ObjectProperties::serialize(std::ostream &os) const
writeU8(os, backface_culling);
os << serializeString(nametag);
writeARGB8(os, nametag_color);
writeF1000(os, automatic_face_movement_max_rotation_per_sec);

// Add stuff only at the bottom.
// Never remove anything, because we don't want new versions of this
}
Expand Down Expand Up @@ -155,6 +158,7 @@ void ObjectProperties::deSerialize(std::istream &is)
backface_culling = readU8(is);
nametag = deSerializeString(is);
nametag_color = readARGB8(is);
automatic_face_movement_max_rotation_per_sec = readF1000(is);
}catch(SerializationError &e){}
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/object_properties.h
Expand Up @@ -50,7 +50,7 @@ struct ObjectProperties
bool backface_culling;
std::string nametag;
video::SColor nametag_color;

f32 automatic_face_movement_max_rotation_per_sec;

ObjectProperties();
std::string dump();
Expand Down
6 changes: 6 additions & 0 deletions src/script/common/c_content.cpp
Expand Up @@ -201,13 +201,19 @@ void read_object_properties(lua_State *L, int index,
}
lua_pop(L, 1);
getboolfield(L, -1, "backface_culling", prop->backface_culling);

getstringfield(L, -1, "nametag", prop->nametag);
lua_getfield(L, -1, "nametag_color");
if (!lua_isnil(L, -1)) {
video::SColor color = prop->nametag_color;
if (read_color(L, -1, &color))
prop->nametag_color = color;
}

This comment has been minimized.

Copy link
@oleastre

oleastre Dec 20, 2015

Contributor

I got some problems loading games:
Runtime error from mod 'default' in callback on_joinplayer(): attempt to index a nil value

Simply adding a lua_pop(L, 1); here solves the issue.

lua_getfield(L, -1, "automatic_face_movement_max_rotation_per_sec");
if (lua_isnumber(L, -1)) {
prop->automatic_face_movement_max_rotation_per_sec = luaL_checknumber(L, -1);
}
lua_pop(L, 1);
}

Expand Down

1 comment on commit 70ea5d5

@HybridDog
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it needs to be added to lua_api.txt, doesn't it?
https://github.com/minetest/minetest/blob/master/doc/lua_api.txt#L3238

Please sign in to comment.