ProtoBot
Loading...
Searching...
No Matches
Builder Class Reference

An instance of a Builder is created when ProtoBot's Build Manager has done the following for an approved ResourceRequest.
- Found an ideal location to place
- Found A worker that is avalible to construct the building
- Attempted to generate an AStar Path to the located. More...

#include <Builder.h>

Public Member Functions

 Builder (BWAPI::Unit unitReference, BWAPI::UnitType buildingToPlace, BWAPI::Position requestedLocation, Path path)
void onFrame ()
 onFrame is where the logic happens for a Builder to perform the clicks to travel to a location.


BWAPI::Unit getUnitReference ()
void setUnitReference (BWAPI::Unit)
void updatePath (Path &path)
 In the case a Builder is destroyed on the way to a location, ProtoBot still attempts to try and build the requested building.
This requires ProtoBot to find another suitable worker that is able to constructed the requested building.
If another worker unit is found, update the path to reflect the current position of the worker to the requestedLocation to build.
Otherwise we should not attempt the try and build this unit.

Public Attributes

BWAPI::Position requestedPositionToBuild
BWAPI::UnitType buildingToConstruct

Private Attributes

BWAPI::Unit unitReference
BWAPI::Position lastPosition
Path referencePath
size_t pathIndex = 0
int idleFrames = 0
bool debug = false

Detailed Description

An instance of a Builder is created when ProtoBot's Build Manager has done the following for an approved ResourceRequest.
- Found an ideal location to place
- Found A worker that is avalible to construct the building
- Attempted to generate an AStar Path to the located.

A Builder's sole repsonbsiblity is to take an approved building ResourceRequest and finally construct the approved building on the map.
Due to Brood War having outdated path finding algorithm, an AStar class is defined to be able to generate the clicks a Builder need's to 'optimally' travel to a location.

NOTE:
There are cases where Builder's are not erased from the Build Managers' list of Builder's and is not the correct behaviour a more advance bot should peform.
Future implementations of ProtoBot should be able to black list locations to build if we dont 'control' a certain area and adapt to find other suitable locations to build.
This prevents the reassignment of workers to suicide into the enemy just to construct one building at a location we cant build or dont control currently.

Parameters
unitReference (ally worker unit)
buildingToPlace (type of building to construct)
requestedLocation (ideal location to build)
Path (A* generated path)

Definition at line 30 of file Builder.h.

Constructor & Destructor Documentation

◆ Builder()

Builder::Builder ( BWAPI::Unit unitReference,
BWAPI::UnitType buildingToPlace,
BWAPI::Position requestedLocation,
Path path )

Definition at line 4 of file Builder.cpp.

4 :
5 unitReference(unitReference),
6 buildingToConstruct(buildingToConstruct),
7 requestedPositionToBuild(positionToBuild),
8 referencePath(path)
9{
10 if (referencePath.positions.empty() && debug) std::cout << "Path is empty to place " << buildingToConstruct << " at " << positionToBuild << "\n";
11
12 unitReference->stop();
13
14 lastPosition = unitReference->getPosition();
15}

◆ ~Builder()

Builder::~Builder ( )

Definition at line 17 of file Builder.cpp.

18{
19
20}

Member Function Documentation

◆ getUnitReference()

BWAPI::Unit Builder::getUnitReference ( )

Definition at line 94 of file Builder.cpp.

95{
96 return unitReference;
97}

◆ onFrame()

void Builder::onFrame ( )

onFrame is where the logic happens for a Builder to perform the clicks to travel to a location.

There are two cases a builder needs to consider to reach a location.
- An AStar Path was not generated
- An AStar Path was succesfully generated

[Failure]
If AStar wasnt able to generate a Path to the requestedLocation, a Builder will just use the native Brood War pathfinding to a location as a fail safe.
This will sometimes result in weird behaviour due to how bad the path finding can be but, it is better than not building something.
As a worker is traveling to a location, when a Builder is withing a CONSTRUCT_DISTANCE_THRESHOLD amount of pixels away from a building location, a call to build the buildingToConstruct is sent the StarCraft.

Besides just checking if a Builder is within a certain range of a buildingLocation. There is also a small check to prevent a Builder from idling due to Brood War.
To make sure that a worker is infact not standing still, if a Builder's unit reference is flag as idle, a Builder will 'spam' the click until a Builder is moving again.

[Success]
In the case an AStar Path was succesfully generated, we need to perform a couple different steps compared to the AStar failure case. An AStar Path is just a vector of positions a Builder needs to travel to 'optimally' arrive at a location.
To iterate through the list, we tell a Builder to move to the index of the path using the pathIndex.
The path index is way we keep track of what position we should move to until we reach the end of the Path.
When we get close to a position in the AStar generated Path change the pathIndex to the next position in the vector. Similarly, when we get close to the requestedLocation to build or we reach the end of a list, instead of commanding a worker to move, we instead tell the Builder to build.

