From 9dc816e77b89b4357559efaeaa56b2bb345d6350 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 30 Mar 2021 10:26:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=9B=AE=E6=A0=87=E9=80=89?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/gameserver/creature.cc | 26 ++++++++++++++++++++++++++ server/gameserver/creature.h | 3 +++ server/gameserver/gridcell.cc | 15 ++++++++++----- server/gameserver/gridcell.h | 8 ++++---- server/gameserver/gridservice.cc | 15 ++++++++++++++- server/gameserver/gridservice.h | 3 +++ server/gameserver/human.h | 1 - server/gameserver/zombiemode.ai.cc | 28 ++++++++-------------------- server/gameserver/zombiemode.ai.h | 3 ++- 9 files changed, 70 insertions(+), 32 deletions(-) diff --git a/server/gameserver/creature.cc b/server/gameserver/creature.cc index 5540b077..e6bcea3e 100644 --- a/server/gameserver/creature.cc +++ b/server/gameserver/creature.cc @@ -892,6 +892,18 @@ void Creature::ResetAction() bool Creature::IsProperTarget(Creature* target) { + if (target->dead) { + return false; + } + if (a8::HasBitFlag(target->status, HS_Disable)) { + return false; + } + if (team_id == target->team_id) { + return false; + } + if (room->GetRoomMode() == kZombieMode && GetRace() == target->GetRace()) { + return false; + } return true; } @@ -903,3 +915,17 @@ bool Creature::IsEnemy(Creature* target) return team_id != target->team_id; } } + +void Creature::TouchProperTargets(std::function func) +{ + auto callback = + [this, func] (Creature* c, bool& stop) + { + if (IsProperTarget(c)) { + func(c, stop); + } + }; + room->grid_service->TouchCreatures(room->GetRoomIdx(), + GetGridList(), + callback); +} diff --git a/server/gameserver/creature.h b/server/gameserver/creature.h index 6bb2cd14..cfd99288 100644 --- a/server/gameserver/creature.h +++ b/server/gameserver/creature.h @@ -18,6 +18,7 @@ class Creature : public MoveableEntity { public: + int status = 0; bool dead = false; int team_id = 0; bool aiming = false; @@ -90,6 +91,8 @@ class Creature : public MoveableEntity int GetActionType() { return action_type; } int GetActionTargetId() { return action_target_id; } + void TouchProperTargets(std::function func); + private: virtual void AddBuffPostProc(Creature* caster, Buff* buff); diff --git a/server/gameserver/gridcell.cc b/server/gameserver/gridcell.cc index 94a64e9e..352aed3e 100644 --- a/server/gameserver/gridcell.cc +++ b/server/gameserver/gridcell.cc @@ -29,7 +29,7 @@ void GridCell::ClearRoomData(Room* room) creatures_[room->GetRoomIdx()].clear(); } -void GridCell::TouchHumanList(std::function& func, +void GridCell::TouchHumanList(std::function func, int room_idx, bool& stop) { @@ -45,11 +45,16 @@ void GridCell::TouchHumanList(std::function& func, } } -void GridCell::TouchHumanListEx(std::function func, - int room_idx, - bool& stop) +void GridCell::TouchCreatures(std::function& func, + int room_idx, + bool& stop) { - TouchHumanList(func, room_idx, stop); + for (Creature* c : creatures_[room_idx]) { + func(c, stop); + if (stop) { + return; + } + } } void GridCell::AddBullet(Bullet* bullet) diff --git a/server/gameserver/gridcell.h b/server/gameserver/gridcell.h index 8c3f1b05..307df702 100644 --- a/server/gameserver/gridcell.h +++ b/server/gameserver/gridcell.h @@ -13,12 +13,12 @@ public: GridCell(); void ClearRoomData(Room* room); - void TouchHumanList(std::function& func, + void TouchHumanList(std::function func, + int room_idx, + bool& stop); + void TouchCreatures(std::function& func, int room_idx, bool& stop); - void TouchHumanListEx(std::function func, - int room_idx, - bool& stop); void AddCreature(Creature* c); void RemoveCreature(Creature* c); diff --git a/server/gameserver/gridservice.cc b/server/gameserver/gridservice.cc index 2014a856..d5aa67dd 100644 --- a/server/gameserver/gridservice.cc +++ b/server/gameserver/gridservice.cc @@ -378,11 +378,24 @@ void GridService::TouchAllLayerHumanList(int room_idx, } } +void GridService::TouchCreatures(int room_idx, + std::set& grid_list, + std::function func) +{ + bool stop = false; + for (auto& cell : grid_list) { + cell->TouchHumanList(func, room_idx, stop); + if (stop) { + return; + } + } +} + void GridService::DeatchHuman(Human* target) { for (auto& cell : target->GetGridList()) { bool stop = false; - cell->TouchHumanListEx + cell->TouchHumanList ( [target] (Human* hum, bool& stop) { diff --git a/server/gameserver/gridservice.h b/server/gameserver/gridservice.h index 6533fddd..89dddf81 100644 --- a/server/gameserver/gridservice.h +++ b/server/gameserver/gridservice.h @@ -49,6 +49,9 @@ class GridService void TouchAllLayerHumanList(int room_idx, std::set& grid_list, std::function func); + void TouchCreatures(int room_idx, + std::set& grid_list, + std::function func); void DeatchHuman(Human* hum); private: diff --git a/server/gameserver/human.h b/server/gameserver/human.h index 8921cb1b..bad771f4 100644 --- a/server/gameserver/human.h +++ b/server/gameserver/human.h @@ -82,7 +82,6 @@ class Human : public Creature int lethal_weapon = 0; long long join_frameno = 0; long long enable_frameno = 0; - int status = 0; float atk_add = 0.0f; int emoji1 = 0; int emoji2 = 0; diff --git a/server/gameserver/zombiemode.ai.cc b/server/gameserver/zombiemode.ai.cc index efdc7993..5cf20bf7 100644 --- a/server/gameserver/zombiemode.ai.cc +++ b/server/gameserver/zombiemode.ai.cc @@ -155,7 +155,7 @@ void ZombieModeAI::UpdateThinking() ChangeToState(ZSE_RandomWalk); } } else { - Human* target = GetTarget(); + Creature* target = GetTarget(); if (target) { node_->target = target; ChangeToState(ZSE_Attack); @@ -369,7 +369,7 @@ void ZombieModeAI::ChangeToState(ZombieState_e to_state) node_->exec_frame_num = 0; } -Human* ZombieModeAI::GetTarget() +Creature* ZombieModeAI::GetTarget() { if (GetAiLevel() <= 1) { return nullptr; @@ -379,30 +379,18 @@ Human* ZombieModeAI::GetTarget() return nullptr; } - Human* target = nullptr; - myself->TouchAllLayerHumanList + Creature* target = nullptr; + myself->TouchProperTargets ( - [myself, &target] (Human* hum, bool& stop) + [myself, &target] (Creature* c, bool& stop) { - if (hum->dead) { - return; - } - if (a8::HasBitFlag(hum->status, HS_Disable)) { - return; - } - if (myself->team_id == hum->team_id) { - return; - } - if (myself->GetRace() == hum->GetRace()) { - return; - } if (target) { if (myself->GetPos().ManhattanDistance(target->GetPos()) > - myself->GetPos().ManhattanDistance(hum->GetPos())) { - target = hum; + myself->GetPos().ManhattanDistance(c->GetPos())) { + target = c; } } else { - target = hum; + target = c; } }); if (target) { diff --git a/server/gameserver/zombiemode.ai.h b/server/gameserver/zombiemode.ai.h index 677cc911..34696c2b 100644 --- a/server/gameserver/zombiemode.ai.h +++ b/server/gameserver/zombiemode.ai.h @@ -12,6 +12,7 @@ enum ZombieState_e }; class Human; +class Creature; class ZombieAINode; class ZombieModeAI : public AIComponent { @@ -35,7 +36,7 @@ private: void DoShot(); void DoSkill(int skill_id); - Human* GetTarget(); + Creature* GetTarget(); float GetAttackRange(); int GetAttackTimes();