From 418d98deea6c30f4d7cdd4160c77f606e02c909c Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 30 Mar 2021 14:05:06 +0800 Subject: [PATCH] 1 --- server/gameserver/hero.ai.cc | 24 ++++++++++++------------ server/gameserver/hero.ai.h | 5 +++-- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/server/gameserver/hero.ai.cc b/server/gameserver/hero.ai.cc index 348b0b0..a1fdfa9 100644 --- a/server/gameserver/hero.ai.cc +++ b/server/gameserver/hero.ai.cc @@ -136,7 +136,7 @@ void HeroAI::UpdateThinking() } else { Human* target = GetTarget(); if (target) { - node_.target = target; + node_.target.Attach(target); ChangeToStateAI(HSE_Attack); } else { if (hero->room->GetFrameNo() >= node_.next_random_move_frameno) { @@ -156,7 +156,7 @@ void HeroAI::UpdateThinking() void HeroAI::UpdateAttack() { Hero* myself = (Hero*)owner; - if (!node_.target || node_.target->dead) { + if (!node_.target.Get() || node_.target.Get()->dead) { ChangeToStateAI(HSE_Thinking); return; } @@ -164,7 +164,7 @@ void HeroAI::UpdateAttack() ChangeToStateAI(HSE_Thinking); return; } - float distance = myself->GetPos().Distance(node_.target->GetPos()); + float distance = myself->GetPos().Distance(node_.target.Get()->GetPos()); if (distance > GetAttackRange()) { if (ai_meta->i->pursuit_radius() <= 0) { //站桩 @@ -222,7 +222,7 @@ void HeroAI::UpdateRandomWalk() void HeroAI::UpdatePursuit() { Hero* myself = (Hero*)owner; - float distance = myself->GetPos().Distance(node_.target->GetPos()); + float distance = myself->GetPos().Distance(node_.target.Get()->GetPos()); if (!myself->HasBuffEffect(kBET_Jump) && distance < GetAttackRange()) { ChangeToStateAI(HSE_Attack); @@ -255,7 +255,7 @@ void HeroAI::ChangeToStateAI(HeroState_e to_state) switch (to_state) { case HSE_Idle: { - node_.target = nullptr; + node_.target.Reset(); node_.param1 = 0; node_.start_shot_frameno = 0; node_.shot_times = 0; @@ -271,7 +271,7 @@ void HeroAI::ChangeToStateAI(HeroState_e to_state) break; case HSE_Thinking: { - node_.target = nullptr; + node_.target.Reset(); node_.param1 = 0; node_.start_shot_frameno = 0; node_.shot_times = 0; @@ -290,7 +290,7 @@ void HeroAI::ChangeToStateAI(HeroState_e to_state) case HSE_RandomWalk: { moving_ = true; - node_.target = nullptr; + node_.target.Reset(); #if 1 node_.param1 = SERVER_FRAME_RATE * ai_meta->GetMoveTime(); #else @@ -314,8 +314,8 @@ void HeroAI::ChangeToStateAI(HeroState_e to_state) case HSE_Pursuit: { moving_ = true; - if (node_.target) { - hero->move_dir = node_.target->GetPos() - hero->GetPos(); + if (node_.target.Get()) { + hero->move_dir = node_.target.Get()->GetPos() - hero->GetPos(); hero->move_dir.Normalize(); #if 0 hero->attack_dir = hero->move_dir; @@ -363,7 +363,7 @@ Human* HeroAI::GetTarget() } }); if (target) { - node_.nearest_human = target; + node_.nearest_human.Attach(target); node_.last_check_nearest_human_frameno = myself->room->GetFrameNo(); float distance = myself->GetPos().Distance(target->GetPos()); if (distance > GetAttackRange()) { @@ -389,14 +389,14 @@ float HeroAI::GetAttackRange() void HeroAI::DoShotAI() { Hero* myself = (Hero*)owner; - if (!node_.target) { + if (!node_.target.Get()) { return; } bool shot_ok = false; a8::Vec2 shot_dir = myself->attack_dir; if (node_.total_shot_times >= node_.next_total_shot_times) { - shot_dir = node_.target->GetPos() - myself->GetPos(); + shot_dir = node_.target.Get()->GetPos() - myself->GetPos(); node_.next_total_shot_times += 7 + (rand() % 6); myself->attack_dir = shot_dir; } diff --git a/server/gameserver/hero.ai.h b/server/gameserver/hero.ai.h index 4442c50..7d853a0 100644 --- a/server/gameserver/hero.ai.h +++ b/server/gameserver/hero.ai.h @@ -1,6 +1,7 @@ #pragma once #include "aicomponent.h" +#include "weakptr.h" namespace MetaData { @@ -30,8 +31,8 @@ public: int next_total_shot_times = 0; long long param1 = 0; - Human* target = nullptr; - Human* nearest_human = nullptr; + CreatureWeakPtr target; + CreatureWeakPtr nearest_human; long long last_check_nearest_human_frameno = 0; a8::Vec2 shot_dir; };