Skip to content
Snippets Groups Projects
Commit e6b395c0 authored by Rojikku98's avatar Rojikku98
Browse files

Started work on onUnityDestroyed

parent 58809f9e
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,8 @@ void define_replay_unit(py::module & m) ...@@ -6,7 +6,8 @@ void define_replay_unit(py::module & m)
{ {
py::class_<UnitInformation>(m, "ReplayUnit") py::class_<UnitInformation>(m, "ReplayUnit")
.def_property_readonly("id", &UnitInformation::getID) .def_property_readonly("id", &UnitInformation::getID)
.def_property_readonly("unit_type", &UnitInformation::getType, "The name of the type") .def_property_readonly("unit_type", &UnitInformation::getType, "The id of the type")
.def_property_readonly("unit_type_name", &UnitInformation::getTypeName, "The name of the type")
.def_property_readonly("position", &UnitInformation::getPosition, "The :class:`library.Point2D` of the unit") .def_property_readonly("position", &UnitInformation::getPosition, "The :class:`library.Point2D` of the unit")
.def_property_readonly("tile_position", &UnitInformation::getTilePosition, "The :class:`library.Point2DI` of the unit") .def_property_readonly("tile_position", &UnitInformation::getTilePosition, "The :class:`library.Point2DI` of the unit")
.def_property_readonly("hit_points", &UnitInformation::getHitPoints) .def_property_readonly("hit_points", &UnitInformation::getHitPoints)
...@@ -27,8 +28,9 @@ void define_replay_unit(py::module & m) ...@@ -27,8 +28,9 @@ void define_replay_unit(py::module & m)
.def_property_readonly("is_valid", &UnitInformation::isValid) .def_property_readonly("is_valid", &UnitInformation::isValid)
.def_property_readonly("is_training", &UnitInformation::isTraining) .def_property_readonly("is_training", &UnitInformation::isTraining)
.def_property_readonly("is_blip", &UnitInformation::isBlip) .def_property_readonly("is_blip", &UnitInformation::isBlip)
.def_property_readonly("target", &UnitInformation::getTarget) // Has target and target crashes if the target died in the same frame
.def_property_readonly("has_target", &UnitInformation::hasTarget) //.def_property_readonly("target", &UnitInformation::getTarget)
//.def_property_readonly("has_target", &UnitInformation::hasTarget)
.def_property_readonly("max_hit_points", &UnitInformation::getMaxHitPoints) .def_property_readonly("max_hit_points", &UnitInformation::getMaxHitPoints)
.def_property_readonly("progress", &UnitInformation::getProgress) .def_property_readonly("progress", &UnitInformation::getProgress)
.def_property_readonly("current_ability_id", &UnitInformation::getCurrentAbilityID, "The AbilityID of currently used ability") .def_property_readonly("current_ability_id", &UnitInformation::getCurrentAbilityID, "The AbilityID of currently used ability")
...@@ -37,6 +39,6 @@ void define_replay_unit(py::module & m) ...@@ -37,6 +39,6 @@ void define_replay_unit(py::module & m)
.def_property_readonly("is_carrying_minerals", &UnitInformation::isCarryingMinerals) .def_property_readonly("is_carrying_minerals", &UnitInformation::isCarryingMinerals)
.def("__hash__", [](const UnitInformation & unit) { return std::hash<const sc2::Unit *>{}(unit.getUnitPtr()); }) .def("__hash__", [](const UnitInformation & unit) { return std::hash<const sc2::Unit *>{}(unit.getUnitPtr()); })
.def(py::self == py::self) .def(py::self == py::self)
.def("__repr__", [](const UnitInformation & unit) { return "<Unit of type: '" + unit.getType() + "'>"; }) .def("__repr__", [](const UnitInformation & unit) { return "<Unit of type: '" + unit.getTypeName() + "'>"; })
; ;
} }
...@@ -99,6 +99,7 @@ PYBIND11_MODULE(library, m) ...@@ -99,6 +99,7 @@ PYBIND11_MODULE(library, m)
.def("on_step", &IDAReplayObserver::OnStep) .def("on_step", &IDAReplayObserver::OnStep)
.def("on_game_end", &IDAReplayObserver::OnGameEnd) .def("on_game_end", &IDAReplayObserver::OnGameEnd)
.def("get_all_units", &IDAReplayObserver::GetAllUnits, "Returns a list of all units") .def("get_all_units", &IDAReplayObserver::GetAllUnits, "Returns a list of all units")
.def("on_unit_destroyed", &IDAReplayObserver::OnUnitInfomationDestroyed, "unit"_a)
; ;
py::class_<sc2::PlayerSetup>(m, "PlayerSetup"); py::class_<sc2::PlayerSetup>(m, "PlayerSetup");
......
...@@ -62,7 +62,7 @@ public: ...@@ -62,7 +62,7 @@ public:
} }
}; };
//todo fixa!
class PyReplayObserver : public IDAReplayObserver class PyReplayObserver : public IDAReplayObserver
{ {
public: public:
......
...@@ -5,7 +5,6 @@ void IDAReplayObserver::setUnits() ...@@ -5,7 +5,6 @@ void IDAReplayObserver::setUnits()
{ {
m_allUnits.clear(); m_allUnits.clear();
Control()->GetObservation();
for (auto & unit : Observation()->GetUnits()) for (auto & unit : Observation()->GetUnits())
{ {
m_allUnits.push_back(UnitInformation(unit, *this)); m_allUnits.push_back(UnitInformation(unit, *this));
...@@ -36,8 +35,22 @@ void IDAReplayObserver::OnGameEnd() ...@@ -36,8 +35,22 @@ void IDAReplayObserver::OnGameEnd()
{ {
} }
void IDAReplayObserver::OnUnitDestroyed(const sc2::Unit* unit)
{
UnitInformation unitInformation = UnitInformation(unit, *this);
OnUnitInfomationDestroyed(&unitInformation);
}
UnitInformation IDAReplayObserver::GetUnit(const CCUnitID tag) const UnitInformation IDAReplayObserver::GetUnit(const CCUnitID tag) const
{ {
std::cout << tag << std::endl;
if (tag == 0) {
std::cout << "TAG == 0" << std::endl;
}
UnitInformation(Observation()->GetUnit(tag), *(IDAReplayObserver *)this);
std::cout << "OK" << std::endl;
return UnitInformation(Observation()->GetUnit(tag), *(IDAReplayObserver *)this); return UnitInformation(Observation()->GetUnit(tag), *(IDAReplayObserver *)this);
} }
......
...@@ -24,6 +24,9 @@ public: ...@@ -24,6 +24,9 @@ public:
void OnGameStart() override; void OnGameStart() override;
void OnStep() override; void OnStep() override;
void OnGameEnd() override; void OnGameEnd() override;
void OnUnitDestroyed(const sc2::Unit*) override;
virtual void OnUnitInfomationDestroyed(const UnitInformation*);
UnitInformation GetUnit(const CCUnitID tag) const; UnitInformation GetUnit(const CCUnitID tag) const;
......
...@@ -14,17 +14,31 @@ UnitInformation::UnitInformation(const sc2::Unit * unit, IDAReplayObserver & rep ...@@ -14,17 +14,31 @@ UnitInformation::UnitInformation(const sc2::Unit * unit, IDAReplayObserver & rep
} }
std::string UnitInformation::getTypeName() const
{
return sc2::UnitTypeToName(m_unit->unit_type);
}
bool UnitInformation::hasTarget() const bool UnitInformation::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
if (t_id == sc2::NullTag) {
return false;
}
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 // IDABot finds the unit with this tag, and returns true if valid
return m_replayObserver->GetUnit(t_id).isValid(); return m_replayObserver->GetUnit(t_id).isValid();
} }
} }
std::cout << "END HAS TARGET" << std::endl;
return false; return false;
} }
...@@ -33,12 +47,17 @@ UnitInformation UnitInformation::getTarget() const ...@@ -33,12 +47,17 @@ UnitInformation UnitInformation::getTarget() const
{ {
BOT_ASSERT(isValid(), "Unit is not valid"); BOT_ASSERT(isValid(), "Unit is not valid");
// if unit has order, check tag of target of first order // if unit has order, check tag of target of first order
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 //The tag is for somereason a null tag
if (t_id == sc2::NullTag) { 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; return *this;
} }
// IDAReplayObserver finds the unit with this tag // IDAReplayObserver finds the unit with this tag
......
...@@ -14,6 +14,7 @@ public: ...@@ -14,6 +14,7 @@ public:
UnitInformation(const sc2::Unit * unit, IDAReplayObserver & replayObserver); UnitInformation(const sc2::Unit * unit, IDAReplayObserver & replayObserver);
std::string getType() const; std::string getType() const;
std::string getTypeName() const;
bool hasTarget() const; bool hasTarget() const;
UnitInformation getTarget() const; UnitInformation getTarget() const;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment