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

The SpenderManager is the way ProtoBot is able to reserve the resouces ProtoBot wants to spend and approve the requests of units ProtoBot is able to make.
This prevents ProtoBot from spending resoucres that ahve already been allocated to another request and allows ProtoBot to make decisions based on what it has already made. More...

#include <SpenderManager.h>

Public Member Functions

bool canAfford (int mineralPrice, int gasPrice, int currentMinerals, int currentGas)
 Given the mineral and gas cost of a unit, considering the amount of minerals and gas we mightve spent this frame, are we able to afford this unit?
int availableMinerals (std::vector< ResourceRequest > &)
 Loops through all of ProtoBot's current ResourceRequest's and checks the amount of minerals we have already reserved for each unit compared the amount of minerals StarCraft says ProtoBot currently has.
If a ResourceRequest has a State other than PendingApproval, we have already reserved the minerals for that request.
int availableGas (std::vector< ResourceRequest > &)
 Loops through all of ProtoBot's current ResourceRequest's and checks the amount of gas we have already reserved for each unit compared the amount of gas StarCraft says ProtoBot currently has.
If a ResourceRequest has a State other than PendingApproval, we have already reserved the gas for that request.
int plannedSupply (std::vector< ResourceRequest > &, BWAPI::Unitset)
 Due to buildings having two states created and completed, this requires ProtoBot to check if we have enough supply being made already to prevent ProtoBot from being supply blocked.
First ProtoBot adds up all the supply buildings and units that are approved in the ResourceRequest queue.
Second a check to all the unit ProtoBot currently is accounted for.
In StarCraft a building does not provide supply until it is in a completed state, to prevent ProtoBot from creating more supply when units are in a created but not completed state this second check prevents the case from happening.
int availableSupply ()
void printQueue ()
void onStart ()
void OnFrame (std::vector< ResourceRequest > &)
 The queue for ProtoBot's ResourceRequest's operates in a first in first out operation, this means that ProtoBot is not able to reorganize the queue in any advance ways but does provide a structed list of units to created in their requested order.

The SpenderManager simply loops through the ResourceRequest's of ProtoBot and checks what the agent is able afford. Due to prioirty not being implemented a simple first pass is ran specifically for Pylons and Worker due to the units being integral to success.

After the initial first pass the queue runs normally through the pending resource requests until we can no longer afford any more units.

Detailed Description

The SpenderManager is the way ProtoBot is able to reserve the resouces ProtoBot wants to spend and approve the requests of units ProtoBot is able to make.
This prevents ProtoBot from spending resoucres that ahve already been allocated to another request and allows ProtoBot to make decisions based on what it has already made.

Definition at line 14 of file SpenderManager.h.

Constructor & Destructor Documentation

◆ SpenderManager()

SpenderManager::SpenderManager ( )

Definition at line 5 of file SpenderManager.cpp.

6{
7
8}

Member Function Documentation

◆ availableGas()

int SpenderManager::availableGas ( std::vector< ResourceRequest > & requests)

Loops through all of ProtoBot's current ResourceRequest's and checks the amount of gas we have already reserved for each unit compared the amount of gas StarCraft says ProtoBot currently has.
If a ResourceRequest has a State other than PendingApproval, we have already reserved the gas for that request.

Parameters
ProtoBot ResourceRequests
Returns
amount of gas not reserved

Definition at line 47 of file SpenderManager.cpp.

48{
49 int currentGasCount = BWAPI::Broodwar->self()->gas();
50
51 for (const ResourceRequest &request : requests)
52 {
53 //Sanity Check
54 if (request.state == ResourceRequest::State::Accepted_Completed ||
55 request.state == ResourceRequest::State::PendingApproval) continue;
56
57 switch (request.type)
58 {
59 case ResourceRequest::Type::Unit:
60 case ResourceRequest::Type::Building:
61 currentGasCount -= request.unit.gasPrice();
62 break;
63
64 case ResourceRequest::Type::Upgrade:
65 currentGasCount -= request.upgrade.gasPrice();
66 break;
67
68 case ResourceRequest::Type::Tech:
69 currentGasCount -= request.tech.gasPrice();
70 break;
71 }
72 }
73
74 return currentGasCount;
75}

◆ availableMinerals()

int SpenderManager::availableMinerals ( std::vector< ResourceRequest > & requests)

Loops through all of ProtoBot's current ResourceRequest's and checks the amount of minerals we have already reserved for each unit compared the amount of minerals StarCraft says ProtoBot currently has.
If a ResourceRequest has a State other than PendingApproval, we have already reserved the minerals for that request.

Parameters
ProtoBot ResourceRequests
Returns
amount of minerals not reserved

Definition at line 17 of file SpenderManager.cpp.

