Enemy Aggro Radius

Manofdusk

Veteran
Veteran
Joined
Apr 26, 2014
Messages
211
Reaction score
39
First Language
English
Primarily Uses
I've managed to get my tactical movement system nearly finished. However, I've run into yet another snag. Of all the ones I've run into, this one is probably the smallest... but it might help if I had another pair of eyes looking at it to tell me what I did wrong:

 I have a parallel event keeping up with the X and Y cords of each visible enemy unit as well as an "aggro" variable set at map start (this is the maximum distance at which an enemy will attack you). I have a separate parallel process keeping up with the current X and Y coordinates of the player (Current X and Current Y).

here is how they are defined:

1101: Enemy 1 X Coord

1102: Enemy 1 Y Coord

1103: Enemy Aggro (set to 3 at map start, but it could be anything)

1076: Current X Coord (Player)

1077: Current Y Coord (Player)

# If enemy 1 is to the right of the target and less then or equal to $game_variables[1103] away, move toward player

Conditional Branch: Variable [1101: Enemy 1 X Coord] >= Variable [1076:Current X Coord ]

   Conditional Branch: Script: $game_variables[1101] - $game_variables[1076] <= $game_variables[1103]

       Set Move Route: [Monster 1] (Wait)

         Change Speed: 4

         Move Toward Player

 Else

Branch End

----------------

# If enemy 1 is to the left of the target and less then or equal to $game_variables[1103] away, move toward player

Conditional Branch: Variable[1076:Current X Coord ]  >= Variable  [1101: Enemy 1 X Coord]

   Conditional Branch: Script: $game_variables[1076] - $game_variables[1101] <= $game_variables[1103]

       Set Move Route: [Monster 1] (Wait)

         Change Speed: 4

         Move Toward Player

 Else

Branch End

----------------

# If enemy 1 is above the target and less then or equal to $game_variables[1103] away, move toward player

Conditional Branch: Variable [1102: Enemy 1 Y Coord] >= Variable [1077:Current Y Coord ]

   Conditional Branch: Script: $game_variables[1102] - $game_variables[1072] <= $game_variables[1103]

       Set Move Route: [Monster 1] (Wait)

         Change Speed: 4

         Move Toward Player

 Else

Branch End

----------------

# If enemy 1 is below the target and less then or equal to $game_variables[1103] away, move toward player

Conditional Branch: Variable [1077:Current Y Coord ] >= Variable  [1102: Enemy 1 Y Coord]

   Conditional Branch: Script: $game_variables[1072] - $game_variables[1102] <= $game_variables[1103]

       Set Move Route: [Monster 1] (Wait)

         Change Speed: 4

         Move Toward Player

 Else

Branch End

-----------------

 The hashtags are what the code is supposed to be doing. It works... sort of. I noticed that the creatures continue to agro toward the player though, regardless of distance. I'm not sure if I have my Scripted Conditional Branch working correctly.

 Any Ideas?
 

TheTrueCyprien

Multipurpose Dude
Veteran
Joined
Jan 23, 2014
Messages
42
Reaction score
12
First Language
German
Primarily Uses
You only check the distance between player and enemy in x or in y direction. So if the player is in aggro range in x direction it will move towards the player regardless the y direction.
 

Manofdusk

Veteran
Veteran
Joined
Apr 26, 2014
Messages
211
Reaction score
39
First Language
English
Primarily Uses
 I would either have to use a nested if statement or a script based conditional branch to check them both at the same time ( "||" is the symbol for "or" correct?)

it would look something like this:

Conditional Branch: Script: $game_variables[1101] >= $game_variables[1076] ||  $game_variables[1102] >= $game_variables[1077]

Conditional Branch: Script: $game_variables[1101] - $game_variables[1076] <= $game_variables[1103] || $game_variables[1102] - $game_variables[1072] <= $game_variables[1103]

Set Move Route: [Monster 1] (Wait)

         Change Speed: 4

         Move Toward Player

 Else

