-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
610 lines (559 loc) · 17.1 KB
/
index.html
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<html><head>
<script type="text/javascript" src="util.js"></script>
<script type="text/javascript" src="creature.js"></script>
<script type="text/javascript" src="terrain.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('search', '1');
function searchComplete() {
if (imageSearch.results && imageSearch.results.length > 0) {
photo_img_elt.innerHTML = '<table border="3"><tr><td><img src="' + imageSearch.results[Math.floor(Math.random() * imageSearch.results.length)].tbUrl + '"/></td></tr></table>';
}
}
map_elt = null;
stat_hp_elt = null;
stat_photos_elt = null;
photo_img_elt = null;
msg_elt = null;
noise = 0;
score = 0;
map = [];
vision_distance = 10;
render_distance = 11;
render_size = 2 * render_distance + 1;
event_distance = 20;
loc_x = 0;
loc_y = 0;
hp = 100;
poison = 0;
photos = 0;
shotsleft = 24;
var player_los_map = [];
for (var x = 0; x <= render_size; x++) {
player_los_map.push([]);
for (var y = 0; y <= render_size; y++) {
player_los_map[x].push(' ');
}
}
function debug_map(map) {
var s = '\n';
s += ' '; for (var x = 0; x < map.length; x++) s += x % 10; s += '\n';
s += ' '; for (var x = 0; x < map.length; x++) s += 'v'; s += '\n';
for (var y = 0; y < map.length; y++) {
s += y % 10 + '>';
for (var x = 0; x < map[0].length; x++) {
s += map[x][y];
}
s += '<\n';
}
s += ' '; for (var x = 0; x < map.length; x++) s += '^'; s += '\n';
return s;
}
function bresenham_line_algorithm(start_x, start_y, end_x, end_y, fn, payload) {
var sx = end_x < start_x ? -1 : (end_x > start_x ? 1 : 0);
var sy = end_y < start_y ? -1 : (end_y > start_y ? 1 : 0);
var ax = sx * (end_x - start_x); var ay = sy * (end_y - start_y);
var cx = start_x; var cy = start_y;
var accum = 0;
while (cx != end_x || cy != end_y) {
if (ax > ay) {
cx += sx; accum += ay;
if (accum >= ax) {
accum -= ax; cy += sy;
}
}
else {
cy += sy; accum += ax;
if (accum >= ay) {
accum -= ay; cx += sx;
}
}
if (!fn(cx, cy, payload)) return;
}
}
function player_los_fn_payload(end_x, end_y) {
this.end_x = end_x; this.end_y = end_y;
this.have_vision = true;
}
function player_los_fn(x, y, payload) {
var dx = x - loc_x; var dy = y - loc_y;
var los_x = dx + render_distance, los_y = dy + render_distance;
if (!payload.have_vision) {
player_los_map[los_x][los_y] = '0';
return 1;
}
if (dx * dx + dy * dy > vision_distance * vision_distance) {
player_los_map[los_x][los_y] = '0';
payload.have_vision = false;
}
if (terrain_mapping[map[x][y]].block_los) {
// I can see the object blocking LOS but not beyond
player_los_map[los_x][los_y] = '1';
payload.have_vision = false;
}
player_los_map[los_x][los_y] = '1';
return 1;
}
function compute_player_los(target_x, target_y, los_map) {
return bresenham_line_algorithm(loc_x, loc_y, target_x, target_y,
player_los_fn, new player_los_fn_payload(target_x, target_y));
}
function player_los() {
var vision_horizon = vision_distance + 1;
// for each cell at the border, compute the LOS to that cell
for (var x = 0; x <= render_size; x++) {
for (var y = 0; y <= render_size; y++) player_los_map[x][y] = ' ';
}
for (var y = loc_y - vision_horizon; y <= loc_y + vision_horizon + 1; y++) {
compute_player_los(loc_x - vision_horizon, y, player_los_map);
compute_player_los(loc_x + vision_horizon + 1, y, player_los_map);
}
for (var x = loc_x - vision_horizon; x <= loc_x + vision_horizon; x++) {
compute_player_los(x, loc_y - vision_horizon, player_los_map);
compute_player_los(x, loc_y + vision_horizon + 1, player_los_map);
}
//los_map_elt.innerHTML = '<pre>' + debug_map(player_los_map) + '</pre>';
}
function paint_map() {
var s = '';
player_los(vision_distance);
for (var y = -render_distance; y <= render_distance; y++) {
for (var x = -render_distance; x <= render_distance; x++) {
if (!x && !y) { s += '@'; continue; }
var map_x = loc_x + x;
var map_y = loc_y + y;
if (player_los_map[x + render_distance][y + render_distance] == '0') {
s += ' ';
continue;
}
for (i = 0; i < creature_list.length; i++) {
if (creature_list[i].x == map_x
&& creature_list[i].y == map_y) {
s += creature_list[i].char; break;
}
}
if (i < creature_list.length) continue;
s += map[map_x][map_y];
}
s += '\n';
}
map_elt.innerHTML = '<pre>' + s + '</pre>';
stat_hp_elt.innerHTML = hp;
stat_photos_elt.innerHTML = photos;
stat_score_elt.innerHTML = score;
stat_shotsleft_elt.innerHTML = shotsleft;
}
function place_terrain(x, y) {
map[x][y] = pick_terrain(x, y);
}
function check_map() {
var width = map.length;
var height = width ? map[0].length : 0;
var x, y;
while(loc_x < event_distance) {
// add to the left
map.unshift([]);
for (y = 0; y < height; y++) { map[0].push(' '); }
for (y = 0; y < height; y++) place_terrain(0, y);
loc_x++;
width++;
}
while(loc_x + event_distance + 1 >= width) {
// add to the right
map.push([]);
for (y = 0; y < height; y++) { map[width].push(' '); }
for (y = 0; y < height; y++) place_terrain(width, y);
width++;
}
while(loc_y < event_distance) {
// add to the top
for (x = 0; x < width; x++) map[x].unshift(' ');
for (x = 0; x < width; x++) place_terrain(x, 0);
loc_y++;
height++;
}
while(loc_y + event_distance + 1 >= height) {
// add to bottom
for (x = 0; x < width; x++) map[x].push(' ');
for (x = 0; x < width; x++) place_terrain(x, height);
height++;
}
}
highlighted_keys = [];
interval_var = null;
function unhighlight(key) {
// msg_elt.innerHTML = '';
while (e = highlighted_keys.pop()) {
e.style.background = 'white';
}
window.clearInterval(interval_var);
}
function highlight(key) {
elt = document.getElementById(key);
elt.style.background = 'black';
highlighted_keys.push(elt);
interval_var = window.setInterval(unhighlight, 200);
}
function damage(dmg, m) {
if (!m) m = '';
hp -= Math.floor(dmg);
if (hp > 0)
msg(m,200);
else msg(m + '...YOU DIED', null);
}
function poisoncheck() {
if (poison > 0) {
damage(Math.random() * 10, 'The poison causes pain.');
poison--;
}
}
function ticktock(t) {
poisoncheck();
add_creatures();
move_creatures(t);
paint_map();
}
msg_color_hexlist = ['#000000', '#111111', '#222222', '#333333', '#444444', '#555555', '#666666', '#777777', '#888888', '#999999', '#AAAAAA', '#BBBBBB', '#CCCCCC', '#DDDDDD', '#EEEEEE', '#FFFFFF'];
msg_interval = null;
msg_color = 0;
function fade_msg() {
msg_elt.style.color = msg_color_hexlist[++msg_color];
if (msg_color == msg_color_hexlist.length - 1) {
window.clearInterval(msg_interval);
msg_interval = null;
}
}
function msg(string, fade) {
if (msg_interval) {
window.clearInterval(msg_interval);
msg_interval = null;
}
msg_elt.innerHTML = string;
msg_elt.style.color = '#000000';
msg_color = 0;
if (fade) msg_interval = window.setInterval(fade_msg, fade);
}
o_key = null;
waiting_fn = null;
function photo_bres_fn(x, y, payload) {
if (!payload.have_vision) return 0;
if (terrain_mapping[map[x][y]].block_los) {
// I can see the object blocking LOS but not beyond
payload.have_vision = false;
}
for (var i = 0; i < creature_list.length; i++) {
var c = creature_list[i];
if (c.x == x && c.y == y) {
if (c.photographed) {
if (payload.photographed.indexOf(c) < 0)
payload.photographed.push(c);
continue;
}
payload.creature = c;
return 0;
}
}
return 1;
}
function photo_bres_fn_payload() {
this.creature = null;
this.have_vision = true;
this.photographed = [];
}
function photo_direction(k) {
limit_keys(null);
msg('taking photo', 10);
waiting_fn = null;
shotsleft--;
var p = new photo_bres_fn_payload();
var closest_creature = null;
switch(k) {
case 'w':
for (var x = loc_x - vision_distance; x <= loc_x + vision_distance; x++) {
p.creature = null; p.have_vision = true;
bresenham_line_algorithm(loc_x, loc_y, x, loc_y - vision_distance, photo_bres_fn, p);
if (p.creature) {
if (closest_creature) {
var dx = loc_x - closest_creature.x;
var dy = loc_y - closest_creature.y;
var cc_dist = dx * dx + dy * dy;
dx = loc_x - p.creature.x; dy = loc_y - p.creature.y;
if (dx * dx + dy * dy < cc_dist)
closest_creature = p.creature;
} else closest_creature = p.creature;
}
}
break;
case 'a':
for (var y = loc_y - vision_distance; y <= loc_y + vision_distance; y++) {
p.creature = null; p.have_vision = true;
bresenham_line_algorithm(loc_x, loc_y, loc_x - vision_distance, y, photo_bres_fn, p);
if (p.creature) {
if (closest_creature) {
var dx = loc_x - closest_creature.x;
var dy = loc_y - closest_creature.y;
var cc_dist = dx * dx + dy * dy;
dx = loc_x - p.creature.x; dy = loc_y - p.creature.y;
if (dx * dx + dy * dy < cc_dist)
closest_creature = p.creature;
} else closest_creature = p.creature;
}
}
break;
case 's':
for (var x = loc_x - vision_distance; x <= loc_x + vision_distance; x++) {
p.creature = null; p.have_vision = true;
bresenham_line_algorithm(loc_x, loc_y, x, loc_y + vision_distance, photo_bres_fn, p);
if (p.creature) {
if (closest_creature) {
var dx = loc_x - closest_creature.x;
var dy = loc_y - closest_creature.y;
var cc_dist = dx * dx + dy * dy;
dx = loc_x - p.creature.x; dy = loc_y - p.creature.y;
if (dx * dx + dy * dy < cc_dist)
closest_creature = p.creature;
} else closest_creature = p.creature;
}
}
break;
case 'd':
for (var y = loc_y - vision_distance; y <= loc_y + vision_distance; y++) {
p.creature = null; p.have_vision = true;
bresenham_line_algorithm(loc_x, loc_y, loc_x + vision_distance, y, photo_bres_fn, p);
if (p.creature) {
if (closest_creature) {
var dx = loc_x - closest_creature.x;
var dy = loc_y - closest_creature.y;
var cc_dist = dx * dx + dy * dy;
dx = loc_x - p.creature.x; dy = loc_y - p.creature.y;
if (dx * dx + dy * dy < cc_dist)
closest_creature = p.creature;
} else closest_creature = p.creature;
}
}
break;
}
if (closest_creature) {
photos++;
var dx = closest_creature.x - loc_x;
var dy = closest_creature.y - loc_y;
var distance = Math.floor(Math.sqrt(dx * dx + dy * dy));
if (distance < 1) distance = 1; // unsure if this can ever happen
if (Math.random() * distance > 5) {
msg('Missed the shot!', null);
} else {
score += Math.floor(100 / distance);
msg('Photo of a ' + closest_creature.name + ' taken!', null);
imageSearch.execute(closest_creature.search);
closest_creature.photographed = true;
}
} else {
if (p.photographed.length == 0) {
msg('Shot wasted!', null);
score -= 10;
} else if (p.photographed.length == 1) {
msg('You already have a photo of that ' + p.photographed[0].name + '.', null);
score--;
} else if (p.photographed.length > 1) {
var m = 'You already have photos of that ';
for (var i = 0; i < p.photographed.length; i++) {
if (i) m += ', ';
m += p.photographed[i].name;
}
m += '.';
msg(m, null);
score--;
}
}
ticktock(0.1);
noise += 3;
}
function ask_photo() {
if (shotsleft) {
msg('Shoot in which direction?', null);
limit_keys('wasd');
waiting_fn = photo_direction;
} else {
msg('<click>', 200);
ticktock(0.1);
}
}
function move_do(dx, dy) {
var new_x = loc_x + dx; var new_y = loc_y + dy;
if (terrain_mapping[map[new_x][new_y]].move) {
loc_x = new_x; loc_y = new_y;
check_map();
}
}
function move_noise(x, y, mult) {
noise *= .5; // natural decay
var addl = terrain_mapping[map[x][y]].noise * mult + Math.random() * .2;
noise += addl;
if (addl > .55) msg("Crunch!", 50);
}
function move_walk(dx, dy) {
move_do(dx, dy);
move_noise(loc_x, loc_y, 1);
ticktock(1);
}
function move_crouch(dx, dy) {
move_do(dx, dy);
move_noise(loc_x, loc_y, .2);
ticktock(3);
}
function move_crawl(dx, dy) {
move_do(dx, dy);
move_noise(loc_x, loc_y, .1);
ticktock(7);
}
move = move_walk;
function crouch() {
var mm = document.getElementById("move_mode");
var ct = document.getElementById("crouch_text");
switch(move) {
case move_walk:
move = move_crouch;
ct.innerHTML = 'rawl';
mm.innerHTML = 'Crouch';
msg('You crouch.', null);
break;
case move_crouch:
move = move_crawl;
ct.innerHTML = ' walk';
mm.innerHTML = 'Crawl';
msg('You crawl.', null);
break;
case move_crawl:
move = move_walk;
ct.innerHTML = 'rouch';
mm.innerHTML = 'Walk';
msg('You walk.', null);
break;
}
ticktock(1);
}
function reload_film() {
shotsleft = 24;
msg('You load a new roll of film.', null);
noise += 3;
ticktock(10);
}
function keypress(k) {
if (hp <= 0) return;
if (waiting_fn) { return waiting_fn(k); }
if (!o_key[k].enabled) return;
switch(k) {
case 'w': highlight(k); move(0, -1); break;
case 'a': highlight(k); move(-1, 0); break;
case 's': highlight(k); move(0, 1); break;
case 'd': highlight(k); move(1, 0); break;
case 't': highlight(k); ask_photo(); break;
case 'r': highlight(k); reload_film(); break;
case 'c': highlight(k); crouch(); break;
}
}
function kp(e) {
switch(e.charCode) {
case 0: switch(e.keyCode) { // re-map arrow keys to wasd
case 37: keypress('a'); return;
case 38: keypress('w'); return;
case 39: keypress('d'); return;
case 40: keypress('s'); return;
}
case 97: keypress('a'); return;
case 99: keypress('c'); return;
case 100: keypress('d'); return;
case 114: keypress('r'); return;
case 115: keypress('s'); return;
case 116: keypress('t'); return;
case 119: keypress('w'); return;
}
}
function key_status_class(elt, enabled) {
this.elt = elt; this.enabled = enabled;
}
function enable_key(k) {
k.elt.style.color = '#000000';
k.enabled = true;
}
function disable_key(k) {
k.elt.style.color = '#CCCCCC';
k.enabled = false;
}
function limit_keys(chars) {
for (var k in o_key) {
if (!chars) { enable_key(o_key[k]); continue; }
if (chars.indexOf(k) >= 0) { enable_key(o_key[k]); continue; }
disable_key(o_key[k]);
}
}
function go() {
imageSearch = new google.search.ImageSearch();
imageSearch.setSearchCompleteCallback(document, searchComplete, null);
map_elt = document.getElementById('map');
msg_elt = document.getElementById('msg');
stat_hp_elt = document.getElementById('stat_hp');
stat_photos_elt = document.getElementById('stat_photos');
photo_img_elt = document.getElementById('photo_img');
stat_score_elt = document.getElementById('stat_score');
stat_shotsleft_elt = document.getElementById('stat_shotsleft');
o_key = {
'w': new key_status_class(document.getElementById('o_w'), true)
,'a': new key_status_class(document.getElementById('o_a'), true)
,'s': new key_status_class(document.getElementById('o_s'), true)
,'d': new key_status_class(document.getElementById('o_d'), true)
,'t': new key_status_class(document.getElementById('o_t'), true)
,'c': new key_status_class(document.getElementById('o_c'), true)
,'r': new key_status_class(document.getElementById('o_r'), true)
};
check_map();
paint_map();
document.onkeypress = kp;
}
</script>
<style type="text/css">
span.key { border-style:solid; border-width: medium; }
</style>
</head><body onload="go()">
<table border="0"><tr><td>
<div id="map">
</div>
</td><td>
<table border="1">
<tr><th>Move</th><td><span id="move_mode">Walk</span></td></tr>
<tr><th>HP</th><td><span id="stat_hp"></span></td></tr>
<tr><th>Photos</th><td><span id="stat_photos"></span></td></tr>
<tr><th>Score</th><td><span id="stat_score"></span></td></tr>
<tr><th>Shots left on this roll</th><td><span id="stat_shotsleft"></span></td></tr>
</table>
</td><td><div id="photo_img"></div></tr></table>
<table border=0>
<tr>
<td></td>
<td align="center"><div id="o_w"><span id="w" class="key" onclick=keypress("w")>W</span><br/>up</div></td>
<td></td>
<td align="center"><div id="o_r"><span id="r" class="key" onclick=keypress("r")>R</span>eplace film</div></td>
<td align="center"><div id="o_t"><span id="t" class="key" onclick=keypress("t")>T</span>ake photo</div></td>
</tr><tr>
<td align="center"><div id="o_a"><span id="a" class="key" onclick=keypress('a')>A</span><br/>left</div></td>
<td></td>
<td align="center"><div id="o_d"><span id="d" class="key" onclick=keypress('d')>D</span><br/>right</div></td>
</tr>
<tr>
<td></td>
<td align="center"><div id="o_s"><span id="s" class="key" onclick=keypress('s')>S</span><br/>down</div></td>
<td></td>
<td align="center"><div id="o_c"><span id="c" class="key" onclick=keypress('c')>C</span><span id="crouch_text">rouch</span></div></td>
</tr></table>
</div>
<div id="msg">
</div>
<br/>
<p>
<i>Goal</i>: You (@) wander around the forest (_ = bare dirt, v = grass, T and
Y = trees) and photograph creatures (c = small cat, C = big cat, s = snake,
^ = bird). The closer you are to your subject, the more points you gain.</p>
<p>
<i>And don't die.</i>
</p>
</body></html>