diff --git a/server/gameserver/android_agent.cc b/server/gameserver/android_agent.cc index 96f8fea7..baaef3fa 100644 --- a/server/gameserver/android_agent.cc +++ b/server/gameserver/android_agent.cc @@ -79,12 +79,23 @@ behaviac::EBTStatus AndroidAgent::DoRandomWalk() return behaviac::BT_FAILURE; } + std::weak_ptr handler = GetOwner()->GetTrigger()->AddListener + ( + kAttacked, + [this] (const std::vector& args) + { + Creature* c = std::any_cast(args.at(0)); + FireEvent("OnAttacked", c->GetUniId(), c->room->GetFrameNo()); + }); + return StartCoroutine ( - [this] () + [this, handler] () { if (GetOwner()->GetMoveHelper()->GetPathSize() <= 0) { - status_ = behaviac::BT_SUCCESS; + if (!handler.expired()) { + GetOwner()->GetTrigger()->RemoveEventHandler(handler); + } return behaviac::BT_SUCCESS; } else { return behaviac::BT_RUNNING; @@ -109,14 +120,24 @@ behaviac::EBTStatus AndroidAgent::DoRandomShot() a8::Vec2 shot_dir = dir; GetOwner()->Shot(shot_dir, shot_ok, 0, 0); + std::weak_ptr handler = GetOwner()->GetTrigger()->AddListener + ( + kAttacked, + [this] (const std::vector& args) + { + Creature* c = std::any_cast(args.at(0)); + FireEvent("OnAttacked", c->GetUniId(), c->room->GetFrameNo()); + }); long long last_frameno = GetOwner()->room->GetFrameNo(); return StartCoroutine ( - [this, last_frameno] () + [this, last_frameno, handler] () { if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 3) { - status_ = behaviac::BT_SUCCESS; + if (!handler.expired()) { + GetOwner()->GetTrigger()->RemoveEventHandler(handler); + } return behaviac::BT_SUCCESS; } else { bool shot_ok = false;