diff --git a/python-api-src/lib_replay_unit.cpp b/python-api-src/lib_replay_unit.cpp index 7a70770dffa7c1738104f451276f1b6f9aaaf3bb..63197f20a344ae22df680e8ffee70d7396aeba29 100644 --- a/python-api-src/lib_replay_unit.cpp +++ b/python-api-src/lib_replay_unit.cpp @@ -28,9 +28,8 @@ void define_replay_unit(py::module & m) .def_property_readonly("is_valid", &ReplayUnit::isValid) .def_property_readonly("is_training", &ReplayUnit::isTraining) .def_property_readonly("is_blip", &ReplayUnit::isBlip) - // Has target and target crashes if the target died in the same frame - //.def_property_readonly("target", &ReplayUnit::getTarget) - //.def_property_readonly("has_target", &ReplayUnit::hasTarget) + .def_property_readonly("target", &ReplayUnit::getTarget) + .def_property_readonly("has_target", &ReplayUnit::hasTarget) .def_property_readonly("max_hit_points", &ReplayUnit::getMaxHitPoints) .def_property_readonly("progress", &ReplayUnit::getProgress) .def_property_readonly("current_ability_id", &ReplayUnit::getCurrentAbilityID, "The AbilityID of currently used ability") diff --git a/src/IDAReplayObserver.cpp b/src/IDAReplayObserver.cpp index b9ed6d12e129328da250cbdd16c7426af27f9643..0af4993668ed4d0af267e7949f6f82c06a78bd1c 100644 --- a/src/IDAReplayObserver.cpp +++ b/src/IDAReplayObserver.cpp @@ -6,9 +6,12 @@ void IDAReplayObserver::setUnits() { m_allUnits.clear(); + m_allUnitsID.clear(); for (auto & unit : Observation()->GetUnits()) { - m_allUnits.push_back(ReplayUnit(unit, *this)); + ReplayUnit replayUnit = ReplayUnit(unit, *this); + m_allUnits.push_back(replayUnit); + m_allUnitsID.insert(replayUnit.getID()); } } @@ -45,7 +48,6 @@ void IDAReplayObserver::OnReplayUnitDestroyed(const ReplayUnit *) void IDAReplayObserver::OnUnitCreated(const sc2::Unit * unit) { ReplayUnit unitInformation = ReplayUnit(unit, *this); - std::cout << "OnUnitCreated" << std::endl; OnReplayUnitCreated(&unitInformation); } @@ -56,7 +58,6 @@ void IDAReplayObserver::OnReplayUnitCreated(const ReplayUnit *) void IDAReplayObserver::OnBuildingConstructionComplete(const sc2::Unit *unit) { ReplayUnit unitInformation = ReplayUnit(unit, *this); - std::cout << "OnBuildingConstructionComplete" << std::endl; OnReplayUnitCreated(&unitInformation); } @@ -64,8 +65,16 @@ void IDAReplayObserver::OnBuildingConstructionComplete(const sc2::Unit *unit) ReplayUnit IDAReplayObserver::GetUnit(const CCUnitID tag) const -{ - return ReplayUnit(Observation()->GetUnit(tag), *(IDAReplayObserver *)this); +{ + + return ReplayUnit(Observation()->GetUnit(tag), *(IDAReplayObserver *)this); + +} + +bool IDAReplayObserver::UnitExists(const CCUnitID tag) const +{ + return m_allUnitsID.find(tag) != m_allUnitsID.end(); + } diff --git a/src/IDAReplayObserver.h b/src/IDAReplayObserver.h index 503726f429d351ce098309e913bb2a87001a036f..83bba966a4ec21cb79b4c75a92731e3c31f019bc 100644 --- a/src/IDAReplayObserver.h +++ b/src/IDAReplayObserver.h @@ -12,6 +12,7 @@ class IDAReplayObserver : public sc2::ReplayObserver { void setUnits(); std::vector<ReplayUnit> m_allUnits; + std::set<CCUnitID> m_allUnitsID; public: IDAReplayObserver(); @@ -26,6 +27,7 @@ public: void OnBuildingConstructionComplete(const sc2::Unit*); ReplayUnit GetUnit(const CCUnitID tag) const; + bool UnitExists(const CCUnitID tag) const; const std::vector<ReplayUnit> & GetAllUnits() const; CCRace GetPlayerRace(int player); diff --git a/src/ReplayUnit.cpp b/src/ReplayUnit.cpp index 1bab9381607394099b2d292bc4c6281db9da854f..5b6298054ac1b692f2a505336980b4f0d00146c1 100644 --- a/src/ReplayUnit.cpp +++ b/src/ReplayUnit.cpp @@ -22,23 +22,16 @@ ReplayUnit::ReplayUnit(const sc2::Unit * unit, IDAReplayObserver & replayObserve bool ReplayUnit::hasTarget() const { BOT_ASSERT(isValid(), "Unit is not valid"); - std::cout << "HAS TARGET" << std::endl; if (getUnitPtr()->orders.size() > 0) { if (getUnitPtr()->orders[0].target_unit_tag != NULL) { CCUnitID t_id = getUnitPtr()->orders[0].target_unit_tag; - //The tag is for somereason a null tag - if (t_id == sc2::NullTag) { - return false; + // IDAReplayObserver checks if the unit with this tag still exists + if (m_replayObserver->UnitExists(t_id)){ + // IDAReplayObserver finds the unit with this tag, and returns true if valid + return m_replayObserver->GetUnit(t_id).isValid(); } - std::cout << "1MID HAS TARGET" << std::endl; - std::cout << "2MID HAS TARGET" << std::endl; - std::cout << "valid" << m_replayObserver->GetUnit(t_id).getType() << std::endl; - std::cout << "AFTER" << std::endl; - // IDABot finds the unit with this tag, and returns true if valid - return m_replayObserver->GetUnit(t_id).isValid(); } } - std::cout << "END HAS TARGET" << std::endl; return false; } @@ -52,18 +45,12 @@ ReplayUnit ReplayUnit::getTarget() const if (getUnitPtr()->orders.size() > 0) { // t_id is set to the unit tag of the target CCUnitID t_id = getUnitPtr()->orders[0].target_unit_tag; - //The tag is for somereason a null tag - if (t_id == sc2::NullTag) { - - std::cout << "nullTAG" << std::endl; - std::cout << "type " << sc2::UnitTypeToName(m_unit->unit_type) <<"pos " << getPosition().x << " x y "<< getPosition().y << ", id " << getID() << "player " << getPlayer() << std::endl; - std::cout << getUnitPtr()->orders.size() << std::endl; - return *this; + // Checks if the tag is a null tag or the unit have been removed + if (t_id != sc2::NullTag && m_replayObserver->UnitExists(t_id)){ + // IDAReplayObserver finds the unit with this tag + return m_replayObserver->GetUnit(t_id); } - // IDAReplayObserver finds the unit with this tag - return m_replayObserver->GetUnit(t_id); } - ReplayUnit this_unit = ReplayUnit(m_unit, *m_replayObserver); return this_unit; }