|
Development Forum |
 |
| Pick the low-hanging fruit | |
|

Malkin
     Manager

|
10/15/2011 | |
I'm trying to get this critter to eat only fruit that it can reach...
Firstly, it's 40 pixels high, and I was wondering if that's translated 1:1 into the game.
Secondly, will this code make it check how high the fruit is, give up if the middle of the fruit is higher than 40 pixels up from the ground, then go on to go left or right towards the fruit?
**calculate relative position
doif ov40 ne null
targ ov40
setv va13 posx
setv va14 posy
endi
targ ownr
doif va14 ge 40
seta ov40 null
elif va13 lt posx and fall ne 1
anim [0 1 0]
velo -4 -3
over
elif fall ne 1
anim [7 8 7]
velo 4 -3
over
endi
subv ov02 1
|
I've had a little look around, and having a small object can cause problems if it's trying to check how high up a big object is - I'm hoping this won't cause problems because most fruit are small.
My TCR Norns |
 Peppery One
Papriko
    
|
10/15/2011 | |
Yes, in theory the sprite size in pixels should apply 1:1 to the coordinate system of the game.
[...]
setv va13 posx
setv va14 posy
endi
targ ownr
doif va14 ge 40
[...] | You use the absolute Y coordinate in the if-statement. It does not compare the Y difference between OWNR and OV40, it only checks how high OV40 hangs in the world itself. As long as your plant is not at the ceiling of the C3 Norn Terrarium, this will pretty much always return false.
You must save the Y position of both, OWNR and OV40. Somewhat like this:* Position A (fruit)
targ ov40
setv va13 posx
setv va14 posy
* Postition B (ownr)
targ ownr
setv va23 posx
setv va24 posy
* Compare positions
setv va34 va24
subv va34 va14
* Now you check if the fruit is within range
* This is true when 0 <= va34 <= 40 |
Lets play plants! Photosynthesis... Photosynthesis... Photosynthesis... |

AquaShee
   
|
10/15/2011 | |
Actually, you should use this:
doif rely ownr targ lt 40
*move towards the fruit and omnomnom
endi |
Basically, RELY checks the vertical distance between the centre points of the 1st and 2nd agent given in the arguments. Unless the fruit is hanging over a cliff, this should work fine. And even then it would just be funny to see the critter launch itself over the edge just to get that tasty apple. You can always double-check while approaching anyways.
The Community Scribble: make (y)our own metaroom! |
 Peppery One
Papriko
    
|
10/15/2011 | |
Aw, did not know that command. That makes it easier of course.
Lets play plants! Photosynthesis... Photosynthesis... Photosynthesis... |

Malkin
     Manager

|
10/15/2011 | |
Do I then have to specify 'if TARG is greater than 40, find a lower-hanging fruit' ?
I wish the CAOS tool worked on Macs. 
**calculate relative position
doif ov40 ne null
targ ov40
setv va13 posx
endi
targ ownr
doif rely ownr targ lt 40 and va13 lt posx and fall ne 1
anim [0 1 0]
velo -4 -3
over
elif rely ownr targ lt 40 and fall ne 1
anim [7 8 7]
velo 4 -3
over
endi
subv ov02 1 |
My TCR Norns |

AquaShee
   
|
10/15/2011 | |
Yeah, what you can do is use ENUM to go through all fruits to find a low-hanging one and go from there.
seta va99 null
enum <fruit id>
doif rely ownr targ lt 40 and va99 eq null
seta va99 targ
endi
next
doif va99 eq null
dbg: outs "Could not find a low-hanging fruit."
gsub Wander
else
gsub EatFruit
endi
|
The Community Scribble: make (y)our own metaroom! |

Malkin
     Manager

|
10/15/2011 | |
Would it be better for them to only enum the fruit that are within their rnge?
My TCR Norns |
 Peppery One
Papriko
    
|
10/16/2011 | |
Yes, you can do that with ESEE. It works exactly like ENUM, but only picks those agents which are in RNGE.
Lets play plants! Photosynthesis... Photosynthesis... Photosynthesis... |

AquaShee
   
|
10/16/2011 | |
ENUM is faster I think, even though it checks many more agents, but using ESEE is indeed simpler.
The Community Scribble: make (y)our own metaroom! |
 Peppery One
Papriko
    
|
10/17/2011 | |
Are you sure ENUM would be faster in this case? As you said, we enumerate through many more agents. In addition we need a DOIF for every single of them to make sure it is in RNGE. This costs time too.
Lets play plants! Photosynthesis... Photosynthesis... Photosynthesis... |

AquaShee
   
|
10/17/2011 | |
I'm very sure yes.
Enumerating though every single agent of a type is extremely fast, since it just goes down the list.
Enumerating through every agent within range is much slower, since the engine has to go down the very same list *and* check the distance to the searching agent.
But getting the RELY value is much quicker than getting the actual distance, it's a simple subtraction instead of geometry.
So if we check the range *after* checking the RELY, then we gain lots of speed because we've already removed the agents that are too high up, only checking the distance of the ones that hang lower.
Of course, on modern computers it doesn't really matter, ESEE would make the code easier to read and maintain.
The Community Scribble: make (y)our own metaroom! |

Malkin
     Manager

|
10/18/2011 | |
So, instead of
seta va99 null
enum <fruit id>
doif rely ownr targ lt 40 and va99 eq null
seta va99 targ
endi |
it would be better to use
seta va99 null
doif rely ownr targ lt 40 and va99 eq null
enum <fruit id>
seta va99 targ
endi | ?
My TCR Norns |

AquaShee
   
|
10/18/2011 | 1 |
The first one is the correct one, actually, with some modifications:
seta va99 null
enum <fruit id>
doif rely ownr targ lt 40 and va99 eq null
doif seee ownr targ eq 1
seta va99 targ
endi
endi
next |
So what this does is that it loops through every fruit in the world and picks the ones that are almost the at same height as the OWNR. then it checks if the OWNR can see this particular agent and if it can, it selects it.
Since it only checks if the agent can see it after determining if it is too high or not, it is quite fast.
This bit is not as fast, but easier to use:
seta va99 null
esee <fruit id>
doif rely ownr targ lt 40 and va99 eq null
seta va99 targ
endi
next |
The above bit loops through every fruit in the world and checks if the OWNR can see it, and then checks if it is high enough. Same result, less code, more CPU intensive.
But with modern computers being rather good, and even older computers having no trouble with DS, option 2 is the best option to pick.
The Community Scribble: make (y)our own metaroom! |