format: reformat project

Signed-off-by: Vyacheslav Ivanov <islavaivanov76@gmail.com>
This commit is contained in:
Vyacheslav Ivanov
2024-08-03 19:53:48 +03:00
committed by Pugemon
parent 736cd175d5
commit bbf33e8e4d
202 changed files with 7389 additions and 5609 deletions
+37 -36
View File
@@ -1,12 +1,12 @@
#ifndef LOGIC_SCRIPTING_API_LUA_HPP_
#define LOGIC_SCRIPTING_API_LUA_HPP_
#include <exception>
#include <string>
#include "lua_util.hpp"
#include <string>
#include <exception>
/// Definitions can be found in local .cpp files
/// Definitions can be found in local .cpp files
/// having same names as declarations
/// l_ prefix means that function is lua_CFunction:
@@ -15,34 +15,34 @@
/// 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 [];
extern const luaL_Reg filelib [];
extern const luaL_Reg guilib [];
extern const luaL_Reg hudlib [];
extern const luaL_Reg inputlib [];
extern const luaL_Reg inventorylib [];
extern const luaL_Reg itemlib [];
extern const luaL_Reg jsonlib [];
extern const luaL_Reg mat4lib [];
extern const luaL_Reg packlib [];
extern const luaL_Reg playerlib [];
extern const luaL_Reg quatlib []; // quat.cpp
extern const luaL_Reg timelib [];
extern const luaL_Reg tomllib [];
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 [];
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[];
extern const luaL_Reg filelib[];
extern const luaL_Reg guilib[];
extern const luaL_Reg hudlib[];
extern const luaL_Reg inputlib[];
extern const luaL_Reg inventorylib[];
extern const luaL_Reg itemlib[];
extern const luaL_Reg jsonlib[];
extern const luaL_Reg mat4lib[];
extern const luaL_Reg packlib[];
extern const luaL_Reg playerlib[];
extern const luaL_Reg quatlib[]; // quat.cpp
extern const luaL_Reg timelib[];
extern const luaL_Reg tomllib[];
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
extern const luaL_Reg skeletonlib [];
extern const luaL_Reg rigidbodylib [];
extern const luaL_Reg transformlib [];
extern const luaL_Reg skeletonlib[];
extern const luaL_Reg rigidbodylib[];
extern const luaL_Reg transformlib[];
// Lua Overrides
extern int l_print(lua::State* L);
@@ -55,23 +55,24 @@ namespace lua {
} else {
throw std::runtime_error(
"invalid number of arguments (" + std::to_string(a) +
" expected)");
" expected)"
);
}
}
[[nodiscard]]
inline uint check_argc(lua::State* L, int a, int b) {
[[nodiscard]] inline uint check_argc(lua::State* L, int a, int b) {
int argc = lua::gettop(L);
if (argc == a || argc == b) {
return static_cast<uint>(argc);
} else {
throw std::runtime_error(
"invalid number of arguments (" + std::to_string(a) + " or " +
std::to_string(b) + " expected)");
std::to_string(b) + " expected)"
);
}
}
}
void initialize_libs_extends(lua::State* L);
#endif // LOGIC_SCRIPTING_API_LUA_HPP_
#endif // LOGIC_SCRIPTING_API_LUA_HPP_
+11 -8
View File
@@ -1,6 +1,5 @@
#include "libentity.hpp"
#include "../../../util/stringutil.hpp"
#include "libentity.hpp"
static int l_get_vel(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
@@ -60,7 +59,9 @@ static int l_set_gravity_scale(lua::State* L) {
static int l_is_vdamping(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
return lua::pushboolean(L, entity->getRigidbody().hitbox.verticalDamping);
return lua::pushboolean(
L, entity->getRigidbody().hitbox.verticalDamping
);
}
return 0;
}
@@ -95,7 +96,9 @@ static int l_set_crouching(lua::State* L) {
static int l_get_body_type(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
return lua::pushstring(L, to_string(entity->getRigidbody().hitbox.type));
return lua::pushstring(
L, to_string(entity->getRigidbody().hitbox.type)
);
}
return 0;
}
@@ -106,7 +109,8 @@ static int l_set_body_type(lua::State* L) {
entity->getRigidbody().hitbox.type = *type;
} else {
throw std::runtime_error(
"unknown body type "+util::quote(lua::tostring(L, 2)));
"unknown body type " + util::quote(lua::tostring(L, 2))
);
}
}
return 0;
@@ -126,7 +130,7 @@ static int l_set_linear_damping(lua::State* L) {
return 0;
}
const luaL_Reg rigidbodylib [] = {
const luaL_Reg rigidbodylib[] = {
{"is_enabled", lua::wrap<l_is_enabled>},
{"set_enabled", lua::wrap<l_set_enabled>},
{"get_vel", lua::wrap<l_get_vel>},
@@ -144,5 +148,4 @@ const luaL_Reg rigidbodylib [] = {
{"set_crouching", lua::wrap<l_set_crouching>},
{"get_body_type", lua::wrap<l_get_body_type>},
{"set_body_type", lua::wrap<l_set_body_type>},
{NULL, NULL}
};
{NULL, NULL}};
+14 -11
View File
@@ -1,12 +1,14 @@
#include "../../../objects/rigging.hpp"
#include "libentity.hpp"
#include "../../../objects/rigging.hpp"
static int index_range_check(const rigging::Skeleton& skeleton, lua::Integer index) {
static int index_range_check(
const rigging::Skeleton& skeleton, lua::Integer index
) {
if (static_cast<size_t>(index) >= skeleton.pose.matrices.size()) {
throw std::runtime_error("index out of range [0, " +
std::to_string(skeleton.pose.matrices.size()) +
"]");
throw std::runtime_error(
"index out of range [0, " +
std::to_string(skeleton.pose.matrices.size()) + "]"
);
}
return static_cast<int>(index);
}
@@ -60,7 +62,8 @@ static int l_set_matrix(lua::State* L) {
static int l_get_texture(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
auto& skeleton = entity->getSkeleton();
skeleton.textures[lua::require_string(L, 2)] = lua::require_string(L, 3);
skeleton.textures[lua::require_string(L, 2)] =
lua::require_string(L, 3);
const auto& found = skeleton.textures.find(lua::require_string(L, 2));
if (found != skeleton.textures.end()) {
return lua::pushstring(L, found->second);
@@ -72,7 +75,8 @@ static int l_get_texture(lua::State* L) {
static int l_set_texture(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
auto& skeleton = entity->getSkeleton();
skeleton.textures[lua::require_string(L, 2)] = lua::require_string(L, 3);
skeleton.textures[lua::require_string(L, 2)] =
lua::require_string(L, 3);
}
return 0;
}
@@ -128,7 +132,7 @@ static int l_set_color(lua::State* L) {
return 0;
}
const luaL_Reg skeletonlib [] = {
const luaL_Reg skeletonlib[] = {
{"get_model", lua::wrap<l_get_model>},
{"set_model", lua::wrap<l_set_model>},
{"get_matrix", lua::wrap<l_get_matrix>},
@@ -140,5 +144,4 @@ const luaL_Reg skeletonlib [] = {
{"set_visible", lua::wrap<l_set_visible>},
{"get_color", lua::wrap<l_get_color>},
{"set_color", lua::wrap<l_set_color>},
{NULL, NULL}
};
{NULL, NULL}};
+2 -3
View File
@@ -44,12 +44,11 @@ static int l_set_rot(lua::State* L) {
return 0;
}
const luaL_Reg transformlib [] = {
const luaL_Reg transformlib[] = {
{"get_pos", lua::wrap<l_get_pos>},
{"set_pos", lua::wrap<l_set_pos>},
{"get_size", lua::wrap<l_get_size>},
{"set_size", lua::wrap<l_set_size>},
{"get_rot", lua::wrap<l_get_rot>},
{"set_rot", lua::wrap<l_set_rot>},
{NULL, NULL}
};
{NULL, NULL}};
+64 -64
View File
@@ -1,7 +1,6 @@
#include "api_lua.hpp"
#include "../../../audio/audio.hpp"
#include "../../../engine.hpp"
#include "api_lua.hpp"
inline const char* DEFAULT_CHANNEL = "regular";
@@ -37,17 +36,15 @@ inline audio::speakerid_t play_sound(
return 0;
}
return audio::play(
sound,
sound,
glm::vec3(
static_cast<float>(x),
static_cast<float>(y),
static_cast<float>(z)
),
relative,
volume,
static_cast<float>(x), static_cast<float>(y), static_cast<float>(z)
),
relative,
volume,
pitch,
loop,
audio::PRIORITY_NORMAL,
audio::PRIORITY_NORMAL,
channel
);
}
@@ -68,32 +65,31 @@ inline audio::speakerid_t play_stream(
}
auto paths = scripting::engine->getResPaths();
return audio::play_stream(
paths->find(filename),
paths->find(filename),
glm::vec3(
static_cast<float>(x),
static_cast<float>(y),
static_cast<float>(z)
),
relative,
volume,
pitch,
loop,
static_cast<float>(x), static_cast<float>(y), static_cast<float>(z)
),
relative,
volume,
pitch,
loop,
channel
);
}
/// @brief audio.play_stream(
/// name: string,
/// x: number,
/// y: number,
/// z: number,
/// x: number,
/// y: number,
/// z: number,
/// volume: number,
/// pitch: number,
/// channel: string = "regular",
/// loop: bool = false)
static int l_audio_play_stream(lua::State* L) {
return lua::pushinteger(L, static_cast<lua::Integer>(
play_stream(
return lua::pushinteger(
L,
static_cast<lua::Integer>(play_stream(
lua::tostring(L, 1),
false,
lua::tonumber(L, 2),
@@ -103,8 +99,8 @@ static int l_audio_play_stream(lua::State* L) {
lua::tonumber(L, 6),
lua::toboolean(L, 8),
extract_channel_index(L, 7)
)
));
))
);
}
/// @brief audio.play_stream_2d(
@@ -114,31 +110,35 @@ static int l_audio_play_stream(lua::State* L) {
/// channel: string = "regular",
/// loop: bool = false)
static int l_audio_play_stream_2d(lua::State* L) {
return lua::pushinteger(L, static_cast<lua::Integer>(
play_stream(
return lua::pushinteger(
L,
static_cast<lua::Integer>(play_stream(
lua::tostring(L, 1),
true,
0.0, 0.0, 0.0,
0.0,
0.0,
0.0,
lua::tonumber(L, 2),
lua::tonumber(L, 3),
lua::toboolean(L, 5),
extract_channel_index(L, 4)
)
));
))
);
}
/// @brief audio.play_sound(
/// name: string,
/// x: number,
/// y: number,
/// z: number,
/// name: string,
/// x: number,
/// y: number,
/// z: number,
/// volume: number,
/// pitch: number,
/// channel: string = "regular",
/// loop: bool = false)
static int l_audio_play_sound(lua::State* L) {
return lua::pushinteger(L, static_cast<lua::Integer>(
play_sound(
return lua::pushinteger(
L,
static_cast<lua::Integer>(play_sound(
lua::tostring(L, 1),
false,
lua::tonumber(L, 2),
@@ -148,28 +148,31 @@ static int l_audio_play_sound(lua::State* L) {
lua::tonumber(L, 6),
lua::toboolean(L, 8),
extract_channel_index(L, 7)
)
));
))
);
}
/// @brief audio.play_sound_2d(
/// name: string,
/// name: string,
/// volume: number,
/// pitch: number,
/// channel: string = "regular",
/// loop: bool = false)
static int l_audio_play_sound_2d(lua::State* L) {
return lua::pushinteger(L, static_cast<lua::Integer>(
play_sound(
return lua::pushinteger(
L,
static_cast<lua::Integer>(play_sound(
lua::tostring(L, 1),
true,
0.0, 0.0, 0.0,
0.0,
0.0,
0.0,
lua::tonumber(L, 2),
lua::tonumber(L, 3),
lua::toboolean(L, 5),
extract_channel_index(L, 4)
)
));
))
);
}
/// @brief audio.stop(speakerid: integer) -> nil
@@ -181,7 +184,7 @@ static int l_audio_stop(lua::State* L) {
return 0;
}
/// @brief audio.pause(speakerid: integer) -> nil
/// @brief audio.pause(speakerid: integer) -> nil
static int l_audio_pause(lua::State* L) {
auto speaker = audio::get_speaker(lua::tointeger(L, 1));
if (speaker != nullptr) {
@@ -190,7 +193,7 @@ static int l_audio_pause(lua::State* L) {
return 0;
}
/// @brief audio.resume(speakerid: integer) -> nil
/// @brief audio.resume(speakerid: integer) -> nil
static int l_audio_resume(lua::State* L) {
auto speaker = audio::get_speaker(lua::tointeger(L, 1));
if (speaker != nullptr && speaker->isPaused()) {
@@ -199,7 +202,7 @@ static int l_audio_resume(lua::State* L) {
return 0;
}
/// @brief audio.set_loop(speakerid: integer, value: bool) -> nil
/// @brief audio.set_loop(speakerid: integer, value: bool) -> nil
static int l_audio_set_loop(lua::State* L) {
auto speaker = audio::get_speaker(lua::tointeger(L, 1));
if (speaker != nullptr) {
@@ -208,7 +211,7 @@ static int l_audio_set_loop(lua::State* L) {
return 0;
}
/// @brief audio.set_volume(speakerid: integer, value: number) -> nil
/// @brief audio.set_volume(speakerid: integer, value: number) -> nil
static int l_audio_set_volume(lua::State* L) {
auto speaker = audio::get_speaker(lua::tointeger(L, 1));
if (speaker != nullptr) {
@@ -217,7 +220,7 @@ static int l_audio_set_volume(lua::State* L) {
return 0;
}
/// @brief audio.set_pitch(speakerid: integer, value: number) -> nil
/// @brief audio.set_pitch(speakerid: integer, value: number) -> nil
static int l_audio_set_pitch(lua::State* L) {
auto speaker = audio::get_speaker(lua::tointeger(L, 1));
if (speaker != nullptr) {
@@ -226,7 +229,7 @@ static int l_audio_set_pitch(lua::State* L) {
return 0;
}
/// @brief audio.set_time(speakerid: integer, value: number) -> nil
/// @brief audio.set_time(speakerid: integer, value: number) -> nil
static int l_audio_set_time(lua::State* L) {
auto speaker = audio::get_speaker(lua::tointeger(L, 1));
if (speaker != nullptr) {
@@ -235,7 +238,8 @@ static int l_audio_set_time(lua::State* L) {
return 0;
}
/// @brief audio.set_position(speakerid: integer, x: number, y: number, z: number) -> nil
/// @brief audio.set_position(speakerid: integer, x: number, y: number, z:
/// number) -> nil
static int l_audio_set_position(lua::State* L) {
auto speaker = audio::get_speaker(lua::tointeger(L, 1));
if (speaker != nullptr) {
@@ -243,15 +247,14 @@ static int l_audio_set_position(lua::State* L) {
auto y = lua::tonumber(L, 3);
auto z = lua::tonumber(L, 4);
speaker->setPosition(glm::vec3(
static_cast<float>(x),
static_cast<float>(y),
static_cast<float>(z)
static_cast<float>(x), static_cast<float>(y), static_cast<float>(z)
));
}
return 0;
}
/// @brief audio.set_velocity(speakerid: integer, x: number, y: number, z: number) -> nil
/// @brief audio.set_velocity(speakerid: integer, x: number, y: number, z:
/// number) -> nil
static int l_audio_set_velocity(lua::State* L) {
auto speaker = audio::get_speaker(lua::tointeger(L, 1));
if (speaker != nullptr) {
@@ -259,15 +262,13 @@ static int l_audio_set_velocity(lua::State* L) {
auto y = lua::tonumber(L, 3);
auto z = lua::tonumber(L, 4);
speaker->setVelocity(glm::vec3(
static_cast<float>(x),
static_cast<float>(y),
static_cast<float>(z)
static_cast<float>(x), static_cast<float>(y), static_cast<float>(z)
));
}
return 0;
}
/// @brief audio.is_playing(speakerid: integer) -> bool
/// @brief audio.is_playing(speakerid: integer) -> bool
static int l_audio_is_playing(lua::State* L) {
auto speaker = audio::get_speaker(lua::tointeger(L, 1));
if (speaker != nullptr) {
@@ -276,7 +277,7 @@ static int l_audio_is_playing(lua::State* L) {
return lua::pushboolean(L, false);
}
/// @brief audio.is_paused(speakerid: integer) -> bool
/// @brief audio.is_paused(speakerid: integer) -> bool
static int l_audio_is_paused(lua::State* L) {
auto speaker = audio::get_speaker(lua::tointeger(L, 1));
if (speaker != nullptr) {
@@ -358,7 +359,7 @@ static int l_audio_count_streams(lua::State* L) {
return lua::pushinteger(L, audio::count_streams());
}
const luaL_Reg audiolib [] = {
const luaL_Reg audiolib[] = {
{"play_sound", lua::wrap<l_audio_play_sound>},
{"play_sound_2d", lua::wrap<l_audio_play_sound_2d>},
{"play_stream", lua::wrap<l_audio_play_stream>},
@@ -383,5 +384,4 @@ const luaL_Reg audiolib [] = {
{"get_velocity", lua::wrap<l_audio_get_velocity>},
{"count_speakers", lua::wrap<l_audio_count_speakers>},
{"count_streams", lua::wrap<l_audio_count_streams>},
{NULL, NULL}
};
{NULL, NULL}};
+41 -29
View File
@@ -1,14 +1,13 @@
#include "api_lua.hpp"
#include "../../../world/Level.hpp"
#include "../../../voxels/Chunks.hpp"
#include "../../../voxels/Chunk.hpp"
#include "../../../voxels/Block.hpp"
#include "../../../voxels/voxel.hpp"
#include "../../../lighting/Lighting.hpp"
#include "../../../content/Content.hpp"
#include "../../../lighting/Lighting.hpp"
#include "../../../logic/BlocksController.hpp"
#include "../../../logic/LevelController.hpp"
#include "../../../voxels/Block.hpp"
#include "../../../voxels/Chunk.hpp"
#include "../../../voxels/Chunks.hpp"
#include "../../../voxels/voxel.hpp"
#include "../../../world/Level.hpp"
#include "api_lua.hpp"
using namespace scripting;
@@ -80,14 +79,16 @@ static int l_seek_origin(lua::State* L) {
auto z = lua::tointeger(L, 3);
auto vox = level->chunks->get(x, y, z);
auto def = indices->blocks.get(vox->id);
return lua::pushivec3_stack(L, level->chunks->seekOrigin({x, y, z}, def, vox->state));
return lua::pushivec3_stack(
L, level->chunks->seekOrigin({x, y, z}, def, vox->state)
);
}
static int l_set(lua::State* L) {
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto id = lua::tointeger(L, 4);
auto id = lua::tointeger(L, 4);
auto state = lua::tointeger(L, 5);
bool noupdate = lua::toboolean(L, 6);
if (static_cast<size_t>(id) >= indices->blocks.count()) {
@@ -97,7 +98,7 @@ static int l_set(lua::State* L) {
return 0;
}
level->chunks->set(x, y, z, id, int2blockstate(state));
level->lighting->onBlockSet(x,y,z, id);
level->lighting->onBlockSet(x, y, z, id);
if (!noupdate) {
blocks->updateSides(x, y, z);
}
@@ -232,7 +233,7 @@ static int l_set_user_bits(lua::State* L) {
size_t mask = ((1 << bits) - 1) << offset;
auto value = (lua::tointeger(L, 6) << offset) & mask;
auto chunk = level->chunks->getChunkByVoxel(x, y, z);
if (chunk == nullptr) {
return 0;
@@ -243,7 +244,7 @@ static int l_set_user_bits(lua::State* L) {
}
vox->state.userbits = (vox->state.userbits & (~mask)) | value;
chunk->setModifiedAndUnsaved();
return 0;
return 0;
}
static int l_is_replaceable_at(lua::State* L) {
@@ -265,7 +266,7 @@ static int l_get_textures(lua::State* L) {
lua::createtable(L, 6, 0);
for (size_t i = 0; i < 6; i++) {
lua::pushstring(L, def->textureFaces[i]);
lua::rawseti(L, i+1);
lua::rawseti(L, i + 1);
}
return 1;
}
@@ -283,7 +284,7 @@ static int l_get_hitbox(lua::State* L) {
if (auto def = require_block(L)) {
auto& hitbox = def->rt.hitboxes[lua::tointeger(L, 2)].at(0);
lua::createtable(L, 2, 0);
lua::pushvec3(L, hitbox.min());
lua::rawseti(L, 1);
@@ -296,14 +297,14 @@ static int l_get_hitbox(lua::State* L) {
static int l_get_rotation_profile(lua::State* L) {
if (auto def = require_block(L)) {
return lua::pushstring(L, def->rotations.name);
return lua::pushstring(L, def->rotations.name);
}
return 0;
}
static int l_get_picking_item(lua::State* L) {
if (auto def = require_block(L)) {
return lua::pushinteger(L, def->rt.pickingItem);
return lua::pushinteger(L, def->rt.pickingItem);
}
return 0;
}
@@ -323,11 +324,14 @@ static int l_place(lua::State* L) {
}
const auto def = level->content->getIndices()->blocks.get(id);
if (def == nullptr) {
throw std::runtime_error("there is no block with index "+std::to_string(id));
throw std::runtime_error(
"there is no block with index " + std::to_string(id)
);
}
auto player = level->getObject<Player>(playerid);
controller->getBlocksController()->placeBlock(
player ? player.get() : nullptr, def, int2blockstate(state), x, y, z);
player ? player.get() : nullptr, def, int2blockstate(state), x, y, z
);
return 0;
}
@@ -343,7 +347,8 @@ static int l_destruct(lua::State* L) {
const auto def = level->content->getIndices()->blocks.get(voxel->id);
auto player = level->getObject<Player>(playerid);
controller->getBlocksController()->breakBlock(
player ? player.get() : nullptr, def, x, y, z);
player ? player.get() : nullptr, def, x, y, z
);
return 0;
}
@@ -354,13 +359,15 @@ static int l_raycast(lua::State* L) {
glm::vec3 end;
glm::ivec3 normal;
glm::ivec3 iend;
if (auto voxel = level->chunks->rayCast(start, dir, maxDistance, end, normal, iend)) {
if (auto voxel = level->chunks->rayCast(
start, dir, maxDistance, end, normal, iend
)) {
if (lua::gettop(L) >= 4) {
lua::pushvalue(L, 4);
} else {
lua::createtable(L, 0, 5);
}
lua::pushvec3(L, end);
lua::setfield(L, "endpoint");
@@ -385,10 +392,16 @@ static int l_compose_state(lua::State* L) {
throw std::runtime_error("expected array of 3 integers");
}
blockstate state {};
lua::rawgeti(L, 1, 1); state.rotation = lua::tointeger(L, -1); lua::pop(L);
lua::rawgeti(L, 2, 1); state.segment = lua::tointeger(L, -1); lua::pop(L);
lua::rawgeti(L, 3, 1); state.userbits = lua::tointeger(L, -1); lua::pop(L);
lua::rawgeti(L, 1, 1);
state.rotation = lua::tointeger(L, -1);
lua::pop(L);
lua::rawgeti(L, 2, 1);
state.segment = lua::tointeger(L, -1);
lua::pop(L);
lua::rawgeti(L, 3, 1);
state.userbits = lua::tointeger(L, -1);
lua::pop(L);
return lua::pushinteger(L, blockstate2int(state));
}
@@ -409,7 +422,7 @@ static int l_decompose_state(lua::State* L) {
return 1;
}
const luaL_Reg blocklib [] = {
const luaL_Reg blocklib[] = {
{"index", lua::wrap<l_index>},
{"name", lua::wrap<l_get_def>},
{"material", lua::wrap<l_material>},
@@ -442,5 +455,4 @@ const luaL_Reg blocklib [] = {
{"raycast", lua::wrap<l_raycast>},
{"compose_state", lua::wrap<l_compose_state>},
{"decompose_state", lua::wrap<l_decompose_state>},
{NULL, NULL}
};
{NULL, NULL}};
+12 -11
View File
@@ -1,20 +1,19 @@
#include "api_lua.hpp"
#include <glm/ext.hpp>
#include "../../../content/Content.hpp"
#include "../../../world/Level.hpp"
#include "../../../window/Camera.hpp"
#include <glm/ext.hpp>
#include "../../../world/Level.hpp"
#include "api_lua.hpp"
using namespace scripting;
template<int(*getterfunc)(lua::State*, const Camera&)>
template <int (*getterfunc)(lua::State*, const Camera&)>
static int l_camera_getter(lua::State* L) {
size_t index = static_cast<size_t>(lua::tointeger(L, 1));
return getterfunc(L, *level->cameras.at(index));
}
template<void(*setterfunc)(lua::State*, Camera&, int)>
template <void (*setterfunc)(lua::State*, Camera&, int)>
static int l_camera_setter(lua::State* L) {
size_t index = static_cast<size_t>(lua::tointeger(L, 1));
setterfunc(L, *level->cameras.at(index), 2);
@@ -92,19 +91,22 @@ static int l_look_at(lua::State* L) {
size_t index = static_cast<size_t>(lua::tointeger(L, 1));
auto& camera = *level->cameras.at(index);
auto center = lua::tovec<3>(L, 2);
auto matrix = glm::inverse(glm::lookAt(glm::vec3(), center-camera.position, glm::vec3(0, 1, 0)));
auto matrix = glm::inverse(
glm::lookAt(glm::vec3(), center - camera.position, glm::vec3(0, 1, 0))
);
if (lua::isnumber(L, 3)) {
matrix = glm::mat4_cast(glm::slerp(
glm::quat(camera.rotation),
glm::quat(matrix),
static_cast<float>(lua::tonumber(L, 3))));
static_cast<float>(lua::tonumber(L, 3))
));
}
camera.rotation = matrix;
camera.updateVectors();
return 0;
}
const luaL_Reg cameralib [] = {
const luaL_Reg cameralib[] = {
{"index", lua::wrap<l_index>},
{"name", lua::wrap<l_name>},
{"get_pos", lua::wrap<l_camera_getter<getter_pos>>},
@@ -123,5 +125,4 @@ const luaL_Reg cameralib [] = {
{"get_right", lua::wrap<l_camera_getter<getter_right>>},
{"get_up", lua::wrap<l_camera_getter<getter_up>>},
{"look_at", lua::wrap<l_look_at>},
{NULL, NULL}
};
{NULL, NULL}};
+17 -15
View File
@@ -1,8 +1,7 @@
#include "api_lua.hpp"
#include "../../CommandsInterpreter.hpp"
#include "../../../engine.hpp"
#include "../../../coders/commons.hpp"
#include "../../../engine.hpp"
#include "../../CommandsInterpreter.hpp"
#include "api_lua.hpp"
using namespace scripting;
@@ -16,12 +15,16 @@ static int l_add_command(lua::State* L) {
auto func = lua::create_lambda(L);
try {
engine->getCommandsInterpreter()->getRepository()->add(
scheme, description, [func](auto, auto args, auto kwargs) {
scheme,
description,
[func](auto, auto args, auto kwargs) {
return func({args, kwargs});
}
);
} catch (const parsing_error& err) {
throw std::runtime_error(("command scheme error:\n"+err.errorLog()).c_str());
throw std::runtime_error(
("command scheme error:\n" + err.errorLog()).c_str()
);
}
return 0;
}
@@ -64,7 +67,7 @@ static int l_get_command_info(lua::State* L) {
}
const auto& args = command->getArgs();
const auto& kwargs = command->getKwArgs();
lua::createtable(L, 0, 4);
lua::pushstring(L, name);
@@ -72,15 +75,15 @@ static int l_get_command_info(lua::State* L) {
lua::pushstring(L, command->getDescription());
lua::setfield(L, "description");
lua::createtable(L, args.size(), 0);
for (size_t i = 0; i < args.size(); i++) {
auto& arg = args[i];
lua::createtable(L, 0, 2);
lua::pushstring(L, arg.name);
lua::setfield(L, "name");
lua::pushstring(L, cmd::argtype_name(arg.type));
lua::setfield(L, "type");
@@ -88,7 +91,7 @@ static int l_get_command_info(lua::State* L) {
lua::pushboolean(L, true);
lua::setfield(L, "optional");
}
lua::rawseti(L, i+1);
lua::rawseti(L, i + 1);
}
lua::setfield(L, "args");
@@ -99,18 +102,17 @@ static int l_get_command_info(lua::State* L) {
lua::pushstring(L, cmd::argtype_name(arg.type));
lua::setfield(L, "type");
lua::setfield(L, arg.name);
}
lua::setfield(L, "kwargs");
return 1;
}
const luaL_Reg consolelib [] = {
const luaL_Reg consolelib[] = {
{"add_command", lua::wrap<l_add_command>},
{"execute", lua::wrap<l_execute>},
{"set", lua::wrap<l_set>},
{"get_commands_list", lua::wrap<l_get_commands_list>},
{"get_command_info", lua::wrap<l_get_command_info>},
{NULL, NULL}
};
{NULL, NULL}};
+17 -17
View File
@@ -1,25 +1,24 @@
#include "api_lua.hpp"
#include <memory>
#include <vector>
#include "../../../constants.hpp"
#include "../../../engine.hpp"
#include "../../../files/settings_io.hpp"
#include "../../../files/engine_paths.hpp"
#include "../../../files/settings_io.hpp"
#include "../../../frontend/menu.hpp"
#include "../../../frontend/screens/MenuScreen.hpp"
#include "../../../logic/LevelController.hpp"
#include "../../../logic/EngineController.hpp"
#include "../../../world/Level.hpp"
#include "../../../logic/LevelController.hpp"
#include "../../../window/Events.hpp"
#include "../../../window/Window.hpp"
#include "../../../world/Level.hpp"
#include "../../../world/WorldGenerators.hpp"
#include "../../../constants.hpp"
#include <vector>
#include <memory>
#include "api_lua.hpp"
using namespace scripting;
/// @brief Creating new world
/// @param name Name world
/// @param name Name world
/// @param seed Seed world
/// @param generator Type of generation
static int l_new_world(lua::State* L) {
@@ -32,7 +31,7 @@ static int l_new_world(lua::State* L) {
}
/// @brief Open world
/// @param name Name world
/// @param name Name world
static int l_open_world(lua::State* L) {
auto name = lua::require_string(L, 1);
@@ -79,10 +78,12 @@ static int l_delete_world(lua::State* L) {
/// @param remPacks An array of packs to remove
static int l_reconfig_packs(lua::State* L) {
if (!lua::istable(L, 1)) {
throw std::runtime_error("strings array expected as the first argument");
throw std::runtime_error("strings array expected as the first argument"
);
}
if (!lua::istable(L, 2)) {
throw std::runtime_error("strings array expected as the second argument");
throw std::runtime_error("strings array expected as the second argument"
);
}
std::vector<std::string> addPacks;
if (!lua::istable(L, 1)) {
@@ -90,7 +91,7 @@ static int l_reconfig_packs(lua::State* L) {
}
int addLen = lua::objlen(L, 1);
for (int i = 0; i < addLen; i++) {
lua::rawgeti(L, i+1, 1);
lua::rawgeti(L, i + 1, 1);
addPacks.emplace_back(lua::tostring(L, -1));
lua::pop(L);
}
@@ -101,7 +102,7 @@ static int l_reconfig_packs(lua::State* L) {
}
int remLen = lua::objlen(L, 2);
for (int i = 0; i < remLen; i++) {
lua::rawgeti(L, i+1, 2);
lua::rawgeti(L, i + 1, 2);
remPacks.emplace_back(lua::tostring(L, -1));
lua::pop(L);
}
@@ -190,7 +191,7 @@ static int l_get_generators(lua::State* L) {
return 1;
}
const luaL_Reg corelib [] = {
const luaL_Reg corelib[] = {
{"new_world", lua::wrap<l_new_world>},
{"open_world", lua::wrap<l_open_world>},
{"reopen_world", lua::wrap<l_reopen_world>},
@@ -204,5 +205,4 @@ const luaL_Reg corelib [] = {
{"quit", lua::wrap<l_quit>},
{"get_default_generator", lua::wrap<l_get_default_generator>},
{"get_generators", lua::wrap<l_get_generators>},
{NULL, NULL}
};
{NULL, NULL}};
+16 -14
View File
@@ -1,14 +1,14 @@
#include "libentity.hpp"
#include "../../../objects/Player.hpp"
#include "../../../content/Content.hpp"
#include "../../../engine.hpp"
#include "../../../objects/Entities.hpp"
#include "../../../objects/EntityDef.hpp"
#include "../../../objects/Player.hpp"
#include "../../../objects/rigging.hpp"
#include "../../../physics/Hitbox.hpp"
#include "../../../window/Camera.hpp"
#include "../../../content/Content.hpp"
#include "../../../voxels/Chunks.hpp"
#include "../../../engine.hpp"
#include "../../../window/Camera.hpp"
using namespace scripting;
@@ -21,7 +21,6 @@ static EntityDef* require_entity_def(lua::State* L) {
return indices->entities.get(id);
}
static int l_exists(lua::State* L) {
return lua::pushboolean(L, get_entity(L, 1).has_value());
}
@@ -83,7 +82,9 @@ static int l_set_skeleton(lua::State* L) {
std::string skeletonName = lua::require_string(L, 2);
auto rigConfig = content->getSkeleton(skeletonName);
if (rigConfig == nullptr) {
throw std::runtime_error("skeleton not found '"+skeletonName+"'");
throw std::runtime_error(
"skeleton not found '" + skeletonName + "'"
);
}
entity->setRig(rigConfig);
}
@@ -98,7 +99,7 @@ static int l_get_all_in_box(lua::State* L) {
for (size_t i = 0; i < found.size(); i++) {
const auto& entity = found[i];
lua::pushinteger(L, entity.getUID());
lua::rawseti(L, i+1);
lua::rawseti(L, i + 1);
}
return 1;
}
@@ -111,9 +112,9 @@ static int l_get_all_in_radius(lua::State* L) {
for (size_t i = 0; i < found.size(); i++) {
const auto& entity = found[i];
lua::pushinteger(L, entity.getUID());
lua::rawseti(L, i+1);
lua::rawseti(L, i + 1);
}
return 1;
return 1;
}
static int l_raycast(lua::State* L) {
@@ -127,7 +128,9 @@ static int l_raycast(lua::State* L) {
blockid_t block = BLOCK_VOID;
if (auto voxel = level->chunks->rayCast(start, dir, maxDistance, end, normal, iend)) {
if (auto voxel = level->chunks->rayCast(
start, dir, maxDistance, end, normal, iend
)) {
maxDistance = glm::distance(start, end);
block = voxel->id;
}
@@ -137,7 +140,7 @@ static int l_raycast(lua::State* L) {
} else {
lua::createtable(L, 0, 6);
}
lua::pushvec3(L, start + dir * ray->distance);
lua::setfield(L, "endpoint");
@@ -181,7 +184,7 @@ static int l_raycast(lua::State* L) {
return 0;
}
const luaL_Reg entitylib [] = {
const luaL_Reg entitylib[] = {
{"exists", lua::wrap<l_exists>},
{"def_index", lua::wrap<l_def_index>},
{"def_name", lua::wrap<l_def_name>},
@@ -194,5 +197,4 @@ const luaL_Reg entitylib [] = {
{"get_all_in_box", lua::wrap<l_get_all_in_box>},
{"get_all_in_radius", lua::wrap<l_get_all_in_radius>},
{"raycast", lua::wrap<l_raycast>},
{NULL, NULL}
};
{NULL, NULL}};
+7 -8
View File
@@ -1,15 +1,14 @@
#ifndef LOGIC_SCRIPTING_LUA_LIBENTITY_HPP_
#define LOGIC_SCRIPTING_LUA_LIBENTITY_HPP_
#include "api_lua.hpp"
#include "../../LevelController.hpp"
#include "../../../frontend/hud.hpp"
#include "../../../world/Level.hpp"
#include "../../../objects/Entities.hpp"
#include <optional>
#include "../../../frontend/hud.hpp"
#include "../../../objects/Entities.hpp"
#include "../../../world/Level.hpp"
#include "../../LevelController.hpp"
#include "api_lua.hpp"
namespace scripting {
extern Hud* hud;
}
@@ -20,4 +19,4 @@ inline std::optional<Entity> get_entity(lua::State* L, int idx) {
return level->entities->get(id);
}
#endif // LOGIC_SCRIPTING_LUA_LIBENTITY_HPP_
#endif // LOGIC_SCRIPTING_LUA_LIBENTITY_HPP_
+51 -41
View File
@@ -1,13 +1,12 @@
#include "api_lua.hpp"
#include "../../../engine.hpp"
#include "../../../coders/gzip.hpp"
#include "../../../files/files.hpp"
#include "../../../files/engine_paths.hpp"
#include "../../../util/stringutil.hpp"
#include <string>
#include <filesystem>
#include <string>
#include "../../../coders/gzip.hpp"
#include "../../../engine.hpp"
#include "../../../files/engine_paths.hpp"
#include "../../../files/files.hpp"
#include "../../../util/stringutil.hpp"
#include "api_lua.hpp"
namespace fs = std::filesystem;
using namespace scripting;
@@ -42,14 +41,16 @@ static int l_file_read(lua::State* L) {
if (fs::is_regular_file(path)) {
return lua::pushstring(L, files::read_string(path));
}
throw std::runtime_error("file does not exists "+util::quote(path.u8string()));
throw std::runtime_error(
"file does not exists " + util::quote(path.u8string())
);
}
static int l_file_write(lua::State* L) {
fs::path path = resolve_path(lua::require_string(L, 1));
std::string text = lua::require_string(L, 2);
files::write_string(path, text);
return 1;
return 1;
}
static int l_file_remove(lua::State* L) {
@@ -89,7 +90,7 @@ static int l_file_isdir(lua::State* L) {
static int l_file_length(lua::State* L) {
fs::path path = resolve_path(lua::require_string(L, 1));
if (fs::exists(path)){
if (fs::exists(path)) {
return lua::pushinteger(L, fs::file_size(path));
} else {
return lua::pushinteger(L, -1);
@@ -98,12 +99,12 @@ static int l_file_length(lua::State* L) {
static int l_file_mkdir(lua::State* L) {
fs::path path = resolve_path(lua::require_string(L, 1));
return lua::pushboolean(L, fs::create_directory(path));
return lua::pushboolean(L, fs::create_directory(path));
}
static int l_file_mkdirs(lua::State* L) {
fs::path path = resolve_path(lua::require_string(L, 1));
return lua::pushboolean(L, fs::create_directories(path));
return lua::pushboolean(L, fs::create_directories(path));
}
static int l_file_read_bytes(lua::State* L) {
@@ -116,26 +117,32 @@ static int l_file_read_bytes(lua::State* L) {
lua::createtable(L, length, 0);
int newTable = lua::gettop(L);
for(size_t i = 0; i < length; i++) {
for (size_t i = 0; i < length; i++) {
lua::pushinteger(L, bytes[i]);
lua::rawseti(L, i+1, newTable);
lua::rawseti(L, i + 1, newTable);
}
return 1;
}
throw std::runtime_error("file does not exists "+util::quote(path.u8string()));
throw std::runtime_error(
"file does not exists " + util::quote(path.u8string())
);
}
static int read_bytes_from_table(lua::State* L, int tableIndex, std::vector<ubyte>& bytes) {
if(!lua::istable(L, tableIndex)) {
static int read_bytes_from_table(
lua::State* L, int tableIndex, std::vector<ubyte>& bytes
) {
if (!lua::istable(L, tableIndex)) {
throw std::runtime_error("table expected");
} else {
lua::pushnil(L);
while(lua::next(L, tableIndex - 1) != 0) {
while (lua::next(L, tableIndex - 1) != 0) {
const int byte = lua::tointeger(L, -1);
if(byte < 0 || byte > 255) {
throw std::runtime_error("invalid byte '"+std::to_string(byte)+"'");
if (byte < 0 || byte > 255) {
throw std::runtime_error(
"invalid byte '" + std::to_string(byte) + "'"
);
}
bytes.push_back(byte);
bytes.push_back(byte);
lua::pop(L);
}
return 1;
@@ -145,7 +152,7 @@ static int read_bytes_from_table(lua::State* L, int tableIndex, std::vector<ubyt
static int l_file_write_bytes(lua::State* L) {
int pathIndex = 1;
if(!lua::isstring(L, pathIndex)) {
if (!lua::isstring(L, pathIndex)) {
throw std::runtime_error("string expected");
}
@@ -153,15 +160,19 @@ static int l_file_write_bytes(lua::State* L) {
if (auto bytearray = lua::touserdata<lua::Bytearray>(L, -1)) {
auto& bytes = bytearray->data();
return lua::pushboolean(L, files::write_bytes(path, bytes.data(), bytes.size()));
return lua::pushboolean(
L, files::write_bytes(path, bytes.data(), bytes.size())
);
}
std::vector<ubyte> bytes;
int result = read_bytes_from_table(L, -1, bytes);
if(result != 1) {
if (result != 1) {
return result;
} else {
return lua::pushboolean(L, files::write_bytes(path, bytes.data(), bytes.size()));
return lua::pushboolean(
L, files::write_bytes(path, bytes.data(), bytes.size())
);
}
}
@@ -170,7 +181,7 @@ static int l_file_list_all_res(lua::State* L, const std::string& path) {
lua::createtable(L, files.size(), 0);
for (size_t i = 0; i < files.size(); i++) {
lua::pushstring(L, files[i]);
lua::rawseti(L, i+1);
lua::rawseti(L, i + 1);
}
return 1;
}
@@ -182,7 +193,9 @@ static int l_file_list(lua::State* L) {
}
fs::path path = resolve_path(dirname);
if (!fs::is_directory(path)) {
throw std::runtime_error(util::quote(path.u8string())+" is not a directory");
throw std::runtime_error(
util::quote(path.u8string()) + " is not a directory"
);
}
lua::createtable(L, 0, 0);
size_t index = 1;
@@ -197,46 +210,44 @@ static int l_file_list(lua::State* L) {
}
static int l_file_gzip_compress(lua::State* L) {
std::vector<ubyte> bytes;
int result = read_bytes_from_table(L, -1, bytes);
if(result != 1) {
if (result != 1) {
return result;
} else {
auto compressed_bytes = gzip::compress(bytes.data(), bytes.size());
int newTable = lua::gettop(L);
for(size_t i = 0; i < compressed_bytes.size(); i++) {
for (size_t i = 0; i < compressed_bytes.size(); i++) {
lua::pushinteger(L, compressed_bytes.data()[i]);
lua::rawseti(L, i+1, newTable);
lua::rawseti(L, i + 1, newTable);
}
return 1;
}
}
static int l_file_gzip_decompress(lua::State* L) {
static int l_file_gzip_decompress(lua::State* L) {
std::vector<ubyte> bytes;
int result = read_bytes_from_table(L, -1, bytes);
if(result != 1) {
if (result != 1) {
return result;
} else {
auto decompressed_bytes = gzip::decompress(bytes.data(), bytes.size());
int newTable = lua::gettop(L);
for(size_t i = 0; i < decompressed_bytes.size(); i++) {
for (size_t i = 0; i < decompressed_bytes.size(); i++) {
lua::pushinteger(L, decompressed_bytes.data()[i]);
lua::rawseti(L, i+1, newTable);
lua::rawseti(L, i + 1, newTable);
}
return 1;
}
}
const luaL_Reg filelib [] = {
const luaL_Reg filelib[] = {
{"exists", lua::wrap<l_file_exists>},
{"find", lua::wrap<l_file_find>},
{"isdir", lua::wrap<l_file_isdir>},
@@ -254,5 +265,4 @@ const luaL_Reg filelib [] = {
{"write", lua::wrap<l_file_write>},
{"gzip_compress", lua::wrap<l_file_gzip_compress>},
{"gzip_decompress", lua::wrap<l_file_gzip_decompress>},
{NULL, NULL}
};
{NULL, NULL}};
+117 -97
View File
@@ -1,22 +1,21 @@
#include "api_lua.hpp"
#include "../../../engine.hpp"
#include "../../../assets/Assets.hpp"
#include "../../../items/Inventories.hpp"
#include "../../../graphics/ui/gui_util.hpp"
#include "../../../graphics/ui/elements/UINode.hpp"
#include "../../../graphics/ui/elements/Button.hpp"
#include "../../../graphics/ui/elements/Image.hpp"
#include "../../../graphics/ui/elements/CheckBox.hpp"
#include "../../../graphics/ui/elements/TextBox.hpp"
#include "../../../graphics/ui/elements/TrackBar.hpp"
#include "../../../graphics/ui/elements/Panel.hpp"
#include "../../../graphics/ui/elements/Menu.hpp"
#include "../../../graphics/ui/elements/InventoryView.hpp"
#include "../../../engine.hpp"
#include "../../../frontend/UiDocument.hpp"
#include "../../../frontend/locale.hpp"
#include "../../../graphics/ui/elements/Button.hpp"
#include "../../../graphics/ui/elements/CheckBox.hpp"
#include "../../../graphics/ui/elements/Image.hpp"
#include "../../../graphics/ui/elements/InventoryView.hpp"
#include "../../../graphics/ui/elements/Menu.hpp"
#include "../../../graphics/ui/elements/Panel.hpp"
#include "../../../graphics/ui/elements/TextBox.hpp"
#include "../../../graphics/ui/elements/TrackBar.hpp"
#include "../../../graphics/ui/elements/UINode.hpp"
#include "../../../graphics/ui/gui_util.hpp"
#include "../../../items/Inventories.hpp"
#include "../../../util/stringutil.hpp"
#include "../../../world/Level.hpp"
#include "api_lua.hpp"
using namespace gui;
using namespace scripting;
@@ -26,19 +25,23 @@ struct DocumentNode {
std::shared_ptr<UINode> node;
};
static DocumentNode getDocumentNode(lua::State*, const std::string& name, const std::string& nodeName) {
static DocumentNode getDocumentNode(
lua::State*, const std::string& name, const std::string& nodeName
) {
auto doc = engine->getAssets()->get<UiDocument>(name);
if (doc == nullptr) {
throw std::runtime_error("document '"+name+"' not found");
throw std::runtime_error("document '" + name + "' not found");
}
auto node = doc->get(nodeName);
if (node == nullptr) {
throw std::runtime_error("document '"+name+"' has no element with id '"+nodeName+"'");
throw std::runtime_error(
"document '" + name + "' has no element with id '" + nodeName + "'"
);
}
return {doc, node};
}
static DocumentNode getDocumentNode(lua::State* L, int idx=1) {
static DocumentNode getDocumentNode(lua::State* L, int idx = 1) {
lua::getfield(L, "docname", idx);
lua::getfield(L, "name", idx);
auto docname = lua::require_string(L, -2);
@@ -75,7 +78,8 @@ static int l_container_add(lua::State* L) {
auto node = dynamic_cast<Container*>(docnode.node.get());
auto xmlsrc = lua::require_string(L, 2);
try {
auto subnode = guiutil::create(xmlsrc, docnode.document->getEnvironment());
auto subnode =
guiutil::create(xmlsrc, docnode.document->getEnvironment());
node->add(subnode);
UINode::getIndices(subnode, docnode.document->getMapWriteable());
} catch (const std::exception& err) {
@@ -118,7 +122,9 @@ static int l_container_set_interval(lua::State* L) {
static int l_move_into(lua::State* L) {
auto node = getDocumentNode(L, 1);
auto dest = getDocumentNode(L, 2);
UINode::moveInto(node.node, std::dynamic_pointer_cast<Container>(dest.node));
UINode::moveInto(
node.node, std::dynamic_pointer_cast<Container>(dest.node)
);
return 0;
}
@@ -325,44 +331,47 @@ static int l_gui_getattr(lua::State* L) {
auto element = lua::require_string(L, 2);
auto attr = lua::require_string(L, 3);
static const std::unordered_map<std::string_view, std::function<int(UINode*,lua::State*)>> getters {
{"color", p_get_color},
{"hoverColor", p_get_hover_color},
{"pressedColor", p_get_pressed_color},
{"tooltip", p_get_tooltip},
{"tooltipDelay", p_get_tooltip_delay},
{"pos", p_get_pos},
{"wpos", p_get_wpos},
{"size", p_get_size},
{"interactive", p_is_interactive},
{"visible", p_is_visible},
{"enabled", p_is_enabled},
{"move_into", p_move_into}, // deprecated
{"moveInto", p_move_into},
{"add", p_get_add},
{"destruct", p_get_destruct},
{"clear", p_get_clear},
{"setInterval", p_set_interval},
{"placeholder", p_get_placeholder},
{"valid", p_is_valid},
{"caret", p_get_caret},
{"text", p_get_text},
{"editable", p_get_editable},
{"src", p_get_src},
{"value", p_get_value},
{"min", p_get_min},
{"max", p_get_max},
{"step", p_get_step},
{"trackWidth", p_get_track_width},
{"trackColor", p_get_track_color},
{"checked", p_is_checked},
{"page", p_get_page},
{"back", p_get_back},
{"reset", p_get_reset},
{"paste", p_get_paste},
{"inventory", p_get_inventory},
{"focused", p_get_focused},
};
static const std::unordered_map<
std::string_view,
std::function<int(UINode*, lua::State*)>>
getters {
{"color", p_get_color},
{"hoverColor", p_get_hover_color},
{"pressedColor", p_get_pressed_color},
{"tooltip", p_get_tooltip},
{"tooltipDelay", p_get_tooltip_delay},
{"pos", p_get_pos},
{"wpos", p_get_wpos},
{"size", p_get_size},
{"interactive", p_is_interactive},
{"visible", p_is_visible},
{"enabled", p_is_enabled},
{"move_into", p_move_into}, // deprecated
{"moveInto", p_move_into},
{"add", p_get_add},
{"destruct", p_get_destruct},
{"clear", p_get_clear},
{"setInterval", p_set_interval},
{"placeholder", p_get_placeholder},
{"valid", p_is_valid},
{"caret", p_get_caret},
{"text", p_get_text},
{"editable", p_get_editable},
{"src", p_get_src},
{"value", p_get_value},
{"min", p_get_min},
{"max", p_get_max},
{"step", p_get_step},
{"trackWidth", p_get_track_width},
{"trackColor", p_get_track_color},
{"checked", p_is_checked},
{"page", p_get_page},
{"back", p_get_back},
{"reset", p_get_reset},
{"paste", p_get_paste},
{"inventory", p_get_inventory},
{"focused", p_get_focused},
};
auto func = getters.find(attr);
if (func != getters.end()) {
auto docnode = getDocumentNode(L, docname, element);
@@ -391,7 +400,7 @@ static void p_set_pos(UINode* node, lua::State* L, int idx) {
node->setPos(lua::tovec2(L, idx));
}
static void p_set_wpos(UINode* node, lua::State* L, int idx) {
node->setPos(lua::tovec2(L, idx)-node->calcPos());
node->setPos(lua::tovec2(L, idx) - node->calcPos());
}
static void p_set_size(UINode* node, lua::State* L, int idx) {
node->setSize(lua::tovec2(L, idx));
@@ -486,10 +495,12 @@ static void p_set_inventory(UINode* node, lua::State* L, int idx) {
}
}
}
static void p_set_focused(const std::shared_ptr<UINode> &node, lua::State* L, int idx) {
static void p_set_focused(
const std::shared_ptr<UINode>& node, lua::State* L, int idx
) {
if (lua::toboolean(L, idx) && !node->isFocused()) {
engine->getGUI()->setFocus(node);
} else if (node->isFocused()){
} else if (node->isFocused()) {
node->defocus();
}
}
@@ -502,40 +513,46 @@ static int l_gui_setattr(lua::State* L) {
auto docnode = getDocumentNode(L, docname, element);
auto node = docnode.node;
static const std::unordered_map<std::string_view, std::function<void(UINode*,lua::State*,int)>> setters {
{"color", p_set_color},
{"hoverColor", p_set_hover_color},
{"pressedColor", p_set_pressed_color},
{"tooltip", p_set_tooltip},
{"tooltipDelay", p_set_tooltip_delay},
{"pos", p_set_pos},
{"wpos", p_set_wpos},
{"size", p_set_size},
{"interactive", p_set_interactive},
{"visible", p_set_visible},
{"enabled", p_set_enabled},
{"placeholder", p_set_placeholder},
{"text", p_set_text},
{"editable", p_set_editable},
{"src", p_set_src},
{"caret", p_set_caret},
{"value", p_set_value},
{"min", p_set_min},
{"max", p_set_max},
{"step", p_set_step},
{"trackWidth", p_set_track_width},
{"trackColor", p_set_track_color},
{"checked", p_set_checked},
{"page", p_set_page},
{"inventory", p_set_inventory},
};
static const std::unordered_map<
std::string_view,
std::function<void(UINode*, lua::State*, int)>>
setters {
{"color", p_set_color},
{"hoverColor", p_set_hover_color},
{"pressedColor", p_set_pressed_color},
{"tooltip", p_set_tooltip},
{"tooltipDelay", p_set_tooltip_delay},
{"pos", p_set_pos},
{"wpos", p_set_wpos},
{"size", p_set_size},
{"interactive", p_set_interactive},
{"visible", p_set_visible},
{"enabled", p_set_enabled},
{"placeholder", p_set_placeholder},
{"text", p_set_text},
{"editable", p_set_editable},
{"src", p_set_src},
{"caret", p_set_caret},
{"value", p_set_value},
{"min", p_set_min},
{"max", p_set_max},
{"step", p_set_step},
{"trackWidth", p_set_track_width},
{"trackColor", p_set_track_color},
{"checked", p_set_checked},
{"page", p_set_page},
{"inventory", p_set_inventory},
};
auto func = setters.find(attr);
if (func != setters.end()) {
func->second(node.get(), L, 4);
}
static const std::unordered_map<std::string_view, std::function<void(std::shared_ptr<UINode>,lua::State*,int)>> setters2 {
{"focused", p_set_focused},
};
static const std::unordered_map<
std::string_view,
std::function<void(std::shared_ptr<UINode>, lua::State*, int)>>
setters2 {
{"focused", p_set_focused},
};
auto func2 = setters2.find(attr);
if (func2 != setters2.end()) {
func2->second(node, L, 4);
@@ -547,7 +564,9 @@ static int l_gui_get_env(lua::State* L) {
auto name = lua::require_string(L, 1);
auto doc = engine->getAssets()->get<UiDocument>(name);
if (doc == nullptr) {
throw std::runtime_error("document '"+std::string(name)+"' not found");
throw std::runtime_error(
"document '" + std::string(name) + "' not found"
);
}
lua::getglobal(L, lua::env_name(*doc->getEnvironment()));
return 1;
@@ -568,13 +587,15 @@ static int l_gui_reindex(lua::State* L) {
auto name = lua::require_string(L, 1);
auto doc = engine->getAssets()->get<UiDocument>(name);
if (doc == nullptr) {
throw std::runtime_error("document '"+std::string(name)+"' not found");
throw std::runtime_error(
"document '" + std::string(name) + "' not found"
);
}
doc->rebuildIndices();
return 0;
}
/// @brief gui.get_locales_info() -> table of tables
/// @brief gui.get_locales_info() -> table of tables
static int l_gui_get_locales_info(lua::State* L) {
auto& locales = langs::locales_info;
lua::createtable(L, 0, locales.size());
@@ -591,7 +612,7 @@ static int l_gui_getviewport(lua::State* L) {
return lua::pushvec2(L, engine->getGUI()->getContainer()->getSize());
}
const luaL_Reg guilib [] = {
const luaL_Reg guilib[] = {
{"get_viewport", lua::wrap<l_gui_getviewport>},
{"getattr", lua::wrap<l_gui_getattr>},
{"setattr", lua::wrap<l_gui_setattr>},
@@ -599,5 +620,4 @@ const luaL_Reg guilib [] = {
{"str", lua::wrap<l_gui_str>},
{"get_locales_info", lua::wrap<l_gui_get_locales_info>},
{"__reindex", lua::wrap<l_gui_reindex>},
{NULL, NULL}
};
{NULL, NULL}};
+18 -14
View File
@@ -1,10 +1,11 @@
#include "api_lua.hpp"
#include <glm/glm.hpp>
#include <iostream>
#include "../../../assets/Assets.hpp"
#include "../../../content/Content.hpp"
#include "../../../engine.hpp"
#include "../../../frontend/hud.hpp"
#include "../../../frontend/UiDocument.hpp"
#include "../../../frontend/hud.hpp"
#include "../../../graphics/ui/elements/InventoryView.hpp"
#include "../../../items/Inventories.hpp"
#include "../../../logic/BlocksController.hpp"
@@ -14,9 +15,7 @@
#include "../../../voxels/Chunks.hpp"
#include "../../../voxels/voxel.hpp"
#include "../../../world/Level.hpp"
#include <iostream>
#include <glm/glm.hpp>
#include "api_lua.hpp"
namespace scripting {
extern Hud* hud;
@@ -45,20 +44,24 @@ static int l_hud_open_block(lua::State* L) {
auto vox = level->chunks->get(x, y, z);
if (vox == nullptr) {
throw std::runtime_error("block does not exists at " +
std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(z)
throw std::runtime_error(
"block does not exists at " + std::to_string(x) + " " +
std::to_string(y) + " " + std::to_string(z)
);
}
auto def = content->getIndices()->blocks.get(vox->id);
auto assets = engine->getAssets();
auto layout = assets->get<UiDocument>(def->uiLayout);
if (layout == nullptr) {
throw std::runtime_error("block '"+def->name+"' has no ui layout");
throw std::runtime_error("block '" + def->name + "' has no ui layout");
}
auto id = blocks->createBlockInventory(x, y, z);
hud->openInventory(
glm::ivec3(x, y, z), layout, level->inventories->get(id), playerInventory
glm::ivec3(x, y, z),
layout,
level->inventories->get(id),
playerInventory
);
lua::pushinteger(L, id);
@@ -73,7 +76,7 @@ static int l_hud_show_overlay(lua::State* L) {
auto assets = engine->getAssets();
auto layout = assets->get<UiDocument>(name);
if (layout == nullptr) {
throw std::runtime_error("there is no ui layout "+util::quote(name));
throw std::runtime_error("there is no ui layout " + util::quote(name));
}
hud->showOverlay(layout, playerInventory);
return 0;
@@ -83,7 +86,9 @@ static UiDocument* require_layout(const char* name) {
auto assets = engine->getAssets();
auto layout = assets->get<UiDocument>(name);
if (layout == nullptr) {
throw std::runtime_error("layout '"+std::string(name)+"' is not found");
throw std::runtime_error(
"layout '" + std::string(name) + "' is not found"
);
}
return layout;
}
@@ -132,7 +137,7 @@ static int l_hud_is_inventory_open(lua::State* L) {
return lua::pushboolean(L, hud->isInventoryOpen());
}
const luaL_Reg hudlib [] = {
const luaL_Reg hudlib[] = {
{"open_inventory", lua::wrap<l_hud_open_inventory>},
{"close_inventory", lua::wrap<l_hud_close_inventory>},
{"open_block", lua::wrap<l_hud_open_block>},
@@ -145,5 +150,4 @@ const luaL_Reg hudlib [] = {
{"is_paused", lua::wrap<l_hud_is_paused>},
{"is_inventory_open", lua::wrap<l_hud_is_inventory_open>},
{"get_player", lua::wrap<l_hud_get_player>},
{NULL, NULL}
};
{NULL, NULL}};
+26 -20
View File
@@ -1,12 +1,11 @@
#include "api_lua.hpp"
#include "../../../window/input.hpp"
#include "../../../window/Events.hpp"
#include "../../../util/stringutil.hpp"
#include "../../../graphics/ui/GUI.hpp"
#include "../../../frontend/screens/Screen.hpp"
#include "../../../frontend/hud.hpp"
#include "../../../engine.hpp"
#include "../../../frontend/hud.hpp"
#include "../../../frontend/screens/Screen.hpp"
#include "../../../graphics/ui/GUI.hpp"
#include "../../../util/stringutil.hpp"
#include "../../../window/Events.hpp"
#include "../../../window/input.hpp"
#include "api_lua.hpp"
namespace scripting {
extern Hud* hud;
@@ -15,19 +14,23 @@ using namespace scripting;
static int l_keycode(lua::State* L) {
auto name = lua::require_string(L, 1);
return lua::pushinteger(L, static_cast<int>(input_util::keycode_from(name)));
return lua::pushinteger(
L, static_cast<int>(input_util::keycode_from(name))
);
}
static int l_mousecode(lua::State* L) {
auto name = lua::require_string(L, 1);
return lua::pushinteger(L, static_cast<int>(input_util::mousecode_from(name)));
return lua::pushinteger(
L, static_cast<int>(input_util::mousecode_from(name))
);
}
static int l_add_callback(lua::State* L) {
auto bindname = lua::require_string(L, 1);
const auto& bind = Events::bindings.find(bindname);
if (bind == Events::bindings.end()) {
throw std::runtime_error("unknown binding "+util::quote(bindname));
throw std::runtime_error("unknown binding " + util::quote(bindname));
}
lua::pushvalue(L, 2);
runnable actual_callback = lua::create_runnable(L);
@@ -65,7 +68,7 @@ static int l_is_active(lua::State* L) {
auto bindname = lua::require_string(L, 1);
const auto& bind = Events::bindings.find(bindname);
if (bind == Events::bindings.end()) {
throw std::runtime_error("unknown binding "+util::quote(bindname));
throw std::runtime_error("unknown binding " + util::quote(bindname));
}
return lua::pushboolean(L, bind->second.active());
}
@@ -77,17 +80,22 @@ static int l_is_pressed(lua::State* L) {
throw std::runtime_error("expected 'input_type:key' format");
}
auto prefix = code.substr(0, sep);
auto name = code.substr(sep+1);
auto name = code.substr(sep + 1);
if (prefix == "key") {
return lua::pushboolean(L, Events::pressed(static_cast<int>(input_util::keycode_from(name))));
return lua::pushboolean(
L, Events::pressed(static_cast<int>(input_util::keycode_from(name)))
);
} else if (prefix == "mouse") {
return lua::pushboolean(L, Events::clicked(static_cast<int>(input_util::mousecode_from(name))));
return lua::pushboolean(
L,
Events::clicked(static_cast<int>(input_util::mousecode_from(name)))
);
} else {
throw std::runtime_error("unknown input type "+util::quote(code));
throw std::runtime_error("unknown input type " + util::quote(code));
}
}
const luaL_Reg inputlib [] = {
const luaL_Reg inputlib[] = {
{"keycode", lua::wrap<l_keycode>},
{"mousecode", lua::wrap<l_mousecode>},
{"add_callback", lua::wrap<l_add_callback>},
@@ -95,6 +103,4 @@ const luaL_Reg inputlib [] = {
{"get_bindings", lua::wrap<l_get_bindings>},
{"is_active", lua::wrap<l_is_active>},
{"is_pressed", lua::wrap<l_is_pressed>},
{NULL, NULL}
};
{NULL, NULL}};
+14 -13
View File
@@ -1,10 +1,9 @@
#include "api_lua.hpp"
#include "../../../content/Content.hpp"
#include "../../../world/Level.hpp"
#include "../../../items/ItemStack.hpp"
#include "../../../items/Inventories.hpp"
#include "../../../items/ItemStack.hpp"
#include "../../../logic/BlocksController.hpp"
#include "../../../world/Level.hpp"
#include "api_lua.hpp"
using namespace scripting;
@@ -17,7 +16,7 @@ static void validate_itemid(itemid_t id) {
static std::shared_ptr<Inventory> get_inventory(int64_t id) {
auto inv = level->inventories->get(id);
if (inv == nullptr) {
throw std::runtime_error("inventory not found: "+std::to_string(id));
throw std::runtime_error("inventory not found: " + std::to_string(id));
}
return inv;
}
@@ -25,15 +24,19 @@ static std::shared_ptr<Inventory> get_inventory(int64_t id) {
static std::shared_ptr<Inventory> get_inventory(int64_t id, int arg) {
auto inv = level->inventories->get(id);
if (inv == nullptr) {
throw std::runtime_error("inventory not found: "+std::to_string(id)+
" argument "+std::to_string(arg));
throw std::runtime_error(
"inventory not found: " + std::to_string(id) + " argument " +
std::to_string(arg)
);
}
return inv;
}
static void validate_slotid(int slotid, Inventory* inv) {
if (static_cast<size_t>(slotid) >= inv->size()) {
throw std::runtime_error("slot index is out of range [0..inventory.size(invid)]");
throw std::runtime_error(
"slot index is out of range [0..inventory.size(invid)]"
);
}
}
@@ -128,12 +131,12 @@ static int l_inventory_move(lua::State* L) {
if (slotBid == -1) {
invB->move(slot, content->getIndices());
} else {
invB->move(slot, content->getIndices(), slotBid, slotBid+1);
invB->move(slot, content->getIndices(), slotBid, slotBid + 1);
}
return 0;
}
const luaL_Reg inventorylib [] = {
const luaL_Reg inventorylib[] = {
{"get", lua::wrap<l_inventory_get>},
{"set", lua::wrap<l_inventory_set>},
{"size", lua::wrap<l_inventory_size>},
@@ -143,6 +146,4 @@ const luaL_Reg inventorylib [] = {
{"bind_block", lua::wrap<l_inventory_bind_block>},
{"unbind_block", lua::wrap<l_inventory_unbind_block>},
{"clone", lua::wrap<l_inventory_clone>},
{NULL, NULL}
};
{NULL, NULL}};
+4 -6
View File
@@ -1,7 +1,6 @@
#include "api_lua.hpp"
#include "../../../content/Content.hpp"
#include "../../../items/ItemDef.hpp"
#include "api_lua.hpp"
using namespace scripting;
@@ -45,17 +44,16 @@ static int l_item_get_icon(lua::State* L) {
case item_icon_type::sprite:
return lua::pushstring(L, def->icon);
case item_icon_type::block:
return lua::pushstring(L, "block-previews:"+def->icon);
return lua::pushstring(L, "block-previews:" + def->icon);
}
}
return 0;
}
const luaL_Reg itemlib [] = {
const luaL_Reg itemlib[] = {
{"index", lua::wrap<l_item_index>},
{"name", lua::wrap<l_item_name>},
{"stack_size", lua::wrap<l_item_stack_size>},
{"defs_count", lua::wrap<l_item_defs_count>},
{"icon", lua::wrap<l_item_get_icon>},
{NULL, NULL}
};
{NULL, NULL}};
+3 -5
View File
@@ -1,7 +1,6 @@
#include "api_lua.hpp"
#include "../../../coders/json.hpp"
#include "../../../data/dynamic.hpp"
#include "api_lua.hpp"
static int l_json_stringify(lua::State* L) {
auto value = lua::tovalue(L, 1);
@@ -21,8 +20,7 @@ static int l_json_parse(lua::State* L) {
return lua::pushvalue(L, element);
}
const luaL_Reg jsonlib [] = {
const luaL_Reg jsonlib[] = {
{"tostring", lua::wrap<l_json_stringify>},
{"parse", lua::wrap<l_json_parse>},
{NULL, NULL}
};
{NULL, NULL}};
+43 -34
View File
@@ -1,7 +1,7 @@
#include "api_lua.hpp"
#include <sstream>
#include "api_lua.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/ext/matrix_transform.hpp>
#include <glm/gtx/matrix_decompose.hpp>
@@ -32,10 +32,13 @@ static int l_determinant(lua::State* L) {
}
/// Overloads:
/// mat4.mul(m1: float[16], m2: float[16]) -> float[16] - creates matrix of m1 and m2 multiplication result
/// mat4.mul(m1: float[16], m2: float[16], dst: float[16]) -> float[16] - updates dst matrix with m1 and m2 multiplication result
/// mat4.mul(m1: float[16], v: float[3 or 4]) -> float[3 or 4] - creates vector of m1 and v multiplication result
/// mat4.mul(m1: float[16], v: float[3 or 4], dst: float[3 or 4]) -> float[3 or 4] - updates dst vector with m1 and v multiplication result
/// mat4.mul(m1: float[16], m2: float[16]) -> float[16] - creates matrix of m1
/// and m2 multiplication result mat4.mul(m1: float[16], m2: float[16], dst:
/// float[16]) -> float[16] - updates dst matrix with m1 and m2 multiplication
/// result mat4.mul(m1: float[16], v: float[3 or 4]) -> float[3 or 4] - creates
/// vector of m1 and v multiplication result mat4.mul(m1: float[16], v: float[3
/// or 4], dst: float[3 or 4]) -> float[3 or 4] - updates dst vector with m1 and
/// v multiplication result
static int l_mul(lua::State* L) {
uint argc = lua::check_argc(L, 2, 3);
auto matrix1 = lua::tomat4(L, 1);
@@ -46,17 +49,21 @@ static int l_mul(lua::State* L) {
switch (argc) {
case 2: {
if (len2 == 4) {
return lua::pushvec4_stack(L, matrix1 * lua::tovec4(L, 2));
return lua::pushvec4_stack(L, matrix1 * lua::tovec4(L, 2));
} else if (len2 == 3) {
return lua::pushvec3_stack(L, matrix1 * glm::vec4(lua::tovec3(L, 2), 1.0f));
return lua::pushvec3_stack(
L, matrix1 * glm::vec4(lua::tovec3(L, 2), 1.0f)
);
}
return lua::pushmat4(L, matrix1 * lua::tomat4(L, 2));
}
case 3: {
if (len2 == 4) {
return lua::setvec(L, 3, matrix1 * lua::tovec4(L, 2));
return lua::setvec(L, 3, matrix1 * lua::tovec4(L, 2));
} else if (len2 == 3) {
return lua::setvec(L, 3, matrix1 * glm::vec4(lua::tovec3(L, 2), 1.0f));
return lua::setvec(
L, 3, matrix1 * glm::vec4(lua::tovec3(L, 2), 1.0f)
);
}
return lua::setmat4(L, 3, matrix1 * lua::tomat4(L, 2));
}
@@ -66,9 +73,10 @@ static int l_mul(lua::State* L) {
/// Overloads:
/// mat4.<func>(vec: float[3]) -> float[16] - creates transform matrix
/// mat4.<func>(matrix: float[16], vec: float[3]) -> float[16] - creates transformed copy of matrix
/// mat4.<func>(matrix: float[16], vec: float[3], dst: float[16]) -> sets dst to transformed version of matrix
template<glm::mat4(*func)(const glm::mat4&, const glm::vec3&)>
/// mat4.<func>(matrix: float[16], vec: float[3]) -> float[16] - creates
/// transformed copy of matrix mat4.<func>(matrix: float[16], vec: float[3],
/// dst: float[16]) -> sets dst to transformed version of matrix
template <glm::mat4 (*func)(const glm::mat4&, const glm::vec3&)>
inline int l_transform_func(lua::State* L) {
uint argc = lua::gettop(L);
switch (argc) {
@@ -87,16 +95,20 @@ inline int l_transform_func(lua::State* L) {
return lua::setmat4(L, 3, func(matrix, vec));
}
default: {
throw std::runtime_error("invalid arguments number (1, 2 or 3 expected)");
throw std::runtime_error(
"invalid arguments number (1, 2 or 3 expected)"
);
}
}
return 0;
}
/// Overloads:
/// mat4.rotate(vec: float[3], angle: float) -> float[16] - creates rotation matrix
/// mat4.rotate(matrix: float[16], vec: float[3], angle: float) -> float[16] - creates rotated copy of matrix
/// mat4.rotate(matrix: float[16], vec: float[3], angle: float, dst: float[16]) -> sets dst to rotated version of matrix
/// mat4.rotate(vec: float[3], angle: float) -> float[16] - creates rotation
/// matrix mat4.rotate(matrix: float[16], vec: float[3], angle: float) ->
/// float[16] - creates rotated copy of matrix mat4.rotate(matrix: float[16],
/// vec: float[3], angle: float, dst: float[16]) -> sets dst to rotated version
/// of matrix
inline int l_rotate(lua::State* L) {
uint argc = lua::gettop(L);
switch (argc) {
@@ -118,15 +130,18 @@ inline int l_rotate(lua::State* L) {
return lua::setmat4(L, 4, glm::rotate(matrix, angle, vec));
}
default: {
throw std::runtime_error("invalid arguments number (2, 3 or 4 expected)");
throw std::runtime_error(
"invalid arguments number (2, 3 or 4 expected)"
);
}
}
return 0;
}
/// Overloads:
/// mat4.inverse(matrix: float[16]) -> float[16] - creates inversed version of the matrix
/// mat4.inverse(matrix: float[16], dst: float[16]) -> float[16] - updates dst matrix with inversed version of the matrix
/// mat4.inverse(matrix: float[16]) -> float[16] - creates inversed version of
/// the matrix mat4.inverse(matrix: float[16], dst: float[16]) -> float[16] -
/// updates dst matrix with inversed version of the matrix
static int l_inverse(lua::State* L) {
uint argc = lua::check_argc(L, 1, 2);
auto matrix = lua::tomat4(L, 1);
@@ -142,8 +157,9 @@ static int l_inverse(lua::State* L) {
}
/// Overloads:
/// mat4.transpose(matrix: float[16]) -> float[16] - creates transposed version of the matrix
/// mat4.transpose(matrix: float[16], dst: float[16]) -> float[16] - updates dst matrix with transposed version of the matrix
/// mat4.transpose(matrix: float[16]) -> float[16] - creates transposed version
/// of the matrix mat4.transpose(matrix: float[16], dst: float[16]) -> float[16]
/// - updates dst matrix with transposed version of the matrix
static int l_transpose(lua::State* L) {
uint argc = lua::check_argc(L, 1, 2);
auto matrix = lua::tomat4(L, 1);
@@ -174,15 +190,10 @@ static int l_decompose(lua::State* L) {
glm::vec3 skew;
glm::vec4 perspective;
if (glm::decompose(
matrix,
scale,
rotation,
translation,
skew,
perspective
)) {
matrix, scale, rotation, translation, skew, perspective
)) {
lua::createtable(L, 0, 6);
lua::pushvec3(L, scale);
lua::setfield(L, "scale");
@@ -258,7 +269,7 @@ static int l_tostring(lua::State* L) {
return lua::pushstring(L, ss.str());
}
const luaL_Reg mat4lib [] = {
const luaL_Reg mat4lib[] = {
{"idt", lua::wrap<l_idt>},
{"mul", lua::wrap<l_mul>},
{"scale", lua::wrap<l_transform_func<glm::scale>>},
@@ -271,6 +282,4 @@ const luaL_Reg mat4lib [] = {
{"look_at", lua::wrap<l_look_at>},
{"from_quat", lua::wrap<l_from_quat>},
{"tostring", lua::wrap<l_tostring>},
{NULL, NULL}
};
{NULL, NULL}};
+37 -29
View File
@@ -1,29 +1,28 @@
#include "api_lua.hpp"
#include <algorithm>
#include <filesystem>
#include <stdexcept>
#include <string>
#include "../../../engine.hpp"
#include "../../../content/Content.hpp"
#include "../../../assets/AssetsLoader.hpp"
#include "../../../files/engine_paths.hpp"
#include "../../../content/Content.hpp"
#include "../../../engine.hpp"
#include "../../../files/WorldFiles.hpp"
#include "../../../files/engine_paths.hpp"
#include "../../../world/Level.hpp"
#include "../../../world/World.hpp"
#include <string>
#include <stdexcept>
#include <filesystem>
#include <algorithm>
#include "api_lua.hpp"
using namespace scripting;
static int l_pack_get_folder(lua::State* L) {
std::string packName = lua::tostring(L, 1);
if (packName == "core") {
auto folder = engine->getPaths()->getResources().u8string()+"/";
auto folder = engine->getPaths()->getResources().u8string() + "/";
return lua::pushstring(L, folder);
}
for (auto& pack : engine->getContentPacks()) {
if (pack.id == packName) {
return lua::pushstring(L, pack.folder.u8string()+"/");
return lua::pushstring(L, pack.folder.u8string() + "/");
}
}
return lua::pushstring(L, "");
@@ -54,7 +53,7 @@ static int l_pack_get_available(lua::State* L) {
manager.exclude(pack.id);
}
auto names = manager.getAllNames();
lua::createtable(L, names.size(), 0);
for (size_t i = 0; i < names.size(); i++) {
lua::pushstring(L, names[i]);
@@ -63,7 +62,9 @@ static int l_pack_get_available(lua::State* L) {
return 1;
}
static int l_pack_get_info(lua::State* L, const ContentPack& pack, const Content* content) {
static int l_pack_get_info(
lua::State* L, const ContentPack& pack, const Content* content
) {
lua::createtable(L, 0, 5);
lua::pushstring(L, pack.id);
@@ -82,10 +83,10 @@ static int l_pack_get_info(lua::State* L, const ContentPack& pack, const Content
lua::setfield(L, "version");
auto assets = engine->getAssets();
std::string icon = pack.id+".icon";
if (!AssetsLoader::loadExternalTexture(assets, icon, {
pack.folder/fs::path("icon.png")
})) {
std::string icon = pack.id + ".icon";
if (!AssetsLoader::loadExternalTexture(
assets, icon, {pack.folder / fs::path("icon.png")}
)) {
icon = "gui/no_icon";
}
@@ -98,13 +99,20 @@ static int l_pack_get_info(lua::State* L, const ContentPack& pack, const Content
auto& dpack = pack.dependencies[i];
std::string prefix;
switch (dpack.level) {
case DependencyLevel::required: prefix = "!"; break;
case DependencyLevel::optional: prefix = "?"; break;
case DependencyLevel::weak: prefix = "~"; break;
default: throw std::runtime_error("");
case DependencyLevel::required:
prefix = "!";
break;
case DependencyLevel::optional:
prefix = "?";
break;
case DependencyLevel::weak:
prefix = "~";
break;
default:
throw std::runtime_error("");
}
lua::pushfstring(L, "%s%s", prefix.c_str(), dpack.id.c_str());
lua::rawseti(L, i+1);
lua::rawseti(L, i + 1);
}
lua::setfield(L, "dependencies");
}
@@ -126,12 +134,13 @@ static int l_pack_get_info(lua::State* L, const ContentPack& pack, const Content
/// } or nil
static int l_pack_get_info(lua::State* L) {
auto packid = lua::tostring(L, 1);
auto content = engine->getContent();
auto& packs = engine->getContentPacks();
auto found = std::find_if(packs.begin(), packs.end(), [packid](const auto& pack) {
return pack.id == packid;
});
auto found =
std::find_if(packs.begin(), packs.end(), [packid](const auto& pack) {
return pack.id == packid;
});
if (found == packs.end()) {
// TODO: optimize
fs::path worldFolder("");
@@ -160,11 +169,10 @@ static int l_pack_get_base_packs(lua::State* L) {
return 1;
}
const luaL_Reg packlib [] = {
const luaL_Reg packlib[] = {
{"get_folder", lua::wrap<l_pack_get_folder>},
{"get_installed", lua::wrap<l_pack_get_installed>},
{"get_available", lua::wrap<l_pack_get_available>},
{"get_info", lua::wrap<l_pack_get_info>},
{"get_base_packs", lua::wrap<l_pack_get_base_packs>},
{NULL, NULL}
};
{NULL, NULL}};
+11 -13
View File
@@ -1,14 +1,13 @@
#include "libentity.hpp"
#include <algorithm>
#include <glm/glm.hpp>
#include "../../../world/Level.hpp"
#include "../../../objects/Player.hpp"
#include "../../../items/Inventory.hpp"
#include "../../../objects/Entities.hpp"
#include "../../../objects/Player.hpp"
#include "../../../physics/Hitbox.hpp"
#include "../../../window/Camera.hpp"
#include "../../../items/Inventory.hpp"
#include <glm/glm.hpp>
#include <algorithm>
#include "../../../world/Level.hpp"
#include "libentity.hpp"
using namespace scripting;
@@ -41,7 +40,7 @@ static int l_get_vel(lua::State* L) {
return lua::pushvec3_stack(L, hitbox->velocity);
}
}
return 0;
return 0;
}
static int l_set_vel(lua::State* L) {
@@ -155,7 +154,6 @@ static int l_get_spawnpoint(lua::State* L) {
return 0;
}
static int l_set_spawnpoint(lua::State* L) {
auto player = get_player(L, 1);
@@ -193,7 +191,8 @@ static int l_get_camera(lua::State* L) {
return 0;
}
auto found = std::find(
level->cameras.begin(), level->cameras.end(), player->currentCamera);
level->cameras.begin(), level->cameras.end(), player->currentCamera
);
if (found == level->cameras.end()) {
return 0;
}
@@ -210,7 +209,7 @@ static int l_set_camera(lua::State* L) {
return 0;
}
const luaL_Reg playerlib [] = {
const luaL_Reg playerlib[] = {
{"get_pos", lua::wrap<l_get_pos>},
{"set_pos", lua::wrap<l_set_pos>},
{"get_vel", lua::wrap<l_get_vel>},
@@ -231,5 +230,4 @@ const luaL_Reg playerlib [] = {
{"set_entity", lua::wrap<l_set_entity>},
{"get_camera", lua::wrap<l_get_camera>},
{"set_camera", lua::wrap<l_set_camera>},
{NULL, NULL}
};
{NULL, NULL}};
+4 -4
View File
@@ -32,7 +32,8 @@ static int l_tostring(lua::State* L) {
auto quat = lua::toquat(L, 1);
std::stringstream ss;
ss << "quat" << "{";
ss << "quat"
<< "{";
for (int i = 0; i < 4; i++) {
if (i > 0) {
ss << ", ";
@@ -43,9 +44,8 @@ static int l_tostring(lua::State* L) {
return lua::pushstring(L, ss.str());
}
const luaL_Reg quatlib [] = {
const luaL_Reg quatlib[] = {
{"from_mat4", lua::wrap<l_from_mat4>},
{"slerp", lua::wrap<l_slerp>},
{"tostring", lua::wrap<l_tostring>},
{NULL, NULL}
};
{NULL, NULL}};
+3 -5
View File
@@ -1,7 +1,6 @@
#include "api_lua.hpp"
#include "../../../engine.hpp"
#include "../../../window/Window.hpp"
#include "api_lua.hpp"
static int l_time_uptime(lua::State* L) {
return lua::pushnumber(L, Window::time());
@@ -11,8 +10,7 @@ static int l_time_delta(lua::State* L) {
return lua::pushnumber(L, scripting::engine->getDelta());
}
const luaL_Reg timelib [] = {
const luaL_Reg timelib[] = {
{"uptime", lua::wrap<l_time_uptime>},
{"delta", lua::wrap<l_time_delta>},
{NULL, NULL}
};
{NULL, NULL}};
+3 -5
View File
@@ -1,7 +1,6 @@
#include "api_lua.hpp"
#include "../../../coders/toml.hpp"
#include "../../../data/dynamic.hpp"
#include "api_lua.hpp"
using namespace scripting;
@@ -23,8 +22,7 @@ static int l_toml_parse(lua::State* L) {
return lua::pushvalue(L, *value);
}
const luaL_Reg tomllib [] = {
const luaL_Reg tomllib[] = {
{"tostring", lua::wrap<l_toml_stringify>},
{"parse", lua::wrap<l_toml_parse>},
{NULL, NULL}
};
{NULL, NULL}};
+38 -33
View File
@@ -1,11 +1,12 @@
#include "api_lua.hpp"
#include <sstream>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/random.hpp>
#include <glm/gtx/vector_angle.hpp>
#include <sstream>
template<typename T>
#include "api_lua.hpp"
template <typename T>
inline T angle(glm::vec<2, T> vec) {
auto val = std::atan2(vec.y, vec.x);
if (val < 0.0) {
@@ -14,19 +15,19 @@ inline T angle(glm::vec<2, T> vec) {
return val;
}
template<int n, template<class> class Op>
template <int n, template <class> class Op>
static int l_binop(lua::State* L) {
uint argc = lua::check_argc(L, 2, 3);
auto a = lua::tovec<n>(L, 1);
if (lua::isnumber(L, 2)) { // scalar second operand overload
if (lua::isnumber(L, 2)) { // scalar second operand overload
auto b = lua::tonumber(L, 2);
Op op;
if (argc == 2) {
lua::createtable(L, n, 0);
for (uint i = 0; i < n; i++) {
lua::pushnumber(L, op(a[i], b));
lua::rawseti(L, i+1);
lua::rawseti(L, i + 1);
}
return 1;
} else {
@@ -39,7 +40,7 @@ static int l_binop(lua::State* L) {
lua::createtable(L, n, 0);
for (uint i = 0; i < n; i++) {
lua::pushnumber(L, op(a[i], b[i]));
lua::rawseti(L, i+1);
lua::rawseti(L, i + 1);
}
return 1;
} else {
@@ -48,7 +49,7 @@ static int l_binop(lua::State* L) {
}
}
template<int n, glm::vec<n, float>(*func)(const glm::vec<n, float>&)>
template <int n, glm::vec<n, float> (*func)(const glm::vec<n, float>&)>
static int l_unaryop(lua::State* L) {
uint argc = lua::check_argc(L, 1, 2);
auto vec = func(lua::tovec<n>(L, 1));
@@ -57,7 +58,7 @@ static int l_unaryop(lua::State* L) {
lua::createtable(L, n, 0);
for (uint i = 0; i < n; i++) {
lua::pushnumber(L, vec[i]);
lua::rawseti(L, i+1);
lua::rawseti(L, i + 1);
}
return 1;
case 2:
@@ -66,25 +67,25 @@ static int l_unaryop(lua::State* L) {
return 0;
}
template<int n, float(*func)(const glm::vec<n, float>&)>
template <int n, float (*func)(const glm::vec<n, float>&)>
static int l_scalar_op(lua::State* L) {
lua::check_argc(L, 1);
auto vec = lua::tovec<n>(L, 1);
return lua::pushnumber(L, func(vec));
}
template<int n>
template <int n>
static int l_pow(lua::State* L) {
uint argc = lua::check_argc(L, 2, 3);
auto a = lua::tovec<n>(L, 1);
if (lua::isnumber(L, 2)) {
if (lua::isnumber(L, 2)) {
auto b = lua::tonumber(L, 2);
if (argc == 2) {
lua::createtable(L, n, 0);
for (uint i = 0; i < n; i++) {
lua::pushnumber(L, pow(a[i], b));
lua::rawseti(L, i+1);
lua::rawseti(L, i + 1);
}
return 1;
} else {
@@ -96,7 +97,7 @@ static int l_pow(lua::State* L) {
lua::createtable(L, n, 0);
for (uint i = 0; i < n; i++) {
lua::pushnumber(L, pow(a[i], b[i]));
lua::rawseti(L, i+1);
lua::rawseti(L, i + 1);
}
return 1;
} else {
@@ -105,7 +106,7 @@ static int l_pow(lua::State* L) {
}
}
template<int n>
template <int n>
static int l_dot(lua::State* L) {
lua::check_argc(L, 2);
const auto& a = lua::tovec<n>(L, 1);
@@ -113,7 +114,7 @@ static int l_dot(lua::State* L) {
return lua::pushnumber(L, glm::dot(a, b));
}
template<int n>
template <int n>
static int l_inverse(lua::State* L) {
uint argc = lua::check_argc(L, 1, 2);
auto vec = lua::tovec<n>(L, 1);
@@ -121,8 +122,8 @@ static int l_inverse(lua::State* L) {
case 1:
lua::createtable(L, n, 0);
for (uint i = 0; i < n; i++) {
lua::pushnumber(L, (-1)*vec[i]);
lua::rawseti(L, i+1);
lua::pushnumber(L, (-1) * vec[i]);
lua::rawseti(L, i + 1);
}
return 1;
case 2:
@@ -137,8 +138,11 @@ static int l_spherical_rand(lua::State* L) {
case 1:
return lua::pushvec3(L, glm::sphericalRand(lua::tonumber(L, 1)));
case 2:
return lua::setvec(L, 2,
glm::sphericalRand(static_cast<float>(lua::tonumber(L, 1))));
return lua::setvec(
L,
2,
glm::sphericalRand(static_cast<float>(lua::tonumber(L, 1)))
);
}
return 0;
}
@@ -148,12 +152,16 @@ static int l_vec2_angle(lua::State* L) {
if (argc == 1) {
return lua::pushnumber(L, glm::degrees(angle(lua::tovec2(L, 1))));
} else {
return lua::pushnumber(L, glm::degrees(angle(
glm::vec2(lua::tonumber(L, 1), lua::tonumber(L, 2)))));
return lua::pushnumber(
L,
glm::degrees(
angle(glm::vec2(lua::tonumber(L, 1), lua::tonumber(L, 2)))
)
);
}
}
template<int n>
template <int n>
static int l_tostring(lua::State* L) {
lua::check_argc(L, 1);
auto vec = lua::tovec<n>(L, 1);
@@ -169,7 +177,7 @@ static int l_tostring(lua::State* L) {
return lua::pushstring(L, ss.str());
}
const luaL_Reg vec2lib [] = {
const luaL_Reg vec2lib[] = {
{"add", lua::wrap<l_binop<2, std::plus>>},
{"sub", lua::wrap<l_binop<2, std::minus>>},
{"mul", lua::wrap<l_binop<2, std::multiplies>>},
@@ -183,10 +191,9 @@ const luaL_Reg vec2lib [] = {
{"pow", lua::wrap<l_pow<2>>},
{"dot", lua::wrap<l_dot<2>>},
{"angle", lua::wrap<l_vec2_angle>},
{NULL, NULL}
};
{NULL, NULL}};
const luaL_Reg vec3lib [] = {
const luaL_Reg vec3lib[] = {
{"add", lua::wrap<l_binop<3, std::plus>>},
{"sub", lua::wrap<l_binop<3, std::minus>>},
{"mul", lua::wrap<l_binop<3, std::multiplies>>},
@@ -200,10 +207,9 @@ const luaL_Reg vec3lib [] = {
{"pow", lua::wrap<l_pow<3>>},
{"dot", lua::wrap<l_dot<3>>},
{"spherical_rand", lua::wrap<l_spherical_rand>},
{NULL, NULL}
};
{NULL, NULL}};
const luaL_Reg vec4lib [] = {
const luaL_Reg vec4lib[] = {
{"add", lua::wrap<l_binop<4, std::plus>>},
{"sub", lua::wrap<l_binop<4, std::minus>>},
{"mul", lua::wrap<l_binop<4, std::multiplies>>},
@@ -216,5 +222,4 @@ const luaL_Reg vec4lib [] = {
{"inverse", lua::wrap<l_inverse<4>>},
{"pow", lua::wrap<l_pow<4>>},
{"dot", lua::wrap<l_dot<4>>},
{NULL, NULL}
};
{NULL, NULL}};
+15 -15
View File
@@ -1,14 +1,13 @@
#include "api_lua.hpp"
#include <cmath>
#include <filesystem>
#include "../../../assets/Assets.hpp"
#include "../../../assets/AssetsLoader.hpp"
#include "../../../engine.hpp"
#include "../../../files/engine_paths.hpp"
#include "../../../world/Level.hpp"
#include "../../../world/World.hpp"
#include "../../../engine.hpp"
#include <cmath>
#include <filesystem>
#include "api_lua.hpp"
using namespace scripting;
namespace fs = std::filesystem;
@@ -20,17 +19,19 @@ static int l_world_get_list(lua::State* L) {
lua::createtable(L, worlds.size(), 0);
for (size_t i = 0; i < worlds.size(); i++) {
lua::createtable(L, 0, 1);
auto name = worlds[i].filename().u8string();
lua::pushstring(L, name);
lua::setfield(L, "name");
auto assets = engine->getAssets();
std::string icon = "world#"+name+".icon";
if (!AssetsLoader::loadExternalTexture(assets, icon, {
worlds[i]/fs::path("icon.png"),
worlds[i]/fs::path("preview.png")
})) {
std::string icon = "world#" + name + ".icon";
if (!AssetsLoader::loadExternalTexture(
assets,
icon,
{worlds[i] / fs::path("icon.png"),
worlds[i] / fs::path("preview.png")}
)) {
icon = "gui/no_world_icon";
}
lua::pushstring(L, icon);
@@ -84,7 +85,7 @@ static int l_world_is_night(lua::State* L) {
return lua::pushboolean(L, daytime < 0.2 || daytime > 0.8);
}
const luaL_Reg worldlib [] = {
const luaL_Reg worldlib[] = {
{"get_list", lua::wrap<l_world_get_list>},
{"get_total_time", lua::wrap<l_world_get_total_time>},
{"get_day_time", lua::wrap<l_world_get_day_time>},
@@ -95,5 +96,4 @@ const luaL_Reg worldlib [] = {
{"is_day", lua::wrap<l_world_is_day>},
{"is_night", lua::wrap<l_world_is_night>},
{"exists", lua::wrap<l_world_exists>},
{NULL, NULL}
};
{NULL, NULL}};
+4 -4
View File
@@ -4,16 +4,16 @@
#include "../../../delegates.hpp"
#include "../scripting.hpp"
#ifdef __linux__
#ifdef __linux__
#include <luajit-2.1/luaconf.h>
#include <luajit-2.1/lua.hpp>
#else
#include <lua.hpp>
#endif
#include <glm/glm.hpp>
#include <string>
#include <stdexcept>
#include <string>
#ifndef LUAJIT_VERSION
#error LuaJIT required
@@ -32,4 +32,4 @@ namespace lua {
using Integer = lua_Integer;
}
#endif // LOGIC_SCRIPTING_LUA_HPP_
#endif // LOGIC_SCRIPTING_LUA_HPP_
+12 -12
View File
@@ -1,14 +1,12 @@
#include "lua_custom_types.hpp"
#include "lua_util.hpp"
#include <sstream>
#include "lua_util.hpp"
using namespace lua;
Bytearray::Bytearray(size_t capacity)
: buffer(capacity) {
Bytearray::Bytearray(size_t capacity) : buffer(capacity) {
buffer.resize(capacity);
}
@@ -32,7 +30,7 @@ static int l_bytearray_insert(lua::State* L) {
return 0;
}
auto& data = buffer->data();
auto index = tointeger(L, 2)-1;
auto index = tointeger(L, 2) - 1;
if (static_cast<size_t>(index) > data.size()) {
return 0;
}
@@ -47,11 +45,11 @@ static int l_bytearray_remove(lua::State* L) {
return 0;
}
auto& data = buffer->data();
auto index = tointeger(L, 2)-1;
auto index = tointeger(L, 2) - 1;
if (static_cast<size_t>(index) > data.size()) {
return 0;
}
data.erase(data.begin()+index);
data.erase(data.begin() + index);
return 0;
}
@@ -67,7 +65,7 @@ static int l_bytearray_meta_meta_call(lua::State* L) {
std::vector<ubyte> buffer(len);
buffer.resize(len);
for (size_t i = 0; i < len; i++) {
rawgeti(L, i+1);
rawgeti(L, i + 1);
buffer[i] = static_cast<ubyte>(tointeger(L, -1));
pop(L);
}
@@ -92,7 +90,7 @@ static int l_bytearray_meta_index(lua::State* L) {
return pushcfunction(L, found->second);
}
}
auto index = tointeger(L, 2)-1;
auto index = tointeger(L, 2) - 1;
if (static_cast<size_t>(index) > data.size()) {
return 0;
}
@@ -105,7 +103,7 @@ static int l_bytearray_meta_newindex(lua::State* L) {
return 0;
}
auto& data = buffer->data();
auto index = static_cast<size_t>(tointeger(L, 2)-1);
auto index = static_cast<size_t>(tointeger(L, 2) - 1);
auto value = tointeger(L, 3);
if (index >= data.size()) {
if (index == data.size()) {
@@ -131,7 +129,9 @@ static int l_bytearray_meta_tostring(lua::State* L) {
}
auto& data = buffer->data();
if (data.size() > 512) {
return pushstring(L, "bytearray["+std::to_string(data.size())+"]{...}");
return pushstring(
L, "bytearray[" + std::to_string(data.size()) + "]{...}"
);
} else {
std::stringstream ss;
ss << "bytearray[" << std::to_string(data.size()) << "]{";
+4 -4
View File
@@ -1,11 +1,11 @@
#ifndef LOGIC_SCRIPTING_LUA_LUA_CUSTOM_TYPES_HPP_
#define LOGIC_SCRIPTING_LUA_LUA_CUSTOM_TYPES_HPP_
#include "lua_commons.hpp"
#include <string>
#include <vector>
#include "lua_commons.hpp"
namespace lua {
class Userdata {
public:
@@ -19,7 +19,7 @@ namespace lua {
Bytearray(size_t capacity);
Bytearray(std::vector<ubyte> buffer);
virtual ~Bytearray();
const std::string& getTypeName() const override {
return TYPENAME;
}
@@ -32,4 +32,4 @@ namespace lua {
};
}
#endif // LOGIC_SCRIPTING_LUA_LUA_CUSTOM_TYPES_HPP_
#endif // LOGIC_SCRIPTING_LUA_LUA_CUSTOM_TYPES_HPP_
+12 -15
View File
@@ -1,13 +1,13 @@
#include "lua_engine.hpp"
#include "api_lua.hpp"
#include "lua_custom_types.hpp"
#include "../../../debug/Logger.hpp"
#include "../../../util/stringutil.hpp"
#include <iomanip>
#include <iostream>
#include "../../../debug/Logger.hpp"
#include "../../../util/stringutil.hpp"
#include "api_lua.hpp"
#include "lua_custom_types.hpp"
static debug::Logger logger("lua-state");
static lua::State* main_thread = nullptr;
@@ -16,7 +16,9 @@ using namespace lua;
luaerror::luaerror(const std::string& message) : std::runtime_error(message) {
}
static void remove_lib_funcs(lua::State* L, const char* libname, const char* funcs[]) {
static void remove_lib_funcs(
lua::State* L, const char* libname, const char* funcs[]
) {
if (getglobal(L, libname)) {
for (uint i = 0; funcs[i]; i++) {
pushnil(L);
@@ -78,14 +80,7 @@ void lua::initialize() {
pop(L, luaopen_bit(L));
pop(L, luaopen_os(L));
const char* removed_os[] {
"execute",
"exit",
"remove",
"rename",
"setlocale",
"tmpname",
nullptr
};
"execute", "exit", "remove", "rename", "setlocale", "tmpname", nullptr};
remove_lib_funcs(L, "os", removed_os);
create_libs(L);
@@ -107,7 +102,9 @@ void lua::finalize() {
lua_close(main_thread);
}
bool lua::emit_event(lua::State* L, const std::string &name, std::function<int(lua::State*)> args) {
bool lua::emit_event(
lua::State* L, const std::string& name, std::function<int(lua::State*)> args
) {
getglobal(L, "events");
getfield(L, "emit");
pushstring(L, name);
+10 -7
View File
@@ -1,21 +1,24 @@
#ifndef LOGIC_SCRIPTING_LUA_STATE_HPP_
#define LOGIC_SCRIPTING_LUA_STATE_HPP_
#include "lua_util.hpp"
#include <stdexcept>
#include <string>
#include "../scripting_functional.hpp"
#include "../../../data/dynamic.hpp"
#include "../../../delegates.hpp"
#include <string>
#include <stdexcept>
#include "../scripting_functional.hpp"
#include "lua_util.hpp"
namespace lua {
void initialize();
void finalize();
bool emit_event(lua::State*, const std::string& name, std::function<int(lua::State*)> args=[](auto*){return 0;});
bool emit_event(
lua::State*,
const std::string& name,
std::function<int(lua::State*)> args = [](auto*) { return 0; }
);
lua::State* get_main_thread();
}
#endif // LOGIC_SCRIPTING_LUA_STATE_HPP_
#endif // LOGIC_SCRIPTING_LUA_STATE_HPP_
+1 -3
View File
@@ -1,6 +1,5 @@
#include "api_lua.hpp"
#include "../../../debug/Logger.hpp"
#include "api_lua.hpp"
static debug::Logger logger("lua-debug");
@@ -24,7 +23,6 @@ static int l_debug_log(lua::State* L) {
void initialize_libs_extends(lua::State* L) {
if (lua::getglobal(L, "debug")) {
lua::pushcfunction(L, lua::wrap<l_debug_error>);
lua::setfield(L, "error");
+13 -13
View File
@@ -1,25 +1,25 @@
#include "api_lua.hpp"
#include <iostream>
#include "api_lua.hpp"
/// @brief Modified version of luaB_print from lbaselib.c
int l_print(lua::State* L) {
int n = lua::gettop(L); /* number of arguments */
int n = lua::gettop(L); /* number of arguments */
lua::getglobal(L, "tostring");
for (int i=1; i<=n; i++) {
lua::pushvalue(L, -1); /* function to be called */
lua::pushvalue(L, i); /* value to print */
for (int i = 1; i <= n; i++) {
lua::pushvalue(L, -1); /* function to be called */
lua::pushvalue(L, i); /* value to print */
lua::call(L, 1, 1);
const char* s = lua::tostring(L, -1); /* get result */
const char* s = lua::tostring(L, -1); /* get result */
if (s == NULL)
return luaL_error(L, LUA_QL("tostring") " must return a string to "
LUA_QL("print"));
if (i > 1)
std::cout << "\t";
return luaL_error(
L,
LUA_QL("tostring") " must return a string to " LUA_QL("print")
);
if (i > 1) std::cout << "\t";
std::cout << s;
lua::pop(L); /* pop result */
lua::pop(L); /* pop result */
}
std::cout << std::endl;
return 0;
}
+30 -23
View File
@@ -1,9 +1,9 @@
#include "lua_util.hpp"
#include "../../../util/stringutil.hpp"
#include <iostream>
#include <iomanip>
#include <iostream>
#include "../../../util/stringutil.hpp"
using namespace lua;
@@ -19,7 +19,7 @@ int lua::userdata_destructor(lua::State* L) {
}
std::string lua::env_name(int env) {
return "_ENV"+util::mangleid(env);
return "_ENV" + util::mangleid(env);
}
int lua::pushvalue(State* L, const dynamic::Value& value) {
@@ -38,7 +38,7 @@ int lua::pushvalue(State* L, const dynamic::Value& value) {
createtable(L, list->size(), 0);
for (size_t i = 0; i < list->size(); i++) {
pushvalue(L, list->get(i));
rawseti(L, i+1);
rawseti(L, i + 1);
}
} else if (auto mapptr = std::get_if<Map_sptr>(&value)) {
auto map = *mapptr;
@@ -80,8 +80,11 @@ dynamic::Value lua::tovalue(State* L, int idx) {
}
}
case LUA_TFUNCTION:
return "<function "+std::to_string(
reinterpret_cast<ptrdiff_t>(lua_topointer(L, idx)))+">";
return "<function " +
std::to_string(
reinterpret_cast<ptrdiff_t>(lua_topointer(L, idx))
) +
">";
case LUA_TSTRING:
return std::string(tostring(L, idx));
case LUA_TTABLE: {
@@ -112,19 +115,20 @@ dynamic::Value lua::tovalue(State* L, int idx) {
}
default:
throw std::runtime_error(
"lua type "+std::string(lua_typename(L, type))+" is not supported"
"lua type " + std::string(lua_typename(L, type)) +
" is not supported"
);
}
}
static int l_error_handler(lua_State* L) {
if (!isstring(L, 1)) { // 'message' not a string?
return 1; // keep it intact
if (!isstring(L, 1)) { // 'message' not a string?
return 1; // keep it intact
}
if (get_from(L, "debug", "traceback")) {
lua_pushvalue(L, 1); // pass error message
lua_pushinteger(L, 2); // skip this function and traceback
lua_call(L, 2, 1); // call debug.traceback
lua_pushvalue(L, 1); // pass error message
lua_pushinteger(L, 2); // skip this function and traceback
lua_call(L, 2, 1); // call debug.traceback
}
return 1;
}
@@ -160,7 +164,8 @@ int lua::call_nothrow(State* L, int argc, int nresults) {
void lua::dump_stack(State* L) {
int top = gettop(L);
for (int i = 1; i <= top; i++) {
std::cout << std::setw(3) << i << std::setw(20) << luaL_typename(L, i) << std::setw(30);
std::cout << std::setw(3) << i << std::setw(20) << luaL_typename(L, i)
<< std::setw(30);
switch (lua::type(L, i)) {
case LUA_TNUMBER:
std::cout << tonumber(L, i);
@@ -190,13 +195,16 @@ static std::shared_ptr<std::string> create_lambda_handler(State* L) {
setfield(L, name);
pop(L, 2);
return std::shared_ptr<std::string>(new std::string(name), [=](std::string* name) {
getglobal(L, LAMBDAS_TABLE);
pushnil(L);
setfield(L, *name);
pop(L);
delete name;
});
return std::shared_ptr<std::string>(
new std::string(name),
[=](std::string* name) {
getglobal(L, LAMBDAS_TABLE);
pushnil(L);
setfield(L, *name);
pop(L);
delete name;
}
);
}
runnable lua::create_runnable(State* L) {
@@ -231,7 +239,7 @@ int lua::create_environment(State* L, int parent) {
// local env = {}
createtable(L, 0, 1);
// setmetatable(env, {__index=_G})
createtable(L, 0, 1);
if (parent == 0) {
@@ -249,7 +257,6 @@ int lua::create_environment(State* L, int parent) {
return id;
}
void lua::removeEnvironment(State* L, int id) {
if (id == 0) {
return;
+144 -90
View File
@@ -1,31 +1,32 @@
#ifndef LOGIC_SCRIPTING_LUA_UTIL_HPP_
#define LOGIC_SCRIPTING_LUA_UTIL_HPP_
#include "lua_commons.hpp"
#include "lua_custom_types.hpp"
#include <unordered_map>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include "lua_commons.hpp"
#include "lua_custom_types.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/quaternion.hpp>
// NOTE: const std::string& used instead of string_view because c_str() needed
namespace lua {
inline std::string LAMBDAS_TABLE = "$L"; // lambdas storage
inline std::string CHUNKS_TABLE = "$C"; // precompiled lua chunks
inline std::string LAMBDAS_TABLE = "$L"; // lambdas storage
inline std::string CHUNKS_TABLE = "$C"; // precompiled lua chunks
extern std::unordered_map<std::type_index, std::string> usertypeNames;
int userdata_destructor(lua::State* L);
std::string env_name(int env);
template <lua_CFunction func> int wrap(lua_State *L) {
template <lua_CFunction func>
int wrap(lua_State* L) {
int result = 0;
try {
result = func(L);
}
// transform exception with description into lua_error
catch (std::exception &e) {
catch (std::exception& e) {
luaL_error(L, e.what());
}
// Rethrow any other exception (lua error for example)
@@ -35,7 +36,7 @@ namespace lua {
return result;
}
inline void pop(lua::State* L, int n=1) {
inline void pop(lua::State* L, int n = 1) {
lua_pop(L, n);
}
inline void insert(lua::State* L, int idx) {
@@ -59,15 +60,15 @@ namespace lua {
inline const char* type_name(lua::State* L, int idx) {
return lua_typename(L, idx);
}
inline int rawget(lua::State* L, int idx=-2) {
inline int rawget(lua::State* L, int idx = -2) {
lua_rawget(L, idx);
return 1;
}
inline int rawgeti(lua::State* L, int n, int idx=-1) {
inline int rawgeti(lua::State* L, int n, int idx = -1) {
lua_rawgeti(L, idx, n);
return 1;
}
inline void rawseti(lua::State* L, int n, int idx=-2) {
inline void rawseti(lua::State* L, int n, int idx = -2) {
lua_rawseti(L, idx, n);
}
@@ -93,7 +94,7 @@ namespace lua {
if (getglobal(L, name)) {
return 1;
} else {
throw std::runtime_error("global name "+name+" not found");
throw std::runtime_error("global name " + name + " not found");
}
}
@@ -124,27 +125,29 @@ namespace lua {
return 1;
}
template<int n>
template <int n>
inline int pushvec(lua::State* L, glm::vec<n, float> vec) {
createtable(L, n, 0);
for (int i = 0; i < n; i++) {
pushnumber(L, vec[i]);
rawseti(L, i+1);
rawseti(L, i + 1);
}
return 1;
}
template<int n>
template <int n>
inline int pushivec(lua::State* L, glm::vec<n, int> vec) {
createtable(L, n, 0);
for (int i = 0; i < n; i++) {
pushinteger(L, vec[i]);
rawseti(L, i+1);
rawseti(L, i + 1);
}
return 1;
}
inline int pushivec3_stack(lua::State* L, lua::Integer x, lua::Integer y, lua::Integer z) {
inline int pushivec3_stack(
lua::State* L, lua::Integer x, lua::Integer y, lua::Integer z
) {
pushinteger(L, x);
pushinteger(L, y);
pushinteger(L, z);
@@ -172,7 +175,7 @@ namespace lua {
return 4;
}
inline void setmetatable(lua::State* L, int idx=-2) {
inline void setmetatable(lua::State* L, int idx = -2) {
lua_setmetatable(L, idx);
}
inline int pushvalue(lua::State* L, int idx) {
@@ -191,14 +194,14 @@ namespace lua {
return pushvec(L, vec);
}
inline int pushcolor(lua::State* L, glm::vec4 vec) {
return pushivec(L, glm::ivec4(vec*255.0f));
return pushivec(L, glm::ivec4(vec * 255.0f));
}
inline int pushquat(lua::State* L, glm::quat quat) {
createtable(L, 4, 0);
for (size_t i = 0; i < 4; i++) {
pushnumber(L, quat[i]);
rawseti(L, i+1);
rawseti(L, i + 1);
}
return 1;
}
@@ -206,7 +209,7 @@ namespace lua {
pushvalue(L, idx);
for (int i = 0; i < 4; i++) {
pushnumber(L, quat[i]);
rawseti(L, i+1);
rawseti(L, i + 1);
}
return 1;
}
@@ -216,29 +219,29 @@ namespace lua {
for (uint x = 0; x < 4; x++) {
uint i = y * 4 + x;
pushnumber(L, matrix[y][x]);
rawseti(L, i+1);
rawseti(L, i + 1);
}
}
return 1;
}
/// @brief pushes matrix table to the stack and updates it with glm matrix
/// @brief pushes matrix table to the stack and updates it with glm matrix
inline int setmat4(lua::State* L, int idx, glm::mat4 matrix) {
pushvalue(L, idx);
for (uint y = 0; y < 4; y++) {
for (uint x = 0; x < 4; x++) {
uint i = y * 4 + x;
pushnumber(L, matrix[y][x]);
rawseti(L, i+1);
rawseti(L, i + 1);
}
}
return 1;
}
template<int n>
template <int n>
inline int setvec(lua::State* L, int idx, glm::vec<n, float> vec) {
pushvalue(L, idx);
for (int i = 0; i < n; i++) {
pushnumber(L, vec[i]);
rawseti(L, i+1);
rawseti(L, i + 1);
}
return 1;
}
@@ -251,8 +254,8 @@ namespace lua {
return 1;
}
template<typename... Args>
inline int pushfstring(lua_State *L, const char * fmt, Args... args) {
template <typename... Args>
inline int pushfstring(lua_State* L, const char* fmt, Args... args) {
lua_pushfstring(L, fmt, args...);
return 1;
}
@@ -287,7 +290,7 @@ namespace lua {
inline bool isuserdata(lua::State* L, int idx) {
return lua_isuserdata(L, idx);
}
inline void setfield(lua::State* L, const std::string& name, int idx=-2) {
inline void setfield(lua::State* L, const std::string& name, int idx = -2) {
lua_setfield(L, idx, name.c_str());
}
inline bool toboolean(lua::State* L, int idx) {
@@ -308,28 +311,30 @@ namespace lua {
inline void setglobal(lua::State* L, const std::string& name) {
lua_setglobal(L, name.c_str());
}
template<class T>
template <class T>
inline T* touserdata(lua::State* L, int idx) {
if (void* rawptr = lua_touserdata(L, idx)) {
return static_cast<T*>(rawptr);
}
return nullptr;
}
template<class T, typename... Args>
template <class T, typename... Args>
inline int newuserdata(lua::State* L, Args&&... args) {
const auto& found = usertypeNames.find(typeid(T));
void* ptr = lua_newuserdata(L, sizeof(T));
new (ptr) T(args...);
if (found == usertypeNames.end()) {
log_error("usertype is not registred: "+std::string(typeid(T).name()));
log_error(
"usertype is not registred: " + std::string(typeid(T).name())
);
} else if (getglobal(L, found->second)) {
setmetatable(L);
}
return 1;
}
template<class T, lua_CFunction func>
template <class T, lua_CFunction func>
inline void newusertype(lua::State* L, const std::string& name) {
usertypeNames[typeid(T)] = name;
func(L);
@@ -338,63 +343,74 @@ namespace lua {
setfield(L, "__gc");
setglobal(L, name);
}
}
template<int n>
inline glm::vec<n, float> tovec(lua::State* L, int idx) {
template <int n>
inline glm::vec<n, float> tovec(lua::State* L, int idx) {
pushvalue(L, idx);
if (!istable(L, idx) || objlen(L, idx) < n) {
throw std::runtime_error("value must be an array of "+std::to_string(n)+" numbers");
throw std::runtime_error(
"value must be an array of " + std::to_string(n) + " numbers"
);
}
glm::vec<n, float> vec;
for (int i = 0; i < n; i++) {
rawgeti(L, i+1);
vec[i] = tonumber(L, -1);
rawgeti(L, i + 1);
vec[i] = tonumber(L, -1);
pop(L);
}
pop(L);
return vec;
}
inline glm::vec2 tovec2(lua::State* L, int idx) {
inline glm::vec2 tovec2(lua::State* L, int idx) {
pushvalue(L, idx);
if (!istable(L, idx) || objlen(L, idx) < 2) {
throw std::runtime_error("value must be an array of two numbers");
}
rawgeti(L, 1);
auto x = tonumber(L, -1); pop(L);
auto x = tonumber(L, -1);
pop(L);
rawgeti(L, 2);
auto y = tonumber(L, -1); pop(L);
auto y = tonumber(L, -1);
pop(L);
pop(L);
return glm::vec2(x, y);
}
inline glm::vec3 tovec3(lua::State* L, int idx) {
inline glm::vec3 tovec3(lua::State* L, int idx) {
pushvalue(L, idx);
if (!istable(L, idx) || objlen(L, idx) < 3) {
throw std::runtime_error("value must be an array of three numbers");
}
rawgeti(L, 1);
auto x = tonumber(L, -1); pop(L);
auto x = tonumber(L, -1);
pop(L);
rawgeti(L, 2);
auto y = tonumber(L, -1); pop(L);
auto y = tonumber(L, -1);
pop(L);
rawgeti(L, 3);
auto z = tonumber(L, -1); pop(L);
auto z = tonumber(L, -1);
pop(L);
pop(L);
return glm::vec3(x, y, z);
}
inline glm::vec4 tovec4(lua::State* L, int idx) {
inline glm::vec4 tovec4(lua::State* L, int idx) {
pushvalue(L, idx);
if (!istable(L, idx) || objlen(L, idx) < 4) {
throw std::runtime_error("value must be an array of four numbers");
}
rawgeti(L, 1);
auto x = tonumber(L, -1); pop(L);
auto x = tonumber(L, -1);
pop(L);
rawgeti(L, 2);
auto y = tonumber(L, -1); pop(L);
auto y = tonumber(L, -1);
pop(L);
rawgeti(L, 3);
auto z = tonumber(L, -1); pop(L);
auto z = tonumber(L, -1);
pop(L);
rawgeti(L, 4);
auto w = tonumber(L, -1); pop(L);
auto w = tonumber(L, -1);
pop(L);
pop(L);
return glm::vec4(x, y, z, w);
}
@@ -405,25 +421,32 @@ namespace lua {
throw std::runtime_error("value must be an array of four numbers");
}
rawgeti(L, 1);
auto x = tonumber(L, -1); pop(L);
auto x = tonumber(L, -1);
pop(L);
rawgeti(L, 2);
auto y = tonumber(L, -1); pop(L);
auto y = tonumber(L, -1);
pop(L);
rawgeti(L, 3);
auto z = tonumber(L, -1); pop(L);
auto z = tonumber(L, -1);
pop(L);
rawgeti(L, 4);
auto w = tonumber(L, -1); pop(L);
auto w = tonumber(L, -1);
pop(L);
pop(L);
return glm::quat(x, y, z, w);
}
inline glm::vec3 tovec3_stack(lua::State* L, int idx) {
inline glm::vec3 tovec3_stack(lua::State* L, int idx) {
return glm::vec3(
tonumber(L, idx), tonumber(L, idx+1), tonumber(L, idx+2)
tonumber(L, idx), tonumber(L, idx + 1), tonumber(L, idx + 2)
);
}
inline glm::vec4 tovec4_stack(lua::State* L, int idx) {
inline glm::vec4 tovec4_stack(lua::State* L, int idx) {
return glm::vec4(
tonumber(L, idx), tonumber(L, idx+1), tonumber(L, idx+2), tonumber(L, idx+3)
tonumber(L, idx),
tonumber(L, idx + 1),
tonumber(L, idx + 2),
tonumber(L, idx + 3)
);
}
inline glm::mat4 tomat4(lua::State* L, int idx) {
@@ -435,7 +458,7 @@ namespace lua {
for (uint y = 0; y < 4; y++) {
for (uint x = 0; x < 4; x++) {
uint i = y * 4 + x;
rawgeti(L, i+1);
rawgeti(L, i + 1);
matrix[y][x] = static_cast<float>(tonumber(L, -1));
pop(L);
}
@@ -450,21 +473,25 @@ namespace lua {
throw std::runtime_error("RGBA array required");
}
rawgeti(L, 1);
auto r = tonumber(L, -1); pop(L);
rawgeti(L, 2);
auto g = tonumber(L, -1); pop(L);
rawgeti(L, 3);
auto b = tonumber(L, -1); pop(L);
rawgeti(L, 4);
auto a = tonumber(L, -1); pop(L);
auto r = tonumber(L, -1);
pop(L);
return glm::vec4(r/255, g/255, b/255, a/255);
rawgeti(L, 2);
auto g = tonumber(L, -1);
pop(L);
rawgeti(L, 3);
auto b = tonumber(L, -1);
pop(L);
rawgeti(L, 4);
auto a = tonumber(L, -1);
pop(L);
pop(L);
return glm::vec4(r / 255, g / 255, b / 255, a / 255);
}
int pushvalue(lua::State*, const dynamic::Value& value);
dynamic::Value tovalue(lua::State*, int idx);
inline bool getfield(lua::State* L, const std::string& name, int idx=-1) {
inline bool getfield(lua::State* L, const std::string& name, int idx = -1) {
lua_getfield(L, idx, name.c_str());
if (isnil(L, -1)) {
pop(L);
@@ -473,7 +500,7 @@ namespace lua {
return true;
}
inline bool hasfield(lua::State* L, const std::string& name, int idx=-1) {
inline bool hasfield(lua::State* L, const std::string& name, int idx = -1) {
lua_getfield(L, idx, name.c_str());
if (isnil(L, -1)) {
pop(L);
@@ -485,21 +512,23 @@ namespace lua {
inline const char* require_string(lua::State* L, int idx) {
if (!isstring(L, idx)) {
throw luaerror("string expected at "+std::to_string(idx));
throw luaerror("string expected at " + std::to_string(idx));
}
return tostring(L, idx);
}
std::wstring require_wstring(lua::State*, int idx);
inline bool rename(lua::State* L, const std::string& from, const std::string& to) {
inline bool rename(
lua::State* L, const std::string& from, const std::string& to
) {
getglobal(L, from);
if (isnil(L, -1)) {
pop(L, 1);
return false;
}
setglobal(L, to);
// remove previous
pushnil(L);
setglobal(L, from);
@@ -511,11 +540,13 @@ namespace lua {
setglobal(L, name);
}
inline int setfenv(lua::State* L, int idx=-2) {
inline int setfenv(lua::State* L, int idx = -2) {
return lua_setfenv(L, idx);
}
inline void loadbuffer(lua::State* L, int env, const std::string& src, const std::string& file) {
inline void loadbuffer(
lua::State* L, int env, const std::string& src, const std::string& file
) {
if (luaL_loadbuffer(L, src.c_str(), src.length(), file.c_str())) {
throw luaerror(tostring(L, -1));
}
@@ -524,45 +555,64 @@ namespace lua {
}
}
inline void store_in(lua::State* L, const std::string& tableName, const std::string& name) {
inline void store_in(
lua::State* L, const std::string& tableName, const std::string& name
) {
if (getglobal(L, tableName)) {
pushvalue(L, -2);
setfield(L, name);
pop(L, 2);
} else {
throw std::runtime_error("table "+tableName+" not found");
throw std::runtime_error("table " + tableName + " not found");
}
}
inline int get_from(lua::State* L, const std::string& tableName, const std::string& name, bool required=false) {
inline int get_from(
lua::State* L,
const std::string& tableName,
const std::string& name,
bool required = false
) {
if (getglobal(L, tableName)) {
if (getfield(L, name)) {
return 1;
} else if (required) {
throw std::runtime_error("table "+tableName+" has no member "+name);
throw std::runtime_error(
"table " + tableName + " has no member " + name
);
}
return 0;
} else {
throw std::runtime_error("table "+tableName+" not found");
throw std::runtime_error("table " + tableName + " not found");
}
}
int call(lua::State*, int argc, int nresults=-1);
int call_nothrow(lua::State*, int argc, int nresults=1);
int call(lua::State*, int argc, int nresults = -1);
int call_nothrow(lua::State*, int argc, int nresults = 1);
inline int eval(lua::State* L, int env, const std::string& src, const std::string& file="<eval>") {
auto srcText = "return "+src;
inline int eval(
lua::State* L,
int env,
const std::string& src,
const std::string& file = "<eval>"
) {
auto srcText = "return " + src;
loadbuffer(L, env, srcText, file);
return call(L, 0);
}
inline int execute(lua::State* L, int env, const std::string& src, const std::string& file="<eval>") {
inline int execute(
lua::State* L,
int env,
const std::string& src,
const std::string& file = "<eval>"
) {
loadbuffer(L, env, src, file);
return call_nothrow(L, 0);
}
runnable create_runnable(lua::State*);
scripting::common_func create_lambda(lua::State* );
scripting::common_func create_lambda(lua::State*);
inline int pushenv(lua::State* L, int env) {
if (getglobal(L, env_name(env))) {
@@ -574,16 +624,20 @@ namespace lua {
void removeEnvironment(lua::State*, int id);
void dump_stack(lua::State*);
inline void addfunc(lua::State* L, const std::string& name, lua_CFunction func) {
inline void addfunc(
lua::State* L, const std::string& name, lua_CFunction func
) {
pushcfunction(L, func);
setglobal(L, name);
}
inline void openlib(lua::State* L, const std::string& name, const luaL_Reg* libfuncs) {
inline void openlib(
lua::State* L, const std::string& name, const luaL_Reg* libfuncs
) {
createtable(L, 0, 0);
luaL_setfuncs(L, libfuncs, 0);
setglobal(L, name);
}
}
#endif // LOGIC_SCRIPTING_LUA_UTIL_HPP_
#endif // LOGIC_SCRIPTING_LUA_UTIL_HPP_