-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathYoAgent.cpp
89 lines (80 loc) · 2.51 KB
/
YoAgent.cpp
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
#include "YoAgent.h"
#include "UnitTypes.h"
#include "Pathing.h"
#include "Placement.h"
using namespace sc2;
using namespace sc2util;
void YoAgent::OnStep()
{
// do the updates
updateFromObservation(Observation());
// call onStep
OnYoStep();
// handle order queue
dispatchOrders();
actions->updateBusy();
}
void YoAgent::OnUnitEnterVision(const Unit * u)
{
if (u->alliance == Unit::Alliance::Enemy) {
enemies.insert_or_assign(u->tag, u);
}
if (sc2util::IsBuilding(u->unit_type)) {
auto foot = Observation()->GetAbilityData()[(int)Observation()->GetUnitTypeData()[(int)u->unit_type].ability_id].footprint_radius * 2;
setBuildingAt(info, u->pos, foot, true); // PAthing
placer.setBuildingAt(info, u->pos, foot, true);
}
}
void YoAgent::OnUnitDestroyed(const sc2::Unit * u)
{
if (u->alliance == Unit::Alliance::Enemy) {
enemies.erase(u->tag);
}
if (sc2util::IsBuilding(u->unit_type)) {
auto foot = Observation()->GetAbilityData()[(int)Observation()->GetUnitTypeData()[(int)u->unit_type].ability_id].footprint_radius * 2;
setBuildingAt(info, u->pos, foot, false);
placer.setBuildingAt(info, u->pos, foot, false);
}
}
void YoAgent::OnUnitCreated(const sc2::Unit * u)
{
if (sc2util::IsBuilding(u->unit_type)) {
auto foot = Observation()->GetAbilityData()[(int)Observation()->GetUnitTypeData()[(int)u->unit_type].ability_id].footprint_radius * 2;
setBuildingAt(info, u->pos, foot, true);
placer.setBuildingAt(info, u->pos, foot, true);
}
}
const YoAgent::UnitsMap & YoAgent::allEnemies() const
{
return enemies;
}
void YoAgent::updateFromObservation(const sc2::ObservationInterface * obs)
{
minerals = Observation()->GetMinerals();
gas = Observation()->GetVespene();
supplyleft = Observation()->GetFoodCap() - Observation()->GetFoodUsed();
}
void YoAgent::dispatchOrders()
{
std::unordered_map<sc2::Tag, const Order *> finalorders;
for (const Order & order : orders) {
auto it = finalorders.find(order.subject->tag);
if (it == finalorders.end()) {
finalorders[order.subject->tag] = ℴ
} else if ( it->second->priority >= order.priority) {
// replace order
it->second = ℴ
}
}
for (const auto & entry : finalorders) {
const Order & order = *entry.second;
if (order.tag == Order::SELF) {
Actions()->UnitCommand(order.subject, order.ability);
} else if (order.tag == Order::UNIT) {
Actions()->UnitCommand(order.subject, order.ability, order.targetU);
} else { // it's a POS order
Actions()->UnitCommand(order.subject, order.ability, order.targetP);
}
}
orders.clear();
}