-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevelMap.js
213 lines (186 loc) · 5.55 KB
/
LevelMap.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
function LevelMap(width, height) {
this.width = width;
this.height = height;
this.difficulty = 15;
}
/**
* Generate the new level
*/
LevelMap.prototype.generate = function() {
this.tileMap = new Array(this.width);
for (var i = 0; i < this.width; i++) {
this.tileMap[i] = new Array(this.height);
for (var j = 0; j < this.height; j++) {
this.tileMap[i][j] = BlockType.ANY;
}
}
for (var wallX = 0; wallX < this.width; wallX++) {
for (var wallY = 0; wallY < this.height; wallY += this.height - 1) {
this.setBlockAt(new Vec2(wallX, wallY), BlockType.WALL);
}
}
for (var wallY = 0; wallY < this.height; wallY++) {
for (var wallX = 0; wallX < this.width; wallX += this.width - 1) {
this.setBlockAt(new Vec2(wallX, wallY), BlockType.WALL);
}
}
// Give it a 1 buffer away from the walls because the walls are easymode
var goalPos = new Vec2(2 + Utils.randInt(this.width - 4),
2 + Utils.randInt(this.height - 4));
this.setBlockAt(goalPos, BlockType.GOAL);
// easy n-step map
var currentPos = goalPos;
var step = 0;
var failures = 0;
while (step < this.difficulty && failures < 16000) {
var direction = Utils.randElement(Vec2.Direction.ALL);
var oppositeDirection = Vec2.getOppositeDirection(direction);
var stopPos = currentPos.add(oppositeDirection);
var stopBlock = this.getBlockAt(stopPos);
if (stopBlock === BlockType.GOAL || stopBlock === BlockType.EMPTY) {
failures++;
continue;
}
this.setBlockAt(stopPos, BlockType.WALL);
var emptyPos = this.getRandomEmptyInDirection(currentPos, direction);
if (!emptyPos || this.getBlockAt(emptyPos) === BlockType.GOAL) {
failures++;
this.setBlockAt(stopPos, stopBlock);
continue;
}
if (this.getBlockAt(emptyPos.add(direction)) === BlockType.WALL) {
failures++;
this.setBlockAt(stopPos, stopBlock);
continue;
}
this.markEmptyUntil(currentPos, emptyPos.add(direction));
currentPos = emptyPos;
step++;
failures = 0;
}
var finalBlock = this.getBlockAt(currentPos);
if (finalBlock === BlockType.GOAL) {
console.log('Somehow it wants the goal location as a block position. ' +
'This is dumb.');
}
this.setBlockAt(currentPos, BlockType.BLOCK);
this.finalize();
};
/**
* Get a random empty or ANY tile in a given direction, marking all the tiles
* as empty
*
* @param {Vec2} startPos
* @param {Vec2} direction
* @return {Vec2?} location of empty tile
*/
LevelMap.prototype.getRandomEmptyInDirection = function(startPos, direction) {
var currentPos = startPos.add(direction);
var possibilities = [];
while (true) {
var block = this.getBlockAt(currentPos);
if (block === BlockType.WALL) {
break;
}
if (block === BlockType.ANY || block === BlockType.EMPTY) {
possibilities.push(currentPos);
}
currentPos = currentPos.add(direction);
}
if (possibilities.length === 0) {
console.log('NO FUTURE');
return null;
}
console.log(possibilities.length + ' FUTURE');
return Utils.randElement(possibilities);
};
/**
* Move a block at blockPos in a direction
*
* @param {Vec2} blockPos
* @param {Vec2} direction
* @return {Vec2} new position of block
*/
LevelMap.prototype.moveBlock = function(blockPos, direction) {
var block = this.getBlockAt(blockPos);
if (block !== BlockType.BLOCK) {
throw new Error('attempt to move a non-block');
}
var finalPos = blockPos.add(direction);
while (true) {
var nextPos = finalPos.add(direction);
var block = this.getBlockAt(nextPos);
if (block === BlockType.WALL || block === BlockType.BLOCK) {
break;
}
finalPos = nextPos;
}
var finalBlock = this.getBlockAt(finalPos);
if (finalBlock === BlockType.GOAL) {
this.won = true;
}
this.setBlockAt(finalPos, BlockType.BLOCK);
this.setBlockAt(blockPos, BlockType.EMPTY);
return finalPos;
};
/**
* Finalize the generation of a level
*/
LevelMap.prototype.finalize = function() {
for (var x = 0; x < this.width; x++) {
for (var y = 0; y < this.height; y++) {
if (this.tileMap[x][y] === BlockType.ANY) {
this.tileMap[x][y] = BlockType.EMPTY;
}
}
}
};
/**
* Mark all tiles in (startPos endPos] as empty or empty-like
*
* @param {Vec2} startPos
* @param {Vec2} endPos
*/
LevelMap.prototype.markEmptyUntil = function(startPos, endPos) {
var direction = Vec2.getDirectionBetween(startPos, endPos);
var currentPos = startPos.add(direction);
var possibilities = [];
console.log('from ' + startPos.toString() + ' ' + this.getBlockAt(startPos));
console.log('to ' + endPos.toString() + ' ' + this.getBlockAt(endPos));
while (true) {
var block = this.getBlockAt(currentPos);
console.log(currentPos.x + ',' + currentPos.y + ' ? ' +
endPos.x + ',' + endPos.y);
if (block === BlockType.ANY) {
this.setBlockAt(currentPos, BlockType.EMPTY);
}
console.log(block);
if (currentPos.intEq(endPos)) {
console.log('done!');
break;
}
currentPos = currentPos.add(direction);
}
};
/**
* Return the block at a position
*
* @param {Vec2} pos
* @return {Block} Block at pos
*/
LevelMap.prototype.getBlockAt = function(pos) {
if (typeof(this.tileMap[pos.x]) === 'undefined') {
console.log('POS: ' + pos.x + ',' + pos.y);
console.log(new Error().stack);
}
return this.tileMap[pos.x][pos.y];
};
/**
* Set the block at a position
*
* @param {Vec2} pos
* @param {Block} block
*/
LevelMap.prototype.setBlockAt = function(pos, block) {
this.tileMap[pos.x][pos.y] = block;
};