-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
104 lines (88 loc) · 3.32 KB
/
main.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
const carCanvas = document.getElementById("carCanvas");
//? Why below line commented ?? Read learning number 1 from learnings.txt
// canvas.height = window.innerHeight;
carCanvas.width = 200;
const networkCanvas = document.getElementById("networkCanvas");
networkCanvas.width = 400;
// COntect to handel everything with car
const carCtx = carCanvas.getContext("2d");
const networkCtx = networkCanvas.getContext("2d");
const road = new Road(carCanvas.width / 2, carCanvas.width * 0.9);
// const car = new Car(road.getLaneCenter(1), 100, 30, 50, "KEYS");
// const car = new Car(road.getLaneCenter(1), 100, 30, 50, "AI");
let N = 400;
const cars = generateCars(N);
let bestCar = cars[0];
if (localStorage.getItem("bestBrain")) {
for (let i = 0; i < cars.length; i++) {
cars[i].brain = JSON.parse(localStorage.getItem("bestBrain"));
if (i != 0) {
NeuralNetwork.mutate(cars[i].brain, 0.1);
}
}
}
const traffic = [
new Car(road.getLaneCenter(1), -100, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(0), -300, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(2), -300, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(1), -500, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(2), -650, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(0), -900, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(1), -1000, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(0), -1100, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(0), -1200, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(1), -1200, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(1), -1400, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(2), -1300, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(1), -1500, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(0), -1600, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(1), -1700, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(2), -1800, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(1), -1900, 30, 50, "DUMMY", 2),
];
animate();
const refreshButton = document.getElementById("refreshButton");
refreshButton.addEventListener("click", function () {
location.reload();
});
function save() {
localStorage.setItem("bestBrain", JSON.stringify(bestCar.brain));
}
function discard() {
localStorage.removeItem("bestBrain");
}
function generateCars(N) {
const cars = [];
for (let i = 1; i <= N; i++) {
cars.push(new Car(road.getLaneCenter(1), 100, 30, 50, "AI"));
}
return cars;
}
function animate(time) {
for (let i = 0; i < traffic.length; i++) {
traffic[i].update(road.borders, []);
}
for (let i = 0; i < cars.length; i++) {
cars[i].update(road.borders, traffic);
}
bestCar = cars.find((c) => c.y == Math.min(...cars.map((c) => c.y)));
carCanvas.height = window.innerHeight;
networkCanvas.height = window.innerHeight;
carCtx.save();
carCtx.translate(0, -bestCar.y + carCanvas.height * 0.75);
road.draw(carCtx);
for (let i = 0; i < traffic.length; i++) {
traffic[i].draw(carCtx, "red");
}
carCtx.globalAlpha = 0.2;
for (let i = 0; i < cars.length; i++) {
cars[i].draw(carCtx, "blue");
}
carCtx.globalAlpha = 1;
bestCar.draw(carCtx, "blue", true);
carCtx.restore();
// Calls animate method again and again which gives illusion of movement
networkCtx.lineDashOffset = -time / 50;
Visualizer.drawNetwork(networkCtx, bestCar.brain);
requestAnimationFrame(animate);
}