Author Topic: Enemy Recognition.  (Read 5862 times)

Offline Chlorate

  • Bot Neophyte
  • *
  • Posts: 6
    • View Profile
Enemy Recognition.
« on: May 04, 2010, 07:37:49 PM »
So I've been fiddling around with a bit of code for a while, as I mentioned in my greeting message, so my bot uses more of its eyes to find food instead of spinning and moving around randomly.

So my bot's eyes are set up like so:
Code: [Select]
cond
start
 628 .eye3dir store
 90 .eye1dir store
 -628 .eye7dir store
 -90 .eye9dir store
stop

Eyes 3 and 9 aren't doing anything right now, but I'm considering doing something with them later.
Eyes 1 and 9 are moved further apart slightly to give a wider field of view.

I've set it up so that if it sees something in eye 1 or eye 2 (or indeed eyes 8 or 9), but not in eye 5 it'll turn to face it.
However no matter what I try, I can't make the bot not turn to face a member of its own species and as a result they'll often end up going around in circles with each other and getting eaten.

Here's an example of what I've been trying:
Code: [Select]
cond

 *.refeye *.myeye !=
 *.eye5 0 =
 *.eye1 0 >
 *.eye2 0 > or

start
 100 .aimsx store
stop

Does the condition  *.refeye *.myeye != only work if it's referring to a robot in view of eye5?

I've been scratching my head and not getting very far because of this problem, can anyone hint as to what may be wrong?

Thanks.

Offline bacillus

  • Bot Overlord
  • ****
  • Posts: 907
    • View Profile
Enemy Recognition.
« Reply #1 on: May 05, 2010, 12:48:59 AM »
If you want to use the refvars on anything other than .eye5, you have to change the focus eye. For example, this code sets the focus eye to eye 3:

-2 .focuseye store

Check out OCULUS II in the gene depository, it's a command that sets the focus eye to the nearest object.
"They laughed at Columbus, they laughed at Fulton, they laughed at the Wright brothers. But they also laughed at Bozo the Clown."
- Carl Sagan

Offline Chlorate

  • Bot Neophyte
  • *
  • Posts: 6
    • View Profile
Enemy Recognition.
« Reply #2 on: May 05, 2010, 11:39:57 AM »
Thanks for the boot in the right direction.

Although I couldn't make head nor tail of OCULUS II.  

So now I've written the code that makes my bot focus on the nearest object, however I still can't get it to not turn to face members of its own species.

Here's the bit of code responsible for turning the bot:
Code: [Select]
'8 Turn to face
cond
 *.refeye *.myeye !=
 *.eye5 0 =
 *.focuseye 0 !=
 *.eye1 *.eye2 add 0 >
 *.eye8 *.eye9 add 0 > or
start
 *.focuseye -30 mult .aimsx store
stop

Any more pointers would be appreciated, this is getting quite infuriating now  

Offline ashton15

  • Bot Builder
  • **
  • Posts: 99
    • View Profile
Enemy Recognition.
« Reply #3 on: May 05, 2010, 12:02:43 PM »
Quote from: Chlorate
Thanks for the boot in the right direction.

Although I couldn't make head nor tail of OCULUS II.  

So now I've written the code that makes my bot focus on the nearest object, however I still can't get it to not turn to face members of its own species.

Here's the bit of code responsible for turning the bot:
Code: [Select]
'8 Turn to face
cond
 *.refeye *.myeye !=
 *.eye5 0 =
 *.focuseye 0 !=
 *.eye1 *.eye2 add 0 >
 *.eye8 *.eye9 add 0 > or
start
 *.focuseye -30 mult .aimsx store
stop

Any more pointers would be appreciated, this is getting quite infuriating now  

