voxel long

This commit is contained in:
alexei-zebra 2024-01-09 18:58:58 +03:00
parent 3542a00445
commit 0175a8b76c
54 changed files with 615 additions and 593 deletions

View file

@ -57,7 +57,7 @@ void BlocksController::updateSides(int x, int y, int z) {
}
void BlocksController::breakBlock(Player* player, const Block* def, int x, int y, int z) {
chunks->set(x,y,z, voxel{0,0});
chunks->set(x,y,z, voxel());
lighting->onBlockSet(x,y,z, 0);
if (def->rt.funcsset.onbroken) {
scripting::on_block_broken(player, def, x, y, z);
@ -75,10 +75,9 @@ void BlocksController::updateBlock(int x, int y, int z) {
return;
}
if (def->rt.funcsset.update) {
int states = vox->states;
int id = vox->id;
voxel tmp = *vox;
scripting::update_block(def, x, y, z);
if (states != vox->states || id!= vox->id)
if (tmp != *vox)
updateSides(x, y, z);
}
}

View file

@ -229,7 +229,6 @@ void PlayerController::updateInteraction(){
if (vox != nullptr){
player->selectedVoxel = *vox;
selectedBlockId = vox->id;
selectedBlockStates = vox->states;
selectedBlockPosition = iend;
selectedPointPosition = end;
selectedBlockNormal = norm;
@ -242,21 +241,21 @@ void PlayerController::updateInteraction(){
if (def->rotatable){
const std::string& name = def->rotations.name;
if (name == "pipe") {
if (norm.x < 0.0f) voxel.setDir(BLOCK_DIR_WEST);
else if (norm.x > 0.0f) voxel.setDir(BLOCK_DIR_EAST);
else if (norm.y > 0.0f) voxel.setDir(BLOCK_DIR_UP);
else if (norm.y < 0.0f) voxel.setDir(BLOCK_DIR_DOWN);
else if (norm.z > 0.0f) voxel.setDir(BLOCK_DIR_NORTH);
else if (norm.z < 0.0f) voxel.setDir(BLOCK_DIR_SOUTH);
if (norm.x < 0.0f) voxel.dir = (voxel_t)VOX_DIR::WEST;
else if (norm.x > 0.0f) voxel.dir = (voxel_t)VOX_DIR::EAST;
else if (norm.y > 0.0f) voxel.dir = (voxel_t)VOX_DIR::UP;
else if (norm.y < 0.0f) voxel.dir = (voxel_t)VOX_DIR::DOWN;
else if (norm.z > 0.0f) voxel.dir = (voxel_t)VOX_DIR::NORTH;
else if (norm.z < 0.0f) voxel.dir = (voxel_t)VOX_DIR::SOUTH;
} else if (name == "pane") {
glm::vec3 vec = camera->dir;
if (abs(vec.x) > abs(vec.z)){
if (vec.x > 0.0f) voxel.setDir(BLOCK_DIR_EAST);
if (vec.x < 0.0f) voxel.setDir(BLOCK_DIR_WEST);
if (vec.x > 0.0f) voxel.dir = (voxel_t)VOX_DIR::EAST;
if (vec.x < 0.0f) voxel.dir = (voxel_t)VOX_DIR::WEST;
}
if (abs(vec.x) < abs(vec.z)){
if (vec.z > 0.0f) voxel.setDir(BLOCK_DIR_SOUTH);
if (vec.z < 0.0f) voxel.setDir(BLOCK_DIR_NORTH);
if (vec.z > 0.0f) voxel.dir = (voxel_t)VOX_DIR::SOUTH;
if (vec.z < 0.0f) voxel.dir = (voxel_t)VOX_DIR::NORTH;
}
}
}
@ -300,7 +299,7 @@ void PlayerController::updateInteraction(){
player->chosenBlock = chunks->get(x,y,z)->id;
}
} else {
player->selectedVoxel = voxel{0, 0};
player->selectedVoxel = voxel();
selectedBlockId = -1;
selectedBlockStates = 0;
}

View file

@ -2,22 +2,115 @@
#include "scripting.h"
#include <glm/glm.hpp>
#include <bit>
#include "../../physics/Hitbox.h"
#include "../../objects/Player.h"
#include "../../world/Level.h"
#include "../../objects/Player.h"
#include "../../physics/Hitbox.h"
#include "../../window/Camera.h"
#include "../../content/Content.h"
#include "../../voxels/Block.h"
#include "../../voxels/Chunks.h"
#include "../../voxels/voxel.h"
#include "../../lighting/Lighting.h"
int l_block_name(lua_State* L) {
/**
* ,--------,
*: | PLAYER |
* |--------'--------------------------------------,
* | get_player_pos(): |
* | return x:float, y:float, z:float |
* | |
* | set_player_pos(x:float, y:float, z:float) |
* | |
* | get_player_rot(): |
* | return x:float, y:float |
* | |
* | set_player_rot(x:float, y:float) |
* | |
* | get_player_front(): |
* | return x:float, y:float, z:float |
* | |
*TODO | set_player_front(x:float, y:float, z:float) |
* '-----------------------------------------------'
*/
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);
lua_pushnumber(L, pos.z);
return 3;
}
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);
scripting::level->player->hitbox->position = glm::vec3(x, y, z);
return 0;
}
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) {
double x = lua_tonumber(L, 1);
double y = lua_tonumber(L, 2);
scripting::level->player->cam = glm::vec2(x, y);
return 0;
}
int l_get_player_front(lua_State* L) {
glm::vec3 front = scripting::level->player->camera->front;
lua_pushnumber(L, front.x);
lua_pushnumber(L, front.y);
lua_pushnumber(L, front.z);
return 3;
}
// TODO: set_player_front(x:float, y:float, z:float);
/**
* ,--------,
*: | VOXELS |
* |--------'---------------------------------------,
* | pars_vox(vox:long): |
* | return id:short, variant:char, |
* | dir:char, signal:char, states:int |
* | |
* | creat_vox(id:short, variant:char, dir:char, |
* | signal:char, states:int): |
* | return vox:long |
* | |
* | idv_2_name(id:short, variant:char): |
* | return name:str |
* | |
* | name_2_idv(name:str): |
* | return id:short, variant:char |
* '------------------------------------------------'
*/
int l_block_id_2_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_block_name_2_id(lua_State* L) {
auto name = lua_tostring(L, 1);
lua_pushinteger(L, scripting::content->requireBlock(name)->rt.id);
return 1;
}
int l_is_solid_at(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
@ -32,21 +125,14 @@ int l_blocks_count(lua_State* L) {
return 1;
}
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) {
voxel voxel;
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
voxel.id = lua_tointeger(L, 4);
voxel.states = lua_tointeger(L, 5);
scripting::level->chunks->set(x, y, z, voxel);
scripting::level->lighting->onBlockSet(x,y,z, voxel.id);
voxel vox = std::bit_cast<voxel>(lua_tointeger(L, 4));
scripting::level->chunks->set(x, y, z, vox);
scripting::level->lighting->onBlockSet(x,y,z, vox.id);
return 0;
}
@ -55,68 +141,8 @@ int l_get_block(lua_State* L) {
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z);
int id = vox == nullptr ? -1 : vox->id;
lua_pushinteger(L, id);
return 1;
}
inline int lua_pushivec3(lua_State* L, int x, int y, int z) {
lua_pushinteger(L, x);
lua_pushinteger(L, y);
lua_pushinteger(L, z);
return 3;
}
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);
lua_pushnumber(L, pos.z);
return 3;
}
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) {
double x = lua_tonumber(L, 1);
double y = lua_tonumber(L, 2);
glm::vec2& cam = scripting::level->player->cam;
cam.x = x;
cam.y = y;
return 0;
}
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);
scripting::level->player->hitbox->position = glm::vec3(x, y, z);
return 0;
}
// old functions, not recommended to use
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);
voxel* vox = scripting::level->chunks->get(x, y, z);
int states = vox == nullptr ? 0 : vox->states;
lua_pushinteger(L, states);
return 1;
}
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);
lua_pushboolean(L, scripting::level->chunks->isReplaceableBlock(x, y, z));
voxel_t vox_ = std::bit_cast<voxel_t>(vox);
lua_pushinteger(L, vox_);
return 1;
}
@ -126,8 +152,7 @@ int l_get_block_dir(lua_State* L) {
y = lua_tointeger(L, 2),
z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z);
int states = vox->getDir();
lua_pushinteger(L, states);
lua_pushinteger(L, vox->dir);
return 1;
}
int l_set_block_dir(lua_State* L) {
@ -136,7 +161,7 @@ int l_set_block_dir(lua_State* L) {
z = lua_tointeger(L, 3),
s = lua_tointeger(L, 4);
voxel* vox = scripting::level->chunks->get(x, y, z);
vox->setDir(s);
vox->dir = s;
return 0;
}
@ -145,61 +170,19 @@ int l_get_block_sig(lua_State* L) {
y = lua_tointeger(L, 2),
z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z);
bool sig = vox->getSig();
lua_pushboolean(L, sig);
lua_pushinteger(L, vox->signal);
return 1;
}
int l_set_block_sig(lua_State* L) {
int x = lua_tointeger(L, 1),
y = lua_tointeger(L, 2),
z = lua_tointeger(L, 3);
bool s = lua_toboolean(L, 4);
bool sig = lua_tointeger(L, 4);
voxel* vox = scripting::level->chunks->get(x, y, z);
vox->setSig(s);
vox->signal = sig;
return 0;
}
int l_get_block_custom_state(lua_State* L) {
int x = lua_tointeger(L, 1),
y = lua_tointeger(L, 2),
z = lua_tointeger(L, 3),
i = lua_tointeger(L, 4);
voxel* vox = scripting::level->chunks->get(x, y, z);
bool state = vox->getCustomState(i);
lua_pushboolean(L, state);
return 1;
}
int l_set_block_custom_state(lua_State* L) {
int x = lua_tointeger(L, 1),
y = lua_tointeger(L, 2),
z = lua_tointeger(L, 3),
i = lua_tointeger(L, 4);
bool s = lua_toboolean(L, 5);
voxel* vox = scripting::level->chunks->get(x, y, z);
vox->setCustomState(i, s);
return 0;
}
int l_get_block_custom_value(lua_State* L) {
int x = lua_tointeger(L, 1),
y = lua_tointeger(L, 2),
z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z);
int state = vox->getCustomValue();
lua_pushinteger(L, state);
return 1;
}
int l_set_block_custom_value(lua_State* L) {
int x = lua_tointeger(L, 1),
y = lua_tointeger(L, 2),
z = lua_tointeger(L, 3),
v = lua_tointeger(L, 4);
voxel* vox = scripting::level->chunks->get(x, y, z);
vox->setCustomValue(v);
return 0;
}
// for specific modes
int l_get_block_custom_bits(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
@ -228,6 +211,26 @@ int l_set_block_custom_bits(lua_State* L) {
return 0;
}
// old functions, not recommended to use
// 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);
// voxel* vox = scripting::level->chunks->get(x, y, z);
// int states = vox == nullptr ? 0 : vox->states;
// lua_pushinteger(L, states);
// return 1;
// }
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);
lua_pushboolean(L, scripting::level->chunks->isReplaceableBlock(x, y, z));
return 1;
}
inline int lua_pushivec3(lua_State* L, int x, int y, int z) {
lua_pushinteger(L, x);
@ -248,7 +251,7 @@ int l_get_block_x(lua_State* L) {
if (!def->rotatable) {
return lua_pushivec3(L, 1, 0, 0);
} else {
const CoordSystem& rot = def->rotations.variants[vox->getDir()];
const CoordSystem& rot = def->rotations.variants[vox->dir];
return lua_pushivec3(L, rot.axisX.x, rot.axisX.y, rot.axisX.z);
}
}
@ -264,7 +267,7 @@ int l_get_block_y(lua_State* L) {
if (!def->rotatable) {
return lua_pushivec3(L, 0, 1, 0);
} else {
const CoordSystem& rot = def->rotations.variants[vox->getDir()];
const CoordSystem& rot = def->rotations.variants[vox->dir];
return lua_pushivec3(L, rot.axisY.x, rot.axisY.y, rot.axisY.z);
}
}
@ -280,7 +283,7 @@ int l_get_block_z(lua_State* L) {
if (!def->rotatable) {
return lua_pushivec3(L, 0, 0, 1);
} else {
const CoordSystem& rot = def->rotations.variants[vox->getDir()];
const CoordSystem& rot = def->rotations.variants[vox->dir];
return lua_pushivec3(L, rot.axisZ.x, rot.axisZ.y, rot.axisZ.z);
}
}
@ -290,8 +293,12 @@ int l_get_block_z(lua_State* L) {
lua_setglobal(L, NAME))
void apilua::create_funcs(lua_State* L) {
lua_addfunc(L, l_block_index, "block_index");
lua_addfunc(L, l_block_name, "block_name");
// old functions
lua_addfunc(L, l_block_id_2_name, "block_index");
lua_addfunc(L, l_block_name_2_id, "block_name");
lua_addfunc(L, l_block_id_2_name, "block_id_2_name");
lua_addfunc(L, l_block_name_2_id, "block_name_2_id");
lua_addfunc(L, l_blocks_count, "blocks_count");
lua_addfunc(L, l_is_solid_at, "is_solid_at");
lua_addfunc(L, l_is_replaceable_at, "is_replaceable_at");
@ -305,14 +312,10 @@ void apilua::create_funcs(lua_State* L) {
lua_addfunc(L, l_get_player_rot, "get_player_rot");
lua_addfunc(L, l_set_player_rot, "set_player_rot");
lua_addfunc(L, l_get_block_states, "get_block_states"); // old function
// lua_addfunc(L, l_get_block_states, "get_block_states"); // old function
lua_addfunc(L, l_get_block_dir, "get_block_dir");
lua_addfunc(L, l_set_block_dir, "set_block_dir");
lua_addfunc(L, l_get_block_custom_state, "get_block_custom_state");
lua_addfunc(L, l_set_block_custom_state, "set_block_custom_state");
lua_addfunc(L, l_get_block_custom_value, "get_block_custom_value");
lua_addfunc(L, l_set_block_custom_value, "set_block_custom_value");
lua_addfunc(L, l_get_block_sig, "get_block_sig");
lua_addfunc(L, l_set_block_sig, "set_block_sig");
lua_addfunc(L, l_get_block_x, "get_block_X");