18{
19 int currentMineralCount = BWAPI::Broodwar->self()->minerals();
20
21 for (const ResourceRequest &request : requests)
22 {
23 //Sanity Check
24 if (request.state == ResourceRequest::State::Accepted_Completed ||
25 request.state == ResourceRequest::State::PendingApproval) continue;
26
27 switch (request.type)
28 {
29 case ResourceRequest::Type::Unit:
30 case ResourceRequest::Type::Building:
31 currentMineralCount -= request.unit.mineralPrice();
32 break;
33
34 case ResourceRequest::Type::Upgrade:
35 currentMineralCount -= request.upgrade.mineralPrice();
36 break;
37
38 case ResourceRequest::Type::Tech:
39 currentMineralCount -= request.tech.mineralPrice();
40 break;
41 }
42 }
43
44 return currentMineralCount;
45}

◆ availableSupply()

int SpenderManager::availableSupply ( )

Definition at line 78 of file SpenderManager.cpp.

79{
80 //int unusedSupply = (BWAPI::Broodwar->self()->supplyTotal() - BWAPI::Broodwar->self()->supplyUsed()) / 2;
81
82 return (BWAPI::Broodwar->self()->supplyTotal() - BWAPI::Broodwar->self()->supplyUsed()) / 2;;
83}

◆ canAfford()

bool SpenderManager::canAfford ( int mineralPrice,
int gasPrice,
int currentMinerals,
int currentGas )

Given the mineral and gas cost of a unit, considering the amount of minerals and gas we mightve spent this frame, are we able to afford this unit?

Parameters
mineralPrice
gasPrice
currentMinerals (minerals we have not spent considering the minerals we reserved)
currentGas (gas we have not spent considering the gas we reserved)
Returns

Definition at line 126 of file SpenderManager.cpp.

127{
128 if (((currentMinerals - mineralPrice) >= 0) && ((currentGas - gasPrice) >= 0))
129 {
130 return true;
131 }
132
133 return false;
134}

◆ OnFrame()

void SpenderManager::OnFrame ( std::vector< ResourceRequest > & requests)

The queue for ProtoBot's ResourceRequest's operates in a first in first out operation, this means that ProtoBot is not able to reorganize the queue in any advance ways but does provide a structed list of units to created in their requested order.

The SpenderManager simply loops through the ResourceRequest's of ProtoBot and checks what the agent is able afford. Due to prioirty not being implemented a simple first pass is ran specifically for Pylons and Worker due to the units being integral to success.

After the initial first pass the queue runs normally through the pending resource requests until we can no longer afford any more units.

Parameters
ProtoBot ResourceRequest

Definition at line 143 of file SpenderManager.cpp.

144{
145 //Calculate avalible minerals to spend. This considers the minerals we plan to spend as well.
146 int currentMineralCount = availableMinerals(requests);
147 int currentGasCount = availableGas(requests);
148
149 //Need to modify spender manager to be able to consider supply usage.
150 int currentSupply = availableSupply();
151 int mineralPrice = 0;
152 int gasPrice = 0;
153 bool canAffordRequest = false;
154
155 //Pylon first pass. These units should have priority.
156 for (ResourceRequest& request : requests)
157 {
158 if (request.state == ResourceRequest::State::PendingApproval
159 && ((request.type == ResourceRequest::Type::Building && request.unit == BWAPI::UnitTypes::Protoss_Pylon) || request.type == ResourceRequest::Type::Unit && request.unit == BWAPI::UnitTypes::Protoss_Probe))
160 {
161 mineralPrice = request.unit.mineralPrice();
162 gasPrice = request.unit.gasPrice();
163
164 canAffordRequest = canAfford(mineralPrice, gasPrice, currentMineralCount, currentGasCount);
165
166 if (canAffordRequest)
167 {
168 request.state = ResourceRequest::State::Approved_InProgress;
169 request.frameRequestApproved = BWAPI::Broodwar->getFrameCount();
170 currentMineralCount -= mineralPrice;
171 currentGasCount -= gasPrice;
172 //std::cout << "Able to afford " << request.unit.getName() << " (Request Number " << request.requestNumber << ") : Setting to approved(Frame : " << request.frameRequestApproved << ")\n";
173 }
174 }
175 }
176
177 //spend money until we cant anymore.
178 for (ResourceRequest& request : requests)
179 {
180 if (request.state != ResourceRequest::State::PendingApproval) continue;
181
182 switch (request.type)
183 {
184 case ResourceRequest::Type::Unit:
185 case ResourceRequest::Type::Building:
186 mineralPrice = request.unit.mineralPrice();
187 gasPrice = request.unit.gasPrice();
188 canAffordRequest = canAfford(mineralPrice, gasPrice, currentMineralCount, currentGasCount);
189
190 //if (canAffordRequest) std::cout << "Able to afford " << request.unit.getName() << " (Request Number " << request.requestNumber << ")";
191 break;
192 case ResourceRequest::Type::Upgrade:
193 mineralPrice = request.upgrade.mineralPrice();
194 gasPrice = request.upgrade.gasPrice();
195 canAffordRequest = canAfford(mineralPrice, gasPrice, currentMineralCount, currentGasCount);
196
197 //if (canAffordRequest) std::cout << "Able to afford " << request.upgrade.getName() << " (Request Number " << request.requestNumber << ")";
198 break;
199 case ResourceRequest::Type::Tech:
200 mineralPrice = request.tech.mineralPrice();
201 gasPrice = request.tech.gasPrice();
202 canAffordRequest = canAfford(mineralPrice, gasPrice, currentMineralCount, currentGasCount);
203
204 //if (canAffordRequest) std::cout << "Able to afford " << request.tech.getName() << " (Request Number " << request.requestNumber << ")";
205 break;
206 }
207
208
209 if (canAffordRequest)
210 {
211 request.state = ResourceRequest::State::Approved_InProgress;
212 request.frameRequestApproved = BWAPI::Broodwar->getFrameCount();
213 currentMineralCount -= mineralPrice;
214 currentGasCount -= gasPrice;
215
216 //std::cout << " : Setting to approved (Frame: " << request.frameRequestApproved << ")\n";
217 }
218 else
219 {
220 break;
221 }
222 }
223}
int availableGas(std::vector< ResourceRequest > &)
Loops through all of ProtoBot's current ResourceRequest's and checks the amount of gas we have alread...
int availableMinerals(std::vector< ResourceRequest > &)
Loops through all of ProtoBot's current ResourceRequest's and checks the amount of minerals we have a...
bool canAfford(int mineralPrice, int gasPrice, int currentMinerals, int currentGas)
Given the mineral and gas cost of a unit, considering the amount of minerals and gas we mightve spent...

