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
1 merge request!6Replays
build/
.idea
venv
.vs
......@@ -19,4 +19,3 @@ pages:
- public
only:
- master
- Doc
_autosummary/
_build/
venv
.idea
......@@ -9,7 +9,7 @@ IDAReplayObserver
.. 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
:ref:`replays`).
......@@ -25,6 +25,14 @@ IDAReplayObserver
call the parent's on_game_start method in order to make it work (see
: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:
.. method:: IDAReplayObserver.get_all_units(self) -> List[library.ReplayUnit]
......
......@@ -68,7 +68,7 @@ if the information you want could be collected with SC2Reader.
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.
......
......@@ -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")
......
......@@ -136,8 +136,20 @@ PYBIND11_MODULE(library, m)
.def("on_game_end", &IDAReplayObserver::OnGameEnd)
.def("get_all_units", &IDAReplayObserver::GetAllUnits, "Returns a list of all units")
.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::enum_<sc2::Difficulty>(m, "Difficulty")
......
......@@ -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
......
#include "IDAReplayObserver.h"
#include "Util.h"
#include <pybind11/pybind11.h>
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());
}
}
......@@ -32,18 +37,44 @@ void IDAReplayObserver::OnGameEnd()
void IDAReplayObserver::OnUnitDestroyed(const sc2::Unit* unit)
{
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
{
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
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();
for (auto & playerInfo : Observation()->GetGameInfo().player_info)
{
if (playerInfo.player_id == playerID)
{
return playerInfo.race_actual;
}
}
return ReplayControl()->GetReplayInfo().replay_path;
}
BOT_ASSERT(false, "Failed to find the player's race!");
return sc2::Race::Random;
sc2::GameResult IDAReplayObserver::GetResultForPlayer(int player)
{
return ReplayControl()->GetReplayInfo().players[player].game_result;
}
......@@ -12,6 +12,7 @@ class IDAReplayObserver : public sc2::ReplayObserver
{
void setUnits();
std::vector<ReplayUnit> m_allUnits;
std::set<CCUnitID> m_allUnitsID;
public:
IDAReplayObserver();
......@@ -20,12 +21,18 @@ public:
void OnStep() override;
void OnGameEnd() 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;
bool UnitExists(const CCUnitID tag) 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
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;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment