-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_util.js
153 lines (126 loc) · 3.38 KB
/
map_util.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
(function() {
// Define direction
var UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3, LAST_D = 4;
var DIM = 10;
var NUM_PLANE = 3;
/**
* This function will tell you which units this plane will ocupy
* params: a plane
* return: a list of (x, y), where the plane will occupy
*/
function occupiedByPlane(plane) {
function rotate (fixpoint, points) {
var ret = new Array();
for (var i = 0; i < points.length; ++i){
var point = points[i];
ret.push({x: fixpoint.x - (point.y - fixpoint.y),
y: fixpoint.y + (point.x - fixpoint.x)});
}
return ret;
}
function pointsOfUpPlane(p) {
var ret = new Array();
ret.push({x: p.x, y:p.y});
for (var i = -2; i<= 2; ++i)
ret.push({x: p.x+i, y:p.y+1});
ret.push({x: p.x, y: p.y+2});
for (var i = -1; i<= 1; ++i)
ret.push({x: p.x+i, y:p.y+3});
return ret;
}
var ret = pointsOfUpPlane(plane);
var head = {
x: plane.x,
y: plane.y
};
for (var i = 0; i < plane.d; ++i)
ret = rotate(head, ret);
return ret;
}
function generateMap() {
function genZeroMap() {
var ret = new Array();
for (var i = 0; i < DIM; ++i){
ret[i] = new Array();
for (var j = 0; j < DIM; ++j)
ret[i][j] = 0;
}
return ret;
}
function getRandomPlane() {
function outOfBound(plane){
var points = occupiedByPlane(plane);
for (var i = 0; i < points.length; ++i) {
var pair = points[i];
if (pair.x < 0 || pair.x >= DIM ||
pair.y < 0 || pair.y >= DIM)
return true;
}
return false;
}
function getRandomNum(min, max){
var range = max - min;
var rand = Math.random();
return min + Math.floor(rand*range);
}
var plane;
do{
plane = {
x: getRandomNum(0, DIM),
y: getRandomNum(0, DIM),
d: getRandomNum(0, LAST_D)
};
} while(outOfBound(plane));
return plane;
}
function compatible(plane, bitmap) {
var points = occupiedByPlane(plane);
for (var i = 0; i < points.length; ++i){
var pair = points[i];
if (bitmap[pair.x][pair.y] == 1)
return false;
}
return true;
}
function drawPlaneToBitmap(plane, bitmap) {
var points = occupiedByPlane(plane);
for (var i = 0; i < points.length; ++i){
var pair = points[i];
bitmap[pair.x][pair.y] = 1;
}
}
var bitMap = genZeroMap();
var ans = new Array();
for (var i = 0; i < NUM_PLANE; ++i){
var plane;
do{
plane = getRandomPlane();
} while(!compatible(plane, bitMap));
drawPlaneToBitmap(plane, bitMap);
ans[i] = plane;
}
// Add the fucking offset
for (var i = 0; i < ans.length; ++i){
var point = ans[i];
switch(point.d){
case UP:
point.x -= 2;
break;
case RIGHT:
point.x -= 3;
point.y -= 2;
break;
case DOWN:
point.x -= 2;
point.y -= 3;
break;
case LEFT:
point.y -= 2;
break;
}
var temp = point.x; point.x = point.y; point.y = temp;
}
return ans;
};
window.generate_map = generateMap;
})();