-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (59 loc) · 1.81 KB
/
index.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
// GA
let generation = 1;
// Synaptic
const Neuron = synaptic.Neuron,
Layer = synaptic.Layer,
Network = synaptic.Network,
Trainer = synaptic.Trainer,
Architect = synaptic.Architect;
// config constants
const carsCount = 100;
const trafficCount = 25;
const yLimit = -5000;
let mutateRatio = 0.1;
const roundLength = 1800;
const maxSpeedCars = 500;
// neural network data
let trainData = [];
// global variables
let bestCar = null;
let frameTime = 0;
let deltaTime = 0;
let resetTimeOut = -1;
// admin actions control
let paused = false, refreshCanvas = true, resetGameNow = false;
document.onkeypress = e => {
console.log(e.keyCode)
if (e.keyCode == 32) {
(paused = !paused) || animate(frameTime) ;
document.getElementById('paused').style.display = paused ? 'flex' : 'none';
} else if (e.keyCode == 13) {
refreshCanvas = !refreshCanvas;
} else if (e.keyCode == 82 || e.keyCode == 114) {
resetGameNow = true;
} else if (e.keyCode == 83 || e.keyCode == 115) {
saveModel();
}
};
// main settings
carCanvas.width = 250;
const carCtx = carCanvas.getContext('2d');
networkCanvas.width = innerWidth - 450;
const networkCtx = networkCanvas.getContext('2d');
// road declaration
const road = new Road(carCanvas.width / 2, carCanvas.width * 0.95);
// traffic declaration
let traffic = generateTraffic(trafficCount);
//let trafficOriginalCoords = JSON.parse(localStorage.getItem('trafficOriginalCoords')); resetTraffic(traffic);
let trafficOriginalCoords = traffic.map( c => ({ x: c.x, y: c.y, maxSpeed: c.maxSpeed }));
// cars declaration and init
const controlType = 'AI';
let cars;
try {
const model = restoreModel();
cars = generateCars(carsCount, model, controlType);
animate(1);
} catch(e) {
cars = generateCars(carsCount, null, controlType);
animate(1);
};