-
Notifications
You must be signed in to change notification settings - Fork 0
/
def2.rhai
123 lines (114 loc) · 2.89 KB
/
def2.rhai
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
let my_color = worker(0).color;
if "tick" in memory == false {
info("NOT SET");
memory.tick = 0;
} else {
info(`${memory.tick}`);
memory.tick += 1;
}
let row = [];
row.pad(40, true);
let matrix = [];
matrix.pad(40, row);
for w in map.workers{
matrix[w.x][w.y] = false;
}
let moves1 = [[1, 0, 1], [-1, 0, 2], [0, 1, 3], [0, -1, 4]];
let moves2 = [[0, 1, 3], [0, -1, 4], [1, 0, 1], [-1, 0, 2]];
for w in 0..8 {
let x = worker(w).x;
let y = worker(w).y;
let b = moves(moves1, x, y, my_color, matrix, map, w);
if b == 0{
let a = closest_exit(x, y,my_color, map);
b = move(x, y, a, matrix);
}
switch b{
1 => {
worker(w).move_right();
matrix[x+1][y] = false;
info(`${w} right`);
},
2 => {
worker(w).move_left();
matrix[x-1][y] = false;
info(`${w} left`);
},
3 => {
worker(w).move_up();
matrix[x][y+1] = false;
info(`${w} up`);
},
4 => {
worker(w).move_down();
matrix[x][y-1] = false;
info(`${w} down`);
},
}
}
fn moves(moves1, x, y, my_color, matrix, map, w){
let poss_pos = [];
for move in moves1 {
let x2 = x+move[0];
let y2 = y+move[1];
if valid_pos(x2, y2) && map[x2][y2] != my_color && matrix[x2][y2]{
poss_pos += move[2];
}
}
let m = [0, 0]; //0-> dist, 1-> pos
info(`WORKER ${w} POSSIBLE POS: ${len(poss_pos)}`);
for pos in poss_pos{
let pp = [[1,0] , [-1,0] , [0,1] , [0,-1]];
let min = min_dist([x + pp[pos-1][0], y + pp[pos-1][1]], map, my_color);
if min >= m[0]{
m = [min, pos];
}
}
return m[1];
}
fn min_dist(pos, map, my_color){
//distance between some position and the closest enemy square
let min = 80;
for w in map.workers{
if w.color != my_color{
let n = 0;
n = distance(pos, w);
if min > n{
min = n;
}
}
}
info(`pos: ${pos}, min distance: ${min}`);
return min;
}
fn distance(pos, w){
return (pos[0] - w.x).abs() + (pos[1] - w.y).abs();
}
fn closest_exit(x, y, my_color, map){
for n in 1..40{
for i in range(-n, n+1){
for j in range(-n, n+1){
if valid_pos(x+i, y+j) && map[x+i][y+j] != my_color && (i).abs()+(j).abs() == n{
info(`${x+i}, ${y+j}`);
return [x+i, y+j];
}
}
}
}
}
fn move(x, y, a, matrix){
if x < a[0] && matrix[x+1][y]{
return 1;
}
if x > a[0] && matrix[x-1][y]{
return 2;
}if y < a[1] && matrix[x][y+1]{
return 3;
}if y > a[1] && matrix[x][y-1]{
return 4;
}
return 0;
}
fn valid_pos(x, y){
return x <= 39 && x >= 0 && y <= 39 && y>= 0;
}