-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductionManager.cpp
More file actions
215 lines (199 loc) · 8.28 KB
/
ProductionManager.cpp
File metadata and controls
215 lines (199 loc) · 8.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <sc2api/sc2_api.h>
#include <iostream>
#include <vector>
#include "sc2utils/sc2_manage_process.h"
#include "ProductionManager.h"
#include "Bot.h"
using namespace sc2;
ProductionManager::ProductionManager() {}
void ProductionManager::OnUnitIdle(Bot* bot, const Unit* building_idle) {
idle_buildings_.push_back(building_idle);
}
void ProductionManager::Update(Bot* bot) {
// MAYBE A GOOD IMPROVMENT IN THE FUTURE :: DetermineProductionForIdle, new function : recolt need for future
// Then make some scoring between proposed actions, and then choose.
DetermineProductionForIdleBuildings(bot);
}
void ProductionManager::DetermineProductionForIdleBuildings(Bot* bot) {
// The determined production for a building is either what they produce, either if they stay idle.
// When they're not idle anymore, they're removed from idle_buildings_.
bool action_found_for_building = false;
for (int i = 0; i < idle_buildings_.size(); i++) {
action_found_for_building = false;
const Unit* building_idle = idle_buildings_[i];
switch (building_idle->unit_type.ToType())
{
case UNIT_TYPEID::TERRAN_COMMANDCENTER: {
action_found_for_building = DetermineProductionForCommandCenter(bot, building_idle);
break;
}
case UNIT_TYPEID::TERRAN_BARRACKS: {
action_found_for_building = DetermineProductionForBarracks(bot, building_idle);
break;
}
case UNIT_TYPEID::TERRAN_ENGINEERINGBAY: {
action_found_for_building = DetermineProductionForEngineeringBay(bot, building_idle);
break;
}
default:
//If unit isn't managed by this module, delete it from vector.
idle_buildings_.erase(idle_buildings_.begin() + i);
i--;
break;
}
if (action_found_for_building) {
idle_buildings_.erase(idle_buildings_.begin() + i);
i--;
}
}
}
bool ProductionManager::DetermineProductionForCommandCenter(Bot* bot, const Unit* command_center_idle) {
// Returns true if action got assigned to the building.
const int COST_OF_SCV = 50; // TODO(team) :: Find a correct architecture to store Unit_Infos.
const int SPACE_TAKEN_BY_SCV = 1; // TODO(team) :: Find a correct architecture to store Unit_Infos.
const ObservationInterface* observation = bot->Observation();
if (command_center_idle->assigned_harvesters < command_center_idle->ideal_harvesters && UnitCanBeConstructed(observation, COST_OF_SCV, 0, SPACE_TAKEN_BY_SCV)) {
bot->Actions()->UnitCommand(command_center_idle, ABILITY_ID::TRAIN_SCV);
return true;
}
return false;
// TODO(nathan) :: Add the refinery logic to the condition of TRAIN_SCV.
}
bool ProductionManager::DetermineProductionForBarracks(Bot* bot, const Unit* barracks_idle) {
// Returns true if action got assigned to the building.
const int COST_OF_MARINE = 50;
const int SPACE_TAKEN_BY_MARINE = 1;
const int COST_OF_MARAUDER = 100;
const int COST_VESPENE_OF_MARAUDER = 25;
const int SPACE_TAKEN_BY_MARAUDER = 2;
const int COST_OF_GHOST = 150;
const int COST_VESPENE_OF_GHOST = 125;
const int SPACE_TAKEN_BY_GHOST = 2;
const int COST_OF_TECH_LAB = 50;
const int COST_VESPENE_OF_TECH_LAB = 25;
const ObservationInterface* observation = bot->Observation();
if (UnitCanBeConstructed(observation, COST_OF_TECH_LAB, COST_VESPENE_OF_TECH_LAB, 0)) {
if (GetIfNeedATechLab(bot, barracks_idle)) {
std::cout << "BIDIDI" << std::endl;
bot->Actions()->UnitCommand(barracks_idle, ABILITY_ID::BUILD_TECHLAB_BARRACKS);
bot->barrackWithTechLabBuilt_.push_back(barracks_idle->pos);
return true;
}
}
if (GetIfHasATechLab(bot, barracks_idle)) {
if (UnitCanBeConstructed(observation, COST_OF_GHOST, COST_VESPENE_OF_GHOST, SPACE_TAKEN_BY_GHOST)) {
Units units = bot->Observation()->GetUnits(Unit::Alliance::Self, IsUnit(UNIT_TYPEID::TERRAN_GHOSTACADEMY));
if (units.size() > 0) {
bot->Actions()->UnitCommand(barracks_idle, ABILITY_ID::TRAIN_GHOST);
return true;
}
}
if (UnitCanBeConstructed(observation, COST_OF_MARAUDER, COST_VESPENE_OF_MARAUDER, SPACE_TAKEN_BY_MARAUDER)) {
bot->Actions()->UnitCommand(barracks_idle, ABILITY_ID::TRAIN_MARAUDER);
return true;
}
}
if (UnitCanBeConstructed(observation, COST_OF_MARINE, 0, SPACE_TAKEN_BY_MARINE)) {
bot->Actions()->UnitCommand(barracks_idle, ABILITY_ID::TRAIN_MARINE);
return true;
}
return false;
}
bool ProductionManager::GetIfNeedATechLab(Bot *bot, const Unit* barrack_idle) {
int i = 0;
for (i = 0; i < bot->barrackWithTechLab_.size(); i++) {
if (bot->barrackWithTechLab_[i].x == barrack_idle->pos.x && bot->barrackWithTechLab_[i].y == barrack_idle->pos.y) {
bot->barrackWithTechLab_.erase(bot->barrackWithTechLab_.begin() + i);
return true;
}
}
return false;
}
bool ProductionManager::GetIfHasATechLab(Bot *bot, const Unit* barrack_idle) {
int i = 0;
for (i = 0; i < bot->barrackWithTechLabBuilt_.size(); i++) {
if (bot->barrackWithTechLabBuilt_[i].x == barrack_idle->pos.x && bot->barrackWithTechLabBuilt_[i].y == barrack_idle->pos.y) {
return true;
}
}
return false;
}
bool ProductionManager::DetermineProductionForEngineeringBay(Bot* bot, const Unit* engineeringBay_idle) {
const int COST_OF_INFANTERY_WEAPON_1 = 100;
const int COST_OF_INFANTERY_WEAPON_2 = 300;
const int COST_OF_INFANTERY_WEAPON_3 = 250;
const int COST_OF_INFANTERY_ARMOR_1 = 100;
const int COST_OF_INFANTERY_ARMOR_2 = 175;
const int COST_OF_INFANTERY_ARMOR_3 = 250;
Units units_armory = bot->Observation()->GetUnits(Unit::Alliance::Self, IsUnit(UNIT_TYPEID::TERRAN_ARMORY));
bool hasArmory = false;
for (const auto& unit : units_armory) {
if (unit->unit_type == UNIT_TYPEID::TERRAN_ARMORY) {
hasArmory = true;
}
}
const ObservationInterface* observation = bot->Observation();
if (current_infantery_weapon_level >= 3 && current_infantery_armor_level >= 3) {
return true;
}
if (current_infantery_weapon_level <= current_infantery_armor_level) {
if (current_infantery_weapon_level < 1) {
if (UnitCanBeConstructed(observation, COST_OF_INFANTERY_WEAPON_1, COST_OF_INFANTERY_WEAPON_1, 0)) {
bot->Actions()->UnitCommand(engineeringBay_idle, ABILITY_ID::RESEARCH_TERRANINFANTRYWEAPONSLEVEL1);
current_infantery_weapon_level++;
return true;
}
}
else if (current_infantery_weapon_level < 2 && hasArmory) { // TODO :: Add dependency to armurerie
std::cout << "TEST" << std::endl;
if (UnitCanBeConstructed(observation, COST_OF_INFANTERY_WEAPON_2, COST_OF_INFANTERY_WEAPON_2, 0)) {
std::cout << "TEST1" << std::endl;
bot->Actions()->UnitCommand(engineeringBay_idle, ABILITY_ID::RESEARCH_TERRANINFANTRYWEAPONSLEVEL2);
current_infantery_weapon_level++;
return true;
}
}
else if (current_infantery_weapon_level < 3 && hasArmory) {
if (UnitCanBeConstructed(observation, COST_OF_INFANTERY_WEAPON_3, COST_OF_INFANTERY_WEAPON_3, 0)) {
bot->Actions()->UnitCommand(engineeringBay_idle, ABILITY_ID::RESEARCH_TERRANINFANTRYWEAPONSLEVEL3);
current_infantery_weapon_level++;
return true;
}
}
}
else {
if (current_infantery_armor_level < 1) {
if (UnitCanBeConstructed(observation, COST_OF_INFANTERY_ARMOR_1, COST_OF_INFANTERY_ARMOR_1, 0)) {
bot->Actions()->UnitCommand(engineeringBay_idle, ABILITY_ID::RESEARCH_TERRANINFANTRYARMORLEVEL1);
current_infantery_armor_level++;
return true;
}
}
else if (current_infantery_armor_level < 2 && hasArmory) { // TODO :: Add dependency to armurerie
if (UnitCanBeConstructed(observation, COST_OF_INFANTERY_ARMOR_2, COST_OF_INFANTERY_ARMOR_2, 0)) {
bot->Actions()->UnitCommand(engineeringBay_idle, ABILITY_ID::RESEARCH_TERRANINFANTRYARMORLEVEL2);
current_infantery_armor_level++;
return true;
}
}
else if (current_infantery_armor_level < 3 && hasArmory) {
if (UnitCanBeConstructed(observation, COST_OF_INFANTERY_ARMOR_3, COST_OF_INFANTERY_ARMOR_3, 0)) {
bot->Actions()->UnitCommand(engineeringBay_idle, ABILITY_ID::RESEARCH_TERRANINFANTRYARMORLEVEL3);
current_infantery_armor_level++;
return true;
}
}
}
return false;
}
bool ProductionManager::UnitCanBeConstructed(const ObservationInterface* observation, const int mineral_cost_of_unit, const int vespene_cost_of_unit, const int space_taken_by_unit) {
// TODO (team) :: this shit violate SRP.
int space_left = observation->GetFoodCap() - observation->GetFoodUsed();
if (observation->GetMinerals() < mineral_cost_of_unit || observation->GetVespene() < vespene_cost_of_unit) {
return false;
}
if (space_left < space_taken_by_unit) {
return false;
}
return true;
}