Don't go near the light!

Hi everyone,

Attempting to come up with a solution that involves the least amount of conditional statement nesting as possible.

Here's the scenario:

Player must stay out of Event X's "circumference of light". The circumference in question is simply determined by an arrangement of tiles around the event, and if the player steps on one of those tiles, they get the penalty.

The event is also moving, so the player stay out of the spotlight effect and make their way around the event while trying to reach their goal.

Where I'm stuck is, is how to effectively manage the space around the moving event. The area that the light covers is four tiles wide and four tiles long, but with each of the corners cut out, like this:

XXX
XXXXX
XXXXX
XXXXX
XXX

Of course that area will always be moving with the event, so I'll need to constantly track that area, and also be able to inquire if the player has stepped into that space.

One CRUDE solution, which I'm probably going to go with if nothing else is suggested, is to actually invisible events all around the main event in the spaces I mentioned, and have them touch-activated. I can then penalize the player anytime they step on one.

I think the problem here is, if it's "player touch" and not "event touch", the events won't trigger if the player is still and the event comes onto their tile.

So, anyone got any thoughts on this?

Thanks!

EDIT: So I've had an experiment with the "Player Touch" and "Event Touch" triggers with some peculiar results.

"Event Touch" will work in both cases of the event or the player making the contact, however there seems to be some odd caveats to this. If the event is set below the player with through ON, the event will not trigger if it touches the player, however the opposite will work.

If the event is set to the Same as Player with through ON again, then neither case seems to work. It doesn't work if I run into the event, and it doesn't work if I'm still and the event runs into me.

