-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain - Copie.cpp.old
207 lines (179 loc) · 5.59 KB
/
main - Copie.cpp.old
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
#include <sc2api/sc2_api.h>
#include "sc2utils/sc2_manage_process.h"
#include <iostream>
using namespace sc2;
class Bot : public Agent {
int numberOfVespeneGeyser;
public:
virtual void OnGameStart() final {
numberOfVespeneGeyser = 0;
}
virtual void OnUnitIdle(const Unit* unit) final {
switch (unit->unit_type.ToType()) {
case UNIT_TYPEID::TERRAN_COMMANDCENTER: {
Actions()->UnitCommand(unit, ABILITY_ID::TRAIN_SCV);
break;
}
case UNIT_TYPEID::TERRAN_SCV: {
const Unit* mineral_target = FindNearestMineralPatch(unit->pos);
if (!mineral_target) {
break;
}
Actions()->UnitCommand(unit, ABILITY_ID::SMART, mineral_target);
break;
}
case UNIT_TYPEID::TERRAN_BARRACKS: {
Actions()->UnitCommand(unit, ABILITY_ID::TRAIN_MARINE);
break;
}
case UNIT_TYPEID::TERRAN_MARINE: {
if (CountUnitType(UNIT_TYPEID::TERRAN_MARINE) > 10) {
const GameInfo& game_info = Observation()->GetGameInfo();
Actions()->UnitCommand(unit, ABILITY_ID::ATTACK_ATTACK, game_info.enemy_start_locations.front());
}
break;
}
default: {
break;
}
}
}
const Unit* FindNearestMineralPatch(const Point2D& start) {
Units units = 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;
}
const Unit* FindNearestGeyser(const Point2D& start) {
Units units = 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_VESPENEGEYSER) {
float d = DistanceSquared2D(u->pos, start);
if (d < distance) {
distance = d;
target = u;
}
}
}
return target;
}
virtual void OnStep() {
const ObservationInterface* observation = Observation();
TryBuildSupplyDepot();
if(observation->GetMinerals() > 500)
TryBuildVespeneGeyser();
TryBuildBarracks();
}
size_t CountUnitType(UNIT_TYPEID unit_type) {
return Observation()->GetUnits(Unit::Alliance::Self, IsUnit(unit_type)).size();
}
bool TryBuildBarracks() {
const ObservationInterface* observation = Observation();
if (CountUnitType(UNIT_TYPEID::TERRAN_SUPPLYDEPOT) < 1) {
return false;
}
if (CountUnitType(UNIT_TYPEID::TERRAN_BARRACKS) > 2) {
return false;
}
return TryBuildStructure(ABILITY_ID::BUILD_BARRACKS);
}
bool TryBuildStructure(ABILITY_ID ability_type_for_structure, UNIT_TYPEID unit_type = UNIT_TYPEID::TERRAN_SCV) {
const ObservationInterface* observation = Observation();
// If a unit already is building a supply structure of this type, do nothing.
// Also get an scv to build the structure.
const Unit* unit_to_build = nullptr;
int already_building = 0;
Units units = observation->GetUnits(Unit::Alliance::Self);
for (const auto& unit : units) {
for (const auto& order : unit->orders) {
if (order.ability_id == ability_type_for_structure) {
already_building++;
if (already_building >= 2)
return false;
}
}
if (unit->unit_type == unit_type) {
unit_to_build = unit;
}
}
float rx = GetRandomScalar();
float ry = GetRandomScalar();
Actions()->UnitCommand(unit_to_build,
ability_type_for_structure,
Point2D(unit_to_build->pos.x + rx * 15.0f, unit_to_build->pos.y + ry * 15.0f));
return true;
}
bool TryBuildSupplyDepot() {
const ObservationInterface* observation = Observation();
// If we are not supply capped, don't build a supply depot.
if (observation->GetFoodUsed() <= observation->GetFoodCap() - 2)
return false;
// Try and build a depot. Find a random SCV and give it the order.
return TryBuildStructure(ABILITY_ID::BUILD_SUPPLYDEPOT);
}
bool TryBuildVespeneGeyser() {
const ObservationInterface* observation = Observation();
if(numberOfVespeneGeyser <= 0){
const Unit* unit_to_build = nullptr;
Units units = observation->GetUnits(Unit::Alliance::Self);
for (const auto& unit : units) {
if (unit->unit_type == UNIT_TYPEID::TERRAN_SCV) {
unit_to_build = unit;
}
}
numberOfVespeneGeyser++;
Actions()->UnitCommand(unit_to_build,
ABILITY_ID::BUILD_REFINERY,
FindNearestGeyser(unit_to_build->pos));
}
return true;
}
virtual void OnBuildingConstructionComplete(const Unit *unit) final {
const ObservationInterface* observation = Observation();
int toto = (int) (unit->unit_type.ToType());
std::cout << toto << std::endl;
switch (unit->unit_type.ToType()) {
case UNIT_TYPEID::TERRAN_REFINERY: {
Units units = observation->GetUnits(Unit::Alliance::Self);
for (const auto& unit_ : units) {
if (unit_->unit_type == UNIT_TYPEID::TERRAN_SCV) {
std::cout << "ENTER IF" << std::endl;
Actions()->UnitCommand(unit_, ABILITY_ID::HARVEST_GATHER, unit);
if (unit->assigned_harvesters >= 3) {
break;
}
}
}
break;
}
default: {
break;
}
}
}
};
int main(int argc, char* argv[]) {
Coordinator coordinator;
coordinator.LoadSettings(argc, argv);
Bot bot;
coordinator.SetParticipants({
CreateParticipant(Race::Terran, &bot),
CreateComputer(Race::Zerg)
});
coordinator.LaunchStarcraft();
coordinator.StartGame(sc2::kMapBelShirVestigeLE);
while (coordinator.Update()) {
}
return 0;
}