From 5cf7397ddfd71710bb670df3042cd7d7ca6e4fe1 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 8 Dec 2022 19:28:18 +0800 Subject: [PATCH] 1 --- server/gameserver/android_agent.cc | 227 ++++++++++++++++------------- server/gameserver/base_agent.cc | 15 +- server/gameserver/base_agent.h | 7 +- 3 files changed, 142 insertions(+), 107 deletions(-) diff --git a/server/gameserver/android_agent.cc b/server/gameserver/android_agent.cc index 2108dc4a..96f8fea7 100644 --- a/server/gameserver/android_agent.cc +++ b/server/gameserver/android_agent.cc @@ -4,6 +4,7 @@ #include "android.h" #include "room.h" #include "movehelper.h" +#include "trigger.h" AndroidAgent::AndroidAgent():BaseAgent() { @@ -24,6 +25,7 @@ behaviac::EBTStatus AndroidAgent::DoIdle(int min_time, int max_time) if (status_ == behaviac::BT_RUNNING) { return DoRunningCb(); } + int idle_time = a8::RandEx(min_time, max_time); xtimer_list* timer = GetOwner()->room->xtimer.AddDeadLineTimerAndAttach ( @@ -34,18 +36,31 @@ behaviac::EBTStatus AndroidAgent::DoIdle(int min_time, int max_time) }, &GetOwner()->xtimer_attacher.timer_list_); std::weak_ptr timer_ptr = GetOwner()->room->xtimer.GetTimerPtr(timer); - status_runing_cb_ = - [this, timer_ptr] () - { - if (timer_ptr.lock()) { - return behaviac::BT_RUNNING; - } else { - status_ = behaviac::BT_SUCCESS; - return behaviac::BT_SUCCESS; - } - }; - status_ = behaviac::BT_RUNNING; - return status_; + + 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, timer_ptr, handler] () + { + if (!timer_ptr.expired()) { + return behaviac::BT_RUNNING; + } else { + if (!handler.expired()) { + GetOwner()->GetTrigger()->RemoveEventHandler(handler); + } + return behaviac::BT_SUCCESS; + } + }, + "CoIdle" + ); } behaviac::EBTStatus AndroidAgent::DoRandomWalk() @@ -63,25 +78,28 @@ behaviac::EBTStatus AndroidAgent::DoRandomWalk() if (GetOwner()->GetMoveHelper()->GetPathSize() <= 0) { return behaviac::BT_FAILURE; } - status_runing_cb_ = - [this] () - { - if (GetOwner()->GetMoveHelper()->GetPathSize() <= 0) { - status_ = behaviac::BT_SUCCESS; - return behaviac::BT_SUCCESS; - } else { - return behaviac::BT_RUNNING; - } - }; - status_ = behaviac::BT_RUNNING; - return status_; + + return StartCoroutine + ( + [this] () + { + if (GetOwner()->GetMoveHelper()->GetPathSize() <= 0) { + status_ = behaviac::BT_SUCCESS; + return behaviac::BT_SUCCESS; + } else { + return behaviac::BT_RUNNING; + } + }, + "CoRandomWalk" + ); } behaviac::EBTStatus AndroidAgent::DoRandomShot() { if (status_ == behaviac::BT_RUNNING) { - return status_runing_cb_(); + return DoRunningCb(); } + a8::Vec2 dir = GetOwner()->GetMoveDir(); dir.Rotate((10 + rand() % 360)/ 180.0f); dir.Normalize(); @@ -91,24 +109,25 @@ behaviac::EBTStatus AndroidAgent::DoRandomShot() a8::Vec2 shot_dir = dir; GetOwner()->Shot(shot_dir, shot_ok, 0, 0); + long long last_frameno = GetOwner()->room->GetFrameNo(); - status_runing_cb_ = - [this, last_frameno] () - { - if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 3) { - status_ = behaviac::BT_SUCCESS; - return behaviac::BT_SUCCESS; - } else { - bool shot_ok = false; - a8::Vec2 shot_dir = GetOwner()->GetAttackDir(); - GetOwner()->Shot(shot_dir, shot_ok, 0, 0); + return StartCoroutine + ( + [this, last_frameno] () + { + if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 3) { + status_ = behaviac::BT_SUCCESS; + return behaviac::BT_SUCCESS; + } else { + bool shot_ok = false; + a8::Vec2 shot_dir = GetOwner()->GetAttackDir(); + GetOwner()->Shot(shot_dir, shot_ok, 0, 0); - return behaviac::BT_RUNNING; - } - }; - - status_ = behaviac::BT_RUNNING; - return status_; + return behaviac::BT_RUNNING; + } + }, + "CoRandomShot" + ); } behaviac::EBTStatus AndroidAgent::DoAttack() @@ -130,23 +149,23 @@ behaviac::EBTStatus AndroidAgent::DoAttack() GetOwner()->Shot(shot_dir, shot_ok, 0, 0); long long last_frameno = GetOwner()->room->GetFrameNo(); - status_runing_cb_ = - [this, last_frameno] () - { - if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 3) { - status_ = behaviac::BT_SUCCESS; - return behaviac::BT_SUCCESS; - } else { - bool shot_ok = false; - a8::Vec2 shot_dir = GetOwner()->GetAttackDir(); - GetOwner()->Shot(shot_dir, shot_ok, 0, 0); + return StartCoroutine + ( + [this, last_frameno] () + { + if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 3) { + status_ = behaviac::BT_SUCCESS; + return behaviac::BT_SUCCESS; + } else { + bool shot_ok = false; + a8::Vec2 shot_dir = GetOwner()->GetAttackDir(); + GetOwner()->Shot(shot_dir, shot_ok, 0, 0); - return behaviac::BT_RUNNING; - } - }; - - status_ = behaviac::BT_RUNNING; - return status_; + return behaviac::BT_RUNNING; + } + }, + "CoAttack" + ); } behaviac::EBTStatus AndroidAgent::DoPursuit() @@ -162,51 +181,51 @@ behaviac::EBTStatus AndroidAgent::DoPursuit() CreatureWeakPtr target = enemy->GetWeakPtrRef(); long long last_frameno = GetOwner()->room->GetFrameNo(); long long last_pursuit_frameno = GetOwner()->room->GetFrameNo(); - status_runing_cb_ = - [this, last_frameno, target, last_pursuit_frameno] () mutable - { - if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 10 || - !target.Get() || target.Get()->dead) { - status_ = behaviac::BT_SUCCESS; - return behaviac::BT_SUCCESS; - } else { - a8::Vec2 dir = target.Get()->GetPos() - GetOwner()->GetPos(); - if (dir.Norm() <= 1.0f) { - GetOwner()->GetMoveHelper()->CalcTargetPos(60); - last_pursuit_frameno = GetOwner()->room->GetFrameNo(); - } else { - bool is_shot = false; - if (dir.Norm() > 300) { - if (GetOwner()->GetMoveHelper()->GetPathSize() < 1) { - dir.Normalize(); - GetOwner()->SetMoveDir(dir); - GetOwner()->SetAttackDir(dir); - GetOwner()->GetMoveHelper()->CalcTargetPos(200); - last_pursuit_frameno = GetOwner()->room->GetFrameNo(); - } else { - if (GetOwner()->room->GetFrameNo() - last_pursuit_frameno > SERVER_FRAME_RATE * 1) { - dir.Normalize(); - GetOwner()->SetMoveDir(dir); - GetOwner()->SetAttackDir(dir); - GetOwner()->GetMoveHelper()->CalcTargetPos(200); - last_pursuit_frameno = GetOwner()->room->GetFrameNo(); - } - } - } else { - dir.Normalize(); - is_shot = true; - } - if (is_shot) { - bool shot_ok = false; - a8::Vec2 shot_dir = dir; - GetOwner()->SetAttackDir(dir); - GetOwner()->Shot(shot_dir, shot_ok, 0, 0); - } - } - return behaviac::BT_RUNNING; - } - }; - - status_ = behaviac::BT_RUNNING; - return status_; + return StartCoroutine + ( + [this, last_frameno, target, last_pursuit_frameno] () mutable + { + if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 10 || + !target.Get() || target.Get()->dead) { + status_ = behaviac::BT_SUCCESS; + return behaviac::BT_SUCCESS; + } else { + a8::Vec2 dir = target.Get()->GetPos() - GetOwner()->GetPos(); + if (dir.Norm() <= 1.0f) { + GetOwner()->GetMoveHelper()->CalcTargetPos(60); + last_pursuit_frameno = GetOwner()->room->GetFrameNo(); + } else { + bool is_shot = false; + if (dir.Norm() > 300) { + if (GetOwner()->GetMoveHelper()->GetPathSize() < 1) { + dir.Normalize(); + GetOwner()->SetMoveDir(dir); + GetOwner()->SetAttackDir(dir); + GetOwner()->GetMoveHelper()->CalcTargetPos(200); + last_pursuit_frameno = GetOwner()->room->GetFrameNo(); + } else { + if (GetOwner()->room->GetFrameNo() - last_pursuit_frameno > SERVER_FRAME_RATE * 1) { + dir.Normalize(); + GetOwner()->SetMoveDir(dir); + GetOwner()->SetAttackDir(dir); + GetOwner()->GetMoveHelper()->CalcTargetPos(200); + last_pursuit_frameno = GetOwner()->room->GetFrameNo(); + } + } + } else { + dir.Normalize(); + is_shot = true; + } + if (is_shot) { + bool shot_ok = false; + a8::Vec2 shot_dir = dir; + GetOwner()->SetAttackDir(dir); + GetOwner()->Shot(shot_dir, shot_ok, 0, 0); + } + } + return behaviac::BT_RUNNING; + } + }, + "CoPursuit" + ); } diff --git a/server/gameserver/base_agent.cc b/server/gameserver/base_agent.cc index f3dd009f..a5b77775 100644 --- a/server/gameserver/base_agent.cc +++ b/server/gameserver/base_agent.cc @@ -32,14 +32,24 @@ behaviac::EBTStatus BaseAgent::DoRunningCb() abort(); } status_ = status_runing_cb_(); +#ifdef DEBUG + if ((GetOwner()->room->GetFrameNo() - status_frameno_) % SERVER_FRAME_RATE == 0) { + a8::XPrintf("Running Status:%s %d\n", {status_name_, status_}); + } +#endif if (status_ != behaviac::BT_RUNNING) { status_runing_cb_ = nullptr; } return status_; } -behaviac::EBTStatus BaseAgent::StartCoroutine(std::function cb) +behaviac::EBTStatus BaseAgent::StartCoroutine(std::function cb, + const char* name) { +#ifdef DEBUG + status_frameno_ = GetOwner()->room->GetFrameNo(); + status_name_ = name; +#endif status_runing_cb_ = std::move(cb); status_ = behaviac::BT_RUNNING; return status_; @@ -104,6 +114,7 @@ behaviac::EBTStatus BaseAgent::CoAttackTarget(int target_id) } } return behaviac::BT_RUNNING; - } + }, + "CoAttackTarget" ); } diff --git a/server/gameserver/base_agent.h b/server/gameserver/base_agent.h index 31d77359..39e35991 100644 --- a/server/gameserver/base_agent.h +++ b/server/gameserver/base_agent.h @@ -24,9 +24,14 @@ public: protected: behaviac::EBTStatus DoRunningCb(); - behaviac::EBTStatus StartCoroutine(std::function cb); + behaviac::EBTStatus StartCoroutine(std::function cb, + const char* name); protected: +#ifdef DEBUG + long long status_frameno_ = 0; + const char* status_name_ = nullptr; +#endif behaviac::EBTStatus status_= behaviac::BT_SUCCESS; std::function status_runing_cb_;