the refvars your using only work for the focuseye, what I'd reccomend is have 2 eyes facing forwards (eye5 and eye6 for example) make 6 the focuseye and then with eye5 you can use memloc (like a ref but you specify what you ref) and the in out communication sysvars so point eye5 to whatever is closest and you can use memloc to tell wether it is your species or not (I'd advise using Dna length for this rather than eyes because it's harder to fake) then you can keep the focuseye looking at the vegetable or enemy or whatever it's looking at without turning. Just say if you need some help coding it (hint: use  .eye5dir). if you do work it out as a favour I can put it into unconditional logic for you which would make it use less energy. That's a thing mr green tentacle monster I looked at your occulus 2 thing again now I understand the code, couldn't you easily improve it byadding in some unconditional condiditions so you don't store a value every cycle and again rather than a store could you just go through logic gates and do incs and decs so if it moved just one eye to the right you could use a tenth of the energy you would normally use. Just an idea.

Offline Chlorate

  • Bot Neophyte
  • *
  • Posts: 6
    • View Profile
Enemy Recognition.
« Reply #4 on: May 05, 2010, 12:14:31 PM »
But the target bot is being looked at by the focus eye, as I mentioned before I wrote some code that switches the focus eye from eye5 to whichever has the closest bot to it.

Forgive me if I turn down your favour, but I've only been at this for a couple of days now, and I'm still trying to understand the basic syntax.
Thanks anyway.

Offline Moonfisher

  • Bot Overlord
  • ****
  • Posts: 592
    • View Profile
Enemy Recognition.
« Reply #5 on: May 05, 2010, 01:08:46 PM »
Would be easier to see if you posted all the eye and turning code together, the error might not be in the gene you think it is...
From what I can see the gene should be working correctly... if your focus eye is on a friendly unit the gene won't trigger.
But remember that changing the focus eye won't give you any new input untill next cycle. Any actions you perform in the code won't take effect till the end of the cycle. (But since you change focus eye during the code execution you'll be able to read the new value, the focus eye just hasn't changed yet, and the value in eyef and all the refvars will still be the ones from the old focuseye)

Offline Chlorate

  • Bot Neophyte
  • *
  • Posts: 6
    • View Profile
Enemy Recognition.
« Reply #6 on: May 05, 2010, 01:13:44 PM »
Hmm, ok here's the whole code.
Apologies for it being really quite inefficient code, it's a work in progress and has a couple of bits that haven't been implemented yet:

Code: [Select]
cond
 *.robage 0 =
start
 3 475 store
 def current 101
stop
 
'2 Eye Position
cond
start
 628 .eye3dir store
 90 .eye1dir store
 -628 .eye7dir store
 -90 .eye9dir store
stop

'3 Focus eye position determination: target left
cond
 *.eye5 0 =
 *.eye1 0 >
 *.eye2 0 > or
start
 -4 .current store
 *.current .focuseye store
stop

'4 eye 1 or 2 is closer
cond
 *.current -4 =
 *.eye2 *.eye1 >
start
 -3 .current store
 *.current .focuseye store
stop

'5 target right
cond
 *.eye5 0 =
 *.eye8 0 >
 *.eye9 0 > or
start
 4 .current store
 *.current .focuseye store
stop

'6 8 or 9 closer
cond
 *.current 4 =
 *.eye8 *.eye9 >
start
 3 .current store
 *.current .focuseye store
stop

7 conditional statement
cond
 *.eye5 0 =
 *.eye1 add *.eye2 *.eye8 add *.eye9 >
start
 -4 .current store
else
 4 .current store
stop

'8 Turn to face
cond
 
 *.eye5 0 =
 *.focuseye 0 !=
 *.eye1 *.eye2 add 0 >
 *.eye8 *.eye9 add 0 > or
 *.refeye *.myeye !=
start
 *.focuseye -30 mult .aimsx store
stop


'9 focus on eye5
cond
 *.refeye *.myeye !=
 *.eye5 0 >
 *.eye5 *.eye1 add *.eye2 >
 *.eye5 *.eye8 add *.eye9 >
start
 0 .aimdx store
 0 .current store
 *.current .focuseye store
