Merge branch 'main' into main

This commit is contained in:
Onran 2024-02-21 22:41:44 +09:00 committed by GitHub
commit 12e6a92798
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
26 changed files with 191 additions and 116 deletions

View File

@ -1,42 +0,0 @@
name: C/C++ AppImage (wayland)
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build-appimage:
strategy:
matrix:
include:
- os: ubuntu-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
with:
submodules: 'true'
- name: install dependencies
run: |
sudo apt-get update
sudo apt-get install -y build-essential libglfw3-wayland libglfw3-dev libglew-dev libglm-dev libpng-dev libopenal-dev libluajit-5.1-dev cmake squashfs-tools
sudo ln -s /usr/lib/x86_64-linux-gnu/libluajit-5.1.a /usr/lib/x86_64-linux-gnu/liblua5.1.a
sudo ln -s /usr/include/luajit-2.1 /usr/include/lua
- name: configure
run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DVOXELENGINE_BUILD_APPDIR=1
- name: build
run: cmake --build build -t install
- name: Build AppImage
uses: AppImageCrafters/build-appimage-action@fe2205a4d6056be47051f7b1b3811106e9814910
env:
UPDATE_INFO: gh-releases-zsync|MihailRis|VoxelEngine-Cpp|latest|*x86_64.AppImage.zsync
with:
recipe: dev/AppImageBuilder.yml
- uses: actions/upload-artifact@v2
with:
name: AppImage
path: './*.AppImage*'

View File

@ -20,8 +20,10 @@ function parse_path(path)
return string.sub(path, 1, index-1), string.sub(path, index+1, -1)
end
package = {
loaded={}
}
local __cached_scripts = {}
local __cached_results = {}
-- Load script with caching
--
@ -31,38 +33,44 @@ local __cached_results = {}
-- nocache - ignore cached script, load anyway
function load_script(path, nocache)
local packname, filename = parse_path(path)
local fullpath = file.resolve(path);
-- __cached_scripts used in condition because cached result may be nil
if not nocache and __cached_scripts[fullpath] ~= nil then
return __cached_results[fullpath]
if not nocache and __cached_scripts[path] ~= nil then
return package.loaded[path]
end
if not file.isfile(path) then
error("script '"..filename.."' not found in '"..packname.."'")
end
local script, err = loadfile(fullpath)
local script, err = loadfile(file.resolve(path))
if script == nil then
error(err)
end
local result = script()
if not nocache then
__cached_scripts[fullpath] = script
__cached_results[fullpath] = result
__cached_scripts[path] = script
package.loaded[path] = result
end
return result
end
function __scripts_cleanup()
print("cleaning scripts cache")
for k, v in pairs(__cached_scripts) do
local packname, _ = parse_path(k)
if packname ~= "core" then
print("unloaded "..k)
__cached_scripts[k] = nil
package.loaded[k] = nil
end
end
end
function require(path)
local prefix, file = parse_path(path)
return load_script(prefix..":modules/"..file..".lua")
end
function __reset_scripts_cache()
__cached_scripts = {}
__cached_results = {}
end
function sleep(timesec)
local start = time.uptime()
while time.uptime() - start < timesec do

View File

@ -559,11 +559,11 @@ bool WorldFiles::readWorldInfo(World* world) {
return true;
}
void WorldFiles::writePlayer(Player* player) {
void WorldFiles::writePlayer(std::shared_ptr<Player> player) {
files::write_json(getPlayerFile(), player->serialize().release());
}
bool WorldFiles::readPlayer(Player* player) {
bool WorldFiles::readPlayer(std::shared_ptr<Player> player) {
fs::path file = getPlayerFile();
if (!fs::is_regular_file(file)) {
std::cerr << "warning: player.json does not exists" << std::endl;

View File

@ -143,13 +143,13 @@ public:
chunk_inventories_map fetchInventories(int x, int z);
bool readWorldInfo(World* world);
bool readPlayer(Player* player);
bool readPlayer(std::shared_ptr<Player> player);
void writeRegion(int x, int y,
WorldRegion* entry,
fs::path file,
int layer);
void writePlayer(Player* player);
void writePlayer(std::shared_ptr<Player> player);
/* @param world world info to save (nullable) */
void write(const World* world, const Content* content);
void writePacks(const World* world);

View File

@ -361,10 +361,18 @@ std::wstring TextBox::getText() const {
return input;
}
void TextBox::setText(std::wstring value) {
void TextBox::setText(const std::wstring value) {
this->input = value;
}
std::wstring TextBox::getPlaceholder() const {
return placeholder;
}
void TextBox::setPlaceholder(const std::wstring& placeholder) {
this->placeholder = placeholder;
}
// ============================== InputBindBox ================================
InputBindBox::InputBindBox(Binding& binding, glm::vec4 padding)
: Panel(glm::vec2(100,32), padding, 0),

View File

@ -122,6 +122,8 @@ namespace gui {
virtual std::wstring getText() const;
/* Set TextBox content text */
virtual void setText(std::wstring value);
virtual std::wstring getPlaceholder() const;
virtual void setPlaceholder(const std::wstring&);
virtual bool validate();
virtual void setValid(bool valid);
virtual bool isValid() const;

View File

@ -645,6 +645,6 @@ void Hud::setPause(bool pause) {
menu->setVisible(pause);
}
Player* Hud::getPlayer() const {
std::shared_ptr<Player> Hud::getPlayer() const {
return frontend->getLevel()->player;
}

View File

@ -118,7 +118,7 @@ public:
void remove(HudElement& element);
void remove(std::shared_ptr<gui::UINode> node);
Player* getPlayer() const;
std::shared_ptr<Player> getPlayer() const;
};
#endif /* SRC_HUD_H_ */

View File

@ -102,13 +102,9 @@ void BlocksRenderer::face(const vec3& coord,
void BlocksRenderer::vertex(const vec3& coord,
float u, float v,
const vec4& tint,
const vec3& X,
const vec3& Y,
const vec3& Z) {
// TODO: optimize
vec3 axisX = glm::normalize(X);
vec3 axisY = glm::normalize(Y);
vec3 axisZ = glm::normalize(Z);
const vec3& axisX,
const vec3& axisY,
const vec3& axisZ) {
vec3 pos = coord+axisZ*0.5f+(axisX+axisY)*0.5f;
vec4 light = pickSoftLight(ivec3(round(pos.x), round(pos.y), round(pos.z)), axisX, axisY);
vertex(coord, u, v, light * tint);
@ -130,11 +126,15 @@ void BlocksRenderer::face(const vec3& coord,
float d = glm::dot(Z, SUN_VECTOR);
d = 0.8f + d * 0.2f;
vec3 axisX = glm::normalize(X);
vec3 axisY = glm::normalize(Y);
vec3 axisZ = glm::normalize(Z);
vec4 tint(d);
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint, X, Y, Z);
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, tint, X, Y, Z);
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, tint, X, Y, Z);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, tint, X, Y, Z);
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint, axisX, axisY, axisZ);
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, tint, axisX, axisY, axisZ);
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, tint, axisX, axisY, axisZ);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, tint, axisX, axisY, axisZ);
} else {
vec4 tint(1.0f);
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint);
@ -142,7 +142,7 @@ void BlocksRenderer::face(const vec3& coord,
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, tint);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, tint);
}
index(0, 1, 2, 0, 2, 3);
index(0, 1, 2, 0, 2, 3);
}
void BlocksRenderer::tetragonicFace(const vec3& coord, const vec3& p1,

25
src/interfaces/Object.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef OBJECT_H
#define OBJECT_H
#include "stdlib.h"
#include <iostream>
class Level;
class Object {
private:
public:
uint64_t objectUID;
bool shouldUpdate = true;
public:
~Object() { destroyed(); }
public:
virtual void spawned() { }
virtual void update(float delta) { }
virtual void destroyed() { }
};
#endif /* OBJECT_H */

View File

@ -4,12 +4,11 @@
#include <memory>
#include "../coders/json.h"
class Serializable
{
class Serializable {
public:
virtual ~Serializable() { }
virtual std::unique_ptr<dynamic::Map> serialize() const = 0;
virtual void deserialize(dynamic::Map* src) = 0;
};
#endif
#endif /* SERIALIZABLE_H */

View File

@ -13,7 +13,7 @@
class ContentLUT;
class ContentIndices;
class Inventory : Serializable {
class Inventory : public Serializable {
int64_t id;
std::vector<ItemStack> slots;
public:

View File

@ -73,11 +73,11 @@ public:
return map;
}
static inline light_t combine(int r, int g, int b, int s) {
static constexpr light_t combine(int r, int g, int b, int s) {
return r | (g << 4) | (b << 8) | (s << 12);
}
static inline light_t extract(light_t light, ubyte channel) {
static constexpr light_t extract(light_t light, ubyte channel) {
return (light >> (channel << 2)) & 0xF;
}

View File

@ -6,6 +6,7 @@
#include "ChunksController.h"
#include "scripting/scripting.h"
#include "../interfaces/Object.h"
LevelController::LevelController(EngineSettings& settings, Level* level)
: settings(settings), level(level) {
@ -23,7 +24,20 @@ void LevelController::update(float delta, bool input, bool pause) {
player->update(delta, input, pause);
level->update();
chunks->update(settings.chunks.loadSpeed);
// erease null pointers
level->objects.remove_if([](auto obj) { return obj == nullptr; });
if (!pause) {
// update all objects that needed
for(auto obj : level->objects)
{
if(obj) {
if(obj->shouldUpdate) {
obj->update(delta);
}
}
}
blocks->update(delta);
}
}

View File

@ -31,7 +31,7 @@ const float C_ZOOM = 0.1f;
const float CROUCH_SHIFT_Y = -0.2f;
CameraControl::CameraControl(Player* player, const CameraSettings& settings)
CameraControl::CameraControl(std::shared_ptr<Player> player, const CameraSettings& settings)
: player(player),
camera(player->camera),
currentViewCamera(player->currentCamera),
@ -202,13 +202,13 @@ void PlayerController::resetKeyboard() {
}
void PlayerController::updateControls(float delta){
player->update(level, input, delta);
player->updateInput(level, input, delta);
}
void PlayerController::updateInteraction(){
auto indices = level->content->getIndices();
Chunks* chunks = level->chunks;
Player* player = level->player;
Player* player = level->player.get();
Lighting* lighting = level->lighting;
Camera* camera = player->camera.get();
glm::vec3 end;

View File

@ -12,7 +12,7 @@ class Level;
class BlocksController;
class CameraControl {
Player* player;
std::shared_ptr<Player> player;
std::shared_ptr<Camera> camera, currentViewCamera;
const CameraSettings& settings;
glm::vec3 offset;
@ -20,7 +20,7 @@ class CameraControl {
float shakeTimer = 0.0f;
glm::vec3 interpVel {0.0f};
public:
CameraControl(Player* player, const CameraSettings& settings);
CameraControl(std::shared_ptr<Player> player, const CameraSettings& settings);
void updateMouse(PlayerInput& input);
void update(PlayerInput& input, float delta, Chunks* chunks);
void refresh();
@ -28,7 +28,7 @@ public:
class PlayerController {
Level* level;
Player* player;
std::shared_ptr<Player> player;
PlayerInput input;
CameraControl camControl;
BlocksController* blocksController;

View File

@ -103,6 +103,29 @@ static bool getattr(lua_State* L, gui::FullCheckBox* box, const std::string& att
return false;
}
static bool getattr(lua_State* L, gui::TextBox* box, const std::string& attr) {
if (box == nullptr)
return false;
if (attr == "text") {
lua_pushstring(L, util::wstr2str_utf8(box->getText()).c_str());
return true;
} else if (attr == "placeholder") {
lua_pushstring(L, util::wstr2str_utf8(box->getPlaceholder()).c_str());
return true;
}
return false;
}
static bool setattr(lua_State* L, gui::FullCheckBox* box, const std::string& attr) {
if (box == nullptr)
return false;
if (attr == "checked") {
box->setChecked(lua_toboolean(L, 4));
return true;
}
return false;
}
static bool setattr(lua_State* L, gui::Button* button, const std::string& attr) {
if (button == nullptr)
return false;
@ -115,11 +138,14 @@ static bool setattr(lua_State* L, gui::Button* button, const std::string& attr)
return false;
}
static bool setattr(lua_State* L, gui::FullCheckBox* box, const std::string& attr) {
static bool setattr(lua_State* L, gui::TextBox* box, const std::string& attr) {
if (box == nullptr)
return false;
if (attr == "checked") {
box->setChecked(lua_toboolean(L, 4));
if (attr == "text") {
box->setText(util::str2wstr_utf8(lua_tostring(L, 4)));
return true;
} else if (attr == "placeholder") {
box->setPlaceholder(util::str2wstr_utf8(lua_tostring(L, 4)));
return true;
}
return false;
@ -161,6 +187,8 @@ int l_gui_getattr(lua_State* L) {
return 1;
if (getattr(L, dynamic_cast<gui::Label*>(node), attr))
return 1;
if (getattr(L, dynamic_cast<gui::TextBox*>(node), attr))
return 1;
if (getattr(L, dynamic_cast<gui::TrackBar*>(node), attr))
return 1;
if (getattr(L, dynamic_cast<gui::FullCheckBox*>(node), attr))
@ -197,6 +225,8 @@ int l_gui_setattr(lua_State* L) {
return 0;
if (setattr(L, dynamic_cast<gui::Label*>(node), attr))
return 0;
if (setattr(L, dynamic_cast<gui::TextBox*>(node), attr))
return 0;
if (setattr(L, dynamic_cast<gui::TrackBar*>(node), attr))
return 0;
if (setattr(L, dynamic_cast<gui::FullCheckBox*>(node), attr))

View File

@ -57,7 +57,7 @@ int l_player_get_inv(lua_State* L) {
int playerid = lua_tointeger(L, 1);
if (playerid != 1)
return 0;
Player* player = scripting::level->player;
auto player = scripting::level->player;
lua_pushinteger(L, player->getInventory()->getId());
lua_pushinteger(L, player->getChosenSlot());
return 2;

View File

@ -124,6 +124,9 @@ void scripting::on_world_quit() {
state->callNoThrow(0);
}
}
if (state->getglobal("__scripts_cleanup")) {
state->callNoThrow(0);
}
scripting::level = nullptr;
scripting::content = nullptr;
scripting::indices = nullptr;

View File

@ -33,7 +33,7 @@ Player::Player(glm::vec3 position, float speed, std::shared_ptr<Inventory> inv)
Player::~Player() {
}
void Player::update(
void Player::updateInput(
Level* level,
PlayerInput& input,
float delta) {

View File

@ -8,6 +8,7 @@
#include "../voxels/voxel.h"
#include "../settings.h"
#include "../interfaces/Serializable.h"
#include "../interfaces/Object.h"
class Camera;
class Hitbox;
@ -32,7 +33,7 @@ struct PlayerInput {
bool flight;
};
class Player : Serializable {
class Player : public Object, public Serializable {
float speed;
int chosenSlot;
glm::vec3 spawnpoint {};
@ -52,7 +53,7 @@ public:
~Player();
void teleport(glm::vec3 position);
void update(Level* level, PlayerInput& input, float delta);
void updateInput(Level* level, PlayerInput& input, float delta);
void attemptToFindSpawnpoint(Level* level);

View File

@ -9,9 +9,9 @@
#include <glm/glm.hpp>
class GLFWwindow;
class ImageData;
struct DisplaySettings;
struct GLFWwindow;
struct GLFWmonitor;
enum class blendmode {

View File

@ -8,19 +8,27 @@
#include "../voxels/ChunksStorage.h"
#include "../physics/Hitbox.h"
#include "../physics/PhysicsSolver.h"
#include "../interfaces/Object.h"
#include "../objects/Player.h"
#include "../items/Inventory.h"
#include "../items/Inventories.h"
Level::Level(World* world, const Content* content, Player* player, EngineSettings& settings)
const float DEF_PLAYER_Y = 100.0f;
const float DEF_PLAYER_SPEED = 4.0f;
const int DEF_PLAYER_INVENTORY_SIZE = 40;
Level::Level(World* world, const Content* content, EngineSettings& settings)
: world(world),
content(content),
player(player),
chunksStorage(new ChunksStorage(this)),
events(new LevelEvents()) ,
settings(settings)
{
objCounter = 0;
physics = new PhysicsSolver(glm::vec3(0, -22.6f, 0));
auto inv = std::make_shared<Inventory>(world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE);
player = spawnObject<Player>(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv);
uint matrixSize = (settings.chunks.loadDistance+
settings.chunks.padding) * 2;
@ -33,15 +41,20 @@ Level::Level(World* world, const Content* content, Player* player, EngineSetting
});
inventories = std::make_unique<Inventories>(*this);
inventories->store(player->getInventory());
}
Level::~Level(){
delete chunks;
delete events;
delete physics;
delete player;
delete lighting;
delete chunksStorage;
for(auto obj : objects)
{
obj.reset();
}
}
void Level::update() {
@ -58,3 +71,18 @@ void Level::update() {
World* Level::getWorld() {
return world.get();
}
template<class T, typename... Args>
std::shared_ptr<T> Level::spawnObject(Args&&... args)
{
static_assert(std::is_base_of<Object, T>::value, "T must be a derived of Object class");
std::shared_ptr<T> tObj = std::make_shared<T>(args...);
std::shared_ptr<Object> obj = std::dynamic_pointer_cast<Object, T>(tObj);
objects.push_back(obj);
obj->objectUID = objCounter;
obj->spawned();
objCounter += 1;
return tObj;
}

View File

@ -5,10 +5,14 @@
#include "../typedefs.h"
#include "../settings.h"
#include <list>
#include <vector>
#include <chrono>
class Content;
class World;
class Player;
class Object;
class Chunks;
class Inventory;
class Inventories;
@ -18,10 +22,13 @@ class PhysicsSolver;
class ChunksStorage;
class Level {
private:
int objCounter;
public:
std::unique_ptr<World> world;
const Content* const content;
Player* player;
std::list<std::shared_ptr<Object>> objects;
std::shared_ptr<Player> player;
Chunks* chunks;
ChunksStorage* chunksStorage;
std::unique_ptr<Inventories> inventories;
@ -34,13 +41,18 @@ public:
Level(World* world,
const Content* content,
Player* player,
EngineSettings& settings);
~Level();
void update();
World* getWorld();
// Spawns object of class T and returns pointer to it.
// @param T class that derives the Object class
// @param args pass arguments needed for T class constructor
template<class T, typename... Args>
std::shared_ptr<T> spawnObject(Args&&... args);
};
#endif /* WORLD_LEVEL_H_ */

View File

@ -82,10 +82,6 @@ void World::write(Level* level) {
wfile->writePlayer(level->player);
}
const float DEF_PLAYER_Y = 100.0f;
const float DEF_PLAYER_SPEED = 4.0f;
const int DEF_PLAYER_INVENTORY_SIZE = 40;
Level* World::create(std::string name,
std::string type,
fs::path directory,
@ -94,12 +90,8 @@ Level* World::create(std::string name,
const Content* content,
const std::vector<ContentPack>& packs) {
auto world = new World(name, type, directory, seed, settings, content, packs);
auto inv = std::make_shared<Inventory>(world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE);
auto player = new Player(
glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv
);
auto level = new Level(world, content, player, settings);
level->inventories->store(player->getInventory());
auto level = new Level(world, content, settings);
return level;
}
@ -116,13 +108,8 @@ Level* World::load(fs::path directory,
throw world_load_error("could not to find world.json");
}
auto inv = std::make_shared<Inventory>(0, DEF_PLAYER_INVENTORY_SIZE);
auto player = new Player(
glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv
);
auto level = new Level(world.get(), content, player, settings);
wfile->readPlayer(player);
level->inventories->store(player->getInventory());
auto level = new Level(world.get(), content, settings);
wfile->readPlayer(level->player);
world.release();
return level;
}

View File

@ -26,7 +26,7 @@ public:
world_load_error(std::string message);
};
class World : Serializable {
class World : public Serializable {
std::string name;
std::string type;
uint64_t seed;