From b3fa342e6260bb4500e0b86987f7f3621feeabf7 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 13 May 2021 15:53:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20.=E5=8A=A0=E8=A1=80?= =?UTF-8?q?=E9=81=93=E5=85=B7=EF=BC=8C=E5=AD=98=E5=9C=A8=E5=BB=B6=E8=BF=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/gameserver/human.cc | 102 +++++++++++++++++++------------------ server/gameserver/human.h | 1 + 2 files changed, 54 insertions(+), 49 deletions(-) diff --git a/server/gameserver/human.cc b/server/gameserver/human.cc index 75b4ed9..2c75836 100644 --- a/server/gameserver/human.cc +++ b/server/gameserver/human.cc @@ -3152,64 +3152,59 @@ void Human::ProcReloadAction() void Human::ProcUseItemAction() { + MetaData::Equip* item_meta = MetaMgr::Instance()->GetEquipBySlotId(action_item_id); + if (!item_meta) { + return; + } + if (GetInventory(item_meta->i->_inventory_slot()) <= 0) { + return; + } switch (action_item_id) { case IS_HEALTHKIT: { - MetaData::Equip* item_meta = MetaMgr::Instance()->GetEquipBySlotId(action_item_id); - if (item_meta){ - if (GetInventory(item_meta->i->_inventory_slot()) > 0) { - float old_health = GetHP(); - ability.hp += item_meta->i->heal(); - ability.hp = std::min(GetHP(), GetMaxHP()); - stats.heal_amount += GetHP() - old_health; - DecInventory(item_meta->i->_inventory_slot(), 1); - need_sync_active_player = true; - SyncAroundPlayers(__FILE__, __LINE__, __func__); - } - } + AddHp(item_meta->i->heal()); + DecInventory(item_meta->i->_inventory_slot(), 1); } break; case IS_PAIN_KILLER: { - MetaData::Equip* item_meta = MetaMgr::Instance()->GetEquipBySlotId(action_item_id); - if (item_meta){ - if (GetInventory(item_meta->i->_inventory_slot()) > 0) { - if (pain_killer_timer) { - int passed_time = (room->GetFrameNo() - pain_killer_frameno) * FRAME_RATE_MS; - int left_time = std::max(0, pain_killer_lastingtime * 1000 - passed_time); - int anodyne_max_time = MetaMgr::Instance()->GetSysParamAsInt("anodyne_max_time"); - left_time = std::min(left_time, anodyne_max_time * 1000); - pain_killer_lastingtime += std::min(item_meta->i->time() * 1000, anodyne_max_time * 1000 - left_time) / 1000; - need_sync_active_player = true; - } else { - pain_killer_frameno = room->GetFrameNo(); - pain_killer_lastingtime = item_meta->i->time(); - pain_killer_timer = room->xtimer.AddRepeatTimerAndAttach - ( - SERVER_FRAME_RATE, - a8::XParams() - .SetSender(this) - .SetParam1(item_meta->i->heal()), - [] (const a8::XParams& param) - { - Human* hum = (Human*)param.sender.GetUserData(); - float old_health = hum->GetHP(); - hum->ability.hp += param.param1.GetDouble(); - hum->ability.hp = std::min(hum->GetHP(), hum->GetMaxHP()); - hum->stats.heal_amount += hum->GetHP() - old_health; - hum->SyncAroundPlayers(__FILE__, __LINE__, __func__); - if (hum->room->GetFrameNo() - hum->pain_killer_frameno > hum->pain_killer_lastingtime * SERVER_FRAME_RATE) { - hum->room->xtimer.DeleteTimer(hum->pain_killer_timer); - hum->pain_killer_timer = nullptr; - } - }, - &xtimer_attacher.timer_list_ - ); - } - DecInventory(item_meta->i->_inventory_slot(), 1); - need_sync_active_player = true; + if (pain_killer_timer) { + int passed_time = (room->GetFrameNo() - pain_killer_frameno) * FRAME_RATE_MS; + int left_time = std::max(0, pain_killer_lastingtime * 1000 - passed_time); + int anodyne_max_time = MetaMgr::Instance()->GetSysParamAsInt("anodyne_max_time"); + left_time = std::min(left_time, anodyne_max_time * 1000); + pain_killer_lastingtime += std::min(item_meta->i->time() * 1000, + anodyne_max_time * 1000 - left_time) / 1000; + } else { + pain_killer_frameno = room->GetFrameNo(); + pain_killer_lastingtime = item_meta->i->time(); + if (pain_killer_lastingtime > 0) { + pain_killer_timer = room->xtimer.AddRepeatTimerAndAttach + ( + SERVER_FRAME_RATE, + a8::XParams() + .SetSender(this) + .SetParam1(item_meta->i->heal()), + [] (const a8::XParams& param) + { + Human* hum = (Human*)param.sender.GetUserData(); + hum->AddHp(param.param1.GetDouble()); + if (hum->room->GetFrameNo() - hum->pain_killer_frameno > + hum->pain_killer_lastingtime * SERVER_FRAME_RATE) { + hum->room->xtimer.DeleteTimer(hum->pain_killer_timer); + } + }, + &xtimer_attacher.timer_list_, + [] (const a8::XParams& param) + { + Human* hum = (Human*)param.sender.GetUserData(); + hum->pain_killer_timer = nullptr; + }); } + AddHp(item_meta->i->heal()); } + DecInventory(item_meta->i->_inventory_slot(), 1); + need_sync_active_player = true; } break; default: @@ -3653,3 +3648,12 @@ void Human::GMAddItem(int item_id, int item_num) need_sync_active_player = true; SyncAroundPlayers(__FILE__, __LINE__, __func__); } + +void Human::AddHp(float hp) +{ + float old_health = GetHP(); + ability.hp += hp; + ability.hp = std::min(GetHP(), GetMaxHP()); + stats.heal_amount += GetHP() - old_health; + SyncAroundPlayers(__FILE__, __LINE__, __func__); +} diff --git a/server/gameserver/human.h b/server/gameserver/human.h index 06f8610..3cd09c4 100644 --- a/server/gameserver/human.h +++ b/server/gameserver/human.h @@ -246,6 +246,7 @@ class Human : public Creature virtual std::string GetName() override { return name;}; void UpdateViewObjects(); void GMAddItem(int item_id, int item_num); + void AddHp(float hp); protected: void _InternalUpdateMove(float speed);