{
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);
+}
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<typename T>
+ static T readParam(lua_State *L, int index);
};
// Do it
v3f pos = checkFloatPos(L, 1);
- float radius = luaL_checknumber(L, 2) * BS;
+ float radius = readParam<float>(L, 2) * BS;
std::vector<u16> ids;
env->getObjectsInsideRadius(ids, pos, radius);
ScriptApiBase *script = getScriptApiBase(L);
GET_ENV_PTR;
// Do it
- float timeofday_f = luaL_checknumber(L, 1);
+ float timeofday_f = readParam<float>(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
} 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<float>(L, 3);
+ params.spread = v3f(1, 1, 1) * readParam<float>(L, 4);
}
params.seed += (int)env->getServerMap().getSeed();
MetaDataRef *ref = checkobject(L, 1);
std::string name = luaL_checkstring(L, 2);
- float a = luaL_checknumber(L, 3);
+ float a = readParam<float>(L, 3);
std::string str = ftos(a);
Metadata *meta = ref->getmeta(true);
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<float>(L,2);
+ f32 e = readParam<float>(L,3);
env->getMap().setNodeTimer(NodeTimer(t, e, o->m_p));
return 0;
}
NodeTimerRef *o = checkobject(L, 1);
ServerEnvironment *env = o->m_env;
if(env == NULL) return 0;
- f32 t = luaL_checknumber(L,2);
+ f32 t = readParam<float>(L,2);
env->getMap().setNodeTimer(NodeTimer(t, 0, o->m_p));
return 0;
}
} 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<float>(L, 3);
+ params.spread = v3f(1, 1, 1) * readParam<float>(L, 4);
}
LuaPerlinNoise *o = new LuaPerlinNoise(¶ms);
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<float>(L, 2) * core::RADTODEG;
// Do it
co->setYaw(yaw);
return 0;
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<float>(L, 2) * core::RADTODEG;
// Do it
co->setPitchAndSend(pitch);
return 1;
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<float>(L, 2) * core::RADTODEG;
// Do it
co->setYawAndSend(yaw);
return 1;
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<float>(L, 2) * core::RADTODEG;
// Do it
co->setPitchAndSend(pitch);
return 1;
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<float>(L, 2) * core::RADTODEG;
// Do it
co->setYawAndSend(yaw);
return 1;
float ratio = 0.0f;
if (!lua_isnil(L, 2)) {
do_override = true;
- ratio = luaL_checknumber(L, 2);
+ ratio = readParam<float>(L, 2);
}
if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio))
{
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<float>(L, 2);
+ float gain = readParam<float>(L, 3);
getServer(L)->fadeSound(handle, step, gain);
return 0;
}
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<float>(L, 3)));
return 1;
}
int level = -1;
if (!lua_isnone(L, 3) && !lua_isnil(L, 3))
- level = luaL_checknumber(L, 3);
+ level = readParam<float>(L, 3);
std::ostringstream os;
compressZlib(std::string(data, size), os, level);