java-topology/defects/megaglest-0002/patch/megaglest-0002.patch

63 lines
2.2 KiB
Diff

# UNDF: UNDF-2026-000000983
--- a/source/glest_game/world/unit_updater.h
+++ b/source/glest_game/world/unit_updater.h
@@ -1,6 +1,7 @@
#ifndef _GLEST_GAME_UNITUPDATER_H_
#define _GLEST_GAME_UNITUPDATER_H_
+#include <unordered_set>
#include <vector>
...
@@ -127,7 +128,7 @@
vector<Unit *> findUnitsInRange(const Unit *unit, int radius);
private:
- void findUnitsForCell(Cell *cell, vector<Unit *> &units);
+ void findUnitsForCell(Cell *cell, vector<Unit *> &units, std::unordered_set<int> &seenIds);
--- a/source/glest_game/world/unit_updater.cpp
+++ b/source/glest_game/world/unit_updater.cpp
@@ -3461,13 +3461,10 @@
-void UnitUpdater::findUnitsForCell(Cell *cell, vector<Unit *> &units) {
+void UnitUpdater::findUnitsForCell(Cell *cell, vector<Unit *> &units, std::unordered_set<int> &seenIds) {
// all fields
if (cell != NULL) {
for (int k = 0; k < fieldCount; k++) {
Field f = static_cast<Field>(k);
// check field
Unit *cellUnit = cell->getUnit(f);
if (cellUnit != NULL && cellUnit->isAlive()) {
- // check if unit already is in list
- bool found = false;
- for (unsigned int i = 0; i < units.size(); ++i) {
- Unit *unitInList = units[i];
- if (unitInList->getId() == cellUnit->getId()) {
- found = true;
- break;
- }
- }
- if (found == false) {
+ // O(1) dedup via hash set instead of O(U) linear scan
+ if (seenIds.insert(cellUnit->getId()).second) {
units.push_back(cellUnit);
}
}
@@ -3492,6 +3489,8 @@
vector<Unit *> UnitUpdater::findUnitsInRange(const Unit *unit, int radius) {
int range = radius;
vector<Unit *> units;
+ std::unordered_set<int> seenIds;
// aux vars
int size = unit->getType()->getSize();
@@ -3507,7 +3506,7 @@
#endif
Cell *cell = map->getCell(i, j);
- findUnitsForCell(cell, units);
+ findUnitsForCell(cell, units, seenIds);
}
}
}