Skip to content

Commit

Permalink
Scripting introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Dec 24, 2023
1 parent ce26f0c commit 33d77c2
Show file tree
Hide file tree
Showing 35 changed files with 625 additions and 134 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/appimage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ jobs:
- name: install dependencies
run: |
sudo apt-get update
sudo apt-get install -y build-essential libglfw3-dev libglfw3 libglew-dev libglm-dev libpng-dev libopenal-dev cmake squashfs-tools
sudo apt-get install -y build-essential libglfw3-dev libglfw3 libglew-dev libglm-dev libpng-dev libopenal-dev -libluajit-5.1-dev cmake squashfs-tools
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
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ jobs:
- uses: actions/checkout@v3

- name: Install packages
# If libluajit-5.1-dev is not available, use this:
# git clone https://luajit.org/git/luajit.git
# cd luajit
# make && make install INSTALL_INC=/usr/include/lua
run: |
sudo apt-get update
sudo apt-get install libglfw3-dev libglfw3 libglew-dev libglm-dev libpng-dev libopenal-dev
sudo apt-get install libglfw3-dev libglfw3 libglew-dev libglm-dev libpng-dev libopenal-dev -libluajit-5.1-dev
sudo ln -s /usr/include/luajit-2.1 /usr/include/lua
- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
Expand Down
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ endif()
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(OpenAL REQUIRED)
# luajit has no CMakeLists.txt to use it as subdirectory, so install it
find_package(Lua REQUIRED)

if (WIN32)
set(PNGLIB spng)
Expand All @@ -67,8 +69,8 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -no-pie")
endif()


target_link_libraries(${PROJECT_NAME} ${LIBS} glfw OpenGL::GL ${OPENAL_LIBRARY} GLEW::GLEW ${PNGLIB})
include_directories(${LUA_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${LIBS} glfw OpenGL::GL ${OPENAL_LIBRARY} GLEW::GLEW ${PNGLIB} ${LUA_LIBRARIES})

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/res DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,36 @@ cmake --build .
```

## Install libs:

#### Debian-based distro:
`$ sudo apt install libglfw3-dev libglfw3 libglew-dev libglm-dev libpng-dev libopenal-dev`
`$ sudo apt install libglfw3-dev libglfw3 libglew-dev libglm-dev libpng-dev libopenal-dev libluajit-5.1-dev`

CMake missing LUA_INCLUDE_DIR solution:
`$ sudo ln -s /usr/include/luajit-2.1 /usr/include/lua`

#### RHEL-based distro:
`$ sudo dnf install glfw-devel glfw glew-devel glm-devel libpng-devel openal-devel`

\+ install LuaJIT

#### Arch-based distro:
If you use X11
`$ sudo pacman -S glfw-x11 glew glm libpng openal`

If you use Wayland
`$ sudo pacman -S glfw-wayland glew glm libpng openal`

\+ install LuaJIT

#### LuaJIT installation:
```sh
git clone https://luajit.org/git/luajit.git
cd luajit
make && sudo make install INSTALL_INC=/usr/include/lua
```

#### macOS:

`$ brew install glfw3 glew glm libpng`

Download, compile and install OpenAL
Download, compile and install OpenAL and LuaJIT
3 changes: 2 additions & 1 deletion res/content/base/blocks/flower.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"light-passing": true,
"obstacle": false,
"replaceable": true,
"grounded": true,
"model": "X",
"hitbox": [0.15, 0.0, 0.15, 0.7, 0.7, 0.7]
}
}
3 changes: 2 additions & 1 deletion res/content/base/blocks/grass.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"light-passing": true,
"obstacle": false,
"replaceable": true,
"grounded": true,
"model": "X",
"hitbox": [0.15, 0.0, 0.15, 0.7, 0.7, 0.7]
}
}
20 changes: 20 additions & 0 deletions res/content/base/scripts/grass_block.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function on_random_update(x, y, z)
local dirtid = block_index('base:dirt');
if is_solid_at(x, y+1, z) then
set_block(x, y, z, dirtid)
else
local grassblockid = block_index('base:grass_block')
for lx=-1,1 do
for ly=-1,1 do
for lz=-1,1 do
if get_block(x + lx, y + ly, z + lz) == dirtid then
if not is_solid_at(x + lx, y + ly + 1, z + lz) then
set_block(x + lx, y + ly, z + lz, grassblockid)
return
end
end
end
end
end
end
end
Empty file added res/scripts/world.lua
Empty file.
10 changes: 9 additions & 1 deletion src/content/ContentLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "../typedefs.h"

#include "ContentPack.h"
#include "../logic/scripting/scripting.h"

namespace fs = std::filesystem;

Expand Down Expand Up @@ -84,6 +85,7 @@ Block* ContentLoader::loadBlock(std::string name, fs::path file) {
root->flag("light-passing", def->lightPassing);
root->flag("breakable", def->breakable);
root->flag("selectable", def->selectable);
root->flag("grounded", def->grounded);
root->flag("sky-light-passing", def->skyLightPassing);
root->num("draw-group", def->drawGroup);

Expand All @@ -100,8 +102,14 @@ void ContentLoader::load(ContentBuilder* builder) {
if (blocksarr) {
for (uint i = 0; i < blocksarr->size(); i++) {
std::string name = blocksarr->str(i);
std::string prefix = pack->id+":"+name;
fs::path blockfile = folder/fs::path("blocks/"+name+".json");
builder->add(loadBlock(pack->id+":"+name, blockfile));
Block* block = loadBlock(prefix, blockfile);
builder->add(block);
fs::path scriptfile = folder/fs::path("scripts/"+name+".lua");
if (fs::is_regular_file(scriptfile)) {
scripting::load_block_script(prefix, scriptfile, &block->rt.funcsset);
}
}
}
}
71 changes: 37 additions & 34 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "content/ContentPack.h"
#include "content/ContentLoader.h"
#include "frontend/locale/langs.h"
#include "logic/scripting/scripting.h"

#include "definitions.h"

Expand All @@ -45,6 +46,7 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths)
}

auto resdir = paths->getResources();
scripting::initialize();

std::cout << "-- loading assets" << std::endl;
std::vector<fs::path> roots {resdir};
Expand Down Expand Up @@ -117,6 +119,7 @@ void Engine::mainloop() {
}

Engine::~Engine() {
scripting::close();
screen = nullptr;
delete gui;

Expand All @@ -128,40 +131,6 @@ Engine::~Engine() {
std::cout << "-- engine finished" << std::endl;
}

gui::GUI* Engine::getGUI() {
return gui;
}

EngineSettings& Engine::getSettings() {
return settings;
}

Assets* Engine::getAssets() {
return assets.get();
}

void Engine::setScreen(std::shared_ptr<Screen> screen) {
this->screen = screen;
}

const Content* Engine::getContent() const {
return content.get();
}

std::vector<ContentPack>& Engine::getContentPacks() {
return contentPacks;
}

EnginePaths* Engine::getPaths() {
return paths;
}

void Engine::setLanguage(std::string locale) {
settings.ui.language = locale;
langs::setup(paths->getResources(), locale, contentPacks);
menus::create_menus(this, gui->getMenu());
}

void Engine::loadContent() {
auto resdir = paths->getResources();
ContentBuilder contentBuilder;
Expand Down Expand Up @@ -197,3 +166,37 @@ void Engine::loadAllPacks() {
contentPacks.clear();
ContentPack::scan(resdir/fs::path("content"), contentPacks);
}

void Engine::setScreen(std::shared_ptr<Screen> screen) {
this->screen = screen;
}

void Engine::setLanguage(std::string locale) {
settings.ui.language = locale;
langs::setup(paths->getResources(), locale, contentPacks);
menus::create_menus(this, gui->getMenu());
}

gui::GUI* Engine::getGUI() {
return gui;
}

EngineSettings& Engine::getSettings() {
return settings;
}

Assets* Engine::getAssets() {
return assets.get();
}

const Content* Engine::getContent() const {
return content.get();
}

std::vector<ContentPack>& Engine::getContentPacks() {
return contentPacks;
}

EnginePaths* Engine::getPaths() {
return paths;
}
12 changes: 6 additions & 6 deletions src/files/WorldFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

#include <cassert>
#include <iostream>
#include <sstream>
#include <cstdint>
#include <fstream>
#include <iostream>

const int SECTION_POSITION = 1;
const int SECTION_ROTATION = 2;
Expand Down Expand Up @@ -94,7 +94,7 @@ WorldFiles::~WorldFiles(){
}

WorldRegion* WorldFiles::getRegion(regionsmap& regions, int x, int z) {
auto found = regions.find(ivec2(x, z));
auto found = regions.find(glm::ivec2(x, z));
if (found == regions.end())
return nullptr;
return found->second;
Expand All @@ -104,7 +104,7 @@ WorldRegion* WorldFiles::getOrCreateRegion(regionsmap& regions, int x, int z) {
WorldRegion* region = getRegion(regions, x, z);
if (region == nullptr) {
region = new WorldRegion();
regions[ivec2(x, z)] = region;
regions[glm::ivec2(x, z)] = region;
}
return region;
}
Expand Down Expand Up @@ -367,7 +367,7 @@ void WorldFiles::writeRegions(regionsmap& regions, const fs::path& folder, int l
WorldRegion* region = it.second;
if (region->getChunks() == nullptr || !region->isUnsaved())
continue;
ivec2 key = it.first;
glm::ivec2 key = it.first;
writeRegion(key.x, key.y, region, folder, layer);
}
}
Expand Down Expand Up @@ -458,7 +458,7 @@ bool WorldFiles::readWorldInfo(World* world) {
}

void WorldFiles::writePlayer(Player* player){
vec3 position = player->hitbox->position;
glm::vec3 position = player->hitbox->position;
json::JObject root;
json::JArray& posarr = root.putArray("position");
posarr.put(position.x);
Expand All @@ -484,7 +484,7 @@ bool WorldFiles::readPlayer(Player* player) {

std::unique_ptr<json::JObject> root(files::read_json(file));
json::JArray* posarr = root->arr("position");
vec3& position = player->hitbox->position;
glm::vec3& position = player->hitbox->position;
position.x = posarr->num(0);
position.y = posarr->num(1);
position.z = posarr->num(2);
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/WorldRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera){
shader->uniform3f("u_cameraPos", camera->position);
shader->uniform1i("u_cubemap", 1);
{
blockid_t id = level->player->choosenBlock;
blockid_t id = level->player->chosenBlock;
Block* block = contentIds->getBlockDef(id);
assert(block != nullptr);
float multiplier = 0.5f;
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ void HudRenderer::drawContentAccess(const GfxContext& ctx, Player* player) {
tint.g *= 1.2f;
tint.b *= 1.2f;
if (Events::jclicked(mousecode::BUTTON_1)) {
player->choosenBlock = i+1;
player->chosenBlock = i+1;
}
} else {
tint = vec4(1.0f);
Expand Down Expand Up @@ -335,7 +335,7 @@ void HudRenderer::draw(const GfxContext& ctx){
subctx.depthTest(true);
subctx.cullFace(true);

Block* cblock = contentIds->getBlockDef(player->choosenBlock);
Block* cblock = contentIds->getBlockDef(player->chosenBlock);
assert(cblock != nullptr);
blocksPreview->draw(cblock, width - 56, uicamera->getFov() - 56, 48, vec4(1.0f));
//drawBlockPreview(cblock, width - 56, uicamera->fov - 56, 48, 48, vec4(1.0f));
Expand Down
Loading

0 comments on commit 33d77c2

Please sign in to comment.