I am working on a line of sight script.
I have two sources of inspiration for this. One is the script for Jailbreak, and the other is a custom script that BMR created for me presumably based off of his line of sight mechanic in Legacy.
So here are the things it needs to do:
1 - Set a loop up that will continuously loop through the appropriate amount of npcs to check each ones line of sight.
-Jailbreak and Legacy seem to differ here, but I like BMR's approach which is to trigger it using a timer.
-Both methods use a FOR loop to initiate this, this seems to work well because even if you only need line of sight for one npc
you can just set the first and last to the same.
2 - check which direction a npc is facing and set the line of sight accordingly.
- BRM uses a switch statement, and Jailbreak just uses if commands. The former seems cleaner
EDIT - I left a crucial piece of information out! A cone-shaped line of sight WOULD be ideal, but I've started out just trying to get a simple 3 tile line in the direction the npc is facing.
3 - check if any of the tiles in between you and the npc are wall tiles
- The example script I got from BMR didn't include this part. I think it would work if I put a readpassblock command into the
switch statement segment to get it to work.
4 - trigger a separate script if you are found to be within the line of sight
- this will move the npc to the square directly in front of your character
With all of that considered let's get down to code. For a little context, this part fits into the scripts I have running that causes 3
guards to patrol a campsite one at a time. Here is where I start the timer:
Code: Select all
script, brigand, begin
set tag(21, on)
set tag(27,off)
variable(brig1,brig1x,brig1y)
skippable wait(150)
set tag(26, on)
if (check tag(28) == false) then (
#create the npc and start him walking around.
create NPC(2, 26, 14, east)
set timer(0,5,3,@guardcheck)
brig1 := NPC reference(2)
The loops for the 3 guards are basically identical in this regard.
Now here is the guardcheck script that BMR gave me, with a few modifications to get it to run etc...
Code: Select all
script, guardcheck, begin
#Declare all of the different variables
#--------------------------------------
variable(
mex #Hero X position
mey #Hero Y position
npc1 #Starting NPC to check
npc2 #Final NPC to check
npcd #NPC Direction
nx #NPC X
ny #NPC Y
cx #Current X, for checking X variables for LoS
cy #Current Y, for checking Y variables for LoS
wx #Working X
wy #Working Y
ctr #Counter, for looping through the different NPCs
hf #Hero Found, becomes TRUE if hero is within a certain distance
dt #Distance Threshold, how far to detect from
)
#--------------------------------------
#Generate all the variables
#--------------------------
if (check tag(22)) then ( npc1 := npc reference(2))
else if (check tag(23)) then(npc1:= npc reference(3))
else if (check tag(24)) then (npc1:= npc reference(4))
if (check tag(22)) then ( npc2 := npc reference(2))
else if (check tag(23)) then(npc2:= npc reference(3))
else if (check tag(24)) then (npc2:= npc reference(4))
hf := FALSE
ctr := 3
dt := 3 #Set the distance to detect at 3 tiles
#--------------------------
for(ctr, npc1, npc2) do(
npcd := npc direction(npc1)
nx := npc x(npc1)
ny := npc y(npc1)
mex := hero x(me)
mey := hero y(me)
switch(npcd) do(
case(north) do(
for(wy, ny, ny -- dt) do( if(wy == mey && nx == mex) then(hf := TRUE) )
)
case(south) do(
for(wy, ny, ny + dt) do( if(wy == mey && nx == mex) then(hf := TRUE) )
)
case(west) do(
for(wx, nx, nx -- dt) do( if(ny == mey && mex <= nx -- dt && mex >= nx) then(hf := TRUE) )
)
case(east) do(
for(wx, nx, nx + dt) do( if(ny == mey && mex >= nx && mex <= nx + dt) then(hf := TRUE) )
)
)
if(hf == TRUE) then(somethinghappens)
)
end
script, somethinghappens, begin
#Add what you want to happen here.
reset game
end
-In this case I ALWAYS want npc2 to be the same as npc1. I'm sure I could declare this in a prettier way.
-I was the one who put the messy-as-hell math parts into the east and west cases. I did not put it into north and south because it is easier for me to test if its working in my game with the east and west cases.
I put these in because while ny and mey HAVE to be the same, mex and nx are going to be different and within a certain range.
- I haven't implemented wall checking yet, because I want to be able to trigger the "somethinghappens" script successfully first. My idea of how to implement wall checking is by using a readpassblock statement inside the IF loop in each case.
Currently, nothing happens. I put a reset game command at the bottom underneath the if statement that calls somethinghappens, just to make sure the script was being triggered properly, and it is, but somehow HF isn't properly being set to true, so nothing happens.