2015-06-08 23:59:07 +02:00
window . Client = require ( 'agario-client' ) ;
window . EventEmitter = require ( 'events' ) . EventEmitter ;
2015-06-05 17:41:07 +02:00
function AnimatedValue ( value ) {
this . write ( value ) ;
2015-06-05 16:22:41 +02:00
}
AnimatedValue . prototype = {
2015-06-05 17:41:07 +02:00
get : function ( ) {
2015-06-06 13:59:39 +02:00
if ( this . timeout ) {
var now = performance . now ( ) ,
end = this . frTime + this . timeout ;
if ( now >= end ) {
this . timeout = 0 ;
return this . toVal ;
} else {
if ( this . following ) {
this . toVal = this . following ( ) ;
}
return this . toVal - ( this . toVal - this . frVal ) * ( end - now ) / this . timeout ;
}
2015-06-05 16:22:41 +02:00
} else {
2015-06-06 13:59:39 +02:00
return this . toVal ;
2015-06-05 16:22:41 +02:00
}
} ,
2015-06-05 17:41:07 +02:00
set : function ( value , timeout ) {
2015-06-05 16:22:41 +02:00
if ( value != this . toVal ) {
this . frVal = this . get ( ) ;
this . toVal = value ;
2015-06-06 13:59:39 +02:00
this . timeout = timeout ;
this . following = undefined ;
2015-06-05 16:22:41 +02:00
this . frTime = performance . now ( ) ;
}
} ,
2015-06-06 13:59:39 +02:00
follow : function ( following , timeout ) {
this . frVal = this . get ( ) ;
this . following = following ;
this . timeout = timeout ;
this . frTime = performance . now ( ) ;
} ,
2015-06-05 17:41:07 +02:00
write : function ( value ) {
2015-06-05 16:22:41 +02:00
this . frVal = value ;
this . toVal = value ;
2015-06-05 17:41:07 +02:00
this . timeout = 0 ;
this . frTime = performance . now ( ) ; // so end == now
2015-06-05 16:22:41 +02:00
}
} ;
2015-06-06 15:53:17 +02:00
PIXI . Container . prototype . bringToFront = function ( ) {
2015-06-06 13:59:39 +02:00
if ( this . parent ) {
var parent = this . parent ;
parent . removeChild ( this ) ;
parent . addChild ( this ) ;
}
} ;
2015-06-05 16:22:41 +02:00
function BallView ( main , ball ) {
this . main = main ;
this . ball = ball ;
2015-06-06 15:53:17 +02:00
this . container = new PIXI . Container ( ) ;
2015-06-05 16:22:41 +02:00
this . graphic = new PIXI . Graphics ( ) ;
2015-06-06 15:53:17 +02:00
this . container . addChild ( this . graphic ) ;
2015-06-05 16:22:41 +02:00
2015-06-05 17:41:07 +02:00
this . x = new AnimatedValue ( 0 ) ;
this . y = new AnimatedValue ( 0 ) ;
this . s = new AnimatedValue ( 0 ) ;
2015-06-05 16:22:41 +02:00
var _this = this ;
this . appear ( ) ;
2015-06-05 17:41:07 +02:00
this . ball . on ( 'appear' , function ( ) {
2015-06-05 16:22:41 +02:00
_this . appear ( ) ;
} ) ;
2015-06-06 13:59:39 +02:00
this . ball . on ( 'destroy' , function ( reason ) {
if ( reason . reason == 'eaten' ) {
var eater = _this . main . balls [ reason . by ] ;
if ( eater && eater . ball . id != _this . ball . id ) {
_this . x . follow ( function ( ) {
return eater . x . get ( ) ;
} , 100 ) ;
_this . y . follow ( function ( ) {
return eater . y . get ( ) ;
} , 100 ) ;
setTimeout ( function ( ) {
_this . disappear ( ) ;
} , 50 ) ;
} else {
_this . disappear ( ) ;
}
} else {
_this . disappear ( ) ;
}
2015-06-05 16:22:41 +02:00
} ) ;
2015-06-05 17:41:07 +02:00
this . ball . on ( 'disappear' , function ( ) {
2015-06-06 13:59:39 +02:00
_this . disappear ( ) ;
2015-06-05 16:22:41 +02:00
} ) ;
2015-06-06 13:59:39 +02:00
this . ball . on ( 'move' , function ( old _x , old _y , new _x , new _y ) {
_this . x . set ( new _x , 100 ) ;
_this . y . set ( new _y , 100 ) ;
2015-06-05 16:22:41 +02:00
} ) ;
2015-06-06 13:59:39 +02:00
this . ball . on ( 'resize' , function ( old _size , new _size ) {
_this . s . set ( new _size , 100 ) ;
_this . main . zSort ( new _size ) ;
2015-06-05 16:22:41 +02:00
} ) ;
}
BallView . prototype = {
2015-06-05 17:41:07 +02:00
appear : function ( ) {
this . x . write ( this . ball . x ) ;
this . y . write ( this . ball . y ) ;
2015-06-06 13:59:39 +02:00
this . s . set ( this . ball . size , 100 ) ;
2015-06-05 16:22:41 +02:00
this . shape ( ) ;
2015-06-06 15:53:17 +02:00
this . setName ( ) ;
this . setMass ( ) ;
2015-06-06 13:59:39 +02:00
this . main . zSort ( this . ball . size ) ;
2015-06-06 15:53:17 +02:00
this . main . stage . addChild ( this . container ) ;
2015-06-05 16:22:41 +02:00
} ,
2015-06-06 13:59:39 +02:00
disappear : function ( ) {
this . s . set ( 0 , 100 ) ;
2015-06-06 15:53:17 +02:00
var _this = this ;
2015-06-06 13:59:39 +02:00
setTimeout ( function ( ) {
2015-06-06 15:53:17 +02:00
_this . main . stage . removeChild ( _this . container ) ;
2015-06-06 13:59:39 +02:00
} , 100 ) ;
} ,
2015-06-05 17:41:07 +02:00
shape : function ( ) {
2015-06-05 16:22:41 +02:00
this . graphic . clear ( ) ;
2015-06-06 13:59:39 +02:00
this . graphic . beginFill ( this . ball . virus ? 0x005500 : this . ball . color . replace ( '#' , '0x' ) , 1 ) ;
2015-06-05 16:22:41 +02:00
this . graphic . drawCircle ( 0 , 0 , 1 ) ;
this . graphic . endFill ( ) ;
} ,
2015-06-06 15:53:17 +02:00
setName : function ( ) {
if ( this . ball . name ) {
if ( ! this . name ) {
this . name = new PIXI . Text ( this . ball . name , {
font : 'bold 20pt Arial' ,
fill : 0xFFFFFF ,
stroke : 0x000000 ,
strokeThickness : 5
} ) ;
var _this = this ;
this . ball . on ( 'rename' , function ( ) {
_this . updateName ( ) ;
} ) ;
}
this . updateName ( ) ;
this . container . addChild ( this . name ) ;
} else {
if ( this . name ) {
this . container . removeChild ( this . text ) ;
this . ball . removeAllListener ( 'rename' ) ;
delete this . text ;
}
}
} ,
updateName : function ( ) {
this . name . resolution = 10 ;
this . name . scale . x = this . name . scale . y *= 2 * 0.9 / this . name . width ;
this . name . position . x = - this . name . width / 2 ;
this . name . position . y = - this . name . height / 2 ;
} ,
setMass : function ( ) {
if ( this . ball . mine ) {
if ( ! this . mass ) {
this . mass = new PIXI . Text ( this . ball . size , {
font : 'bold 20pt Arial' ,
fill : 0xFFFFFF ,
stroke : 0x000000 ,
strokeThickness : 5
} ) ;
var _this = this ;
this . ball . on ( 'resize' , function ( ) {
_this . updateMass ( ) ;
} ) ;
}
this . updateMass ( ) ;
this . container . addChild ( this . mass ) ;
} else {
if ( this . mass ) {
this . container . removeChild ( this . mass ) ;
this . ball . removeAllListeners ( 'rename' ) ;
delete this . mass ;
}
}
} ,
updateMass : function ( ) {
this . mass . text = this . ball . size ;
this . mass . resolution = 10 ;
this . mass . scale . x = this . mass . scale . y *= 0.5 / this . mass . width ;
this . mass . position . x = - this . mass . width / 2 ;
this . mass . position . y = this . name ? this . name . height / 2 : 0 ;
} ,
2015-06-05 17:41:07 +02:00
render : function ( ) {
2015-06-06 15:53:17 +02:00
this . container . position . x = this . x . get ( ) ;
this . container . position . y = this . y . get ( ) ;
this . container . scale . x = this . container . scale . y = this . s . get ( ) ;
2015-06-05 16:22:41 +02:00
}
} ;
function Viewer ( client , container ) {
this . client = client ;
this . container = container ;
this . balls = { } ;
this . addRenderer ( ) ;
this . addStats ( ) ;
var _this = this ;
2015-06-05 17:41:07 +02:00
client . once ( 'mapSizeLoad' , function ( min _x , min _y , max _x , max _y ) {
2015-06-05 16:22:41 +02:00
_this . gameWidth = max _x ;
_this . gameHeight = max _y ;
_this . initStage ( ) ;
_this . addListners ( ) ;
_this . addBorders ( ) ;
_this . animate ( ) ;
2015-06-08 20:19:28 +02:00
_this . homeview = true ;
client . once ( 'myNewBall' , function ( ) {
_this . homeview = false ;
} ) ;
2015-06-05 16:22:41 +02:00
_this . emit ( 'launched' ) ;
} ) ;
2015-06-05 17:41:07 +02:00
window . addEventListener ( 'resize' , function ( ) {
2015-06-05 16:22:41 +02:00
_this . updateSize ( ) ;
} ) ;
}
Viewer . prototype = {
2015-06-05 17:41:07 +02:00
getSize : function ( ) {
2015-06-05 16:22:41 +02:00
this . width = window . innerWidth ;
this . height = window . innerHeight ;
} ,
2015-06-05 17:41:07 +02:00
addRenderer : function ( ) {
2015-06-05 16:22:41 +02:00
this . getSize ( ) ;
this . renderer = PIXI . autoDetectRenderer ( this . width , this . height , {
antialias : true
} ) ;
this . container . appendChild ( this . renderer . view ) ;
} ,
2015-06-05 17:41:07 +02:00
updateSize : function ( ) {
2015-06-05 16:22:41 +02:00
this . getSize ( ) ;
this . renderer . resize ( this . width , this . height ) ;
} ,
2015-06-08 20:19:28 +02:00
defaultScale : function ( ) {
return Math . max ( this . width / 1920 , this . height / 1080 )
} ,
2015-06-05 17:41:07 +02:00
initStage : function ( ) {
2015-06-05 16:22:41 +02:00
this . stage = new PIXI . Container ( ) ;
2015-06-05 17:41:07 +02:00
this . cam = {
x : new AnimatedValue ( this . gameWidth / 2 ) ,
y : new AnimatedValue ( this . gameHeight / 2 ) ,
2015-06-08 20:19:28 +02:00
s : new AnimatedValue ( this . defaultScale ( ) )
2015-06-05 17:41:07 +02:00
} ;
2015-06-08 20:19:28 +02:00
this . d = { } ;
this . dg = new PIXI . Graphics ( ) ;
this . stage . addChild ( this . dg ) ;
2015-06-05 16:22:41 +02:00
} ,
2015-06-05 17:41:07 +02:00
addListners : function ( ) {
2015-06-05 16:22:41 +02:00
var _this = this ;
2015-06-05 17:41:07 +02:00
this . client . on ( 'ballAppear' , function ( id ) {
2015-06-05 16:22:41 +02:00
if ( ! _this . balls [ id ] ) {
_this . balls [ id ] = new BallView ( _this , this . balls [ id ] ) ;
2015-06-05 17:51:37 +02:00
} else { }
2015-06-05 16:22:41 +02:00
} ) ;
2015-06-06 13:59:39 +02:00
this . client . on ( 'ballDestroy' , function ( id ) {
delete this . balls [ id ] ;
} ) ;
2015-06-05 16:22:41 +02:00
} ,
2015-06-05 17:41:07 +02:00
addBorders : function ( ) {
2015-06-05 16:22:41 +02:00
this . borders = new PIXI . Graphics ( ) ;
this . borders . lineStyle ( 5 , 0xFF3300 , 1 ) ;
this . borders . drawRect ( 0 , 0 , this . gameWidth , this . gameHeight ) ;
this . stage . addChild ( this . borders ) ;
} ,
2015-06-05 17:41:07 +02:00
addStats : function ( ) {
2015-06-05 16:22:41 +02:00
this . stats = new Stats ( ) ;
this . stats . setMode ( 1 ) ;
this . stats . domElement . style . position = 'absolute' ;
this . stats . domElement . style . left = '0px' ;
this . stats . domElement . style . top = '0px' ;
document . body . appendChild ( this . stats . domElement ) ;
} ,
2015-06-06 13:59:39 +02:00
zSort : function ( at ) {
if ( ! at ) {
at = 0 ;
}
var keys = Object . keys ( this . balls ) ;
var _this = this ;
keys . sort ( function ( a , b ) {
return _this . balls [ a ] . ball . size - _this . balls [ b ] . ball . size ;
} ) ;
for ( var key _offset in keys ) {
var ball = this . balls [ keys [ key _offset ] ] ;
if ( ball . ball . size >= at ) {
2015-06-06 15:53:17 +02:00
ball . container . bringToFront ( ) ;
2015-06-06 13:59:39 +02:00
}
}
} ,
2015-06-05 17:41:07 +02:00
posCamera : function ( ) {
2015-06-05 16:22:41 +02:00
var x = y = p = 0 ;
for ( var ball _id in this . client . my _balls ) {
var ball = this . client . balls [ this . client . my _balls [ ball _id ] ] ;
if ( ! ball . visible ) continue ;
x += ball . x * ball . size ;
y += ball . y * ball . size ;
p += ball . size ;
}
if ( p > 0 ) { // if we have visible ball(s)
2015-06-05 17:41:07 +02:00
this . cam . x . set ( x / p , 100 ) ;
this . cam . y . set ( y / p , 100 ) ;
2015-06-08 20:19:28 +02:00
this . cam . s . set ( Math . pow ( Math . min ( 64 / p , 1 ) , 0.4 ) * this . defaultScale ( ) , 500 ) ;
} else if ( this . homeview ) {
this . cam . s . write ( this . defaultScale ( ) ) ;
2015-06-05 16:22:41 +02:00
} // else: don't move the camera
} ,
2015-06-05 17:41:07 +02:00
render : function ( ) {
2015-06-05 16:22:41 +02:00
for ( var ball _id in this . client . balls ) {
var ball = this . balls [ ball _id ] ;
if ( ball ) {
ball . render ( ) ;
}
}
} ,
2015-06-05 17:41:07 +02:00
animate : function ( ) {
2015-06-05 16:22:41 +02:00
this . stats . begin ( ) ;
this . render ( ) ;
this . posCamera ( ) ;
2015-06-05 17:41:07 +02:00
this . stage . scale . x = this . stage . scale . y = this . cam . s . get ( ) ;
this . stage . position . x = - this . cam . x . get ( ) * this . stage . scale . x + this . width / 2 ;
this . stage . position . y = - this . cam . y . get ( ) * this . stage . scale . y + this . height / 2 ;
2015-06-05 16:22:41 +02:00
this . renderer . render ( this . stage ) ;
this . stats . end ( ) ;
this . emit ( 'animate' ) ;
var _this = this ;
2015-06-05 17:41:07 +02:00
requestAnimationFrame ( function ( ) {
2015-06-05 16:22:41 +02:00
_this . animate ( ) ;
} ) ;
}
} ;
// Inherit from EventEmitter
for ( var key in EventEmitter . prototype ) {
Viewer . prototype [ key ] = EventEmitter . prototype [ key ] ;
}
function Pointer ( viewer ) {
this . viewer = viewer ;
this . client = this . viewer . client ;
this . dest = { // Destination, relative to camera center
x : 0 ,
y : 0
} ;
var _this = this ;
2015-06-05 17:41:07 +02:00
this . viewer . once ( 'launched' , function ( ) {
2015-06-05 16:22:41 +02:00
_this . viewer . stage . interactive = true ;
2015-06-05 17:41:07 +02:00
_this . viewer . stage . on ( 'mousemove' , function ( e ) {
2015-06-05 16:22:41 +02:00
_this . pointermove ( e ) ;
} ) ;
2015-06-05 17:41:07 +02:00
_this . viewer . stage . on ( 'touchmove' , function ( e ) {
2015-06-05 16:22:41 +02:00
_this . pointermove ( e ) ;
} ) ;
2015-06-05 17:41:07 +02:00
_this . viewer . on ( 'animate' , function ( e ) {
2015-06-05 16:22:41 +02:00
_this . move ( ) ;
} ) ;
} ) ;
2015-06-05 17:41:07 +02:00
window . addEventListener ( 'keydown' , function ( e ) {
2015-06-05 16:22:41 +02:00
if ( e . keyCode == 87 ) {
_this . client . eject ( ) ;
} else if ( e . keyCode == 32 ) {
_this . client . split ( ) ;
}
} ) ;
}
Pointer . prototype = {
2015-06-05 17:41:07 +02:00
move : function ( ) {
this . client . moveTo ( this . viewer . cam . x . get ( ) + this . dest . x , this . viewer . cam . y . get ( ) + this . dest . y ) ;
2015-06-05 16:22:41 +02:00
} ,
2015-06-05 17:41:07 +02:00
pointermove : function ( e ) {
2015-06-05 16:22:41 +02:00
var gamePos = e . data . getLocalPosition ( this . viewer . stage ) ;
2015-06-05 17:51:37 +02:00
this . dest = {
2015-06-05 17:41:07 +02:00
x : gamePos . x - this . viewer . cam . x . get ( ) ,
y : gamePos . y - this . viewer . cam . y . get ( )
2015-06-05 16:22:41 +02:00
} ;
2015-06-06 13:59:39 +02:00
if ( Math . abs ( this . dest . x ) < 10 && Math . abs ( this . dest . y ) < 10 ) {
2015-06-05 17:51:37 +02:00
this . dest = {
x : 0 ,
y : 0
} ;
}
2015-06-05 16:22:41 +02:00
this . move ( ) ;
}
} ;
function Controller ( client ) {
this . client = client ;
this . server = {
region : 'EU-London' ,
ip : '127.0.0.1' ,
port : 9158
2015-06-05 17:51:37 +02:00
} ;
2015-06-05 16:22:41 +02:00
this . nick = 'agario-client' ;
this . autoRespawn = false ;
this . gui = new dat . GUI ( ) ;
this . servgui = this . gui . addFolder ( 'Server' ) ;
this . servgui . add ( this . server , 'region' , [ 'US-Fremont' , 'US-Atlanta' , 'BR-Brazil' , 'EU-London' , 'RU-Russia' , 'JP-Tokyo' , 'CN-China' , 'SG-Singapore' , 'TK-Turkey' ] ) ;
this . servgui . add ( this , 'findServer' ) ;
this . servgui . add ( this . server , 'ip' ) ;
this . servgui . add ( this . server , 'port' ) ;
this . servgui . add ( this , 'connect' ) ;
this . servgui . add ( this , 'disconnect' ) ;
this . servgui . open ( ) ;
this . cellgui = this . gui . addFolder ( 'Cell' ) ;
this . cellgui . add ( this , 'nick' ) ;
this . cellgui . add ( this , 'spawn' ) ;
this . cellgui . add ( this , 'autoRespawn' ) ;
2015-06-05 23:54:54 +02:00
var scoreGui = this . cellgui . add ( this . client , 'score' ) . listen ( ) ;
2015-06-06 13:59:39 +02:00
this . client . on ( 'scoreUpdate' , function ( ) {
2015-06-05 23:54:54 +02:00
scoreGui . updateDisplay ( ) ;
} ) ;
2015-06-05 16:22:41 +02:00
this . leadergui = this . gui . addFolder ( 'Leaderboard' ) ;
this . leaders = { } ;
this . resetLeader ( ) ;
for ( var i = 1 ; i <= 10 ; i ++ ) {
this . leadergui . add ( this . leaders , i ) ;
}
var _this = this ;
2015-06-05 17:41:07 +02:00
client . on ( 'connected' , function ( ) {
2015-06-05 16:22:41 +02:00
_this . servgui . close ( ) ;
_this . cellgui . open ( ) ;
_this . leadergui . open ( ) ;
if ( _this . autoRespawn ) {
_this . spawn ( ) ;
}
} ) ;
2015-06-05 17:41:07 +02:00
client . on ( 'reset' , function ( ) {
2015-06-05 16:22:41 +02:00
_this . servgui . open ( ) ;
_this . cellgui . close ( ) ;
_this . leadergui . close ( ) ;
_this . resetLeader ( ) ;
} ) ;
2015-06-05 17:41:07 +02:00
client . on ( 'lostMyBalls' , function ( ) {
2015-06-05 16:22:41 +02:00
if ( _this . autoRespawn ) {
_this . spawn ( ) ;
}
} ) ;
2015-06-05 17:41:07 +02:00
client . on ( 'leaderBoardUpdate' , function ( old , leaders ) {
2015-06-05 16:22:41 +02:00
for ( var i in leaders ) {
var rank = parseInt ( i ) + 1 ;
_this . leaders [ rank ] = this . balls [ leaders [ i ] ] . name || 'An unnamed cell' ;
for ( var j in _this . leadergui . _ _controllers ) {
_this . leadergui . _ _controllers [ j ] . updateDisplay ( ) ;
}
}
} ) ;
}
Controller . prototype = {
2015-06-05 17:41:07 +02:00
findServer : function ( ) {
2015-06-05 16:22:41 +02:00
// Because of SOP, this will never work
x = new XMLHttpRequest ( ) ;
x . open ( 'POST' , 'http://m.agar.io' , false ) ;
x . setRequestHeader ( 'Content-Type' , 'application/x-www-form-urlencoded' ) ;
x . setRequestHeader ( 'Content-Length' , this . server . region . length ) ;
// x.setRequestHeader('Origin', 'http://agar.io');
// x.setRequestHeader('Referer', 'http://agar.io/');
x . send ( this . server . region ) ;
s = x . responseText . split ( ':' ) ;
this . server . ip = s [ 0 ] ;
this . server . port = s [ 1 ] ;
for ( var i in this . servgui . _ _controllers ) {
this . servgui . _ _controllers [ i ] . updateDisplay ( ) ;
}
} ,
2015-06-05 17:41:07 +02:00
connect : function ( ) {
2015-06-05 16:22:41 +02:00
this . client . connect ( 'ws://' + this . server . ip + ':' + this . server . port ) ;
} ,
2015-06-05 17:41:07 +02:00
disconnect : function ( ) {
2015-06-05 16:22:41 +02:00
this . client . disconnect ( ) ;
} ,
2015-06-05 17:41:07 +02:00
spawn : function ( ) {
2015-06-05 16:22:41 +02:00
this . client . spawn ( this . nick ) ;
} ,
2015-06-05 17:41:07 +02:00
resetLeader : function ( ) {
2015-06-05 16:22:41 +02:00
for ( var i = 1 ; i <= 10 ; i ++ ) {
this . leaders [ i ] = '---' ;
}
}
} ;
function IA ( client ) {
this . client = client ;
this . begin ( ) ;
}
IA . prototype = {
2015-06-05 17:41:07 +02:00
begin : function ( ) {
2015-06-05 16:22:41 +02:00
var _this = this ;
2015-06-05 17:41:07 +02:00
this . interval = setInterval ( function ( ) {
2015-06-05 16:22:41 +02:00
_this . food ( ) ;
// _this.decide();
} , 100 ) ;
} ,
2015-06-05 17:41:07 +02:00
end : function ( ) {
2015-06-05 16:22:41 +02:00
clearInterval ( this . interval _id ) ;
} ,
2015-06-05 17:41:07 +02:00
getDistanceBetweenBalls : function ( ball _1 , ball _2 ) { //this calculates distance between 2 balls
2015-06-05 16:22:41 +02:00
return Math . sqrt ( Math . pow ( ball _1 . x - ball _2 . x , 2 ) + Math . pow ( ball _2 . y - ball _1 . y , 2 ) ) ;
} ,
2015-06-05 17:41:07 +02:00
getAngleBetweenBalls : function ( b1 , b2 ) { // output in rad
2015-06-05 16:22:41 +02:00
dX = b2 . x - b1 . x ;
dY = b2 . y - b1 . y ;
return Math . tan ( dY / dX ) ;
} ,
2015-06-05 17:41:07 +02:00
food : function ( ) {
2015-06-05 16:22:41 +02:00
var candidate _ball = null ; //first we don't have candidate to eat
var candidate _distance = 0 ;
var my _ball = this . client . balls [ this . client . my _balls [ 0 ] ] ; //we get our first ball. We don't care if there more then one, its just example.
if ( ! my _ball ) return ; //if our ball not spawned yet then we abort. We will come back here in 100ms later
for ( var ball _id in this . client . balls ) { //we go true all balls we know about
var ball = this . client . balls [ ball _id ] ;
if ( ball . virus ) continue ; //if ball is a virus (green non edible thing) then we skip it
if ( ! ball . visible ) continue ; //if ball is not on our screen (field of view) then we skip it
if ( ball . mine ) continue ; //if ball is our ball - then we skip it
if ( ball . size / my _ball . size > 0.5 ) continue ; //if ball is bigger than 50% of our size - then we skip it
var distance = this . getDistanceBetweenBalls ( ball , my _ball ) ; //we calculate distances between our ball and candidate
if ( candidate _ball && distance > candidate _distance ) continue ; //if we do have some candidate and distance to it smaller, than distance to this ball, we skip it
candidate _ball = ball ; //we found new candidate and we record him
candidate _distance = this . getDistanceBetweenBalls ( ball , my _ball ) ; //we record distance to him to compare it with other balls
}
if ( ! candidate _ball ) return ; //if we didn't find any candidate, we abort. We will come back here in 100ms later
this . client . log ( 'closest ' + candidate _ball + ', distance ' + candidate _distance ) ;
this . client . moveTo ( candidate _ball . x , candidate _ball . y ) ; //we send move command to move to food's coordinates
} ,
2015-06-05 17:41:07 +02:00
decide : function ( ) {
2015-06-05 16:22:41 +02:00
var my _ball = this . client . balls [ this . client . my _balls [ 0 ] ] ; // TODO Handle more balls
if ( ! my _ball ) return ;
var candidates = [ ] ;
for ( var ball _id in this . client . balls ) {
var ball = this . client . balls [ ball _id ] ;
if ( ! ball . visible ) continue ;
var score = 0 ;
if ( ball . mine ) {
score = 1 ;
} else {
if ( ball . virus ) {
if ( ball . size > my _ball . size ) {
score = - 5 ;
} else {
score = 5 ;
}
} else {
score = Math . max ( 1000 - this . getDistanceBetweenBalls ( ball , my _ball ) - ball . size - my _ball . size , 0 ) ;
if ( ball . size < my _ball . size ) {
score = score ;
} else {
score = - score * 5 ;
}
}
}
candidates . push ( {
x : ball . x - my _ball . x ,
y : ball . y - my _ball . y ,
score : score
} ) ;
}
var x = y = p = 0 ;
for ( var candidate _id in candidates ) {
var candidate = candidates [ candidate _id ] ;
// console.log(candidate.x, candidate.y, candidate.score)
x += candidate . x * candidate . score ;
y += candidate . y * candidate . score ;
p += candidate . score ;
}
this . client . moveTo ( my _ball . x + x / p , my _ball . y + y / p ) ;
}
} ;
var d = { } ; // DEBUG Allow access from console
2015-06-05 17:41:07 +02:00
window . onload = function ( ) {
2015-06-05 16:22:41 +02:00
d . client = new Client ( 'worker' ) ;
d . viewer = new Viewer ( d . client , document . getElementById ( 'viewer' ) ) ;
d . controller = new Controller ( d . client ) ;
d . pointer = new Pointer ( d . viewer ) ;
// d.ia = new IA(d.client);
} ;