-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
254 lines (210 loc) · 5.95 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
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
const playGame = (async function(options = {}) {
const game = new Game({
//mapFile: "./maps/5.map",
mapFile: "./maps/test_6.map",
renderOptions: {
renderDistance: 9,
//renderMode: RENDER_MODE.BASIC,
renderMode: RENDER_MODE.RAYCAST,
viewDistance: 3,
alwaysDrawWalls: false,
drawRays: false,
color: true,
canvas: JL("pre.canvas.main"),
enabled: true
},
inputOptions: {
useExternalInput: true
},
navigationOptions: {
mode: NAVIGATION_MODE.DIRECTION
},
...options
});
const ui = new UI({
game: game,
element: JL(".ui")
});
game.init();
window.game = game;
console.log(game);
return game;
});
const trainAI = (async function(options = {}) {
let gen = 0;
const model = await fetch("./models/map1.json").then(res => res.json());
function newGeneration(network = null) {
const previewGame = new Game({
mapFile: "./maps/2.map",
//mapFile: "./maps/test_1.map",
renderOptions: {
renderDistance: 16,
renderMode: RENDER_MODE.BASIC,
//renderMode: RENDER_MODE.RAYCAST,
viewDistance: 3,
alwaysDrawWalls: false,
drawRays: false,
canvas: JL("pre.canvas.main"),
enabled: true
},
inputOptions: {
useExternalInput: false
},
...options
});
previewGame.on("init", () => {
gen++;
const games = [];
let interval = null;
//Create a new generation
for(let i = 0; i < 600; i++) {
const game = new Game({
mapData: previewGame.mapData,
//mapFile: "./maps/test_1.map",
renderOptions: {
renderDistance: 9,
renderMode: RENDER_MODE.BASIC,
//renderMode: RENDER_MODE.RAYCAST,
viewDistance: 3,
alwaysDrawWalls: false,
drawRays: true,
enabled: false
},
inputOptions: {
useExternalInput: false
}
});
game._AIPlayer = new PlayerAI(game, {});
games.push(game);
if(network) game._AIPlayer.mutate(network);
game.on("end", e => {
if(e.result === "lose") {
game._AIPlayer.died = true;
} else {
clearInterval(interval);
console.log("WINNER !");
console.log(game._AIPlayer.network.save());
}
//console.log("Game ended!", e.result);
//console.log(game._AIPlayer);
});
game.init();
}
//Copy all players into preview game
games.forEach(game => previewGame.map.addObject(game.map.getPlayer()));
//Game loop
const maxSteps = previewGame.map.search(previewGame.map.spawn, Exit).path.length + 1;
let steps = 0;
interval = setInterval(() => {
for(const game of games) {
const AI = game._AIPlayer;
if(AI.died) continue;
const direction = AI.predictMovement();
game.controller.move(direction);
}
steps++;
previewGame.renderer.renderFrame();
if(steps >= Math.min(10 + gen, maxSteps)) {
clearInterval(interval);
console.log("Reached max steps!");
//calculate fitness (distance to exit)
for(const game of games) {
const AI = game._AIPlayer;
if(AI.died) AI.fitness;
AI.fitness = AI.calculateFitness();
}
//sort by fitness
games.sort((a, b) => b._AIPlayer.fitness - a._AIPlayer.fitness);
console.log(games[0]._AIPlayer.fitness);
//create new generation
newGeneration(games[0]._AIPlayer.network);
}
}, 0);
console.log("Generation:", gen);
});
previewGame.init();
}
newGeneration(/*NeuralNetwork.load(model)*/);
//window.game = previewGame;
//console.log(previewGame);
});
const useAIv2 = (async function(options = {}) {
const game = new Game({
//mapFile: "./maps/5.map",
//mapFile: "./maps/test_8.map",
mapFile: "./maps/test_debug_0.map",
renderOptions: {
renderMode: RENDER_MODE.BASIC,
//renderMode: RENDER_MODE.RAYCAST,
renderDistance: 16,
viewDistance: 3,
alwaysDrawWalls: false,
drawRays: false,
color: true,
canvas: JL("pre.canvas.main"),
enabled: true
},
inputOptions: {
useExternalInput: false
},
...options
});
const AIPlayer = new PlayerAIv2(game, {
computeOptions: {
delay: options._renderHelper ? options._ai_delay_main || 0 : 0,
renderHelper: options._renderHelper || false
}
});
const ui = new UI({
game: game,
element: JL(".ui"),
AIPlayer: AIPlayer
});
game.init();
window.game = game;
console.log(game);
return game;
});
const buildOptions = (function() {
const options = {
renderOptions: {},
inputOptions: {},
navigationOptions: {},
};
options._playerMode = JL("#playerMode").value;
options._ai_delay_main = JL("#ai_delay_main").value;
options._renderHelper = JL("#renderHelper").checked;
options.mapFile = "./maps/" + JL("#mapFile").value;
options.renderOptions.renderMode = JL("#renderMode").value;
options.renderOptions.renderDistance = parseInt(JL("#renderDistance").value);
options.renderOptions.viewDistance = parseInt(JL("#viewDistance").value);
options.renderOptions.alwaysDrawWalls = JL("#alwaysDrawWalls").checked;
options.renderOptions.drawRays = JL("#drawRays").checked;
options.renderOptions.colors = JL("#color").checked;
options.renderOptions.canvas = JL(".canvas.main");
options.navigationOptions.mode = parseInt(JL("#navigationMode").value);
return options;
});
(function main() {
//return trainAI();
//return useAIv2();
//return playGame();
JL("#start").onclick = e => {
const options = buildOptions();
console.log(options);
if(window.game && window.game.isRunning) {
console.log("Stopping currently running game!");
window.game.endGame("interrupt");
window.game = null;
}
JL(".log").innerHTML = "";
JL(".helper-games").innerHTML = "";
if(options._playerMode === "player") {
playGame(options);
} else if(options._playerMode === "ai_agent_v1") {
trainAI(options);
} else if(options._playerMode === "ai_agent_v2") {
useAIv2(options);
}
};
})();