Skip to content
Snippets Groups Projects
Commit 907971dd authored by Edvin Bergström's avatar Edvin Bergström
Browse files

Fixed target

parent cbe8af94
No related branches found
No related tags found
No related merge requests found
...@@ -28,9 +28,8 @@ void define_replay_unit(py::module & m) ...@@ -28,9 +28,8 @@ void define_replay_unit(py::module & m)
.def_property_readonly("is_valid", &ReplayUnit::isValid) .def_property_readonly("is_valid", &ReplayUnit::isValid)
.def_property_readonly("is_training", &ReplayUnit::isTraining) .def_property_readonly("is_training", &ReplayUnit::isTraining)
.def_property_readonly("is_blip", &ReplayUnit::isBlip) .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("target", &ReplayUnit::getTarget) .def_property_readonly("has_target", &ReplayUnit::hasTarget)
//.def_property_readonly("has_target", &ReplayUnit::hasTarget)
.def_property_readonly("max_hit_points", &ReplayUnit::getMaxHitPoints) .def_property_readonly("max_hit_points", &ReplayUnit::getMaxHitPoints)
.def_property_readonly("progress", &ReplayUnit::getProgress) .def_property_readonly("progress", &ReplayUnit::getProgress)
.def_property_readonly("current_ability_id", &ReplayUnit::getCurrentAbilityID, "The AbilityID of currently used ability") .def_property_readonly("current_ability_id", &ReplayUnit::getCurrentAbilityID, "The AbilityID of currently used ability")
......
...@@ -6,9 +6,12 @@ ...@@ -6,9 +6,12 @@
void IDAReplayObserver::setUnits() void IDAReplayObserver::setUnits()
{ {
m_allUnits.clear(); m_allUnits.clear();
m_allUnitsID.clear();
for (auto & unit : Observation()->GetUnits()) 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 *) ...@@ -45,7 +48,6 @@ void IDAReplayObserver::OnReplayUnitDestroyed(const ReplayUnit *)
void IDAReplayObserver::OnUnitCreated(const sc2::Unit * unit) void IDAReplayObserver::OnUnitCreated(const sc2::Unit * unit)
{ {
ReplayUnit unitInformation = ReplayUnit(unit, *this); ReplayUnit unitInformation = ReplayUnit(unit, *this);
std::cout << "OnUnitCreated" << std::endl;
OnReplayUnitCreated(&unitInformation); OnReplayUnitCreated(&unitInformation);
} }
...@@ -56,7 +58,6 @@ void IDAReplayObserver::OnReplayUnitCreated(const ReplayUnit *) ...@@ -56,7 +58,6 @@ void IDAReplayObserver::OnReplayUnitCreated(const ReplayUnit *)
void IDAReplayObserver::OnBuildingConstructionComplete(const sc2::Unit *unit) void IDAReplayObserver::OnBuildingConstructionComplete(const sc2::Unit *unit)
{ {
ReplayUnit unitInformation = ReplayUnit(unit, *this); ReplayUnit unitInformation = ReplayUnit(unit, *this);
std::cout << "OnBuildingConstructionComplete" << std::endl;
OnReplayUnitCreated(&unitInformation); OnReplayUnitCreated(&unitInformation);
} }
...@@ -64,8 +65,16 @@ void IDAReplayObserver::OnBuildingConstructionComplete(const sc2::Unit *unit) ...@@ -64,8 +65,16 @@ void IDAReplayObserver::OnBuildingConstructionComplete(const sc2::Unit *unit)
ReplayUnit IDAReplayObserver::GetUnit(const CCUnitID tag) const 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();
} }
......
...@@ -12,6 +12,7 @@ class IDAReplayObserver : public sc2::ReplayObserver ...@@ -12,6 +12,7 @@ class IDAReplayObserver : public sc2::ReplayObserver
{ {
void setUnits(); void setUnits();
std::vector<ReplayUnit> m_allUnits; std::vector<ReplayUnit> m_allUnits;
std::set<CCUnitID> m_allUnitsID;
public: public:
IDAReplayObserver(); IDAReplayObserver();
...@@ -26,6 +27,7 @@ public: ...@@ -26,6 +27,7 @@ public:
void OnBuildingConstructionComplete(const sc2::Unit*); void OnBuildingConstructionComplete(const sc2::Unit*);
ReplayUnit GetUnit(const CCUnitID tag) const; ReplayUnit GetUnit(const CCUnitID tag) const;
bool UnitExists(const CCUnitID tag) const;
const std::vector<ReplayUnit> & GetAllUnits() const; const std::vector<ReplayUnit> & GetAllUnits() const;
CCRace GetPlayerRace(int player); CCRace GetPlayerRace(int player);
......
...@@ -22,23 +22,16 @@ ReplayUnit::ReplayUnit(const sc2::Unit * unit, IDAReplayObserver & replayObserve ...@@ -22,23 +22,16 @@ ReplayUnit::ReplayUnit(const sc2::Unit * unit, IDAReplayObserver & replayObserve
bool ReplayUnit::hasTarget() const bool ReplayUnit::hasTarget() const
{ {
BOT_ASSERT(isValid(), "Unit is not valid"); BOT_ASSERT(isValid(), "Unit is not valid");
std::cout << "HAS TARGET" << std::endl;
if (getUnitPtr()->orders.size() > 0) { if (getUnitPtr()->orders.size() > 0) {
if (getUnitPtr()->orders[0].target_unit_tag != NULL) { if (getUnitPtr()->orders[0].target_unit_tag != NULL) {
CCUnitID t_id = getUnitPtr()->orders[0].target_unit_tag; CCUnitID t_id = getUnitPtr()->orders[0].target_unit_tag;
//The tag is for somereason a null tag // IDAReplayObserver checks if the unit with this tag still exists
if (t_id == sc2::NullTag) { if (m_replayObserver->UnitExists(t_id)){
return false; // 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; return false;
} }
...@@ -52,18 +45,12 @@ ReplayUnit ReplayUnit::getTarget() const ...@@ -52,18 +45,12 @@ ReplayUnit ReplayUnit::getTarget() const
if (getUnitPtr()->orders.size() > 0) { if (getUnitPtr()->orders.size() > 0) {
// t_id is set to the unit tag of the target // t_id is set to the unit tag of the target
CCUnitID t_id = getUnitPtr()->orders[0].target_unit_tag; CCUnitID t_id = getUnitPtr()->orders[0].target_unit_tag;
//The tag is for somereason a null tag // Checks if the tag is a null tag or the unit have been removed
if (t_id == sc2::NullTag) { if (t_id != sc2::NullTag && m_replayObserver->UnitExists(t_id)){
// IDAReplayObserver finds the unit with this tag
std::cout << "nullTAG" << std::endl; return m_replayObserver->GetUnit(t_id);
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;
} }
// IDAReplayObserver finds the unit with this tag
return m_replayObserver->GetUnit(t_id);
} }
ReplayUnit this_unit = ReplayUnit(m_unit, *m_replayObserver); ReplayUnit this_unit = ReplayUnit(m_unit, *m_replayObserver);
return this_unit; return this_unit;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment