diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index a20f1c96..ed5c4863 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -252,9 +252,11 @@ function session.reset_entry(name) session.entries[name] = nil end -function timeit(func, ...) +function timeit(iters, func, ...) local tm = time.uptime() - func(...) + for i=1,iters do + func(...) + end print("[time mcs]", (time.uptime()-tm) * 1000000) end diff --git a/src/logic/scripting/lua/api_lua.hpp b/src/logic/scripting/lua/api_lua.hpp index 341aa94b..d76a3796 100644 --- a/src/logic/scripting/lua/api_lua.hpp +++ b/src/logic/scripting/lua/api_lua.hpp @@ -4,10 +4,19 @@ #include "lua_util.hpp" #include - + +/// Definitions can be found in local .cpp files +/// having same names as declarations + +/// l_ prefix means that function is lua_CFunction: +/// int l_function_name(lua_State* L); +/// use following syntax: +/// int l_function_name(lua::State* L); + // Libraries extern const luaL_Reg audiolib []; extern const luaL_Reg blocklib []; +extern const luaL_Reg cameralib []; extern const luaL_Reg consolelib []; extern const luaL_Reg corelib []; extern const luaL_Reg entitylib []; @@ -23,9 +32,9 @@ extern const luaL_Reg packlib []; extern const luaL_Reg playerlib []; extern const luaL_Reg timelib []; extern const luaL_Reg tomllib []; -extern const luaL_Reg vec2lib []; -extern const luaL_Reg vec3lib []; -extern const luaL_Reg vec4lib []; +extern const luaL_Reg vec2lib []; // vecn.cpp +extern const luaL_Reg vec3lib []; // vecn.cpp +extern const luaL_Reg vec4lib []; // vecn.cpp extern const luaL_Reg worldlib []; // Components diff --git a/src/logic/scripting/lua/lib__transform.cpp b/src/logic/scripting/lua/lib__transform.cpp index c1de157c..11ee1566 100644 --- a/src/logic/scripting/lua/lib__transform.cpp +++ b/src/logic/scripting/lua/lib__transform.cpp @@ -1,13 +1,13 @@ #include "libentity.hpp" -static int l_transform_get_pos(lua::State* L) { +static int l_get_pos(lua::State* L) { if (auto entity = get_entity(L, 1)) { return lua::pushvec3_arr(L, entity->getTransform().pos); } return 0; } -static int l_transform_set_pos(lua::State* L) { +static int l_set_pos(lua::State* L) { if (auto entity = get_entity(L, 1)) { auto vec = lua::tovec3(L, 2); entity->getTransform().setPos(vec); @@ -16,28 +16,28 @@ static int l_transform_set_pos(lua::State* L) { return 0; } -static int l_transform_get_size(lua::State* L) { +static int l_get_size(lua::State* L) { if (auto entity = get_entity(L, 1)) { return lua::pushvec3_arr(L, entity->getTransform().size); } return 0; } -static int l_transform_set_size(lua::State* L) { +static int l_set_size(lua::State* L) { if (auto entity = get_entity(L, 1)) { entity->getTransform().setSize(lua::tovec3(L, 2)); } return 0; } -static int l_transform_get_rot(lua::State* L) { +static int l_get_rot(lua::State* L) { if (auto entity = get_entity(L, 1)) { return lua::pushmat4(L, entity->getTransform().rot); } return 0; } -static int l_transform_set_rot(lua::State* L) { +static int l_set_rot(lua::State* L) { if (auto entity = get_entity(L, 1)) { entity->getTransform().setRot(lua::tomat4(L, 2)); } @@ -45,11 +45,11 @@ static int l_transform_set_rot(lua::State* L) { } const luaL_Reg transformlib [] = { - {"get_pos", lua::wrap}, - {"set_pos", lua::wrap}, - {"get_size", lua::wrap}, - {"set_size", lua::wrap}, - {"get_rot", lua::wrap}, - {"set_rot", lua::wrap}, + {"get_pos", lua::wrap}, + {"set_pos", lua::wrap}, + {"get_size", lua::wrap}, + {"set_size", lua::wrap}, + {"get_rot", lua::wrap}, + {"set_rot", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libcamera.cpp b/src/logic/scripting/lua/libcamera.cpp new file mode 100644 index 00000000..65ca32f4 --- /dev/null +++ b/src/logic/scripting/lua/libcamera.cpp @@ -0,0 +1,18 @@ +#include "api_lua.hpp" + +#include "../../../content/Content.hpp" +#include "../../../world/Level.hpp" + +using namespace scripting; + +int l_index(lua::State* L) { + auto name = lua::require_string(L, 1); + auto& indices = content->getIndices(ResourceType::CAMERA); + std::cout << "index: " << name << std::endl; + return lua::pushinteger(L, indices.indexOf(name)); +} + +const luaL_Reg cameralib [] = { + {"index", lua::wrap}, + {NULL, NULL} +}; diff --git a/src/logic/scripting/lua/libentity.cpp b/src/logic/scripting/lua/libentity.cpp index fc7732ae..8c82f83d 100644 --- a/src/logic/scripting/lua/libentity.cpp +++ b/src/logic/scripting/lua/libentity.cpp @@ -8,11 +8,11 @@ using namespace scripting; -static int l_entity_exists(lua::State* L) { +static int l_exists(lua::State* L) { return lua::pushboolean(L, get_entity(L, 1).has_value()); } -static int l_entity_spawn(lua::State* L) { +static int l_spawn(lua::State* L) { auto level = controller->getLevel(); auto defname = lua::tostring(L, 1); auto& def = content->entities.require(defname); @@ -25,14 +25,14 @@ static int l_entity_spawn(lua::State* L) { return 1; } -static int l_entity_despawn(lua::State* L) { +static int l_despawn(lua::State* L) { if (auto entity = get_entity(L, 1)) { entity->destroy(); } return 0; } -static int l_entity_set_rig(lua::State* L) { +static int l_set_rig(lua::State* L) { if (auto entity = get_entity(L, 1)) { std::string skeletonName = lua::require_string(L, 2); auto rigConfig = content->getRig(skeletonName); @@ -45,9 +45,9 @@ static int l_entity_set_rig(lua::State* L) { } const luaL_Reg entitylib [] = { - {"exists", lua::wrap}, - {"spawn", lua::wrap}, - {"despawn", lua::wrap}, - {"set_rig", lua::wrap}, + {"exists", lua::wrap}, + {"spawn", lua::wrap}, + {"despawn", lua::wrap}, + {"set_rig", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index 1c1ff32b..0b2070b2 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -48,6 +48,7 @@ static void create_libs(lua::State* L) { openlib(L, "world", worldlib); openlib(L, "entities", entitylib); + openlib(L, "cameras", cameralib); // components openlib(L, "__skeleton", skeletonlib);