add 'on_breaking', 'on_block_breaking' events
This commit is contained in:
@@ -96,11 +96,12 @@ struct ContentPackStats {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct WorldFuncsSet {
|
struct WorldFuncsSet {
|
||||||
bool onblockplaced : 1;
|
bool onblockplaced;
|
||||||
bool onblockreplaced : 1;
|
bool onblockreplaced;
|
||||||
bool onblockbroken : 1;
|
bool onblockbreaking;
|
||||||
bool onblockinteract : 1;
|
bool onblockbroken;
|
||||||
bool onplayertick : 1;
|
bool onblockinteract;
|
||||||
|
bool onplayertick;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ContentPackRuntime {
|
class ContentPackRuntime {
|
||||||
|
|||||||
@@ -544,6 +544,7 @@ void PlayerController::updateInteraction(float delta) {
|
|||||||
}
|
}
|
||||||
auto& target = indices->blocks.require(vox->id);
|
auto& target = indices->blocks.require(vox->id);
|
||||||
if (lclick) {
|
if (lclick) {
|
||||||
|
scripting::on_block_breaking(&player, target, iend);
|
||||||
if (player.isInstantDestruction() && target.breakable) {
|
if (player.isInstantDestruction() && target.breakable) {
|
||||||
blocksController.breakBlock(
|
blocksController.breakBlock(
|
||||||
&player, target, iend.x, iend.y, iend.z
|
&player, target, iend.x, iend.y, iend.z
|
||||||
|
|||||||
@@ -336,16 +336,24 @@ void scripting::random_update_block(const Block& block, const glm::ivec3& pos) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_block_placed(
|
/// TODO: replace template with index
|
||||||
Player* player, const Block& block, const glm::ivec3& pos
|
template<bool WorldFuncsSet::*worldfunc>
|
||||||
|
static bool on_block_common(
|
||||||
|
const std::string& suffix,
|
||||||
|
bool blockfunc,
|
||||||
|
Player* player,
|
||||||
|
const Block& block,
|
||||||
|
const glm::ivec3& pos
|
||||||
) {
|
) {
|
||||||
if (block.rt.funcsset.onplaced) {
|
bool result = false;
|
||||||
std::string name = block.name + ".placed";
|
if (blockfunc) {
|
||||||
lua::emit_event(lua::get_main_state(), name, [pos, player](auto L) {
|
std::string name = block.name + "." + suffix;
|
||||||
lua::pushivec_stack(L, pos);
|
result =
|
||||||
lua::pushinteger(L, player ? player->getId() : -1);
|
lua::emit_event(lua::get_main_state(), name, [pos, player](auto L) {
|
||||||
return 4;
|
lua::pushivec_stack(L, pos);
|
||||||
});
|
lua::pushinteger(L, player->getId());
|
||||||
|
return 4;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
auto args = [&](lua::State* L) {
|
auto args = [&](lua::State* L) {
|
||||||
lua::pushinteger(L, block.rt.id);
|
lua::pushinteger(L, block.rt.id);
|
||||||
@@ -354,93 +362,53 @@ void scripting::on_block_placed(
|
|||||||
return 5;
|
return 5;
|
||||||
};
|
};
|
||||||
for (auto& [packid, pack] : content->getPacks()) {
|
for (auto& [packid, pack] : content->getPacks()) {
|
||||||
if (pack->worldfuncsset.onblockplaced) {
|
if (pack->worldfuncsset.*worldfunc) {
|
||||||
lua::emit_event(
|
lua::emit_event(
|
||||||
lua::get_main_state(), packid + ":.blockplaced", args
|
lua::get_main_state(), packid + ":.block" + suffix, args
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void scripting::on_block_placed(
|
||||||
|
Player* player, const Block& block, const glm::ivec3& pos
|
||||||
|
) {
|
||||||
|
on_block_common<&WorldFuncsSet::onblockplaced>(
|
||||||
|
"placed", block.rt.funcsset.onplaced, player, block, pos
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_block_replaced(
|
void scripting::on_block_replaced(
|
||||||
Player* player, const Block& block, const glm::ivec3& pos
|
Player* player, const Block& block, const glm::ivec3& pos
|
||||||
) {
|
) {
|
||||||
if (block.rt.funcsset.onreplaced) {
|
on_block_common<&WorldFuncsSet::onblockreplaced>(
|
||||||
std::string name = block.name + ".replaced";
|
"replaced", block.rt.funcsset.onreplaced, player, block, pos
|
||||||
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;
|
void scripting::on_block_breaking(
|
||||||
});
|
Player* player, const Block& block, const glm::ivec3& pos
|
||||||
}
|
) {
|
||||||
auto args = [&](lua::State* L) {
|
on_block_common<&WorldFuncsSet::onblockbreaking>(
|
||||||
lua::pushinteger(L, block.rt.id);
|
"breaking", block.rt.funcsset.onbreaking, player, block, pos
|
||||||
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(
|
void scripting::on_block_broken(
|
||||||
Player* player, const Block& block, const glm::ivec3& pos
|
Player* player, const Block& block, const glm::ivec3& pos
|
||||||
) {
|
) {
|
||||||
if (block.rt.funcsset.onbroken) {
|
on_block_common<&WorldFuncsSet::onblockbroken>(
|
||||||
std::string name = block.name + ".broken";
|
"broken", block.rt.funcsset.onbroken, player, block, pos
|
||||||
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.onblockbroken) {
|
|
||||||
lua::emit_event(
|
|
||||||
lua::get_main_state(), packid + ":.blockbroken", args
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool scripting::on_block_interact(
|
bool scripting::on_block_interact(
|
||||||
Player* player, const Block& block, const glm::ivec3& pos
|
Player* player, const Block& block, const glm::ivec3& pos
|
||||||
) {
|
) {
|
||||||
std::string name = block.name + ".interact";
|
return on_block_common<&WorldFuncsSet::onblockinteract>(
|
||||||
auto result = lua::emit_event(lua::get_main_state(), name, [pos, player](auto L) {
|
"interact", block.rt.funcsset.oninteract, player, block, pos
|
||||||
lua::pushivec_stack(L, pos);
|
);
|
||||||
lua::pushinteger(L, player->getId());
|
|
||||||
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.onblockinteract) {
|
|
||||||
lua::emit_event(
|
|
||||||
lua::get_main_state(), packid + ":.blockinteract", args
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_player_tick(Player* player, int tps) {
|
void scripting::on_player_tick(Player* player, int tps) {
|
||||||
@@ -804,6 +772,8 @@ void scripting::load_content_script(
|
|||||||
funcsset.update = register_event(env, "on_update", prefix + ".update");
|
funcsset.update = register_event(env, "on_update", prefix + ".update");
|
||||||
funcsset.randupdate =
|
funcsset.randupdate =
|
||||||
register_event(env, "on_random_update", prefix + ".randupdate");
|
register_event(env, "on_random_update", prefix + ".randupdate");
|
||||||
|
funcsset.onbreaking =
|
||||||
|
register_event(env, "on_breaking", prefix + ".breaking");
|
||||||
funcsset.onbroken = register_event(env, "on_broken", prefix + ".broken");
|
funcsset.onbroken = register_event(env, "on_broken", prefix + ".broken");
|
||||||
funcsset.onplaced = register_event(env, "on_placed", prefix + ".placed");
|
funcsset.onplaced = register_event(env, "on_placed", prefix + ".placed");
|
||||||
funcsset.onreplaced =
|
funcsset.onreplaced =
|
||||||
@@ -857,6 +827,8 @@ void scripting::load_world_script(
|
|||||||
register_event(env, "on_world_quit", prefix + ":.worldquit");
|
register_event(env, "on_world_quit", prefix + ":.worldquit");
|
||||||
funcsset.onblockplaced =
|
funcsset.onblockplaced =
|
||||||
register_event(env, "on_block_placed", prefix + ":.blockplaced");
|
register_event(env, "on_block_placed", prefix + ":.blockplaced");
|
||||||
|
funcsset.onblockbreaking =
|
||||||
|
register_event(env, "on_block_breaking", prefix + ":.blockbreaking");
|
||||||
funcsset.onblockbroken =
|
funcsset.onblockbroken =
|
||||||
register_event(env, "on_block_broken", prefix + ":.blockbroken");
|
register_event(env, "on_block_broken", prefix + ":.blockbroken");
|
||||||
funcsset.onblockreplaced =
|
funcsset.onblockreplaced =
|
||||||
|
|||||||
@@ -77,6 +77,9 @@ namespace scripting {
|
|||||||
void on_block_replaced(
|
void on_block_replaced(
|
||||||
Player* player, const Block& block, const glm::ivec3& pos
|
Player* player, const Block& block, const glm::ivec3& pos
|
||||||
);
|
);
|
||||||
|
void on_block_breaking(
|
||||||
|
Player* player, const Block& block, const glm::ivec3& pos
|
||||||
|
);
|
||||||
void on_block_broken(
|
void on_block_broken(
|
||||||
Player* player, const Block& block, const glm::ivec3& pos
|
Player* player, const Block& block, const glm::ivec3& pos
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ struct BlockFuncsSet {
|
|||||||
bool init : 1;
|
bool init : 1;
|
||||||
bool update : 1;
|
bool update : 1;
|
||||||
bool onplaced : 1;
|
bool onplaced : 1;
|
||||||
|
bool onbreaking : 1;
|
||||||
bool onbroken : 1;
|
bool onbroken : 1;
|
||||||
bool onreplaced : 1;
|
bool onreplaced : 1;
|
||||||
bool oninteract : 1;
|
bool oninteract : 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user