New scripting features

This commit is contained in:
MihailRis
2024-01-12 12:16:54 +03:00
parent 05fe86e64c
commit faabb8178a
10 changed files with 129 additions and 43 deletions
+52 -17
View File
@@ -6,19 +6,49 @@
#include "../../physics/Hitbox.h"
#include "../../objects/Player.h"
#include "../../world/Level.h"
#include "../../world/World.h"
#include "../../content/Content.h"
#include "../../voxels/Block.h"
#include "../../voxels/Chunks.h"
#include "../../voxels/voxel.h"
#include "../../lighting/Lighting.h"
#include "../../logic/BlocksController.h"
int l_block_name(lua_State* L) {
static int l_world_get_day_time(lua_State* L) {
lua_pushnumber(L, scripting::level->world->daytime);
return 1;
}
static int l_world_set_day_time(lua_State* L) {
double value = scripting::level->world->daytime;
scripting::level->world->daytime = fmod(value, 1.0);
return 0;
}
static int l_world_get_seed(lua_State* L) {
lua_pushinteger(L, scripting::level->world->seed);
return 1;
}
static const luaL_Reg worldlib [] = {
{"get_day_time", l_world_get_day_time},
{"set_day_time", l_world_set_day_time},
{"get_seed", l_world_get_seed},
{NULL, NULL}
};
int luaopen_world(lua_State* L) {
luaL_openlib(L, "world", worldlib, 0);
return 1;
}
static int l_block_name(lua_State* L) {
int id = lua_tointeger(L, 1);
lua_pushstring(L, scripting::content->indices->getBlockDef(id)->name.c_str());
return 1;
}
int l_is_solid_at(lua_State* L) {
static int l_is_solid_at(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
@@ -27,29 +57,32 @@ int l_is_solid_at(lua_State* L) {
return 1;
}
int l_blocks_count(lua_State* L) {
static int l_blocks_count(lua_State* L) {
lua_pushinteger(L, scripting::content->indices->countBlockDefs());
return 1;
}
int l_block_index(lua_State* L) {
static int l_block_index(lua_State* L) {
auto name = lua_tostring(L, 1);
lua_pushinteger(L, scripting::content->requireBlock(name)->rt.id);
return 1;
}
int l_set_block(lua_State* L) {
static int l_set_block(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
int id = lua_tointeger(L, 4);
int states = lua_tointeger(L, 5);
bool noupdate = lua_toboolean(L, 6);
scripting::level->chunks->set(x, y, z, id, states);
scripting::level->lighting->onBlockSet(x,y,z, id);
if (!noupdate)
scripting::blocks->updateSides(x, y, z);
return 0;
}
int l_get_block(lua_State* L) {
static int l_get_block(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
@@ -66,7 +99,7 @@ inline int lua_pushivec3(lua_State* L, int x, int y, int z) {
return 3;
}
int l_get_block_x(lua_State* L) {
static int l_get_block_x(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
@@ -83,7 +116,7 @@ int l_get_block_x(lua_State* L) {
}
}
int l_get_block_y(lua_State* L) {
static int l_get_block_y(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
@@ -100,7 +133,7 @@ int l_get_block_y(lua_State* L) {
}
}
int l_get_block_z(lua_State* L) {
static int l_get_block_z(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
@@ -117,7 +150,7 @@ int l_get_block_z(lua_State* L) {
}
}
int l_get_player_pos(lua_State* L) {
static int l_get_player_pos(lua_State* L) {
glm::vec3 pos = scripting::level->player->hitbox->position;
lua_pushnumber(L, pos.x);
lua_pushnumber(L, pos.y);
@@ -125,14 +158,14 @@ int l_get_player_pos(lua_State* L) {
return 3;
}
int l_get_player_rot(lua_State* L) {
static int l_get_player_rot(lua_State* L) {
glm::vec2 rot = scripting::level->player->cam;
lua_pushnumber(L, rot.x);
lua_pushnumber(L, rot.y);
return 2;
}
int l_set_player_rot(lua_State* L) {
static int l_set_player_rot(lua_State* L) {
double x = lua_tonumber(L, 1);
double y = lua_tonumber(L, 2);
glm::vec2& cam = scripting::level->player->cam;
@@ -141,7 +174,7 @@ int l_set_player_rot(lua_State* L) {
return 0;
}
int l_set_player_pos(lua_State* L) {
static int l_set_player_pos(lua_State* L) {
double x = lua_tonumber(L, 1);
double y = lua_tonumber(L, 2);
double z = lua_tonumber(L, 3);
@@ -149,7 +182,7 @@ int l_set_player_pos(lua_State* L) {
return 0;
}
int l_get_block_states(lua_State* L) {
static int l_get_block_states(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
@@ -159,7 +192,7 @@ int l_get_block_states(lua_State* L) {
return 1;
}
int l_get_block_user_bits(lua_State* L) {
static int l_get_block_user_bits(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
@@ -177,7 +210,7 @@ int l_get_block_user_bits(lua_State* L) {
return 1;
}
int l_set_block_user_bits(lua_State* L) {
static int l_set_block_user_bits(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
@@ -195,7 +228,7 @@ int l_set_block_user_bits(lua_State* L) {
return 0;
}
int l_is_replaceable_at(lua_State* L) {
static int l_is_replaceable_at(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
@@ -208,6 +241,8 @@ int l_is_replaceable_at(lua_State* L) {
lua_setglobal(L, NAME))
void apilua::create_funcs(lua_State* L) {
luaopen_world(L);
lua_addfunc(L, l_block_index, "block_index");
lua_addfunc(L, l_block_name, "block_name");
lua_addfunc(L, l_blocks_count, "blocks_count");