diff --git a/src/include/color.h b/src/include/color.h index 7366040ba..756f0f39f 100644 --- a/src/include/color.h +++ b/src/include/color.h @@ -71,7 +71,11 @@ class CUnitColors public: CUnitColors() {} -public: + void Clear(); + + void Set(std::vector<CColor> &colors); + +private: std::vector<CColor> Colors; }; diff --git a/src/include/player.h b/src/include/player.h index be7867eb9..4ee079844 100644 --- a/src/include/player.h +++ b/src/include/player.h @@ -160,10 +160,6 @@ public: IntColor Color; /// color of units on minimap - CUnitColors UnitColors; /// Unit colors for new units - - std::vector<CUnit *> FreeWorkers; /// Container for free workers - // Upgrades/Allows: CAllow Allow; /// Allowed for player CUpgradeTimers UpgradeTimers; /// Timer for the upgrades @@ -187,8 +183,19 @@ public: void AddUnit(CUnit &unit); void RemoveUnit(CUnit &unit); + + std::vector<CUnit *>::const_iterator FreeWorkersBegin() const; + std::vector<CUnit *>::const_iterator FreeWorkersEnd() const; + std::vector<CUnit *>::iterator FreeWorkersBegin(); + std::vector<CUnit *>::iterator FreeWorkersEnd(); + + CUnit *GetFreeWorker(int index) const; + int GetFreeWorkersCount() const; void UpdateFreeWorkers(); + void ClearUnitColors(); + void SetUnitColors(std::vector<CColor> &colors); + /// Get a resource of the player int GetResource(const int resource, const int type); /// Adds/subtracts some resources to/from the player store @@ -289,7 +296,9 @@ public: void SetRevealed(const bool revealed); private: + CUnitColors UnitColors; /// Unit colors for new units std::vector<CUnit *> Units; /// units of this player + std::vector<CUnit *> FreeWorkers; /// Container for free workers unsigned int Enemy; /// enemy bit field for this player unsigned int Allied; /// allied bit field for this player std::set<uint8_t> HasVisionFrom; /// set of player indexes that have shared their vision with this player diff --git a/src/stratagus/player.cpp b/src/stratagus/player.cpp index b6bdaeae9..59668dca5 100644 --- a/src/stratagus/player.cpp +++ b/src/stratagus/player.cpp @@ -394,7 +394,7 @@ void CleanPlayers() void FreePlayerColors() { for (int i = 0; i < PlayerMax; ++i) { - Players[i].UnitColors.Colors.clear(); + Players[i].ClearUnitColors(); } PlayerColorsRGB.clear(); } @@ -850,6 +850,36 @@ void CPlayer::RemoveUnit(CUnit &unit) Assert(last == &unit || this->Units[last->PlayerSlot] == last); } +std::vector<CUnit *>::const_iterator CPlayer::FreeWorkersBegin() const +{ + return FreeWorkers.begin(); +} + +std::vector<CUnit *>::iterator CPlayer::FreeWorkersBegin() +{ + return FreeWorkers.begin(); +} + +std::vector<CUnit *>::const_iterator CPlayer::FreeWorkersEnd() const +{ + return FreeWorkers.end(); +} + +std::vector<CUnit *>::iterator CPlayer::FreeWorkersEnd() +{ + return FreeWorkers.end(); +} + +CUnit *CPlayer::GetFreeWorker(int index) const +{ + return FreeWorkers[index]; +} + +int CPlayer::GetFreeWorkersCount() const +{ + return static_cast<int>(FreeWorkers.size()); +} + void CPlayer::UpdateFreeWorkers() { FreeWorkers.clear(); @@ -871,7 +901,6 @@ void CPlayer::UpdateFreeWorkers() } } - std::vector<CUnit *>::const_iterator CPlayer::UnitBegin() const { return Units.begin(); @@ -1272,7 +1301,7 @@ void GraphicPlayerPixels(int colorIndex, const CGraphic &sprite) void SetPlayersPalette() { for (int i = 0; i < PlayerMax; ++i) { - Players[i].UnitColors.Colors = PlayerColorsRGB[i]; + Players[i].SetUnitColors(PlayerColorsRGB[i]); } } @@ -1493,4 +1522,14 @@ bool CPlayer::IsTeamed(const CUnit &unit) const return this->IsTeamed(*unit.Player); } +void CPlayer::ClearUnitColors() +{ + UnitColors.Clear(); +} + +void CPlayer::SetUnitColors(std::vector<CColor> &colors) +{ + UnitColors.Set(colors); +} + //@} diff --git a/src/ui/interface.cpp b/src/ui/interface.cpp index 990da478b..a7cf77475 100644 --- a/src/ui/interface.cpp +++ b/src/ui/interface.cpp @@ -449,18 +449,18 @@ void UiToggleTerrain() */ void UiFindIdleWorker() { - if (ThisPlayer->FreeWorkers.empty()) { + if (ThisPlayer->GetFreeWorkersCount() == 0) { return; } - CUnit *unit = ThisPlayer->FreeWorkers[0]; + CUnit *unit = ThisPlayer->GetFreeWorker(0); if (LastIdleWorker) { - const std::vector<CUnit *> &freeWorkers = ThisPlayer->FreeWorkers; - std::vector<CUnit *>::const_iterator it = std::find(freeWorkers.begin(), - freeWorkers.end(), - LastIdleWorker); - if (it != ThisPlayer->FreeWorkers.end()) { - if (*it != ThisPlayer->FreeWorkers.back()) { - unit = *(++it); + bool found = false; + for(auto it = ThisPlayer->FreeWorkersBegin(); it != ThisPlayer->FreeWorkersEnd(); ++it) { + if (found) { + unit = *it; + break; + } else if (*it == unit) { + found = true; } } } diff --git a/src/ui/mainscr.cpp b/src/ui/mainscr.cpp index aad439ac6..c4ec94040 100644 --- a/src/ui/mainscr.cpp +++ b/src/ui/mainscr.cpp @@ -619,7 +619,7 @@ void DrawResources() label.Draw(UI.Resources[ScoreCost].TextX, UI.Resources[ScoreCost].TextY + (score > 99999) * 3, score); } if (UI.Resources[FreeWorkersCount].TextX != -1) { - const int workers = ThisPlayer->FreeWorkers.size(); + const int workers = ThisPlayer->GetFreeWorkersCount(); int textX = UI.Resources[FreeWorkersCount].TextX; if (textX < 0) { // XXX: this is hacky, but what use is that bit otherwise diff --git a/src/ui/mouse.cpp b/src/ui/mouse.cpp index 348bd0be8..532f214fe 100644 --- a/src/ui/mouse.cpp +++ b/src/ui/mouse.cpp @@ -719,7 +719,7 @@ static void HandleMouseOn(const PixelPos screenPos) CGraphic* g = UI.Resources[FreeWorkersCount].G; if (g && (x = UI.Resources[FreeWorkersCount].IconX) && (y = UI.Resources[FreeWorkersCount].IconY)) { int textX = UI.Resources[FreeWorkersCount].TextX; - if (textX > 0 || ThisPlayer->FreeWorkers.size() > 0) { + if (textX > 0 || ThisPlayer->GetFreeWorkersCount() > 0) { if (screenPos > PixelPos(x, y) && screenPos < PixelPos(x + g->getWidth(), y + g->getHeight())) { ButtonAreaUnderCursor = ButtonAreaMenu; ButtonUnderCursor = ButtonUnderFreeWorkers; diff --git a/src/video/color.cpp b/src/video/color.cpp index 9e8331661..499bafe17 100644 --- a/src/video/color.cpp +++ b/src/video/color.cpp @@ -67,6 +67,16 @@ void CColor::Parse(lua_State *l, const int offset) this->A = 255; } +void CUnitColors::Clear() +{ + Colors.clear(); +} + +void CUnitColors::Set(std::vector<CColor> &colors) +{ + Colors = colors; +} + IntColor InterpolateColor(IntColor color1, IntColor color2, float fraction) { unsigned char r1 = (color1 >> 16) & 0xff;