diff --git a/src/script/lua_api/l_base.cpp b/src/script/lua_api/l_base.cpp index e62aa2368..052f661bb 100644 --- a/src/script/lua_api/l_base.cpp +++ b/src/script/lua_api/l_base.cpp @@ -90,3 +90,15 @@ bool ModApiBase::isNaN(lua_State *L, int idx) { return lua_type(L, idx) == LUA_TNUMBER && std::isnan(lua_tonumber(L, idx)); } + +/* + * Read template functions + */ +template<> +float ModApiBase::readParam(lua_State *L, int index) +{ + if (isNaN(L, index)) + throw LuaError("NaN value is not allowed."); + + return (float) luaL_checknumber(L, index); +} diff --git a/src/script/lua_api/l_base.h b/src/script/lua_api/l_base.h index 20aa4cc09..d0160f03b 100644 --- a/src/script/lua_api/l_base.h +++ b/src/script/lua_api/l_base.h @@ -71,4 +71,15 @@ public: int top); static bool isNaN(lua_State *L, int idx); + + /** + * Read a value using a template type T from Lua State L and index + * + * @tparam T type to read from Lua + * @param L Lua state + * @param index Lua Index to read + * @return read value from Lua + */ + template + static T readParam(lua_State *L, int index); }; diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 6d45d6c49..2eb7f3395 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -666,7 +666,7 @@ int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L) // Do it v3f pos = checkFloatPos(L, 1); - float radius = luaL_checknumber(L, 2) * BS; + float radius = readParam(L, 2) * BS; std::vector ids; env->getObjectsInsideRadius(ids, pos, radius); ScriptApiBase *script = getScriptApiBase(L); @@ -690,7 +690,7 @@ int ModApiEnvMod::l_set_timeofday(lua_State *L) GET_ENV_PTR; // Do it - float timeofday_f = luaL_checknumber(L, 1); + float timeofday_f = readParam(L, 1); sanity_check(timeofday_f >= 0.0 && timeofday_f <= 1.0); int timeofday_mh = (int)(timeofday_f * 24000.0); // This should be set directly in the environment but currently @@ -925,8 +925,8 @@ int ModApiEnvMod::l_get_perlin(lua_State *L) } else { params.seed = luaL_checkint(L, 1); params.octaves = luaL_checkint(L, 2); - params.persist = luaL_checknumber(L, 3); - params.spread = v3f(1, 1, 1) * luaL_checknumber(L, 4); + params.persist = readParam(L, 3); + params.spread = v3f(1, 1, 1) * readParam(L, 4); } params.seed += (int)env->getServerMap().getSeed(); diff --git a/src/script/lua_api/l_metadata.cpp b/src/script/lua_api/l_metadata.cpp index 3901ac2cf..59017dbff 100644 --- a/src/script/lua_api/l_metadata.cpp +++ b/src/script/lua_api/l_metadata.cpp @@ -190,7 +190,7 @@ int MetaDataRef::l_set_float(lua_State *L) MetaDataRef *ref = checkobject(L, 1); std::string name = luaL_checkstring(L, 2); - float a = luaL_checknumber(L, 3); + float a = readParam(L, 3); std::string str = ftos(a); Metadata *meta = ref->getmeta(true); diff --git a/src/script/lua_api/l_nodetimer.cpp b/src/script/lua_api/l_nodetimer.cpp index 55d3aec70..15a59744f 100644 --- a/src/script/lua_api/l_nodetimer.cpp +++ b/src/script/lua_api/l_nodetimer.cpp @@ -43,8 +43,8 @@ int NodeTimerRef::l_set(lua_State *L) NodeTimerRef *o = checkobject(L, 1); ServerEnvironment *env = o->m_env; if(env == NULL) return 0; - f32 t = luaL_checknumber(L,2); - f32 e = luaL_checknumber(L,3); + f32 t = readParam(L,2); + f32 e = readParam(L,3); env->getMap().setNodeTimer(NodeTimer(t, e, o->m_p)); return 0; } @@ -55,7 +55,7 @@ int NodeTimerRef::l_start(lua_State *L) NodeTimerRef *o = checkobject(L, 1); ServerEnvironment *env = o->m_env; if(env == NULL) return 0; - f32 t = luaL_checknumber(L,2); + f32 t = readParam(L,2); env->getMap().setNodeTimer(NodeTimer(t, 0, o->m_p)); return 0; } diff --git a/src/script/lua_api/l_noise.cpp b/src/script/lua_api/l_noise.cpp index 85e908356..d382ca3bd 100644 --- a/src/script/lua_api/l_noise.cpp +++ b/src/script/lua_api/l_noise.cpp @@ -69,8 +69,8 @@ int LuaPerlinNoise::create_object(lua_State *L) } else { params.seed = luaL_checkint(L, 1); params.octaves = luaL_checkint(L, 2); - params.persist = luaL_checknumber(L, 3); - params.spread = v3f(1, 1, 1) * luaL_checknumber(L, 4); + params.persist = readParam(L, 3); + params.spread = v3f(1, 1, 1) * readParam(L, 4); } LuaPerlinNoise *o = new LuaPerlinNoise(¶ms); diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 8377f95c8..f87c02a6d 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -898,7 +898,7 @@ int ObjectRef::l_set_yaw(lua_State *L) if (isNaN(L, 2)) throw LuaError("ObjectRef::set_yaw: NaN value is not allowed."); - float yaw = luaL_checknumber(L, 2) * core::RADTODEG; + float yaw = readParam(L, 2) * core::RADTODEG; // Do it co->setYaw(yaw); return 0; @@ -1118,7 +1118,7 @@ int ObjectRef::l_set_look_vertical(lua_State *L) ObjectRef *ref = checkobject(L, 1); PlayerSAO* co = getplayersao(ref); if (co == NULL) return 0; - float pitch = luaL_checknumber(L, 2) * core::RADTODEG; + float pitch = readParam(L, 2) * core::RADTODEG; // Do it co->setPitchAndSend(pitch); return 1; @@ -1131,7 +1131,7 @@ int ObjectRef::l_set_look_horizontal(lua_State *L) ObjectRef *ref = checkobject(L, 1); PlayerSAO* co = getplayersao(ref); if (co == NULL) return 0; - float yaw = luaL_checknumber(L, 2) * core::RADTODEG; + float yaw = readParam(L, 2) * core::RADTODEG; // Do it co->setYawAndSend(yaw); return 1; @@ -1149,7 +1149,7 @@ int ObjectRef::l_set_look_pitch(lua_State *L) ObjectRef *ref = checkobject(L, 1); PlayerSAO* co = getplayersao(ref); if (co == NULL) return 0; - float pitch = luaL_checknumber(L, 2) * core::RADTODEG; + float pitch = readParam(L, 2) * core::RADTODEG; // Do it co->setPitchAndSend(pitch); return 1; @@ -1167,7 +1167,7 @@ int ObjectRef::l_set_look_yaw(lua_State *L) ObjectRef *ref = checkobject(L, 1); PlayerSAO* co = getplayersao(ref); if (co == NULL) return 0; - float yaw = luaL_checknumber(L, 2) * core::RADTODEG; + float yaw = readParam(L, 2) * core::RADTODEG; // Do it co->setYawAndSend(yaw); return 1; @@ -1739,7 +1739,7 @@ int ObjectRef::l_override_day_night_ratio(lua_State *L) float ratio = 0.0f; if (!lua_isnil(L, 2)) { do_override = true; - ratio = luaL_checknumber(L, 2); + ratio = readParam(L, 2); } if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio)) diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp index d663503e5..05d2a3349 100644 --- a/src/script/lua_api/l_server.cpp +++ b/src/script/lua_api/l_server.cpp @@ -455,8 +455,8 @@ int ModApiServer::l_sound_fade(lua_State *L) { NO_MAP_LOCK_REQUIRED; s32 handle = luaL_checkinteger(L, 1); - float step = luaL_checknumber(L, 2); - float gain = luaL_checknumber(L, 3); + float step = readParam(L, 2); + float gain = readParam(L, 3); getServer(L)->fadeSound(handle, step, gain); return 0; } diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index c1b760941..b25697611 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -179,8 +179,7 @@ int ModApiUtil::l_get_hit_params(lua_State *L) if(lua_isnoneornil(L, 3)) push_hit_params(L, getHitParams(groups, &tp)); else - push_hit_params(L, getHitParams(groups, &tp, - luaL_checknumber(L, 3))); + push_hit_params(L, getHitParams(groups, &tp, readParam(L, 3))); return 1; } @@ -270,7 +269,7 @@ int ModApiUtil::l_compress(lua_State *L) int level = -1; if (!lua_isnone(L, 3) && !lua_isnil(L, 3)) - level = luaL_checknumber(L, 3); + level = readParam(L, 3); std::ostringstream os; compressZlib(std::string(data, size), os, level);