-
Notifications
You must be signed in to change notification settings - Fork 0
/
black-magic.js
187 lines (160 loc) · 4.73 KB
/
black-magic.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
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
// credit: Grant Slatton
const ATTACK = "a";
const MOVE = "m";
const ALL_DIRECTIONS = [Direction.North, Direction.East, Direction.South, Direction.West];
const COORDS = {};
for(let x = 1; x <= 17; x++) {
for(let y = 1; y <= 17; y++) {
COORDS[new Coords(x, y)] = new Coords(x, y);
}
}
const is_legal_coordinate = c => {
if(c.x <= 0 || c.x > 17 || c.y <= 0 || c.y > 17) return false;
if(c.y <= 5-c.x) return false;
if(c.y <= c.x-13) return false;
if(c.y >= c.x+13) return false;
if(c.y >= 31-c.x) return false;
return true;
};
const score = (friends, enemies) => {
const unit_score = Object.keys(friends).length - Object.keys(enemies).length;
let health_score = 0;
for(const x in friends) {
health_score += Math.pow(friends[x], 0.5);
}
for(const x in enemies) {
health_score -= Math.pow(enemies[x], 0.5);
}
const map_score = {};
for(const x in friends) {
map_score[x] = { surround: 0, distance: 0 };
}
for(const x in enemies) {
map_score[x] = { surround: 0, distance: 0 };
}
for(const f in friends) {
const fc = COORDS[f];
for(const e in enemies) {
const d = COORDS[f].distanceTo(COORDS[e]);
const d_score = 1.0 / Math.pow(d, 2);
map_score[e].distance += d_score;
map_score[f].distance -= d_score;
if(d == 1) {
map_score[e].surround += 1;
map_score[f].surround -= 1;
}
}
}
let surround_score = 0;
let distance_score = 0;
for(const x of Object.values(map_score)) {
surround_score += Math.pow(x.surround, 2);
distance_score += Math.pow(x.distance, 2);
}
return [unit_score, surround_score, health_score, distance_score];
};
const array_cmp = (a, b) => {
for(let i = 0; i < a.length; i++) {
let x = a[i] - b[i];
if(x != 0) return x;
}
return 0;
}
const tick = (friends, enemies, actions) => {
for(const source in actions) {
const action = actions[source];
if(action == null) continue;
let [action_type, direction] = action;
if(action_type == MOVE) {
const target = COORDS[source].add(direction);
if(!(target in enemies) && !(target in friends) && is_legal_coordinate(target)) {
const obj = source in friends? friends : enemies;
obj[target] = obj[source];
delete obj[source];
}
}
}
for(const source in actions) {
const action = actions[source];
if(action == null) continue;
let [action_type, direction] = action;
if(action_type == ATTACK) {
const target = COORDS[source].add(direction);
if(target in enemies) enemies[target] -= 1;
if(target in friends) friends[target] -= 1;
}
}
for(const enemy in enemies) {
if(enemies[enemy] <= 0) delete enemies[enemy];
}
for(const friend in friends) {
if(friends[friend] <= 0) delete friends[friend];
}
};
let ACTIONS = null;
function initTurn(state) {
const friends = {}
for(const x of state.objsByTeam(state.ourTeam)) {
friends[x.coords] = x.health;
}
const enemies = {}
for(const x of state.objsByTeam(state.otherTeam)) {
enemies[x.coords] = x.health;
}
let best_actions = {};
for(const enemy in enemies) {
best_actions[enemy] = null;
let lowest_health = 1000;
for(const direction of ALL_DIRECTIONS) {
const target = COORDS[enemy].add(direction);
if(!(target in friends)) continue;
let h = friends[target];
if(h > lowest_health) continue;
lowest_health = h;
best_actions[enemy] = [ATTACK, direction];
}
}
const possible_actions = {}
for(const friend in friends) {
best_actions[friend] = null;
possible_actions[friend] = [null];
for(const direction of ALL_DIRECTIONS) {
const target = COORDS[friend].add(direction);
if(!is_legal_coordinate(target)) continue;
if(target in enemies) {
possible_actions[friend].push([ATTACK, direction]);
} else {
possible_actions[friend].push([MOVE, direction]);
}
}
}
let fs = Object.assign({}, friends);
let es = Object.assign({}, enemies);
tick(fs, es, best_actions);
let best_score = score(fs, es);
for(const f in friends) {
let actions = Object.assign({}, best_actions);
for(const a of possible_actions[f]) {
actions[f] = a;
fs = Object.assign({}, friends);
es = Object.assign({}, enemies);
tick(fs, es, actions);
let s = score(fs, es);
if(array_cmp(best_score, s) < 0) {
best_score = s;
best_actions = Object.assign({}, actions);
}
}
}
ACTIONS = best_actions;
}
function robot(state, unit) {
let best_action = ACTIONS[unit.coords];
if(best_action == null) return null;
[action_type, direction] = best_action;
if(action_type == ATTACK) {
return Action.attack(direction);
} else {
return Action.move(direction);
}
};