◆ onStart()

void SpenderManager::onStart ( )

Definition at line 138 of file SpenderManager.cpp.

139{
140
141}

◆ plannedSupply()

int SpenderManager::plannedSupply ( std::vector< ResourceRequest > & requests,
BWAPI::Unitset buildings )

Due to buildings having two states created and completed, this requires ProtoBot to check if we have enough supply being made already to prevent ProtoBot from being supply blocked.
First ProtoBot adds up all the supply buildings and units that are approved in the ResourceRequest queue.
Second a check to all the unit ProtoBot currently is accounted for.
In StarCraft a building does not provide supply until it is in a completed state, to prevent ProtoBot from creating more supply when units are in a created but not completed state this second check prevents the case from happening.

Parameters
ProtoBot ResourceRequests
ProtoBot units
Returns
amount of supply not used

Definition at line 85 of file SpenderManager.cpp.

86{
87 const int totalSupply = BWAPI::Broodwar->self()->supplyTotal() / 2; //Current supply total StarCraft notes us having.
88 const int usedSupply = BWAPI::Broodwar->self()->supplyUsed() / 2; //Used supply total StarCraft notes us having.
89
90 int plannedUsedSuply = 0; //Supply we plan on using (Units are in the build queue but have not been accepted yet.
91 int plannedSupply = 0; //Buildings we plan on constructing that provide supply.
92
93 for (const ResourceRequest &request : requests)
94 {
95 //Sanity Check
96 if (request.state == ResourceRequest::State::Accepted_Completed) continue;
97
98
99 switch (request.type)
100 {
101 case ResourceRequest::Type::Unit:
102 plannedUsedSuply += (request.unit.supplyRequired() / 2);
103 break;
104 case ResourceRequest::Type::Building:
105 plannedSupply += request.unit.supplyProvided() / 2;
106 break;
107 }
108 }
109
110 //Consider buildings that are not constructed yet for potential supply.
111 for (const BWAPI::Unit building : buildings)
112 {
113 if (!building->isCompleted())
114 {
115 plannedSupply += building->getType().supplyProvided() / 2;
116 }
117 }
118
119 /*std::cout << "Total supply (with planned building considerations) = " << (totalSupply + plannedSupply) << "\n";
120 std::cout << "Used supply (with planned unit considerations) = " << usedSupply << "\n";
121 std::cout << "StarCraft Supply Avalible = " << (totalSupply - usedSupply) << "\n";
122 std::cout << "Spender Manager Supply Avalible " << ((totalSupply + plannedSupply) - (usedSupply + plannedUsedSuply)) << "\n";*/
123 return ((totalSupply + plannedSupply) - (usedSupply + plannedUsedSuply));
124}
int plannedSupply(std::vector< ResourceRequest > &, BWAPI::Unitset)
Due to buildings having two states created and completed, this requires ProtoBot to check if we have ...

◆ printQueue()

void SpenderManager::printQueue ( )

Definition at line 10 of file SpenderManager.cpp.

11{
12
13}

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