From 167d9b6facce505c849a839aebc6e18ef83f5e66 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 26 Aug 2021 16:28:21 +0800 Subject: [PATCH] 1 --- server/gameserver/incubator.cc | 35 +++++++++++++++++++++++++++++++-- server/gameserver/incubator.h | 3 ++- server/gameserver/player.cc | 36 ++++++++++++++++++++++++++-------- server/gameserver/player.h | 2 ++ server/gameserver/room.cc | 15 ++++++++------ 5 files changed, 74 insertions(+), 17 deletions(-) diff --git a/server/gameserver/incubator.cc b/server/gameserver/incubator.cc index 63f3b7b..52c599f 100644 --- a/server/gameserver/incubator.cc +++ b/server/gameserver/incubator.cc @@ -39,7 +39,7 @@ void Incubator::AllocAndroid(Human* target, int num) Human* hum = hold_humans_[0]; a8::Vec2 old_pos = hum->GetPos(); hum->SetPos(target->GetPos() + dir * (MetaMgr::Instance()->incubator_base_length + rand_len)); - if (hum->CollisonDetection() || !CanSet(hum, target)) { + if (hum->CollisonDetection() || !CanSee(hum, target)) { hum->SetPos(old_pos); } else { room->EnableHuman(hum); @@ -125,7 +125,7 @@ void Incubator::RecycleAndroid(Human* hum) } } -bool Incubator::CanSet(Human* hum, Human* exclude_hum) +bool Incubator::CanSee(Human* hum, Human* exclude_hum) { Human* target = hum; bool can_set = true; @@ -184,3 +184,34 @@ void Incubator::AutoAllocAndroid() break; } } + +void Incubator::ActiveAndroid(Human* hum, Human* android) +{ + a8::Vec2 center = room->GetGasData().pos_new; + float start_distance = a8::RandEx(200, 600); + a8::Vec2 start_dir = a8::Vec2::UP; + start_dir.Rotate(a8::RandAngle()); + + android->SetPos(hum->GetPos()); + int try_count = 0; + while (++try_count < 100) { + android->SetPos(center + start_dir * start_distance); + if (android->CollisonDetection() && !CanSee(android, hum)) { + break; + } + } + room->EnableHuman(android); + for (auto itr = hold_humans_.begin(); itr != hold_humans_.end(); ++itr) { + if (*itr == android) { + hold_humans_.erase(itr); + } + } +#ifdef DEBUG + room->BroadcastDebugMsg(a8::Format("active android id:%d pos:%d,%d", + { + android->GetUniId(), + android->GetPos().x, + android->GetPos().y + })); +#endif +} diff --git a/server/gameserver/incubator.h b/server/gameserver/incubator.h index 47a293b..6504362 100644 --- a/server/gameserver/incubator.h +++ b/server/gameserver/incubator.h @@ -13,9 +13,10 @@ class Incubator void UnInit(); void AllocAndroid(Human* target, int num); void RecycleAndroid(Human* hum); + void ActiveAndroid(Human* hum, Human* android); private: - bool CanSet(Human* hum, Human* exclude_hum); + bool CanSee(Human* hum, Human* exclude_hum); void AutoAllocAndroid(); private: diff --git a/server/gameserver/player.cc b/server/gameserver/player.cc index 798c260..6e2b3bb 100644 --- a/server/gameserver/player.cc +++ b/server/gameserver/player.cc @@ -1393,15 +1393,35 @@ void Player::_CMGameOver(f8::MsgHdr& hdr, const cs::CMGameOver& msg) void Player::_CMWatchWar(f8::MsgHdr& hdr, const cs::CMWatchWar& msg) { - cs::SMWatchWar respmsg; - Human* target = room->GetWatchWarTarget(this); - if (target) { - SendNotifyMsg(respmsg); - hdr.hum->FollowTarget(target); - } else { - respmsg.set_error_code(1); - SendNotifyMsg(respmsg); + if (watch_war_req_timer_) { + return; } + auto cb = + [] (const a8::XParams& param) { + #if 0 + cs::SMWatchWar respmsg; + Human* target = room->GetWatchWarTarget(this); + if (target) { + SendNotifyMsg(respmsg); + hdr.hum->FollowTarget(target); + } else { + respmsg.set_error_code(1); + SendNotifyMsg(respmsg); + } + #endif + }; + watch_war_req_timer_ = room->xtimer.AddDeadLineTimerAndAttach + ( + NEXT_FRAME_TIMER, + a8::XParams() + .SetSender(this), + cb, + &xtimer_attacher.timer_list_, + [] (const a8::XParams& param) + { + Player* hum = (Player*)param.sender.GetUserData(); + hum->watch_war_req_timer_ = nullptr; + }); } void Player::_CMLeave(f8::MsgHdr& hdr, const cs::CMLeave& msg) diff --git a/server/gameserver/player.h b/server/gameserver/player.h index 29f345c..5715217 100644 --- a/server/gameserver/player.h +++ b/server/gameserver/player.h @@ -17,6 +17,7 @@ namespace cs class Room; class Loot; class Obstacle; +struct xtimer_list; class Player : public Human { public: @@ -134,6 +135,7 @@ private: std::map>> box_hash_; std::set receved_box_hash_; long long last_cmmove_frameno_ = 0; + xtimer_list* watch_war_req_timer_ = nullptr; friend class EntityFactory; }; diff --git a/server/gameserver/room.cc b/server/gameserver/room.cc index 2f6b7fb..49c2626 100644 --- a/server/gameserver/room.cc +++ b/server/gameserver/room.cc @@ -923,6 +923,7 @@ Human* Room::GetWatchWarTarget(Human* hum) return member; } } + std::vector players; std::vector humans; for (auto& pair : human_hash_) { @@ -934,15 +935,17 @@ Human* Room::GetWatchWarTarget(Human* hum) } } } + + Human* target = nullptr; if (!players.empty()) { - Human* target = players[rand() % players.size()]; - return target; + target = players[rand() % players.size()]; + } else if (!humans.empty()) { + target = humans[rand() % humans.size()]; } - if (!humans.empty()) { - Human* target = humans[rand() % humans.size()]; - return target; + if (target && target->IsAndroid() && a8::HasBitFlag(target->status, CS_Disable)) { + incubator_->ActiveAndroid(hum, target); } - return nullptr; + return target; } bool Room::BattleStarted()