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

Merge branch 'master' of gitlab.liu.se:edvbe696/pycommandcenter

parents a904ebed c9ab1249
No related branches found
No related tags found
No related merge requests found
build/ build/
.idea
venv
.vs
...@@ -19,4 +19,3 @@ pages: ...@@ -19,4 +19,3 @@ pages:
- public - public
only: only:
- master - master
- Doc
_autosummary/ _autosummary/
_build/ _build/
venv
.idea
...@@ -9,7 +9,7 @@ IDAReplayObserver ...@@ -9,7 +9,7 @@ IDAReplayObserver
.. method:: IDAReplayObserver.on_game_start(self) .. method:: IDAReplayObserver.on_game_start(self)
This method is called when a replay has stared, when you inherit it you have to This method is called when a replay has started, when you inherit it you have to
call the parent's on_game_start method in order to make it work (see call the parent's on_game_start method in order to make it work (see
:ref:`replays`). :ref:`replays`).
...@@ -25,6 +25,14 @@ IDAReplayObserver ...@@ -25,6 +25,14 @@ IDAReplayObserver
call the parent's on_game_start method in order to make it work (see call the parent's on_game_start method in order to make it work (see
:ref:`replays`). :ref:`replays`).
.. method:: IDAReplayObserver.on_unit_created(self)
This method is called when the an unit is created, that includes when an unit leaves a refinery.
.. method:: IDAReplayObserver.on_unit_destroyed(self)
This unit is called when a unit is destroyed.
Methods: Methods:
.. method:: IDAReplayObserver.get_all_units(self) -> List[library.ReplayUnit] .. method:: IDAReplayObserver.get_all_units(self) -> List[library.ReplayUnit]
......
...@@ -68,7 +68,7 @@ if the information you want could be collected with SC2Reader. ...@@ -68,7 +68,7 @@ if the information you want could be collected with SC2Reader.
ReplayObserver ReplayObserver
-------------- --------------
This is the second technique it is much like using a bot but whit the difference This is the second technique it is much like using a bot but with the difference
that no action can be preform just observations. that no action can be preform just observations.
......
...@@ -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")
......
...@@ -136,8 +136,20 @@ PYBIND11_MODULE(library, m) ...@@ -136,8 +136,20 @@ PYBIND11_MODULE(library, m)
.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("get_player_race", &IDAReplayObserver::GetPlayerRace,"player_id"_a) .def("get_player_race", &IDAReplayObserver::GetPlayerRace,"player_id"_a)
.def("get_replay_path", &IDAReplayObserver::GetReplayPath)
.def("get_result_for_player", &IDAReplayObserver::GetResultForPlayer, "player_id"_a)
.def("on_unit_destroyed", &IDAReplayObserver::OnReplayUnitDestroyed, "unit"_a)
.def("on_unit_created", &IDAReplayObserver::OnReplayUnitCreated, "unit"_a)
; ;
py::enum_<sc2::GameResult>(m, "GameResult")
.value("Win", sc2::GameResult::Win)
.value("Loss", sc2::GameResult::Loss)
.value("Tie", sc2::GameResult::Tie)
.value("Undecided", sc2::GameResult::Undecided);
py::class_<sc2::PlayerSetup>(m, "PlayerSetup"); py::class_<sc2::PlayerSetup>(m, "PlayerSetup");
py::enum_<sc2::Difficulty>(m, "Difficulty") py::enum_<sc2::Difficulty>(m, "Difficulty")
......
...@@ -95,6 +95,27 @@ public: ...@@ -95,6 +95,27 @@ public:
); );
} }
void OnReplayUnitDestroyed(const ReplayUnit *unit) override
{
PYBIND11_OVERLOAD_NAME(
void,
IDAReplayObserver,
"on_unit_destroyed",
OnReplayUnitDestroyed,
unit
);
}
void OnReplayUnitCreated(const ReplayUnit *unit) override
{
PYBIND11_OVERLOAD_NAME(
void,
IDAReplayObserver,
"on_unit_created",
OnReplayUnitCreated,
unit
);
}
}; };
// The functions below are all defined in different .cpp files, in order // The functions below are all defined in different .cpp files, in order
......
#include "IDAReplayObserver.h" #include "IDAReplayObserver.h"
#include "Util.h" #include "Util.h"
#include <pybind11/pybind11.h>
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());
} }
} }
...@@ -32,18 +37,44 @@ void IDAReplayObserver::OnGameEnd() ...@@ -32,18 +37,44 @@ void IDAReplayObserver::OnGameEnd()
void IDAReplayObserver::OnUnitDestroyed(const sc2::Unit* unit) void IDAReplayObserver::OnUnitDestroyed(const sc2::Unit* unit)
{ {
ReplayUnit unitInformation = ReplayUnit(unit, *this); ReplayUnit unitInformation = ReplayUnit(unit, *this);
OnUnitInfomationDestroyed(&unitInformation); OnReplayUnitDestroyed(&unitInformation);
}
void IDAReplayObserver::OnReplayUnitDestroyed(const ReplayUnit *)
{
}
void IDAReplayObserver::OnUnitCreated(const sc2::Unit * unit)
{
ReplayUnit unitInformation = ReplayUnit(unit, *this);
OnReplayUnitCreated(&unitInformation);
}
void IDAReplayObserver::OnReplayUnitCreated(const ReplayUnit *)
{
} }
void IDAReplayObserver::OnUnitInfomationDestroyed(const ReplayUnit *) void IDAReplayObserver::OnBuildingConstructionComplete(const sc2::Unit *unit)
{ {
ReplayUnit unitInformation = ReplayUnit(unit, *this);
OnReplayUnitCreated(&unitInformation);
} }
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();
} }
...@@ -57,19 +88,19 @@ const std::vector<ReplayUnit>& IDAReplayObserver::GetAllUnits() const ...@@ -57,19 +88,19 @@ const std::vector<ReplayUnit>& IDAReplayObserver::GetAllUnits() const
return m_allUnits; return m_allUnits;
} }
CCRace IDAReplayObserver::GetPlayerRace(int player) const CCRace IDAReplayObserver::GetPlayerRace(int player)
{
return ReplayControl()->GetReplayInfo().players[player].race;
}
std::string IDAReplayObserver::GetReplayPath()
{ {
auto playerID = Observation()->GetPlayerID(); return ReplayControl()->GetReplayInfo().replay_path;
for (auto & playerInfo : Observation()->GetGameInfo().player_info) }
{
if (playerInfo.player_id == playerID)
{
return playerInfo.race_actual;
}
}
BOT_ASSERT(false, "Failed to find the player's race!"); sc2::GameResult IDAReplayObserver::GetResultForPlayer(int player)
return sc2::Race::Random; {
return ReplayControl()->GetReplayInfo().players[player].game_result;
} }
...@@ -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();
...@@ -20,12 +21,18 @@ public: ...@@ -20,12 +21,18 @@ public:
void OnStep() override; void OnStep() override;
void OnGameEnd() override; void OnGameEnd() override;
void OnUnitDestroyed(const sc2::Unit*) override; void OnUnitDestroyed(const sc2::Unit*) override;
void OnUnitInfomationDestroyed(const ReplayUnit*); virtual void OnReplayUnitDestroyed(const ReplayUnit*);
void OnUnitCreated(const sc2::Unit*);
virtual void OnReplayUnitCreated(const ReplayUnit*);
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) const; CCRace GetPlayerRace(int player);
std::string GetReplayPath();
sc2::GameResult GetResultForPlayer(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