This commit is contained in:
aozhiwei 2023-06-21 19:57:24 +08:00
parent 36cc1e758d
commit e883cd3b07
2 changed files with 107 additions and 111 deletions

View File

@ -13,6 +13,12 @@
#include "mt/Robot.h" #include "mt/Robot.h"
template<typename T>
static auto SpToWp(std::shared_ptr<T> sp)
{
return std::weak_ptr<T>(sp);
}
AndroidAgent::AndroidAgent():BaseAgent() AndroidAgent::AndroidAgent():BaseAgent()
{ {
@ -46,37 +52,37 @@ behaviac::EBTStatus AndroidAgent::DoRandomWalk()
return behaviac::BT_FAILURE; return behaviac::BT_FAILURE;
} }
auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED auto context = MAKE_BTCONTEXT
( (
CreatureWeakPtr owner;
CreatureWeakPtr last_attacker; CreatureWeakPtr last_attacker;
long long last_attacked_frameno = 0; long long last_attacked_frameno = 0;
std::weak_ptr<EventHandlerPtr> handler;
); );
context->owner = GetOwner()->GetWeakPtrRef(); context->AddHandler(GetOwner()->GetTrigger()->AddListener
context->handler = GetOwner()->GetTrigger()->AddListener
( (
kAttacked, kAttacked,
[context_wp = context->GetWp()] (const a8::Args& args) [context_wp = SpToWp(context)] (const a8::Args& args)
{ {
if (!context_wp.expired()) { if (!context_wp.expired()) {
auto context = context_wp.lock(); auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0); Creature* c = args.Get<Creature*>(0);
context->last_attacker = c->GetWeakPtrRef(); context->events.push_back
context->last_attacked_frameno = c->room->GetFrameNo();
}
});
context->_destory_cb =
( (
[context = context.get()] () BtEvent::Create
(
kBetOnAttack,
a8::Args({
c->GetUniId(),
kBetOnAttack
}),
[c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable
{ {
if (context->owner.Get()) { return c_wp.Get() && !c_wp.Get()->dead;
context->owner.Get()->GetTrigger()->RemoveEventHandler(context->handler); })
);
} }
}); }));
#if 0
auto co = std::make_shared<BtCoroutine>(context, "CoRandomWalk"); auto co = std::make_shared<BtCoroutine>(context, "CoRandomWalk");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] ()
@ -88,7 +94,6 @@ behaviac::EBTStatus AndroidAgent::DoRandomWalk()
} }
}; };
return StartCoroutine(co); return StartCoroutine(co);
#endif
} }
behaviac::EBTStatus AndroidAgent::DoRandomShot() behaviac::EBTStatus AndroidAgent::DoRandomShot()
@ -109,18 +114,15 @@ behaviac::EBTStatus AndroidAgent::DoRandomShot()
glm::vec3 shot_dir = dir; glm::vec3 shot_dir = dir;
GetOwner()->Shot(AdjustShotDir(shot_dir), shot_ok, 0, 0); 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; CreatureWeakPtr last_attacker;
long long last_attacked_frameno = 0; long long last_attacked_frameno = 0;
long long last_frameno = 0; long long last_frameno = 0;
std::weak_ptr<EventHandlerPtr> handler;
); );
context->owner = GetOwner()->GetWeakPtrRef();
context->last_frameno = GetOwner()->room->GetFrameNo(); context->last_frameno = GetOwner()->room->GetFrameNo();
context->handler = GetOwner()->GetTrigger()->AddListener context->AddHandler(GetOwner()->GetTrigger()->AddListener
( (
kAttacked, kAttacked,
[context_wp = context->GetWp()] (const a8::Args& args) [context_wp = context->GetWp()] (const a8::Args& args)
@ -128,20 +130,23 @@ behaviac::EBTStatus AndroidAgent::DoRandomShot()
if (!context_wp.expired()) { if (!context_wp.expired()) {
auto context = context_wp.lock(); auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0); Creature* c = args.Get<Creature*>(0);
context->last_attacker = c->GetWeakPtrRef(); context->events.push_back
context->last_attacked_frameno = c->room->GetFrameNo();
}
});
context->_destory_cb =
( (
[context = context.get()] () BtEvent::Create
(
kBetOnAttack,
a8::Args({
c->GetUniId(),
kBetOnAttack
}),
[c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable
{ {
if (context->owner.Get()) { return c_wp.Get() && !c_wp.Get()->dead;
context->owner.Get()->GetTrigger()->RemoveEventHandler(context->handler); })
);
} }
}); }));
#if 0
auto co = std::make_shared<BtCoroutine>(context, "CoRandomShot"); auto co = std::make_shared<BtCoroutine>(context, "CoRandomShot");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] ()
@ -159,7 +164,6 @@ behaviac::EBTStatus AndroidAgent::DoRandomShot()
} }
}; };
return StartCoroutine(co); return StartCoroutine(co);
#endif
} }
behaviac::EBTStatus AndroidAgent::DoAttack() behaviac::EBTStatus AndroidAgent::DoAttack()
@ -185,18 +189,15 @@ behaviac::EBTStatus AndroidAgent::DoAttack()
GetOwner()->Shot(AdjustShotDir(shot_dir), shot_ok, 0, 0); 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; CreatureWeakPtr target;
long long last_frameno = 0; long long last_frameno = 0;
); );
context->owner = GetOwner()->GetWeakPtrRef();
context->target = enemy->GetWeakPtrRef(); context->target = enemy->GetWeakPtrRef();
context->last_frameno = GetOwner()->room->GetFrameNo(); context->last_frameno = GetOwner()->room->GetFrameNo();
#if 0
auto co = std::make_shared<BtCoroutine>(context, "CoAttack"); auto co = std::make_shared<BtCoroutine>(context, "CoAttack");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] ()
@ -238,7 +239,6 @@ behaviac::EBTStatus AndroidAgent::DoAttack()
} }
}; };
return StartCoroutine(co); return StartCoroutine(co);
#endif
} }
behaviac::EBTStatus AndroidAgent::DoPursuit() behaviac::EBTStatus AndroidAgent::DoPursuit()
@ -251,20 +251,17 @@ behaviac::EBTStatus AndroidAgent::DoPursuit()
return behaviac::BT_FAILURE; return behaviac::BT_FAILURE;
} }
auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED auto context = MAKE_BTCONTEXT
( (
CreatureWeakPtr owner;
CreatureWeakPtr target; CreatureWeakPtr target;
long long last_frameno = 0; long long last_frameno = 0;
long long last_pursuit_frameno = 0; long long last_pursuit_frameno = 0;
); );
context->owner = GetOwner()->GetWeakPtrRef();
context->target = enemy->GetWeakPtrRef(); context->target = enemy->GetWeakPtrRef();
context->last_frameno = GetOwner()->room->GetFrameNo(); context->last_frameno = GetOwner()->room->GetFrameNo();
context->last_pursuit_frameno = GetOwner()->room->GetFrameNo(); context->last_pursuit_frameno = GetOwner()->room->GetFrameNo();
#if 0
auto co = std::make_shared<BtCoroutine>(context, "CoPursuit"); auto co = std::make_shared<BtCoroutine>(context, "CoPursuit");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] ()
@ -340,7 +337,6 @@ behaviac::EBTStatus AndroidAgent::DoPursuit()
} }
}; };
return StartCoroutine(co); return StartCoroutine(co);
#endif
} }
glm::vec3& AndroidAgent::AdjustShotDir(glm::vec3& shot_dir) glm::vec3& AndroidAgent::AdjustShotDir(glm::vec3& shot_dir)

