-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrack.js
375 lines (360 loc) · 9.12 KB
/
track.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
* @constructor
*/
function Track()
{
this.tileMap = new Array(256); // tile contents
this.tileStep = new Array(256); // as stepped through during the race
this.roadSizeWidth = .1;
this.trackName = [ "Training Grounds",
"Indefinite Loop",
"Winding Heights",
"Gordian Knot Interchange",
"Maelstrom Speedway"
];
}
Track.prototype = {
/**
* Returns the contents of the tile at (x,y)
* -1 if out of bounds
*/
getTileAt : function(x, y)
{
return this.tileMap[(y*16+x)&255];
},
/**
* Returns the altitude at (x,y) at the given section
* If they are fractional coordinates, altitude is interpolated
*
*/
getRealAltitudeAt : function(x, y, step)
{
if (step<0 || step>=this.totalSteps) {
return -1;
}
var section = this.sections[step];
var altitude = section.z + section.steepness*[x-section.x, 0, y-section.y, 0, section.x+1-x, 0, section.y+1-y][section.dir&7];
return altitude;
},
/**
* Returns the tile step at (x,y)
* This represents, in order, the steps (or checkpoints), one for each tile
* to go through to complete a laps. It it part of a mechanism designed to
* ensure the car actually completes a lap.
* -1 if out of bounds
*/
getStepAt : function(x,y)
{
return this.tileStep[(y*16+x)&255];
},
/**
* Test if the provided coordinates are within the road or offroad
* Used for detection of car vs scenery collisions
* @param tile the index of the tile
* @param wX the world-coordinate X inside the tile - should be in [0, 1]
* @param wY the world-coordinate Y inside the tile - should be in [0, 1]
* @return { offRoad : true if collision, angle : the angle of the exerted force }
*/
testOffRoad : function(tile, wX, wY)
{
var tileAngle = Math.PI*(tile&3)/2;
var c = Math.cos(tileAngle), s = Math.sin(tileAngle);
var tX = .5+c*(wX-.5)+s*(wY-.5);
var tY = .5-s*(wX-.5)+c*(wY-.5);
var forceAngle = 0;
var offRoad = false;
switch (tile>>2) {
case 0 :
offRoad = (tY<this.roadSizeWidth || tY>1-this.roadSizeWidth);
forceAngle = Math.PI*(tY<this.roadSizeWidth?.5:1.5);
break;
case 1 :
var dist = Math.sqrt(tX*tX+tY*tY);
offRoad = (dist<this.roadSizeWidth || dist>1-this.roadSizeWidth);
forceAngle = Math.atan2(tY, tX)+(dist<this.roadSizeWidth?0:Math.PI);
break;
default:
}
return { offRoad : offRoad, angle : forceAngle+tileAngle };
},
createTrack : function(trackIndex)
{
var desc = this.getTrackDescription(trackIndex);
this.totalSteps = 0;
for (var i=0; i<256; ++i) {
this.tileMap[i]=this.tileStep[i]=-1;
}
this.sections=[];
var dX = [1, 1, 0, -1, -1, -1, 0, 1], dY = [0, 1, 1, 1, 0, -1, -1, -1];
/*
-1 = nothing
0 = straight line, to east
1 = straight line, to north
2 = straight line, to west
3 = straight line, to south
4 = 90° bend, left to bottom
5 = 90° bend, top to left
6 = 90° bend, top to right
7 = 90° bend, right to bottom
*/
var dI = [[7, -1, 4, -1, 5, -1, 6, -1], // left 90°
[], // left 45° - not implemented
[0, -1, 1, -1, 2, -1, 3, -1], // straight
[], // right 45° - not implemented
[4, -1, 5, -1, 6, -1, 7, -1] // right 90°
];
this.landscapeSeed = desc[0];
var x=desc[1], y=desc[2], currentAltitude=desc[3], dir=2, offset=4;
while (offset<desc.length) {
var angle = desc[offset];
var distance = desc[offset+1];
var deltaH = desc[offset+2];
for (var index=0; index<distance; ++index) {
var newTile = dI[2+angle][dir];
// each section in a curve, and beginning and end sections of a straight line are landmarks
// landmarks are used as target for CPU cars
this.sections.push({angle:angle, dir:dir, x:x, y:y, z:currentAltitude, tile:newTile, steepness:deltaH, landmark:(angle!=0||index==0||index==distance-1)});
this.tileMap[y*16+x] = newTile;
var step = this.tileStep[y*16+x];
if (step == -1) { // empty tile : create a track block
step = this.totalSteps++;
} else if (currentAltitude < this.sections[step].z) { // crossing, the new section is below
step = (step<<8) + this.totalSteps++;
} else { // crossing, the new section is above
step = ((this.totalSteps++)<<8) + step;
}
// for crossing, the step in the lower eight bits (step&255) is always below the bridge
// this is used in the display
this.tileStep[y*16+x] = step;
dir=(dir-angle)&7;
x+=dX[dir];
y+=dY[dir];
currentAltitude+=deltaH;
}
offset+=3;
}
},
/**
* Returns the track description for a given level.
* The description is intended to be used by Track.createTrack()
* @param level 0-based level (0 to ...)
* @return track description, false if out of bounds
*
* Format
* - float seed for the pseudorandom generator
* - three values for start X, Y and Altitude.
* - groups of three values for each block
* * angle : -2 for 90° left, -1 for 45° left, 0 for straight, 1 for 45° right, 2 for 90° right
* * distance in blocks. 2 with a 90° angle will produce a U-turn
* * delta altitude : +1 to increase 1m. Underpasses should be at least 2m high, but the slope can be several blocks long.
*/
getTrackDescription : function(level) {
return [
/*
// First test track
[.4,
2, 7, 0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