diff --git a/server/gameserver/android_agent.cc b/server/gameserver/android_agent.cc index eb9ab31c..3ed8695a 100644 --- a/server/gameserver/android_agent.cc +++ b/server/gameserver/android_agent.cc @@ -13,6 +13,12 @@ #include "mt/Robot.h" +template +static auto SpToWp(std::shared_ptr sp) +{ + return std::weak_ptr(sp); +} + AndroidAgent::AndroidAgent():BaseAgent() { @@ -46,37 +52,37 @@ behaviac::EBTStatus AndroidAgent::DoRandomWalk() return behaviac::BT_FAILURE; } - auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED + auto context = MAKE_BTCONTEXT ( - CreatureWeakPtr owner; CreatureWeakPtr last_attacker; long long last_attacked_frameno = 0; - std::weak_ptr handler; ); - context->owner = GetOwner()->GetWeakPtrRef(); - context->handler = GetOwner()->GetTrigger()->AddListener + context->AddHandler(GetOwner()->GetTrigger()->AddListener ( kAttacked, - [context_wp = context->GetWp()] (const a8::Args& args) + [context_wp = SpToWp(context)] (const a8::Args& args) { if (!context_wp.expired()) { auto context = context_wp.lock(); Creature* c = args.Get(0); - context->last_attacker = c->GetWeakPtrRef(); - context->last_attacked_frameno = c->room->GetFrameNo(); + context->events.push_back + ( + BtEvent::Create + ( + kBetOnAttack, + a8::Args({ + c->GetUniId(), + kBetOnAttack + }), + [c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable + { + return c_wp.Get() && !c_wp.Get()->dead; + }) + ); } - }); - context->_destory_cb = - ( - [context = context.get()] () - { - if (context->owner.Get()) { - context->owner.Get()->GetTrigger()->RemoveEventHandler(context->handler); - } - }); + })); -#if 0 auto co = std::make_shared(context, "CoRandomWalk"); co->runing_cb = [this, context] () @@ -88,7 +94,6 @@ behaviac::EBTStatus AndroidAgent::DoRandomWalk() } }; return StartCoroutine(co); - #endif } behaviac::EBTStatus AndroidAgent::DoRandomShot() @@ -109,18 +114,15 @@ behaviac::EBTStatus AndroidAgent::DoRandomShot() glm::vec3 shot_dir = dir; GetOwner()->Shot(AdjustShotDir(shot_dir), shot_ok, 0, 0); - auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED + auto context = MAKE_BTCONTEXT ( - CreatureWeakPtr owner; CreatureWeakPtr last_attacker; long long last_attacked_frameno = 0; long long last_frameno = 0; - std::weak_ptr handler; ); - context->owner = GetOwner()->GetWeakPtrRef(); context->last_frameno = GetOwner()->room->GetFrameNo(); - context->handler = GetOwner()->GetTrigger()->AddListener + context->AddHandler(GetOwner()->GetTrigger()->AddListener ( kAttacked, [context_wp = context->GetWp()] (const a8::Args& args) @@ -128,20 +130,23 @@ behaviac::EBTStatus AndroidAgent::DoRandomShot() if (!context_wp.expired()) { auto context = context_wp.lock(); Creature* c = args.Get(0); - context->last_attacker = c->GetWeakPtrRef(); - context->last_attacked_frameno = c->room->GetFrameNo(); + context->events.push_back + ( + BtEvent::Create + ( + kBetOnAttack, + a8::Args({ + c->GetUniId(), + kBetOnAttack + }), + [c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable + { + return c_wp.Get() && !c_wp.Get()->dead; + }) + ); } - }); - context->_destory_cb = - ( - [context = context.get()] () - { - if (context->owner.Get()) { - context->owner.Get()->GetTrigger()->RemoveEventHandler(context->handler); - } - }); + })); - #if 0 auto co = std::make_shared(context, "CoRandomShot"); co->runing_cb = [this, context] () @@ -159,7 +164,6 @@ behaviac::EBTStatus AndroidAgent::DoRandomShot() } }; return StartCoroutine(co); - #endif } behaviac::EBTStatus AndroidAgent::DoAttack() @@ -185,18 +189,15 @@ behaviac::EBTStatus AndroidAgent::DoAttack() GetOwner()->Shot(AdjustShotDir(shot_dir), shot_ok, 0, 0); - auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED + auto context = MAKE_BTCONTEXT ( - CreatureWeakPtr owner; CreatureWeakPtr target; long long last_frameno = 0; ); - context->owner = GetOwner()->GetWeakPtrRef(); context->target = enemy->GetWeakPtrRef(); context->last_frameno = GetOwner()->room->GetFrameNo(); - #if 0 auto co = std::make_shared(context, "CoAttack"); co->runing_cb = [this, context] () @@ -238,7 +239,6 @@ behaviac::EBTStatus AndroidAgent::DoAttack() } }; return StartCoroutine(co); - #endif } behaviac::EBTStatus AndroidAgent::DoPursuit() @@ -251,20 +251,17 @@ behaviac::EBTStatus AndroidAgent::DoPursuit() return behaviac::BT_FAILURE; } - auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED + auto context = MAKE_BTCONTEXT ( - CreatureWeakPtr owner; CreatureWeakPtr target; long long last_frameno = 0; long long last_pursuit_frameno = 0; ); - context->owner = GetOwner()->GetWeakPtrRef(); context->target = enemy->GetWeakPtrRef(); context->last_frameno = GetOwner()->room->GetFrameNo(); context->last_pursuit_frameno = GetOwner()->room->GetFrameNo(); - #if 0 auto co = std::make_shared(context, "CoPursuit"); co->runing_cb = [this, context] () @@ -340,7 +337,6 @@ behaviac::EBTStatus AndroidAgent::DoPursuit() } }; return StartCoroutine(co); - #endif } glm::vec3& AndroidAgent::AdjustShotDir(glm::vec3& shot_dir) diff --git a/server/gameserver/hero_agent.cc b/server/gameserver/hero_agent.cc index d6d49e89..8f308340 100644 --- a/server/gameserver/hero_agent.cc +++ b/server/gameserver/hero_agent.cc @@ -14,6 +14,12 @@ #include "mt/Map.h" #include "mt/Hero.h" +template +static auto SpToWp(std::shared_ptr sp) +{ + return std::weak_ptr(sp); +} + HeroAgent::HeroAgent():BaseAgent() { @@ -39,16 +45,11 @@ behaviac::EBTStatus HeroAgent::DoRandomWalk() return behaviac::BT_FAILURE; } - auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED + auto context = MAKE_BTCONTEXT ( - CreatureWeakPtr owner; - CreatureWeakPtr last_attacker; - long long last_attacked_frameno = 0; - std::weak_ptr handler; ); - context->owner = GetOwner()->GetWeakPtrRef(); - context->handler = GetOwner()->GetTrigger()->AddListener + context->AddHandler(GetOwner()->GetTrigger()->AddListener ( kAttacked, [context_wp = context->GetWp()] (const a8::Args& args) @@ -56,21 +57,24 @@ behaviac::EBTStatus HeroAgent::DoRandomWalk() if (!context_wp.expired()) { auto context = context_wp.lock(); Creature* c = args.Get(0); - context->last_attacker = c->GetWeakPtrRef(); - context->last_attacked_frameno = c->room->GetFrameNo(); + context->events.push_back + ( + BtEvent::Create + ( + kBetOnAttack, + a8::Args({ + c->GetUniId(), + kBetOnAttack + }), + [c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable + { + return c_wp.Get() && !c_wp.Get()->dead; + }) + ); } - }); - context->_destory_cb = - ( - [context = context.get()] () - { - if (context->owner.Get()) { - context->owner.Get()->GetTrigger()->RemoveEventHandler(context->handler); - } - }); + })); - #if 0 - auto co = std::make_shared(context, "CoRandomWalk"); + auto co = std::make_shared(context, "CoRandomWalk"); co->runing_cb = [this, context] () { @@ -81,7 +85,6 @@ behaviac::EBTStatus HeroAgent::DoRandomWalk() } }; return StartCoroutine(co); - #endif } behaviac::EBTStatus HeroAgent::DoRandomShot() @@ -99,18 +102,14 @@ behaviac::EBTStatus HeroAgent::DoRandomShot() glm::vec3 shot_dir = dir; GetOwner()->Shot(shot_dir, shot_ok, 0, 0); - auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED + auto context = MAKE_BTCONTEXT ( - CreatureWeakPtr owner; - CreatureWeakPtr last_attacker; - long long last_attacked_frameno = 0; long long last_frameno = 0; std::weak_ptr handler; ); - context->owner = GetOwner()->GetWeakPtrRef(); context->last_frameno = GetOwner()->room->GetFrameNo(); - context->handler = GetOwner()->GetTrigger()->AddListener + context->AddHandler(GetOwner()->GetTrigger()->AddListener ( kAttacked, [context_wp = context->GetWp()] (const a8::Args& args) @@ -118,20 +117,23 @@ behaviac::EBTStatus HeroAgent::DoRandomShot() if (!context_wp.expired()) { auto context = context_wp.lock(); Creature* c = args.Get(0); - context->last_attacker = c->GetWeakPtrRef(); - context->last_attacked_frameno = c->room->GetFrameNo(); + context->events.push_back + ( + BtEvent::Create + ( + kBetOnAttack, + a8::Args({ + c->GetUniId(), + kBetOnAttack + }), + [c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable + { + return c_wp.Get() && !c_wp.Get()->dead; + }) + ); } - }); - context->_destory_cb = - ( - [context = context.get()] () - { - if (context->owner.Get()) { - context->owner.Get()->GetTrigger()->RemoveEventHandler(context->handler); - } - }); + })); - #if 0 auto co = std::make_shared(context, "CoRandomShot"); co->runing_cb = [this, context] () @@ -147,7 +149,6 @@ behaviac::EBTStatus HeroAgent::DoRandomShot() } }; return StartCoroutine(co); - #endif } behaviac::EBTStatus HeroAgent::DoAttack() @@ -178,34 +179,42 @@ behaviac::EBTStatus HeroAgent::DoAttack() GetOwner()->Shot(shot_dir, shot_ok, 0, 0); } - auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED + auto context = MAKE_BTCONTEXT ( - CreatureWeakPtr owner; CreatureWeakPtr target; long long last_frameno = 0; - CreatureWeakPtr last_attacker; - long long last_attacked_frameno = 0; - std::weak_ptr handler; ); - context->owner = GetOwner()->GetWeakPtrRef(); context->target = enemy->GetWeakPtrRef(); context->last_frameno = GetOwner()->room->GetFrameNo(); if (GetOwner()->AsHero()->master.Get()) { - context->handler = GetOwner()->AsHero()->master.Get()->GetTrigger()->AddListener + context->AddHandler(GetOwner()->AsHero()->master.Get()->GetTrigger()->AddListener ( kBulletHitEvent, [context_wp = context->GetWp()] (const a8::Args& args) { if (!context_wp.expired()) { auto context = context_wp.lock(); - Creature* c = args.Get(1); - context->last_attacker = c->GetWeakPtrRef(); - context->last_attacked_frameno = c->room->GetFrameNo(); + Creature* c = args.Get(0); + context->events.push_back + ( + BtEvent::Create + ( + kBetOnAttack, + a8::Args({ + c->GetUniId(), + kBetOnAttack + }), + [c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable + { + return c_wp.Get() && !c_wp.Get()->dead; + }) + ); } - }); + })); } + #if 0 context->_destory_cb = ( [context = context.get()] () @@ -219,8 +228,8 @@ behaviac::EBTStatus HeroAgent::DoAttack() } } }); + #endif - #if 0 auto co = std::make_shared(context, "CoAttack"); co->runing_cb = [this, context] () @@ -257,7 +266,6 @@ behaviac::EBTStatus HeroAgent::DoAttack() } }; return StartCoroutine(co); - #endif } behaviac::EBTStatus HeroAgent::DoPursuit() @@ -270,20 +278,17 @@ behaviac::EBTStatus HeroAgent::DoPursuit() return behaviac::BT_FAILURE; } - auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED + auto context = MAKE_BTCONTEXT ( - CreatureWeakPtr owner; CreatureWeakPtr target; long long last_frameno = 0; long long last_pursuit_frameno = 0; ); - context->owner = GetOwner()->GetWeakPtrRef(); context->target = enemy->GetWeakPtrRef(); context->last_frameno = GetOwner()->room->GetFrameNo(); context->last_pursuit_frameno = GetOwner()->room->GetFrameNo(); - #if 0 auto co = std::make_shared(context, "CoPursuit"); co->runing_cb = [this, context] () @@ -359,7 +364,6 @@ behaviac::EBTStatus HeroAgent::DoPursuit() } }; return StartCoroutine(co); - #endif } behaviac::EBTStatus HeroAgent::DoHelpAttack(int target_uniid) @@ -393,7 +397,7 @@ behaviac::EBTStatus HeroAgent::DoHelpAttack(int target_uniid) GetOwner()->Shot(shot_dir, shot_ok, 0, 0); } - auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED + auto context = MAKE_BTCONTEXT ( CreatureWeakPtr target; long long last_frameno = 0; @@ -402,7 +406,6 @@ behaviac::EBTStatus HeroAgent::DoHelpAttack(int target_uniid) context->target = enemy->GetWeakPtrRef(); context->last_frameno = GetOwner()->room->GetFrameNo(); - #if 0 auto co = std::make_shared(context, "CoHelpAttack"); co->runing_cb = [this, context] () @@ -439,7 +442,6 @@ behaviac::EBTStatus HeroAgent::DoHelpAttack(int target_uniid) } }; return StartCoroutine(co); - #endif } behaviac::EBTStatus HeroAgent::DoFlyToMasterAround() @@ -527,7 +529,7 @@ behaviac::EBTStatus HeroAgent::DoFollowMaster() return behaviac::BT_FAILURE; } - auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED + auto context = MAKE_BTCONTEXT ( long long last_frameno = 0; long long last_follow_frameno = 0; @@ -536,7 +538,6 @@ behaviac::EBTStatus HeroAgent::DoFollowMaster() context->last_frameno = GetOwner()->room->GetFrameNo(); context->last_follow_frameno = GetOwner()->room->GetFrameNo(); - #if 0 auto co = std::make_shared(context, "CoFollowMaster"); co->runing_cb = [this, context] () @@ -565,7 +566,6 @@ behaviac::EBTStatus HeroAgent::DoFollowMaster() } }; return StartCoroutine(co); - #endif } float HeroAgent::GetMasterDistance()