View File

@ -14,6 +14,12 @@
#include "mt/Map.h" #include "mt/Map.h"
#include "mt/Hero.h" #include "mt/Hero.h"
template<typename T>
static auto SpToWp(std::shared_ptr<T> sp)
{
return std::weak_ptr<T>(sp);
}
HeroAgent::HeroAgent():BaseAgent() HeroAgent::HeroAgent():BaseAgent()
{ {
@ -39,16 +45,11 @@ behaviac::EBTStatus HeroAgent::DoRandomWalk()
return behaviac::BT_FAILURE; 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<EventHandlerPtr> handler;
); );
context->owner = GetOwner()->GetWeakPtrRef(); context->AddHandler(GetOwner()->GetTrigger()->AddListener
context->handler = GetOwner()->GetTrigger()->AddListener
( (
kAttacked, kAttacked,
[context_wp = context->GetWp()] (const a8::Args& args) [context_wp = context->GetWp()] (const a8::Args& args)
@ -56,20 +57,23 @@ behaviac::EBTStatus HeroAgent::DoRandomWalk()
if (!context_wp.expired()) { if (!context_wp.expired()) {
auto context = context_wp.lock(); auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0); Creature* c = args.Get<Creature*>(0);
context->last_attacker = c->GetWeakPtrRef(); context->events.push_back
context->last_attacked_frameno = c->room->GetFrameNo();
}
});
context->_destory_cb =
( (
[context = context.get()] () BtEvent::Create
(
kBetOnAttack,
a8::Args({
c->GetUniId(),
kBetOnAttack
}),
[c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable
{ {
if (context->owner.Get()) { return c_wp.Get() && !c_wp.Get()->dead;
context->owner.Get()->GetTrigger()->RemoveEventHandler(context->handler); })
);
} }
}); }));
#if 0
auto co = std::make_shared<BtCoroutine>(context, "CoRandomWalk"); auto co = std::make_shared<BtCoroutine>(context, "CoRandomWalk");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] ()
@ -81,7 +85,6 @@ behaviac::EBTStatus HeroAgent::DoRandomWalk()
} }
}; };
return StartCoroutine(co); return StartCoroutine(co);
#endif
} }
behaviac::EBTStatus HeroAgent::DoRandomShot() behaviac::EBTStatus HeroAgent::DoRandomShot()
@ -99,18 +102,14 @@ behaviac::EBTStatus HeroAgent::DoRandomShot()
glm::vec3 shot_dir = dir; glm::vec3 shot_dir = dir;
GetOwner()->Shot(shot_dir, shot_ok, 0, 0); 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; long long last_frameno = 0;
std::weak_ptr<EventHandlerPtr> handler; std::weak_ptr<EventHandlerPtr> handler;
); );
context->owner = GetOwner()->GetWeakPtrRef();
context->last_frameno = GetOwner()->room->GetFrameNo(); context->last_frameno = GetOwner()->room->GetFrameNo();
context->handler = GetOwner()->GetTrigger()->AddListener context->AddHandler(GetOwner()->GetTrigger()->AddListener
( (
kAttacked, kAttacked,
[context_wp = context->GetWp()] (const a8::Args& args) [context_wp = context->GetWp()] (const a8::Args& args)
@ -118,20 +117,23 @@ behaviac::EBTStatus HeroAgent::DoRandomShot()
if (!context_wp.expired()) { if (!context_wp.expired()) {
auto context = context_wp.lock(); auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0); Creature* c = args.Get<Creature*>(0);
context->last_attacker = c->GetWeakPtrRef(); context->events.push_back
context->last_attacked_frameno = c->room->GetFrameNo();
}
});
context->_destory_cb =
( (
[context = context.get()] () BtEvent::Create
(
kBetOnAttack,
a8::Args({
c->GetUniId(),
kBetOnAttack
}),
[c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable
{ {
if (context->owner.Get()) { return c_wp.Get() && !c_wp.Get()->dead;
context->owner.Get()->GetTrigger()->RemoveEventHandler(context->handler); })
);
} }
}); }));
#if 0
auto co = std::make_shared<BtCoroutine>(context, "CoRandomShot"); auto co = std::make_shared<BtCoroutine>(context, "CoRandomShot");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] ()
@ -147,7 +149,6 @@ behaviac::EBTStatus HeroAgent::DoRandomShot()
} }
}; };
return StartCoroutine(co); return StartCoroutine(co);
#endif
} }
behaviac::EBTStatus HeroAgent::DoAttack() behaviac::EBTStatus HeroAgent::DoAttack()
@ -178,34 +179,42 @@ behaviac::EBTStatus HeroAgent::DoAttack()
GetOwner()->Shot(shot_dir, shot_ok, 0, 0); GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
} }
auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED auto context = MAKE_BTCONTEXT
( (
CreatureWeakPtr owner;
CreatureWeakPtr target; CreatureWeakPtr target;
long long last_frameno = 0; long long last_frameno = 0;
CreatureWeakPtr last_attacker;
long long last_attacked_frameno = 0;
std::weak_ptr<EventHandlerPtr> handler;
); );
context->owner = GetOwner()->GetWeakPtrRef();
context->target = enemy->GetWeakPtrRef(); context->target = enemy->GetWeakPtrRef();
context->last_frameno = GetOwner()->room->GetFrameNo(); context->last_frameno = GetOwner()->room->GetFrameNo();
if (GetOwner()->AsHero()->master.Get()) { if (GetOwner()->AsHero()->master.Get()) {
context->handler = GetOwner()->AsHero()->master.Get()->GetTrigger()->AddListener context->AddHandler(GetOwner()->AsHero()->master.Get()->GetTrigger()->AddListener
( (
kBulletHitEvent, kBulletHitEvent,
[context_wp = context->GetWp()] (const a8::Args& args) [context_wp = context->GetWp()] (const a8::Args& args)
{ {
if (!context_wp.expired()) { if (!context_wp.expired()) {
auto context = context_wp.lock(); auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(1); Creature* c = args.Get<Creature*>(0);
context->last_attacker = c->GetWeakPtrRef(); context->events.push_back
context->last_attacked_frameno = c->room->GetFrameNo(); (
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->_destory_cb =
( (
[context = context.get()] () [context = context.get()] ()
@ -219,8 +228,8 @@ behaviac::EBTStatus HeroAgent::DoAttack()
} }
} }
}); });
#endif
#if 0
auto co = std::make_shared<BtCoroutine>(context, "CoAttack"); auto co = std::make_shared<BtCoroutine>(context, "CoAttack");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] ()
@ -257,7 +266,6 @@ behaviac::EBTStatus HeroAgent::DoAttack()
} }
}; };
return StartCoroutine(co); return StartCoroutine(co);
#endif
} }
behaviac::EBTStatus HeroAgent::DoPursuit() behaviac::EBTStatus HeroAgent::DoPursuit()
@ -270,20 +278,17 @@ behaviac::EBTStatus HeroAgent::DoPursuit()
return behaviac::BT_FAILURE; return behaviac::BT_FAILURE;
} }
auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED auto context = MAKE_BTCONTEXT
( (
CreatureWeakPtr owner;
CreatureWeakPtr target; CreatureWeakPtr target;
long long last_frameno = 0; long long last_frameno = 0;
long long last_pursuit_frameno = 0; long long last_pursuit_frameno = 0;
); );
context->owner = GetOwner()->GetWeakPtrRef();
context->target = enemy->GetWeakPtrRef(); context->target = enemy->GetWeakPtrRef();
context->last_frameno = GetOwner()->room->GetFrameNo(); context->last_frameno = GetOwner()->room->GetFrameNo();
context->last_pursuit_frameno = GetOwner()->room->GetFrameNo(); context->last_pursuit_frameno = GetOwner()->room->GetFrameNo();
#if 0
auto co = std::make_shared<BtCoroutine>(context, "CoPursuit"); auto co = std::make_shared<BtCoroutine>(context, "CoPursuit");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] ()
@ -359,7 +364,6 @@ behaviac::EBTStatus HeroAgent::DoPursuit()
} }
}; };
return StartCoroutine(co); return StartCoroutine(co);
#endif
} }
behaviac::EBTStatus HeroAgent::DoHelpAttack(int target_uniid) 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); GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
} }
auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED auto context = MAKE_BTCONTEXT
( (
CreatureWeakPtr target; CreatureWeakPtr target;
long long last_frameno = 0; long long last_frameno = 0;
@ -402,7 +406,6 @@ behaviac::EBTStatus HeroAgent::DoHelpAttack(int target_uniid)
context->target = enemy->GetWeakPtrRef(); context->target = enemy->GetWeakPtrRef();
context->last_frameno = GetOwner()->room->GetFrameNo(); context->last_frameno = GetOwner()->room->GetFrameNo();
#if 0
auto co = std::make_shared<BtCoroutine>(context, "CoHelpAttack"); auto co = std::make_shared<BtCoroutine>(context, "CoHelpAttack");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] ()
@ -439,7 +442,6 @@ behaviac::EBTStatus HeroAgent::DoHelpAttack(int target_uniid)
} }
}; };
return StartCoroutine(co); return StartCoroutine(co);
#endif
} }
behaviac::EBTStatus HeroAgent::DoFlyToMasterAround() behaviac::EBTStatus HeroAgent::DoFlyToMasterAround()
@ -527,7 +529,7 @@ behaviac::EBTStatus HeroAgent::DoFollowMaster()
return behaviac::BT_FAILURE; return behaviac::BT_FAILURE;
} }
auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED auto context = MAKE_BTCONTEXT
( (
long long last_frameno = 0; long long last_frameno = 0;
long long last_follow_frameno = 0; long long last_follow_frameno = 0;
@ -536,7 +538,6 @@ behaviac::EBTStatus HeroAgent::DoFollowMaster()
context->last_frameno = GetOwner()->room->GetFrameNo(); context->last_frameno = GetOwner()->room->GetFrameNo();
context->last_follow_frameno = GetOwner()->room->GetFrameNo(); context->last_follow_frameno = GetOwner()->room->GetFrameNo();
#if 0
auto co = std::make_shared<BtCoroutine>(context, "CoFollowMaster"); auto co = std::make_shared<BtCoroutine>(context, "CoFollowMaster");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] ()
@ -565,7 +566,6 @@ behaviac::EBTStatus HeroAgent::DoFollowMaster()
} }
}; };
return StartCoroutine(co); return StartCoroutine(co);
#endif
} }
float HeroAgent::GetMasterDistance() float HeroAgent::GetMasterDistance()