-
Notifications
You must be signed in to change notification settings - Fork 0
/
heuristic-bot.js
144 lines (126 loc) · 4.39 KB
/
heuristic-bot.js
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
// credit: Edward
const DistTo = 4;// 1-4
const Prox = 4;// 1-4
const FleeHealth = 2;//1-3
const CloseVal = 1;//1-3
const FriendlyProx = 3;//1-4
const HasEnemyD = 3; // 1-4
const hmm = '..';
const DB=false;
function robot(state, unit) {
//console.log("Hello World!");
function getObj(x,y){
return state.objByCoords(new Coords(x,y));
}
//console.log(getObj(1,3));
function run(dir){
if(DB){debug.log("trying",dir);}
let D=dir;
if(DB){debug.log("has",getObj(unit.coords.x+D.toCoords.y,unit.coords.y+D.toCoords.y));}
if(!getObj(unit.coords.x+D.toCoords.x,unit.coords.y+D.toCoords.y)){
if(DB){debug.log(""+D,getObj(unit.coords.x+D.toCoords.x,unit.coords.y+D.toCoords.y));}
return Action.move(dir);
}
D=dir.rotateCw;
if(!getObj(unit.coords.x+D.toCoords.x,unit.coords.y+D.toCoords.y)){
if(DB){debug.log(""+D,getObj(unit.coords.x+D.toCoords.x,unit.coords.y+D.toCoords.y));}
return Action.move(D);
}
D=dir.rotateCcw;
if(!getObj(unit.coords.x+D.toCoords.x,unit.coords.y+D.toCoords.y)){
if(DB){debug.log(""+D,getObj(unit.coords.x+D.toCoords.x,unit.coords.y+D.toCoords.y));}
return Action.move(D);
}
return Action.move(dir);
}
function flee(dir){
if(DB){debug.log("trying",dir);}
let D=dir;
if(DB){debug.log("has",getObj(unit.coords.x+D.toCoords.y,unit.coords.y+D.toCoords.x));}
if(!getObj(unit.coords.x+D.toCoords.x,unit.coords.y+D.toCoords.y)){
if(DB){debug.log(""+D,getObj(unit.coords.x+D.toCoords.x,unit.coords.y+D.toCoords.y));}
return Action.move(dir);
}
D=dir.rotateCw;
if(!getObj(unit.coords.x+D.toCoords.x,unit.coords.y+D.toCoords.y)){
if(DB){debug.log(""+D,getObj(unit.coords.x+D.toCoords.x,unit.coords.y+D.toCoords.y));}
return Action.move(D);
}
D=dir.rotateCcw;
if(!getObj(unit.coords.x+D.toCoords.x,unit.coords.y+D.toCoords.y)){
if(DB){debug.log(""+D,getObj(unit.coords.x+D.toCoords.x,unit.coords.y+D.toCoords.y));}
return Action.move(D);
}
return Action.attack(dir.opposite);
}
let enemies = state.objsByTeam(state.otherTeam)
if(enemies){
let closestEnemy = _.minBy(enemies,
e => e.coords.distanceTo(unit.coords) + e.health/10
)
let direction = unit.coords.directionTo(closestEnemy.coords);
if(unit.health<FleeHealth){
if(DB){debug.log("tas","flee");}
if(closestEnemy.health<unit.health){
return Action.attack(direction);
}
return flee(direction.opposite);
}
if(unit.coords.distanceTo(closestEnemy.coords) < DistTo) {
let closeGuys=0;
for(let i=-Prox;i<=Prox;i++){
for(let j=-Prox;j<=Prox;j++){
let obj=getObj(closestEnemy.coords.x+i,closestEnemy.coords.y+j);
if(obj&&obj.team==unit.team){
closeGuys++;
}
}
}
if(closeGuys<CloseVal){
if(DB){debug.log("tas","flee");}
return run(direction.opposite);
}
if(unit.coords.distanceTo(closestEnemy.coords) === 1) {
return Action.attack(direction);
} else {
if(DB){debug.log("task","attack");}
return run(direction);
}
}
let friends = state.objsByTeam(state.ourTeam);
let closestFriend = _.minBy(friends,
f =>{
let nearbyEnemey=false;
let enemies = state.objsByTeam(state.otherTeam)
let closestEnemy = _.minBy(enemies,
e => e.coords.distanceTo(f.coords) + e.health/10
)
if(f.coords.distanceTo(closestEnemy.coords)>FriendlyProx){
return Infinity;
}
return f.coords.distanceTo(unit.coords);
}
)
if(closestFriend){
let enemies = state.objsByTeam(state.otherTeam)
let closestEnemy = _.minBy(enemies,
e => e.coords.distanceTo(closestFriend.coords) + e.health/10
)
if(closestFriend.coords.distanceTo(closestEnemy.coords)<HasEnemyD){
if(DB){debug.log("task","friend");}
return run(unit.coords.directionTo(closestEnemy.coords));
}
}
}
let to = unit.coords.directionTo(new Coords(10,10));
let inward = getObj(unit.coords.x+to.toCoords.x,unit.coords.y+to.toCoords.y);
if(!getObj(unit.coords.x+to.opposite.toCoords.x,unit.coords.y+to.opposite.toCoords.y)){
if(DB){debug.log("task","out");}
return run(unit.coords.directionTo(new Coords(10,10)).opposite);
}
if(!inward){
return Action.attack(to);
}
if(DB){debug.log("task","do nothing");}
return null;
}