Suffice to say, if Event Touch doesn't work with "Through" on, then when the edges of the flame light travel into out of bounds areas on the map, the events will get stuck and end up in all the wrong places. So another solution may be necessary.
I did something similar (but for a completely different purpose) using a script call within a parallel process event. Something like this (I'm not testing it, so the syntax and logic might need to be tweaked):

Code:
var px = $gamePlayer.x; var py = $gamePlayer.y;
var evt = $gameMap.event(id)
var ex1 = evt.x - 2; var ex2 = evt.x + 2
var ey1 = evt.y - 2; var ey2 = evt.y + 2
if (px >= ex1 && px <= ex2 && py >= ey1 && py <= ey2)
  if ((px !== ex1 && px !== ex2) || (py !== ey1 && py !== ey2))
    $gameSwitches.setValue(15, true);

So what's the above doing? First 4 lines are getting the x and y coordinates of the player and the event (replace id with the event id = EV002 would be 2, not 2 and not EV002 and not whatever name you gave that event)

Code:
if (px >= ex1 && px <= ex2 && py >= ey1 && py <= ey2)
checks that the player is within that 5x5 grid surrounding the event

Code:
if ((px !== ex1 && px !== ex2) || (py !== ey1 && py !== ey2))
checks that the player is not on one of the corners of that 5x5 grid

and
Code:
$gameSwitches.setValue(15, true);
is whatever you want to happen if the player happens to be on one of those 'danger' tiles (replace 15 with the id of the switch you want to turn on). Ideally, that switch would both do whatever action you want when the player gets 'caught' and will also stop the event that contains this script call from running ... unless you just want to do something like execute some damage, rest a few seconds to let the player get out of the way, and then let them resume, continuing to check.

Add a Wait 4 Frames before your script call, as you really don't need it to check 60 times a second, as it'll cause your game to lag.

ps - your area of light is actually 5 tiles wide and 5 tiles high, not 4.


In fact, you could do all of that just using event commands and not script calls at all. And it wouldn't be terribly complicated. If you want to go that route, just say so, and I'll give you the commands to use.
Thanks for the response @Shaz

I did something similar (but for a completely different purpose) using a script call within a parallel process event. Something like this (I'm not testing it, so the syntax and logic might need to be tweaked):

I think this will probably work, I'll just need to adjust it for an arrangement of values that don't make up a square.

In fact, you could do all of that just using event commands and not script calls at all. And it wouldn't be terribly complicated. If you want to go that route, just say so, and I'll give you the commands to use.

I probably will go with the script, but I'm interested to see what it would it look like without one.

EDIT: Let's say the shape changes drastically, and now I want 7x7:

X
XXX
XXXXX
XXXXXXX
XXXXX
XXX
X

That introduces *a lot* of area where the player is still within the initial 7x7 grid but could get away without a penalty. What is the most effective way of checking for all those blank space areas?

I've already worked out that I'll need 8 more variables (I think - + / - 3, + / - 2 for each axis) but I guess I'm concerned about how to structure this part:

if ((px !== ex1 && px !== ex2) || (py !== ey1 && py !== ey2))

I could continue a massive chain of if statements, but if I could utilize the logical OR operator within a statement, that could save a lot of space, right?

I don't know if this allowed, but this is sort of what I mean (don't worry about the variable names, just the structure):

if (px !== ex1 || px !== ex2 || px !== ex3 || px !== exm1 || px !== exm2 || px !== exm3 && py === ey3)

EDIT AGAIN: So I finally found a solution that works, and this is the script I ended up using:

var px = $gamePlayer.x; var py = $gamePlayer.y; var ex = $gameMap.event(24).x; var ey = $gameMap.event(24).y; var distx = ex - px; var disty = ey - py;

if (distx < 0){

distx = -distx;

}

if (disty < 0){

disty = -disty

}

var dist = distx + disty;

if (dist <=3){

$gameSwitches.setValue(170, true);

}

Still be interested to see the non-script version of this though.
One possible way i found after i just found stuff that goes above my current skills.
This helped but iam not totally through with absorbing it.
https://www.mathelounge.de/11656/bestimme-zu-jeder-geraden-die-steigung-m#a11674

We take 2 special triangle legs. Those got a gradient, the upper one for example:
+ gradient triangle leg: 2x = -1y or 1x = -0,5y // - gradient triangle leg: 2x = 1y or 1x = 0,5y
+ gradient triangle leg: 2 Steps right on the X axis results in -1 Step but up in the Y Axis(mv y axis seems reversed).
Side Info: We are working with Integers so hopefully everything under 1 is rounded down, for example 3,9 = 3

.....x x x x x x x x x x x x
.............0 1 2 3 4 5 6

-y3...........................6
-y2....................4 5 6
-y1.............2 3 4 5 6 + gradient triangle leg
y0......0 1 2 3 4 5 6 y zero line // number 0 is also position of guard watching right, we can move the guards x position inside the triangle later on.
+y1..............2 3 4 5 6 - gradient triangle leg
+y2......................4 5 6
+y3.............................6

px = Player.x - Guard.x = distance on x (Lets make px = 3) (Distance on x is 3)
py = Player.y - Guard.y = Lets make py = -2 and lets later find out if this y is above or below the +gradient triangle leg or on the same line.
c = Guard Vision minimum range (later on it depends if you use the > or the >= to vary the outcome, lets worry about that later)
d = Guard Vision max Range on x axis = 6
m = Your +Gradient (x1 = y0,5) = 0,5
G = y position of a unknown point on triangle leg, its x position is determind by px, its y by px * m

Three main Condition Blocks we seem to need.
1.
If Guard is watching right and a is between c and d // if yes: continue below -- else player is not in vision range on x coordinate.
2.
We do px * 0,5 = 3 * 0,5 = 1,5 = 1 (y = 1) -- MVs y axis seems vice versa (So its -1 not 1) = -1 // Guardy(0) +(-1) = Guard.y is -1 now

is py more or equal to -1
is -2 more or equal to -1 if yes players y position is on or below the +gradient triangle leg.
if no: the player y is not inside the triangle. That was the case in this example.
But if its yes, we also need to get to the third condition for the -gradioent leg.

But this is very complicate and iam relearning and learning it myself, so explaining is even harder.
Cant sit very long, got some unexected back problems after finaly being healed of back problems for some time.
Atleast this is more than the standard Circular or Rectangle shape.
This could also be done with Screen coordinates for more finetuning, but calculating in maptiles seems to be ok at the moment.

Edit: Fixed some hard errors. Hope there are not more =). But i find good to see a Way that doesnt need angles,volumes,cosinus sinus or square root or other stuff that makes my head smoke at the moment.
Edit2: I made much more error and fliped numbers in calculations to wrong positions.
this way is still valid, the above one is just not error free. I struggle with coordinates but its fun to practice. Sorry for posting such unfinished answer, it just takes to much work and time to finish specialy explaining while not having this in particular not build one time. So maybe better this than nothing for the next month =). I wonder why noone made a plugin yet?

Original thread: https://forums.rpgmakerweb.com/threads/dont-go-near-the-light.113443/