-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
353 lines (303 loc) · 11.7 KB
/
map.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Experiment with Midpoint Displacement Algorithm
// Based on Make No Wonder
// map constructor
function Map() {
// modifiable
this.tileSize = 1;
this.roughness = 10;
// constants
this.mapSize = 1024;
this.mapFraction = 2;
this.mapCanvasW = 500;
this.mapCanvasH = 500;
this.mapCanvasTileW = this.mapCanvasW / this.tileSize;
this.mapCanvasTileH = this.mapCanvasH / this.tileSize;
this.mapTileCount = this.mapSize/this.mapFraction;
this.mapTileMax = this.mapTileCount-1;
this.mapTileMultiplier = this.tileSize / this.mapFraction; // 1, 2, 4, 8
this.mapTileSquare = this.mapTileMultiplier*this.mapTileMultiplier; // 1, 4, 16, 64
this.mapCanvasHalfTiles = this.mapCanvasTileW/2;
}
// globals
var map = new Map();
var mapData = multiDimensionalArray( map.mapSize+1, map.mapSize+1 );
var mapArray;
var mapOffsetX, mapOffsetY, drawOffsetX, drawOffsetY;
// canvas
var mapCanvas = document.getElementById("mapCanvas");
var mapContext = mapCanvas.getContext("2d");
mapCanvas.width = map.mapCanvasW;
mapCanvas.height = map.mapCanvasH;
// seedMap
// starts off the map generation, seeds the first 4 corners
function seedMap(dataObject) {
var x = map.mapSize, y = map.mapSize, tr, tl, t, br, bl, b, r, l, center;
// top left
dataObject[0][0] = Math.random();
tl = dataObject[0][0];
// bottom left
dataObject[0][map.mapSize] = Math.random();
bl = dataObject[0][map.mapSize];
// top right
dataObject[map.mapSize][0] = Math.random();
tr = dataObject[map.mapSize][0];
// bottom right
dataObject[map.mapSize][map.mapSize] = Math.random();
br = dataObject[map.mapSize][map.mapSize]
// center
dataObject[map.mapSize / 2][map.mapSize / 2] = dataObject[0][0] + dataObject[0][map.mapSize] + dataObject[map.mapSize][0] + dataObject[map.mapSize][map.mapSize] / 4;
dataObject[map.mapSize / 2][map.mapSize / 2] = normalize(dataObject[map.mapSize / 2][map.mapSize / 2]);
center = dataObject[map.mapSize / 2][map.mapSize / 2];
dataObject[map.mapSize / 2][map.mapSize] = bl + br + center / 3;
dataObject[map.mapSize / 2][0] = tl + tr + center / 3;
dataObject[map.mapSize][map.mapSize / 2] = tr + br + center / 3;
dataObject[0][map.mapSize / 2] = tl + bl + center / 3;
// call displacment
midpointDisplacement(dataObject, map.mapSize);
}
// terrain generation
function midpointDisplacement(dataObject, dimension) {
var newDimension = dimension / 2, top, topRight, topLeft, bottom, bottomLeft, bottomRight, right, left, center, i, j;
if (newDimension > map.mapFraction) {
for (var i=newDimension; i<=map.mapSize; i+=newDimension) {
for (var j=newDimension; j<=map.mapSize; j+=newDimension) {
x = i - (newDimension / 2);
y = j - (newDimension / 2);
topLeft = dataObject[i - newDimension][j - newDimension];
topRight = dataObject[i][j - newDimension];
bottomLeft = dataObject[i - newDimension][j];
bottomRight = dataObject[i][j];
// center
dataObject[x][y] = (topLeft + topRight + bottomLeft + bottomRight) / 4 + displace(dimension);
dataObject[x][y] = normalize(dataObject[x][y]);
center = dataObject[x][y];
//console.log("center: " + x + " " + y);
// top
if (j - (newDimension * 2) + (newDimension / 2) > 0) {
dataObject[x][j - newDimension] = (topLeft + topRight + center + dataObject[x][j - dimension + (newDimension / 2)]) / 4 + displace(dimension);
} else {
dataObject[x][j - newDimension] = (topLeft + topRight + center) / 3+ displace(dimension);
}
dataObject[x][j - newDimension] = normalize(dataObject[x][j - newDimension]);
//console.log("top: " + x + " " + (j-newDimension));
// bottom
if (j + (newDimension / 2) < map.mapSize) {
dataObject[x][j] = (bottomLeft + bottomRight + center + dataObject[x][j + (newDimension / 2)]) / 4+ displace(dimension);
} else {
dataObject[x][j] = (bottomLeft + bottomRight + center) / 3+ displace(dimension);
}
dataObject[x][j] = normalize(dataObject[x][j]);
//console.log("bottom: " + x + " " + j);
// right
if (i + (newDimension / 2) < map.mapSize) {
dataObject[i][y] = (topRight + bottomRight + center + dataObject[i + (newDimension / 2)][y]) / 4+ displace(dimension);
} else {
dataObject[i][y] = (topRight + bottomRight + center) / 3+ displace(dimension);
}
dataObject[i][y] = normalize(dataObject[i][y]);
//console.log("right: " + i + " " + y);
// left
if (i - (newDimension * 2) + (newDimension / 2) > 0) {
dataObject[i - newDimension][y] = (topLeft + bottomLeft + center + dataObject[i - dimension + (newDimension / 2)][y]) / 4 + displace(dimension);
} else {
dataObject[i - newDimension][y] = (topLeft + bottomLeft + center) / 3+ displace(dimension);
}
dataObject[i - newDimension][y] = normalize(dataObject[i - newDimension][y]);
//console.log("left: " + (i-newDimension) + " " + y);
}
}
midpointDisplacement(dataObject, newDimension);
}
}
function generateMap() {
mapArray = multiDimensionalArray(map.mapTileCount, map.mapTileCount);
// build mapArray from mapData, round terrain values, fix terrain value spikes
for (var x=0; x<map.mapTileCount; x++) {
for (var y=0; y<map.mapTileCount; y++) {
mapArray[x][y] = Math.round(mapData[x*map.mapFraction][y*map.mapFraction]*100)/100; // round terrain values to .00 places
if (mapArray[x][y] > 1) {
mapArray[x][y] = 1;
}
}
}
}
function drawMap() {
mapContext.clearRect(0, 0, map.mapCanvasW, map.mapCanvasH);
// if view area is odd number of tiles wide and high (map.mapCanvasTileW%2 is 0 if even number of tiles, 1 if odd number of tiles)
map.mapCanvasHalfTiles = map.mapCanvasTileW/2;
if (map.mapCanvasTileW%2) {
map.mapCanvasHalfTiles = map.mapCanvasHalfTiles - 0.5;
}
mapOffsetX = Math.round(map.mapTileCount/2) - map.mapCanvasHalfTiles; // @ center
mapOffsetY = Math.round(map.mapTileCount/2) - map.mapCanvasHalfTiles; // @ center
drawOffsetX = mapOffsetX * map.tileSize;
drawOffsetY = mapOffsetY * map.tileSize;
drawTerrain();
}
function drawTerrain() {
// water, waves
mapContext.fillStyle = '#5591b0';
for (var x=0; x<=map.mapCanvasTileW; x++) {
for (var y=0; y<=map.mapCanvasTileH; y++) {
if (typeof(mapArray[x+mapOffsetX]) !== "undefined" && x+mapOffsetX < map.mapTileCount) {
if (typeof(mapArray[x+mapOffsetX][y+mapOffsetY]) !== "undefined" && y+mapOffsetY < map.mapTileCount) {
if (mapArray[x+mapOffsetX][y+mapOffsetY] <= 0.45 || (mapArray[x+mapOffsetX][y+mapOffsetY] > 3 && mapArray[x+mapOffsetX][y+mapOffsetY] < 5)) {
mapContext.fillRect(x*map.tileSize, y*map.tileSize, map.tileSize, map.tileSize);
}
}
}
}
}
// shallows
mapContext.fillStyle = '#67A0B7';
for (var x=0; x<=map.mapCanvasTileW; x++) {
for (var y=0; y<=map.mapCanvasTileH; y++) {
if (typeof(mapArray[x+mapOffsetX]) !== "undefined" && x+mapOffsetX < map.mapTileCount) {
if (typeof(mapArray[x+mapOffsetX][y+mapOffsetY]) !== "undefined" && y+mapOffsetY < map.mapTileCount) {
if (mapArray[x+mapOffsetX][y+mapOffsetY] > 0.45 && mapArray[x+mapOffsetX][y+mapOffsetY] <= 0.55) {
mapContext.fillRect(x*map.tileSize, y*map.tileSize, map.tileSize, map.tileSize);
}
}
}
}
}
// sand
mapContext.fillStyle = '#D3D1A5';
for (var x=0; x<=map.mapCanvasTileW; x++) {
for (var y=0; y<=map.mapCanvasTileH; y++) {
if (typeof(mapArray[x+mapOffsetX]) !== "undefined" && x+mapOffsetX < map.mapTileCount) {
if (typeof(mapArray[x+mapOffsetX][y+mapOffsetY]) !== "undefined" && y+mapOffsetY < map.mapTileCount) {
if (mapArray[x+mapOffsetX][y+mapOffsetY] > 0.55 && mapArray[x+mapOffsetX][y+mapOffsetY] <= 0.6) {
mapContext.fillRect(x*map.tileSize, y*map.tileSize, map.tileSize, map.tileSize);
}
}
}
}
}
// field
mapContext.fillStyle = '#91B58C';
for (var x=0; x<=map.mapCanvasTileW; x++) {
for (var y=0; y<=map.mapCanvasTileH; y++) {
if (typeof(mapArray[x+mapOffsetX]) !== "undefined" && x+mapOffsetX < map.mapTileCount) {
if (typeof(mapArray[x+mapOffsetX][y+mapOffsetY]) !== "undefined" && y+mapOffsetY < map.mapTileCount) {
if ( mapArray[x+mapOffsetX][y+mapOffsetY] > 0.6 && mapArray[x+mapOffsetX][y+mapOffsetY] <= 0.85 ) {
mapContext.fillRect(x*map.tileSize, y*map.tileSize, map.tileSize, map.tileSize);
}
}
}
}
}
// field 2
mapContext.fillStyle = '#8AAD86';
for (var x=0; x<=map.mapCanvasTileW; x++) {
for (var y=0; y<=map.mapCanvasTileH; y++) {
if (typeof(mapArray[x+mapOffsetX]) !== "undefined" && x+mapOffsetX < map.mapTileCount) {
if (typeof(mapArray[x+mapOffsetX][y+mapOffsetY]) !== "undefined" && y+mapOffsetY < map.mapTileCount) {
if ((mapArray[x+mapOffsetX][y+mapOffsetY] > 0.85 && mapArray[x+mapOffsetX][y+mapOffsetY] < 0.92) || (mapArray[x+mapOffsetX][y+mapOffsetY] === 2.4)) {
mapContext.fillRect(x*map.tileSize, y*map.tileSize, map.tileSize, map.tileSize);
}
}
}
}
}
// mountain
mapContext.fillStyle = '#8c9074';
for (var x=0; x<=map.mapCanvasTileW; x++) {
for (var y=0; y<=map.mapCanvasTileH; y++) {
if (typeof(mapArray[x+mapOffsetX]) !== "undefined" && x+mapOffsetX < map.mapTileCount) {
if (typeof(mapArray[x+mapOffsetX][y+mapOffsetY]) !== "undefined" && y+mapOffsetY < map.mapTileCount) {
if (mapArray[x+mapOffsetX][y+mapOffsetY] >= 0.92 && mapArray[x+mapOffsetX][y+mapOffsetY] < 0.99) {
mapContext.fillRect(x*map.tileSize, y*map.tileSize, map.tileSize, map.tileSize);
}
}
}
}
}
// snow cap
mapContext.fillStyle = '#8c9074';
for (var x = 0; x<=map.mapCanvasTileW; x++) {
for (var y=0; y<=map.mapCanvasTileH; y++) {
if (typeof(mapArray[x+mapOffsetX]) !== "undefined" && x+mapOffsetX < map.mapTileCount) {
if (typeof(mapArray[x+mapOffsetX][y+mapOffsetY]) !== "undefined" && y+mapOffsetY < map.mapTileCount) {
if (mapArray[x+mapOffsetX][y+mapOffsetY] >= 0.99) {
mapContext.fillRect(x*map.tileSize, y*map.tileSize, map.tileSize, map.tileSize);
}
}
}
}
}
}
// HELPER FUNCTIONS
// create a multi-dimensional array (don't need to fill array, just create it)
function multiDimensionalArray(nRows, nCols) {
var a = [nRows];
for (var i=0; i<nRows; i++) {
var b = [];
b.length = nCols;
a[i] = b;
}
return a;
}
// normalize a value to make sure it is within bounds
function normalize(value) {
if (value > 1) {
value = 1;
} else if (value < 0) {
value = 0;
}
return value;
}
// random function to offset map center
function displace(num) {
var max = num / (map.mapSize + map.mapSize) * map.roughness;
return (Math.random() - 0.5) * max;
}
//------------------------------------------------------------------
//------------------------------------------------------------------
// SEED AND START MAP
$(document).ready(function(){
var create_button = $('#create');
seedMap(mapData);
generateMap();
drawMap();
create_button.click(function(){
var roughness = parseInt($('#roughness-input').val());
if (roughness) {
map.roughness = roughness;
} else {
map.roughness = 10;
}
var altitude = parseInt($('#altitude-input').val());
console.log(altitude);
if (altitude) {
var tileSize;
switch (altitude) {
case 1 : tileSize = 10; break;
case 2 : tileSize = 5; break;
case 3 : tileSize = 4; break;
case 4 : tileSize = 2; break;
case 5 : tileSize = 1; break;
// case 6 : tileSize = 25; break;
// case 7 : tileSize = 20; break;
// case 8 : tileSize = 10; break;
// case 9 : tileSize = 5; break;
// case 10 : tileSize = 4; break;
// case 11 : tileSize = 2; break;
// case 12 : tileSize = 1; break;
}
map.tileSize = tileSize;
} else {
map.tileSize = 1;
}
seedMap(mapData);
generateMap();
drawMap();
})
create_button.hover(function(){
create_button.toggleClass('create-hover');
$('.create-text').toggleClass('create-text-hover');
}, function(){
create_button.toggleClass('create-hover');
$('.create-text').toggleClass('create-text-hover');
})
})