-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoldierManager.cpp
More file actions
185 lines (161 loc) · 6.33 KB
/
SoldierManager.cpp
File metadata and controls
185 lines (161 loc) · 6.33 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
#include <sc2api/sc2_api.h>
#include <iostream>
#include <vector>
#include "sc2utils/sc2_manage_process.h"
#include "SoldierManager.h"
#include "Bot.h"
using namespace sc2;
SoldierManager::SoldierManager() {}
void SoldierManager::OnBuildingConstructionComplete(Bot* bot, const Unit* unit) {
GetNewSCVToWork(bot, unit);
GetIdleSneekySCVToHarvest(bot);
}
void SoldierManager::GetNewSCVToWork(Bot* bot, const Unit* unit) {
const Unit* nearest_mineral_patch = bot->FindNearestMineralPatch(unit->pos);
if (unit->unit_type == UNIT_TYPEID::TERRAN_COMMANDCENTER) {
bot->Actions()->UnitCommand(unit, ABILITY_ID::SMART, nearest_mineral_patch);
}
}
void SoldierManager::GetIdleSneekySCVToHarvest(Bot* bot) {
Units units = bot->Observation()->GetUnits(Unit::Alliance::Self, IsUnit(UNIT_TYPEID::TERRAN_SCV));
bool action_found;
for (const auto& unit : units) {
if (unit->orders.size() == 0) {
if (unit->pos.x > 80 || unit->pos.y > 120) {
action_found = DetermineActionForSCV(bot, unit);
}
}
}
}
void SoldierManager::OnUnitIdle(Bot* bot, const Unit* unit_idle)
{
if (std::find(idle_soldiers_.begin(), idle_soldiers_.end(), unit_idle) == idle_soldiers_.end()) {
if (unit_idle->unit_type.ToType() != UNIT_TYPEID::TERRAN_SCV) {
idle_soldiers_.push_back(unit_idle);
}
if (unit_idle->unit_type.ToType() == UNIT_TYPEID::TERRAN_MARINE) {
if (unit_idle->pos.x > 80 && unit_idle->pos.y > 120) {
number_of_sneeky_idle_marines_++;
}
else {
number_of_idle_marines_++;
}
}
else if (unit_idle->unit_type.ToType() == UNIT_TYPEID::TERRAN_MARAUDER || unit_idle->unit_type.ToType() == UNIT_TYPEID::TERRAN_GHOST) {
if (unit_idle->pos.x > 80 && unit_idle->pos.y > 120) {
number_of_sneeky_idle_marines_ += 2;
}
else {
number_of_idle_marines_ += 2;
}
}
}
}
void SoldierManager::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.
DetermineActionForIdleSoldiers(bot);
}
void SoldierManager::DetermineActionForIdleSoldiers(Bot* bot)
{
bool action_found_for_soldier = false;
bool enought_marines_for_attack = (number_of_idle_marines_ >= number_min_of_marines_to_attack_);
bool enought_sneeky_marines_for_attack = (number_of_sneeky_idle_marines_ >= number_min_of_sneeky_marines_to_attack_);
for (int i = 0; i < idle_soldiers_.size(); i++) {
action_found_for_soldier = false;
const Unit* soldier_idle = idle_soldiers_[i];
switch (soldier_idle->unit_type.ToType())
{
case UNIT_TYPEID::TERRAN_MARINE:
action_found_for_soldier = DetermineActionForMarine(bot, soldier_idle, enought_marines_for_attack, enought_sneeky_marines_for_attack);
break;
case UNIT_TYPEID::TERRAN_MARAUDER:
action_found_for_soldier = DetermineActionForMarauder(bot, soldier_idle, enought_marines_for_attack, enought_sneeky_marines_for_attack);
break;
case UNIT_TYPEID::TERRAN_GHOST:
action_found_for_soldier = DetermineActionForMarauder(bot, soldier_idle, enought_marines_for_attack, enought_sneeky_marines_for_attack);
break;
case UNIT_TYPEID::TERRAN_SCV:
action_found_for_soldier = DetermineActionForSCV(bot, soldier_idle);
break;
default:
//If unit isn't managed by this module, delete it from vector.
idle_soldiers_.erase(idle_soldiers_.begin() + i);
i--;
break;
}
if (action_found_for_soldier) {
idle_soldiers_.erase(idle_soldiers_.begin() + i);
i--;
}
}
}
bool SoldierManager::DetermineActionForGhost(Bot* bot, const Unit* marauder_idle, bool enought_marines_for_attack, bool enought_sneeky_marines_for_attack)
{
return DetermineActionForGhost(bot, marauder_idle, enought_marines_for_attack, enought_sneeky_marines_for_attack);
}
bool SoldierManager::DetermineActionForMarauder(Bot* bot, const Unit* marauder_idle, bool enought_marines_for_attack, bool enought_sneeky_marines_for_attack)
{
return DetermineActionForMarine(bot, marauder_idle, enought_marines_for_attack, enought_sneeky_marines_for_attack);
}
bool SoldierManager::DetermineActionForMarine(Bot* bot, const Unit* marine_idle, bool enought_marines_for_attack, bool enought_sneeky_marines_for_attack)
{
if (enought_marines_for_attack && (marine_idle->pos.x <= 80 || marine_idle->pos.y <= 120))
{
const GameInfo& game_info = bot->Observation()->GetGameInfo();
Point2D target_location = FindRandomLocationInArea(game_info.enemy_start_locations.front(), 8);
bot->Actions()->UnitCommand(marine_idle, ABILITY_ID::ATTACK_ATTACK, target_location);
number_of_idle_marines_--;
return true;
}
else if (enought_sneeky_marines_for_attack && marine_idle->pos.x > 80 && marine_idle->pos.y > 120)
{
const GameInfo& game_info = bot->Observation()->GetGameInfo();
Point2D target_location = FindRandomLocationInArea(game_info.enemy_start_locations.front(), 8);
bot->Actions()->UnitCommand(marine_idle, ABILITY_ID::ATTACK_ATTACK, target_location);
number_of_sneeky_idle_marines_--;
return true;
}
return false;
}
bool SoldierManager::DetermineActionForSCV(Bot* bot, const Unit* scv_idle) {
if (scv_idle->pos.x <= 80 || scv_idle->pos.y <= 120)
{
Units refineries = bot->Observation()->GetUnits(Unit::Alliance::Self, IsUnit(UNIT_TYPEID::TERRAN_REFINERY));
std::cout << refineries.size() << std::endl;
for (const auto& refinery : refineries) {
if (refinery->assigned_harvesters < refinery->ideal_harvesters) {
bot->Actions()->UnitCommand(scv_idle, ABILITY_ID::HARVEST_GATHER, refinery);
}
}
}
else if (scv_idle->pos.x > 85 && scv_idle->pos.y > 120)
{
const Unit* nearest_mineral_patch = FindNearestMineralPatch(scv_idle->pos, bot);
bot->Actions()->UnitCommand(scv_idle, ABILITY_ID::HARVEST_GATHER, nearest_mineral_patch);
return true;
}
return false;
}
const Unit* SoldierManager::FindNearestMineralPatch(const Point2D& start, Bot *bot) {
Units units = bot->Observation()->GetUnits(Unit::Alliance::Neutral);
float distance = std::numeric_limits<float>::max();
const Unit* target = nullptr;
for (const auto& u : units) {
if (u->unit_type == UNIT_TYPEID::NEUTRAL_MINERALFIELD) {
float d = DistanceSquared2D(u->pos, start);
if (d < distance) {
distance = d;
target = u;
}
}
}
return target;
}
Point2D SoldierManager::FindRandomLocationInArea(Point2D center, float radius)
{
Point2D location = center;
location.x += radius * cos(rand());
location.y += radius * sin(rand());
return location;
}