From 6e58b6b66a2bb0005f1fdc769b804076adc96ece Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 30 Mar 2021 18:05:38 +0800 Subject: [PATCH] 1 --- server/gameserver/creature.cc | 147 +++++++++++++++++++++++++++++++ server/gameserver/creature.h | 14 ++- server/gameserver/hero.ai.cc | 14 --- server/gameserver/human.cc | 157 ++-------------------------------- server/gameserver/human.h | 11 --- 5 files changed, 165 insertions(+), 178 deletions(-) diff --git a/server/gameserver/creature.cc b/server/gameserver/creature.cc index c32728d..9763dbf 100644 --- a/server/gameserver/creature.cc +++ b/server/gameserver/creature.cc @@ -69,6 +69,15 @@ void InternalShot(Creature* c, Creature::Creature():MoveableEntity() { weak_ptr_chunk_.Set(this); + inventory_[IS_1XSCOPE] = 1; + + if (MetaMgr::Instance()->fighting_mode) { + inventory_[IS_9MM] = FIGHTING_MODE_BULLET_NUM; + inventory_[IS_556MM] = FIGHTING_MODE_BULLET_NUM; + inventory_[IS_762MM] = FIGHTING_MODE_BULLET_NUM; + inventory_[IS_12GAUGE] = FIGHTING_MODE_BULLET_NUM; + inventory_[IS_RPG] = FIGHTING_MODE_BULLET_NUM; + } } Creature::~Creature() @@ -957,3 +966,141 @@ void Creature::UpdatePoisoning() SyncAroundPlayers(__FILE__, __LINE__, __func__); } } + +void Creature::Shot(a8::Vec2& target_dir, bool& shot_ok) +{ + shot_ok = false; + if (!curr_weapon->meta) { + return; + } + if (downed) { + return; + } + + if (curr_weapon->weapon_idx != 0 && + curr_weapon->ammo <= 0) { + AutoLoadingBullet(); + return; + } + if ((room->GetFrameNo() - last_shot_frameno_) * (1000 / SERVER_FRAME_RATE) < + curr_weapon->GetAttrValue(kHAT_FireRate) + ) { + return; + } + + InternalShot(this, + curr_weapon->meta, + curr_weapon->GetUpgradeMeta(), + curr_weapon->bullet_meta, + curr_weapon->weapon_lv, + 0, + 5, + false); + + --curr_weapon->ammo; + int slot_id = curr_weapon->meta->i->_inventory_slot(); + switch (slot_id) { + case 5: + { + //手雷 + if (GetInventory(slot_id) > 0) { + DecInventory(slot_id, 1); + ++curr_weapon->ammo; + } else { + int weapon_idx = curr_weapon->weapon_idx; + *curr_weapon = Weapon(); + curr_weapon->weapon_idx = weapon_idx; + if (weapons[SMOKE_SLOT].weapon_id != 0) { + curr_weapon = &weapons[SMOKE_SLOT]; + } else { + curr_weapon = &weapons[0]; + } + AutoLoadingBullet(); + } + need_sync_active_player = true; + SyncAroundPlayers(__FILE__, __LINE__, __func__); + } + break; + case 6: + { + //烟雾弹 + if (GetInventory(slot_id) > 0) { + DecInventory(slot_id, 1); + ++curr_weapon->ammo; + } else { + int weapon_idx = curr_weapon->weapon_idx; + *curr_weapon = Weapon(); + curr_weapon->weapon_idx = weapon_idx; + if (weapons[FRAG_SLOT].weapon_id != 0) { + curr_weapon = &weapons[FRAG_SLOT]; + } else { + curr_weapon = &weapons[0]; + } + AutoLoadingBullet(); + } + need_sync_active_player = true; + SyncAroundPlayers(__FILE__, __LINE__, __func__); + } + break; + } + last_shot_frameno_ = room->GetFrameNo(); + if (!need_sync_active_player && IsPlayer()) { + room->frame_event.AddBulletNumChg((Human*)this); + } + shot_ok = true; +} + +void Creature::AutoLoadingBullet(bool manual) +{ + Weapon* p_weapon = curr_weapon; + if (car_weapon.meta) { + p_weapon = &car_weapon; + } + if (p_weapon->weapon_idx != 0 && + (p_weapon->ammo <= 0 || + (manual && p_weapon->ammo < p_weapon->GetClipVolume())) + ) { + MetaData::Equip* bullet_meta = MetaMgr::Instance()->GetEquip(p_weapon->meta->i->use_bullet()); + if (bullet_meta && + bullet_meta->i->_inventory_slot() >= 0 && + bullet_meta->i->_inventory_slot() < (int)inventory_.size() + ) { + if (GetInventory(bullet_meta->i->_inventory_slot()) > 0) { + if (on_loading_bullet) { + on_loading_bullet(); + } + StartAction(AT_Reload, + p_weapon->GetAttrValue(kHAT_ReloadTime), + p_weapon->weapon_id, + p_weapon->weapon_idx); + } + } + return; + } +} + +int Creature::GetInventory(int slot_id) +{ + if (!IsValidSlotId(slot_id)) { + abort(); + } + return inventory_[slot_id]; +} + +void Creature::AddInventory(int slot_id, int num) +{ + assert(num > 0); + if (!IsValidSlotId(slot_id)) { + abort(); + } + inventory_[slot_id] += num; +} + +void Creature::DecInventory(int slot_id, int num) +{ + assert(num > 0); + if (!IsValidSlotId(slot_id)) { + abort(); + } + inventory_[slot_id] -= num; +} diff --git a/server/gameserver/creature.h b/server/gameserver/creature.h index 11f0e63..829856d 100644 --- a/server/gameserver/creature.h +++ b/server/gameserver/creature.h @@ -19,7 +19,10 @@ class Creature : public MoveableEntity { public: + std::vector weapons; + long long last_shot_frameno_ = 0; int status = 0; + bool downed = false; bool dead = false; bool real_dead = false; int team_id = 0; @@ -35,6 +38,7 @@ class Creature : public MoveableEntity Weapon car_weapon; bool need_sync_active_player = false; + std::function on_loading_bullet; Creature(); virtual ~Creature() override; @@ -100,6 +104,14 @@ class Creature : public MoveableEntity void TouchProperTargets(std::function func); CreatureWeakPtrChunk* GetWeakPtrChunk() { return &weak_ptr_chunk_; }; + void Shot(a8::Vec2& target_dir, bool& shot_ok); + + void AutoLoadingBullet(bool manual = false); + int GetInventory(int slot_id); + void AddInventory(int slot_id, int num); + void DecInventory(int slot_id, int num); + std::array& GetInventoryData() { return inventory_; }; + private: virtual void AddBuffPostProc(Creature* caster, Buff* buff); @@ -141,7 +153,7 @@ private: a8::Vec2 skill_target_pos_; std::map skill_hash_; std::map passive_skill_hash_; - + std::array inventory_ = {}; friend class Skill; }; diff --git a/server/gameserver/hero.ai.cc b/server/gameserver/hero.ai.cc index efc7852..74de353 100644 --- a/server/gameserver/hero.ai.cc +++ b/server/gameserver/hero.ai.cc @@ -42,9 +42,7 @@ void HeroAI::Update(int delta_time) hero->poisoning_time += delta_time; } if (hero->poisoning) { - #if 0 hero->UpdatePoisoning(); - #endif } if (hero->dead) { return; @@ -299,9 +297,7 @@ void HeroAI::ChangeToStateAI(HeroState_e to_state) hero->move_dir = a8::Vec2(1.0f, 0); hero->move_dir.Rotate(a8::RandAngle()); hero->move_dir.Normalize(); - #if 0 hero->attack_dir = hero->move_dir; - #endif if (node_.param1 <= 1) { moving_ = false; } @@ -313,9 +309,7 @@ void HeroAI::ChangeToStateAI(HeroState_e to_state) if (node_.target.Get()) { hero->move_dir = node_.target.Get()->GetPos() - hero->GetPos(); hero->move_dir.Normalize(); - #if 0 hero->attack_dir = hero->move_dir; - #endif } } break; @@ -364,11 +358,9 @@ float HeroAI::GetAttackRange() { float attack_range = 0; Hero* myself = (Hero*)owner; - #if 0 if (myself->curr_weapon && myself->curr_weapon->meta) { attack_range = myself->curr_weapon->meta->i->range(); } - #endif attack_range = std::min(ai_meta->i->attack_range(), (int)attack_range); return attack_range; } @@ -401,9 +393,7 @@ void HeroAI::DoShotAI() } a8::Vec2 old_attack_dir = myself->attack_dir; myself->attack_dir = shot_dir; - #if 0 myself->Shot(shot_dir, shot_ok); - #endif myself->attack_dir = old_attack_dir; if (shot_ok) { if (node_.shot_times <= 0) { @@ -418,13 +408,9 @@ void HeroAI::DoShotAI() int HeroAI::GetAttackTimes() { Hero* myself = (Hero*)owner; - #if 0 if (myself->curr_weapon) { return std::min(ai_meta->i->attack_times(), myself->curr_weapon->GetClipVolume()); } else { return ai_meta->i->attack_times(); } - #endif - return 0; } - diff --git a/server/gameserver/human.cc b/server/gameserver/human.cc index c0d029e..de02edd 100644 --- a/server/gameserver/human.cc +++ b/server/gameserver/human.cc @@ -58,15 +58,6 @@ Human::Human():Creature() } weapons[0] = default_weapon; curr_weapon = &weapons[0]; - inventory_[IS_1XSCOPE] = 1; - - if (MetaMgr::Instance()->fighting_mode) { - inventory_[IS_9MM] = FIGHTING_MODE_BULLET_NUM; - inventory_[IS_556MM] = FIGHTING_MODE_BULLET_NUM; - inventory_[IS_762MM] = FIGHTING_MODE_BULLET_NUM; - inventory_[IS_12GAUGE] = FIGHTING_MODE_BULLET_NUM; - inventory_[IS_RPG] = FIGHTING_MODE_BULLET_NUM; - } } Human::~Human() @@ -417,89 +408,6 @@ void Human::FillMFTeamData(cs::MFTeamData* team_data, bool is_game_over) } } -void Human::Shot(a8::Vec2& target_dir, bool& shot_ok) -{ - shot_ok = false; - if (!curr_weapon->meta) { - return; - } - if (downed) { - return; - } - - if (curr_weapon->weapon_idx != 0 && - curr_weapon->ammo <= 0) { - AutoLoadingBullet(); - return; - } - if ((room->GetFrameNo() - last_shot_frameno_) * (1000 / SERVER_FRAME_RATE) < - curr_weapon->GetAttrValue(kHAT_FireRate) - ) { - return; - } - - InternalShot(this, - curr_weapon->meta, - curr_weapon->GetUpgradeMeta(), - curr_weapon->bullet_meta, - curr_weapon->weapon_lv, - 0, - 5, - false); - - --curr_weapon->ammo; - int slot_id = curr_weapon->meta->i->_inventory_slot(); - switch (slot_id) { - case 5: - { - //手雷 - if (GetInventory(slot_id) > 0) { - DecInventory(slot_id, 1); - ++curr_weapon->ammo; - } else { - int weapon_idx = curr_weapon->weapon_idx; - *curr_weapon = Weapon(); - curr_weapon->weapon_idx = weapon_idx; - if (weapons[SMOKE_SLOT].weapon_id != 0) { - curr_weapon = &weapons[SMOKE_SLOT]; - } else { - curr_weapon = &weapons[0]; - } - AutoLoadingBullet(); - } - need_sync_active_player = true; - SyncAroundPlayers(__FILE__, __LINE__, __func__); - } - break; - case 6: - { - //烟雾弹 - if (GetInventory(slot_id) > 0) { - DecInventory(slot_id, 1); - ++curr_weapon->ammo; - } else { - int weapon_idx = curr_weapon->weapon_idx; - *curr_weapon = Weapon(); - curr_weapon->weapon_idx = weapon_idx; - if (weapons[FRAG_SLOT].weapon_id != 0) { - curr_weapon = &weapons[FRAG_SLOT]; - } else { - curr_weapon = &weapons[0]; - } - AutoLoadingBullet(); - } - need_sync_active_player = true; - SyncAroundPlayers(__FILE__, __LINE__, __func__); - } - break; - } - last_shot_frameno_ = room->GetFrameNo(); - if (!need_sync_active_player) { - room->frame_event.AddBulletNumChg(this); - } - shot_ok = true; -} - void Human::CarShot(a8::Vec2& target_dir) { if (!car_weapon.meta) { @@ -741,35 +649,6 @@ float Human::GetMaxHP() return ability.max_hp; } -void Human::AutoLoadingBullet(bool manual) -{ - Weapon* p_weapon = curr_weapon; - if (car_weapon.meta) { - p_weapon = &car_weapon; - } - if (p_weapon->weapon_idx != 0 && - (p_weapon->ammo <= 0 || - (manual && p_weapon->ammo < p_weapon->GetClipVolume())) - ) { - MetaData::Equip* bullet_meta = MetaMgr::Instance()->GetEquip(p_weapon->meta->i->use_bullet()); - if (bullet_meta && - bullet_meta->i->_inventory_slot() >= 0 && - bullet_meta->i->_inventory_slot() < (int)inventory_.size() - ) { - if (GetInventory(bullet_meta->i->_inventory_slot()) > 0) { - if (on_loading_bullet) { - on_loading_bullet(); - } - StartAction(AT_Reload, - p_weapon->GetAttrValue(kHAT_ReloadTime), - p_weapon->weapon_id, - p_weapon->weapon_idx); - } - } - return; - } -} - void Human::FillSMGameOver(cs::SMGameOver& msg) { if (stats.rank <= 0) { @@ -1411,7 +1290,7 @@ void Human::FillMFActivePlayerData(cs::MFActivePlayerData* player_data) auto p = player_data->add_weapons(); weapon.ToPB(p); } - for (auto& num : inventory_) { + for (auto& num : GetInventoryData()) { player_data->add_inventory(num); } player_data->set_energy_shield(energy_shield); @@ -1482,32 +1361,6 @@ void Human::RecalcBaseAttr() ability.max_hp = std::max(ability.hp, ability.max_hp); } -int Human::GetInventory(int slot_id) -{ - if (!IsValidSlotId(slot_id)) { - abort(); - } - return inventory_[slot_id]; -} - -void Human::AddInventory(int slot_id, int num) -{ - assert(num > 0); - if (!IsValidSlotId(slot_id)) { - abort(); - } - inventory_[slot_id] += num; -} - -void Human::DecInventory(int slot_id, int num) -{ - assert(num > 0); - if (!IsValidSlotId(slot_id)) { - abort(); - } - inventory_[slot_id] -= num; -} - int Human::GetVolume(int slot_id) { if (!IsValidSlotId(slot_id)) { @@ -2483,8 +2336,8 @@ void Human::DeadDrop() backpack = 0; } } - for (size_t slot = 0; slot < inventory_.size(); ++slot) { - if (inventory_[slot] > 0) { + for (size_t slot = 0; slot < GetInventoryData().size(); ++slot) { + if (GetInventoryData()[slot] > 0) { MetaData::Equip* equip_meta = MetaMgr::Instance()->GetEquipBySlotId(slot); if (equip_meta) { if (equip_meta->i->equip_type() == EQUIP_TYPE_BULLET) { @@ -2493,7 +2346,7 @@ void Human::DeadDrop() case IS_SMOKE: { a8::Vec2 drop_pos = GetPos(); - room->DropItem(drop_pos, equip_meta->i->id(), inventory_[slot], 1); + room->DropItem(drop_pos, equip_meta->i->id(), GetInventoryData()[slot], 1); } break; default: @@ -2501,7 +2354,7 @@ void Human::DeadDrop() } } else { a8::Vec2 drop_pos = GetPos(); - room->DropItem(drop_pos, equip_meta->i->id(), inventory_[slot], 1); + room->DropItem(drop_pos, equip_meta->i->id(), GetInventoryData()[slot], 1); } } } diff --git a/server/gameserver/human.h b/server/gameserver/human.h index 579dd0d..6da8b38 100644 --- a/server/gameserver/human.h +++ b/server/gameserver/human.h @@ -50,9 +50,6 @@ class Human : public Creature MetaData::Equip* helmet_meta = nullptr; MetaData::Equip* chest_meta = nullptr; MetaData::Dress* skin_jlf_meta = nullptr; - long long last_shot_frameno_ = 0; - - std::function on_loading_bullet; std::string name; std::string avatar_url; @@ -65,7 +62,6 @@ class Human : public Creature int sex = 0; std::string user_data; long long last_cmmove_frameno = 0; - bool downed = false; bool disconnected = false; int anim_type = 0; int anim_seq = 0; @@ -91,7 +87,6 @@ class Human : public Creature long long real_dead_frameno = 0; Weapon default_weapon; - std::vector weapons; int curr_scope_idx = 0; @@ -148,7 +143,6 @@ class Human : public Creature void FillItemList(::google::protobuf::RepeatedPtrField<::cs::MFPair>* pb_item_list); long long GetRealDeadFrameNo(Room* room); void FillMFTeamData(cs::MFTeamData* team_data, bool is_game_over); - void Shot(a8::Vec2& target_dir, bool& shot_ok); void CarShot(a8::Vec2& target_dir); virtual void RecalcSelfCollider() override; bool IsCollisionInMapService(); @@ -156,7 +150,6 @@ class Human : public Creature float GetRadius(); float GetHP(); float GetMaxHP(); - void AutoLoadingBullet(bool manual = false); void BeKill(int killer_id, const std::string& killer_name, int weapon_id); virtual void DecHP(float dec_hp, int killer_id, const std::string& killer_name, int weapon_id) override; void AddToNewObjects(Entity* entity); @@ -185,9 +178,6 @@ class Human : public Creature void FillMFGasData(cs::MFGasData* gas_data); void RecalcVolume(); void RecalcBaseAttr(); - int GetInventory(int slot_id); - void AddInventory(int slot_id, int num); - void DecInventory(int slot_id, int num); int GetVolume(int slot_id); void RecoverHp(int inc_hp); void FillBodyState(::google::protobuf::RepeatedPtrField<::cs::MFBodyState>* states); @@ -300,7 +290,6 @@ protected: bool leave_ = false; long long leave_frameno_ = 0; - std::array inventory_ = {}; std::array volume_ = {}; std::set new_objects; std::set part_objects;