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

fungerar ej

parent 927d2206
No related branches found
No related tags found
1 merge request!6Replays
#include "library.h"
namespace py = pybind11;
void define_replay_unit(py::module & m)
{
py::class_<UnitInformation>(m, "ReplayUnit")
.def_property_readonly("unit_type", &Unit::getType, "The :class:`library.UnitType` of the unit")
.def_property_readonly("position", &Unit::getPosition, "The :class:`library.Point2D` of the unit")
.def_property_readonly("tile_position", &Unit::getTilePosition, "The :class:`library.Point2DI` of the unit")
.def_property_readonly("hit_points", &Unit::getHitPoints)
.def_property_readonly("shields", &Unit::getShields)
.def_property_readonly("energy", &Unit::getEnergy)
.def_property_readonly("player", &Unit::getPlayer)
.def_property_readonly("id", &Unit::getID)
.def_property_readonly("build_percentage", &Unit::getBuildPercentage)
.def_property_readonly("weapon_cooldown", &Unit::getWeaponCooldown)
.def_property_readonly("is_completed", &Unit::isCompleted)
.def_property_readonly("is_being_constructed", &Unit::isBeingConstructed)
.def_property_readonly("is_cloaked", &Unit::isCloaked)
.def_property_readonly("is_flying", &Unit::isFlying)
.def_property_readonly("buffs", &Unit::buffs)
.def_property_readonly("is_alive", &Unit::isAlive)
.def_property_readonly("is_powered", &Unit::isPowered)
.def_property_readonly("is_idle", &Unit::isIdle)
.def_property_readonly("is_burrowed", &Unit::isBurrowed)
.def_property_readonly("is_valid", &Unit::isValid)
.def_property_readonly("is_training", &Unit::isTraining)
.def_property_readonly("is_blip", &Unit::isBlip)
.def_property_readonly("target", &Unit::getTarget)
.def_property_readonly("has_target", &Unit::hasTarget)
.def_property_readonly("max_hit_points", &Unit::getMaxHitPoints)
.def_property_readonly("progress", &Unit::getProgress)
.def_property_readonly("current_ability_id", &Unit::getCurrentAbilityID, "The AbilityID of currently used ability")
.def_property_readonly("facing", &Unit::getFacing)
.def_property_readonly("radius", &Unit::getRadius)
.def_property_readonly("is_carrying_minerals", &Unit::isCarryingMinerals)
.def("__hash__", [](const Unit & unit) { return std::hash<const sc2::Unit *>{}(unit.getUnitPtr()); })
.def(py::self == py::self)
.def("__repr__", [](const Unit & unit) { return "<Unit of type: '" + unit.getType().getName() + "'>"; });
}
......@@ -8,6 +8,7 @@ PYBIND11_MODULE(library, m)
define_typeenums(m);
define_unit(m);
define_replay_unit(m);
define_unittype(m);
define_util(m);
define_point(m);
......@@ -30,12 +31,6 @@ PYBIND11_MODULE(library, m)
.def("load_replay_list",&sc2::Coordinator::SetReplayPath, "replay_path"_a)
.def("add_replay_observer",&sc2::Coordinator::AddReplayObserver, "replay_observer"_a);
py::class_<sc2::ReplayObserver, PyReplayObserver>(m, "ReplayObserver")
.def(py::init())
.def("on_game_start", &sc2::ReplayObserver::OnGameStart)
.def("on_step",&sc2::ReplayObserver::OnStep)
.def("on_game_end",&sc2::ReplayObserver::OnGameEnd);
py::enum_<sc2::Race>(m, "Race")
.value("Terran", sc2::Race::Terran)
.value("Zerg", sc2::Race::Zerg)
......@@ -93,6 +88,23 @@ PYBIND11_MODULE(library, m)
.def_property_readonly("gas", &IDABot::GetGas, "How much gas we currently have")
.def_property_readonly("current_frame", &IDABot::GetCurrentFrame, "Which frame we are currently on");
py::class_<sc2::ReplayObserver>(m, "ReplayObserver")
.def(py::init())
.def("on_game_start", &sc2::ReplayObserver::OnGameStart)
.def("on_step", &sc2::ReplayObserver::OnStep)
.def("on_game_end", &sc2::ReplayObserver::OnGameEnd);
//py::class_<sc2::ReplayObserver>(m, "ReplayObserver")
//.def(py::init());
py::class_<IDAReplayObserver, PyReplayObserver, sc2::ReplayObserver>(m, "IDAReplayObserver")
.def(py::init())
.def("on_game_start", &IDAReplayObserver::OnGameStart)
.def("on_step", &IDAReplayObserver::OnStep)
.def("on_game_end", &IDAReplayObserver::OnGameEnd)
.def("get_all_units", &IDAReplayObserver::GetAllUnits, "Returns a list of all units");
py::class_<sc2::PlayerSetup>(m, "PlayerSetup");
py::enum_<sc2::Difficulty>(m, "Difficulty")
......
......@@ -3,6 +3,7 @@
#include <pybind11/pybind11.h>
#include <sc2api/sc2_api.h>
#include "../src/IDABot.h"
#include "../src/IDAReplayObserver.h"
#include <iostream>
#include <pybind11/stl.h> /* Automatic conversion from std::vector to Python lists */
#include <pybind11/operators.h> /* Convenient operator support */
......@@ -62,14 +63,15 @@ public:
};
//todo fixa!
class PyReplayObserver : public sc2::ReplayObserver
class PyReplayObserver : public IDAReplayObserver
{
public:
using IDAReplayObserver::IDAReplayObserver;
void OnGameStart() override
{
PYBIND11_OVERLOAD_NAME(
void,
sc2::ReplayObserver,
IDAReplayObserver,
"on_game_start",
OnGameStart
);
......@@ -78,7 +80,7 @@ public:
{
PYBIND11_OVERLOAD_NAME(
void,
sc2::ReplayObserver,
IDAReplayObserver,
"on_step",
OnStep
);
......@@ -89,6 +91,7 @@ public:
// to keep compilation snappy
void define_typeenums(pybind11::module & m);
void define_unit(pybind11::module & m);
void define_replay_unit(pybind11::module & m);
void define_unittype(pybind11::module &m);
void define_util(pybind11::module &m);
void define_point(pybind11::module &m);
......
#include "IDAReplayObserver.h"
#include "Util.h"
void IDAReplayObserver::setUnits()
{
std::cout << "setunits" << std::endl;
m_allUnits.clear();
Control()->GetObservation();
for (auto & unit : Observation()->GetUnits())
{
m_allUnits.push_back(UnitInformation(unit, *this));
}
}
IDAReplayObserver::IDAReplayObserver():
sc2::ReplayObserver()
{
}
void IDAReplayObserver::OnGameStart()
{
setUnits();
}
void IDAReplayObserver::OnStep()
{
setUnits();
}
const std::vector<UnitInformation>& IDAReplayObserver::GetAllUnits() const
{
return m_allUnits;
}
......@@ -4,30 +4,17 @@
#include <limits>
#include "Common.h"
#include "UnitInformation.h"
#include "MapTools.h"
#include "BaseLocationManager.h"
#include "UnitInfoManager.h"
#include "BuildingPlacer.h"
#include "TechTree.h"
#include "TechTreeImproved.h"
#include "MetaType.h"
#include "Unit.h"
class UnitInformation;
class IDAReplayObserver : public sc2::ReplayObserver
{
MapTools m_map;
BaseLocationManager m_bases;
UnitInfoManager m_unitInfo;
TechTree m_techTree;
BuildingPlacer m_buildingPlacer;
std::vector<Unit> m_allUnits;
std::vector<CCPosition> m_baseLocations;
std::vector<UnitInformation> m_allUnits;
void setUnits();
void OnError(const std::vector<sc2::ClientError> & client_errors,
const std::vector<std::string> & protocol_errors = {}) override;
public:
IDAReplayObserver();
......@@ -35,7 +22,7 @@ public:
void OnGameStart() override;
void OnStep() override;
const std::vector<Unit> & GetAllUnits() const;
const std::vector<UnitInformation> & GetAllUnits() const;
};
......@@ -18,6 +18,13 @@ Unit::Unit(const sc2::Unit * unit, IDABot & bot)
}
Unit::Unit(const sc2::Unit * unit)
: m_unit(unit)
, m_unitID(unit->tag)
{
}
const sc2::Unit * Unit::getUnitPtr() const
{
return m_unit;
......
......@@ -18,6 +18,7 @@ public:
Unit();
Unit(const sc2::Unit * unit, IDABot & bot);
Unit(const sc2::Unit * unit);
const sc2::Unit * getUnitPtr() const;
const sc2::UnitTypeID & getAPIUnitType() const;
......
#include "UnitInformation.h"
UnitInformation::UnitInformation(const sc2::Unit * unit, IDAReplayObserver & replayObserver)
: m_replayObserver(&replayObserver), Unit(unit)
{
}
const UnitType & UnitInformation::getType() const
{
//m_replayObserver->Observation()
// TODO: insert return statement here1
}
#pragma once
#include "Unit.h"
#include "IDAReplayObserver.h"
class IDAReplayObserver;
class UnitInformation: public Unit
{
mutable IDAReplayObserver * m_replayObserver;
public:
UnitInformation(const sc2::Unit * unit, IDAReplayObserver & replayObserver);
const UnitType & getType() const;
};
\ No newline at end of file
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