Skip to content

Commit

Permalink
add player.create, test.set_setting, test.sleep_until, world.count_ch…
Browse files Browse the repository at this point in the history
…unks
  • Loading branch information
MihailRis committed Dec 12, 2024
1 parent 8a5042f commit 9ec8788
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 34 deletions.
13 changes: 12 additions & 1 deletion dev/tests/example.lua
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
print("hello world")
test.set_setting("chunks.load-distance", 2)
test.set_setting("chunks.load-speed", 16)

test.new_world("demo", "2019", "core:default")
local pid = player.create("Xerxes")
assert(player.get_name(pid) == "Xerxes")
test.sleep_until(function() return world.count_chunks() >= 9 end, 1000)
print(world.count_chunks())
assert(block.get(0, 0, 0) == block.index("core:obstacle"))
block.destruct(0, 0, 0, pid)
assert(block.get(0, 0, 0) == 0)
test.close_world(true)
12 changes: 12 additions & 0 deletions res/scripts/stdlib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ if test then
test.open_world = core.open_world
test.close_world = core.close_world
test.reconfig_packs = core.reconfig_packs
test.set_setting = core.set_setting
test.tick = coroutine.yield

function test.sleep_until(predicate, max_ticks)
max_ticks = max_ticks or 1e9
local ticks = 0
while ticks < max_ticks and not predicate() do
test.tick()
end
if ticks == max_ticks then
error("max ticks exceed")
end
end
end

------------------------------------------------
Expand Down
11 changes: 0 additions & 11 deletions src/frontend/screens/LevelScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,6 @@ void LevelScreen::update(float delta) {
level->getWorld()->updateTimers(delta);
animator->update(delta);
}

glm::vec3 position = player->getPosition();
level->loadMatrix(
position.x,
position.z,
settings.chunks.loadDistance.get() + settings.chunks.padding.get() * 2
);
controller->getChunksController()->update(
settings.chunks.loadSpeed.get(), settings.chunks.loadDistance.get(),
floordiv(position.x, CHUNK_W), floordiv(position.z, CHUNK_D)
);
if (!hud->isPause()) {
playerController->update(delta, !inputLocked);
}
Expand Down
41 changes: 21 additions & 20 deletions src/lighting/Lighting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ void Lighting::buildSkyLight(int cx, int cz){
solverS->solve();
}

void Lighting::onChunkLoaded(int cx, int cz, bool expand){
LightSolver* solverR = this->solverR.get();
LightSolver* solverG = this->solverG.get();
LightSolver* solverB = this->solverB.get();
LightSolver* solverS = this->solverS.get();

void Lighting::onChunkLoaded(int cx, int cz, bool expand) {
auto& solverR = *this->solverR;
auto& solverG = *this->solverG;
auto& solverB = *this->solverB;
auto& solverS = *this->solverS;

auto blockDefs = content->getIndices()->blocks.getDefs();
auto chunk = chunks->getChunk(cx, cz);
Expand All @@ -103,9 +104,9 @@ void Lighting::onChunkLoaded(int cx, int cz, bool expand){
int gx = x + cx * CHUNK_W;
int gz = z + cz * CHUNK_D;
if (block->rt.emissive){
solverR->add(gx,y,gz,block->emission[0]);
solverG->add(gx,y,gz,block->emission[1]);
solverB->add(gx,y,gz,block->emission[2]);
solverR.add(gx,y,gz,block->emission[0]);
solverG.add(gx,y,gz,block->emission[1]);
solverB.add(gx,y,gz,block->emission[2]);
}
}
}
Expand All @@ -119,10 +120,10 @@ void Lighting::onChunkLoaded(int cx, int cz, bool expand){
int gz = z + cz * CHUNK_D;
int rgbs = chunk->lightmap.get(x, y, z);
if (rgbs){
solverR->add(gx,y,gz, Lightmap::extract(rgbs, 0));
solverG->add(gx,y,gz, Lightmap::extract(rgbs, 1));
solverB->add(gx,y,gz, Lightmap::extract(rgbs, 2));
solverS->add(gx,y,gz, Lightmap::extract(rgbs, 3));
solverR.add(gx,y,gz, Lightmap::extract(rgbs, 0));
solverG.add(gx,y,gz, Lightmap::extract(rgbs, 1));
solverB.add(gx,y,gz, Lightmap::extract(rgbs, 2));
solverS.add(gx,y,gz, Lightmap::extract(rgbs, 3));
}
}
}
Expand All @@ -134,19 +135,19 @@ void Lighting::onChunkLoaded(int cx, int cz, bool expand){
int gz = z + cz * CHUNK_D;
int rgbs = chunk->lightmap.get(x, y, z);
if (rgbs){
solverR->add(gx,y,gz, Lightmap::extract(rgbs, 0));
solverG->add(gx,y,gz, Lightmap::extract(rgbs, 1));
solverB->add(gx,y,gz, Lightmap::extract(rgbs, 2));
solverS->add(gx,y,gz, Lightmap::extract(rgbs, 3));
solverR.add(gx,y,gz, Lightmap::extract(rgbs, 0));
solverG.add(gx,y,gz, Lightmap::extract(rgbs, 1));
solverB.add(gx,y,gz, Lightmap::extract(rgbs, 2));
solverS.add(gx,y,gz, Lightmap::extract(rgbs, 3));
}
}
}
}
}
solverR->solve();
solverG->solve();
solverB->solve();
solverS->solve();
solverR.solve();
solverG.solve();
solverB.solve();
solverS.solve();
}

void Lighting::onBlockSet(int x, int y, int z, blockid_t id){
Expand Down
13 changes: 13 additions & 0 deletions src/logic/LevelController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "maths/voxmaths.hpp"
#include "objects/Entities.hpp"
#include "objects/Players.hpp"
#include "objects/Player.hpp"
#include "physics/Hitbox.hpp"
#include "scripting/scripting.hpp"
#include "settings.hpp"
Expand All @@ -29,6 +30,18 @@ LevelController::LevelController(Engine* engine, std::unique_ptr<Level> levelPtr
}

void LevelController::update(float delta, bool pause) {
for (const auto& [uid, player] : *level->players) {
glm::vec3 position = player->getPosition();
level->loadMatrix(
position.x,
position.z,
settings.chunks.loadDistance.get() + settings.chunks.padding.get() * 2
);
chunks->update(
settings.chunks.loadSpeed.get(), settings.chunks.loadDistance.get(),
floordiv(position.x, CHUNK_W), floordiv(position.z, CHUNK_D)
);
}
if (!pause) {
// update all objects that needed
blocks->update(delta);
Expand Down
9 changes: 9 additions & 0 deletions src/logic/scripting/lua/libs/libplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@ static int l_set_name(lua::State* L) {
return 0;
}

static int l_create(lua::State* L) {
auto player = level->players->create();
if (lua::isstring(L, 1)) {
player->setName(lua::require_string(L, 1));
}
return lua::pushinteger(L, player->getId());
}

const luaL_Reg playerlib[] = {
{"get_pos", lua::wrap<l_get_pos>},
{"set_pos", lua::wrap<l_set_pos>},
Expand Down Expand Up @@ -277,5 +285,6 @@ const luaL_Reg playerlib[] = {
{"set_camera", lua::wrap<l_set_camera>},
{"get_name", lua::wrap<l_get_name>},
{"set_name", lua::wrap<l_set_name>},
{"create", lua::wrap<l_create>},
{NULL, NULL}
};
9 changes: 9 additions & 0 deletions src/logic/scripting/lua/libs/libworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "lighting/Lighting.hpp"
#include "voxels/Chunk.hpp"
#include "voxels/Chunks.hpp"
#include "voxels/ChunksStorage.hpp"
#include "world/Level.hpp"
#include "world/World.hpp"

Expand Down Expand Up @@ -231,6 +232,13 @@ static int l_set_chunk_data(lua::State* L) {
return 1;
}

static int l_count_chunks(lua::State* L) {
if (level == nullptr) {
return 0;
}
return lua::pushinteger(L, level->chunksStorage->size());
}

const luaL_Reg worldlib[] = {
{"is_open", lua::wrap<l_is_open>},
{"get_list", lua::wrap<l_get_list>},
Expand All @@ -246,5 +254,6 @@ const luaL_Reg worldlib[] = {
{"exists", lua::wrap<l_exists>},
{"get_chunk_data", lua::wrap<l_get_chunk_data>},
{"set_chunk_data", lua::wrap<l_set_chunk_data>},
{"count_chunks", lua::wrap<l_count_chunks>},
{NULL, NULL}
};
4 changes: 2 additions & 2 deletions src/util/AreaMap2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace util {
std::fill(secondBuffer.begin(), secondBuffer.end(), T{});
for (TCoord y = 0; y < sizeY; y++) {
for (TCoord x = 0; x < sizeX; x++) {
auto& value = firstBuffer[y * sizeX + x];
auto value = std::move(firstBuffer[y * sizeX + x]);
auto nx = x - dx;
auto ny = y - dy;
if (value == T{}) {
Expand All @@ -40,7 +40,7 @@ namespace util {
valuesCount--;
continue;
}
secondBuffer[ny * sizeX + nx] = value;
secondBuffer[ny * sizeX + nx] = std::move(value);
}
}
std::swap(firstBuffer, secondBuffer);
Expand Down
4 changes: 4 additions & 0 deletions src/voxels/ChunksStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,7 @@ std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
chunk->blocksMetadata = regions.getBlocksData(chunk->x, chunk->z);
return chunk;
}

size_t ChunksStorage::size() const {
return chunksMap->size();
}
2 changes: 2 additions & 0 deletions src/voxels/ChunksStorage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ class ChunksStorage {

std::shared_ptr<Chunk> fetch(int x, int z);
std::shared_ptr<Chunk> create(int x, int z);

size_t size() const;
};

0 comments on commit 9ec8788

Please sign in to comment.