From 3dc145a21010f67fc59f6abd3a7bda5b2d0da8b3 Mon Sep 17 00:00:00 2001 From: jowen005 Date: Sat, 8 Aug 2026 00:15:06 -0400 Subject: [PATCH] Set max unit groups and fixed unit destruction bug --- activeGameState.cpp | 48 +++++++++++++++++++++++++++++++++++++++------ activeGameState.h | 2 ++ unit.cpp | 20 +++++++++++++++++++ unit.h | 1 + 4 files changed, 65 insertions(+), 6 deletions(-) diff --git a/activeGameState.cpp b/activeGameState.cpp index f5375f6..3e90132 100755 --- a/activeGameState.cpp +++ b/activeGameState.cpp @@ -829,21 +829,57 @@ namespace battleship{ if(controlPressed) { // Ctrl + group number + Q assigns the current selection of units - unitGroups[pendingGroup] = mainPlayer->getSelectedUnits(); + + // If there are currently maximum allowed units in existence then do not allow creation of a new unique group, only allow overwritting an existing group + if(static_cast(unitGroups.size()) >= maxUnitGroups) + { + // See if pendingGroup is equal to any key and overwrite the key if so + auto it = unitGroups.find(pendingGroup); + if(it != unitGroups.end()) + { + unitGroups[pendingGroup] = mainPlayer->getSelectedUnits(); + } + // Iterate through all the keys to see if pendingGroup is equal to any key and overwrite the key if so + // for(const auto& [key, value]: unitGroups) + // { + // if(key == pendingGroup) + // { + // unitGroups[pendingGroup] = mainPlayer->getSelectedUnits(); + // } + // } + // If it can't overwrite then the pendingGroup will just be reset since this will bypass the rest of the if statements + } + else + { + unitGroups[pendingGroup] = mainPlayer->getSelectedUnits(); + } + } else { // Pressing a number and Q without shifting replaces the unit selection with the unit group for the number pressed if(!shiftPressed) { - deselectUnits(); - mainPlayer->selectUnits(unitGroups[pendingGroup]); + auto it = unitGroups.find(pendingGroup); + if(it != unitGroups.end()) + { + deselectUnits(); + mainPlayer->selectUnits(it->second); + } + // deselectUnits(); + // mainPlayer->selectUnits(unitGroups[pendingGroup]); } - // If shift is pressed then shift plus a number and Q adds the selection to the current selection + // If shift is pressed then shift plus a number and Q adds the selection to the current group of the existing key else { - mainPlayer->selectUnits(unitGroups[pendingGroup]); - unitGroups[pendingGroup] = mainPlayer->getSelectedUnits(); + auto it = unitGroups.find(pendingGroup); + if(it != unitGroups.end()) + { + mainPlayer->selectUnits(it->second); + unitGroups[pendingGroup] = mainPlayer->getSelectedUnits(); + } + // mainPlayer->selectUnits(unitGroups[pendingGroup]); + // unitGroups[pendingGroup] = mainPlayer->getSelectedUnits(); } } // Reset the pending group diff --git a/activeGameState.h b/activeGameState.h index a741ff4..fa6923d 100755 --- a/activeGameState.h +++ b/activeGameState.h @@ -51,6 +51,7 @@ namespace battleship{ inline std::vector getGuiRects(){return guiRects;} inline std::vector getGuiTexts(){return guiTexts;} inline Player* getPlayer(){return mainPlayer;} + inline std::unordered_map>& getUnitGroups(){return unitGroups;} // Returns the unitGroups by reference // inline std::vector& getUnitGroup(int i){return unitGroups[i];} inline void setBuildableStructSelected(bool bss){this->buildableStructSelected = bss;} inline bool isBuildableStructSelected(){return buildableStructSelected;} @@ -96,6 +97,7 @@ namespace battleship{ bool shiftPressed = false; bool controlPressed = false; int pendingGroup = -1; // The group value entered to save and select a group of units. -1 means no units have been selected + int maxUnitGroups = 100; // There can be a max of 100 unit groups at a time bool selectMouseClicked = false; bool orderMouseClicked = false; bool buildableStructSelected = false; diff --git a/unit.cpp b/unit.cpp index 70e4537..ca4ea34 100755 --- a/unit.cpp +++ b/unit.cpp @@ -12,6 +12,8 @@ #include #include +#include + #include "unit.h" #include "weapon.h" #include "util.h" @@ -64,6 +66,7 @@ namespace battleship{ destroySound(); destroyHitbox(); destroyModel(); + removeFromUnitGroup(); } void Unit::initProperties(){ @@ -172,6 +175,23 @@ namespace battleship{ losLightNode = nullptr; } + /// @brief Check if unit is in a unit group and if so remove it + void Unit::removeFromUnitGroup(){ + ActiveGameState *activeState = (ActiveGameState*)GameManager::getSingleton()->getStateManager()->getAppStateByType(AppStateType::ACTIVE_STATE); + std::unordered_map>& unitGroups = activeState->getUnitGroups(); + + // Iterate through all the individual unit groups and see if this unit is in any of them + for(auto& [key, value]: unitGroups) + { + if(std::find(value.begin(), value.end(), this) != value.end()) + { + //std::erase(value, this); + value.erase(std::remove(value.begin(), value.end(), this), value.end()); + } + } + + } + int Unit::getNumFreeGarrisonSlots(){ int numFreeSlots = 0; diff --git a/unit.h b/unit.h index 0bcf425..54bd923 100755 --- a/unit.h +++ b/unit.h @@ -112,6 +112,7 @@ namespace battleship{ bool canGarrison(Vehicle*); void initLosLight(); void destroyLosLight(); + void removeFromUnitGroup(); int getNumFreeGarrisonSlots(); inline void setState(State s){state = s;} inline State getState(){return state;}