There is also a small check to prevent a Builder from idling due to Brood War. Even in the case of a Builder using an AStar generated Path, a unit is still able to get stuck on the hitboxes of other units.

To make sure that a worker is infact not standing still due to this, if a Builder's unit reference is flag as idle, a Builder will change the pathIndex to the next position in the Path to hopefully fix this unit hitbox collision.

Definition at line 22 of file Builder.cpp.

23{
24 if(referencePath.positions.empty() == false && debug)
25 AStar::drawPath(referencePath);
26
27 if (referencePath.positions.empty())
28 {
29 if (unitReference->getDistance(requestedPositionToBuild) < CONSTRUCT_DISTANCE_THRESHOLD)
30 {
31 unitReference->build(buildingToConstruct, BWAPI::TilePosition(requestedPositionToBuild));
32 }
33
34 if (unitReference->isIdle())
35 {
36 if (buildingToConstruct.isRefinery())
37 {
38 unitReference->rightClick(BWAPI::Position(requestedPositionToBuild.x - 32, requestedPositionToBuild.y));
39 }
40 else
41 {
42 unitReference->rightClick(requestedPositionToBuild);
43 }
44 }
45
46 }
47 else
48 {
49 if(lastPosition == unitReference->getPosition())
50 {
51 idleFrames++;
52
53 if (idleFrames >= IDLE_FRAMES_BEFORE_FORCE_NEXT_POSITION) idleFrames = IDLE_FRAMES_BEFORE_FORCE_NEXT_POSITION;
54
55 if(debug) std::cout << "[Builder " << unitReference->getID() << " is idling] Idle Frames: " << idleFrames << "\n";
56 }
57 else
58 {
59 idleFrames = 0;
60 }
61 lastPosition = unitReference->getPosition();
62
63 if (idleFrames >= IDLE_FRAMES_BEFORE_FORCE_NEXT_POSITION)
64 {
65 if (debug) std::cout << "Builder Idling updating position index\n";
66 pathIndex++;
67 idleFrames = 0;
68 }
69
70 if (pathIndex >= referencePath.positions.size() || unitReference->getDistance(requestedPositionToBuild) < CONSTRUCT_DISTANCE_THRESHOLD)
71 {
72 unitReference->build(buildingToConstruct, BWAPI::TilePosition(requestedPositionToBuild));
73 if(debug) BWAPI::Broodwar->drawTextMap(unitReference->getPosition(), "BUILDING");
74 }
75 else
76 {
77 if (unitReference->getDistance(referencePath.positions.at(pathIndex)) < PATH_DISTANCE_THRESHOLD)
78 {
79 if ((pathIndex + 1) != referencePath.positions.size())
80 {
81 pathIndex++;
82 unitReference->rightClick(referencePath.positions.at(pathIndex));
83 }
84 }
85
86 //Incase unit gets stuck
87 if (unitReference->isIdle() && !(pathIndex >= referencePath.positions.size())) unitReference->rightClick(referencePath.positions.at(pathIndex));
88
89 if (debug) BWAPI::Broodwar->drawTextMap(unitReference->getPosition(), "MOVING");
90 }
91 }
92}
static void drawPath(Path path)
Draws path on map for debugging. Uses BWAPI::Broodwar->drawLineMap().

◆ setUnitReference()

void Builder::setUnitReference ( BWAPI::Unit unit)

Definition at line 99 of file Builder.cpp.

100{
101 unitReference = unit;
102}

◆ updatePath()

void Builder::updatePath ( Path & path)

In the case a Builder is destroyed on the way to a location, ProtoBot still attempts to try and build the requested building.
This requires ProtoBot to find another suitable worker that is able to constructed the requested building.
If another worker unit is found, update the path to reflect the current position of the worker to the requestedLocation to build.
Otherwise we should not attempt the try and build this unit.

Parameters
path

Definition at line 104 of file Builder.cpp.

105{
106 pathIndex = 0;
107 referencePath = path;
108}

Member Data Documentation

◆ buildingToConstruct

BWAPI::UnitType Builder::buildingToConstruct

Definition at line 43 of file Builder.h.

◆ debug

bool Builder::debug = false
private

Definition at line 39 of file Builder.h.

◆ idleFrames

int Builder::idleFrames = 0
private

Definition at line 38 of file Builder.h.

◆ lastPosition

BWAPI::Position Builder::lastPosition
private

Definition at line 34 of file Builder.h.

◆ pathIndex

size_t Builder::pathIndex = 0
private

Definition at line 37 of file Builder.h.

◆ referencePath

Path Builder::referencePath
private

Definition at line 36 of file Builder.h.

◆ requestedPositionToBuild

BWAPI::Position Builder::requestedPositionToBuild

Definition at line 42 of file Builder.h.

◆ unitReference

BWAPI::Unit Builder::unitReference
private

Definition at line 33 of file Builder.h.


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