Merge branch 'main' of https://github.com/MihailRis/VoxelEngine-Cpp
This commit is contained in:
commit
2163eaca71
@ -10,6 +10,9 @@ block.index(name: str) -> int
|
||||
-- Returns the id of the block material.
|
||||
block.material(blockid: int) -> str
|
||||
|
||||
-- Table of materials by their full names (example: base:carpet)
|
||||
block.materials: table<string, table>
|
||||
|
||||
-- Returns the block display name.
|
||||
block.caption(blockid: int) -> str
|
||||
|
||||
|
||||
@ -10,6 +10,8 @@ world.get_list() -> tables array {
|
||||
name: str,
|
||||
-- world icon/preview (loading automatically)
|
||||
icon: str
|
||||
-- engine version the world was saved on
|
||||
version: {int, int}
|
||||
}
|
||||
|
||||
-- Returns current day time in range \[0.0-1.0\]
|
||||
|
||||
@ -16,6 +16,12 @@ function on_broken(x, y, z, playerid)
|
||||
|
||||
Called on block broken by player
|
||||
|
||||
```lua
|
||||
function on_replaced(x, y, z, playerid)
|
||||
```
|
||||
|
||||
Called on block replaced with other by player
|
||||
|
||||
```lua
|
||||
function on_interact(x, y, z, playerid) -> bool
|
||||
```
|
||||
|
||||
@ -10,6 +10,9 @@ block.index(name: str) -> int
|
||||
-- Возвращает id материала блока.
|
||||
block.material(blockid: int) -> str
|
||||
|
||||
-- Таблица материалов по их полным именам (пример: base:carpet)
|
||||
block.materials: table<string, table>
|
||||
|
||||
-- Возвращает название блока, отображаемое в интерфейсе.
|
||||
block.caption(blockid: int) -> str
|
||||
|
||||
|
||||
@ -9,7 +9,9 @@ world.get_list() -> массив таблиц {
|
||||
-- название мира
|
||||
name: str,
|
||||
-- предпросмотр (автоматически загружаемая текстура)
|
||||
icon: str
|
||||
icon: str,
|
||||
-- версия движка, на которой был сохранен мир
|
||||
version: {int, int}
|
||||
}
|
||||
|
||||
-- Возвращает текущее игровое время от 0.0 до 1.0, где 0.0 и 1.0 - полночь, 0.5 - полдень.
|
||||
|
||||
@ -16,6 +16,12 @@ function on_broken(x, y, z, playerid)
|
||||
|
||||
Вызывается после разрушения блока игроком
|
||||
|
||||
```lua
|
||||
function on_replaced(x, y, z, playerid)
|
||||
```
|
||||
|
||||
Вызывается после замены блока игроком
|
||||
|
||||
```lua
|
||||
function on_interact(x, y, z, playerid) -> bool
|
||||
```
|
||||
|
||||
@ -1,6 +1,13 @@
|
||||
function on_open()
|
||||
local worlds = world.get_list()
|
||||
for _, info in ipairs(worlds) do
|
||||
local major, minor = core.get_version()
|
||||
if info.version[1] > major or info.version[2] > minor then
|
||||
info.versionColor = "#A02010"
|
||||
else
|
||||
info.versionColor = "#808080"
|
||||
end
|
||||
info.versionString = string.format("%s.%s", unpack(info.version))
|
||||
document.worlds:add(gui.template("world", info))
|
||||
end
|
||||
end
|
||||
|
||||
@ -14,4 +14,5 @@
|
||||
onclick='core.delete_world("%{name}")'>
|
||||
<image src='gui/delete_icon' size='32,32' color='#FFFFFF50'/>
|
||||
</button>
|
||||
<label color='%{versionColor}' pos='317,44'>%{versionString}</label>
|
||||
</container>
|
||||
|
||||
@ -483,16 +483,6 @@ void ContentLoader::loadBlock(
|
||||
auto configFile = folder / fs::path("blocks/" + name + ".json");
|
||||
if (fs::exists(configFile)) loadBlock(def, full, configFile);
|
||||
|
||||
auto scriptfile = folder / fs::path("scripts/" + def.scriptName + ".lua");
|
||||
if (fs::is_regular_file(scriptfile)) {
|
||||
scripting::load_block_script(
|
||||
env,
|
||||
full,
|
||||
scriptfile,
|
||||
pack->id + ":scripts/" + def.scriptName + ".lua",
|
||||
def.rt.funcsset
|
||||
);
|
||||
}
|
||||
if (!def.hidden) {
|
||||
auto& item = builder.items.create(full + BLOCK_ITEM_SUFFIX);
|
||||
item.generated = true;
|
||||
@ -514,17 +504,6 @@ void ContentLoader::loadItem(
|
||||
auto folder = pack->folder;
|
||||
auto configFile = folder / fs::path("items/" + name + ".json");
|
||||
if (fs::exists(configFile)) loadItem(def, full, configFile);
|
||||
|
||||
auto scriptfile = folder / fs::path("scripts/" + def.scriptName + ".lua");
|
||||
if (fs::is_regular_file(scriptfile)) {
|
||||
scripting::load_item_script(
|
||||
env,
|
||||
full,
|
||||
scriptfile,
|
||||
pack->id + ":scripts/" + def.scriptName + ".lua",
|
||||
def.rt.funcsset
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
static std::tuple<std::string, std::string, std::string> create_unit_id(
|
||||
@ -728,18 +707,6 @@ void ContentLoader::load() {
|
||||
|
||||
auto folder = pack->folder;
|
||||
|
||||
// Load main world script
|
||||
fs::path scriptFile = folder / fs::path("scripts/world.lua");
|
||||
if (fs::is_regular_file(scriptFile)) {
|
||||
scripting::load_world_script(
|
||||
env,
|
||||
pack->id,
|
||||
scriptFile,
|
||||
pack->id + ":scripts/world.lua",
|
||||
runtime->worldfuncsset
|
||||
);
|
||||
}
|
||||
|
||||
// Load world generators
|
||||
fs::path generatorsDir = folder / fs::u8path("generators");
|
||||
foreach_file(generatorsDir, [this](const fs::path& file) {
|
||||
@ -807,17 +774,6 @@ void ContentLoader::load() {
|
||||
);
|
||||
});
|
||||
|
||||
// Load entity components
|
||||
fs::path componentsDir = folder / fs::u8path("scripts/components");
|
||||
foreach_file(componentsDir, [this](const fs::path& file) {
|
||||
auto name = pack->id + ":" + file.stem().u8string();
|
||||
scripting::load_entity_component(
|
||||
name,
|
||||
file,
|
||||
pack->id + ":scripts/components/" + file.filename().u8string()
|
||||
);
|
||||
});
|
||||
|
||||
// Process content.json and load defined content units
|
||||
auto contentFile = pack->getContentFile();
|
||||
if (fs::exists(contentFile)) {
|
||||
@ -825,6 +781,61 @@ void ContentLoader::load() {
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static void load_scripts(Content& content, ContentUnitDefs<T>& units) {
|
||||
for (const auto& [name, def] : units.getDefs()) {
|
||||
size_t pos = name.find(':');
|
||||
if (pos == std::string::npos) {
|
||||
throw std::runtime_error("invalid content unit name");
|
||||
}
|
||||
const auto runtime = content.getPackRuntime(name.substr(0, pos));
|
||||
const auto& pack = runtime->getInfo();
|
||||
const auto& folder = pack.folder;
|
||||
auto scriptfile = folder / fs::path("scripts/" + def->scriptName + ".lua");
|
||||
if (fs::is_regular_file(scriptfile)) {
|
||||
scripting::load_content_script(
|
||||
runtime->getEnvironment(),
|
||||
name,
|
||||
scriptfile,
|
||||
pack.id + ":scripts/" + def->scriptName + ".lua",
|
||||
def->rt.funcsset
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ContentLoader::loadScripts(Content& content) {
|
||||
load_scripts(content, content.blocks);
|
||||
load_scripts(content, content.items);
|
||||
|
||||
for (const auto& [packid, runtime] : content.getPacks()) {
|
||||
const auto& pack = runtime->getInfo();
|
||||
const auto& folder = pack.folder;
|
||||
|
||||
// Load main world script
|
||||
fs::path scriptFile = folder / fs::path("scripts/world.lua");
|
||||
if (fs::is_regular_file(scriptFile)) {
|
||||
scripting::load_world_script(
|
||||
runtime->getEnvironment(),
|
||||
pack.id,
|
||||
scriptFile,
|
||||
pack.id + ":scripts/world.lua",
|
||||
runtime->worldfuncsset
|
||||
);
|
||||
}
|
||||
// Load entity components
|
||||
fs::path componentsDir = folder / fs::u8path("scripts/components");
|
||||
foreach_file(componentsDir, [&pack](const fs::path& file) {
|
||||
auto name = pack.id + ":" + file.stem().u8string();
|
||||
scripting::load_entity_component(
|
||||
name,
|
||||
file,
|
||||
pack.id + ":scripts/components/" + file.filename().u8string()
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void ContentLoader::loadResources(ResourceType type, const dv::value& list) {
|
||||
for (size_t i = 0; i < list.size(); i++) {
|
||||
builder.resourceIndices[static_cast<size_t>(type)].add(
|
||||
|
||||
@ -17,6 +17,7 @@ struct ContentPack;
|
||||
struct GeneratorDef;
|
||||
|
||||
class ResPaths;
|
||||
class Content;
|
||||
class ContentBuilder;
|
||||
class ContentPackRuntime;
|
||||
struct ContentPackStats;
|
||||
@ -76,4 +77,6 @@ public:
|
||||
|
||||
void fixPackIndices();
|
||||
void load();
|
||||
|
||||
static void loadScripts(Content& content);
|
||||
};
|
||||
|
||||
@ -97,6 +97,7 @@ struct ContentPackStats {
|
||||
|
||||
struct world_funcs_set {
|
||||
bool onblockplaced : 1;
|
||||
bool onblockreplaced : 1;
|
||||
bool onblockbroken : 1;
|
||||
bool onblockinteract : 1;
|
||||
bool onplayertick : 1;
|
||||
|
||||
@ -361,8 +361,10 @@ void Engine::loadContent() {
|
||||
ContentLoader(&pack, contentBuilder, *resPaths).load();
|
||||
load_configs(pack.folder);
|
||||
}
|
||||
|
||||
content = contentBuilder.build();
|
||||
scripting::on_content_load(content.get());
|
||||
|
||||
ContentLoader::loadScripts(*content);
|
||||
|
||||
langs::setup(resdir, langs::current->getId(), contentPacks);
|
||||
loadAssets();
|
||||
|
||||
@ -75,6 +75,13 @@ void BlocksController::breakBlock(
|
||||
void BlocksController::placeBlock(
|
||||
Player* player, const Block& def, blockstate state, int x, int y, int z
|
||||
) {
|
||||
auto voxel = chunks.get(x, y, z);
|
||||
if (voxel == nullptr) {
|
||||
return;
|
||||
}
|
||||
const auto& prevDef = level.content->getIndices()->blocks.require(voxel->id);
|
||||
scripting::on_block_replaced(player, prevDef, {x, y, z});
|
||||
|
||||
onBlockInteraction(
|
||||
player, glm::ivec3(x, y, z), def, BlockInteraction::placing
|
||||
);
|
||||
|
||||
@ -20,6 +20,12 @@
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
static int l_get_version(lua::State* L) {
|
||||
return lua::pushvec_stack(
|
||||
L, glm::vec2(ENGINE_VERSION_MAJOR, ENGINE_VERSION_MINOR)
|
||||
);
|
||||
}
|
||||
|
||||
/// @brief Creating new world
|
||||
/// @param name Name world
|
||||
/// @param seed Seed world
|
||||
@ -239,6 +245,7 @@ static int l_quit(lua::State*) {
|
||||
}
|
||||
|
||||
const luaL_Reg corelib[] = {
|
||||
{"get_version", lua::wrap<l_get_version>},
|
||||
{"new_world", lua::wrap<l_new_world>},
|
||||
{"open_world", lua::wrap<l_open_world>},
|
||||
{"reopen_world", lua::wrap<l_reopen_world>},
|
||||
|
||||
@ -4,7 +4,9 @@
|
||||
|
||||
#include "assets/Assets.hpp"
|
||||
#include "assets/AssetsLoader.hpp"
|
||||
#include "coders/json.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "files/files.hpp"
|
||||
#include "files/engine_paths.hpp"
|
||||
#include "world/Level.hpp"
|
||||
#include "world/World.hpp"
|
||||
@ -32,22 +34,34 @@ static int l_get_list(lua::State* L) {
|
||||
for (size_t i = 0; i < worlds.size(); i++) {
|
||||
lua::createtable(L, 0, 1);
|
||||
|
||||
auto name = worlds[i].filename().u8string();
|
||||
const auto& folder = worlds[i];
|
||||
|
||||
auto root = json::parse(files::read_string(folder/fs::u8path("world.json")));
|
||||
const auto& versionMap = root["version"];
|
||||
int versionMajor = versionMap["major"].asInteger();
|
||||
int versionMinor = versionMap["minor"].asInteger();
|
||||
|
||||
|
||||
auto name = folder.filename().u8string();
|
||||
lua::pushstring(L, name);
|
||||
lua::setfield(L, "name");
|
||||
|
||||
auto assets = engine->getAssets();
|
||||
std::string icon = "world#" + name + ".icon";
|
||||
if (!AssetsLoader::loadExternalTexture(
|
||||
assets,
|
||||
icon,
|
||||
{worlds[i] / fs::path("icon.png"),
|
||||
worlds[i] / fs::path("preview.png")}
|
||||
)) {
|
||||
assets,
|
||||
icon,
|
||||
{worlds[i] / fs::path("icon.png"),
|
||||
worlds[i] / fs::path("preview.png")}
|
||||
)) {
|
||||
icon = "gui/no_world_icon";
|
||||
}
|
||||
lua::pushstring(L, icon);
|
||||
lua::setfield(L, "icon");
|
||||
|
||||
lua::pushvec2(L, {versionMajor, versionMinor});
|
||||
lua::setfield(L, "version");
|
||||
|
||||
lua::rawseti(L, i + 1);
|
||||
}
|
||||
return 1;
|
||||
|
||||
@ -157,10 +157,25 @@ void scripting::process_post_runnables() {
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::on_content_load(Content* content) {
|
||||
scripting::content = content;
|
||||
scripting::indices = content->getIndices();
|
||||
|
||||
auto L = lua::get_main_state();
|
||||
if (lua::getglobal(L, "block")) {
|
||||
const auto& materials = content->getBlockMaterials();
|
||||
lua::createtable(L, 0, materials.size());
|
||||
for (const auto& [name, material] : materials) {
|
||||
lua::pushvalue(L, material->serialize());
|
||||
lua::setfield(L, name);
|
||||
}
|
||||
lua::setfield(L, "materials");
|
||||
lua::pop(L);
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::on_world_load(LevelController* controller) {
|
||||
scripting::level = controller->getLevel();
|
||||
scripting::content = level->content;
|
||||
scripting::indices = level->content->getIndices();
|
||||
scripting::blocks = controller->getBlocksController();
|
||||
scripting::controller = controller;
|
||||
|
||||
@ -269,6 +284,32 @@ void scripting::on_block_placed(
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::on_block_replaced(
|
||||
Player* player, const Block& block, const glm::ivec3& pos
|
||||
) {
|
||||
if (block.rt.funcsset.onreplaced) {
|
||||
std::string name = block.name + ".replaced";
|
||||
lua::emit_event(lua::get_main_state(), name, [pos, player](auto L) {
|
||||
lua::pushivec_stack(L, pos);
|
||||
lua::pushinteger(L, player ? player->getId() : -1);
|
||||
return 4;
|
||||
});
|
||||
}
|
||||
auto args = [&](lua::State* L) {
|
||||
lua::pushinteger(L, block.rt.id);
|
||||
lua::pushivec_stack(L, pos);
|
||||
lua::pushinteger(L, player ? player->getId() : -1);
|
||||
return 5;
|
||||
};
|
||||
for (auto& [packid, pack] : content->getPacks()) {
|
||||
if (pack->worldfuncsset.onblockreplaced) {
|
||||
lua::emit_event(
|
||||
lua::get_main_state(), packid + ":.blockreplaced", args
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::on_block_broken(
|
||||
Player* player, const Block& block, const glm::ivec3& pos
|
||||
) {
|
||||
@ -671,7 +712,7 @@ int scripting::get_values_on_stack() {
|
||||
return lua::gettop(lua::get_main_state());
|
||||
}
|
||||
|
||||
void scripting::load_block_script(
|
||||
void scripting::load_content_script(
|
||||
const scriptenv& senv,
|
||||
const std::string& prefix,
|
||||
const fs::path& file,
|
||||
@ -686,13 +727,15 @@ void scripting::load_block_script(
|
||||
register_event(env, "on_random_update", prefix + ".randupdate");
|
||||
funcsset.onbroken = register_event(env, "on_broken", prefix + ".broken");
|
||||
funcsset.onplaced = register_event(env, "on_placed", prefix + ".placed");
|
||||
funcsset.onreplaced =
|
||||
register_event(env, "on_replaced", prefix + ".replaced");
|
||||
funcsset.oninteract =
|
||||
register_event(env, "on_interact", prefix + ".interact");
|
||||
funcsset.onblockstick =
|
||||
register_event(env, "on_blocks_tick", prefix + ".blockstick");
|
||||
}
|
||||
|
||||
void scripting::load_item_script(
|
||||
void scripting::load_content_script(
|
||||
const scriptenv& senv,
|
||||
const std::string& prefix,
|
||||
const fs::path& file,
|
||||
@ -737,6 +780,8 @@ void scripting::load_world_script(
|
||||
register_event(env, "on_block_placed", prefix + ":.blockplaced");
|
||||
funcsset.onblockbroken =
|
||||
register_event(env, "on_block_broken", prefix + ":.blockbroken");
|
||||
funcsset.onblockreplaced =
|
||||
register_event(env, "on_block_replaced", prefix + ":.blockreplaced");
|
||||
funcsset.onblockinteract =
|
||||
register_event(env, "on_block_interact", prefix + ":.blockinteract");
|
||||
funcsset.onplayertick =
|
||||
|
||||
@ -45,6 +45,8 @@ namespace scripting {
|
||||
|
||||
void initialize(Engine* engine);
|
||||
|
||||
void on_content_load(Content* content);
|
||||
|
||||
bool register_event(
|
||||
int env, const std::string& name, const std::string& id
|
||||
);
|
||||
@ -69,6 +71,9 @@ namespace scripting {
|
||||
void on_block_placed(
|
||||
Player* player, const Block& block, const glm::ivec3& pos
|
||||
);
|
||||
void on_block_replaced(
|
||||
Player* player, const Block& block, const glm::ivec3& pos
|
||||
);
|
||||
void on_block_broken(
|
||||
Player* player, const Block& block, const glm::ivec3& pos
|
||||
);
|
||||
@ -128,7 +133,7 @@ namespace scripting {
|
||||
/// @param file item script file
|
||||
/// @param fileName script file path using the engine format
|
||||
/// @param funcsset block callbacks set
|
||||
void load_block_script(
|
||||
void load_content_script(
|
||||
const scriptenv& env,
|
||||
const std::string& prefix,
|
||||
const fs::path& file,
|
||||
@ -142,7 +147,7 @@ namespace scripting {
|
||||
/// @param file item script file
|
||||
/// @param fileName script file path using the engine format
|
||||
/// @param funcsset item callbacks set
|
||||
void load_item_script(
|
||||
void load_content_script(
|
||||
const scriptenv& env,
|
||||
const std::string& prefix,
|
||||
const fs::path& file,
|
||||
|
||||
@ -8,6 +8,15 @@
|
||||
#include "presets/ParticlesPreset.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
|
||||
dv::value BlockMaterial::serialize() const {
|
||||
return dv::object({
|
||||
{"name", name},
|
||||
{"stepsSound", stepsSound},
|
||||
{"placeSound", placeSound},
|
||||
{"breakSound", breakSound}
|
||||
});
|
||||
}
|
||||
|
||||
std::string to_string(BlockModel model) {
|
||||
switch (model) {
|
||||
case BlockModel::none:
|
||||
|
||||
@ -39,6 +39,7 @@ struct block_funcs_set {
|
||||
bool update : 1;
|
||||
bool onplaced : 1;
|
||||
bool onbroken : 1;
|
||||
bool onreplaced : 1;
|
||||
bool oninteract : 1;
|
||||
bool randupdate : 1;
|
||||
bool onblockstick : 1;
|
||||
@ -104,6 +105,8 @@ struct BlockMaterial {
|
||||
std::string stepsSound {""};
|
||||
std::string placeSound {""};
|
||||
std::string breakSound {""};
|
||||
|
||||
dv::value serialize() const;
|
||||
};
|
||||
|
||||
/// @brief Block properties definition
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user