'x coordinates are stored into out3, y coordinates are stored in mkshell and tielen
'the x and y distances to the network access are assumed to be 2 digits numbers and are 
'stored into body with a loss of accuracy.
'when swarming is 0(no) head and eye will store the heads information into out1 and out2
'vice versa when swarming is >0(yes) head and eye will store the eyes information into out1 and out2
'the head will move towards the coordinates in out1 and out2


def function 50
'(0= head 0> eye)
def swarming 55
'(0=prioritize food in sight 0> prioritize network orders)
def satxpos 60
def satypos 61

'initialize (includes MB grow routine which is used for moving)
'enable reading of x coordinatess from eye
cond
*.tmemloc .out3 !=
start
.out3 .tmemloc store
stop

'complete the bot, become the eye and face toward network access 
cond
*.numties 0 =
*.function 0 =
start
90 .repro store
.function inc
*.satxpos *.satypos angle .setaim store
stop

'form the tie and turn away
cond
*100 0 =
*.refeye *.myeye =
start
17 .tie store 
618.aimdx store
100 inc
stop


'data storage
'x coordinate of enemy into out3
cond
*.refeye *.myeye !=
*.refxpos *.out3 sub abs 50 >
start 
*.refxpos .out3 store
stop

'first 3 digits of y coordinate of enemy into last 3 digits of shell
cond
*.refeye *.myeye !=
*.refypos *.refypos 100 mod sub 100 div *.shell 1000 mod !=
start 
*.refypos *.refypos 100 mod sub 100 div *.shell 1000 mod sub .mkshell
stop

'last 2 digits of y coordinate of enemy into last 2 digits of tielen
cond
*.refeye *.myeye !=
*.refypos 100 mod *.tielen 100 mod sub abs 50 >
start
*.refypos 100 mod .fixlen store
stop

'x coordinate of network target into out3
cond
*.refeye *.myeye =
*.in1 *.out3 sub abs 50 >
start 
*.in1 .out3 store
stop

'first 3 digits of y coordinate of network target into last 3 digits of shell
cond
*.refeye *.myeye =
*.in2 *.in2 100 mod sub 100 div *.shell 1000 mod !=
start 
*.in2 *.in2 100 mod sub 100 div *.shell 1000 mod sub .mkshell
stop


'last 2 digits of y coordinate of network target into last 2 digits of tielen
cond
*.refeye *.myeye =
*.in2 100 mod *.tielen 100 mod sub abs 50 >
start
*.in2 100 mod .fixlen store
stop

'x coordinate of network access
cond
*.refeye *.myeye =
*.function 0 >
*.refxpos *.satxpos sub abs 50 >
start
*.refxpos .satxpos store
stop


'y coordinate of network access
cond
*.refeye *.myeye =
*.function 0 >
*.refypos *.satypos sub abs 50 >
start
*.refypos .satypos store
stop


'head gets info about network access x coordinate
cond
*.function 0 =
*.trefxpos *.satxpos sub abs 50 >
start
*.trefxpos .satxpos store
stop

'head gets info about network access y coordinate
cond
*.function 0 =
*.trefypos *.satypos sub abs 50 >
start
*.trefypos .satypos store
stop


'target choosing
'head uses his x coordinate
cond
*.function 0 =
*.swarming 0 =
start
*.out3 .out1 store
stop

'head uses eyes x coordinate
cond
*.function 0 >
*.swarming 0 >
start
*.tmemval .out1 store
stop

cond
'head uses his y coordinate
*.function 0 =
* swarming 0 =
start
*.shell 1000 mod 100 mult *.tielen 100 mod add .out1 store
stop

cond
'head uses eyes y coordinate
*.function 0 =
* swarming 0 =
start
*.trefshell 1000 mod 100 mult *.tielen 100 mod add .out1 store
stop

'movement

'aim at target
cond
start
*.out1 *.out2 angle .setaim store
stop

'aim at attacker
cond
*.hit 0 !=
start
*.shang .setaim store
stop

cond
*.swarming 0 =
*.eye5 70 <
function 0 =
start
*.refvel 5 40 *.eye5 sub sgn 1 add mult .up store
stop

cond
*.swarming 0 >
function 0 =
start
50 *.vel sub .up store
stop


'satellites and reproduction
'
cond 
*.function 0 >
*.eye5 10 <
*.refeye *.myeye =
start
17 .deltie store
.swarming inc
stop

cond
*.nrg 10000 >
*.function 0 >
start
50 .repro store
314 .aimsx store
stop

cond
*.hit 0 !=
*.numties 0 =
*.function 0 >
start
0 .function store
0 .swarming store
stop

cond
*.numties 0 =
*.eye5 0 =
start
105 inc
stop

cond
*.eye5 0 !=
*105 0 >
start
0 105 store
stop

cond
*105 100 >
*.numties 0 =
*.function 0 >
start
0 .function store
0 105 store
stop


'swarming or not swarming?

cond
*.function 0 =
*.body 10 mod *.swarming !=
start
*.body 10 mod *.swarming sub dup sign 1 add mult 2 div .fdbody store
*.body 10 mod *.swarming sub dup sign 1 sub mult 2 div abs .strbody store
stop

cond
*.function 0 >
*.trefbody 10 mod *.swarming !=
start
*.trefbody 10 mod *.swarming sub dup sign 1 add mult 2 div .fdbody store
*.trefbody 10 mod *.swarming sub dup sign 1 sub mult 2 div abs .strbody store
stop

cond
*.function 0 =
*.eye5 0 =
*.refeye *.myeye =
or
start
106 inc
stop

cond
*.eye5 0 !=
*.refeye *.myeye !=
*106 0 >
start
0 106 store
stop


cond
*.function 0 =
*106 40 >
*.swarming 0 =
start
.swarming inc
stop

cond
*.out1 *.out2 pyth 200 <
start
0 .swarming store
stop

'waste and weight`

cond
*.nrg 1000 >
start
100 .strbody store
stop

cond
*.nrg 300 <
start
100 .fdbody store
stop


cond
*.waste 100 >
start
*.waste .shootval store
-4 .shoot store
stop

'feeding and defending

cond
*.eye5 35 >
*.refeye *.myeye !=
*.venom 60 >
start
-3 .shoot store
stop


*.eye5 35 >
*.refeye *.myeye !=
*.venom 60 <
start
4 .shootval store
-1 .shoot store
stop

cond
*.ploc .shoot !=
*.vloc .shoot !=
start
.shoot .ploc store
.shoot .vloc store
-2 .venval store
stop

cond
*.poison 200 <
*.nrg 1000 >
start
20 .strpoison store
stop

cond
*.venom 200 <
*.nrg 1000 >
start
20 .strvenom store
stop

end