-
Notifications
You must be signed in to change notification settings - Fork 6
/
chlorine_renderer.html
1384 lines (1034 loc) · 37 KB
/
chlorine_renderer.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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>Chlorine</title>
<style>
body {
margin: 0;
border: 0;
padding: 0;
background-color: black;
color: white;
user-select: none;
cursor: default;
overflow: hidden;
}
#info_line {
margin: 10px; /* calculation below of info_line_height total size relies on this */
border: 0;
padding: 0;
overflow: hidden;
}
</style>
</head>
<body>
<table style="width: calc(100% - 20px);" id="info_line"><tr>
<td style="width: 80%" id="unitbox">Starting up...</td>
<td align="right" id="measurebox"></td>
<td align="right" id="turnbox"></td>
</tr></table>
<canvas id="canvas" width="0" height="0"></canvas>
<script>
"use strict";
const colours = ["#eeaaee", "#63ceca", "#ffbe00", "#c5ec98"];
const dark_colours = ["#9b6c9b", "#336a66", "#a17b00", "#708a51"];
const ranks = ["..", "1st", "2nd", "3rd", "4th"];
const PLANET_LINE_THICKNESS = 4;
const alert = require("./modules/alert");
const fs = require("fs");
const ipcRenderer = require("electron").ipcRenderer;
const path = require("path");
const stream = require("stream");
const zstd = require("node-zstandard");
const info_line = document.getElementById("info_line");
const unitbox = document.getElementById("unitbox");
const measurebox = document.getElementById("measurebox");
const turnbox = document.getElementById("turnbox");
const canvas = document.getElementById("canvas");
const context = canvas.getContext("2d");
const info_line_height = info_line.offsetHeight + 20; // relies on #info_line having margin of 10
function dist(x1, y1, x2, y2) {
let dx = x2 - x1;
let dy = y2 - y1;
return Math.sqrt(dx * dx + dy * dy);
}
function deg_to_rad(d) {
return d / 180 * Math.PI;
}
function projection(x1, y1, distance, degrees) { // Given a coordinate, a distance and an angle, find a new coordinate.
if (distance === 0) {
return [x1, y1];
}
let radians = deg_to_rad(degrees);
let x2 = distance * Math.cos(radians) + x1;
let y2 = distance * Math.sin(radians) + y1;
return [x2, y2];
}
function event_sorter(a, b) {
if (a.entity.owner === undefined) { a.entity.owner = -1; }
if (b.entity.owner === undefined) { b.entity.owner = -1; }
if (a.entity.owner < b.entity.owner) { return -1; }
if (a.entity.owner === b.entity.owner) {
if (a.entity.id < b.entity.id) { return -1; }
return 1;
}
return 1;
}
function make_renderer() {
let renderer = Object.create(null);
renderer.filename = "";
renderer.game = null;
renderer.turn = 0;
renderer.buggy = false;
renderer.tracking_type = null;
renderer.tracking_id = -1;
renderer.tracking_turn = -1;
renderer.spawn_counts = null;
renderer.weapon_ranges = false;
renderer.tactical_ranges_0 = false;
renderer.tactical_ranges_1 = false;
renderer.tactical_ranges_2 = false;
renderer.tactical_ranges_3 = false;
renderer.docking_ranges = false;
renderer.show_planets = true;
renderer.tails = true;
renderer.pointers = false;
renderer.deaths = true;
renderer.crosshair = true;
renderer.precise = false;
renderer.angle_messages = true;
renderer.dragging = false;
renderer.cameraZoom = 1.0;
renderer.cameraPosition = [0, 0];
renderer.dragStartCameraPosition = [0, 0];
renderer.measure_entity = null;
try {
renderer.message_lookups = JSON.parse(fs.readFileSync(path.join(__dirname, "messages.json")));
} catch(err) {
renderer.message_lookups = [];
}
renderer.open = (filename) => {
console.log(`Trying to load ${filename}`);
let was_plain_json = false;
try {
let contents = fs.readFileSync(filename);
renderer.game = JSON.parse(contents);
was_plain_json = true;
}
catch (err) {}
if (was_plain_json) {
renderer.finish_load(filename);
return;
}
let all_chunks = [];
let loading_stream = new stream.Writable();
loading_stream._write = (chunk, encoding, done) => {
all_chunks.push(chunk.toString());
done();
};
zstd.decompressFileToStream(filename, loading_stream, (err, result) => {
if (err) {
alert("Couldn't load this file");
console.log("Error 1");
console.log(err);
}
result.on("error", (err) => {
alert("Couldn't load this file");
console.log("Error 2");
console.log(err);
});
result.on("finish", () => {
try {
renderer.game = JSON.parse(all_chunks.join(""));
} catch(err) {
console.log("Error 3");
console.log(err);
return;
}
renderer.finish_load(filename);
});
});
};
renderer.finish_load = (filename) => {
renderer.filename = filename;
renderer.buggy = false;
renderer.turn = 0;
renderer.tracking_type = null;
unitbox.innerHTML = "No selection.";
renderer.count_spawns();
renderer.count_self_collisions();
renderer.draw();
};
renderer.count_self_collisions = () => {
renderer.self_collision_counts = [];
// We make an array of dimension <players> * <turns> to
// hold how many times a player crashed their own ships together.
for (let p = 0; p < renderer.game.num_players; p++) {
renderer.self_collision_counts.push([0]);
}
for (let n = 0; n < renderer.game.frames.length; n++) {
for (let p = 0; p < renderer.self_collision_counts.length; p++) {
renderer.self_collision_counts[p].push(renderer.self_collision_counts[p][n]);
}
let map = renderer.events_map(n);
if (map.destroyed) {
let destructions_by_time = {};
for (let n = 0; n < map.destroyed.length; n++) {
let event = map.destroyed[n];
if (destructions_by_time[event.time] === undefined) {
destructions_by_time[event.time] = [];
}
destructions_by_time[event.time].push(event);
}
for (let t in destructions_by_time) {
if (destructions_by_time.hasOwnProperty(t) === false) {
continue;
}
for (let a = 0; a < destructions_by_time[t].length; a++) {
for (let b = a + 1; b < destructions_by_time[t].length; b++) {
let x1 = destructions_by_time[t][a].x;
let y1 = destructions_by_time[t][a].y;
let x2 = destructions_by_time[t][b].x;
let y2 = destructions_by_time[t][b].y;
if (dist(x1, y1, x2, y2) <= 1.001) { // I've seen slightly > 1
if (destructions_by_time[t][a].entity.owner === destructions_by_time[t][b].entity.owner) {
let p = destructions_by_time[t][a].entity.owner;
renderer.self_collision_counts[p][n + 1] += 1;
}
}
}
}
}
}
}
};
renderer.count_spawns = () => {
renderer.spawn_counts = [];
// We make an array of dimension <players> * <turns> to
// hold how many ships a player has ever made at each turn.
for (let n = 0; n < renderer.game.num_players; n++) {
renderer.spawn_counts.push([0]);
}
// Count initial ships... (will be 3 in a normal replay)
let frame = renderer.game.frames[0];
for (let pid in frame.ships) {
if (frame.ships.hasOwnProperty(pid) === false) {
continue;
}
for (let sid in frame.ships[pid]) {
if (frame.ships[pid].hasOwnProperty(sid) === false) {
continue;
}
renderer.spawn_counts[pid][0]++;
}
}
// Count spawn events...
for (let n = 0; n < renderer.game.frames.length; n++) {
for (let p = 0; p < renderer.spawn_counts.length; p++) {
renderer.spawn_counts[p].push(renderer.spawn_counts[p][n]);
}
let frame = renderer.game.frames[n];
let events = frame.events;
if (events !== undefined) {
for (let i = 0; i < events.length; i++) {
let event = events[i];
if (event.event === "spawned") {
renderer.spawn_counts[event.entity.owner][n + 1]++;
}
}
}
}
};
renderer.save = (filename) => {
if (!filename || !renderer.game) {
return;
}
fs.writeFileSync(filename, JSON.stringify(renderer.game, null, "\t"));
};
renderer.save_frame = (filename) => {
if (!filename || !renderer.game) {
return;
}
fs.writeFileSync(filename, JSON.stringify(renderer.game.frames[renderer.turn], null, "\t"));
};
renderer.save_moves = (filename) => {
if (!filename || !renderer.game) {
return;
}
let s = "";
let moves = renderer.game.moves[renderer.turn];
if (moves) {
s = JSON.stringify(moves, null, "\t");
}
fs.writeFileSync(filename, s);
};
renderer.forward = (n) => { // Negative arguments are allowed (go back).
if (!renderer.game) {
return;
}
renderer.turn += n;
renderer.measure_reset();
if (renderer.turn < 0) {
renderer.turn = 0;
}
if (renderer.turn >= renderer.game.frames.length) {
renderer.turn = renderer.game.frames.length - 1;
}
renderer.draw();
};
renderer.go_to_next_event = () => {
renderer.measure_reset();
while (1) {
renderer.turn++
if (renderer.turn >= renderer.game.frames.length) {
renderer.turn = renderer.game.frames.length - 1;
break;
}
if (renderer.game.frames[renderer.turn]["events"] !== undefined) {
if (renderer.game.frames[renderer.turn]["events"].length > 0) {
break;
}
}
}
renderer.draw();
};
renderer.toggle = (varname) => {
renderer[varname] = !renderer[varname];
renderer.draw();
};
renderer.select_ship = (id) => {
renderer.tracking_type = "ship";
renderer.tracking_id = id;
renderer.tracking_turn = renderer.turn;
renderer.measure_reset(); // FIXME: should actually measure
renderer.draw();
};
renderer.select_planet = (id) => {
renderer.tracking_type = "planet";
renderer.tracking_id = id.toString(); // FIXME: the code below implicitly assumes these things are strings
renderer.tracking_turn = renderer.turn;
renderer.measure_reset(); // FIXME: should actually measure
renderer.draw();
};
renderer.draw = () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight - info_line_height;
if (!renderer.game) {
return;
}
let scale = Math.min(canvas.width / renderer.game.width, canvas.height / renderer.game.height) * renderer.cameraZoom;
context.clearRect(0, 0, canvas.width, canvas.height);
context.save();
context.translate(-renderer.cameraPosition[0] * scale, -renderer.cameraPosition[1] * scale);
renderer.display_tracked();
renderer.draw_planets();
renderer.draw_ships();
renderer.draw_events();
let turn_string = `${renderer.turn} / ${renderer.game.frames.length - 1}`;
document.title = turn_string + " --- " + renderer.game.player_names.join(" / ");
turnbox.innerHTML = turn_string;
renderer.send_info();
renderer.send_events();
context.restore();
};
renderer.display_tracked_planet = (planet) => {
let precision = renderer.precise ? 6 : 2;
let owner_string = (planet.owner === null) ? "Unowned" : renderer.game.player_names[planet.owner];
let colour = (planet.owner === null) ? "white" : colours[planet.owner];
let dock_string = `${planet.docked_ships.length} / ${planet.docking_spots} docked`;
unitbox.innerHTML = ` Planet ${planet.id} (<span style="color: ${colour}">${owner_string}</span>)`;
unitbox.innerHTML += ` — [ ${planet.x.toFixed(precision)} , ${planet.y.toFixed(precision)} ]`;
unitbox.innerHTML += ` — ${planet.health} hp — ${dock_string} — ${planet.r.toFixed(precision)} radius`;
if (renderer.crosshair) {
renderer.draw_crosshair(planet);
}
};
renderer.display_tracked_ship = (ship) => {
let precision = renderer.precise ? 6 : 2;
let owner_string = renderer.game.player_names[ship.owner];
let colour = colours[ship.owner];
unitbox.innerHTML = `Ship ${ship.id} (<span style="color: ${colour}">${owner_string}</span>)`;
unitbox.innerHTML += ` — [ ${ship.x.toFixed(precision)} , ${ship.y.toFixed(precision)} ] — ${ship.health} hp`;
if (ship.docking.status !== "undocked" && !(ship.docking.status === "undocking" && ship.docking.turns_left === 1)) {
unitbox.innerHTML += ` — <span style="color: ${colour}">${ship.docking.status}</span>`;
} else {
let moves = renderer.game.moves[renderer.turn];
if (moves) {
let command = moves[ship.owner][0][ship.id];
if (command) {
let speed = command.magnitude;
let angle = command.angle;
if (speed !== undefined && angle !== undefined) {
let angle_string = angle.toString();
if (renderer.angle_messages && angle >= 360) {
let message = Math.floor(angle / 360) - 1;
angle_string = `${angle % 360} (msg: ${message})`; // Just show number, if not in file.
for (let n = 0; n < renderer.message_lookups.length; n++) {
let lookups = renderer.message_lookups[n];
if (lookups["name"] === undefined) {
continue;
}
if (owner_string.toLowerCase().indexOf(lookups["name"].toLowerCase()) !== -1) {
if (lookups[message.toString()] !== undefined) {
message = `"${lookups[message.toString()]}"`;
angle_string = `${angle % 360} (msg: ${message})`;
break;
}
}
}
}
unitbox.innerHTML += ` — next: <span style="color: ${colour}">thrust ${speed} / ${angle_string}</span>`;
} else {
unitbox.innerHTML += ` — next: <span style="color: ${colour}">${command.type}</span>`;
}
}
}
}
if (renderer.crosshair) {
renderer.draw_crosshair(ship);
}
};
renderer.draw_crosshair = (entity) => {
let scale = Math.min(canvas.width / renderer.game.width, canvas.height / renderer.game.height) * renderer.cameraZoom;
context.setLineDash([5, 15]);
context.lineWidth = 1;
context.strokeStyle = "gray";
context.beginPath();
context.moveTo(entity.x * scale, entity.y * scale);
context.lineTo(entity.x * scale, 0);
context.stroke();
context.beginPath();
context.moveTo(entity.x * scale, entity.y * scale);
context.lineTo(entity.x * scale, canvas.height * scale);
context.stroke();
context.beginPath();
context.moveTo(entity.x * scale, entity.y * scale);
context.lineTo(0, entity.y * scale);
context.stroke();
context.beginPath();
context.moveTo(entity.x * scale, entity.y * scale);
context.lineTo(canvas.width * scale, entity.y * scale);
context.stroke();
context.setLineDash([]);
};
renderer.display_tracked = () => {
if (renderer.tracking_type === null) {
unitbox.innerHTML = "No selection.";
return;
}
let frame = renderer.game.frames[renderer.turn];
if (renderer.tracking_type === "ship") {
for (let pid in frame.ships) {
if (frame.ships.hasOwnProperty(pid) === false) {
continue;
}
if (frame.ships[pid][renderer.tracking_id] !== undefined) {
renderer.display_tracked_ship(frame.ships[pid][renderer.tracking_id]);
return;
}
}
} else if (renderer.tracking_type === "planet") {
let frame_planets = renderer.current_planets();
for (let n = 0; n < frame_planets.length; n++) { // Note than n here doesn't match planet ID.
let planet = frame_planets[n];
if (planet.id === renderer.tracking_id) {
renderer.display_tracked_planet(planet);
return;
}
}
}
// Dead. But leave the selection as it is.
if (renderer.turn > renderer.tracking_turn) {
unitbox.innerHTML = "(selection no longer present)";
} else {
unitbox.innerHTML = "(selection not yet present)";
}
};
renderer.measure_reset = () => {
measurebox.innerHTML = "";
renderer.measure_entity = null;
};
renderer.measure = (entity) => {
if (renderer.measure_entity !== null) {
let d = dist(renderer.measure_entity.x, renderer.measure_entity.y, entity.x, entity.y);
measurebox.innerHTML = `(dist: ${d.toFixed(6)})`;
}
renderer.measure_entity = entity;
};
renderer.click = (event) => {
if (!renderer.game) {
return;
}
let scale = Math.min(canvas.width / renderer.game.width, canvas.height / renderer.game.height) * renderer.cameraZoom;
let worldx = event.offsetX / scale + renderer.cameraPosition[0];
let worldy = event.offsetY / scale + renderer.cameraPosition[1];
// Was the click inside a planet?
let frame_planets = renderer.current_planets();
for (let n = 0; n < frame_planets.length; n++) { // Note than n here doesn't match planet ID.
let planet = frame_planets[n];
if (dist(worldx, worldy, planet.x, planet.y) < planet.r) {
renderer.tracking_type = "planet";
renderer.tracking_id = planet.id;
renderer.tracking_turn = renderer.turn;
renderer.measure(planet);
renderer.draw();
return;
}
}
// Was the click near a ship?
let best_ship = null;
let best_distance = 99999;
let frame = renderer.game.frames[renderer.turn];
for (let pid in frame.ships) {
if (frame.ships.hasOwnProperty(pid) === false) {
continue;
}
for (let sid in frame.ships[pid]) {
if (frame.ships[pid].hasOwnProperty(sid) === false) {
continue;
}
let ship = frame.ships[pid][sid];
let distance = dist(worldx, worldy, ship.x, ship.y);
if (distance < best_distance) {
best_ship = ship;
best_distance = distance;
}
}
}
if (best_distance < 5 / renderer.cameraZoom) {
renderer.tracking_type = "ship";
renderer.tracking_id = best_ship.id;
renderer.tracking_turn = renderer.turn;
renderer.measure(best_ship);
renderer.draw();
return;
}
// User clicked empty space.
renderer.tracking_type = null;
renderer.measure_reset();
renderer.draw();
};
renderer.scroll = (event) => {
// Backported from users hesch + succcubbus...
let scale = Math.min(canvas.width / renderer.game.width, canvas.height / renderer.game.height);
let oldScale = scale * renderer.cameraZoom;
renderer.cameraZoom += event.wheelDelta / 720.0;
renderer.cameraZoom = Math.max(1, Math.min(renderer.cameraZoom, 7));
let newScale = scale * renderer.cameraZoom;
renderer.cameraPosition[0] += event.offsetX / oldScale;
renderer.cameraPosition[0] -= event.offsetX / newScale;
renderer.cameraPosition[1] += event.offsetY / oldScale;
renderer.cameraPosition[1] -= event.offsetY / newScale;
renderer.draw();
};
renderer.scroll_reset = () => {
renderer.dragging = false;
renderer.cameraZoom = 1.0;
renderer.cameraPosition = [0, 0];
renderer.dragStartCameraPosition = [0, 0];
renderer.draw();
};
renderer.ships_destroyed_in_frame = (i) => { // returns a map of ship id --> destruction point
let ret = Object.create(null);
let frame = renderer.game.frames[i];
if (frame === undefined) {
return ret;
}
let events = frame.events;
if (events === undefined) {
return ret;
}
for (let i = 0; i < events.length; i++) {
let event = events[i];
if (event.event === "destroyed" && event.entity.type === "ship") {
ret[event.entity.id] = {x: event.x, y: event.y, owner: event.entity.owner};
}
}
return ret;
};
renderer.current_planets = () => {
// Returns an array of planets this turn with all needed info, BUT THE INDICES DO NOT MATCH PLANET ID.
if (!renderer.game) {
return [];
}
let ret = [];
let frame = renderer.game.frames[renderer.turn];
let game_planets = renderer.game.planets; // The planets we're told about at the top-level of the JSON.
for (let plid in frame.planets) { // "plid" means planet ID.
if (frame.planets.hasOwnProperty(plid) === false) {
continue;
}
let pl = Object.create(null);
pl.id = plid;
pl.x = game_planets[plid].x; // The frame's planet doesn't contain various things;
pl.y = game_planets[plid].y; // so get those from the game_planets....
pl.r = game_planets[plid].r;
pl.docking_spots = game_planets[plid].docking_spots;
pl.docked_ships = frame.planets[plid].docked_ships;
pl.health = frame.planets[plid].health;
pl.owner = frame.planets[plid].owner;
pl.current_production = frame.planets[plid].current_production;
ret.push(pl);
}
return ret;
};
renderer.draw_planets = () => {
if (renderer.show_planets === false) {
return;
}
let scale = Math.min(canvas.width / renderer.game.width, canvas.height / renderer.game.height) * renderer.cameraZoom;
let frame_planets = renderer.current_planets();
for (let n = 0; n < frame_planets.length; n++) { // Note than n here doesn't match planet ID.
let planet = frame_planets[n];
let x = planet.x * scale;
let y = planet.y * scale;
let r = planet.r * scale;
let adjusted_r = Math.max(1, r - PLANET_LINE_THICKNESS / 2); // Compensate for line thickness so the planet's edge is correct.
if (planet.owner === null) {
context.strokeStyle = "gray";
} else {
context.strokeStyle = colours[parseInt(planet.owner, 10)];
}
context.lineWidth = PLANET_LINE_THICKNESS;
context.beginPath();
context.arc(x, y, adjusted_r, 0, Math.PI * 2, true);
context.stroke();
if (renderer.docking_ranges) {
context.lineWidth = 1;
context.beginPath();
context.arc(x, y, r + renderer.game.constants.DOCK_RADIUS * scale, 0, Math.PI * 2, true);
context.stroke();
}
if (planet.docked_ships.length > 0) {
for (let n = 0; n < planet.docked_ships.length; n++) {
let sid = planet.docked_ships[n];
let ship = renderer.game.frames[renderer.turn].ships[planet.owner][sid];
let x2 = ship.x * scale;
let y2 = ship.y * scale;
context.lineWidth = 1;
context.strokeStyle = "white";
context.beginPath();
context.moveTo(x, y);
context.lineTo(x2, y2);
context.stroke();
}
}
}
};
renderer.draw_ships = () => {
let scale = Math.min(canvas.width / renderer.game.width, canvas.height / renderer.game.height) * renderer.cameraZoom;
let frame = renderer.game.frames[renderer.turn];
let previous_frame = (renderer.turn <= 0) ? null : renderer.game.frames[renderer.turn - 1];
let next_frame = (renderer.turn > renderer.game.frames.length - 2) ? null : renderer.game.frames[renderer.turn + 1];
for (let pid in frame.ships) {
if (frame.ships.hasOwnProperty(pid) === false) {
continue;
}
let tactical_ranges = renderer[`tactical_ranges_${pid}`];
for (let sid in frame.ships[pid]) {
if (frame.ships[pid].hasOwnProperty(sid) === false) {
continue;
}
let ship = frame.ships[pid][sid];
// While we're looking at ships, see if we see non-zero velocities, and log that fact.
if (ship.vel_x !== 0 || ship.vel_y !== 0) {
renderer.buggy = true;
}
let x = ship.x * scale;
let y = ship.y * scale;
let r = renderer.game.constants.SHIP_RADIUS * scale;
context.fillStyle = colours[parseInt(pid, 10)];
context.beginPath();
context.arc(x, y, r, 0, Math.PI * 2, true);
context.fill();
if (renderer.weapon_ranges && ship.docking.status === "undocked") {
context.strokeStyle = colours[parseInt(pid, 10)];
context.lineWidth = 1;
context.beginPath();
context.arc(x, y, (renderer.game.constants.WEAPON_RADIUS + renderer.game.constants.SHIP_RADIUS) * scale, 0, Math.PI * 2, true);
context.stroke();
}
if (tactical_ranges && ship.docking.status === "undocked") {
context.strokeStyle = colours[parseInt(pid, 10)];
context.lineWidth = 1;
context.beginPath();
context.arc(x, y, 13 * scale, 0, Math.PI * 2, true);
context.stroke();
}
if (previous_frame && renderer.tails) {
let last_ship = previous_frame.ships[pid][sid];
if (last_ship) {
let x2 = last_ship.x * scale;
let y2 = last_ship.y * scale;
context.lineWidth = 1;
context.strokeStyle = colours[parseInt(pid, 10)];
context.beginPath();
context.moveTo(x, y);
context.lineTo(x2, y2);
context.stroke();
}
}
if (renderer.pointers && next_frame !== null) {
let player_orders = renderer.game.moves[renderer.turn][pid][0];
let order = player_orders[sid];
if (order !== undefined && order.type === "thrust") {
let [intended_x, intended_y] = projection(ship.x, ship.y, order.magnitude, order.angle);
let x2 = intended_x * scale;
let y2 = intended_y * scale;
context.lineWidth = 1;
context.strokeStyle = dark_colours[parseInt(pid, 10)];
context.beginPath();
context.moveTo(x, y);
context.lineTo(x2, y2);
context.stroke();
}
}
}
}
};
renderer.draw_events = () => {
if (renderer.turn === 0 || renderer.deaths === false) {
return;
}
let previous_frame = renderer.game.frames[renderer.turn - 1];
let scale = Math.min(canvas.width / renderer.game.width, canvas.height / renderer.game.height) * renderer.cameraZoom;
// Visually, things look better if we draw last frame's events. In any case, events logically
// occur *between* frames so its a matter of philosophy which frame to draw them in.
let ships_destroyed_map = renderer.ships_destroyed_in_frame(renderer.turn - 1);
let sids = Object.keys(ships_destroyed_map);
for (let n = 0; n < sids.length; n++) {
let sid = sids[n];
let ship = ships_destroyed_map[sid];
let x = ship.x * scale;
let y = ship.y * scale;
// If the ship existed last turn, draw its final movement...
let pid = ship.owner;
let last_ship = previous_frame.ships[pid][sid];
if (last_ship && renderer.tails) {
let x2 = last_ship.x * scale;
let y2 = last_ship.y * scale;
context.lineWidth = 1;
context.strokeStyle = colours[parseInt(pid, 10)];
context.beginPath();
context.moveTo(x, y);
context.lineTo(x2, y2);
context.stroke();
}
// Draw a destruction marker...
context.lineWidth = 1;
context.strokeStyle = colours[parseInt(pid, 10)]; // "#ffffff";
context.beginPath();
context.moveTo(x - 1 * scale, y - 1 * scale);
context.lineTo(x + 1 * scale, y + 1 * scale);
context.stroke();
context.beginPath();
context.moveTo(x + 1 * scale, y - 1 * scale);
context.lineTo(x - 1 * scale, y + 1 * scale);
context.stroke();
}
};
renderer.send_info = () => {
let frame = renderer.game.frames[renderer.turn];
let planet_count = [];
for (let n = 0; n < renderer.game.num_players; n++) {
planet_count.push(0);
}
for (let plid in frame.planets) {
if (frame.planets.hasOwnProperty(plid) === false) {
continue;
}
if (frame.planets[plid].owner !== null) {
planet_count[frame.planets[plid].owner] += 1;
}
}
let lines = [];
lines.push(`<p>${renderer.filename}</p>`);
if (renderer.buggy) {
let bug_notice = `<span style="color: #ff3333;">velocity bug seen</span>`;
lines.push(`<p>-d "${renderer.game.width} ${renderer.game.height}" -s ${renderer.game.seed} ${bug_notice}</p>`);
} else {
lines.push(`<p>-d "${renderer.game.width} ${renderer.game.height}" -s ${renderer.game.seed}</p>`);
}
if (renderer.game.engine_version !== undefined) {
lines.push(`<p>Engine: ${renderer.game.engine_version}</p>`);