forked from 50m30n3/Neurobots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.js
276 lines (233 loc) · 4.71 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
var mousedown;
var lastx, lasty;
function wheelzoom( event )
{
if( event.wheelDelta < 0 )
zoom *= 1.1;
else
zoom *= 0.9;
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 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-400)/zoom-camx;
var cy = (y-300)/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 <= 64 ) && ( value >= 2 ) )
braindepth = value;
else
{
alert( "Brain size out of range (2-64)" );
return;
}
value = parseInt( document.getElementById( "bsizebox" ).value );
if( ( value <= 64 ) && ( value >= 4 ) && ( value%2 == 0 ) )
brainsize = value;
else
{
alert( "Brain complexity out of range (4-64) or not a multiple of 2" );
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 foodratechange( value )
{
if( ( value <= 50 ) && ( value >= 5 ) )
foodrate = 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 );
}
}