From edf298685e8b59cd7415fe8bb5ff23767a0c5d2f Mon Sep 17 00:00:00 2001 From: Kinglykrab <89047260+Kinglykrab@users.noreply.github.com> Date: Wed, 20 Oct 2021 15:59:28 -0400 Subject: [PATCH] [Quest API] Convert all char arrays to strings. (#1612) * [Quest API] Convert all char arrays to strings. Also change multiple loops for zone controller to one loop. * Remove 'this' keyword' --- zone/attack.cpp | 26 ++++++++++---------------- zone/client_packet.cpp | 7 +++---- zone/entity.cpp | 8 ++++---- zone/mob.cpp | 12 ++++-------- zone/mob_ai.cpp | 16 ++++++---------- zone/spells.cpp | 30 ++++++++++++------------------ zone/waypoints.cpp | 5 ++--- 7 files changed, 41 insertions(+), 63 deletions(-) diff --git a/zone/attack.cpp b/zone/attack.cpp index 39c613cc6..9b835afb3 100644 --- a/zone/attack.cpp +++ b/zone/attack.cpp @@ -2211,11 +2211,8 @@ bool NPC::Death(Mob* killer_mob, int32 damage, uint16 spell, EQ::skills::SkillTy Mob *oos = nullptr; if (killer_mob) { oos = killer_mob->GetOwnerOrSelf(); - - char buffer[48] = { 0 }; - snprintf(buffer, 47, "%d %d %d %d", killer_mob->GetID(), damage, spell, static_cast(attack_skill)); - - if (parse->EventNPC(EVENT_DEATH, this, oos, buffer, 0) != 0) { + std::string buffer = fmt::format("{} {} {} {}", killer_mob->GetID(), damage, spell, static_cast(attack_skill)); + if (parse->EventNPC(EVENT_DEATH, this, oos, buffer.c_str(), 0) != 0) { if (GetHP() < 0) { SetHP(0); } @@ -2238,10 +2235,8 @@ bool NPC::Death(Mob* killer_mob, int32 damage, uint16 spell, EQ::skills::SkillTy } } else { - char buffer[48] = { 0 }; - snprintf(buffer, 47, "%d %d %d %d", 0, damage, spell, static_cast(attack_skill)); - - if (parse->EventNPC(EVENT_DEATH, this, nullptr, buffer, 0) != 0) { + std::string buffer = fmt::format("{} {} {} {}", 0, damage, spell, static_cast(attack_skill)); + if (parse->EventNPC(EVENT_DEATH, this, nullptr, buffer.c_str(), 0) != 0) { if (GetHP() < 0) { SetHP(0); } @@ -2632,16 +2627,15 @@ bool NPC::Death(Mob* killer_mob, int32 damage, uint16 spell, EQ::skills::SkillTy entity_list.UpdateFindableNPCState(this, true); - char buffer[48] = { 0 }; - snprintf(buffer, 47, "%d %d %d %d", killer_mob ? killer_mob->GetID() : 0, damage, spell, static_cast(attack_skill)); - parse->EventNPC(EVENT_DEATH_COMPLETE, this, oos, buffer, 0); + std::string buffer = fmt::format("{} {} {} {}", killer_mob ? killer_mob->GetID() : 0, damage, spell, static_cast(attack_skill)); + parse->EventNPC(EVENT_DEATH_COMPLETE, this, oos, buffer.c_str(), 0); /* Zone controller process EVENT_DEATH_ZONE (Death events) */ if (RuleB(Zone, UseZoneController)) { - if (entity_list.GetNPCByNPCTypeID(ZONE_CONTROLLER_NPC_ID) && this->GetNPCTypeID() != ZONE_CONTROLLER_NPC_ID) { - char data_pass[100] = { 0 }; - snprintf(data_pass, 99, "%d %d %d %d %d", killer_mob ? killer_mob->GetID() : 0, damage, spell, static_cast(attack_skill), this->GetNPCTypeID()); - parse->EventNPC(EVENT_DEATH_ZONE, entity_list.GetNPCByNPCTypeID(ZONE_CONTROLLER_NPC_ID)->CastToNPC(), nullptr, data_pass, 0); + auto controller = entity_list.GetNPCByNPCTypeID(ZONE_CONTROLLER_NPC_ID); + if (controller && GetNPCTypeID() != ZONE_CONTROLLER_NPC_ID) { + std::string data_pass = fmt::format("{} {} {} {} {}", killer_mob ? killer_mob->GetID() : 0, damage, spell, static_cast(attack_skill), GetNPCTypeID()); + parse->EventNPC(EVENT_DEATH_ZONE, controller, nullptr, data_pass.c_str(), 0); } } diff --git a/zone/client_packet.cpp b/zone/client_packet.cpp index f1c47a4d1..8fec728e8 100644 --- a/zone/client_packet.cpp +++ b/zone/client_packet.cpp @@ -11164,14 +11164,13 @@ void Client::Handle_OP_PopupResponse(const EQApplicationPacket *app) break; } - char buf[16]; - sprintf(buf, "%d", popup_response->popupid); + std::string buf = fmt::format("{}", popup_response->popupid); - parse->EventPlayer(EVENT_POPUP_RESPONSE, this, buf, 0); + parse->EventPlayer(EVENT_POPUP_RESPONSE, this, buf.c_str(), 0); Mob *Target = GetTarget(); if (Target && Target->IsNPC()) { - parse->EventNPC(EVENT_POPUP_RESPONSE, Target->CastToNPC(), this, buf, 0); + parse->EventNPC(EVENT_POPUP_RESPONSE, Target->CastToNPC(), this, buf.c_str(), 0); } } diff --git a/zone/entity.cpp b/zone/entity.cpp index c80d13ba8..8a15d10a4 100644 --- a/zone/entity.cpp +++ b/zone/entity.cpp @@ -723,10 +723,10 @@ void EntityList::AddNPC(NPC *npc, bool SendSpawnPacket, bool dontqueue) /* Zone controller process EVENT_SPAWN_ZONE */ if (RuleB(Zone, UseZoneController)) { - if (entity_list.GetNPCByNPCTypeID(ZONE_CONTROLLER_NPC_ID) && npc->GetNPCTypeID() != ZONE_CONTROLLER_NPC_ID){ - char data_pass[100] = { 0 }; - snprintf(data_pass, 99, "%d %d", npc->GetID(), npc->GetNPCTypeID()); - parse->EventNPC(EVENT_SPAWN_ZONE, entity_list.GetNPCByNPCTypeID(ZONE_CONTROLLER_NPC_ID)->CastToNPC(), nullptr, data_pass, 0); + auto controller = entity_list.GetNPCByNPCTypeID(ZONE_CONTROLLER_NPC_ID); + if (controller && npc->GetNPCTypeID() != ZONE_CONTROLLER_NPC_ID){ + std::string data_pass = fmt::format("{} {}", npc->GetID(), npc->GetNPCTypeID()); + parse->EventNPC(EVENT_SPAWN_ZONE, controller, nullptr, data_pass.c_str(), 0); } } diff --git a/zone/mob.cpp b/zone/mob.cpp index f58b6a92a..c5d8d24c0 100644 --- a/zone/mob.cpp +++ b/zone/mob.cpp @@ -1334,11 +1334,9 @@ void Mob::CreateHPPacket(EQApplicationPacket* app) { if (ds->hp < GetNextHPEvent()) { - char buf[10]; - snprintf(buf, 9, "%i", GetNextHPEvent()); - buf[9] = '\0'; + std::string buf = fmt::format("{}", GetNextHPEvent()); SetNextHPEvent(-1); - parse->EventNPC(EVENT_HP, CastToNPC(), nullptr, buf, 0); + parse->EventNPC(EVENT_HP, CastToNPC(), nullptr, buf.c_str(), 0); } } @@ -1346,11 +1344,9 @@ void Mob::CreateHPPacket(EQApplicationPacket* app) { if (ds->hp > GetNextIncHPEvent()) { - char buf[10]; - snprintf(buf, 9, "%i", GetNextIncHPEvent()); - buf[9] = '\0'; + std::string buf = fmt::format("{}", GetNextIncHPEvent()); SetNextIncHPEvent(-1); - parse->EventNPC(EVENT_HP, CastToNPC(), nullptr, buf, 1); + parse->EventNPC(EVENT_HP, CastToNPC(), nullptr, buf.c_str(), 1); } } } diff --git a/zone/mob_ai.cpp b/zone/mob_ai.cpp index 962d63f3d..af74f3510 100644 --- a/zone/mob_ai.cpp +++ b/zone/mob_ai.cpp @@ -1766,9 +1766,8 @@ void NPC::AI_DoMovement() { } //kick off event_waypoint arrive - char temp[16]; - sprintf(temp, "%d", cur_wp); - parse->EventNPC(EVENT_WAYPOINT_ARRIVE, CastToNPC(), nullptr, temp, 0); + std::string buf = fmt::format("{}", cur_wp); + parse->EventNPC(EVENT_WAYPOINT_ARRIVE, CastToNPC(), nullptr, buf.c_str(), 0); // No need to move as we are there. Next loop will // take care of normal grids, even at pause 0. // We do need to call and setup a wp if we're cur_wp=-2 @@ -1885,9 +1884,8 @@ void NPC::AI_SetupNextWaypoint() { if (!DistractedFromGrid) { //kick off event_waypoint depart - char temp[16]; - sprintf(temp, "%d", cur_wp); - parse->EventNPC(EVENT_WAYPOINT_DEPART, CastToNPC(), nullptr, temp, 0); + std::string buf = fmt::format("{}", cur_wp); + parse->EventNPC(EVENT_WAYPOINT_DEPART, CastToNPC(), nullptr, buf.c_str(), 0); //setup our next waypoint, if we are still on our normal grid //remember that the quest event above could have done anything it wanted with our grid @@ -2470,10 +2468,8 @@ void NPC::CheckSignal() { if (!signal_q.empty()) { int signal_id = signal_q.front(); signal_q.pop_front(); - char buf[32]; - snprintf(buf, 31, "%d", signal_id); - buf[31] = '\0'; - parse->EventNPC(EVENT_SIGNAL, this, nullptr, buf, 0); + std::string buf = fmt::format("{}", signal_id); + parse->EventNPC(EVENT_SIGNAL, this, nullptr, buf.c_str(), 0); } } diff --git a/zone/spells.cpp b/zone/spells.cpp index 36202f12c..dd7442911 100644 --- a/zone/spells.cpp +++ b/zone/spells.cpp @@ -289,14 +289,12 @@ bool Mob::CastSpell(uint16 spell_id, uint16 target_id, CastingSlot slot, } if(IsClient()) { - char temp[64]; - sprintf(temp, "%d", spell_id); - if (parse->EventPlayer(EVENT_CAST_BEGIN, CastToClient(), temp, 0) != 0) + std::string buf = fmt::format("{}", spell_id); + if (parse->EventPlayer(EVENT_CAST_BEGIN, CastToClient(), buf.c_str(), 0) != 0) return false; } else if(IsNPC()) { - char temp[64]; - sprintf(temp, "%d", spell_id); - parse->EventNPC(EVENT_CAST_BEGIN, CastToNPC(), nullptr, temp, 0); + std::string buf = fmt::format("{}", spell_id); + parse->EventNPC(EVENT_CAST_BEGIN, CastToNPC(), nullptr, buf.c_str(), 0); } //To prevent NPC ghosting when spells are cast from scripts @@ -1440,13 +1438,11 @@ void Mob::CastedSpellFinished(uint16 spell_id, uint32 target_id, CastingSlot slo // if(IsClient()) { - char temp[64]; - sprintf(temp, "%d", spell_id); - parse->EventPlayer(EVENT_CAST, CastToClient(), temp, 0); + std::string buf = fmt::format("{}", spell_id); + parse->EventPlayer(EVENT_CAST, CastToClient(), buf.c_str(), 0); } else if(IsNPC()) { - char temp[64]; - sprintf(temp, "%d", spell_id); - parse->EventNPC(EVENT_CAST, CastToNPC(), nullptr, temp, 0); + std::string buf = fmt::format("{}", spell_id); + parse->EventNPC(EVENT_CAST, CastToNPC(), nullptr, buf.c_str(), 0); } if(bard_song_mode) @@ -3654,15 +3650,13 @@ bool Mob::SpellOnTarget(uint16 spell_id, Mob *spelltar, int reflect_effectivenes /* Send the EVENT_CAST_ON event */ if(spelltar->IsNPC()) { - char temp1[100]; - sprintf(temp1, "%d", spell_id); - parse->EventNPC(EVENT_CAST_ON, spelltar->CastToNPC(), this, temp1, 0); + std::string buf = fmt::format("{}", spell_id); + parse->EventNPC(EVENT_CAST_ON, spelltar->CastToNPC(), this, buf.c_str(), 0); } else if (spelltar->IsClient()) { - char temp1[100]; - sprintf(temp1, "%d", spell_id); - parse->EventPlayer(EVENT_CAST_ON, spelltar->CastToClient(),temp1, 0); + std::string buf = fmt::format("{}", spell_id); + parse->EventPlayer(EVENT_CAST_ON, spelltar->CastToClient(), buf.c_str(), 0); } mod_spell_cast(spell_id, spelltar, reflect_effectiveness, use_resist_adjust, resist_adjust, isproc); diff --git a/zone/waypoints.cpp b/zone/waypoints.cpp index 97e9d6091..72bdf14d5 100644 --- a/zone/waypoints.cpp +++ b/zone/waypoints.cpp @@ -138,11 +138,10 @@ void NPC::ResumeWandering() if (m_CurrentWayPoint.x == GetX() && m_CurrentWayPoint.y == GetY()) { // are we we at a waypoint? if so, trigger event and start to next - char temp[100]; - itoa(cur_wp, temp, 10); //do this before updating to next waypoint + std::string buf = fmt::format("{}", cur_wp); CalculateNewWaypoint(); SetAppearance(eaStanding, false); - parse->EventNPC(EVENT_WAYPOINT_DEPART, this, nullptr, temp, 0); + parse->EventNPC(EVENT_WAYPOINT_DEPART, this, nullptr, buf.c_str(), 0); } // if not currently at a waypoint, we continue on to the one we were headed to before the stop } else