3393 lines
108 KiB
C++
3393 lines
108 KiB
C++
#include "precompile.h"
|
|
|
|
#include <random>
|
|
|
|
#include <a8/mutable_xobject.h>
|
|
#include <a8/timer.h>
|
|
#include <a8/udplog.h>
|
|
#include <a8/collision.h>
|
|
|
|
#include "playermgr.h"
|
|
#include "player.h"
|
|
#include "cs_proto.pb.h"
|
|
#include "room.h"
|
|
#include "android.h"
|
|
#include "metamgr.h"
|
|
#include "bullet.h"
|
|
#include "collider.h"
|
|
#include "obstacle.h"
|
|
#include "roomobstacle.h"
|
|
#include "building.h"
|
|
#include "loot.h"
|
|
#include "roommgr.h"
|
|
#include "app.h"
|
|
#include "gamelog.h"
|
|
#include "typeconvert.h"
|
|
#include "entityfactory.h"
|
|
#include "perfmonitor.h"
|
|
|
|
#include "framework/cpp/utils.h"
|
|
|
|
const size_t NORMAL_ROOM_MAX_PLAYER_NUM = 40;
|
|
const size_t MINI_ROOM_MAX_PLAYER_NUM = 20;
|
|
|
|
const int SHUA_RANGE = 580;
|
|
|
|
static long long RoomXGetTickCount(void* context)
|
|
{
|
|
Room* room = (Room*)context;
|
|
return room->GetFrameNo();
|
|
}
|
|
|
|
Room::~Room()
|
|
{
|
|
|
|
}
|
|
|
|
void Room::InitData(RoomInitInfo& init_info)
|
|
{
|
|
room_idx_ = init_info.room_idx;
|
|
room_mode_ = init_info.room_mode;
|
|
room_uuid_ = init_info.room_uuid;
|
|
room_type_ = init_info.room_type;
|
|
creator_game_times_ = init_info.creator_game_times;
|
|
creator_register_time_ = init_info.creator_register_time;
|
|
force_entry_newbie_room_ = init_info.force_entry_newbie_room;
|
|
|
|
map_tpl_name_ = init_info.map_tpl_name;
|
|
grid_service = init_info.grid_service;
|
|
map_service = init_info.map_service;
|
|
map_meta_ = init_info.map_meta;
|
|
mini_room_spawn_points_ = init_info.mini_room_spawn_points;
|
|
normal_room_spawn_points_ = init_info.normal_room_spawn_points;
|
|
level0room_born_point_meta_ = init_info.level0room_born_point_meta;
|
|
level1room_born_point_meta_ = init_info.level1room_born_point_meta;
|
|
loots_ = init_info.loots;
|
|
buildings_ = init_info.buildings;
|
|
level0room_spec_things_ = init_info.level0room_spec_things;
|
|
}
|
|
|
|
void Room::Init()
|
|
{
|
|
xtimer.Init(RoomXGetTickCount, this, 100, 100);
|
|
xtimer_attacher_.xtimer = &xtimer;
|
|
|
|
CreateSpawnPoints();
|
|
CreateLoots();
|
|
CreateDropObjs();
|
|
InitObstacleDatas();
|
|
ShuaAndroid();
|
|
if (room_type_ == RT_NewBrid && creator_game_times_ <= 0) {
|
|
CreateLevel0RoomSpecThings();
|
|
}
|
|
#ifdef DEBUG
|
|
InitDebugInfo();
|
|
#endif
|
|
}
|
|
|
|
void Room::UnInit()
|
|
{
|
|
#ifdef DEBUG
|
|
UnInitDebugInfo();
|
|
#endif
|
|
timer_attacher.ClearTimerList();
|
|
xtimer_attacher_.ClearTimerList();
|
|
for (auto& pair : accountid_hash_) {
|
|
PlayerMgr::Instance()->RemovePlayerBySocket(pair.second->socket_handle);
|
|
}
|
|
for (auto& pair : uniid_hash_) {
|
|
delete pair.second;
|
|
}
|
|
uniid_hash_.clear();
|
|
for (auto& pair : later_add_hash_) {
|
|
delete pair.second;
|
|
}
|
|
later_add_hash_.clear();
|
|
for (auto& pair : removed_robot_hash_) {
|
|
delete pair.second;
|
|
}
|
|
removed_robot_hash_.clear();
|
|
grid_service->ClearRoomData(this);
|
|
PerfMonitor::Instance()->alive_count -= alive_count_;
|
|
}
|
|
|
|
void Room::Update(int delta_time)
|
|
{
|
|
xtimer.Update();
|
|
if (IsGameOver() && GetFrameNo() - game_over_frameno_ > SERVER_FRAME_RATE * 20) {
|
|
return;
|
|
}
|
|
|
|
elapsed_time_ += delta_time;
|
|
while (elapsed_time_ >= 50) {
|
|
if (GetFrameNo() % 2 == 0) {
|
|
UpdateGas();
|
|
}
|
|
for (auto& pair : moveable_hash_) {
|
|
pair.second->Update(50);
|
|
}
|
|
if (GetFrameNo() % 2 == 0) {
|
|
SyncFrameData();
|
|
}
|
|
++frameno_;
|
|
elapsed_time_ -= 50;
|
|
}
|
|
}
|
|
|
|
int Room::GetPlayerNum()
|
|
{
|
|
return accountid_hash_.size();
|
|
}
|
|
|
|
Player* Room::GetPlayerByAccountId(const std::string& accountid)
|
|
{
|
|
auto itr = accountid_hash_.find(accountid);
|
|
return itr != accountid_hash_.end() ? itr->second : nullptr;
|
|
}
|
|
|
|
Player* Room::GetPlayerByUniId(int uniid)
|
|
{
|
|
Entity* entity = GetEntityByUniId(uniid);
|
|
if (!entity) {
|
|
return nullptr;
|
|
}
|
|
return entity->IsEntityType(ET_Player) && entity->IsEntitySubType(EST_Player) ?
|
|
(Player*)entity : nullptr;
|
|
}
|
|
|
|
Entity* Room::GetEntityByUniId(int uniid)
|
|
{
|
|
auto itr = uniid_hash_.find(uniid);
|
|
return itr != uniid_hash_.end() ? itr->second : nullptr;
|
|
}
|
|
|
|
int Room::AliveCount()
|
|
{
|
|
return alive_count_;
|
|
}
|
|
|
|
Player* Room::NewPlayer()
|
|
{
|
|
Player* hum = EntityFactory::Instance()->MakePlayer(AllocUniid());
|
|
return hum;
|
|
}
|
|
|
|
void Room::AddPlayer(Player* hum)
|
|
{
|
|
if (gas_data_.gas_mode != GasInactive) {
|
|
abort();
|
|
}
|
|
while (human_hash_.size() >= GetRoomMaxPlayerNum()) {
|
|
RandRemoveAndroid();
|
|
}
|
|
hum->born_point = AllocBornPoint(hum);
|
|
if (!hum->born_point) {
|
|
hum->SetPos(GetDefaultBornPoint());
|
|
} else {
|
|
hum->SetPos(hum->born_point->RandPoint());
|
|
}
|
|
hum->attack_dir = hum->GetPos();
|
|
hum->attack_dir.Normalize();
|
|
hum->attack_dir.Rotate(a8::RandAngle());
|
|
hum->move_dir = hum->attack_dir;
|
|
|
|
hum->room = this;
|
|
hum->join_frameno = GetFrameNo();
|
|
hum->Initialize();
|
|
AddToEntityHash(hum);
|
|
AddToMoveableHash(hum);
|
|
AddToAccountHash(hum);
|
|
AddToHumanHash(hum);
|
|
AddToAliveHumanHash(hum);
|
|
MatchTeam(hum);
|
|
hum->PushJoinRoomMsg();
|
|
++alive_count_;
|
|
alive_count_chged_frameno_ = GetFrameNo();
|
|
++PerfMonitor::Instance()->alive_count;
|
|
|
|
grid_service->AddHuman(hum);
|
|
hum->FindLocation();
|
|
hum->RefreshView();
|
|
AddPlayerPostProc(hum);
|
|
NotifyUiUpdate();
|
|
GameLog::Instance()->GameStart(hum);
|
|
}
|
|
|
|
int Room::AllocUniid()
|
|
{
|
|
while (GetEntityByUniId(++current_uniid_) ||
|
|
later_add_hash_.find(current_uniid_) != later_add_hash_.end() ||
|
|
current_uniid_ == 0) {}
|
|
return current_uniid_;
|
|
}
|
|
|
|
void Room::ShuaAndroid()
|
|
{
|
|
if (gas_data_.gas_mode != GasInactive) {
|
|
return;
|
|
}
|
|
int robot_num = a8::RandEx(MetaMgr::Instance()->refresh_robot_min_num,
|
|
MetaMgr::Instance()->refresh_robot_max_num);
|
|
int refresh_time = a8::RandEx(MetaMgr::Instance()->refresh_robot_min_time,
|
|
MetaMgr::Instance()->refresh_robot_max_time);
|
|
if (robot_num > 0 && refresh_time > 0) {
|
|
if (IsMiniRoom()) {
|
|
#if 0
|
|
robot_num /= 2;
|
|
robot_num = std::max(1, robot_num);
|
|
#endif
|
|
refresh_time /= 2;
|
|
refresh_time = std::max(2, refresh_time);
|
|
}
|
|
CreateAndroid(robot_num);
|
|
xtimer.AddDeadLineTimerAndAttach(SERVER_FRAME_RATE * refresh_time,
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
room->ShuaAndroid();
|
|
},
|
|
&xtimer_attacher_.timer_list_);
|
|
}
|
|
}
|
|
|
|
void Room::ShowAndroid(Human* target, int num)
|
|
{
|
|
if (!target->born_point) {
|
|
return;
|
|
}
|
|
int i = 0;
|
|
for (auto& pair : human_hash_) {
|
|
Human* hum = pair.second;
|
|
if (hum->IsAndroid() &&
|
|
a8::HasBitFlag(hum->status, HS_Disable)) {
|
|
if (hum->born_point) {
|
|
DecBornPointHumanNum(hum->born_point, hum);
|
|
}
|
|
hum->born_point = target->born_point;
|
|
IncBornPointHumanNum(hum->born_point, hum);
|
|
hum->SetPos(hum->born_point->RandPoint());
|
|
EnableHuman(hum);
|
|
++i;
|
|
if (i >= num) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::CreateAndroid(int robot_num)
|
|
{
|
|
for (int i = 0; i < robot_num; ++i) {
|
|
if (human_hash_.size() >= GetRoomMaxPlayerNum()) {
|
|
return;
|
|
}
|
|
MetaData::Robot* robot_meta = MetaMgr::Instance()->RandRobot(refreshed_robot_set_);
|
|
if (!robot_meta) {
|
|
abort();
|
|
}
|
|
|
|
Android* hum = EntityFactory::Instance()->MakeAndroid(AllocUniid());
|
|
hum->name = robot_meta->i->name();
|
|
hum->meta = MetaMgr::Instance()->android_meta;
|
|
hum->robot_meta = robot_meta;
|
|
hum->born_point = AllocBornPoint(hum);
|
|
if (!hum->born_point) {
|
|
hum->SetPos(GetDefaultBornPoint());
|
|
} else {
|
|
hum->SetPos(hum->born_point->RandPoint());
|
|
}
|
|
hum->attack_dir = hum->GetPos();
|
|
hum->attack_dir.Normalize();
|
|
hum->attack_dir.Rotate(a8::RandAngle());
|
|
hum->move_dir = hum->attack_dir;
|
|
hum->room = this;
|
|
hum->Initialize();
|
|
AddToEntityHash(hum);
|
|
AddToHumanHash(hum);
|
|
MatchTeam(hum);
|
|
++alive_count_;
|
|
alive_count_chged_frameno_ = GetFrameNo();
|
|
++PerfMonitor::Instance()->alive_count;
|
|
refreshed_robot_set_.insert(robot_meta->i->id());
|
|
if (GetRoomMode() == kZombieMode) {
|
|
hum->ChangeToRace(kHumanRace, 1);
|
|
}
|
|
|
|
if (!CanAddToScene(hum)) {
|
|
a8::SetBitFlag(hum->status, HS_Disable);
|
|
} else {
|
|
AddToAliveHumanHash(hum);
|
|
AddToMoveableHash(hum);
|
|
grid_service->AddHuman(hum);
|
|
hum->FindLocation();
|
|
hum->RefreshView();
|
|
}
|
|
}
|
|
NotifyUiUpdate();
|
|
}
|
|
|
|
Human* Room::FindEnemy(Human* hum)
|
|
{
|
|
if (a8::HasBitFlag(hum->status, HS_NewBieGuideAndroid) &&
|
|
GetFrameNo() - hum->enable_frameno < SERVER_FRAME_RATE * 8) {
|
|
return first_newbie_;
|
|
}
|
|
std::vector<Human*> enemys;
|
|
enemys.reserve(50);
|
|
EntitySubType_e sub_type = EST_Player;
|
|
if ((rand() % 10) < 2) {
|
|
sub_type = EST_Android;
|
|
}
|
|
if (room_type_ == RT_MidBrid) {
|
|
sub_type = EST_Android;
|
|
Human* target = nullptr;
|
|
hum->room->TouchHumanList
|
|
(
|
|
a8::XParams(),
|
|
[hum, &target] (Human* huma, a8::XParams& param)
|
|
{
|
|
if (!huma->dead &&
|
|
huma->IsAndroid() &&
|
|
hum->team_id != huma->team_id &&
|
|
!a8::HasBitFlag(huma->status, HS_Disable)) {
|
|
if (!target) {
|
|
target = huma;
|
|
} else {
|
|
if (hum->GetPos().ManhattanDistance(huma->GetPos()) <
|
|
hum->GetPos().ManhattanDistance(target->GetPos())) {
|
|
target = huma;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
});
|
|
if (target) {
|
|
if (hum->GetPos().ManhattanDistance(target->GetPos()) > 190) {
|
|
return nullptr;
|
|
}
|
|
}
|
|
return target;
|
|
}
|
|
hum->TouchAllLayerHumanList
|
|
(
|
|
[&enemys, hum, sub_type] (Human* target, bool& stop)
|
|
{
|
|
if (target->IsEntitySubType(sub_type) &&
|
|
!target->dead) {
|
|
if (hum->GetPos().Distance(target->GetPos()) < 300.0f) {
|
|
if (target->team_id == 0 ||
|
|
target->team_id != hum->team_id
|
|
) {
|
|
enemys.push_back(target);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
std::sort(enemys.begin(), enemys.end(),
|
|
[hum] (Human* a, Human *b) -> bool
|
|
{
|
|
return (hum->GetPos()-a->GetPos()).Norm() < (hum->GetPos()-b->GetPos()).Norm();
|
|
});
|
|
return !enemys.empty() ? enemys[0] : nullptr;
|
|
}
|
|
|
|
void Room::FillSMJoinedNotify(Player* self_hum, cs::SMJoinedNotify& msg)
|
|
{
|
|
msg.set_team_mode(msg.team_mode());
|
|
msg.set_player_id(self_hum->GetEntityUniId());
|
|
msg.set_started(false);
|
|
msg.set_room_uuid(a8::XValue(room_uuid_).GetString());
|
|
}
|
|
|
|
void Room::ScatterDrop(a8::Vec2 center, int drop_id)
|
|
{
|
|
MetaData::Drop* drop_meta = MetaMgr::Instance()->GetDrop(drop_id);
|
|
if (drop_meta) {
|
|
std::vector<std::tuple<int, int, int>> drop_items;
|
|
drop_meta->RandItems(drop_items);
|
|
for (auto& item : drop_items) {
|
|
a8::Vec2 dir = a8::Vec2::UP;
|
|
dir.Rotate(a8::RandAngle());
|
|
DropItem(center + dir * (5 + rand() % 50),
|
|
std::get<0>(item), std::get<1>(item),
|
|
std::get<1>(item));
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::DropItem(a8::Vec2 pos, int item_id, int item_count, int item_lv)
|
|
{
|
|
MetaData::Equip* equip_meta = MetaMgr::Instance()->GetEquip(item_id);
|
|
if (equip_meta && equip_meta->i->group_num() > 0 && item_count > 0) {
|
|
int total_count = item_count;
|
|
while (total_count > 0) {
|
|
int drop_num = std::min(total_count, equip_meta->i->group_num());
|
|
if (drop_num < 1) {
|
|
drop_num = 1;
|
|
}
|
|
{
|
|
a8::Vec2 dir = a8::Vec2::UP;
|
|
dir.Rotate(a8::RandAngle());
|
|
pos = pos + dir * (25 + rand() % 50);
|
|
}
|
|
CreateLoot(item_id, pos, drop_num, item_lv);
|
|
total_count -= drop_num;
|
|
}
|
|
}
|
|
}
|
|
|
|
RoomObstacle* Room::CreateObstacle(int id, float x, float y)
|
|
{
|
|
RoomObstacle* entity = InternalCreateObstacle(id, x, y,
|
|
[] (Obstacle*)
|
|
{
|
|
});
|
|
assert(entity);
|
|
if (entity) {
|
|
entity->BroadcastFullState(this);
|
|
}
|
|
return entity;
|
|
}
|
|
|
|
int Room::CreateLoot(int equip_id, a8::Vec2 pos, int count, int equip_lv)
|
|
{
|
|
MetaData::Equip* equip_meta = MetaMgr::Instance()->GetEquip(equip_id);
|
|
if (equip_meta) {
|
|
Loot* entity = EntityFactory::Instance()->MakeLoot(AllocUniid());
|
|
entity->room = this;
|
|
entity->meta = equip_meta;
|
|
entity->SetPos(pos);
|
|
#if 1
|
|
{
|
|
if (entity->GetX() >= map_meta_->i->map_width()) {
|
|
entity->SetX(map_meta_->i->map_width() - 1);
|
|
}
|
|
if (entity->GetX() < 1.0f) {
|
|
entity->SetX(1.0f);
|
|
}
|
|
if (entity->GetY() >= map_meta_->i->map_height()) {
|
|
entity->SetY(map_meta_->i->map_height() - 1);
|
|
}
|
|
if (entity->GetY() < 1.0f) {
|
|
entity->SetY(1.0f);
|
|
}
|
|
}
|
|
#endif
|
|
entity->item_id = equip_id;
|
|
entity->count = count;
|
|
entity->item_level = equip_lv;
|
|
entity->Initialize();
|
|
AddToEntityHash(entity);
|
|
grid_service->AddRoomEntity(this, entity);
|
|
entity->BroadcastFullState(this);
|
|
return entity->GetEntityUniId();
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
void Room::CreateBullet(Human* hum,
|
|
MetaData::Equip* weapon_meta,
|
|
MetaData::EquipUpgrade* weapon_upgrade_meta,
|
|
MetaData::Equip* bullet_meta,
|
|
a8::Vec2 pos,
|
|
a8::Vec2 dir,
|
|
float fly_distance,
|
|
bool is_tank_skin)
|
|
{
|
|
if (grid_service->CanAdd(pos.x, pos.y)) {
|
|
Bullet* bullet = EntityFactory::Instance()->MakeBullet(AllocUniid());
|
|
bullet->player = hum;
|
|
bullet->room = this;
|
|
bullet->gun_meta = weapon_meta;
|
|
bullet->gun_upgrade_meta = weapon_upgrade_meta;
|
|
bullet->meta = bullet_meta;
|
|
bullet->SetPos(pos);
|
|
bullet->dir = dir;
|
|
bullet->born_pos = pos;
|
|
bullet->born_dir = dir;
|
|
bullet->fly_distance = fly_distance;
|
|
bullet->is_tank_skin = is_tank_skin;
|
|
bullet->Initialize();
|
|
AddObjectLater(bullet);
|
|
grid_service->AddBullet(bullet);
|
|
}
|
|
}
|
|
|
|
void Room::RemoveObjectLater(RoomEntity* entity)
|
|
{
|
|
auto remove_func =
|
|
[] (const a8::XParams& param)
|
|
{
|
|
RoomEntity* entity = (RoomEntity*)param.sender.GetUserData();
|
|
switch (entity->GetEntityType()) {
|
|
case ET_Bullet:
|
|
{
|
|
entity->room->RemoveFromMoveableHash((Bullet*)entity);
|
|
entity->room->grid_service->DelBullet((Bullet*)entity);
|
|
}
|
|
break;
|
|
case ET_Loot:
|
|
{
|
|
entity->BroadcastDeleteState(entity->room);
|
|
entity->room->grid_service->DelRoomEntity(entity->room, entity);
|
|
}
|
|
break;
|
|
case ET_Player:
|
|
{
|
|
entity->room->RemoveFromMoveableHash((Human*)entity);
|
|
entity->room->RemoveFromHuamnHash((Human*)entity);
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
abort();
|
|
}
|
|
break;
|
|
}
|
|
entity->room->RemoveFromEntityHash(entity);
|
|
#ifdef DEBUG
|
|
#if 0
|
|
a8::UdpLog::Instance()->Debug
|
|
(
|
|
"remove object pointer:%d uniid:%d",
|
|
{
|
|
(long long)entity,
|
|
entity->GetEntityUniId()
|
|
}
|
|
);
|
|
#endif
|
|
#endif
|
|
delete entity;
|
|
};
|
|
xtimer.AddDeadLineTimerAndAttach(0,
|
|
a8::XParams()
|
|
.SetSender(entity),
|
|
remove_func,
|
|
&entity->xtimer_attacher.timer_list_);
|
|
}
|
|
|
|
void Room::OnHumanDie(Human* hum)
|
|
{
|
|
if (GetRoomMode() == kZombieMode) {
|
|
if (debug_trace) {
|
|
a8::UdpLog::Instance()->Debug
|
|
(
|
|
"OnHumanDie uniid:%d name:%s race:%d level:%d exp:%d killer_id:%d ",
|
|
{
|
|
hum->GetEntityUniId(),
|
|
hum->name,
|
|
hum->GetRace(),
|
|
hum->GetLevel(),
|
|
hum->GetExp(),
|
|
hum->stats.killer_id
|
|
}
|
|
);
|
|
}
|
|
if (hum->GetRace() == kHumanRace) {
|
|
RemoveRescue(hum);
|
|
hum->DeadDrop();
|
|
std::vector<Human*> alive_humans;
|
|
alive_humans.reserve(GetRoomMaxPlayerNum());
|
|
for (auto& pair : human_hash_) {
|
|
if (pair.second != hum) {
|
|
pair.second->WinExp(hum, hum->meta->i->dead_exp());
|
|
}
|
|
if (!pair.second->dead && pair.second->GetRace() == kHumanRace) {
|
|
alive_humans.push_back(pair.second);
|
|
}
|
|
}
|
|
if (alive_humans.size() == 1) {
|
|
Human* terminator = alive_humans[0];
|
|
if (!terminator->HasBuffEffect(kBET_Terminator)) {
|
|
MetaData::Buff* buff_meta = MetaMgr::Instance()->GetBuff(TERMINATOR_BUFF_ID);
|
|
if (buff_meta) {
|
|
terminator->AddBuff(terminator, buff_meta, 1);
|
|
}
|
|
}
|
|
}
|
|
{
|
|
Human* killer = GetPlayerByUniId(hum->stats.killer_id);
|
|
if (killer && killer != hum) {
|
|
killer->WinExp(hum, hum->meta->i->killer_exp());
|
|
}
|
|
}
|
|
} else if (hum->GetRace() == kZombieRace) {
|
|
} else {
|
|
abort();
|
|
}
|
|
NotifyUiUpdate();
|
|
} else {
|
|
--alive_count_;
|
|
alive_count_chged_frameno_ = GetFrameNo();
|
|
--PerfMonitor::Instance()->alive_count;
|
|
RemoveFromAliveHumanHash(hum);
|
|
NotifyUiUpdate();
|
|
CheckShowHand();
|
|
}
|
|
}
|
|
|
|
void Room::OnHumanRevive(Human* hum)
|
|
{
|
|
NotifyUiUpdate();
|
|
}
|
|
|
|
bool Room::OverBorder(const a8::Vec2 pos, float radius)
|
|
{
|
|
//检查x轴
|
|
{
|
|
int left_x = pos.x - radius;
|
|
if (left_x < 0.001f) {
|
|
return true;
|
|
}
|
|
int right_x = pos.x + radius;
|
|
if (right_x > map_meta_->i->map_width()) {
|
|
return true;
|
|
}
|
|
}
|
|
//检查y轴
|
|
{
|
|
int up_y = pos.y + radius;
|
|
if (up_y > map_meta_->i->map_height()) {
|
|
return true;
|
|
}
|
|
int down_y = pos.y - radius;
|
|
if (down_y < 0.001f) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void Room::AdjustPosInnerMap(a8::Vec2& pos, float radius)
|
|
{
|
|
//检查x轴
|
|
{
|
|
int left_x = pos.x - radius;
|
|
if (left_x < 0.001f) {
|
|
pos.x = radius + 10;
|
|
}
|
|
int right_x = pos.x + radius;
|
|
if (right_x > map_meta_->i->map_width()) {
|
|
pos.x = map_meta_->i->map_width() - radius - 10;
|
|
}
|
|
}
|
|
//检查y轴
|
|
{
|
|
int up_y = pos.y + radius;
|
|
if (up_y > map_meta_->i->map_height()) {
|
|
pos.y = map_meta_->i->map_height() - radius - 10;
|
|
}
|
|
int down_y = pos.y - radius;
|
|
if (down_y < 0.001f) {
|
|
pos.y = radius + 10;
|
|
}
|
|
}
|
|
if (OverBorder(pos, radius)) {
|
|
pos = a8::Vec2(1800, 1800);
|
|
#ifdef DEBUG
|
|
abort();
|
|
#endif
|
|
}
|
|
}
|
|
|
|
Human* Room::GetWatchWarTarget(Human* hum)
|
|
{
|
|
if (hum->team_members) {
|
|
for (auto& member : *hum->team_members) {
|
|
if (member != hum && !member->dead) {
|
|
return member;
|
|
}
|
|
}
|
|
}
|
|
std::vector<Human*> humans;
|
|
for (auto& pair : human_hash_) {
|
|
if (pair.first != hum->GetEntityUniId() && !pair.second->dead) {
|
|
humans.push_back(pair.second);
|
|
}
|
|
}
|
|
if (!humans.empty()) {
|
|
Human* target = humans[rand() % humans.size()];
|
|
return target;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
bool Room::BattleStarted()
|
|
{
|
|
return battle_start_frameno_ != 0;
|
|
}
|
|
|
|
int Room::GetAliveTeamNum()
|
|
{
|
|
int num = 0;
|
|
for (auto& pair : team_hash_) {
|
|
for (Human* hum : pair.second) {
|
|
if (!hum->real_dead) {
|
|
++num;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return num;
|
|
}
|
|
|
|
bool Room::CanJoin(const std::string& accountid,
|
|
RoomType_e self_room_type,
|
|
RoomMode_e self_room_mode)
|
|
{
|
|
if (self_room_mode < kChiJiMode) {
|
|
self_room_mode = kChiJiMode;
|
|
}
|
|
if (self_room_mode > kRoomModeEnd) {
|
|
self_room_mode = kZombieMode;
|
|
}
|
|
if (room_mode_ != self_room_mode) {
|
|
return false;
|
|
}
|
|
if (gas_data_.gas_mode != GasInactive) {
|
|
return false;
|
|
}
|
|
if (GetPlayerByAccountId(accountid)) {
|
|
return false;
|
|
}
|
|
if (room_type_ == RT_NewBrid) {
|
|
if (room_type_ != self_room_type || GetPlayerNum() > 0) {
|
|
return false;
|
|
}
|
|
}
|
|
if (room_type_ == RT_MidBrid) {
|
|
if (room_type_ != self_room_type || GetPlayerNum() >= 4) {
|
|
return false;
|
|
}
|
|
}
|
|
return GetPlayerNum() < (int)GetRoomMaxPlayerNum();
|
|
}
|
|
|
|
void Room::OnPlayerOffline(Player* hum)
|
|
{
|
|
if (GetOnlinePlayerNum() <= 0) {
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(
|
|
SERVER_FRAME_RATE * 15,
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
if (room->GetOnlinePlayerNum() <= 0 && !room->added_to_over_room) {
|
|
GameLog::Instance()->ForceOver(room);
|
|
RoomMgr::Instance()->AddOverRoom(room->room_uuid_);
|
|
}
|
|
},
|
|
&timer_attacher.timer_list_);
|
|
}
|
|
}
|
|
|
|
Entity* Room::FindFirstCollisonEntity(const a8::Vec2& aabb_pos, AabbCollider& aabb_box)
|
|
{
|
|
Entity* target = nullptr;
|
|
std::set<GridCell*> tmp_grids;
|
|
grid_service->GetAllCellsByXy(this, aabb_pos.x, aabb_pos.y, tmp_grids);
|
|
grid_service->TouchAllLayerEntityList
|
|
(
|
|
room_idx_,
|
|
tmp_grids,
|
|
[this, &target, &aabb_pos, &aabb_box] (Entity* entity, bool& stop)
|
|
{
|
|
switch (entity->GetEntityType()) {
|
|
case ET_Obstacle:
|
|
{
|
|
if (!target) {
|
|
if (entity->TestCollisionEx(this, aabb_pos, aabb_box)) {
|
|
target = entity;
|
|
stop = true;
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
case ET_Building:
|
|
{
|
|
if (!target || !target->IsEntityType(ET_Building)) {
|
|
AabbCollider building_aabb_box;
|
|
entity->GetAabbBox(building_aabb_box);
|
|
if (building_aabb_box.IntersectEx(aabb_pos, &aabb_box)) {
|
|
target = entity;
|
|
stop = true;
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
});
|
|
return target;
|
|
}
|
|
|
|
void Room::FindLocationWithAabb(Entity* target, const a8::Vec2& aabb_pos, AabbCollider* aabb_box,
|
|
float& new_x, float& new_y)
|
|
{
|
|
a8::Vec2 old_pos = aabb_pos;
|
|
a8::Vec2 new_pos = aabb_pos;
|
|
AabbCollider target_collider;
|
|
target->GetAabbBox(target_collider);
|
|
{
|
|
bool ret = aabb_box->CalcSafePointEx(aabb_pos, &target_collider, new_pos);
|
|
if (!ret) {
|
|
abort();
|
|
}
|
|
}
|
|
a8::Vec2 new_pos_dir = new_pos - old_pos;
|
|
new_pos_dir.Normalize();
|
|
float distance = (new_pos - old_pos).Norm();
|
|
for (int i = distance; i < 100000; i += 5) {
|
|
a8::Vec2 pos = old_pos + new_pos_dir * i;
|
|
new_x = pos.x;
|
|
new_y = pos.y;
|
|
|
|
bool is_collision = false;
|
|
std::set<ColliderComponent*> colliders;
|
|
map_service->GetColliders(this, pos.x, pos.y, colliders);
|
|
for (ColliderComponent* collider : colliders) {
|
|
if (collider->IntersectEx(pos, aabb_box)) {
|
|
is_collision = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!is_collision) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::FillSMUiUpdate(cs::SMUiUpdate& msg)
|
|
{
|
|
for (auto& pair : car_hash_) {
|
|
if (!pair.second.taken) {
|
|
cs::MFCar* car = msg.add_car_list();
|
|
car->set_car_id(pair.second.car_id);
|
|
TypeConvert::ToPb(pair.second.pos, car->mutable_pos());
|
|
}
|
|
}
|
|
if (GetRoomMode() == kZombieMode) {
|
|
#if 1
|
|
{
|
|
zombie_alive_count_ = 0;
|
|
human_alive_count_ = 0;
|
|
for (auto& pair : human_hash_) {
|
|
if (pair.second->GetRace() == kHumanRace) {
|
|
++human_alive_count_;
|
|
} else if (pair.second->GetRace() == kZombieRace) {
|
|
++zombie_alive_count_;
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
msg.set_zombie_num(zombie_alive_count_);
|
|
msg.set_human_num(human_alive_count_);
|
|
if (BattleStarted() && human_alive_count_ <= 2 && !sent_terminator_airdrop) {
|
|
sent_terminator_airdrop = true;
|
|
MetaData::AirDrop* airdrop_meta = MetaMgr::Instance()->GetAirDrop
|
|
(map_meta_->i->terminator_airdrop());
|
|
if (airdrop_meta) {
|
|
AirDrop(airdrop_meta->i->appear_time(),
|
|
airdrop_meta->i->drop_id(),
|
|
airdrop_meta->i->id());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
std::set<Human*>* Room::GetAliveTeam()
|
|
{
|
|
for (auto& pair : team_hash_) {
|
|
for (Human* hum : pair.second) {
|
|
if (!hum->real_dead) {
|
|
return &pair.second;
|
|
}
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
int Room::NewTeam()
|
|
{
|
|
++current_teamid_;
|
|
team_hash_[current_teamid_] = std::set<Human*>();
|
|
return current_teamid_;
|
|
}
|
|
|
|
void Room::TouchPlayerList(a8::XParams param,
|
|
std::function<void (Player*, a8::XParams&)> func)
|
|
{
|
|
if (!func) {
|
|
return;
|
|
}
|
|
for (auto& pair : accountid_hash_) {
|
|
if (pair.second) {
|
|
func(pair.second, param);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::TouchHumanList(a8::XParams param,
|
|
std::function<bool (Human*, a8::XParams&)> func)
|
|
{
|
|
if (!func) {
|
|
return;
|
|
}
|
|
for (auto& pair : human_hash_) {
|
|
if (pair.second) {
|
|
if (!func(pair.second, param)) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::TouchEntityList(a8::XParams param,
|
|
std::function<bool (Entity*, a8::XParams&)> func)
|
|
{
|
|
if (!func) {
|
|
return;
|
|
}
|
|
for (auto& pair : uniid_hash_) {
|
|
if (pair.second) {
|
|
if (!func(pair.second, param)) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::UpdateGas()
|
|
{
|
|
switch (gas_data_.gas_mode) {
|
|
case GasInactive:
|
|
UpdateGasInactive();
|
|
break;
|
|
case GasWaiting:
|
|
UpdateGasWaiting();
|
|
break;
|
|
case GasMoving:
|
|
UpdateGasMoving();
|
|
break;
|
|
}
|
|
#if 1
|
|
if (GetRoomMode() == kZombieMode) {
|
|
int zombie_num = 0;
|
|
for (auto& pair : human_hash_) {
|
|
if (pair.second->GetRace() == kZombieRace) {
|
|
++zombie_num;
|
|
}
|
|
}
|
|
if (zombie_num >= GetRoomMaxPlayerNum() && !game_over_) {
|
|
game_over_ = true;
|
|
game_over_frameno_ = GetFrameNo();
|
|
OnGameOver();
|
|
}
|
|
}
|
|
#endif
|
|
if (gas_data_.gas_mode != GasInactive && GetRoomMode() == kChiJiMode) {
|
|
if (!IsGameOver() && alive_count_ <= MAX_TEAM_NUM && GetAliveTeamNum() <= 1) {
|
|
game_over_ = true;
|
|
game_over_frameno_ = GetFrameNo();
|
|
OnGameOver();
|
|
}
|
|
for (auto& pair : human_hash_) {
|
|
if (pair.second->real_dead ||
|
|
a8::HasBitFlag(pair.second->status, HS_Disable)) {
|
|
continue;
|
|
}
|
|
bool b1 = a8::CircleContainCircle(gas_data_.pos_old,
|
|
gas_data_.gas_progress,
|
|
pair.second->GetPos(),
|
|
pair.second->GetRadius()
|
|
);
|
|
bool b2 = a8::CircleContainCircle(gas_data_.pos_new,
|
|
gas_data_.rad_new,
|
|
pair.second->GetPos(),
|
|
pair.second->GetRadius()
|
|
);
|
|
if (!b1 && !b2) {
|
|
pair.second->poisoning = true;
|
|
} else {
|
|
pair.second->poisoning = false;
|
|
pair.second->poisoning_time = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::UpdateGasInactive()
|
|
{
|
|
if (GetFrameNo() - gas_data_.gas_start_frameno >= GetGasInactiveTime() * SERVER_FRAME_RATE) {
|
|
gas_data_.gas_mode = GasWaiting;
|
|
if (GetRoomMode() == kZombieMode) {
|
|
gas_data_.old_area_meta = MetaMgr::Instance()->GetSafeArea(70001);
|
|
gas_data_.new_area_meta = MetaMgr::Instance()->GetSafeArea(70002);
|
|
} else if (IsMiniRoom()) {
|
|
gas_data_.old_area_meta = MetaMgr::Instance()->GetSafeArea(60001);
|
|
gas_data_.new_area_meta = MetaMgr::Instance()->GetSafeArea(60002);
|
|
} else {
|
|
gas_data_.old_area_meta = MetaMgr::Instance()->GetSafeArea(30001);
|
|
gas_data_.new_area_meta = MetaMgr::Instance()->GetSafeArea(30002);
|
|
}
|
|
gas_data_.gas_progress = gas_data_.old_area_meta->i->rad();
|
|
gas_data_.gas_start_frameno = GetFrameNo();
|
|
gas_data_.pos_old = a8::Vec2(map_meta_->i->map_width() / 2.0f,
|
|
map_meta_->i->map_height() / 2.0f);
|
|
gas_data_.pos_old_bk = gas_data_.pos_old;
|
|
{
|
|
bool gen_ok = GenSmallCircle(gas_data_.pos_old,
|
|
gas_data_.old_area_meta->i->rad(),
|
|
gas_data_.new_area_meta->i->rad(),
|
|
gas_data_.pos_new);
|
|
assert(gen_ok);
|
|
}
|
|
gas_data_.rad_old = gas_data_.old_area_meta->i->rad();
|
|
gas_data_.rad_new = gas_data_.new_area_meta->i->rad();
|
|
battle_start_frameno_ = GetFrameNo();
|
|
if (human_hash_.size() < GetRoomMaxPlayerNum()) {
|
|
CreateAndroid(GetRoomMaxPlayerNum() - human_hash_.size());
|
|
}
|
|
CombineTeam();
|
|
NotifyGameStart();
|
|
NotifyUiUpdate();
|
|
NotifyWxVoip();
|
|
if (room_type_ != RT_NewBrid) {
|
|
InitAirDrop();
|
|
}
|
|
RoomMgr::Instance()->ActiveRoom(room_uuid_);
|
|
}
|
|
}
|
|
|
|
void Room::UpdateGasWaiting()
|
|
{
|
|
if (GetFrameNo() - gas_data_.gas_start_frameno >=
|
|
gas_data_.old_area_meta->i->wait_time() * SERVER_FRAME_RATE) {
|
|
gas_data_.gas_mode = GasMoving;
|
|
gas_data_.gas_start_frameno = GetFrameNo();;
|
|
}
|
|
}
|
|
|
|
void Room::UpdateGasMoving()
|
|
{
|
|
if (GetFrameNo() - gas_data_.gas_start_frameno > 0 && !gas_data_.is_last_gas) {
|
|
float distance = gas_data_.old_area_meta->i->shrink_speed() *
|
|
((GetFrameNo() - gas_data_.gas_start_frameno) * (1.0 / SERVER_FRAME_RATE));
|
|
gas_data_.gas_progress = std::max(gas_data_.rad_old - distance, gas_data_.rad_new);
|
|
if (!(gas_data_.pos_new == gas_data_.pos_old)) {
|
|
a8::Vec2 p1 = gas_data_.pos_new - gas_data_.pos_old_bk;
|
|
gas_data_.pre_pos_old = gas_data_.pos_old;
|
|
if (p1.Norm() - distance <= 0.01f) {
|
|
gas_data_.pos_old = gas_data_.pos_new;
|
|
} else {
|
|
a8::Vec2 dir = p1;
|
|
dir.Normalize();
|
|
gas_data_.pos_old = gas_data_.pos_old_bk + dir * distance;
|
|
}
|
|
if (gas_data_.rad_old - distance <= gas_data_.rad_new) {
|
|
assert(gas_data_.pos_new == gas_data_.pos_old);
|
|
}
|
|
}
|
|
if (std::abs(gas_data_.gas_progress - gas_data_.rad_new) <= 0.001f) {
|
|
int pre_area_id = gas_data_.new_area_meta->i->id();
|
|
a8::Vec2 pre_pos = gas_data_.pos_new;
|
|
if (!MetaMgr::Instance()->GetSafeArea(pre_area_id + 1)) {
|
|
gas_data_.is_last_gas = true;
|
|
return;
|
|
}
|
|
gas_data_.gas_mode = GasWaiting;
|
|
gas_data_.old_area_meta = MetaMgr::Instance()->GetSafeArea(pre_area_id);
|
|
gas_data_.new_area_meta = MetaMgr::Instance()->GetSafeArea(pre_area_id + 1);
|
|
gas_data_.gas_progress = gas_data_.old_area_meta->i->rad();
|
|
gas_data_.gas_start_frameno = GetFrameNo();
|
|
gas_data_.pos_old = pre_pos;
|
|
gas_data_.pos_old_bk = gas_data_.pos_old;
|
|
{
|
|
bool gen_ok = GenSmallCircle(gas_data_.pos_old,
|
|
gas_data_.old_area_meta->i->rad(),
|
|
gas_data_.new_area_meta->i->rad(),
|
|
gas_data_.pos_new);
|
|
assert(gen_ok);
|
|
}
|
|
gas_data_.rad_old = gas_data_.old_area_meta->i->rad();
|
|
gas_data_.rad_new = gas_data_.new_area_meta->i->rad();
|
|
if (!MetaMgr::Instance()->GetSafeArea(gas_data_.new_area_meta->i->id() + 2)) {
|
|
#if 1
|
|
//最后一圈
|
|
if (room_type_ == RT_MidBrid) {
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(2,
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
room->ShuaLastGas();
|
|
},
|
|
&xtimer_attacher_.timer_list_);
|
|
}
|
|
#endif
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
bool Room::GenSmallCircle(a8::Vec2 big_circle_pos, float big_circle_rad, float small_circle_rad,
|
|
a8::Vec2& out_pos)
|
|
{
|
|
if (big_circle_rad <= small_circle_rad) {
|
|
abort();
|
|
}
|
|
a8::Vec2 dir = a8::Vec2::UP;
|
|
dir.Rotate(a8::RandAngle());
|
|
float rad = rand() % (int)(big_circle_rad - small_circle_rad);
|
|
if (rad <= 0.001f){
|
|
rad = 0.001f;
|
|
}
|
|
out_pos = big_circle_pos + dir * rad;
|
|
return true;
|
|
}
|
|
|
|
void Room::MatchTeam(Human* hum)
|
|
{
|
|
if (!hum->team_uuid.empty() && GetRoomMode() != kZombieMode) {
|
|
for (auto& pair : human_hash_) {
|
|
if (pair.second != hum) {
|
|
if (!hum->team_uuid.empty() && pair.second->team_uuid == hum->team_uuid) {
|
|
if (!pair.second->team_members) {
|
|
pair.second->team_id = NewTeam();
|
|
pair.second->team_members = &team_hash_[pair.second->team_id];
|
|
pair.second->team_members->insert(pair.second);
|
|
}
|
|
if (pair.second->team_members->size() < MAX_TEAM_NUM) {
|
|
pair.second->team_members->insert(hum);
|
|
hum->team_id = pair.second->team_id;
|
|
hum->team_members = pair.second->team_members;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (hum->team_id == 0) {
|
|
hum->team_id = NewTeam();
|
|
hum->team_members = &team_hash_[hum->team_id];
|
|
hum->team_members->insert(hum);
|
|
}
|
|
}
|
|
|
|
void Room::CombineTeam()
|
|
{
|
|
if (GetRoomMode() == kZombieMode) {
|
|
return;
|
|
}
|
|
std::map<int, size_t> need_combine_teams;
|
|
std::map<int, size_t> need_combine_teams_copy;
|
|
|
|
if (room_type_ == RT_MidBrid) {
|
|
for (auto& pair : accountid_hash_) {
|
|
Human* hum = pair.second;
|
|
hum->auto_fill = true;
|
|
hum->team_uuid = a8::XValue(room_uuid_).GetString();
|
|
}
|
|
for (size_t i = 0; i < MAX_TEAM_NUM - accountid_hash_.size(); ++i) {
|
|
for (auto& pair : human_hash_) {
|
|
Human* hum = pair.second;
|
|
if (hum->IsAndroid() &&
|
|
!hum->auto_fill) {
|
|
hum->auto_fill = true;
|
|
hum->team_uuid = a8::XValue(room_uuid_).GetString();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int first_team_id = 0;
|
|
int total_count = 0;
|
|
for (auto& pair : team_hash_) {
|
|
for (Human* hum : pair.second) {
|
|
if (hum->auto_fill && !hum->team_uuid.empty()) {
|
|
if (first_team_id == 0) {
|
|
first_team_id = pair.first;
|
|
}
|
|
need_combine_teams[pair.first] = pair.second.size();
|
|
total_count += pair.second.size();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (total_count <= 1) {
|
|
return;
|
|
}
|
|
|
|
size_t first_team_num = 4;
|
|
switch (total_count) {
|
|
case 3:
|
|
{
|
|
first_team_num = 2;
|
|
break;
|
|
}
|
|
case 4:
|
|
{
|
|
first_team_num = 2;
|
|
break;
|
|
}
|
|
case 5:
|
|
{
|
|
first_team_num = 3;
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
first_team_num = 4;
|
|
break;
|
|
}
|
|
}
|
|
if (room_type_ == RT_MidBrid) {
|
|
first_team_num = 4;
|
|
}
|
|
|
|
need_combine_teams_copy = need_combine_teams;
|
|
int try_count = 0;
|
|
do {
|
|
for (auto& pair1 : need_combine_teams) {
|
|
bool combine_ok = false;
|
|
for (auto& pair2 : need_combine_teams_copy) {
|
|
int team_id1 = pair1.first;
|
|
int team_id2 = pair2.first;
|
|
if (team_id2 == first_team_id) {
|
|
continue;
|
|
}
|
|
if (team_id1 == first_team_id) {
|
|
if (pair1.second + pair2.second > first_team_num) {
|
|
continue;
|
|
}
|
|
}
|
|
if (team_id1 != team_id2) {
|
|
if (pair1.second + pair2.second <= MAX_TEAM_NUM) {
|
|
int new_team_num = pair1.second + pair2.second;
|
|
{
|
|
std::set<Human*>& team1_members = team_hash_[team_id1];
|
|
std::set<Human*>& team2_members = team_hash_[team_id2];
|
|
if (team1_members.size() + team2_members.size() > MAX_TEAM_NUM) {
|
|
a8::UdpLog::Instance()->Warning("team_member > 4 :%d",
|
|
{
|
|
team1_members.size() + team2_members.size()
|
|
});
|
|
}
|
|
if (pair1.first == first_team_id || pair1.second >= pair2.second) {
|
|
for (Human* hum : team2_members) {
|
|
hum->team_id = team_id1;
|
|
hum->team_members = &team1_members;
|
|
team1_members.insert(hum);
|
|
}
|
|
team_hash_.erase(pair2.first);
|
|
} else {
|
|
for (Human* hum : team1_members) {
|
|
hum->team_id = team_id2;
|
|
hum->team_members = &team2_members;
|
|
team2_members.insert(hum);
|
|
}
|
|
team_hash_.erase(pair1.first);
|
|
}
|
|
}
|
|
|
|
if (first_team_id == team_id1) {
|
|
if (pair1.second + pair2.second >= first_team_num) {
|
|
need_combine_teams.erase(team_id1);
|
|
need_combine_teams_copy.erase(team_id1);
|
|
} else {
|
|
pair1.second = new_team_num;
|
|
if (need_combine_teams_copy.find(pair1.first) !=
|
|
need_combine_teams_copy.end()) {
|
|
need_combine_teams_copy[pair1.first] = new_team_num;
|
|
}
|
|
}
|
|
need_combine_teams.erase(team_id2);
|
|
need_combine_teams_copy.erase(team_id2);
|
|
} else {
|
|
if (pair1.second >= pair2.second) {
|
|
if (pair1.second + pair2.second >= MAX_TEAM_NUM) {
|
|
need_combine_teams.erase(team_id1);
|
|
need_combine_teams_copy.erase(team_id1);
|
|
} else {
|
|
pair1.second = new_team_num;
|
|
if (need_combine_teams_copy.find(pair1.first) !=
|
|
need_combine_teams_copy.end()) {
|
|
need_combine_teams_copy[pair1.first] = new_team_num;
|
|
}
|
|
}
|
|
need_combine_teams.erase(team_id2);
|
|
need_combine_teams_copy.erase(team_id2);
|
|
} else {
|
|
if (pair1.second + pair2.second >= MAX_TEAM_NUM) {
|
|
need_combine_teams.erase(team_id2);
|
|
need_combine_teams_copy.erase(team_id2);
|
|
} else {
|
|
pair2.second = new_team_num;
|
|
if (need_combine_teams.find(pair2.first) !=
|
|
need_combine_teams.end()) {
|
|
need_combine_teams[pair2.first] = new_team_num;
|
|
}
|
|
}
|
|
need_combine_teams.erase(team_id1);
|
|
need_combine_teams_copy.erase(team_id1);
|
|
}
|
|
}
|
|
combine_ok = true;
|
|
break;
|
|
}
|
|
}
|
|
}//end for
|
|
if (combine_ok) {
|
|
break;
|
|
}
|
|
}
|
|
} while (++try_count < 20);
|
|
}
|
|
|
|
void Room::InitAirDrop()
|
|
{
|
|
if (GetRoomMode() == kZombieMode) {
|
|
for (int airdrop_id : map_meta_->airdrops) {
|
|
MetaData::AirDrop* airdrop_meta = MetaMgr::Instance()->GetAirDrop(airdrop_id);
|
|
if (airdrop_meta) {
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(SERVER_FRAME_RATE * airdrop_meta->i->time(),
|
|
a8::XParams()
|
|
.SetSender(this)
|
|
.SetParam1(airdrop_meta->i->appear_time())
|
|
.SetParam2(airdrop_meta->i->drop_id())
|
|
.SetParam3(airdrop_meta->i->id()),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
if (!room->IsGameOver()) {
|
|
room->AirDrop(param.param1, param.param2, param.param3);
|
|
}
|
|
},
|
|
&xtimer_attacher_.timer_list_);
|
|
}
|
|
}
|
|
} else {
|
|
std::list<MetaData::AirDrop>& air_drops = MetaMgr::Instance()->GetAirDrops();
|
|
for (auto& air_drop : air_drops) {
|
|
if (air_drop.i->id() >= 1 && air_drop.i->id() <= 6) {
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(SERVER_FRAME_RATE * air_drop.i->time(),
|
|
a8::XParams()
|
|
.SetSender(this)
|
|
.SetParam1(air_drop.i->appear_time())
|
|
.SetParam2(air_drop.i->drop_id())
|
|
.SetParam3(air_drop.i->id()),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
if (!room->IsGameOver()) {
|
|
room->AirDrop(param.param1, param.param2, param.param3);
|
|
}
|
|
},
|
|
&xtimer_attacher_.timer_list_);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::AirDrop(int appear_time, int box_id, int airdrop_id)
|
|
{
|
|
MetaData::MapThing* thing_meta = MetaMgr::Instance()->GetMapThing(box_id);
|
|
if (room_type_ == RT_NewBrid &&
|
|
airdrop_times_ < MetaMgr::Instance()->level0room_spec_airdrops.size()) {
|
|
int spec_box_id = MetaMgr::Instance()->level0room_spec_airdrops[airdrop_times_];
|
|
if (MetaMgr::Instance()->GetMapThing(spec_box_id)) {
|
|
thing_meta = MetaMgr::Instance()->GetMapThing(spec_box_id);
|
|
box_id = spec_box_id;
|
|
}
|
|
}
|
|
if (GetRoomMode() == kZombieMode) {
|
|
if (airdrop_id != map_meta_->i->terminator_airdrop()) {
|
|
NotifyCountdown("距离物资箱抵达还有%d秒", appear_time / 1000);
|
|
}
|
|
}
|
|
if (thing_meta && thing_meta->i->type() == 2) {
|
|
a8::Vec2 dir = a8::Vec2::UP;
|
|
dir.Rotate(a8::RandAngle());
|
|
a8::Vec2 box_pos = gas_data_.pos_new + dir * (500 + rand() % 300);
|
|
if (box_pos.x < 1.0f) {
|
|
box_pos.x = 1.0f;
|
|
}
|
|
if (box_pos.x >= map_meta_->i->map_width()) {
|
|
box_pos.x = map_meta_->i->map_width() - 1;
|
|
}
|
|
if (box_pos.y < 1.0f) {
|
|
box_pos.y = 1.0f;
|
|
}
|
|
if (box_pos.y >= map_meta_->i->map_height()) {
|
|
box_pos.y = map_meta_->i->map_height() - 1;
|
|
}
|
|
AdjustAirDropPos(thing_meta, box_pos);
|
|
AabbCollider air_drop_aabb_box;
|
|
{
|
|
air_drop_aabb_box._min.x = 0 - thing_meta->i->width()/2.0f;
|
|
air_drop_aabb_box._min.y = 0 - thing_meta->i->height()/2.0f;
|
|
air_drop_aabb_box._max.x = 0 + thing_meta->i->width()/2.0f;
|
|
air_drop_aabb_box._max.y = 0 + thing_meta->i->height()/2.0f;
|
|
}
|
|
Entity* target = FindFirstCollisonEntity(box_pos, air_drop_aabb_box);
|
|
if (target) {
|
|
float new_x = box_pos.x;
|
|
float new_y = box_pos.y;
|
|
FindLocationWithAabb(target, box_pos, &air_drop_aabb_box, new_x, new_y);
|
|
box_pos.x = new_x;
|
|
box_pos.y = new_y;
|
|
}
|
|
if (box_pos.x + thing_meta->i->width()/2 + 2 > map_meta_->i->map_width() ||
|
|
box_pos.x - thing_meta->i->width()/2 - 2 < 1 ||
|
|
box_pos.y + thing_meta->i->height()/2 + 2 > map_meta_->i->map_height() ||
|
|
box_pos.y - thing_meta->i->height()/2 - 2 < 1
|
|
) {
|
|
return;
|
|
}
|
|
frame_event.AddAirDrop(appear_time, box_id, box_pos);
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(SERVER_FRAME_RATE * appear_time / 1000.f,
|
|
a8::XParams()
|
|
.SetSender(this)
|
|
.SetParam1(a8::MakeInt64(airdrop_id, box_id))
|
|
.SetParam2(box_pos.x)
|
|
.SetParam3(box_pos.y),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
if (!room->IsGameOver()) {
|
|
int airdrop_id = a8::Low32(param.param1.GetInt64());
|
|
int box_id = a8::High32(param.param1.GetInt64());
|
|
RoomObstacle* obstacle = room->CreateObstacle
|
|
(
|
|
box_id,
|
|
param.param2.GetDouble(),
|
|
param.param3.GetDouble()
|
|
);
|
|
obstacle->is_treasure_box = true;
|
|
if (room->GetRoomMode() == kZombieMode) {
|
|
obstacle->is_terminator_airdrop_box =
|
|
airdrop_id == room->map_meta_->i->terminator_airdrop();
|
|
if (obstacle->is_terminator_airdrop_box) {
|
|
room->NotifySysPiao
|
|
(
|
|
"终结者补给箱已送达",
|
|
a8::MkRgb(0, 255, 0),
|
|
3
|
|
);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
&xtimer_attacher_.timer_list_);
|
|
++airdrop_times_;
|
|
}
|
|
}
|
|
|
|
void Room::AdjustAirDropPos(MetaData::MapThing* thing_meta, a8::Vec2& box_pos)
|
|
{
|
|
if (room_type_ == RT_NewBrid) {
|
|
if (airdrop_times_ < MetaMgr::Instance()->newbie_airdrop.size()) {
|
|
for (auto& pair : accountid_hash_) {
|
|
if (pair.second) {
|
|
Human* hum = pair.second;
|
|
a8::Vec2 dir = a8::Vec2::UP;
|
|
dir.Rotate(a8::RandAngle());
|
|
box_pos = hum->GetPos() + dir * (80 + rand() % 50);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
AdjustPosInnerMap(box_pos, 100);
|
|
}
|
|
|
|
RoomObstacle* Room::InternalCreateObstacle(int id, float x, float y,
|
|
std::function<void (Obstacle*)> on_precreate)
|
|
{
|
|
MetaData::MapThing* thing = MetaMgr::Instance()->GetMapThing(id);
|
|
if (thing) {
|
|
RoomObstacle* entity = EntityFactory::Instance()->MakeRoomObstacle(AllocUniid());
|
|
entity->meta = thing;
|
|
entity->room = this;
|
|
entity->SetPos(a8::Vec2(x, y));
|
|
entity->Initialize();
|
|
if (on_precreate) {
|
|
on_precreate(entity);
|
|
}
|
|
AddToEntityHash(entity);
|
|
grid_service->AddRoomEntity(this, entity);
|
|
return entity;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void Room::AddObjectLater(RoomEntity* entity)
|
|
{
|
|
auto add_func = [] (const a8::XParams& param)
|
|
{
|
|
RoomEntity* entity = (RoomEntity*)param.sender.GetUserData();
|
|
if (entity->IsEntityType(ET_Bullet)) {
|
|
MoveableEntity* moveableentity = (MoveableEntity*)entity;
|
|
entity->room->AddToMoveableHash(moveableentity);
|
|
}
|
|
entity->room->AddToEntityHash(entity);
|
|
entity->room->RemoveFromLaterAddHash(entity);
|
|
};
|
|
AddToLaterAddHash(entity);
|
|
xtimer.AddDeadLineTimerAndAttach(0,
|
|
a8::XParams()
|
|
.SetSender(entity),
|
|
add_func,
|
|
&entity->xtimer_attacher.timer_list_);
|
|
}
|
|
|
|
void Room::OnGameOver()
|
|
{
|
|
for (auto& pair : human_hash_) {
|
|
if (IsGameOver() && game_over_frameno_ == GetFrameNo()) {
|
|
pair.second->SendGameOver();
|
|
}
|
|
}
|
|
if (GetRoomMode() == kZombieMode) {
|
|
GameLog::Instance()->ZbModeEnd(this);
|
|
}
|
|
RoomMgr::Instance()->AddOverRoom(room_uuid_);
|
|
}
|
|
|
|
void Room::RandRemoveAndroid()
|
|
{
|
|
Human* hum = nullptr;
|
|
if (room_type_ == RT_MidBrid) {
|
|
for (auto& pair : alive_human_hash_) {
|
|
if (pair.second->IsAndroid()) {
|
|
hum = pair.second;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (!hum) {
|
|
for (auto& pair : human_hash_) {
|
|
if (pair.second->IsAndroid()) {
|
|
hum = pair.second;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (hum) {
|
|
if (hum->team_id != 0) {
|
|
team_hash_.erase(hum->team_id);
|
|
}
|
|
if (hum->born_point) {
|
|
DecBornPointHumanNum(hum->born_point, hum);
|
|
}
|
|
grid_service->DeatchHuman(hum);
|
|
RemoveFromMoveableHash(hum);
|
|
RemoveFromEntityHash(hum);
|
|
RemoveFromHuamnHash(hum);
|
|
RemoveFromAliveHumanHash(hum);
|
|
AddToRemovedRobotHash(hum);
|
|
--alive_count_;
|
|
alive_count_chged_frameno_ = GetFrameNo();
|
|
--PerfMonitor::Instance()->alive_count;
|
|
for (auto& pair : human_hash_) {
|
|
pair.second->RemovePartObjects(hum);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::NotifyUiUpdate()
|
|
{
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(0,
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
room->TouchPlayerList(a8::XParams(),
|
|
[] (Player * hum, a8::XParams & param)
|
|
{
|
|
hum->SendUIUpdate();
|
|
});
|
|
},
|
|
&xtimer_attacher_.timer_list_);
|
|
}
|
|
|
|
void Room::UpdateCarObject(int old_uniid, int new_uniid, a8::Vec2 pos)
|
|
{
|
|
auto itr = car_hash_.find(old_uniid);
|
|
if (itr != car_hash_.end()) {
|
|
CarObject new_obj = itr->second;
|
|
new_obj.pos = pos;
|
|
car_hash_[new_uniid] = new_obj;
|
|
car_hash_.erase(old_uniid);
|
|
} else {
|
|
assert(false);
|
|
}
|
|
}
|
|
|
|
void Room::TakeOnCarObject(int car_uniid)
|
|
{
|
|
auto itr = car_hash_.find(car_uniid);
|
|
if (itr != car_hash_.end()) {
|
|
itr->second.taken = true;
|
|
}
|
|
}
|
|
|
|
void Room::TakeOffCarObject(int car_uniid, a8::Vec2 pos)
|
|
{
|
|
auto itr = car_hash_.find(car_uniid);
|
|
if (itr != car_hash_.end()) {
|
|
itr->second.pos = pos;
|
|
itr->second.taken = false;
|
|
}
|
|
}
|
|
|
|
int Room::CreateAndTakeonCar(int car_id, a8::Vec2 pos)
|
|
{
|
|
MetaData::Equip* item_meta = MetaMgr::Instance()->GetEquip(car_id);
|
|
if (!item_meta) {
|
|
return -1;
|
|
}
|
|
int car_uniid = AllocUniid();
|
|
CarObject car;
|
|
car.car_id = car_id;
|
|
car.pos = pos;
|
|
car.taken = true;
|
|
car_hash_[car_uniid] = car;
|
|
return car_uniid;
|
|
}
|
|
|
|
bool Room::HaveMyTeam(const std::string& team_uuid)
|
|
{
|
|
if (team_uuid.empty()) {
|
|
return false;
|
|
}
|
|
for (auto& pair : accountid_hash_) {
|
|
if (pair.second->team_uuid == team_uuid) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void Room::NotifyWxVoip()
|
|
{
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(0,
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
room->TouchPlayerList(a8::XParams(),
|
|
[] (Player * hum, a8::XParams & param)
|
|
{
|
|
hum->SendWxVoip();
|
|
});
|
|
},
|
|
&xtimer_attacher_.timer_list_);
|
|
}
|
|
|
|
void Room::BattleReport()
|
|
{
|
|
for (auto& pair : accountid_hash_) {
|
|
pair.second->SendGameOver();
|
|
}
|
|
game_over_ = true;
|
|
game_over_frameno_ = frameno_;
|
|
RoomMgr::Instance()->AddOverRoom(room_uuid_);
|
|
}
|
|
|
|
BornPoint* Room::AllocBornPoint(Human* hum)
|
|
{
|
|
BornPoint* born_point = nullptr;
|
|
if (hum->born_point) {
|
|
std::vector<BornPoint*> point_list;
|
|
std::vector<BornPoint*> free_point_list;
|
|
BornPoint* pre_point = nullptr;
|
|
BornPoint* reserve_point = nullptr;
|
|
for (auto& pair : born_point_hash_) {
|
|
if (&pair.second != hum->born_point) {
|
|
if (pair.second.player_num + pair.second.android_num <
|
|
pair.second.thing_tpl->i->param1()) {
|
|
point_list.push_back(&pair.second);
|
|
free_point_list.push_back(&pair.second);;
|
|
}
|
|
if (!reserve_point || rand() % 100 < 10) {
|
|
reserve_point = &pair.second;
|
|
}
|
|
} else {
|
|
pre_point = &pair.second;
|
|
}
|
|
}
|
|
if (!free_point_list.empty()) {
|
|
born_point = free_point_list[rand() % free_point_list.size()];
|
|
if (pre_point) {
|
|
DecBornPointHumanNum(pre_point, hum);
|
|
}
|
|
} else {
|
|
if (point_list.empty() && reserve_point) {
|
|
born_point = ForceTakeBornPoint(hum, reserve_point);
|
|
} else {
|
|
born_point = !point_list.empty() ? point_list[rand() % point_list.size()] : nullptr;
|
|
if (pre_point) {
|
|
DecBornPointHumanNum(pre_point, hum);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
for (auto& pair : born_point_hash_) {
|
|
if (pair.second.player_num + pair.second.android_num <
|
|
pair.second.thing_tpl->i->param1()) {
|
|
born_point = &pair.second;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (born_point) {
|
|
IncBornPointHumanNum(born_point, hum);
|
|
}
|
|
#ifdef DEBUG
|
|
if (!born_point) {
|
|
abort();
|
|
}
|
|
#endif
|
|
return born_point;
|
|
}
|
|
|
|
BornPoint* Room::GetBornPoint(int point_uniid)
|
|
{
|
|
auto itr = born_point_hash_.find(point_uniid);
|
|
return itr != born_point_hash_.end() ? &itr->second : nullptr;
|
|
}
|
|
|
|
void Room::CreateSpawnPoints()
|
|
{
|
|
const std::vector<MetaData::MapTplThing*>* spawn_points = nullptr;
|
|
if (IsMiniRoom()) {
|
|
spawn_points = mini_room_spawn_points_;
|
|
} else {
|
|
spawn_points = normal_room_spawn_points_;
|
|
}
|
|
if (!spawn_points || spawn_points->empty()) {
|
|
abort();
|
|
}
|
|
for (auto& thing_tpl : *spawn_points) {
|
|
int uniid = AllocUniid();
|
|
BornPoint born_point;
|
|
born_point.thing_tpl = thing_tpl;
|
|
born_point_hash_[uniid] = born_point;
|
|
if (thing_tpl == level0room_born_point_meta_) {
|
|
level0room_born_point_uniid_ = uniid;
|
|
}
|
|
if (thing_tpl == level1room_born_point_meta_) {
|
|
level1room_born_point_uniid_ = uniid;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::CreateLoots()
|
|
{
|
|
for (auto& thing_tpl : *loots_) {
|
|
int thing_id = thing_tpl->RandThing();
|
|
MetaData::Equip* equip_meta = MetaMgr::Instance()->GetEquip(thing_id);
|
|
if (equip_meta) {
|
|
int loot_uniid = CreateLoot(
|
|
equip_meta->i->id(),
|
|
a8::Vec2
|
|
(
|
|
thing_tpl->i->x(),
|
|
thing_tpl->i->y()
|
|
),
|
|
1,
|
|
1
|
|
);
|
|
if (loot_uniid && equip_meta->i->equip_type() == EQUIP_TYPE_CAR) {
|
|
Entity* loot_entity = GetEntityByUniId(loot_uniid);
|
|
if (loot_entity && loot_entity->IsEntityType(ET_Loot)) {
|
|
((Loot*)loot_entity)->bullet_num = equip_meta->i->clip_volume();
|
|
((Loot*)loot_entity)->param1 = MetaMgr::Instance()->max_oil;
|
|
((Loot*)loot_entity)->param2 = MetaMgr::Instance()->max_oil;
|
|
CarObject car;
|
|
car.car_id = equip_meta->i->id();
|
|
car.pos = loot_entity->GetPos();
|
|
car_hash_[loot_entity->GetEntityUniId()] = car;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::CreateDropObjs()
|
|
{
|
|
for (auto& building : *buildings_) {
|
|
for (auto& obj : building->meta->i->dropobj()) {
|
|
CreateLoot(obj.id(),
|
|
a8::Vec2
|
|
(
|
|
building->GetX() + obj.x() - building->meta->i->tilewidth() / 2.0,
|
|
building->GetY() + obj.y() - building->meta->i->tileheight() / 2.0
|
|
),
|
|
1,
|
|
1
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::IncBornPointHumanNum(BornPoint* point, Human* hum)
|
|
{
|
|
switch (hum->GetEntitySubType()) {
|
|
case EST_Player:
|
|
{
|
|
++point->player_num;
|
|
}
|
|
break;
|
|
case EST_Android:
|
|
{
|
|
++point->android_num;
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
void Room::DecBornPointHumanNum(BornPoint* point, Human* hum)
|
|
{
|
|
switch (hum->GetEntitySubType()) {
|
|
case EST_Player:
|
|
{
|
|
--point->player_num;
|
|
}
|
|
break;
|
|
case EST_Android:
|
|
{
|
|
--point->android_num;
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
void Room::SecondRandPoint()
|
|
{
|
|
#ifdef DEBUG
|
|
CheckPartObjects();
|
|
#endif
|
|
for (auto& pair : accountid_hash_) {
|
|
Human* hum = pair.second;
|
|
hum->born_point = AllocBornPoint(hum);
|
|
}
|
|
CombineTeamBornPoint();
|
|
if (room_type_ == RT_NewBrid) {
|
|
BornPoint* newbie_point = GetBornPoint(level0room_born_point_uniid_);
|
|
if (newbie_point && first_newbie_) {
|
|
ForceSetBornPoint(first_newbie_, newbie_point);
|
|
} else {
|
|
abort();
|
|
}
|
|
}
|
|
if (room_type_ == RT_MidBrid) {
|
|
BornPoint* newbie_point = GetBornPoint(level1room_born_point_uniid_);
|
|
for (auto& pair : accountid_hash_) {
|
|
pair.second->on_grid_chg = std::bind(&Room::OnHumanGridChg,
|
|
this,
|
|
std::placeholders::_1);
|
|
for (Human* member : *pair.second->team_members) {
|
|
if (newbie_point) {
|
|
ForceSetBornPoint(member, newbie_point);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#ifdef DEBUG
|
|
CheckPartObjects();
|
|
#endif
|
|
if (room_type_ == RT_NewBrid || room_type_ == RT_MidBrid) {
|
|
std::vector<Human*> tmp_humans;
|
|
tmp_humans.reserve(GetRoomMaxPlayerNum());
|
|
for (auto& pair : human_hash_) {
|
|
if (pair.second->IsAndroid() &&
|
|
a8::HasBitFlag(pair.second->status, HS_Disable) &&
|
|
pair.second->team_uuid.empty()
|
|
) {
|
|
tmp_humans.push_back(pair.second);
|
|
}
|
|
}
|
|
std::random_shuffle(tmp_humans.begin(), tmp_humans.end());
|
|
for (size_t i = 0; i < accountid_hash_.size(); ++i) {
|
|
last_human_hash_[tmp_humans[i]->GetEntityUniId()] = tmp_humans[i];
|
|
a8::SetBitFlag(tmp_humans[i]->status, HS_LastAndroid);
|
|
}
|
|
}
|
|
#ifdef DEBUG
|
|
#if 0
|
|
if (GetRoomMode() == kZombieMode) {
|
|
BornPoint* born_point = nullptr;
|
|
int i = 0;
|
|
for (auto& pair : human_hash_) {
|
|
if (!born_point) {
|
|
born_point = pair.second->born_point;
|
|
} else {
|
|
++i;
|
|
pair.second->born_point = born_point;
|
|
a8::Vec2 pos = pair.second->born_point->RandPoint();
|
|
pos.x += i * 60;
|
|
pair.second->SetPos(pos);
|
|
pair.second->FindLocation();
|
|
pair.second->RefreshView();
|
|
grid_service->MoveHuman(pair.second);
|
|
#if 0
|
|
if (++i > 5) {
|
|
i = 0;
|
|
born_point = nullptr;
|
|
}
|
|
#endif
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|
|
}
|
|
|
|
void Room::NotifyGameStart()
|
|
{
|
|
cs::SMGameStart msg;
|
|
for (auto& pair : accountid_hash_) {
|
|
pair.second->SendNotifyMsg(msg);
|
|
}
|
|
|
|
waiting_start_ = true;
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(SERVER_FRAME_RATE * 2,
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
room->waiting_start_ = false;
|
|
},
|
|
&xtimer_attacher_.timer_list_);
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(SERVER_FRAME_RATE * 1,
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
room->ProcDisableHuman();
|
|
room->SecondRandPoint();
|
|
},
|
|
&xtimer_attacher_.timer_list_);
|
|
|
|
if (room_type_ == RT_NewBrid || room_type_ == RT_MidBrid) {
|
|
NewBieRoomStart();
|
|
}
|
|
if (GetRoomMode() == kZombieMode) {
|
|
ZombieModeStart();
|
|
}
|
|
InitAndroidAI();
|
|
}
|
|
|
|
void Room::InitObstacleDatas()
|
|
{
|
|
obstacle_datas_.resize(FIXED_OBJECT_MAXID);
|
|
memset(&obstacle_datas_[0], 0, sizeof(ObstacleData) * FIXED_OBJECT_MAXID);
|
|
}
|
|
|
|
ObstacleData* Room::GetPermanentObstacleData(int obstacle_uniid)
|
|
{
|
|
if (obstacle_uniid >= 0 && obstacle_uniid < (int)obstacle_datas_.size()) {
|
|
return &obstacle_datas_[obstacle_uniid];
|
|
} else {
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
long long Room::GetGasInactiveTime()
|
|
{
|
|
if (room_mode_ == kZombieMode) {
|
|
#if DEBUG
|
|
if (f8::IsTestEnv()) {
|
|
return MetaMgr::Instance()->zbmode_gas_inactive_time;
|
|
} else {
|
|
return 5 + 5;
|
|
}
|
|
#else
|
|
return MetaMgr::Instance()->zbmode_gas_inactive_time;
|
|
#endif
|
|
} else {
|
|
if (room_type_ == RT_NewBrid) {
|
|
if (creator_game_times_ <= 0) {
|
|
return MetaMgr::Instance()->newbie_gas_inactive_time;
|
|
} else {
|
|
return MetaMgr::Instance()->common_gas_inactive_time;
|
|
}
|
|
} else if (room_type_ == RT_MidBrid) {
|
|
return MetaMgr::Instance()->midbrid_gas_inactive_time;
|
|
} else {
|
|
return MetaMgr::Instance()->gas_inactive_time;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::EnableHuman(Human* target)
|
|
{
|
|
if (!RuningInTimer()) {
|
|
abort();
|
|
}
|
|
#ifdef DEBUG
|
|
CheckPartObjects();
|
|
a8::UdpLog::Instance()->Debug("enablehuman %d %d",
|
|
{
|
|
(long long)target,
|
|
target->GetEntityUniId()
|
|
});
|
|
#endif
|
|
if (a8::HasBitFlag(target->status, HS_Disable)) {
|
|
target->OnEnable();
|
|
AddToMoveableHash(target);
|
|
if (!target->real_dead) {
|
|
AddToAliveHumanHash(target);
|
|
}
|
|
}
|
|
#ifdef DEBUG
|
|
CheckPartObjects();
|
|
#endif
|
|
}
|
|
|
|
void Room::DisableHuman(Human* target)
|
|
{
|
|
if (!RuningInTimer()) {
|
|
abort();
|
|
}
|
|
#ifdef DEBUG
|
|
CheckPartObjects();
|
|
a8::UdpLog::Instance()->Debug("disablehuman %d %d",
|
|
{
|
|
(long long)target,
|
|
target->GetEntityUniId()
|
|
});
|
|
#endif
|
|
if (!a8::HasBitFlag(target->status, HS_Disable)) {
|
|
target->OnDisable();
|
|
RemoveFromMoveableHash(target);
|
|
RemoveFromAliveHumanHash(target);
|
|
}
|
|
#ifdef DEBUG
|
|
CheckPartObjects();
|
|
#endif
|
|
}
|
|
|
|
void Room::ShuaGuideAndroid(Human* target)
|
|
{
|
|
#ifdef DEBUG
|
|
a8::UdpLog::Instance()->Debug("room_idx:%d ShuaNewBieAndroid %s %s",
|
|
{
|
|
GetRoomIdx(),
|
|
target->account_id,
|
|
target->name
|
|
});
|
|
#endif
|
|
{
|
|
Human* hum = GetOneCanEnableAndroid();
|
|
if (hum) {
|
|
a8::Vec2 pos = target->GetPos();
|
|
pos.x -= MetaMgr::Instance()->newbie_first_robot_distance;
|
|
if (OverBorder(pos, hum->GetRadius())) {
|
|
pos = target->GetPos();
|
|
pos.x += MetaMgr::Instance()->newbie_first_robot_distance;
|
|
if (OverBorder(pos, hum->GetRadius())) {
|
|
pos = target->GetPos();
|
|
pos.y -= MetaMgr::Instance()->newbie_first_robot_distance;
|
|
if (OverBorder(pos, hum->GetRadius())) {
|
|
pos = target->GetPos();
|
|
pos.y += MetaMgr::Instance()->newbie_first_robot_distance;
|
|
if (OverBorder(pos, hum->GetRadius())) {
|
|
pos = a8::Vec2(1800, 1800);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
hum->SetPos(pos);
|
|
EnableHuman(hum);
|
|
a8::SetBitFlag(hum->status, HS_NewBieGuideAndroid);
|
|
}
|
|
}
|
|
{
|
|
int shua_time = a8::RandEx(
|
|
MetaMgr::Instance()->level0room_shua_robot_min_time,
|
|
MetaMgr::Instance()->level0room_shua_robot_max_time
|
|
);
|
|
xtimer.AddDeadLineTimerAndAttach(SERVER_FRAME_RATE * (shua_time),
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
room->ShuaAndroidTimerFunc();
|
|
},
|
|
&xtimer_attacher_.timer_list_);
|
|
}
|
|
{
|
|
int die_time = a8::RandEx(
|
|
MetaMgr::Instance()->level0room_die_robot_min_time,
|
|
MetaMgr::Instance()->level0room_die_robot_max_time
|
|
);
|
|
xtimer.AddDeadLineTimerAndAttach(SERVER_FRAME_RATE * (die_time),
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
room->DieAndroidTimerFunc();
|
|
},
|
|
&xtimer_attacher_.timer_list_);
|
|
}
|
|
}
|
|
|
|
void Room::ShuaAndroidTimerFunc()
|
|
{
|
|
if (room_type_ == RT_NewBrid || room_type_ == RT_MidBrid) {
|
|
int shua_time = a8::RandEx(
|
|
MetaMgr::Instance()->level0room_shua_robot_min_time,
|
|
MetaMgr::Instance()->level0room_shua_robot_max_time
|
|
);
|
|
int shua_num = a8::RandEx(
|
|
MetaMgr::Instance()->level0room_shua_robot_min_num,
|
|
MetaMgr::Instance()->level0room_shua_robot_max_num
|
|
);
|
|
if (room_type_ == RT_MidBrid) {
|
|
shua_time = a8::RandEx(
|
|
MetaMgr::Instance()->level1room_shua_robot_min_time,
|
|
MetaMgr::Instance()->level1room_shua_robot_max_time
|
|
);
|
|
shua_num = a8::RandEx(
|
|
MetaMgr::Instance()->level1room_shua_robot_min_num,
|
|
MetaMgr::Instance()->level1room_shua_robot_max_num
|
|
);
|
|
}
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(SERVER_FRAME_RATE * shua_time,
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
room->ShuaAndroidTimerFunc();
|
|
},
|
|
&xtimer_attacher_.timer_list_,
|
|
[] (const a8::XParams& param)
|
|
{
|
|
}
|
|
);
|
|
if (shua_time > 0 && shua_num > 0) {
|
|
ProcShuaAndroid(shua_time, shua_num);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::DieAndroidTimerFunc()
|
|
{
|
|
if (room_type_ == RT_NewBrid || room_type_ == RT_MidBrid) {
|
|
int die_time = a8::RandEx(
|
|
MetaMgr::Instance()->level0room_die_robot_min_time,
|
|
MetaMgr::Instance()->level0room_die_robot_max_time
|
|
);
|
|
int die_num = a8::RandEx(
|
|
MetaMgr::Instance()->level0room_die_robot_min_num,
|
|
MetaMgr::Instance()->level0room_die_robot_max_num
|
|
);
|
|
if (room_type_ == RT_MidBrid) {
|
|
die_time = a8::RandEx(
|
|
MetaMgr::Instance()->level1room_die_robot_min_time,
|
|
MetaMgr::Instance()->level1room_die_robot_max_time
|
|
);
|
|
die_num = a8::RandEx(
|
|
MetaMgr::Instance()->level1room_die_robot_min_num,
|
|
MetaMgr::Instance()->level1room_die_robot_max_num
|
|
);
|
|
}
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(SERVER_FRAME_RATE * die_time,
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
room->DieAndroidTimerFunc();
|
|
},
|
|
&xtimer_attacher_.timer_list_,
|
|
[] (const a8::XParams& param)
|
|
{
|
|
}
|
|
);
|
|
if (die_time > 0 && die_num > 0) {
|
|
ProcDieAndroid(die_time, die_num);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::ProcShuaAndroid(int shua_time, int shua_num)
|
|
{
|
|
Human* target = GetOneAlivePlayer();
|
|
if (!target) {
|
|
return;
|
|
}
|
|
int real_shua_num = GetCanShuaNum(shua_num);
|
|
if (real_shua_num <= 0) {
|
|
return;
|
|
}
|
|
#ifdef DEBUG
|
|
a8::UdpLog::Instance()->Debug("ProcShuaAndroid room_idx:%d shua_time:%d shua_num%d real_shua_num:%d",
|
|
{
|
|
GetRoomIdx(),
|
|
shua_time,
|
|
shua_num,
|
|
real_shua_num
|
|
});
|
|
#endif
|
|
|
|
std::vector<Human*> humans;
|
|
GetCanEnableAndroids(humans, real_shua_num);
|
|
for (auto& hum : humans) {
|
|
a8::Vec2 pos = target->GetPos();
|
|
a8::Vec2 dir = target->move_dir;
|
|
dir = a8::Vec2::UP;
|
|
if (rand() % 100 < 1) {
|
|
dir.Rotate(a8::RandAngle() / 2.0f);
|
|
} else {
|
|
dir.Rotate(a8::RandAngle());
|
|
}
|
|
pos = pos + dir * SHUA_RANGE;
|
|
if (OverBorder(pos, hum->GetRadius())) {
|
|
pos.x = target->GetPos().x;
|
|
if (OverBorder(pos, hum->GetRadius())) {
|
|
break;
|
|
}
|
|
}
|
|
hum->SetPos(pos);
|
|
EnableHuman(hum);
|
|
#if 0
|
|
a8::SetBitFlag(hum->status, HS_NewBieGuideAndroid);
|
|
#endif
|
|
InstallCheckAutoDieTimer(hum);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void Room::ProcDieAndroid(int die_time, int die_num)
|
|
{
|
|
#ifdef DEBUG
|
|
a8::UdpLog::Instance()->Debug("ProcDieAndroid room_idx:%d die_time:%d die_num:%d",
|
|
{
|
|
GetRoomIdx(),
|
|
die_time,
|
|
die_num
|
|
});
|
|
#endif
|
|
std::vector<Human*> alive_humans;
|
|
alive_humans.reserve(human_hash_.size());
|
|
GetAliveHumansExcludeLastHuman(alive_humans, 16);
|
|
{
|
|
Human* first_alive_player = nullptr;
|
|
for (auto& pair : accountid_hash_) {
|
|
if (!pair.second->real_dead) {
|
|
first_alive_player = pair.second;
|
|
break;
|
|
}
|
|
}
|
|
if (first_alive_player) {
|
|
std::sort(alive_humans.begin(), alive_humans.end(),
|
|
[first_alive_player] (Human* a, Human* b )
|
|
{
|
|
return first_alive_player->GetPos().ManhattanDistance(a->GetPos()) >
|
|
first_alive_player->GetPos().ManhattanDistance(b->GetPos());
|
|
});
|
|
}
|
|
}
|
|
{
|
|
if (alive_humans.size() == 1) {
|
|
Human* hum = alive_humans[0];
|
|
hum->BeKill(VP_SafeArea,
|
|
"毒圈",
|
|
VW_SafeArea);
|
|
a8::UnSetBitFlag(hum->status, HS_Disable);
|
|
return;
|
|
}
|
|
}
|
|
int dead_num = 0;
|
|
int try_count = 0;
|
|
std::vector<Human*> alive_humans_copy = alive_humans;
|
|
do {
|
|
for (size_t i = 0; i < alive_humans.size(); ++i) {
|
|
Human* hum = alive_humans[i];
|
|
Human* killer = nullptr;
|
|
for (size_t ii = i + 1; ii < alive_humans_copy.size(); ++ii) {
|
|
killer = alive_humans_copy[ii];
|
|
alive_humans.erase(alive_humans.begin() + ii);
|
|
alive_humans.erase(alive_humans.begin() + i);
|
|
alive_humans_copy.erase(alive_humans_copy.begin() + ii);
|
|
alive_humans_copy.erase(alive_humans_copy.begin() + i);
|
|
break;
|
|
}
|
|
if (killer && ((rand() % 100 < 70) || !gas_data_.old_area_meta)) {
|
|
hum->BeKill(killer->GetEntityUniId(),
|
|
killer->name,
|
|
killer->curr_weapon->weapon_id);
|
|
a8::UnSetBitFlag(hum->status, HS_Disable);
|
|
} else {
|
|
hum->BeKill(VP_SafeArea,
|
|
"毒圈",
|
|
VW_SafeArea);
|
|
if (!alive_humans.empty()) {
|
|
alive_humans.erase(alive_humans.begin() + i);
|
|
alive_humans_copy.erase(alive_humans_copy.begin() + i);
|
|
}
|
|
a8::UnSetBitFlag(hum->status, HS_Disable);
|
|
}
|
|
++dead_num;
|
|
break;
|
|
}
|
|
} while(++try_count < 10 && dead_num < die_num && !alive_humans.empty());
|
|
}
|
|
|
|
void Room::CheckAutoDie(Human* target,
|
|
int autodie_time,
|
|
int autodie_distance,
|
|
int check_times)
|
|
{
|
|
bool nodie = false;
|
|
target->TouchAllLayerHumanList
|
|
(
|
|
[&nodie, target, autodie_time, autodie_distance] (Human* hum, bool& stop)
|
|
{
|
|
if (!hum->real_dead &&
|
|
hum->GetEntityUniId() != target->GetEntityUniId() &&
|
|
hum->IsPlayer()) {
|
|
if (std::fabs(hum->GetPos().x - target->GetPos().x) <= autodie_distance &&
|
|
std::fabs(hum->GetPos().y - target->GetPos().y) <= autodie_distance) {
|
|
stop = true;
|
|
nodie = true;
|
|
}
|
|
}
|
|
});
|
|
if (nodie) {
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(
|
|
SERVER_FRAME_RATE * (2 + rand() % 3),
|
|
a8::XParams()
|
|
.SetSender(target)
|
|
.SetParam1(autodie_time)
|
|
.SetParam2(autodie_distance)
|
|
.SetParam3(check_times + 1),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Human* hum = (Human*)param.sender.GetUserData();
|
|
hum->room->CheckAutoDie(
|
|
hum,
|
|
param.param1,
|
|
param.param2,
|
|
param.param3
|
|
);
|
|
},
|
|
&target->xtimer_attacher.timer_list_);
|
|
} else {
|
|
std::vector<Human*> alive_humans;
|
|
GetAliveHumans(alive_humans, 5, target);
|
|
if (!alive_humans.empty()) {
|
|
Human* killer = alive_humans[rand() % alive_humans.size()];
|
|
a8::UnSetBitFlag(target->status, HS_Disable);
|
|
target->BeKill(killer->GetEntityUniId(),
|
|
killer->name,
|
|
killer->curr_weapon->weapon_id);
|
|
} else {
|
|
a8::UnSetBitFlag(target->status, HS_Disable);
|
|
target->BeKill(VP_SafeArea,
|
|
"毒圈",
|
|
VW_SafeArea);
|
|
}
|
|
}
|
|
}
|
|
|
|
bool Room::HasPlayerInRound(const a8::Vec2& pos, float rad)
|
|
{
|
|
for (auto& pair : accountid_hash_) {
|
|
if (std::fabs(pair.second->GetPos().x - pos.x) < rad &&
|
|
std::fabs(pair.second->GetPos().y - pos.y) < rad) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void Room::ProcDisableHuman()
|
|
{
|
|
if (room_type_ == RT_NewBrid || room_type_ == RT_MidBrid) {
|
|
for (auto& pair : human_hash_) {
|
|
if (pair.second->IsAndroid() &&
|
|
pair.second->team_uuid.empty() &&
|
|
!a8::HasBitFlag(pair.second->status, HS_Disable)) {
|
|
DisableHuman(pair.second);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::OnHumanGridChg(Human* target)
|
|
{
|
|
xtimer.AddDeadLineTimerAndAttach(2,
|
|
a8::XParams()
|
|
.SetSender(target),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Human* hum = (Human*)param.sender.GetUserData();
|
|
hum->room->ShuaGridRound(hum);
|
|
},
|
|
&target->xtimer_attacher.timer_list_);
|
|
}
|
|
|
|
void Room::ShuaGridRound(Human* target)
|
|
{
|
|
int count = 0;
|
|
for (auto& pair : human_hash_) {
|
|
Human* hum = pair.second;
|
|
if (hum->IsAndroid() &&
|
|
a8::HasBitFlag(hum->status, HS_Disable) &&
|
|
!a8::HasBitFlag(hum->status, HS_LastAndroid) &&
|
|
!hum->real_dead &&
|
|
hum->team_uuid.empty() &&
|
|
grid_service->InView(target->GetGridId(), hum->GetPos().x, hum->GetPos().y)
|
|
) {
|
|
EnableHuman(hum);
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(
|
|
SERVER_FRAME_RATE * 10,
|
|
a8::XParams()
|
|
.SetSender(hum)
|
|
.SetParam1(10)
|
|
.SetParam2(VIEW_RANGE * 1.5)
|
|
.SetParam3(0),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Human* hum = (Human*)param.sender.GetUserData();
|
|
hum->room->CheckAutoDie(
|
|
hum,
|
|
param.param1,
|
|
param.param2,
|
|
param.param3
|
|
);
|
|
},
|
|
&hum->xtimer_attacher.timer_list_);
|
|
++count;
|
|
}
|
|
}
|
|
#ifdef DEBUG
|
|
a8::UdpLog::Instance()->Debug("OnHumanGrid %d %d", {target->GetGridId(), count});
|
|
#endif
|
|
#ifdef DEBUG
|
|
CheckPartObjects();
|
|
#endif
|
|
}
|
|
|
|
void Room::CheckPartObjects(Human* testa, Human* testb)
|
|
{
|
|
for (auto& pair1 : human_hash_) {
|
|
Human* a = pair1.second;
|
|
for (auto& pair2 : human_hash_) {
|
|
Human* b = pair2.second;
|
|
if (testa && testb) {
|
|
if ((a == testa && b == testb) ||
|
|
(a == testb && b == testa)) {
|
|
}
|
|
}
|
|
if (a->InPartObjects(b)) {
|
|
if (!b->InPartObjects(a)) {
|
|
abort();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for (auto& pair1 : human_hash_) {
|
|
Human* huma = pair1.second;
|
|
huma->TouchAllLayerHumanList
|
|
(
|
|
[&huma, testa, testb] (Human* humb, bool& stop)
|
|
{
|
|
if (testa && testb) {
|
|
if ((huma == testa && humb == testb) ||
|
|
(huma == testb && humb == testa)) {
|
|
}
|
|
}
|
|
if (a8::HasBitFlag(huma->status, HS_Disable) ||
|
|
a8::HasBitFlag(humb->status, HS_Disable) ||
|
|
huma->GetPartObjectsCount() <= 0
|
|
) {
|
|
return;
|
|
}
|
|
if (!huma->InPartObjects(humb)) {
|
|
abort();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
void Room::GetAliveHumans(std::vector<Human*>& alive_humans, size_t num, Human* exclude_hum)
|
|
{
|
|
alive_humans.reserve(num);
|
|
{
|
|
if (GetFrameNo() % 8 < 5) {
|
|
for (auto itr = human_hash_.begin(); itr != human_hash_.end(); ++itr) {
|
|
if (itr->second == exclude_hum) {
|
|
continue;
|
|
}
|
|
CheckAliveHuman(itr->second, alive_humans);
|
|
if (alive_humans.size() > num) {
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
for (auto itr = human_hash_.rbegin(); itr != human_hash_.rend(); ++itr) {
|
|
if (itr->second == exclude_hum) {
|
|
continue;
|
|
}
|
|
CheckAliveHuman(itr->second, alive_humans);
|
|
if (alive_humans.size() > num) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::GetAliveHumansExcludeLastHuman(std::vector<Human*>& alive_humans, size_t num)
|
|
{
|
|
alive_humans.reserve(num);
|
|
{
|
|
if (GetFrameNo() % 8 < 5) {
|
|
for (auto itr = human_hash_.begin(); itr != human_hash_.end(); ++itr) {
|
|
if (a8::HasBitFlag(itr->second->status, HS_LastAndroid)) {
|
|
continue;
|
|
}
|
|
CheckAliveHuman(itr->second, alive_humans);
|
|
if (alive_humans.size() > num) {
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
for (auto itr = human_hash_.rbegin(); itr != human_hash_.rend(); ++itr) {
|
|
if (a8::HasBitFlag(itr->second->status, HS_LastAndroid)) {
|
|
continue;
|
|
}
|
|
CheckAliveHuman(itr->second, alive_humans);
|
|
if (alive_humans.size() > num) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::CheckAliveHuman(Human* hum, std::vector<Human*>& alive_humans)
|
|
{
|
|
if (hum->IsAndroid() &&
|
|
!hum->real_dead &&
|
|
hum->team_uuid.empty() &&
|
|
a8::HasBitFlag(hum->status, HS_Disable) &&
|
|
!HasPlayerInRound(hum->GetPos(), VIEW_RANGE)) {
|
|
alive_humans.push_back(hum);
|
|
}
|
|
}
|
|
|
|
bool Room::RuningInTimer()
|
|
{
|
|
return xtimer.GetRunningTimer() != nullptr;
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
void Room::InitDebugInfo()
|
|
{
|
|
#if 0
|
|
xtimer.AddRepeatTimer
|
|
(
|
|
SERVER_FRAME_RATE * 3,
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
a8::UdpLog::Instance()->Debug("room_idx:%d real_alive_count:%d",
|
|
{
|
|
room->GetRoomIdx(),
|
|
room->RealAliveCount()
|
|
});
|
|
});
|
|
#endif
|
|
}
|
|
|
|
void Room::UnInitDebugInfo()
|
|
{
|
|
#if 0
|
|
a8::UdpLog::Instance()->Debug("destroy room_idx:%d",
|
|
{
|
|
GetRoomIdx()
|
|
});
|
|
#endif
|
|
}
|
|
#endif
|
|
|
|
a8::Vec2 Room::GetDefaultBornPoint()
|
|
{
|
|
a8::Vec2 pos = a8::Vec2(DEFAULT_BORN_POINT_X + rand() % 1400,
|
|
DEFAULT_BORN_POINT_Y + rand() % 1500);
|
|
return pos;
|
|
}
|
|
|
|
void Room::AddPlayerPostProc(Player* hum)
|
|
{
|
|
if (room_type_ == RT_NewBrid) {
|
|
if (creator_game_times_ <= 0) {
|
|
CreateAndroid(10 + rand() % 5);
|
|
} else {
|
|
CreateAndroid(2);
|
|
}
|
|
xtimer.AddDeadLineTimerAndAttach(SERVER_FRAME_RATE * (1 + rand() % 3),
|
|
a8::XParams()
|
|
.SetSender(hum),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Human* hum = (Human*)param.sender.GetUserData();
|
|
hum->room->ShowAndroid(hum, 1 + rand() % 2);
|
|
},
|
|
&hum->xtimer_attacher.timer_list_);
|
|
}
|
|
if (room_type_ == RT_NewBrid || room_type_ == RT_MidBrid) {
|
|
for (int buff_id : MetaMgr::Instance()->newbie_buff_list) {
|
|
MetaData::Buff* buff_meta = MetaMgr::Instance()->GetBuff(buff_id);
|
|
if (buff_meta) {
|
|
hum->AddBuff(hum, buff_meta, 1);
|
|
}
|
|
}
|
|
if (room_type_ == RT_MidBrid) {
|
|
Player* first_player = GetOneAlivePlayer();
|
|
if (first_player && first_player->born_point) {
|
|
if (hum->born_point) {
|
|
DecBornPointHumanNum(hum->born_point, hum);
|
|
}
|
|
hum->born_point = first_player->born_point;
|
|
IncBornPointHumanNum(hum->born_point, hum);
|
|
}
|
|
}
|
|
}
|
|
if (GetRoomMode() == kZombieMode) {
|
|
#if 0
|
|
hum->ChangeToRace(kZombieRace, 2);
|
|
#else
|
|
hum->ChangeToRace(kHumanRace, 1);
|
|
#endif
|
|
}
|
|
#ifdef DEBUG
|
|
xtimer.AddRepeatTimerAndAttach
|
|
(SERVER_FRAME_RATE * 5,
|
|
a8::XParams()
|
|
.SetSender(hum),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Human* hum = (Human*)param.sender.GetUserData();
|
|
std::string debugmsg = a8::Format("weapon_id:%d weapon_lv:%d atk:%f fire_rate:%f "
|
|
"volume:%d maxhp:%f curr_hp:%f",
|
|
{
|
|
hum->curr_weapon->weapon_id,
|
|
hum->curr_weapon->weapon_lv,
|
|
hum->curr_weapon->GetAttrValue(kHAT_Atk),
|
|
hum->curr_weapon->GetAttrValue(kHAT_FireRate),
|
|
hum->curr_weapon->GetAttrValue(kHAT_Volume),
|
|
hum->curr_weapon->GetAttrValue(kHAT_MaxHp),
|
|
hum->GetHP()
|
|
});
|
|
hum->SendDebugMsg(debugmsg);
|
|
},
|
|
&hum->xtimer_attacher.timer_list_);
|
|
#endif
|
|
}
|
|
|
|
void Room::AddToEntityHash(Entity* entity)
|
|
{
|
|
uniid_hash_[entity->GetEntityUniId()] = entity;
|
|
}
|
|
|
|
void Room::AddToHumanHash(Human* hum)
|
|
{
|
|
human_hash_[hum->GetEntityUniId()] = hum;
|
|
}
|
|
|
|
void Room::AddToAliveHumanHash(Human* hum)
|
|
{
|
|
alive_human_hash_[hum->GetEntityUniId()] = hum;
|
|
}
|
|
|
|
void Room::AddToMoveableHash(MoveableEntity* entity)
|
|
{
|
|
moveable_hash_[entity->GetEntityUniId()] = entity;
|
|
}
|
|
|
|
void Room::AddToAccountHash(Player* hum)
|
|
{
|
|
accountid_hash_[hum->account_id] = hum;
|
|
}
|
|
|
|
void Room::AddToLaterAddHash(RoomEntity* entity)
|
|
{
|
|
later_add_hash_[entity->GetEntityUniId()] = entity;
|
|
}
|
|
|
|
void Room::AddToRemovedRobotHash(Human* hum)
|
|
{
|
|
removed_robot_hash_[hum->GetEntityUniId()] = hum;
|
|
}
|
|
|
|
void Room::RemoveFromEntityHash(Entity* entity)
|
|
{
|
|
uniid_hash_.erase(entity->GetEntityUniId());
|
|
}
|
|
|
|
void Room::RemoveFromMoveableHash(MoveableEntity* entity)
|
|
{
|
|
moveable_hash_.erase(entity->GetEntityUniId());
|
|
}
|
|
|
|
void Room::RemoveFromHuamnHash(Human* hum)
|
|
{
|
|
human_hash_.erase(hum->GetEntityUniId());
|
|
}
|
|
|
|
void Room::RemoveFromAliveHumanHash(Human* hum)
|
|
{
|
|
alive_human_hash_.erase(hum->GetEntityUniId());
|
|
}
|
|
|
|
void Room::RemoveFromLaterAddHash(RoomEntity* entity)
|
|
{
|
|
later_add_hash_.erase(entity->GetEntityUniId());
|
|
}
|
|
|
|
void Room::CombineTeamBornPoint()
|
|
{
|
|
for (auto& pair : team_hash_) {
|
|
Human* target = nullptr;
|
|
for (Human* hum : pair.second) {
|
|
if (!target) {
|
|
target = hum;
|
|
} else {
|
|
if (target->born_point) {
|
|
if (hum->born_point) {
|
|
DecBornPointHumanNum(hum->born_point, hum);
|
|
}
|
|
hum->born_point = target->born_point;
|
|
if (hum->born_point) {
|
|
IncBornPointHumanNum(hum->born_point, hum);
|
|
}
|
|
}
|
|
} //end if
|
|
if (!hum->born_point) {
|
|
hum->SetX(DEFAULT_BORN_POINT_X + rand() % 100);
|
|
hum->SetY(DEFAULT_BORN_POINT_Y + rand() % 200);
|
|
} else {
|
|
hum->SetPos(hum->born_point->RandPoint());
|
|
}
|
|
if (!a8::HasBitFlag(hum->status, HS_Disable)) {
|
|
hum->FindLocation();
|
|
hum->RefreshView();
|
|
grid_service->MoveHuman(hum);
|
|
} else if (room_type_ == RT_MidBrid && !hum->team_uuid.empty()){
|
|
EnableHuman(hum);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::ForceSetBornPoint(Human* hum, BornPoint* born_point)
|
|
{
|
|
if (born_point && hum->born_point != born_point) {
|
|
if (hum->born_point) {
|
|
DecBornPointHumanNum(hum->born_point, hum);
|
|
}
|
|
hum->born_point = born_point;
|
|
if (hum->born_point) {
|
|
IncBornPointHumanNum(hum->born_point, hum);
|
|
hum->SetPos(hum->born_point->RandPoint());
|
|
}
|
|
hum->FindLocation();
|
|
hum->RefreshView();
|
|
grid_service->MoveHuman(hum);
|
|
}
|
|
}
|
|
|
|
BornPoint* Room::ForceTakeBornPoint(Human* hum, BornPoint* reserve_born_point)
|
|
{
|
|
if (!reserve_born_point) {
|
|
abort();
|
|
}
|
|
if (hum->born_point == reserve_born_point) {
|
|
abort();
|
|
}
|
|
if (!hum->born_point) {
|
|
abort();
|
|
}
|
|
BornPoint* pre_point = hum->born_point;
|
|
if (pre_point) {
|
|
DecBornPointHumanNum(pre_point, hum);
|
|
}
|
|
for (auto& pair : human_hash_) {
|
|
if (pair.second->born_point == reserve_born_point) {
|
|
ForceSetBornPoint(pair.second, pre_point);
|
|
}
|
|
}
|
|
return reserve_born_point;
|
|
}
|
|
|
|
void Room::NewBieRoomStart()
|
|
{
|
|
if (room_type_ == RT_NewBrid) {
|
|
for (auto& pair : accountid_hash_) {
|
|
first_newbie_ = pair.second;
|
|
break;
|
|
}
|
|
int protect_time = MetaMgr::Instance()->level0room_robot_protect_time;
|
|
if (creator_game_times_ > 0) {
|
|
protect_time = 6;
|
|
}
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(SERVER_FRAME_RATE * protect_time,
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
if (room->GetFirstNewBie()) {
|
|
room->GetFirstNewBie()->ProcNewBieLogic();
|
|
}
|
|
},
|
|
&xtimer_attacher_.timer_list_);
|
|
}
|
|
if (room_type_ == RT_MidBrid) {
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(SERVER_FRAME_RATE * MetaMgr::Instance()->level1room_shua_robot_min_time,
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
room->DieAndroidTimerFunc();
|
|
},
|
|
&xtimer_attacher_.timer_list_);
|
|
}
|
|
}
|
|
|
|
void Room::ZombieModeStart()
|
|
{
|
|
if (GetRoomMode() != kZombieMode) {
|
|
abort();
|
|
}
|
|
if (human_hash_.size() != GetRoomMaxPlayerNum()) {
|
|
abort();
|
|
}
|
|
#ifdef DEBUG
|
|
debug_trace = true;
|
|
#endif
|
|
std::vector<Human*> human_list;
|
|
for (auto& pair : human_hash_) {
|
|
human_list.push_back(pair.second);
|
|
if (RoomMgr::Instance()->IsGM(pair.second->account_id)) {
|
|
debug_trace = true;
|
|
}
|
|
}
|
|
std::random_shuffle(human_list.begin(), human_list.end());
|
|
for (size_t i = 0; i < 2; ++i) {
|
|
Human* hum = human_list[i];
|
|
hum->ChangeToRace(kZombieRace, 1);
|
|
}
|
|
battle_report_timer_ = xtimer.AddDeadLineTimerAndAttach
|
|
(SERVER_FRAME_RATE * MetaMgr::Instance()->zbmode_game_duration,
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
#if 1
|
|
if (!room->game_over_) {
|
|
room->game_timeout_ = true;
|
|
room->game_over_ = true;
|
|
room->game_over_frameno_ = room->GetFrameNo();
|
|
room->OnGameOver();
|
|
}
|
|
#else
|
|
room->BattleReport();
|
|
#endif
|
|
},
|
|
&xtimer_attacher_.timer_list_,
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
room->battle_report_timer_ = nullptr;
|
|
}
|
|
);
|
|
}
|
|
|
|
Human* Room::GetOneCanEnableAndroid()
|
|
{
|
|
std::vector<Human*> humans;
|
|
GetCanEnableAndroids(humans, 1);
|
|
return !humans.empty() ? humans[0] : nullptr;
|
|
}
|
|
|
|
void Room::GetCanEnableAndroids(std::vector<Human*>& humans, size_t num)
|
|
{
|
|
for (auto& pair : human_hash_) {
|
|
if (pair.second->IsAndroid() &&
|
|
!pair.second->real_dead &&
|
|
a8::HasBitFlag(pair.second->status, HS_Disable) &&
|
|
!a8::HasBitFlag(pair.second->status, HS_LastAndroid)
|
|
) {
|
|
if (humans.size() >= num) {
|
|
break;
|
|
}
|
|
humans.push_back(pair.second);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::InstallCheckAutoDieTimer(Human* hum)
|
|
{
|
|
int autodie_time = 10;
|
|
int autodie_distance = 500;
|
|
if (room_type_ == RT_NewBrid) {
|
|
autodie_time = MetaMgr::Instance()->level0room_robot_autodie_time;
|
|
autodie_distance = MetaMgr::Instance()->level0room_robot_autodie_distance;
|
|
} else if (room_type_ == RT_MidBrid) {
|
|
autodie_time = MetaMgr::Instance()->level1room_robot_autodie_time;
|
|
autodie_distance = MetaMgr::Instance()->level1room_robot_autodie_distance;
|
|
}
|
|
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(
|
|
SERVER_FRAME_RATE * autodie_time,
|
|
a8::XParams()
|
|
.SetSender(hum)
|
|
.SetParam1(autodie_time)
|
|
.SetParam2(autodie_distance)
|
|
.SetParam3(0),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Human* hum = (Human*)param.sender.GetUserData();
|
|
hum->room->CheckAutoDie(
|
|
hum,
|
|
param.param1,
|
|
param.param2,
|
|
param.param3
|
|
);
|
|
},
|
|
&hum->xtimer_attacher.timer_list_);
|
|
}
|
|
|
|
Player* Room::GetOneAlivePlayer()
|
|
{
|
|
std::vector<Player*> humans;
|
|
GetAlivePlayers(humans, 1);
|
|
return !humans.empty() ? humans[0] : nullptr;
|
|
}
|
|
|
|
void Room::GetAlivePlayers(std::vector<Player*>& humans, size_t num)
|
|
{
|
|
for (auto& pair : accountid_hash_) {
|
|
if (!pair.second->real_dead) {
|
|
if (humans.size() >= num) {
|
|
break;
|
|
}
|
|
humans.push_back(pair.second);
|
|
}
|
|
}
|
|
}
|
|
|
|
int Room::GetCanShuaNum(int shua_num)
|
|
{
|
|
int real_shua_num = shua_num;
|
|
if (room_type_ == RT_NewBrid) {
|
|
real_shua_num = std::max(0,
|
|
MetaMgr::Instance()->level0room_robot_water - RealAliveCount() +
|
|
(int)accountid_hash_.size());
|
|
} else if (room_type_ == RT_MidBrid) {
|
|
real_shua_num = std::max(0,
|
|
MetaMgr::Instance()->level1room_robot_water - RealAliveCount() +
|
|
(int)accountid_hash_.size());
|
|
}
|
|
return real_shua_num;
|
|
}
|
|
|
|
void Room::CreateLevel0RoomSpecThings()
|
|
{
|
|
if (level0room_spec_things_) {
|
|
for (auto& thing_tpl : *level0room_spec_things_) {
|
|
int thing_id = thing_tpl->RandThing();
|
|
MetaData::MapThing* thing_meta = MetaMgr::Instance()->GetMapThing(thing_id);
|
|
if (thing_meta) {
|
|
InternalCreateObstacle(thing_id, thing_tpl->i->x(), thing_tpl->i->y(),
|
|
[] (Obstacle* entity)
|
|
{
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
bool Room::CanAddToScene(Human* hum)
|
|
{
|
|
if (room_type_ == RT_NewBrid) {
|
|
return false;
|
|
} else if (room_type_ == RT_MidBrid) {
|
|
Player* player = GetOneAlivePlayer();
|
|
if (player) {
|
|
if (std::fabs(hum->GetPos().x - player->GetPos().x) <= 1024 &&
|
|
std::fabs(hum->GetPos().y - player->GetPos().y) <= 1024) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void Room::SyncFrameData()
|
|
{
|
|
for (auto& pair : human_hash_) {
|
|
pair.second->SendUpdateMsg();
|
|
}
|
|
frame_event.Clear();
|
|
}
|
|
|
|
void Room::CheckShowHand()
|
|
{
|
|
if (show_handed_) {
|
|
return;
|
|
}
|
|
if (room_type_ == RT_NewBrid || room_type_ == RT_MidBrid) {
|
|
int real_player_num = accountid_hash_.size();
|
|
if (room_type_ == RT_MidBrid) {
|
|
for (auto& pair : accountid_hash_) {
|
|
Human* player = pair.second;
|
|
real_player_num = 0;
|
|
for (Human* hum : *player->team_members) {
|
|
if (!hum->real_dead) {
|
|
++real_player_num;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
if (alive_count_ <= (int)(real_player_num + last_human_hash_.size())) {
|
|
#ifdef DEBUG
|
|
a8::UdpLog::Instance()->Debug
|
|
(
|
|
"showHand %d %d %d %d",
|
|
{
|
|
alive_count_,
|
|
accountid_hash_.size(),
|
|
last_human_hash_.size(),
|
|
real_player_num
|
|
}
|
|
);
|
|
#endif
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(SERVER_FRAME_RATE,
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Room* room = (Room*)param.sender.GetUserData();
|
|
room->ShowHand();
|
|
},
|
|
&xtimer_attacher_.timer_list_);
|
|
show_handed_ = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::ShowHand()
|
|
{
|
|
std::vector<Player*> players;
|
|
std::vector<Human*> androids;
|
|
for (auto& pair : accountid_hash_) {
|
|
players.push_back(pair.second);
|
|
}
|
|
for (auto& pair : last_human_hash_) {
|
|
androids.push_back(pair.second);
|
|
}
|
|
for (size_t i = 0; i < players.size(); ++i) {
|
|
if (i >= androids.size()) {
|
|
break;
|
|
}
|
|
Player* target = players[i];
|
|
Human* hum = androids[i];
|
|
{
|
|
a8::Vec2 pos = target->GetPos();
|
|
a8::Vec2 dir = target->move_dir;
|
|
if (rand() % 100 < 80) {
|
|
dir.Rotate(a8::RandAngle() / 2.0f);
|
|
} else {
|
|
dir.Rotate(a8::RandAngle());
|
|
}
|
|
pos = pos + dir * VIEW_RANGE;
|
|
if (OverBorder(pos, hum->GetRadius())) {
|
|
pos.x = target->GetPos().x;
|
|
if (OverBorder(pos, hum->GetRadius())) {
|
|
pos = target->GetPos(); //!!!
|
|
}
|
|
}
|
|
hum->last_human_target = target;
|
|
hum->SetPos(pos);
|
|
EnableHuman(hum);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::ShuaLastGas()
|
|
{
|
|
for (auto& pair : human_hash_) {
|
|
Human* hum = pair.second;
|
|
if (hum->IsAndroid() &&
|
|
a8::HasBitFlag(hum->status, HS_Disable) &&
|
|
!a8::HasBitFlag(hum->status, HS_LastAndroid) &&
|
|
!hum->real_dead &&
|
|
hum->team_uuid.empty()
|
|
) {
|
|
EnableHuman(hum);
|
|
xtimer.AddDeadLineTimerAndAttach
|
|
(
|
|
SERVER_FRAME_RATE * 10,
|
|
a8::XParams()
|
|
.SetSender(hum)
|
|
.SetParam1(10)
|
|
.SetParam2(VIEW_RANGE * 1.5)
|
|
.SetParam3(0),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Human* hum = (Human*)param.sender.GetUserData();
|
|
hum->room->CheckAutoDie(
|
|
hum,
|
|
param.param1,
|
|
param.param2,
|
|
param.param3
|
|
);
|
|
},
|
|
&hum->xtimer_attacher.timer_list_);
|
|
}
|
|
}
|
|
#ifdef DEBUG
|
|
CheckPartObjects();
|
|
#endif
|
|
}
|
|
|
|
bool Room::IsMiniRoom()
|
|
{
|
|
#if 1
|
|
return false;
|
|
#else
|
|
return
|
|
GetRoomType() == RT_NewBrid ||
|
|
GetRoomType() == RT_MidBrid ||
|
|
GetRoomType() == RT_OldBrid1;
|
|
#endif
|
|
}
|
|
|
|
void Room::FillObjectPositions(Human* hum, cs::SMUpdate& msg)
|
|
{
|
|
for (auto& pair : human_hash_) {
|
|
if (pair.second != hum) {
|
|
auto p = msg.add_object_positions();
|
|
p->set_obj_uniid(pair.second->GetEntityUniId());
|
|
TypeConvert::ToPb(pair.second->GetPos(), p->mutable_pos());
|
|
TypeConvert::ToPb(pair.second->attack_dir, p->mutable_dir());
|
|
p->set_race((int)pair.second->GetRace());
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::RemoveRescue(Human* hum)
|
|
{
|
|
for (auto& pair : human_hash_) {
|
|
if (pair.second != hum && pair.second->action_type == AT_Relive &&
|
|
pair.second->action_target_id == hum->GetEntityUniId()) {
|
|
pair.second->CancelAction();
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::NotifyCountdown(const std::string& msg, int time)
|
|
{
|
|
for (auto& pair : accountid_hash_) {
|
|
pair.second->SendShowCountdown(msg, time);
|
|
}
|
|
}
|
|
|
|
void Room::NotifySysPiao(const std::string& msg, int color, int duration)
|
|
{
|
|
for (auto& pair : accountid_hash_) {
|
|
pair.second->SendSysPiaoMsg(msg, color, duration);
|
|
}
|
|
}
|
|
|
|
void Room::OnZombieAppear(Human* hum)
|
|
{
|
|
if (hum->GetRace() == kZombieRace && hum->meta->i->level() == 3 && !sent_zombie_boss_notify) {
|
|
sent_zombie_boss_notify = true;
|
|
TouchPlayerList
|
|
(
|
|
a8::XParams()
|
|
.SetParam1(hum->meta->i->name()),
|
|
[] (Player* hum, a8::XParams& param)
|
|
{
|
|
hum->SendSysPiaoMsg(a8::Format("%s出现了", {param.param1.GetString()}),
|
|
a8::MkRgb(255, 0, 0),
|
|
3);
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
int Room::GetAliveCountByRace(RaceType_e race)
|
|
{
|
|
int count = 0;
|
|
for (auto& pair : human_hash_) {
|
|
if (pair.second->GetRace() == race &&
|
|
!pair.second->dead) {
|
|
++count;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
int Room::GetOnlinePlayerNum()
|
|
{
|
|
int num = 0;
|
|
for (auto& pair : accountid_hash_) {
|
|
if (pair.second->socket_handle != 0) {
|
|
++num;
|
|
}
|
|
}
|
|
return num;
|
|
}
|
|
|
|
size_t Room::GetRoomMaxPlayerNum()
|
|
{
|
|
if (room_mode_ == kZombieMode) {
|
|
return MetaMgr::Instance()->zbmode_player_num;
|
|
} else {
|
|
if (IsMiniRoom()) {
|
|
return MINI_ROOM_MAX_PLAYER_NUM;
|
|
} else {
|
|
return NORMAL_ROOM_MAX_PLAYER_NUM;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Room::InitAndroidAI()
|
|
{
|
|
std::vector<Android*> androids;
|
|
for (auto& pair : human_hash_) {
|
|
Human* hum = pair.second;
|
|
if (hum->IsAndroid()) {
|
|
androids.push_back((Android*)hum);
|
|
}
|
|
}
|
|
std::vector<std::tuple<int, int>>* ai_weights = nullptr;
|
|
int ai_weights_space = 0;
|
|
switch (GetRoomType()) {
|
|
case RT_OldBrid1:
|
|
{
|
|
ai_weights = &MetaMgr::Instance()->a_room_ai_weights;
|
|
ai_weights_space = MetaMgr::Instance()->a_room_ai_weights_space;
|
|
}
|
|
break;
|
|
case RT_OldBrid2:
|
|
{
|
|
ai_weights = &MetaMgr::Instance()->b_room_ai_weights;
|
|
ai_weights_space = MetaMgr::Instance()->b_room_ai_weights_space;
|
|
}
|
|
break;
|
|
case RT_OldBrid3:
|
|
{
|
|
ai_weights = &MetaMgr::Instance()->c_room_ai_weights;
|
|
ai_weights_space = MetaMgr::Instance()->c_room_ai_weights_space;
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
ai_weights = &MetaMgr::Instance()->a_room_ai_weights;
|
|
ai_weights_space = MetaMgr::Instance()->a_room_ai_weights_space;
|
|
}
|
|
break;
|
|
}
|
|
std::array<int, MAX_AI_LEVEL> ai_num = {};
|
|
for (Android* hum : androids) {
|
|
#ifdef DEBUG
|
|
#if 0
|
|
hum->SetAiLevel(7);
|
|
continue;
|
|
#endif
|
|
#endif
|
|
int rnd = rand() % ai_weights_space;
|
|
++rnd;
|
|
for (auto& tuple : *ai_weights) {
|
|
int ai_level = std::get<0>(tuple);
|
|
int space = std::get<1>(tuple);
|
|
if (rnd <= space) {
|
|
hum->SetAiLevel(ai_level);
|
|
++ai_num[ai_level];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
#ifdef DEBUG
|
|
for (auto& pair : accountid_hash_) {
|
|
pair.second->SendDebugMsg
|
|
(
|
|
a8::Format("room_type:%d ai_num level1:%d level2:%d level3:%d level4:%d level5:%d level6:%d level7:%d level8:%d",
|
|
{
|
|
GetRoomType(),
|
|
ai_num[0],
|
|
ai_num[1],
|
|
ai_num[2],
|
|
ai_num[3],
|
|
ai_num[4],
|
|
ai_num[5],
|
|
ai_num[6],
|
|
ai_num[7]
|
|
}
|
|
)
|
|
);
|
|
}
|
|
#endif
|
|
}
|