-
Notifications
You must be signed in to change notification settings - Fork 12
/
input.js
340 lines (281 loc) · 6.11 KB
/
input.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
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
var mousedown;
var lastx, lasty;
function wheelzoom( event )
{
if( event.detail )
delta = -event.detail;
else
delta = event.wheelDelta;
if( delta < 0 )
zoom *= 1.1;
else
zoom /= 1.1;
if( zoom > 10.0 ) zoom = 10.0;
if( zoom < 0.25 ) zoom = 0.25;
if( ! running )
draw();
event.preventDefault();
}
function movecam( event )
{
if( ! mousedown ) return;
var dx = event.clientX-lastx;
var dy = event.clientY-lasty;
lastx = event.clientX;
lasty = event.clientY;
camx += dx/zoom;
camy += dy/zoom;
if( camx > worldsize ) camx = worldsize;
if( camx < -worldsize ) camx = -worldsize;
if( camy > worldsize ) camy = worldsize;
if( camy < -worldsize ) camy = -worldsize;
if( ! running )
draw();
}
function winresize( event )
{
var canvas = document.getElementById( "canvas" );
var container = document.getElementById( "container" );
var width = canvas.clientWidth;
var aspect = screen.width/screen.height;
canvas.width = width;
canvas.height = width/aspect;
draw();
}
function mouse_down( event )
{
if( event.button != 1 ) return;
mousedown = true;
lastx = event.clientX;
lasty = event.clientY;
}
function mouse_up( event )
{
mousedown = false;
}
function mouse_click( event )
{
if( event.button == 0 )
{
var x = event.pageX-event.target.offsetLeft;
var y = event.pageY-event.target.offsetTop;
var cx = (x-ctx.canvas.width/2)/zoom-camx;
var cy = (y-ctx.canvas.height/2)/zoom-camy;
var smallest = worldsize*2;
var sel = -1;
for( var i in bots )
{
if( i == selected ) continue;
var dist = Math.sqrt( (cx-bots[i].x)*(cx-bots[i].x)+(cy-bots[i].y)*(cy-bots[i].y) );
if( dist < smallest )
{
smallest = dist;
sel = i;
}
}
if( ( sel != -1 ) && ( smallest < 10 ) )
{
selected = sel;
}
else
{
selected = -1;
}
botstats( true );
}
if( ! running )
draw();
}
function resetcam()
{
zoom = 1.0;
camx = 0.0;
camy = 0.0;
if( ! running )
draw();
}
function startsim()
{
if( ! running )
{
timer = setInterval( loop, 20 );
document.getElementById("startbutton").disabled = true;
document.getElementById("stopbutton").disabled = false;
running = true;
}
}
function stopsim()
{
if( running )
{
clearInterval( timer );
document.getElementById("startbutton").disabled = false;
document.getElementById("stopbutton").disabled = true;
running = false;
}
}
function addbot()
{
var value = parseInt( document.getElementById( "bdepthbox" ).value );
if( ( value <= 16 ) && ( value >= 2 ) )
braindepth = value;
else
{
alert( "Brain size out of range (2-16)" );
return;
}
value = parseInt( document.getElementById( "bsizebox" ).value );
if( ( value <= 16 ) && ( value >= 4 ) && ( value%4 == 0 ) )
brainsize = value;
else
{
alert( "Brain complexity out of range (4-16) or not a multiple of 4" );
return;
}
bots.push( new neurobot( 0,0, Math.PI*2*Math.random() ) );
}
function injectbrain()
{
if( selected != -1 )
{
var result = bots[selected].loaddata( document.getElementById("braindata").value );
if( result.success != true )
{
alert( result.msg );
}
botstats( true );
if( ! running )
draw();
}
}
function bdepthchange( box )
{
if( parseInt( box.value ) > 16 )
box.value = 16;
if( parseInt( box.value ) < 2 )
box.value = 2;
}
function bsizechange( box )
{
if( parseInt( box.value ) > 16 )
box.value = 16;
if( parseInt( box.value ) < 4 )
box.value = 4;
if( parseInt( box.value ) % 4 != 0 )
box.value = parseInt( box.value ) - (parseInt( box.value ) % 4);
}
function foodratechange( box )
{
if( parseInt( box.value ) > 50 )
box.value = 50;
if( parseInt( box.value ) < 5 )
box.value = 5;
if( ( parseInt( box.value ) ) && ( box.value <= 50 ) && ( box.value >= 5 ) )
foodrate = parseInt( box.value );
}
function foodareachange( box )
{
if( parseInt( box.value ) < 100 )
box.value = 100;
if( parseInt( box.value ) > parseInt( document.getElementById( "worldsizebox" ).value ) )
document.getElementById( "worldsizebox" ).value = parseInt( box.value );
}
function worldsizechange( box )
{
if( parseInt( box.value ) < 100 )
box.value = 100;
if( parseInt( box.value ) < parseInt( document.getElementById( "foodareabox" ).value ) )
document.getElementById( "foodareabox" ).value = parseInt( box.value );
}
function refreshlist()
{
list = document.getElementById( "savename" );
while( list.options.length > 0 )
list.remove(0);
var opt = new Option();
opt.disabled = true;
opt.value = "-";
opt.selected = true;
if( localStorage.length > 0 )
{
opt.text = "Select brain to load";
list.disabled = false;
}
else
{
opt.text = "No saved brains";
list.disabled = true;
}
list.options.add( opt );
document.getElementById( "deletebutton" ).disabled = true;
for( var i=0; i<localStorage.length; i++ )
{
list.options.add( new Option( localStorage.key(i), localStorage.key(i) ) );
}
}
function listselect( value )
{
if( value != "-" )
{
document.getElementById( "deletebutton" ).disabled = false;
document.getElementById( "braindata" ).value = localStorage.getItem( list.value );
}
else
{
document.getElementById( "deletebutton" ).disabled = true;
}
}
function deletebrain()
{
list = document.getElementById( "savename" );
if( list.value != "-" )
{
localStorage.removeItem( list.value );
refreshlist()
}
}
function savebrain()
{
if( selected == -1 )
return;
var data = bots[selected].getdata();
var filename = data.split( "!" )[1].substr(0,6) + "-" + bots[selected].braindepth + "x" + bots[selected].brainsize + "-Gen." + bots[selected].generation;
localStorage.setItem( filename, data );
refreshlist()
}
function selnewest()
{
var newest = -1;
var maxgen = -1;
for( var i in bots )
{
if( bots[i].generation > maxgen )
{
maxgen = bots[i].generation;
newest = i;
}
}
if( newest != -1 )
{
selected = newest;
botstats( true );
}
}
function selbest()
{
var best = -1;
var bestval = -1;
for( var i in bots )
{
if( (bots[i].age/100+bots[i].children*100+bots[i].dotseaten*10) > bestval )
{
bestval = (bots[i].age/100+bots[i].children*100+bots[i].dotseaten*10);
best = i;
}
}
if( best != -1 )
{
selected = best;
botstats( true );
}
}