diff --git a/server/gameserver/ai/android/do_attack.cc b/server/gameserver/ai/android/do_attack.cc new file mode 100644 index 00000000..8658c1d6 --- /dev/null +++ b/server/gameserver/ai/android/do_attack.cc @@ -0,0 +1,92 @@ +#include "precompile.h" + +#include +#include + +#include "android_agent.h" +#include "android.h" +#include "room.h" +#include "movement.h" +#include "trigger.h" +#include "glmhelper.h" +#include "skill.h" +#include "btcontext.h" +#include "btevent.h" +#include "btcoroutine.h" + +#include "mt/Robot.h" + +behaviac::EBTStatus AndroidAgent::DoAttack() +{ + if (status_ == behaviac::BT_RUNNING) { + return DoRunningCb(); + } + Human* enemy = GetOwner()->room->FindEnemy(GetOwner()->AsHuman(), 300); + if (!enemy) { + return behaviac::BT_FAILURE; + } + if (GlmHelper::IsEqual2D(GetOwner()->GetPos().ToGlmVec3(), + enemy->GetPos().ToGlmVec3())) { + return behaviac::BT_FAILURE; + } + + glm::vec3 dir = GetOwner()->GetPos().CalcDir(enemy->GetPos()); + GlmHelper::Normalize(dir); + GetOwner()->SetMoveDir(dir); + GetOwner()->SetAttackDir(dir); + bool shot_ok = false; + glm::vec3 shot_dir = dir; + GetOwner()->Shot(AdjustShotDir(shot_dir), shot_ok, 0, 0); + + + auto context = MAKE_BTCONTEXT + ( + CreatureWeakPtr target; + long long last_frameno = 0; + ); + + context->target = enemy->GetWeakPtrRef(); + context->last_frameno = GetOwner()->room->GetFrameNo(); + + auto co = std::make_shared(context, "CoAttack"); + co->runing_cb = + [this, context] () + { + if (GetOwner()->room->GetFrameNo() - context->last_frameno > SERVER_FRAME_RATE * 3) { + status_ = behaviac::BT_SUCCESS; + GetOwner()->shot_hold = false; + return behaviac::BT_SUCCESS; + } else { + bool use_skill_ok = false; + if (context->target.Get() && + !context->target.Get()->dead) { + int skill_id = GetUseableSkill(context->target.Get()); + if (skill_id >= 0) { + GetOwner()->shot_hold = false; + int wait_time = 0; + use_skill_ok = InternalUseSkill(skill_id, context->target, wait_time); + if (use_skill_ok) { + Sleep(wait_time); + } + } + } +#ifdef DEBUG1 + if (context->target.Get()->IsPlayer()) { + a8::XPrintf("DoAttack %d use_skill_ok:%d time:%d\n", + {GetOwner()->GetUniId(), + use_skill_ok ? 1 : 0, + GetOwner()->GetMainSkill() ? GetOwner()->GetMainSkill()->GetLeftTime() : 0 + }); + } +#endif + if (!use_skill_ok) { + bool shot_ok = false; + glm::vec3 shot_dir = GetOwner()->GetAttackDir(); + GetOwner()->shot_hold = true; + GetOwner()->Shot(AdjustShotDir(shot_dir), shot_ok, 0, 0); + } + return behaviac::BT_RUNNING; + } + }; + return StartCoroutine(co); +} diff --git a/server/gameserver/ai/android/do_pursuit.cc b/server/gameserver/ai/android/do_pursuit.cc new file mode 100644 index 00000000..0572d2ab --- /dev/null +++ b/server/gameserver/ai/android/do_pursuit.cc @@ -0,0 +1,116 @@ +#include "precompile.h" + +#include +#include + +#include "android_agent.h" +#include "android.h" +#include "room.h" +#include "movement.h" +#include "trigger.h" +#include "glmhelper.h" +#include "skill.h" +#include "btcontext.h" +#include "btevent.h" +#include "btcoroutine.h" + +#include "mt/Robot.h" + + +behaviac::EBTStatus AndroidAgent::DoPursuit() +{ + if (status_ == behaviac::BT_RUNNING) { + return DoRunningCb(); + } + Human* enemy = GetOwner()->room->FindEnemy(GetOwner()->AsHuman(), 500); + if (!enemy) { + return behaviac::BT_FAILURE; + } + + auto context = MAKE_BTCONTEXT + ( + CreatureWeakPtr target; + long long last_frameno = 0; + long long last_pursuit_frameno = 0; + ); + + context->target = enemy->GetWeakPtrRef(); + context->last_frameno = GetOwner()->room->GetFrameNo(); + context->last_pursuit_frameno = GetOwner()->room->GetFrameNo(); + + auto co = std::make_shared(context, "CoPursuit"); + co->runing_cb = + [this, context] () + { + if (GetOwner()->room->GetFrameNo() - context->last_frameno > SERVER_FRAME_RATE * 10 || + !context->target.Get() || context->target.Get()->dead) { + status_ = behaviac::BT_SUCCESS; + GetOwner()->shot_hold = false; + return behaviac::BT_SUCCESS; + } else { + glm::vec3 dir = GetOwner()->GetPos().CalcDir(context->target.Get()->GetPos()); + if (GlmHelper::Norm(dir) <= 1.0f) { + GetOwner()->GetMovement()->CalcTargetPos(60); + context->last_pursuit_frameno = GetOwner()->room->GetFrameNo(); + } else { + bool is_shot = false; + float distance = GlmHelper::Norm(dir) - GetAttackRange(); + if (distance > 0.001f) { + if (GetOwner()->GetMovement()->GetPathSize() < 1) { + GlmHelper::Normalize(dir); + GetOwner()->SetMoveDir(dir); + GetOwner()->SetAttackDir(dir); + GetOwner()->GetMovement()->CalcTargetPos(distance + 60); + context->last_pursuit_frameno = GetOwner()->room->GetFrameNo(); + } else { + if (GetOwner()->room->GetFrameNo() - context->last_pursuit_frameno > SERVER_FRAME_RATE * 1) { + GlmHelper::Normalize(dir); + GetOwner()->SetMoveDir(dir); + GetOwner()->SetAttackDir(dir); + GetOwner()->GetMovement()->CalcTargetPos(distance + 60); + context->last_pursuit_frameno = GetOwner()->room->GetFrameNo(); + } + } + } else { + GlmHelper::Normalize(dir); + is_shot = true; + } + if (is_shot) { + bool use_skill_ok = false; + if (context->target.Get() && + !context->target.Get()->dead) { + int skill_id = GetUseableSkill(context->target.Get()); + if (skill_id >= 0) { + GetOwner()->shot_hold = false; + int wait_time = 0; + use_skill_ok = InternalUseSkill(skill_id, context->target, wait_time); + if (use_skill_ok) { + Sleep(wait_time); + } + } + } +#ifdef DEBUG1 + if (context->target.Get()->IsPlayer()) { + a8::XPrintf("DoPursuit %d use_skill_ok:%d time:%d\n", + {GetOwner()->GetUniId(), + use_skill_ok ? 1 : 0, + GetOwner()->GetMainSkill() ? GetOwner()->GetMainSkill()->GetLeftTime() : 0 + }); + } +#endif + if (!use_skill_ok) { + bool shot_ok = false; + glm::vec3 shot_dir = dir; + GetOwner()->shot_hold = true; + GetOwner()->SetAttackDir(shot_dir); + GetOwner()->Shot(AdjustShotDir(shot_dir), shot_ok, 0, 0); + } + } else { + GetOwner()->shot_hold = false; + } + } + return behaviac::BT_RUNNING; + } + }; + return StartCoroutine(co); +} diff --git a/server/gameserver/ai/android/do_random_shot.cc b/server/gameserver/ai/android/do_random_shot.cc new file mode 100644 index 00000000..a5c7f7b1 --- /dev/null +++ b/server/gameserver/ai/android/do_random_shot.cc @@ -0,0 +1,92 @@ +#include "precompile.h" + +#include +#include + +#include "android_agent.h" +#include "android.h" +#include "room.h" +#include "movement.h" +#include "trigger.h" +#include "glmhelper.h" +#include "skill.h" +#include "btcontext.h" +#include "btevent.h" +#include "btcoroutine.h" + +#include "mt/Robot.h" + + + +behaviac::EBTStatus AndroidAgent::DoRandomShot() +{ + if (status_ == behaviac::BT_RUNNING) { + return DoRunningCb(); + } + if (a8::HasBitFlag(GetOwner()->status, CS_DisableAttackAndroid)) { + return behaviac::BT_FAILURE; + } + + glm::vec3 dir = GetOwner()->GetMoveDir(); + GlmHelper::RotateY(dir, (10 + rand() % 360)/ 180.0f); + GlmHelper::Normalize(dir); + GetOwner()->SetMoveDir(dir); + GetOwner()->SetAttackDir(dir); + bool shot_ok = false; + glm::vec3 shot_dir = dir; + GetOwner()->Shot(AdjustShotDir(shot_dir), shot_ok, 0, 0); + + auto context = MAKE_BTCONTEXT + ( + CreatureWeakPtr last_attacker; + long long last_attacked_frameno = 0; + long long last_frameno = 0; + ); + + context->last_frameno = GetOwner()->room->GetFrameNo(); + context->AddHandler + ( + GetOwner()->GetWeakPtrRef(), + GetOwner()->GetTrigger()->AddListener + ( + kAttacked, + [context_wp = context->GetWp()] (const a8::Args& args) + { + if (!context_wp.expired()) { + auto context = context_wp.lock(); + Creature* c = args.Get(0); + context->events.push_back + ( + BtEvent::Create + ( + kBetOnAttack, + a8::Args({ + c->GetUniId(), + c->room->GetFrameNo() + }), + [c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable + { + return c_wp.Get() && !c_wp.Get()->dead; + }) + ); + } + })); + + auto co = std::make_shared(context, "CoRandomShot"); + co->runing_cb = + [this, context] () + { + if (GetOwner()->room->GetFrameNo() - context->last_frameno > SERVER_FRAME_RATE * 3) { + GetOwner()->shot_hold = false; + return behaviac::BT_SUCCESS; + } else { + bool shot_ok = false; + glm::vec3 shot_dir = GetOwner()->GetAttackDir(); + GetOwner()->shot_hold = true; + GetOwner()->Shot(AdjustShotDir(shot_dir), shot_ok, 0, 0); + + return behaviac::BT_RUNNING; + } + }; + return StartCoroutine(co); +} diff --git a/server/gameserver/ai/android/do_random_walk.cc b/server/gameserver/ai/android/do_random_walk.cc new file mode 100644 index 00000000..bffd0bc0 --- /dev/null +++ b/server/gameserver/ai/android/do_random_walk.cc @@ -0,0 +1,78 @@ +#include "precompile.h" + +#include +#include + +#include "android_agent.h" +#include "android.h" +#include "room.h" +#include "movement.h" +#include "trigger.h" +#include "glmhelper.h" +#include "skill.h" +#include "btcontext.h" +#include "btevent.h" +#include "btcoroutine.h" + +behaviac::EBTStatus AndroidAgent::DoRandomWalk() +{ + if (status_ == behaviac::BT_RUNNING) { + return DoRunningCb(); + } + + glm::vec3 dir = GetOwner()->GetMoveDir(); + GlmHelper::RotateY(dir, (10 + rand() % 360)/ 180.0f); + GlmHelper::Normalize(dir); + GetOwner()->SetMoveDir(dir); + GetOwner()->SetAttackDir(dir); + GetOwner()->GetMovement()->CalcTargetPos(200); + if (GetOwner()->GetMovement()->GetPathSize() <= 0) { + return behaviac::BT_FAILURE; + } + + auto context = MAKE_BTCONTEXT + ( + CreatureWeakPtr last_attacker; + long long last_attacked_frameno = 0; + ); + + context->AddHandler + ( + GetOwner()->GetWeakPtrRef(), + GetOwner()->GetTrigger()->AddListener + ( + kAttacked, + [context_wp = a8::SpToWp(context)] (const a8::Args& args) + { + if (!context_wp.expired()) { + auto context = context_wp.lock(); + Creature* c = args.Get(0); + context->events.push_back + ( + BtEvent::Create + ( + kBetOnAttack, + a8::Args({ + c->GetUniId(), + c->room->GetFrameNo() + }), + [c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable + { + return c_wp.Get() && !c_wp.Get()->dead; + }) + ); + } + })); + + auto co = std::make_shared(context, "CoRandomWalk"); + co->runing_cb = + [this, context] () + { + if (GetOwner()->GetMovement()->GetPathSize() <= 0) { + return behaviac::BT_SUCCESS; + } else { + return behaviac::BT_RUNNING; + } + }; + return StartCoroutine(co); +} diff --git a/server/gameserver/android_agent.cc b/server/gameserver/android_agent.cc index a3b289de..6c451c0e 100644 --- a/server/gameserver/android_agent.cc +++ b/server/gameserver/android_agent.cc @@ -33,314 +33,6 @@ State_e AndroidAgent::GetState() return kPreBattle; } -behaviac::EBTStatus AndroidAgent::DoRandomWalk() -{ - if (status_ == behaviac::BT_RUNNING) { - return DoRunningCb(); - } - - glm::vec3 dir = GetOwner()->GetMoveDir(); - GlmHelper::RotateY(dir, (10 + rand() % 360)/ 180.0f); - GlmHelper::Normalize(dir); - GetOwner()->SetMoveDir(dir); - GetOwner()->SetAttackDir(dir); - GetOwner()->GetMovement()->CalcTargetPos(200); - if (GetOwner()->GetMovement()->GetPathSize() <= 0) { - return behaviac::BT_FAILURE; - } - - auto context = MAKE_BTCONTEXT - ( - CreatureWeakPtr last_attacker; - long long last_attacked_frameno = 0; - ); - - context->AddHandler - ( - GetOwner()->GetWeakPtrRef(), - GetOwner()->GetTrigger()->AddListener - ( - kAttacked, - [context_wp = a8::SpToWp(context)] (const a8::Args& args) - { - if (!context_wp.expired()) { - auto context = context_wp.lock(); - Creature* c = args.Get(0); - context->events.push_back - ( - BtEvent::Create - ( - kBetOnAttack, - a8::Args({ - c->GetUniId(), - c->room->GetFrameNo() - }), - [c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable - { - return c_wp.Get() && !c_wp.Get()->dead; - }) - ); - } - })); - - auto co = std::make_shared(context, "CoRandomWalk"); - co->runing_cb = - [this, context] () - { - if (GetOwner()->GetMovement()->GetPathSize() <= 0) { - return behaviac::BT_SUCCESS; - } else { - return behaviac::BT_RUNNING; - } - }; - return StartCoroutine(co); -} - -behaviac::EBTStatus AndroidAgent::DoRandomShot() -{ - if (status_ == behaviac::BT_RUNNING) { - return DoRunningCb(); - } - if (a8::HasBitFlag(GetOwner()->status, CS_DisableAttackAndroid)) { - return behaviac::BT_FAILURE; - } - - glm::vec3 dir = GetOwner()->GetMoveDir(); - GlmHelper::RotateY(dir, (10 + rand() % 360)/ 180.0f); - GlmHelper::Normalize(dir); - GetOwner()->SetMoveDir(dir); - GetOwner()->SetAttackDir(dir); - bool shot_ok = false; - glm::vec3 shot_dir = dir; - GetOwner()->Shot(AdjustShotDir(shot_dir), shot_ok, 0, 0); - - auto context = MAKE_BTCONTEXT - ( - CreatureWeakPtr last_attacker; - long long last_attacked_frameno = 0; - long long last_frameno = 0; - ); - - context->last_frameno = GetOwner()->room->GetFrameNo(); - context->AddHandler - ( - GetOwner()->GetWeakPtrRef(), - GetOwner()->GetTrigger()->AddListener - ( - kAttacked, - [context_wp = context->GetWp()] (const a8::Args& args) - { - if (!context_wp.expired()) { - auto context = context_wp.lock(); - Creature* c = args.Get(0); - context->events.push_back - ( - BtEvent::Create - ( - kBetOnAttack, - a8::Args({ - c->GetUniId(), - c->room->GetFrameNo() - }), - [c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable - { - return c_wp.Get() && !c_wp.Get()->dead; - }) - ); - } - })); - - auto co = std::make_shared(context, "CoRandomShot"); - co->runing_cb = - [this, context] () - { - if (GetOwner()->room->GetFrameNo() - context->last_frameno > SERVER_FRAME_RATE * 3) { - GetOwner()->shot_hold = false; - return behaviac::BT_SUCCESS; - } else { - bool shot_ok = false; - glm::vec3 shot_dir = GetOwner()->GetAttackDir(); - GetOwner()->shot_hold = true; - GetOwner()->Shot(AdjustShotDir(shot_dir), shot_ok, 0, 0); - - return behaviac::BT_RUNNING; - } - }; - return StartCoroutine(co); -} - -behaviac::EBTStatus AndroidAgent::DoAttack() -{ - if (status_ == behaviac::BT_RUNNING) { - return DoRunningCb(); - } - Human* enemy = GetOwner()->room->FindEnemy(GetOwner()->AsHuman(), 300); - if (!enemy) { - return behaviac::BT_FAILURE; - } - if (GlmHelper::IsEqual2D(GetOwner()->GetPos().ToGlmVec3(), - enemy->GetPos().ToGlmVec3())) { - return behaviac::BT_FAILURE; - } - - glm::vec3 dir = GetOwner()->GetPos().CalcDir(enemy->GetPos()); - GlmHelper::Normalize(dir); - GetOwner()->SetMoveDir(dir); - GetOwner()->SetAttackDir(dir); - bool shot_ok = false; - glm::vec3 shot_dir = dir; - GetOwner()->Shot(AdjustShotDir(shot_dir), shot_ok, 0, 0); - - - auto context = MAKE_BTCONTEXT - ( - CreatureWeakPtr target; - long long last_frameno = 0; - ); - - context->target = enemy->GetWeakPtrRef(); - context->last_frameno = GetOwner()->room->GetFrameNo(); - - auto co = std::make_shared(context, "CoAttack"); - co->runing_cb = - [this, context] () - { - if (GetOwner()->room->GetFrameNo() - context->last_frameno > SERVER_FRAME_RATE * 3) { - status_ = behaviac::BT_SUCCESS; - GetOwner()->shot_hold = false; - return behaviac::BT_SUCCESS; - } else { - bool use_skill_ok = false; - if (context->target.Get() && - !context->target.Get()->dead) { - int skill_id = GetUseableSkill(context->target.Get()); - if (skill_id >= 0) { - GetOwner()->shot_hold = false; - int wait_time = 0; - use_skill_ok = InternalUseSkill(skill_id, context->target, wait_time); - if (use_skill_ok) { - Sleep(wait_time); - } - } - } -#ifdef DEBUG1 - if (context->target.Get()->IsPlayer()) { - a8::XPrintf("DoAttack %d use_skill_ok:%d time:%d\n", - {GetOwner()->GetUniId(), - use_skill_ok ? 1 : 0, - GetOwner()->GetMainSkill() ? GetOwner()->GetMainSkill()->GetLeftTime() : 0 - }); - } -#endif - if (!use_skill_ok) { - bool shot_ok = false; - glm::vec3 shot_dir = GetOwner()->GetAttackDir(); - GetOwner()->shot_hold = true; - GetOwner()->Shot(AdjustShotDir(shot_dir), shot_ok, 0, 0); - } - return behaviac::BT_RUNNING; - } - }; - return StartCoroutine(co); -} - -behaviac::EBTStatus AndroidAgent::DoPursuit() -{ - if (status_ == behaviac::BT_RUNNING) { - return DoRunningCb(); - } - Human* enemy = GetOwner()->room->FindEnemy(GetOwner()->AsHuman(), 500); - if (!enemy) { - return behaviac::BT_FAILURE; - } - - auto context = MAKE_BTCONTEXT - ( - CreatureWeakPtr target; - long long last_frameno = 0; - long long last_pursuit_frameno = 0; - ); - - context->target = enemy->GetWeakPtrRef(); - context->last_frameno = GetOwner()->room->GetFrameNo(); - context->last_pursuit_frameno = GetOwner()->room->GetFrameNo(); - - auto co = std::make_shared(context, "CoPursuit"); - co->runing_cb = - [this, context] () - { - if (GetOwner()->room->GetFrameNo() - context->last_frameno > SERVER_FRAME_RATE * 10 || - !context->target.Get() || context->target.Get()->dead) { - status_ = behaviac::BT_SUCCESS; - GetOwner()->shot_hold = false; - return behaviac::BT_SUCCESS; - } else { - glm::vec3 dir = GetOwner()->GetPos().CalcDir(context->target.Get()->GetPos()); - if (GlmHelper::Norm(dir) <= 1.0f) { - GetOwner()->GetMovement()->CalcTargetPos(60); - context->last_pursuit_frameno = GetOwner()->room->GetFrameNo(); - } else { - bool is_shot = false; - float distance = GlmHelper::Norm(dir) - GetAttackRange(); - if (distance > 0.001f) { - if (GetOwner()->GetMovement()->GetPathSize() < 1) { - GlmHelper::Normalize(dir); - GetOwner()->SetMoveDir(dir); - GetOwner()->SetAttackDir(dir); - GetOwner()->GetMovement()->CalcTargetPos(distance + 60); - context->last_pursuit_frameno = GetOwner()->room->GetFrameNo(); - } else { - if (GetOwner()->room->GetFrameNo() - context->last_pursuit_frameno > SERVER_FRAME_RATE * 1) { - GlmHelper::Normalize(dir); - GetOwner()->SetMoveDir(dir); - GetOwner()->SetAttackDir(dir); - GetOwner()->GetMovement()->CalcTargetPos(distance + 60); - context->last_pursuit_frameno = GetOwner()->room->GetFrameNo(); - } - } - } else { - GlmHelper::Normalize(dir); - is_shot = true; - } - if (is_shot) { - bool use_skill_ok = false; - if (context->target.Get() && - !context->target.Get()->dead) { - int skill_id = GetUseableSkill(context->target.Get()); - if (skill_id >= 0) { - GetOwner()->shot_hold = false; - int wait_time = 0; - use_skill_ok = InternalUseSkill(skill_id, context->target, wait_time); - if (use_skill_ok) { - Sleep(wait_time); - } - } - } -#ifdef DEBUG1 - if (context->target.Get()->IsPlayer()) { - a8::XPrintf("DoPursuit %d use_skill_ok:%d time:%d\n", - {GetOwner()->GetUniId(), - use_skill_ok ? 1 : 0, - GetOwner()->GetMainSkill() ? GetOwner()->GetMainSkill()->GetLeftTime() : 0 - }); - } -#endif - if (!use_skill_ok) { - bool shot_ok = false; - glm::vec3 shot_dir = dir; - GetOwner()->shot_hold = true; - GetOwner()->SetAttackDir(shot_dir); - GetOwner()->Shot(AdjustShotDir(shot_dir), shot_ok, 0, 0); - } - } else { - GetOwner()->shot_hold = false; - } - } - return behaviac::BT_RUNNING; - } - }; - return StartCoroutine(co); -} glm::vec3& AndroidAgent::AdjustShotDir(glm::vec3& shot_dir) {