add on_block_interact world event
This commit is contained in:
parent
f686749ae8
commit
f6f82644d4
@ -102,6 +102,12 @@ function on_block_broken(blockid, x, y, z, playerid)
|
|||||||
|
|
||||||
Called on block broken by player
|
Called on block broken by player
|
||||||
|
|
||||||
|
```lua
|
||||||
|
function on_block_interact(blockid, x, y, z, playerid) -> bool
|
||||||
|
```
|
||||||
|
|
||||||
|
Called on block RMB click interaction. Prevents block placing if **true** returned.
|
||||||
|
|
||||||
## Layout events
|
## Layout events
|
||||||
|
|
||||||
Script *layouts/layout_name.xml.lua* events.
|
Script *layouts/layout_name.xml.lua* events.
|
||||||
|
|||||||
@ -102,6 +102,12 @@ function on_block_broken(blockid, x, y, z, playerid)
|
|||||||
|
|
||||||
Вызывается после разрушения блока игроком
|
Вызывается после разрушения блока игроком
|
||||||
|
|
||||||
|
```lua
|
||||||
|
function on_block_interact(blockid, x, y, z, playerid) -> bool
|
||||||
|
```
|
||||||
|
|
||||||
|
Вызывается при нажатии на блок ПКМ. Предотвращает установку блоков, если возвращает `true`
|
||||||
|
|
||||||
## События макета
|
## События макета
|
||||||
|
|
||||||
События прописываются в файле `layouts/имя_макета.xml.lua`.
|
События прописываются в файле `layouts/имя_макета.xml.lua`.
|
||||||
|
|||||||
@ -97,6 +97,7 @@ struct ContentPackStats {
|
|||||||
struct world_funcs_set {
|
struct world_funcs_set {
|
||||||
bool onblockplaced : 1;
|
bool onblockplaced : 1;
|
||||||
bool onblockbroken : 1;
|
bool onblockbroken : 1;
|
||||||
|
bool onblockinteract : 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ContentPackRuntime {
|
class ContentPackRuntime {
|
||||||
|
|||||||
@ -64,7 +64,7 @@ void BlocksController::breakBlock(
|
|||||||
);
|
);
|
||||||
chunks->set(x, y, z, 0, {});
|
chunks->set(x, y, z, 0, {});
|
||||||
lighting->onBlockSet(x, y, z, 0);
|
lighting->onBlockSet(x, y, z, 0);
|
||||||
scripting::on_block_broken(player, def, x, y, z);
|
scripting::on_block_broken(player, def, glm::ivec3(x, y, z));
|
||||||
if (def.rt.extended) {
|
if (def.rt.extended) {
|
||||||
updateSides(x, y, z , def.size.x, def.size.y, def.size.z);
|
updateSides(x, y, z , def.size.x, def.size.y, def.size.z);
|
||||||
} else {
|
} else {
|
||||||
@ -80,7 +80,7 @@ void BlocksController::placeBlock(
|
|||||||
);
|
);
|
||||||
chunks->set(x, y, z, def.rt.id, state);
|
chunks->set(x, y, z, def.rt.id, state);
|
||||||
lighting->onBlockSet(x, y, z, def.rt.id);
|
lighting->onBlockSet(x, y, z, def.rt.id);
|
||||||
scripting::on_block_placed(player, def, x, y, z);
|
scripting::on_block_placed(player, def, glm::ivec3(x, y, z));
|
||||||
if (def.rt.extended) {
|
if (def.rt.extended) {
|
||||||
updateSides(x, y, z , def.size.x, def.size.y, def.size.z);
|
updateSides(x, y, z , def.size.x, def.size.y, def.size.z);
|
||||||
} else {
|
} else {
|
||||||
@ -91,7 +91,7 @@ void BlocksController::placeBlock(
|
|||||||
void BlocksController::updateBlock(int x, int y, int z) {
|
void BlocksController::updateBlock(int x, int y, int z) {
|
||||||
voxel* vox = chunks->get(x, y, z);
|
voxel* vox = chunks->get(x, y, z);
|
||||||
if (vox == nullptr) return;
|
if (vox == nullptr) return;
|
||||||
auto& def = level->content->getIndices()->blocks.require(vox->id);
|
const auto& def = level->content->getIndices()->blocks.require(vox->id);
|
||||||
if (def.grounded) {
|
if (def.grounded) {
|
||||||
const auto& vec = get_ground_direction(def, vox->state.rotation);
|
const auto& vec = get_ground_direction(def, vox->state.rotation);
|
||||||
if (!chunks->isSolidBlock(x + vec.x, y + vec.y, z + vec.z)) {
|
if (!chunks->isSolidBlock(x + vec.x, y + vec.y, z + vec.z)) {
|
||||||
@ -100,7 +100,7 @@ void BlocksController::updateBlock(int x, int y, int z) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (def.rt.funcsset.update) {
|
if (def.rt.funcsset.update) {
|
||||||
scripting::update_block(def, x, y, z);
|
scripting::update_block(def, glm::ivec3(x, y, z));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,7 +144,10 @@ void BlocksController::randomTick(
|
|||||||
auto& block = indices->blocks.require(vox.id);
|
auto& block = indices->blocks.require(vox.id);
|
||||||
if (block.rt.funcsset.randupdate) {
|
if (block.rt.funcsset.randupdate) {
|
||||||
scripting::random_update_block(
|
scripting::random_update_block(
|
||||||
block, chunk.x * CHUNK_W + bx, by, chunk.z * CHUNK_D + bz
|
block,
|
||||||
|
glm::ivec3(
|
||||||
|
chunk.x * CHUNK_W + bx, by, chunk.z * CHUNK_D + bz
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -216,82 +216,81 @@ void scripting::on_blocks_tick(const Block& block, int tps) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::update_block(const Block& block, int x, int y, int z) {
|
void scripting::update_block(const Block& block, const glm::ivec3& pos) {
|
||||||
std::string name = block.name + ".update";
|
std::string name = block.name + ".update";
|
||||||
lua::emit_event(lua::get_main_state(), name, [x, y, z](auto L) {
|
lua::emit_event(lua::get_main_state(), name, [pos](auto L) {
|
||||||
return lua::pushivec_stack(L, glm::ivec3(x, y, z));
|
return lua::pushivec_stack(L, pos);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::random_update_block(const Block& block, int x, int y, int z) {
|
void scripting::random_update_block(const Block& block, const glm::ivec3& pos) {
|
||||||
std::string name = block.name + ".randupdate";
|
std::string name = block.name + ".randupdate";
|
||||||
lua::emit_event(lua::get_main_state(), name, [x, y, z](auto L) {
|
lua::emit_event(lua::get_main_state(), name, [pos](auto L) {
|
||||||
return lua::pushivec_stack(L, glm::ivec3(x, y, z));
|
return lua::pushivec_stack(L, pos);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static std::function<int(lua::State*)> create_world_block_event_args(
|
||||||
|
const glm::ivec3& pos, const Block& block, Player* player
|
||||||
|
) {
|
||||||
|
return [&](lua::State* L) {
|
||||||
|
lua::pushinteger(L, block.rt.id);
|
||||||
|
lua::pushivec_stack(L, pos);
|
||||||
|
lua::pushinteger(L, player ? player->getId() : -1);
|
||||||
|
return 5;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
void scripting::on_block_placed(
|
void scripting::on_block_placed(
|
||||||
Player* player, const Block& block, int x, int y, int z
|
Player* player, const Block& block, const glm::ivec3& pos
|
||||||
) {
|
) {
|
||||||
if (block.rt.funcsset.onplaced) {
|
if (block.rt.funcsset.onplaced) {
|
||||||
std::string name = block.name + ".placed";
|
std::string name = block.name + ".placed";
|
||||||
lua::emit_event(lua::get_main_state(), name, [x, y, z, player](auto L) {
|
lua::emit_event(lua::get_main_state(), name, [pos, player](auto L) {
|
||||||
lua::pushivec_stack(L, glm::ivec3(x, y, z));
|
lua::pushivec_stack(L, pos);
|
||||||
lua::pushinteger(L, player ? player->getId() : -1);
|
lua::pushinteger(L, player ? player->getId() : -1);
|
||||||
return 4;
|
return 4;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
auto world_event_args = [&](lua::State* L) {
|
|
||||||
lua::pushinteger(L, block.rt.id);
|
|
||||||
lua::pushivec_stack(L, glm::ivec3(x, y, z));
|
|
||||||
lua::pushinteger(L, player ? player->getId() : -1);
|
|
||||||
return 5;
|
|
||||||
};
|
|
||||||
for (auto& [packid, pack] : content->getPacks()) {
|
for (auto& [packid, pack] : content->getPacks()) {
|
||||||
if (pack->worldfuncsset.onblockplaced) {
|
if (pack->worldfuncsset.onblockplaced) {
|
||||||
lua::emit_event(
|
lua::emit_event(
|
||||||
lua::get_main_state(),
|
lua::get_main_state(),
|
||||||
packid + ":.blockplaced",
|
packid + ":.blockplaced",
|
||||||
world_event_args
|
create_world_block_event_args(pos, block, player)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_block_broken(
|
void scripting::on_block_broken(
|
||||||
Player* player, const Block& block, int x, int y, int z
|
Player* player, const Block& block, const glm::ivec3& pos
|
||||||
) {
|
) {
|
||||||
if (block.rt.funcsset.onbroken) {
|
if (block.rt.funcsset.onbroken) {
|
||||||
std::string name = block.name + ".broken";
|
std::string name = block.name + ".broken";
|
||||||
lua::emit_event(
|
lua::emit_event(
|
||||||
lua::get_main_state(),
|
lua::get_main_state(),
|
||||||
name,
|
name,
|
||||||
[x, y, z, player](auto L) {
|
[pos, player](auto L) {
|
||||||
lua::pushivec_stack(L, glm::ivec3(x, y, z));
|
lua::pushivec_stack(L, pos);
|
||||||
lua::pushinteger(L, player ? player->getId() : -1);
|
lua::pushinteger(L, player ? player->getId() : -1);
|
||||||
return 4;
|
return 4;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
auto world_event_args = [&](lua::State* L) {
|
|
||||||
lua::pushinteger(L, block.rt.id);
|
|
||||||
lua::pushivec_stack(L, glm::ivec3(x, y, z));
|
|
||||||
lua::pushinteger(L, player ? player->getId() : -1);
|
|
||||||
return 5;
|
|
||||||
};
|
|
||||||
for (auto& [packid, pack] : content->getPacks()) {
|
for (auto& [packid, pack] : content->getPacks()) {
|
||||||
if (pack->worldfuncsset.onblockbroken) {
|
if (pack->worldfuncsset.onblockbroken) {
|
||||||
lua::emit_event(
|
lua::emit_event(
|
||||||
lua::get_main_state(),
|
lua::get_main_state(),
|
||||||
packid + ":.blockbroken",
|
packid + ":.blockbroken",
|
||||||
world_event_args
|
create_world_block_event_args(pos, block, player)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool scripting::on_block_interact(
|
bool scripting::on_block_interact(
|
||||||
Player* player, const Block& block, glm::ivec3 pos
|
Player* player, const Block& block, const glm::ivec3& pos
|
||||||
) {
|
) {
|
||||||
std::string name = block.name + ".interact";
|
std::string name = block.name + ".interact";
|
||||||
return lua::emit_event(lua::get_main_state(), name, [pos, player](auto L) {
|
return lua::emit_event(lua::get_main_state(), name, [pos, player](auto L) {
|
||||||
@ -299,6 +298,15 @@ bool scripting::on_block_interact(
|
|||||||
lua::pushinteger(L, player->getId());
|
lua::pushinteger(L, player->getId());
|
||||||
return 4;
|
return 4;
|
||||||
});
|
});
|
||||||
|
for (auto& [packid, pack] : content->getPacks()) {
|
||||||
|
if (pack->worldfuncsset.onblockinteract) {
|
||||||
|
lua::emit_event(
|
||||||
|
lua::get_main_state(),
|
||||||
|
packid + ":.blockinteract",
|
||||||
|
create_world_block_event_args(pos, block, player)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool scripting::on_item_use(Player* player, const ItemDef& item) {
|
bool scripting::on_item_use(Player* player, const ItemDef& item) {
|
||||||
@ -697,6 +705,8 @@ void scripting::load_world_script(
|
|||||||
register_event(env, "on_block_placed", prefix + ":.blockplaced");
|
register_event(env, "on_block_placed", prefix + ":.blockplaced");
|
||||||
funcsset.onblockbroken =
|
funcsset.onblockbroken =
|
||||||
register_event(env, "on_block_broken", prefix + ":.blockbroken");
|
register_event(env, "on_block_broken", prefix + ":.blockbroken");
|
||||||
|
funcsset.onblockinteract =
|
||||||
|
register_event(env, "on_block_interact", prefix + ":.blockinteract");
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::load_layout_script(
|
void scripting::load_layout_script(
|
||||||
|
|||||||
@ -64,15 +64,15 @@ namespace scripting {
|
|||||||
void on_world_quit();
|
void on_world_quit();
|
||||||
void cleanup();
|
void cleanup();
|
||||||
void on_blocks_tick(const Block& block, int tps);
|
void on_blocks_tick(const Block& block, int tps);
|
||||||
void update_block(const Block& block, int x, int y, int z);
|
void update_block(const Block& block, const glm::ivec3& pos);
|
||||||
void random_update_block(const Block& block, int x, int y, int z);
|
void random_update_block(const Block& block, const glm::ivec3& pos);
|
||||||
void on_block_placed(
|
void on_block_placed(
|
||||||
Player* player, const Block& block, int x, int y, int z
|
Player* player, const Block& block, const glm::ivec3& pos
|
||||||
);
|
);
|
||||||
void on_block_broken(
|
void on_block_broken(
|
||||||
Player* player, const Block& block, int x, int y, int z
|
Player* player, const Block& block, const glm::ivec3& pos
|
||||||
);
|
);
|
||||||
bool on_block_interact(Player* player, const Block& block, glm::ivec3 pos);
|
bool on_block_interact(Player* player, const Block& block, const glm::ivec3& pos);
|
||||||
|
|
||||||
/// @brief Called on RMB click with the item selected
|
/// @brief Called on RMB click with the item selected
|
||||||
/// @return true if prevents default action
|
/// @return true if prevents default action
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user