TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
cherrypi::ControllerBase Class Reference

Base class for controllers. More...

#include <controller.h>

Inherited by cherrypi::Controller, and cherrypi::SharedController.

Public Member Functions

 ControllerBase (Module *module)
 
virtual ~ControllerBase ()=default
 
virtual void addUnit (State *state, Unit *unit, UpcId id)
 Add a unit to this controller. More...
 
virtual void removeUnit (State *state, Unit *unit, UpcId id)
 Remove a unit from this controller. More...
 
virtual bool keepUnit (State *state, Unit *unit) const
 Decide whether to keep a unit. More...
 
virtual void step (State *state)
 Advance controller state and produce UPCs. More...
 
bool isControllingUnitWith (Unit *unit, UpcId id) const
 Checks if the controller is controlling the given unit via the given UPC ID. More...
 
virtual const char * getName () const
 A name for this Controller, for debugging purposes. More...
 

Protected Member Functions

void postUpcs (State *state)
 Posts scheduled UPCs to the Blackboard. More...
 
template<typename... Args>
void addUpc (Unit *unit, Args &&...args)
 Schedules an action (as a UPC) for the given unit which will be posted after doStep(). More...
 

Protected Attributes

Modulemodule_
 
std::unordered_map< Unit *, UpcIdunits_
 
std::unordered_map< Unit *, std::pair< UpcId, std::shared_ptr< UPCTuple > > > upcs_
 

Detailed Description

Base class for controllers.

A Controller is a mix between a Task and a Module: it has a sense of unit ownership and contains state similar to Task, and has a step() function which is used to post UPCs to Blackboard similar to Module. It is tailored to control of individual units: units can be added or removed, and per-unit UPC posting is made easy.

For using a controller, Module and Task objects still required. Module create controller instances and call step(). Tasks take care of player-wide unit allocation via Blackboard and provide source UPCs for each unit being controller.

See Controller and SharedController for usage exampels of two common controller patterns.

Constructor & Destructor Documentation

cherrypi::ControllerBase::ControllerBase ( Module module)
virtual cherrypi::ControllerBase::~ControllerBase ( )
virtualdefault

Member Function Documentation

void cherrypi::ControllerBase::addUnit ( State state,
Unit unit,
UpcId  id 
)
virtual

Add a unit to this controller.

This is usually called whenever a new Task for a controller is being cretaed. Re-implement this function if you need to update internal data structures when gaining control of units but make sure to also call the base class method.

Reimplemented in cherrypi::GathererController.

template<typename... Args>
void cherrypi::ControllerBase::addUpc ( Unit unit,
Args &&...  args 
)
inlineprotected

Schedules an action (as a UPC) for the given unit which will be posted after doStep().

virtual const char* cherrypi::ControllerBase::getName ( ) const
inlinevirtual

A name for this Controller, for debugging purposes.

Reimplemented in cherrypi::WorkerBuilderController, cherrypi::BuilderControllerBase, and cherrypi::GathererController.

bool cherrypi::ControllerBase::isControllingUnitWith ( Unit unit,
UpcId  id 
) const

Checks if the controller is controlling the given unit via the given UPC ID.

Tasks are required to call this function before calling removeUnit() when removing units from controllers.

bool cherrypi::ControllerBase::keepUnit ( State state,
Unit unit 
) const
virtual

Decide whether to keep a unit.

By default, this returns false for dead and non-allied units.

Reimplemented in cherrypi::GathererController.

void cherrypi::ControllerBase::postUpcs ( State state)
protected

Posts scheduled UPCs to the Blackboard.

UPCs can be scheduled by addUpc().

void cherrypi::ControllerBase::removeUnit ( State state,
Unit unit,
UpcId  id 
)
virtual

Remove a unit from this controller.

This is usually called from Task::update() to remove units that were assigned to other Tasks, or for which keepUnit() returns false. Re-implement this function if you need to update internal data structures when gaining control of units but make sure to also call the base class method.

Reimplemented in cherrypi::BuilderController, cherrypi::WorkerBuilderController, and cherrypi::GathererController.

void cherrypi::ControllerBase::step ( State state)
virtual

Advance controller state and produce UPCs.

This is intended to be called from Module::step() of the instantiating module. The default implementation does nothing.

Reimplemented in cherrypi::BuilderController, cherrypi::WorkerBuilderController, and cherrypi::GathererController.

Member Data Documentation

Module* cherrypi::ControllerBase::module_
protected
std::unordered_map<Unit*, UpcId> cherrypi::ControllerBase::units_
protected
std::unordered_map<Unit*, std::pair<UpcId, std::shared_ptr<UPCTuple> > > cherrypi::ControllerBase::upcs_
protected

The documentation for this class was generated from the following files: