221 lines
7.2 KiB
C++
221 lines
7.2 KiB
C++
#include "precompile.h"
|
|
|
|
#include "incubator.h"
|
|
#include "room.h"
|
|
#include "human.h"
|
|
#include "metamgr.h"
|
|
|
|
void Incubator::Init()
|
|
{
|
|
xtimer_attacher_.xtimer = &room->xtimer;
|
|
room->xtimer.AddRepeatTimerAndAttach
|
|
(
|
|
SERVER_FRAME_RATE * (2 + rand() % 3),
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Incubator* incubator = (Incubator*)param.sender.GetUserData();
|
|
incubator->AutoAllocAndroid();
|
|
},
|
|
&xtimer_attacher_.timer_list_);
|
|
}
|
|
|
|
void Incubator::UnInit()
|
|
{
|
|
|
|
}
|
|
|
|
void Incubator::AllocAndroid(Human* target, int num)
|
|
{
|
|
if (room->xtimer.GetRunningTimer() == nullptr) {
|
|
abort();
|
|
}
|
|
int try_count = 0;
|
|
a8::Vec2 dir = a8::Vec2::UP;
|
|
while (num > 0 && try_count < 20 && !hold_humans_.empty()) {
|
|
dir.Rotate(a8::RandAngle());
|
|
int rand_len = rand() % MetaMgr::Instance()->incubator_rand_length;
|
|
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() || !CanSee(hum, target)) {
|
|
hum->SetPos(old_pos);
|
|
} else {
|
|
room->EnableHuman(hum);
|
|
#ifdef DEBUG
|
|
#if 0
|
|
if (!target->InNewObjects(hum)) {
|
|
abort();
|
|
}
|
|
#endif
|
|
if (hum->dead) {
|
|
abort();
|
|
}
|
|
#endif
|
|
hum->MustBeAddBuff(hum, kTraceBuffId);
|
|
hold_humans_.erase(hold_humans_.begin());
|
|
--num;
|
|
#ifdef DEBUG
|
|
room->BroadcastDebugMsg(a8::Format("投放机器人 %d:%s pos:%d,%d pos1:%d,%d %d num:%d",
|
|
{hum->GetUniId(),
|
|
hum->name,
|
|
hum->GetPos().x,
|
|
hum->GetPos().y,
|
|
target->GetPos().x,
|
|
target->GetPos().y,
|
|
hum->GetPos().Distance(target->GetPos()),
|
|
hold_humans_.size()}));
|
|
#endif
|
|
}
|
|
++try_count;
|
|
}
|
|
#ifdef DEBUG
|
|
if (num > 0) {
|
|
room->BroadcastDebugMsg(a8::Format("投放机器人 分配失败 %d", {hold_humans_.size()}));
|
|
}
|
|
#endif
|
|
}
|
|
|
|
void Incubator::RecycleAndroid(Human* hum)
|
|
{
|
|
Human* nearest_hum = nullptr;
|
|
Human* target = hum;
|
|
float distance = 10000000;
|
|
room->TraverseAlivePlayers
|
|
(
|
|
a8::XParams(),
|
|
[&nearest_hum, target, &distance] (Human* hum, a8::XParams& param) -> bool
|
|
{
|
|
float tmp_distance = hum->GetPos().ManhattanDistance(target->GetPos());
|
|
if (tmp_distance < distance) {
|
|
nearest_hum = hum;
|
|
distance = tmp_distance;
|
|
}
|
|
return true;
|
|
});
|
|
if (hum->dead) {
|
|
#ifdef DEBUG
|
|
room->BroadcastDebugMsg(a8::Format("回收机器人 %d:%s 角色已死亡",
|
|
{hum->GetUniId(),
|
|
hum->name}));
|
|
#endif
|
|
hum->RemoveBuffByEffectId(kBET_BeRecycle);
|
|
return;
|
|
}
|
|
if (distance < MetaMgr::Instance()->incubator_canset_distance) {
|
|
#ifdef DEBUG
|
|
room->BroadcastDebugMsg(a8::Format("回收机器人 %d:%s 距离太近",
|
|
{hum->GetUniId(),
|
|
hum->name}));
|
|
#endif
|
|
hum->RemoveBuffByEffectId(kBET_BeRecycle);
|
|
return;
|
|
}
|
|
if (distance > MetaMgr::Instance()->incubator_canset_distance + 100) {
|
|
hum->RemoveBuffByEffectId(kBET_BeRecycle);
|
|
hold_humans_.push_back(hum);
|
|
room->DisableHuman(hum);
|
|
#ifdef DEBUG
|
|
room->BroadcastDebugMsg(a8::Format("回收机器人 %d:%s:%d 添加到回收列表",
|
|
{hum->GetUniId(),
|
|
hum->name,
|
|
hold_humans_.size()
|
|
}));
|
|
#endif
|
|
return;
|
|
}
|
|
}
|
|
|
|
bool Incubator::CanSee(Human* hum, Human* exclude_hum)
|
|
{
|
|
Human* target = hum;
|
|
bool can_see = true;
|
|
room->TraverseAlivePlayers
|
|
(
|
|
a8::XParams(),
|
|
[target, exclude_hum, &can_see] (Human* hum, a8::XParams& param) -> bool
|
|
{
|
|
if (hum != exclude_hum) {
|
|
if (target->GetPos().ManhattanDistance(hum->GetPos()) <
|
|
MetaMgr::Instance()->incubator_canset_distance) {
|
|
can_see = false;
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
});
|
|
return can_see;
|
|
}
|
|
|
|
void Incubator::AutoAllocAndroid()
|
|
{
|
|
switch (room->GetGasData().gas_mode) {
|
|
case GasWaiting:
|
|
case GasMoving:
|
|
{
|
|
if (!hold_humans_.empty()){
|
|
Human* hum = hold_humans_[0];
|
|
if (room->GetGasData().gas_mode == GasWaiting &&
|
|
hold_humans_.size() > 1 &&
|
|
((rand() % 100) > 40)) {
|
|
Human* killer = nullptr;
|
|
if (hold_humans_.size() == 2) {
|
|
killer = hold_humans_[1];
|
|
} else {
|
|
killer = hold_humans_[1 + (rand() % (hold_humans_.size() - 1))];
|
|
}
|
|
hum->BeKill(killer->GetUniId(),
|
|
killer->name,
|
|
killer->GetCurrWeapon()->weapon_id);
|
|
} else if (room->GetGasData().gas_count > 1) {
|
|
hum->BeKill(VP_Gas,
|
|
TEXT("battle_server_killer_gas", "毒圈"),
|
|
VW_Gas);
|
|
} else {
|
|
return;
|
|
}
|
|
hold_humans_.erase(hold_humans_.begin());
|
|
room->xtimer.ModifyTimer(room->xtimer.GetRunningTimer(), SERVER_FRAME_RATE * (3 + rand() % 5));
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
}
|
|
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);
|
|
break;
|
|
}
|
|
}
|
|
#ifdef DEBUG
|
|
room->BroadcastDebugMsg(a8::Format("active android id:%d pos:%d,%d",
|
|
{
|
|
android->GetUniId(),
|
|
android->GetPos().x,
|
|
android->GetPos().y
|
|
}));
|
|
#endif
|
|
}
|