stop
end
« Last Edit: May 05, 2010, 01:14:20 PM by Chlorate »

Offline Moonfisher

  • Bot Overlord
  • ****
  • Posts: 592
    • View Profile
Enemy Recognition.
« Reply #7 on: May 05, 2010, 02:44:29 PM »
I'm not sure you can use def in a gene, I'd sugest having those before the first gene.
Also if you wan't to save energy you can have a robage = 0 gene where you set the eyes and such. (Ofcourse it's safer to set them every cycle if you're up against an oponent that might mess with those settings)

But I think the issue may be that current doesn't exist, so you're never changing the focus eye. (If you select a bot you'll see the focus eye is the one with the red edge)
Also a tip, eyef is like eye5 but for the focus eye, it's a good idea to check that the focus eye contains something before turning.

And you should be aware that view distance isn't huge, and looking at the closest object isn't the same as looking at the most dangerous one. You may often be swithing between the 2 closest alies while an enemy is aproaching and firing. Not saying it can't be effective, but you may want a way to make sure the same eyes aren't checked repeatedly when allies are near.

You're welcome to steal anything you like from this thing (Or maybe switch to this behavior when you keep seing alies) :

[div class=\'codetop\']CODE[div class=\'codemain\' style=\'height:200px;white-space:pre;overflow:auto\']'A system with some practical eyes
'And a test bot (F3) wich shows some of the posibilities.
'The conspec is buildt into the eye system, it just uses
'.dnalen to reconize it's own

'- Variables -
'To apply new eye settings store the value 1 in seteyes
'seteyes will be 0 if not set and 2 if they've been set to something.
def seteyes 50

'When seteyes is 1 the values in seteyespace, setoffset and seteyewidth
'will be applied.
def seteyespace 52
def setoffset 53
def seteyewidth 54

'Eyeuse chooses between changing the focuseye to look,
'or turning the current focus eye to look.
'If you plan on shooting without using .aimshoot and you're using the
'system for turning, then remember to set the focus eye to the one you have centered
'Same goes for using angle and setaim to face the oponent,
'always rememeber to keep track of your focuseye.
'(Unless ofcourse you're never using the system for changing eyefocus)
def eyesystem 55


'- Constants -
'eyedist is used for turning and should be 35 unless something changes in the source.
def eyedist 35      'The eyes default offset

'These are just some values I usualy use, but you can use any values you like.
'They also don't need to be defined as constant, you can just push raw values into the set vars.
def fulleyespace 105   '360 degree spread
def fulloffset 140   'Offset to center eye9
def slimeyewidth -30   'Slimmest eye width
def normaleyewidth 0
def wideeyewidth 80   'Wider eyewidth


'- Vars and const for the test bot
'These are just used in the example to show how the system can be used.

'vars
def state 56

'const
def alge 13
def maxbody 15000
def movebody 6000


'********************************************************************
'------------------------- Init Eyes --------------------------------
'--- Initializing the eyes ---
cond
*.seteyes 0 =
start
1 .seteyes store
.dnalen .memloc store
.fulleyespace .seteyespace store
.fulloffset .setoffset store
.normaleyewidth .seteyewidth store
stop
'********************************************************************


'********************************************************************
'------------------------- Eyes -------------------------------------
'--- Setup ---
cond
*.seteyes 1 =
start
*.setoffset .eye9dir store
*.setoffset *.seteyespace add .eye8dir store
*.setoffset *.seteyespace 2 mult add .eye7dir store
*.setoffset *.seteyespace 3 mult add .eye6dir store
*.setoffset *.seteyespace 4 mult add .eye5dir store
*.setoffset *.seteyespace 5 mult add .eye4dir store
*.setoffset *.seteyespace 6 mult add .eye3dir store
*.setoffset *.seteyespace 7 mult add .eye2dir store
*.setoffset *.seteyespace 8 mult add .eye1dir store
*.seteyewidth .eye1width store
*.seteyewidth .eye2width store
*.seteyewidth .eye3width store
*.seteyewidth .eye4width store
*.seteyewidth .eye5width store
*.seteyewidth .eye6width store
*.seteyewidth .eye7width store
*.seteyewidth .eye8width store
*.seteyewidth .eye9width store
2 .seteyes store
stop

'--- Change focus ---
cond
*.eyesystem 0 =
start
*.eyef 0 =
*.memval *.dnalen = or
 501 *.focuseye 5 add 9 mod add * abs sgn 501 *.focuseye 6 add 9 mod add * abs sgn 2 mult | 501 *.focuseye 7 add 9 mod add * abs sgn 4 mult | 501 *.focuseye 8 add 9 mod add * abs sgn 8 mult | 501 *.focuseye 9 add 9 mod add * abs sgn 16 mult | 501 *.focuseye 10 add 9 mod add * abs sgn 32 mult | 501 *.focuseye 11 add 9 mod add * abs sgn 64 mult | 501 *.focuseye 12 add 9 mod add * abs sgn 128 mult | dup dup dup dup dup dup dup dup 1 & 1 sub dup div 1 sub abs *.focuseye 5 add 9 mod 4 sub mult swap 3 & 2 sub dup div 1 sub abs *.focuseye 6 add 9 mod 4 sub mult add swap 7 & 4 sub dup div 1 sub abs *.focuseye 7 add 9 mod 4 sub mult add swap 15 & 8 sub dup div 1 sub abs *.focuseye 8 add 9 mod 4 sub mult add swap 31 & 16 sub dup div 1 sub abs *.focuseye 9 add 9 mod 4 sub mult add swap 63 & 32 sub dup div 1 sub abs *.focuseye 10 add 9 mod 4 sub mult add swap 127 & 64 sub dup div 1 sub abs *.focuseye 11 add 9 mod 4 sub mult add swap 255 & 128 sub dup div 1 sub abs *.focuseye 12 add 9 mod 4 sub mult add swap 511 swap sgn mult store
stop

'--- Turn ---
cond
*.eyesystem 1 =
start
*.eyef 0 =
*.memval *.dnalen = or
 501 *.focuseye 5 add 9 mod add * abs sgn 501 *.focuseye 6 add 9 mod add * abs sgn 2 mult | 501 *.focuseye 7 add 9 mod add * abs sgn 4 mult | 501 *.focuseye 8 add 9 mod add * abs sgn 8 mult | 501 *.focuseye 9 add 9 mod add * abs sgn 16 mult | 501 *.focuseye 10 add 9 mod add * abs sgn 32 mult | 501 *.focuseye 11 add 9 mod add * abs sgn 64 mult | 501 *.focuseye 12 add 9 mod add * abs sgn 128 mult | dup dup dup dup dup dup dup dup 1 & 1 sub dup div 1 sub abs *.aim 521 *.focuseye 5 add 9 mod add * add 35 4 mult add mult swap 3 & 2 sub dup div 1 sub abs *.aim 521 *.focuseye 6 add 9 mod add * add 35 3 mult add mult add swap 7 & 4 sub dup div 1 sub abs *.aim 521 *.focuseye 7 add 9 mod add * add 35 2 mult add mult add swap 15 & 8 sub dup div 1 sub abs *.aim 521 *.focuseye 8 add 9 mod add * add 35 1 mult add mult add swap 31 & 16 sub dup div 1 sub abs *.aim 521 *.focuseye 9 add 9 mod add * add 35 0 mult add mult add swap 63 & 32 sub dup div 1 sub abs *.aim 521 *.focuseye 10 add 9 mod add * add 35 1 mult sub mult add swap 127 & 64 sub dup div 1 sub abs *.aim 521 *.focuseye 11 add 9 mod add * add 35 2 mult sub mult add swap 255 & 128 sub dup div 1 sub abs *.aim 521 *.focuseye 12 add 9 mod add * add 35 3 mult sub mult add swap 19 swap sgn mult store
stop
'********************************************************************



'********************************************************************
'************ From here on it's just the test bot *******************
'********************************************************************

start
.fixpos dec
.deltie inc

*.nrg *.body >
*.nrg 500 > and
100 .strbody store

*.nrg 2000 >
*.body *.nrg < and
*.body .maxbody < and
*.nrg 10 mult *.body sub .strbody store

*.robage 50 >
1 .seteyes store
.wideeyewidth .seteyewidth store
stop

'- Scout -
cond
*.state 0 =
start
*.eyesystem 0 !=
0 .eyesystem store

*.eyef 0 =
*.memval *.dnalen = or
*.maxvel .up store

*.robage 5 <
*.body 1100 > and
*.maxvel - .up store

*.eyef 0 !=
*.memval .alge != and
*.memval *.dnalen != and
*.refvelsx 100 mult .dx store
*.refveldn 100 mult .up store

*.eyef 0 !=
*.memval .alge = and
1 .state store

*.eyef 0 =
*.memval *.dnalen = or
*.shflav 0 = and
*.nrg 20000 > and
*.body 3000 > and
2 .state store

*.eyef 0 !=
*.memval *.dnalen != and
*.body *.refbody 200 sub > and
3 .state store

*.body .movebody >
4 .state store
*.totalmyspecies 2 < and
*.nrg 10000 > and
*.eyef 0 = and
2 .state store
stop

cond
*.state 1 =
start
*.eyesystem 1 !=
1 .eyesystem store
4 .focuseye store

*.eyef 0 !=
*.memval .alge = and
*.refxpos *.refypos angle .setaim store
*.refvelsx *.veldx add .sx store
*.refvelup 100 *.eyef sub add 10 floor .up store
-6 .shoot store
16 .shootval store

*.eyef 0 =
*.memval .alge != or
0 .state store
stop

cond
*.state 2 =
start
*.eyef 0 =
*.memval *.dnalen = or
*.shflav 0 = and
*.nrg 20000 > and
*.body 3000 > and
*.aim 314 add .setaim store
20 .repro store

not
3 .state store
stop

cond
*.state 3 =
start
*.eyesystem 1 !=
1 .eyesystem store
4 .focuseye store

*.eyef 0 !=
*.memval *.dnalen != and
*.refxpos *.refypos angle .setaim store
*.refvelsx *.veldx add .sx store
*.refxpos *.xpos sub abs *.refypos *.ypos sub abs pyth 5 div 30 sub 0 floor 100 ceil *.refvelup add .up store
*.nrg 100 div *.refnrg ceil 25 floor .shootval store
-6 .shoot store

*.shflav 0 !=
*.shang .aimshoot store
-6 .shoot store
40 .shootval store
0 .shflav store

*.eyef 0 =
*.memval *.dnalen = or
0 .state store

*.body .movebody >
4 .state store
stop

cond
*.state 4 =
start
*.eyesystem 1 !=
1 .eyesystem store
4 .focuseye store
1 .seteyes store
.wideeyewidth .seteyewidth store

*.eyef 0 !=
*.memval *.dnalen != and
*.refxpos *.refypos angle .setaim store
-6 .shoot store
*.memval .alge != and
*.nrg 100 div *.refnrg ceil 25 floor .shootval store

*.refxpos *.refypos dist 600 >
*.memval .alge = and
*.shflav 0 = and
-10 .shootval store

*.timer 15 mod 0 =
*.memval .alge = and
150 .aimsx store

*.shflav 0 !=
*.shang .aimshoot store
-6 .shoot store
40 .shootval store
0 .shflav store

*.eyef 0 =
*.memval *.dnalen = or
0 .state store

*.eyef 0 =
*.memval *.dnalen = or
*.memval .alge = or
*.body .movebody < and
0 .state store

*.eyef 0 =
*.memval *.dnalen = or
*.memval .alge = or
*.body .maxbody > and
4 .state store
stop

end

The first 2 genes are for initializing the eyes, the next 2 are 2 different ways of using the eyes, one turns to the next non empty eye, while the other one just changes the focus eye. (Those 2 genes are hard to read, I can post a broken down version, but it'll still be kinda hard to read it) But the point is that you don't need to understand them to use them, you just change the defined values at the beginning of the code.
After that the remaining genes are just a test bot to show how you can use the eye genes.

You can also change the overall eye setup if you like, but be aware that the turning gene won't work without the current eye setup (You can change the width and distance between the eyes by using the defined values, but not scramble it any way you like), however the focus eye switching gene should work no matter how you set the eyes.

Offline Chlorate

  • Bot Neophyte
  • *
  • Posts: 6
    • View Profile
Enemy Recognition.
« Reply #8 on: May 05, 2010, 03:06:57 PM »
I checked that the variable "current" exists by looking it up on the bot's memory, and yes the little red edge marker does move to the relevant eye when something is in its field of view. I built the code up gene by gene testing that they were working ok.

Thanks for the help though, I'll probably have a look at that one sometime. Currently fiddling with something else for the time being.

Offline Moonfisher

  • Bot Overlord
  • ****
  • Posts: 592
    • View Profile
Enemy Recognition.
« Reply #9 on: May 06, 2010, 12:56:59 PM »
Didn't seem to work properly when I ran it, you should try advancing one cycle at the time in the sim to see the result.
From what I can see, if I place a friendly bot in the left or right eye it will turn before switching the focus eye, and after turning it switches the focus eye...
So you're one cycle behind, my first suspition seems to have been the right one:

Quote
But remember that changing the focus eye won't give you any new input untill next cycle. Any actions you perform in the code won't take effect till the end of the cycle. (But since you change focus eye during the code execution you'll be able to read the new value, the focus eye just hasn't changed yet, and the value in eyef and all the refvars will still be the ones from the old focuseye)

You turn before the focus eye has changed.
When your code is executed the only thing that happens is you're setting some values in some sysvars, you don't shoot, turn or anything (Including changing the focuseye).
Then once the end of the code is reached the values are read and the corresponding commands are executed. (Can't remember the order, but I think the important ones go : turn, shoot, tie (Can't remember when movement is done though))

So the fix is easy, just place the turning gene before the genes that manipulate focuseye.


I'd recomend doing something a litle easier though :
(This also seems to have the advantage of turning to the position where the target will be next cycle, not sure why, but it makes everything a lot easier (Maybe that's why ))

[div class=\'codetop\']CODE[div class=\'codemain\' style=\'height:200px;white-space:pre;overflow:auto\']
'Since eye5 is 0 it's enough to make sure eyef isn't and you'll know the focus eye isn't eye5 (No matter what it says in the .focuseye sysvar)
cond
*.eye5 0 =
*.eyef 0 >
*.refeye *.myeye !=
start
*.refxpos *.refypos angle .setaim store
0 .focuseye store
stop
« Last Edit: May 06, 2010, 01:09:55 PM by Moonfisher »

Offline bacillus

  • Bot Overlord
  • ****
  • Posts: 907
    • View Profile
Enemy Recognition.
« Reply #10 on: May 07, 2010, 12:42:12 AM »
Quote from: Chlorate
Thanks for the boot in the right direction.

Although I couldn't make head nor tail of OCULUS II.  

I didn't expect you to understand. It took me half an hour to write that command, and then I had to start all over again because an add was missing somewhere. Basically it just compresses the genes needed for a full comparison between each set of eyes into one super-complicated command. If you want to use it, copy and paste it into a gene that activates every second. Then have something like so:

cond
 *.refeye 0 >
 'insert recognition here
start
 *.refxpos *.refypos angle .setaim store
stop

This will make the bot only turn towards nonspecs.
"They laughed at Columbus, they laughed at Fulton, they laughed at the Wright brothers. But they also laughed at Bozo the Clown."
- Carl Sagan