This commit is contained in:
aozhiwei 2021-03-30 11:54:45 +08:00
parent b6cb96c11c
commit d707b56f54

View File

@ -31,8 +31,8 @@ public:
int next_total_shot_times = 0; int next_total_shot_times = 0;
long long param1 = 0; long long param1 = 0;
Creature* target = nullptr; CreatureWeakPtr target;
Creature* nearest_human = nullptr; CreatureWeakPtr nearest_human;
long long last_check_nearest_human_frameno = 0; long long last_check_nearest_human_frameno = 0;
a8::Vec2 shot_dir; a8::Vec2 shot_dir;
@ -157,7 +157,7 @@ void ZombieModeAI::UpdateThinking()
} else { } else {
Creature* target = GetTarget(); Creature* target = GetTarget();
if (target) { if (target) {
node_->target = target; node_->target.Attach(target);
ChangeToState(ZSE_Attack); ChangeToState(ZSE_Attack);
} else { } else {
if (hum->room->GetFrameNo() >= node_->next_random_move_frameno) { if (hum->room->GetFrameNo() >= node_->next_random_move_frameno) {
@ -180,16 +180,16 @@ void ZombieModeAI::UpdateAttack()
if (myself->HasBuffEffect(kBET_Vertigo)) { if (myself->HasBuffEffect(kBET_Vertigo)) {
return; return;
} }
if (!node_->target || node_->target->dead) { if (!node_->target.Get() || node_->target.Get()->dead) {
ChangeToState(ZSE_Thinking); ChangeToState(ZSE_Thinking);
return; return;
} }
if (node_->exec_frame_num > SERVER_FRAME_RATE * 8 || if (node_->exec_frame_num > SERVER_FRAME_RATE * 8 ||
myself->GetRace() == node_->target->GetRace()) { myself->GetRace() == node_->target.Get()->GetRace()) {
ChangeToState(ZSE_Thinking); ChangeToState(ZSE_Thinking);
return; return;
} }
float distance = myself->GetPos().Distance(node_->target->GetPos()); float distance = myself->GetPos().Distance(node_->target.Get()->GetPos());
if (distance > GetAttackRange()) { if (distance > GetAttackRange()) {
Skill* skill = myself->SelectSkill(); Skill* skill = myself->SelectSkill();
if (myself->CanUseSkill(skill->meta->i->skill_id()) && if (myself->CanUseSkill(skill->meta->i->skill_id()) &&
@ -261,7 +261,7 @@ void ZombieModeAI::UpdateRandomWalk()
void ZombieModeAI::UpdatePursuit() void ZombieModeAI::UpdatePursuit()
{ {
Human* myself = (Human*)owner; Human* myself = (Human*)owner;
float distance = myself->GetPos().Distance(node_->target->GetPos()); float distance = myself->GetPos().Distance(node_->target.Get()->GetPos());
if (distance < GetAttackRange()) { if (distance < GetAttackRange()) {
ChangeToState(ZSE_Attack); ChangeToState(ZSE_Attack);
} else { } else {
@ -287,11 +287,11 @@ void ZombieModeAI::DoMove()
int speed = std::max(1, (int)hum->GetSpeed()) * 1; int speed = std::max(1, (int)hum->GetSpeed()) * 1;
hum->_UpdateMove(speed); hum->_UpdateMove(speed);
hum->on_move_collision = nullptr; hum->on_move_collision = nullptr;
if (node_->nearest_human) { if (node_->nearest_human.Get()) {
if (node_->main_state != ZSE_Pursuit && if (node_->main_state != ZSE_Pursuit &&
hum->GetPos().ManhattanDistance(node_->nearest_human->GetPos()) < 200) { hum->GetPos().ManhattanDistance(node_->nearest_human.Get()->GetPos()) < 200) {
ChangeToState(ZSE_Thinking); ChangeToState(ZSE_Thinking);
} else if (hum->GetPos().ManhattanDistance(node_->nearest_human->GetPos()) > 800) { } else if (hum->GetPos().ManhattanDistance(node_->nearest_human.Get()->GetPos()) > 800) {
GetTarget(); GetTarget();
} }
} }
@ -304,7 +304,7 @@ void ZombieModeAI::ChangeToState(ZombieState_e to_state)
switch (to_state) { switch (to_state) {
case ZSE_Idle: case ZSE_Idle:
{ {
node_->target = nullptr; node_->target.Reset();
node_->param1 = 0; node_->param1 = 0;
node_->start_shot_frameno = 0; node_->start_shot_frameno = 0;
node_->shot_times = 0; node_->shot_times = 0;
@ -319,7 +319,7 @@ void ZombieModeAI::ChangeToState(ZombieState_e to_state)
break; break;
case ZSE_Thinking: case ZSE_Thinking:
{ {
node_->target = nullptr; node_->target.Reset();
node_->param1 = 0; node_->param1 = 0;
node_->start_shot_frameno = 0; node_->start_shot_frameno = 0;
node_->shot_times = 0; node_->shot_times = 0;
@ -338,7 +338,7 @@ void ZombieModeAI::ChangeToState(ZombieState_e to_state)
case ZSE_RandomWalk: case ZSE_RandomWalk:
{ {
node_->moving = true; node_->moving = true;
node_->target = nullptr; node_->target.Reset();
node_->param1 = SERVER_FRAME_RATE * node_->ai_meta->GetMoveTime(); node_->param1 = SERVER_FRAME_RATE * node_->ai_meta->GetMoveTime();
node_->start_shot_frameno = 0; node_->start_shot_frameno = 0;
node_->shot_times = 0; node_->shot_times = 0;
@ -356,8 +356,8 @@ void ZombieModeAI::ChangeToState(ZombieState_e to_state)
case ZSE_Pursuit: case ZSE_Pursuit:
{ {
node_->moving = true; node_->moving = true;
if (node_->target) { if (node_->target.Get()) {
hum->move_dir = node_->target->GetPos() - hum->GetPos(); hum->move_dir = node_->target.Get()->GetPos() - hum->GetPos();
hum->move_dir.Normalize(); hum->move_dir.Normalize();
hum->attack_dir = hum->move_dir; hum->attack_dir = hum->move_dir;
} }
@ -394,7 +394,7 @@ Creature* ZombieModeAI::GetTarget()
} }
}); });
if (target) { if (target) {
node_->nearest_human = target; node_->nearest_human.Attach(target);
node_->last_check_nearest_human_frameno = myself->room->GetFrameNo(); node_->last_check_nearest_human_frameno = myself->room->GetFrameNo();
float distance = myself->GetPos().Distance(target->GetPos()); float distance = myself->GetPos().Distance(target->GetPos());
#if 0 #if 0
@ -427,14 +427,14 @@ float ZombieModeAI::GetAttackRange()
void ZombieModeAI::DoShot() void ZombieModeAI::DoShot()
{ {
Human* myself = (Human*)owner; Human* myself = (Human*)owner;
if (!node_->target) { if (!node_->target.Get()) {
return; return;
} }
bool shot_ok = false; bool shot_ok = false;
a8::Vec2 shot_dir = myself->attack_dir; a8::Vec2 shot_dir = myself->attack_dir;
if (node_->total_shot_times >= node_->next_total_shot_times) { 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); node_->next_total_shot_times += 7 + (rand() % 6);
myself->attack_dir = shot_dir; myself->attack_dir = shot_dir;
} }
@ -467,7 +467,7 @@ void ZombieModeAI::DoShot()
void ZombieModeAI::DoSkill(int skill_id) void ZombieModeAI::DoSkill(int skill_id)
{ {
Human* myself = (Human*)owner; Human* myself = (Human*)owner;
myself->DoSkill(skill_id, node_->target->GetEntityUniId(), a8::Vec2(), node_->target->GetPos()); myself->DoSkill(skill_id, node_->target.Get()->GetEntityUniId(), a8::Vec2(), node_->target.Get()->GetPos());
} }
int ZombieModeAI::GetAttackTimes() int ZombieModeAI::GetAttackTimes()