From 29cec3a0bd9ba896ada1d34f0f114737aaf251c6 Mon Sep 17 00:00:00 2001 From: Grinch_ Date: Mon, 1 Aug 2022 07:03:25 +0600 Subject: [PATCH] Refactor teleport class --- src/cheatmenu.cpp | 2 +- src/teleport.cpp | 204 +++++++++++++++++++--------------------------- src/teleport.h | 30 ++++--- src/vehicle.cpp | 5 +- 4 files changed, 102 insertions(+), 139 deletions(-) diff --git a/src/cheatmenu.cpp b/src/cheatmenu.cpp index a344718..3ecbf8e 100644 --- a/src/cheatmenu.cpp +++ b/src/cheatmenu.cpp @@ -289,7 +289,7 @@ void CheatMenu::Init() D3dHook::SetMouseState(m_bShowMenu); } - if (Teleport::m_bQuickTeleport && quickTeleport.PressedRealtime()) + if (Teleport::IsQuickTeleportActive() && quickTeleport.PressedRealtime()) { D3dHook::SetMouseState(true); } diff --git a/src/teleport.cpp b/src/teleport.cpp index 4655bf4..83528ec 100644 --- a/src/teleport.cpp +++ b/src/teleport.cpp @@ -34,6 +34,11 @@ void Teleport::FetchRadarSpriteData() } #endif +bool Teleport::IsQuickTeleportActive() +{ + return m_bQuickTeleport; +} + void Teleport::Init() { m_bTeleportMarker = gConfig.Get("Features.TeleportMarker", false); @@ -41,52 +46,14 @@ void Teleport::Init() m_fMapSize.x = gConfig.Get("Game.MapSizeX", 6000.0f); m_fMapSize.y = gConfig.Get("Game.MapSizeY", 6000.0f); - Events::processScriptsEvent += [] + Events::drawingEvent += [] { - if ((TPMarker::m_bEnabled == true) && ((CTimer::m_snTimeInMilliseconds - TPMarker::m_nTimer) > 50)) - { - CPlayerPed* player = FindPlayerPed(); - -#ifdef GTASA - CEntity* player_entity = FindPlayerEntity(-1); - TPMarker::m_fPos.z = CWorld::FindGroundZFor3DCoord(TPMarker::m_fPos.x, TPMarker::m_fPos.y, - TPMarker::m_fPos.z + 100.0f, nullptr, &player_entity) + 1.0f; -#else - TPMarker::m_fPos.z = CWorld::FindGroundZFor3DCoord(TPMarker::m_fPos.x, TPMarker::m_fPos.y, - TPMarker::m_fPos.z + 100.0f, nullptr) + 1.0f; -#endif - CVehicle* pVeh = player->m_pVehicle; - - if (pVeh && BY_GAME(player->m_nPedFlags.bInVehicle, player->m_pVehicle, player->m_pVehicle)) - { - BY_GAME(pVeh->Teleport(TPMarker::m_fPos, false), pVeh->Teleport(TPMarker::m_fPos), player->Teleport(TPMarker::m_fPos)); - } - else - { - BY_GAME(player->Teleport(TPMarker::m_fPos, false), player->Teleport(TPMarker::m_fPos), player->Teleport(TPMarker::m_fPos)); - } -#ifdef GTASA - if (TPMarker::m_bJetpack) - { - Command(CPools::GetPedRef(player)); - } -#endif - - TPMarker::m_bEnabled = false; - Command(CPools::GetPedRef(player), false); - Command(); - TheCamera.Fade(0, 1); - } - if (m_bTeleportMarker && teleportMarker.Pressed()) { WarpPlayer(); } - }; #ifdef GTASA - Events::drawingEvent += [] - { if (m_bQuickTeleport && quickTeleport.PressedRealtime()) { static CSprite2d map; @@ -120,56 +87,42 @@ void Teleport::Init() } } } - }; #endif + }; } +#ifdef GTASA template void Teleport::WarpPlayer(CVector pos, int interiorID) { CPlayerPed* pPlayer = FindPlayerPed(); CVehicle* pVeh = pPlayer->m_pVehicle; int hplayer = CPools::GetPedRef(pPlayer); + bool jetpack = Command(0); -#ifdef GTASA - TPMarker::m_bJetpack = Command(0); - if (Type == eTeleportType::Marker || Type == eTeleportType::MapPosition) - { - if (Type == eTeleportType::Marker) + if (Type == eTeleportType::Marker) + { + tRadarTrace targetBlip = CRadar::ms_RadarTrace[LOWORD(FrontEndMenuManager.m_nTargetBlipIndex)]; + if (targetBlip.m_nRadarSprite != RADAR_SPRITE_WAYPOINT) { - tRadarTrace targetBlip = CRadar::ms_RadarTrace[LOWORD(FrontEndMenuManager.m_nTargetBlipIndex)]; - if (targetBlip.m_nRadarSprite != RADAR_SPRITE_WAYPOINT) - { - Util::SetMessage(TEXT("Teleport.TargetBlipText")); - return; - } - pos = targetBlip.m_vecPos; - } - - CEntity* pPlayerEntity = FindPlayerEntity(-1); - pos.z = CWorld::FindGroundZFor3DCoord(pos.x, pos.y, 1000, nullptr, &pPlayerEntity) + 500.f; + Util::SetMessage(TEXT("Teleport.TargetBlipText")); + return; + } + pos = targetBlip.m_vecPos; + } - TPMarker::m_fPos = pos; - TPMarker::m_nTimer = CTimer::m_snTimeInMilliseconds; - TPMarker::m_bEnabled = true; - TheCamera.Fade(0, 0); - Command(CPools::GetPedRef(pPlayer), true); - } -#endif - -#ifdef GTA3 - CStreaming::LoadScene(pos); -#else CStreaming::LoadScene(&pos); CStreaming::LoadSceneCollision(&pos); -#endif CStreaming::LoadAllRequestedModels(false); -#ifdef GTASA + if (Type == eTeleportType::Marker || Type == eTeleportType::MapPosition) + { + CEntity* pPlayerEntity = FindPlayerEntity(-1); + pos.z = CWorld::FindGroundZFor3DCoord(pos.x, pos.y, 1000, nullptr, &pPlayerEntity) + 1.0f; + } if (pVeh && pPlayer->m_nPedFlags.bInVehicle) { - if (CModelInfo::IsTrainModel(pVeh->m_nModelIndex)) { CVector vehPos = pVeh->GetPosition(); @@ -203,15 +156,33 @@ void Teleport::WarpPlayer(CVector pos, int interiorID) pPlayer->Teleport(pos, false); } - if (TPMarker::m_bJetpack) + if (jetpack) { Command(hplayer); } + + pPlayer->m_nAreaCode = interiorID; + Command(interiorID); +} #else +template +void Teleport::WarpPlayer(CVector pos, int interiorID) +{ + CPlayerPed* pPlayer = FindPlayerPed(); + CVehicle* pVeh = pPlayer->m_pVehicle; + +#ifdef GTAVC + CStreaming::LoadScene(&pos); + CStreaming::LoadSceneCollision(&pos); +#else + CStreaming::LoadScene(pos); +#endif + CStreaming::LoadAllRequestedModels(false); + if (pVeh && pPlayer->m_pVehicle) { #ifdef GTAVC - pPlayer->m_nAreaCode = interiorID; + pVeh->m_nAreaCode = interiorID; #endif pVeh->Teleport(pos); } @@ -219,45 +190,17 @@ void Teleport::WarpPlayer(CVector pos, int interiorID) { pPlayer->Teleport(pos); } -#endif -#if defined GTASA || defined GTAVC +#ifdef GTAVC pPlayer->m_nAreaCode = interiorID; Command(interiorID); #endif } - -void Teleport::TeleportToLocation(std::string& rootkey, std::string& bLocName, std::string& loc) -{ - try - { - int dimension = 0; - CVector pos; - sscanf(loc.c_str(), "%d,%f,%f,%f", &dimension, &pos.x, &pos.y, &pos.z); - WarpPlayer(pos, dimension); - } - catch (...) - { - Util::SetMessage(TEXT("Teleport.InvalidLocation")); - } -} - -void Teleport::RemoveTeleportEntry(std::string& category, std::string& key, std::string& val) -{ - if (category == "Custom") - { - m_locData.m_pData->RemoveKey("Custom", key.c_str()); - Util::SetMessage(TEXT("Teleport.LocationRemoved")); - m_locData.m_pData->Save(); - } - else - { - Util::SetMessage(TEXT("Teleport.CustomLocationRemoveOnly")); - } -} +#endif void Teleport::ShowPage() { + static char locBuf[INPUT_BUFFER_SIZE], inBuf[INPUT_BUFFER_SIZE]; if (ImGui::BeginTabBar("Teleport", ImGuiTabBarFlags_NoTooltip + ImGuiTabBarFlags_FittingPolicyScroll)) { ImGui::Spacing(); @@ -266,52 +209,53 @@ void Teleport::ShowPage() ImGui::Spacing(); if (ImGui::BeginChild("Teleport Child")) { +#ifdef GTASA ImGui::Columns(2, nullptr, false); ImGui::Checkbox(TEXT("Teleport.InsertCoord"), &m_bInsertCoord); -#ifdef GTASA + if (Widget::Checkbox(TEXT("Teleport.QuickTeleport"), &m_bQuickTeleport, std::string(TEXT_S("Teleport.QuickTeleportHint") + quickTeleport.GetNameString()).c_str())) { gConfig.Set("Features.QuickTeleport", m_bQuickTeleport); } -#endif ImGui::NextColumn(); -#ifdef GTASA if (Widget::Checkbox(TEXT("Teleport.TeleportMarker"), &m_bTeleportMarker, std::string(TEXT_S("Teleport.TeleportMarkerHint") + teleportMarker.GetNameString()).c_str())) { gConfig.Set("Features.TeleportMarker", m_bTeleportMarker); } -#endif ImGui::Columns(1); +#else + ImGui::Spacing(); + ImGui::Sameline(); + ImGui::Checkbox(TEXT("Teleport.InsertCoord"), &m_bInsertCoord); +#endif ImGui::Spacing(); if (m_bInsertCoord) { CVector pos = FindPlayerPed()->GetPosition(); - strcpy(m_nInputBuffer, + strcpy(inBuf, (std::to_string(static_cast(pos.x)) + ", " + std::to_string(static_cast(pos.y)) + ", " + std::to_string(static_cast(pos.z))).c_str()); } - ImGui::InputTextWithHint(TEXT("Teleport.Coordinates"), "x, y, z", m_nInputBuffer, IM_ARRAYSIZE(m_nInputBuffer)); + ImGui::InputTextWithHint(TEXT("Teleport.Coordinates"), "x, y, z", inBuf, INPUT_BUFFER_SIZE); ImGui::Spacing(); if (ImGui::Button(TEXT("Teleport.TeleportToCoord"), Widget::CalcSize(2))) { CVector pos{0, 0, 10}; - - try + if (sscanf(inBuf,"%f,%f,%f", &pos.x, &pos.y, &pos.z) == 3) { - sscanf(m_nInputBuffer,"%f,%f,%f", &pos.x, &pos.y, &pos.z); pos.z += 1.0f; WarpPlayer(pos); } - catch (...) + else { Util::SetMessage(TEXT("Teleport.InvalidCoord")); } @@ -369,13 +313,13 @@ void Teleport::ShowPage() if (ImGui::CollapsingHeader(TEXT("Window.AddNew"))) { ImGui::Spacing(); - ImGui::InputTextWithHint(TEXT("Teleport.Location"), TEXT("Teleport.LocationHint"), m_nLocationBuffer, IM_ARRAYSIZE(m_nInputBuffer)); - ImGui::InputTextWithHint(TEXT("Teleport.Coordinates"), "x, y, z", m_nInputBuffer, IM_ARRAYSIZE(m_nInputBuffer)); + ImGui::InputTextWithHint(TEXT("Teleport.Location"), TEXT("Teleport.LocationHint"), locBuf, INPUT_BUFFER_SIZE); + ImGui::InputTextWithHint(TEXT("Teleport.Coordinates"), "x, y, z", inBuf, INPUT_BUFFER_SIZE); ImGui::Spacing(); if (ImGui::Button(TEXT("Teleport.AddLocation"), Widget::CalcSize())) { - std::string key = std::string("Custom.") + m_nLocationBuffer; - m_locData.m_pData->Set(key.c_str(), ("0, " + std::string(m_nInputBuffer))); + std::string key = std::string("Custom.") + locBuf; + m_locData.m_pData->Set(key.c_str(), ("0, " + std::string(inBuf))); #ifdef GTASA // Clear the Radar coordinates @@ -387,7 +331,31 @@ void Teleport::ShowPage() } ImGui::Spacing(); - Widget::DataList(m_locData, TeleportToLocation, RemoveTeleportEntry); + Widget::DataList(m_locData, + [](std::string& unk1, std::string& unk2, std::string& loc){ + int dim = 0; + CVector pos; + if (sscanf(loc.c_str(), "%d,%f,%f,%f", &dim, &pos.x, &pos.y, &pos.z) == 4) + { + WarpPlayer(pos, dim); + } + else + { + Util::SetMessage(TEXT("Teleport.InvalidLocation")); + } + }, + [](std::string& category, std::string& key, std::string& _){ + if (category == "Custom") + { + m_locData.m_pData->RemoveKey("Custom", key.c_str()); + Util::SetMessage(TEXT("Teleport.LocationRemoved")); + m_locData.m_pData->Save(); + } + else + { + Util::SetMessage(TEXT("Teleport.CustomLocationRemoveOnly")); + } + }); ImGui::EndTabItem(); } ImGui::EndTabBar(); diff --git a/src/teleport.h b/src/teleport.h index a2e8e8d..24fcd18 100644 --- a/src/teleport.h +++ b/src/teleport.h @@ -8,23 +8,17 @@ enum class eTeleportType Coordinate, }; +/* + Teleport Class + Contains code for the Teleport menu page +*/ class Teleport { private: static inline bool m_bInsertCoord; static inline bool m_bTeleportMarker; - static inline char m_nInputBuffer[INPUT_BUFFER_SIZE]; - static inline char m_nLocationBuffer[INPUT_BUFFER_SIZE]; + static inline bool m_bQuickTeleport; static inline ImVec2 m_fMapSize; - - struct TPMarker - { - static inline bool m_bEnabled; - static inline CVector m_fPos = { -1, -1, -1 }; - static inline uint m_nTimer; - static inline bool m_bJetpack; - }; - #ifdef GTASA static inline DataStore m_SpriteData {"sprites"}; @@ -35,17 +29,21 @@ private: static void FetchRadarSpriteData(); #endif - static void TeleportToLocation(std::string& rootkey, std::string& bLocName, std::string& loc); - static void RemoveTeleportEntry(std::string& rootkey, std::string& key, std::string& val); - public: + static inline ResourceStore m_locData{"locations", eResourceType::TYPE_TEXT}; + Teleport() = delete; Teleport(const Teleport&) = delete; - static inline ResourceStore m_locData{ "locations", eResourceType::TYPE_TEXT }; - static inline bool m_bQuickTeleport; static void Init(); + + // Warp player to position, marker, map etc template static void WarpPlayer(CVector pos = CVector(0, 0, 0), int interiorID = 0); + + // Returns true if quick teleport feature is active + static bool IsQuickTeleportActive(); + + // Draws the teleport menu page static void ShowPage(); }; diff --git a/src/vehicle.cpp b/src/vehicle.cpp index f8aa03a..dc50021 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1065,12 +1065,9 @@ void Vehicle::ShowPage() { Widget::Checkbox(TEXT("Vehicle.LockSpeed"), &m_bLockSpeed); ImGui::Spacing(); - ImGui::InputFloat(TEXT("Vehicle.Set"), &m_fLockSpeed); + Widget::InputFloat(TEXT("Vehicle.Set"), &m_fLockSpeed, 1.0f, 0.0f, 100.0f); ImGui::Spacing(); - m_fLockSpeed = m_fLockSpeed > 100 ? 100 : m_fLockSpeed; - m_fLockSpeed = m_fLockSpeed < 0 ? 0 : m_fLockSpeed; - if (ImGui::Button(TEXT("Vehicle.Set"), ImVec2(Widget::CalcSize(2)))) { Util::SetCarForwardSpeed(pVeh, m_fLockSpeed);