[Quest API] Export target to EVENT_TARGET_CHANGE in Perl/Lua. (#2870)

* [Quest API] Export target to EVENT_TARGET_CHANGE in Perl/Lua.

- Export `$target` to `EVENT_TARGET_CHANGE`.

- Export `e.other` to `event_target_change`.

- Allows operators to not have to grab bot, Client, or NPC's target in Perl with `GetTarget()`.
- Allows operators to not have to grab Client's target in Lua with `GetTarget()`.

* Update mob.cpp

* Update mob.cpp

* Update mob.cpp
This commit is contained in:
Alex King 2023-02-13 01:03:52 -05:00 committed by GitHub
parent d4afc78982
commit 8c363320d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 11 deletions

View File

@ -1561,6 +1561,13 @@ void PerlembParser::ExportEventVariables(
break; break;
} }
case EVENT_TARGET_CHANGE: {
if (extra_pointers && extra_pointers->size() == 1) {
ExportVar(package_name.c_str(), "target", "Mob", std::any_cast<Mob*>(extra_pointers->at(0)));
}
break;
}
case EVENT_WAYPOINT_ARRIVE: case EVENT_WAYPOINT_ARRIVE:
case EVENT_WAYPOINT_DEPART: { case EVENT_WAYPOINT_DEPART: {
ExportVar(package_name.c_str(), "wp", data); ExportVar(package_name.c_str(), "wp", data);

View File

@ -286,6 +286,7 @@ LuaParser::LuaParser() {
PlayerArgumentDispatch[EVENT_ITEM_CLICK_CAST_CLIENT] = handle_player_item_click; PlayerArgumentDispatch[EVENT_ITEM_CLICK_CAST_CLIENT] = handle_player_item_click;
PlayerArgumentDispatch[EVENT_ITEM_CLICK_CLIENT] = handle_player_item_click; PlayerArgumentDispatch[EVENT_ITEM_CLICK_CLIENT] = handle_player_item_click;
PlayerArgumentDispatch[EVENT_DESTROY_ITEM_CLIENT] = handle_player_destroy_item; PlayerArgumentDispatch[EVENT_DESTROY_ITEM_CLIENT] = handle_player_destroy_item;
PlayerArgumentDispatch[EVENT_TARGET_CHANGE] = handle_player_target_change;
ItemArgumentDispatch[EVENT_ITEM_CLICK] = handle_item_click; ItemArgumentDispatch[EVENT_ITEM_CLICK] = handle_item_click;
ItemArgumentDispatch[EVENT_ITEM_CLICK_CAST] = handle_item_click; ItemArgumentDispatch[EVENT_ITEM_CLICK_CAST] = handle_item_click;

View File

@ -1340,6 +1340,22 @@ void handle_player_destroy_item(
} }
} }
void handle_player_target_change(
QuestInterface *parse,
lua_State* L,
Client* client,
std::string data,
uint32 extra_data,
std::vector<std::any> *extra_pointers
) {
if (extra_pointers && extra_pointers->size() == 1) {
Lua_Mob l_mob(std::any_cast<Mob*>(extra_pointers->at(0)));
luabind::adl::object l_mob_o = luabind::adl::object(L, l_mob);
l_mob_o.push(L);
lua_setfield(L, -2, "other");
}
}
// Item // Item
void handle_item_click( void handle_item_click(
QuestInterface *parse, QuestInterface *parse,

View File

@ -698,6 +698,15 @@ void handle_player_destroy_item(
std::vector<std::any> *extra_pointers std::vector<std::any> *extra_pointers
); );
void handle_player_target_change(
QuestInterface *parse,
lua_State* L,
Client* client,
std::string data,
uint32 extra_data,
std::vector<std::any> *extra_pointers
);
// Item // Item
void handle_item_click( void handle_item_click(
QuestInterface *parse, QuestInterface *parse,

View File

@ -4259,19 +4259,36 @@ void Mob::SetTarget(Mob *mob)
target = mob; target = mob;
entity_list.UpdateHoTT(this); entity_list.UpdateHoTT(this);
if (IsNPC()) { const auto has_target_change_event = (
parse->EventNPC(EVENT_TARGET_CHANGE, CastToNPC(), mob, "", 0); parse->HasQuestSub(GetNPCTypeID(), EVENT_TARGET_CHANGE) ||
} parse->PlayerHasQuestSub(EVENT_TARGET_CHANGE) ||
else if (IsClient()) { parse->BotHasQuestSub(EVENT_TARGET_CHANGE)
parse->EventPlayer(EVENT_TARGET_CHANGE, CastToClient(), "", 0); );
if (CastToClient()->admin > AccountStatus::GMMgmt) { if (has_target_change_event) {
DisplayInfo(mob); std::vector<std::any> args;
args.emplace_back(mob);
if (IsNPC()) {
if (parse->HasQuestSub(GetNPCTypeID(), EVENT_TARGET_CHANGE)) {
parse->EventNPC(EVENT_TARGET_CHANGE, CastToNPC(), mob, "", 0, &args);
}
} else if (IsClient()) {
if (parse->PlayerHasQuestSub(EVENT_TARGET_CHANGE)) {
parse->EventPlayer(EVENT_TARGET_CHANGE, CastToClient(), "", 0, &args);
}
if (CastToClient()->admin > AccountStatus::GMMgmt) {
DisplayInfo(mob);
}
CastToClient()->SetBotPrecombat(false); // Any change in target will nullify this flag (target == mob checked above)
} else if (IsBot()) {
if (parse->BotHasQuestSub(EVENT_TARGET_CHANGE)) {
parse->EventBot(EVENT_TARGET_CHANGE, CastToBot(), mob, "", 0, &args);
}
} }
CastToClient()->SetBotPrecombat(false); // Any change in target will nullify this flag (target == mob checked above)
} else if (IsBot()) {
parse->EventBot(EVENT_TARGET_CHANGE, CastToBot(), mob, "", 0);
} }
if (IsPet() && GetOwner() && GetOwner()->IsClient()) { if (IsPet() && GetOwner() && GetOwner()->IsClient()) {