Branch End

--------------------

Conditional Branch: Script: $game_variables[1101] <= $game_variables[1076] ||  $game_variables[1102] <= $game_variables[1077]

Conditional Branch: Script: $game_variables[1076] - $game_variables[1101] <= $game_variables[1103] || $game_variables[1077] - $game_variables[1102] <= $game_variables[1103]

Set Move Route: [Monster 1] (Wait)

         Change Speed: 4

         Move Toward Player

 Else

Branch End

----------------

 or am I over-complicating things?
 

TheTrueCyprien

Multipurpose Dude
Veteran
Joined
Jan 23, 2014
Messages
42
Reaction score
12
First Language
German
Primarily Uses
If statements seperated from each other won't do as you want them to. Also the or statement will just ignore one of the conditions if the other one is still met, so that's also not what we want.

The right way to do it would be something like:

Conditional Branch: Script: c=$game_variables[1101]-$game_variables[1076];c<=$game_variables[1103] && c>=-$game_variables[1103]

    Conditional Branch: Script: c=$game_variables[1102]-$game_variables[1077];c<=$game_variables[1103] && c>=-$game_variables[1103]

         Change Speed: 4

         Move Toward Player

    Branch End

Branch End
 
Last edited by a moderator:
Joined
Jul 2, 2014
Messages
7
Reaction score
0
First Language
English
Primarily Uses
Can you use a distance equation instead?  It looked like there were built in math functions (I'm not using them here and didn't look them up because I'm lazy)

d = sqrt [(x1-x2)^2 + (y1-y2)^2]

if d  <= aggro

  Change Speed: 4

  Move Toward player

Branch End

Sorry that this is just psuedocode, but you'll end up with a circle(approximately) around your player that triggers enemies instead of a square.  I don't know how the
"Move Toward player" part works, I'm pretty new to this.
 

TheTrueCyprien

Multipurpose Dude
Veteran
Joined
Jan 23, 2014
Messages
42
Reaction score
12
First Language
German
Primarily Uses
Can you use a distance equation instead?  It looked like there were built in math functions (I'm not using them here and didn't look them up because I'm lazy)

d = sqrt [(x1-x2)^2 + (y1-y2)^2]

if d  <= aggro

  Change Speed: 4

  Move Toward player

Branch End

Sorry that this is just psuedocode, but you'll end up with a circle(approximately) around your player that triggers enemies instead of a square.  I don't know how the

"Move Toward player" part works, I'm pretty new to this.
Yes, you can: d=Math.sqrt(($game_variables[1101]-$game_variables[1076])**2+($game_variables[1102]-$game_variables[1077])**2);d<=$game_variables[1103]

"Move Toward Player" is just a move route command that makes the event move one step towards the player.
 
Last edited by a moderator:

Manofdusk

Veteran
Veteran
Joined
Apr 26, 2014
Messages
211
Reaction score
39
First Language
English
Primarily Uses
sorry it's been so long since I got back to this but life happened and took me away for a while.

 I'm having a little trouble with the code TheTrueCyprien gave me.

I had to change d to $game_variables[x] and then create a conditional event. The whole thing looks like this now:

----------------------------------------------------

@>Script:  $game_variables[1078]=Math.sqrt(($game_variables[1101]-$game_variables[1076])
:              : **2+($game_variables[1102]-$game_variables[1077])**2); $game_variables[1078]<=
:              : $game_variables[1103]

if $game_variables[1078] <= $game_variables[1103]

@>Set Move Route: [Monster 1]

:                               : $>Change Speed: 4

:                               :$>Move toward Player

Else

@>

Branch End

@>

------------------------------------------

 however, I get this error:

unexpected tPOW, expecting ')'

**2+($game_variables[1102]-$gam...

 I tried it without the $game_variables[1078]<=$game_variables[1103] but got the same error.

once I get this last bug hammered out, I think it'll be more or less ready to release for use :p
 
Last edited by a moderator:

Wavelength

MSD Strong
Global Mod
Joined
Jul 22, 2014
Messages
5,624
Reaction score
5,104
First Language
English
Primarily Uses
RMVXA
The reason you're getting that "Unexpected tPOW" bug is because there is a line break in an unacceptable place in your script command.

I can't tell you the correct place to break it (perhaps after the subtraction symbol), since when I run into things like this I usually either place it in one line in the Script Editor and then call it, or I break it up into smaller chunks (such as saying a = ($game_variables[1102]-$game_variables[1077])**2 and then substituting the variable a for that whole thing in the equation).

But if you play around with the line breaks or you use a workaround like breaking it into chunks and then filling the chunks into one equation, I think it'll work for you.
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,202
First Language
Binary
Primarily Uses
RMMZ
$game_variables[1078]=Math.sqrt(($game_variables[1101]-$game_variables[1076])**2+($game_variables[1102]-$game_variables[1077])**2); $game_variables[1078]<=$game_variables[1103]Is not a correct way to break your lines.

A more accurate way of breaking the lines would be...

$game_variables[1078]=Math.sqrt(($game_variables[1101]-$game_variables[1076])**2+($game_variables[1102]-$game_variables[1077])**2)$game_variables[1078] <= $game_variables[1103]Also - not sure why you have the " $game_variables[1078] <= $game_variables[1103] " as that is basically asking, is variable 1078 lower or equal to variable 1103. and then it is doing absolutely nothing with the result...

Personally, I would break it up like so... (and remove the unnecessary true false check...)

Code:
v1 = $game_variables[1101]v2 = $game_variables[1076]v3 = $game_variables[1102]v4 = $game_variables[1077]$game_variables[1078] = Math.sqrt( ((v1-v2)**2)+((v3-v4)**2) )
 
Last edited by a moderator:

Manofdusk

Veteran
Veteran
Joined
Apr 26, 2014
Messages
211
Reaction score
39
First Language
English
Primarily Uses
well, it seemed like it was going to work. When I first tried the code, it ran and seemed to do just what I was hoping for it to.

 However, as soon as I redid the code for the other 4 instances (different game variables and different local variables (I went v1, v2, v3, v4, v5, v6, v7, ect. all the way up to v20) I get this error.

undefined local variable or method 'v1' for

#<Game_Interpreter:0x92871e4>

v1-v4 worked fine before I added v5-v20. I got all the variables correct so I'm not sure what's happening.
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,202
First Language
Binary
Primarily Uses
RMMZ
Dunno what to tell you I'm afraid. Clearly, within the script call youare using at the time, the variables are not defined. If you are seperating them into individual script call boxes then that wont work, other than that, I dont have a clue ^_^

Could try posting your code / event pages / whatever to give us a more accurate idea of the overall situation. :)
 

Manofdusk

Veteran
Veteran
Joined
Apr 26, 2014
Messages
211
Reaction score
39
First Language
English
Primarily Uses
I figured out the problem

v1 = $game_variables[1101]

v2 = $game_variables[1076]

v3 = $game_variables[1102]

v4 = $game_variables[1077]

$game_variables[1078] = Math.sqrt( ((v1-v2)**2)+((v3-v4)**2) )

v5 = $game_variables[1104]
v6 = $game_variables[1076]
v7 = $game_variables[1105]
v8 = $game_variables[1077]
$game_variables[1117] = Math.sqrt( ((v1-v2)**2)+((v3-v4)**2) )

v9 = $game_variables[1107]
v10 = $game_variables[1076]
v11 = $game_variables[1108]
v12 = $game_variables[1077]
$game_variables[1118] = Math.sqrt( ((v1-v2)**2)+((v3-v4)**2) )
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,202
First Language
Binary
Primarily Uses
RMMZ
lol I knew it would be something silly like that :)
 
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:

Forum statistics

Threads
105,854
Messages
1,017,004
Members
137,562
Latest member
tamedeathman
Top