aboutsummaryrefslogtreecommitdiff
path: root/src/script
diff options
context:
space:
mode:
authorCoderForTheBetter <marcumjp@gmail.com>2018-11-28 03:38:50 -0500
committerLoïc Blot <nerzhul@users.noreply.github.com>2018-11-28 09:38:50 +0100
commitfaa358e797128ab07bb5644ce305a832d59e5596 (patch)
treedcf05ef0611b839503c4845fd89ad9519ee1c4bb /src/script
parent9519d57017cfa28eb4f2fbf3c780f826017bbb00 (diff)
downloadminetest-faa358e797128ab07bb5644ce305a832d59e5596.tar.gz
minetest-faa358e797128ab07bb5644ce305a832d59e5596.tar.bz2
minetest-faa358e797128ab07bb5644ce305a832d59e5596.zip
Add Lua methods 'set_rotation()' and 'get_rotation()' (#7395)
* Adds Lua methods 'set_rotation()' and 'get_rotation'. Also changed some method names to be more clear. Instead of an f32 being sent over network for yaw, now a v3f is sent for rotation on xyz axes. Perserved Lua method set_yaw/setyaw so that old mods still work, other wise to set yaw they would need to switch to set_rotation(0, yaw, 0).
Diffstat (limited to 'src/script')
-rw-r--r--src/script/lua_api/l_object.cpp61
-rw-r--r--src/script/lua_api/l_object.h6
2 files changed, 54 insertions, 13 deletions
diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp
index 14010533e..f077d42a0 100644
--- a/src/script/lua_api/l_object.cpp
+++ b/src/script/lua_api/l_object.cpp
@@ -889,19 +889,51 @@ int ObjectRef::l_get_acceleration(lua_State *L)
return 1;
}
+// set_rotation(self, {x=num, y=num, z=num})
+// Each 'num' is in radians
+int ObjectRef::l_set_rotation(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ ObjectRef *ref = checkobject(L, 1);
+ LuaEntitySAO *co = getluaobject(ref);
+ if (!co)
+ return 0;
+
+ v3f rotation = check_v3f(L, 2) * core::RADTODEG;
+ co->setRotation(rotation);
+ return 0;
+}
+
+// get_rotation(self)
+// returns: {x=num, y=num, z=num}
+// Each 'num' is in radians
+int ObjectRef::l_get_rotation(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ ObjectRef *ref = checkobject(L, 1);
+ LuaEntitySAO *co = getluaobject(ref);
+ if (!co)
+ return 0;
+
+ lua_newtable(L);
+ v3f rotation = co->getRotation() * core::DEGTORAD;
+ push_v3f(L, rotation);
+ return 1;
+}
+
// set_yaw(self, radians)
int ObjectRef::l_set_yaw(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref);
+
if (co == NULL) return 0;
if (isNaN(L, 2))
throw LuaError("ObjectRef::set_yaw: NaN value is not allowed.");
float yaw = readParam<float>(L, 2) * core::RADTODEG;
- // Do it
- co->setYaw(yaw);
+ co->setRotation(v3f(0, yaw, 0));
return 0;
}
@@ -911,9 +943,10 @@ int ObjectRef::l_get_yaw(lua_State *L)
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref);
- if (co == NULL) return 0;
- // Do it
- float yaw = co->getYaw() * core::DEGTORAD;
+ if (!co)
+ return 0;
+
+ float yaw = co->getRotation().Y * core::DEGTORAD;
lua_pushnumber(L, yaw);
return 1;
}
@@ -1046,7 +1079,7 @@ int ObjectRef::l_get_look_dir(lua_State *L)
PlayerSAO* co = getplayersao(ref);
if (co == NULL) return 0;
// Do it
- float pitch = co->getRadPitchDep();
+ float pitch = co->getRadLookPitchDep();
float yaw = co->getRadYawDep();
v3f v(std::cos(pitch) * std::cos(yaw), std::sin(pitch), std::cos(pitch) *
std::sin(yaw));
@@ -1067,7 +1100,7 @@ int ObjectRef::l_get_look_pitch(lua_State *L)
PlayerSAO* co = getplayersao(ref);
if (co == NULL) return 0;
// Do it
- lua_pushnumber(L, co->getRadPitchDep());
+ lua_pushnumber(L, co->getRadLookPitchDep());
return 1;
}
@@ -1096,7 +1129,7 @@ int ObjectRef::l_get_look_vertical(lua_State *L)
PlayerSAO* co = getplayersao(ref);
if (co == NULL) return 0;
// Do it
- lua_pushnumber(L, co->getRadPitch());
+ lua_pushnumber(L, co->getRadLookPitch());
return 1;
}
@@ -1108,7 +1141,7 @@ int ObjectRef::l_get_look_horizontal(lua_State *L)
PlayerSAO* co = getplayersao(ref);
if (co == NULL) return 0;
// Do it
- lua_pushnumber(L, co->getRadYaw());
+ lua_pushnumber(L, co->getRadRotation().Y);
return 1;
}
@@ -1121,7 +1154,7 @@ int ObjectRef::l_set_look_vertical(lua_State *L)
if (co == NULL) return 0;
float pitch = readParam<float>(L, 2) * core::RADTODEG;
// Do it
- co->setPitchAndSend(pitch);
+ co->setLookPitchAndSend(pitch);
return 1;
}
@@ -1134,7 +1167,7 @@ int ObjectRef::l_set_look_horizontal(lua_State *L)
if (co == NULL) return 0;
float yaw = readParam<float>(L, 2) * core::RADTODEG;
// Do it
- co->setYawAndSend(yaw);
+ co->setPlayerYawAndSend(yaw);
return 1;
}
@@ -1152,7 +1185,7 @@ int ObjectRef::l_set_look_pitch(lua_State *L)
if (co == NULL) return 0;
float pitch = readParam<float>(L, 2) * core::RADTODEG;
// Do it
- co->setPitchAndSend(pitch);
+ co->setLookPitchAndSend(pitch);
return 1;
}
@@ -1170,7 +1203,7 @@ int ObjectRef::l_set_look_yaw(lua_State *L)
if (co == NULL) return 0;
float yaw = readParam<float>(L, 2) * core::RADTODEG;
// Do it
- co->setYawAndSend(yaw);
+ co->setPlayerYawAndSend(yaw);
return 1;
}
@@ -1861,6 +1894,8 @@ luaL_Reg ObjectRef::methods[] = {
luamethod_aliased(ObjectRef, get_acceleration, getacceleration),
luamethod_aliased(ObjectRef, set_yaw, setyaw),
luamethod_aliased(ObjectRef, get_yaw, getyaw),
+ luamethod(ObjectRef, set_rotation),
+ luamethod(ObjectRef, get_rotation),
luamethod_aliased(ObjectRef, set_texture_mod, settexturemod),
luamethod_aliased(ObjectRef, set_sprite, setsprite),
luamethod(ObjectRef, get_entity_name),
diff --git a/src/script/lua_api/l_object.h b/src/script/lua_api/l_object.h
index 6876ea512..c7d963d87 100644
--- a/src/script/lua_api/l_object.h
+++ b/src/script/lua_api/l_object.h
@@ -172,6 +172,12 @@ private:
// get_acceleration(self)
static int l_get_acceleration(lua_State *L);
+ // set_rotation(self, {x=num, y=num, z=num})
+ static int l_set_rotation(lua_State *L);
+
+ // get_rotation(self)
+ static int l_get_rotation(lua_State *L);
+
// set_yaw(self, radians)
static int l_set_yaw(lua_State *L);