Post new topic    
Liquid Metal Slime
Send private message
Line of sight not working while my guard NPC is in motion... 
 PostSun Oct 19, 2014 4:56 am
Send private message Reply with quote
Awhile ago I made a Line of Sight code which I thought I had working properly. Turns out the script only worked while my guard NPC was stationary and doesn't work as expected when the guard is walking.

Essentially what I think is happening is the code keeps retriggering before it has completed due to the NPC walking for some reason. I'd like to know if there is a way around this. One particular problem I've noticed is the NPC will fail to see my player even though I have positioned the player clearly in the peripheral line of sight.

Here's a snippet of the code. It may seem large and daunting but don't feel the need to read it all. The code is fairly repetitive and I think you'll get the idea after reading about 10 lines.

Code:

...
   # Line of sight sweep for east-facing NPC
   While (NPC direction (0) == east) do(
   
      # Line of sight point east
         #####   
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         #wait (1)
         atxNPC := NPC X (0)
         atyNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         #If ((xNPC == atxNPC) && (yNPC == atyNPC)) then(
            
         If ((read zone (10, xNPC+1, yNPC) == true) || ((NPC at spot (xNPC+1, yNPC) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+1, yNPC, west)
            If ((read zone (11, xNPC+1, yNPC) == true) || ((xNPC+1 == x) && (yNPC == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+2, yNPC) == true) || ((NPC at spot (xNPC+2, yNPC) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+2, yNPC, west)
            If ((read zone (11, xNPC+2, yNPC) == true) || ((xNPC+2 == x) && (yNPC == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+3, yNPC) == true) || ((NPC at spot (xNPC+3, yNPC) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+3, yNPC, west)
            If ((read zone (11, xNPC+3, yNPC) == true) || ((xNPC+3 == x) && (yNPC == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+4, yNPC) == true) || ((NPC at spot (xNPC+4, yNPC) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+4, yNPC, west)
            If ((read zone (11, xNPC+4, yNPC) == true) || ((xNPC+4 == x) && (yNPC == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+5, yNPC) == true) || ((NPC at spot (xNPC+5, yNPC) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+5, yNPC, west)
            If ((read zone (11, xNPC+5, yNPC) == true) || ((xNPC+5 == x) && (yNPC == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )   
         
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+6, yNPC) == true) || ((NPC at spot (xNPC+6, yNPC) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+6, yNPC, west)
            If ((read zone (11, xNPC+6, yNPC) == true) || ((xNPC+6 == x) && (yNPC == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         
      # Line of sight sweep for east-facing NPC A
   #
   #
   #       xxx
   #   0xxx
   #
   #
   #
   
      # Line of sight point east
         #####   
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         
         If ((read zone (10, xNPC+1, yNPC) == true) || ((NPC at spot (xNPC+1, yNPC) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+1, yNPC, west)
            If ((read zone (11, xNPC+1, yNPC) == true) || ((xNPC+1 == x) && (yNPC == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+2, yNPC) == true) || ((NPC at spot (xNPC+2, yNPC) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+2, yNPC, west)
            If ((read zone (11, xNPC+2, yNPC) == true) || ((xNPC+2 == x) && (yNPC == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+3, yNPC) == true) || ((NPC at spot (xNPC+3, yNPC) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+3, yNPC, west)
            If ((read zone (11, xNPC+3, yNPC) == true) || ((xNPC+3 == x) && (yNPC == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+4, yNPC--1) == true) || ((NPC at spot (xNPC+4, yNPC--1) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+4, yNPC--1, west)
            If ((read zone (11, xNPC+4, yNPC--1) == true) || ((xNPC+4 == x) && (yNPC--1 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+5, yNPC--1) == true) || ((NPC at spot (xNPC+5, yNPC--1) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+5, yNPC--1, west)
            If ((read zone (11, xNPC+5, yNPC--1) == true) || ((xNPC+5 == x) && (yNPC--1 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )   
         
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+6, yNPC--1) == true) || ((NPC at spot (xNPC+6, yNPC--1) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+6, yNPC, west)
            If ((read zone (11, xNPC+6, yNPC--1) == true) || ((xNPC+6 == x) && (yNPC--1 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )   
         #wait (1)
         #)      
         
         
         # Line of sight sweep for east-facing NPC B
   #
   #       xx
   #     xx
   #   0x
   #
   #
   #
   
      # Line of sight point east
         #####   
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
            
         If ((read zone (10, xNPC+1, yNPC) == true) || ((NPC at spot (xNPC+1, yNPC) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+1, yNPC, west)
            If ((read zone (11, xNPC+1, yNPC) == true) || ((xNPC+1 == x) && (yNPC == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+2, yNPC--1) == true) || ((NPC at spot (xNPC+2, yNPC--1) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+2, yNPC--1, west)
            If ((read zone (11, xNPC+2, yNPC--1) == true) || ((xNPC+2 == x) && (yNPC--1 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+3, yNPC--1) == true) || ((NPC at spot (xNPC+3, yNPC--1) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+3, yNPC--1, west)
            If ((read zone (11, xNPC+3, yNPC--1) == true) || ((xNPC+3 == x) && (yNPC--1 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+4, yNPC--2) == true) || ((NPC at spot (xNPC+4, yNPC--2) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+4, yNPC--2, west)
            If ((read zone (11, xNPC+4, yNPC--2) == true) || ((xNPC+4 == x) && (yNPC--2 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+5, yNPC--2) == true) || ((NPC at spot (xNPC+5, yNPC--2) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+5, yNPC--2, west)
            If ((read zone (11, xNPC+5, yNPC--2) == true) || ((xNPC+5 == x) && (yNPC--2 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         
         # Line of sight sweep for east-facing NPC C
   #
   #      x
   #    xx
   #   0
   #
   #
   #
   
      # Line of sight point east
         #####   
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
            
         If ((read zone (10, xNPC+1, yNPC--1) == true) || ((NPC at spot (xNPC+1, yNPC--1) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+1, yNPC--1, west)
            If ((read zone (11, xNPC+1, yNPC--1) == true) || ((xNPC+1 == x) && (yNPC--1 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+2, yNPC--1) == true) || ((NPC at spot (xNPC+2, yNPC--1) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+2, yNPC--1, west)
            If ((read zone (11, xNPC+2, yNPC--1) == true) || ((xNPC+2 == x) && (yNPC--1 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+3, yNPC--2) == true) || ((NPC at spot (xNPC+3, yNPC--2) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+3, yNPC--2, west)
            If ((read zone (11, xNPC+3, yNPC--2) == true) || ((xNPC+3 == x) && (yNPC--2 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         
         # Line of sight sweep for east-facing NPC D
   #
   #
   #       
   #   0xxx
   #       xxx
   #
   #
   
      # Line of sight point east
         #####   
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         
         If ((read zone (10, xNPC+1, yNPC) == true) || ((NPC at spot (xNPC+1, yNPC) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+1, yNPC, west)
            If ((read zone (11, xNPC+1, yNPC) == true) || ((xNPC+1 == x) && (yNPC == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+2, yNPC) == true) || ((NPC at spot (xNPC+2, yNPC) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+2, yNPC, west)
            If ((read zone (11, xNPC+2, yNPC) == true) || ((xNPC+2 == x) && (yNPC == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+3, yNPC) == true) || ((NPC at spot (xNPC+3, yNPC) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+3, yNPC, west)
            If ((read zone (11, xNPC+3, yNPC) == true) || ((xNPC+3 == x) && (yNPC == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+4, yNPC+1) == true) || ((NPC at spot (xNPC+4, yNPC+1) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+4, yNPC+1, west)
            If ((read zone (11, xNPC+4, yNPC+1) == true) || ((xNPC+4 == x) && (yNPC+1 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+5, yNPC+1) == true) || ((NPC at spot (xNPC+5, yNPC+1) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+5, yNPC--1, west)
            If ((read zone (11, xNPC+5, yNPC+1) == true) || ((xNPC+5 == x) && (yNPC+1 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )   
         
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+6, yNPC+1) == true) || ((NPC at spot (xNPC+6, yNPC+1) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+6, yNPC, west)
            If ((read zone (11, xNPC+6, yNPC+1) == true) || ((xNPC+6 == x) && (yNPC+1 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )   
         #wait (1)
         #)      
         
         
   # Line of sight sweep for east-facing NPC E
   #
   #       
   #     
   #   0x
   #     xx
   #       xx
   #
   
      # Line of sight point east
         #####   
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
            
         If ((read zone (10, xNPC+1, yNPC) == true) || ((NPC at spot (xNPC+1, yNPC) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+1, yNPC, west)
            If ((read zone (11, xNPC+1, yNPC) == true) || ((xNPC+1 == x) && (yNPC == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+2, yNPC+1) == true) || ((NPC at spot (xNPC+2, yNPC+1) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+2, yNPC+1, west)
            If ((read zone (11, xNPC+2, yNPC+1) == true) || ((xNPC+2 == x) && (yNPC+1 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+3, yNPC+1) == true) || ((NPC at spot (xNPC+3, yNPC+1) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+3, yNPC+1, west)
            If ((read zone (11, xNPC+3, yNPC+1) == true) || ((xNPC+3 == x) && (yNPC+1 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+4, yNPC+2) == true) || ((NPC at spot (xNPC+4, yNPC+2) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+4, yNPC+2, west)
            If ((read zone (11, xNPC+4, yNPC+2) == true) || ((xNPC+4 == x) && (yNPC+2 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+5, yNPC+2) == true) || ((NPC at spot (xNPC+5, yNPC+2) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+5, yNPC+2, west)
            If ((read zone (11, xNPC+5, yNPC+2) == true) || ((xNPC+5 == x) && (yNPC+2 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
            
      
   # Line of sight sweep for east-facing NPC F
   #
   #     
   #   
   #   0
   #    xx
   #      x
   #
   
      # Line of sight point east
         #####   
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
            
         If ((read zone (10, xNPC+1, yNPC+1) == true) || ((NPC at spot (xNPC+1, yNPC+1) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+1, yNPC+1, west)
            If ((read zone (11, xNPC+1, yNPC+1) == true) || ((xNPC+1 == x) && (yNPC+1 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+2, yNPC+1) == true) || ((NPC at spot (xNPC+2, yNPC+1) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+2, yNPC+1, west)
            If ((read zone (11, xNPC+2, yNPC+1) == true) || ((xNPC+2 == x) && (yNPC+1 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )
         #####
         xNPC := NPC X (0)
         yNPC := NPC Y (0)
         x := hero x (find hero (0))
         y := hero y (find hero (0))
         
         If ((read zone (10, xNPC+3, yNPC+2) == true) || ((NPC at spot (xNPC+3, yNPC+2) <> 0))) then(
            break
            )
         else (
         create NPC (1, xNPC+3, yNPC+2, west)
            If ((read zone (11, xNPC+3, yNPC+2) == true) || ((xNPC+3 == x) && (yNPC+2 == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)
         #wait (1)
         )         
         
         
            
         #wait (1)
         #)
   wait (1)
   )
...

⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
⍠ C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
Metal King Slime
Send private message
Re: Line of sight not working while my guard NPC is in motio 
 PostSun Oct 19, 2014 6:43 am
Send private message Reply with quote
Code:
x := hero x (find hero (0))

This is wrong: hero x takes position in the caterpillar party, but find hero returns a party slot number.

Code:
         create NPC (1, xNPC+1, yNPC, west)
            If ((read zone (11, xNPC+1, yNPC) == true) || ((xNPC+1 == x) && (yNPC == y)))  then(
               play sound (4, false, false)
            )
         #wait (1)
         destroy NPC (1)

You create an NPC, do nothing with it, then destroy it. Was this for debugging (judging by the wait(1))?

The whole script is a giant loop that looks like
Code:
   # Line of sight sweep for east-facing NPC
   While (NPC direction (0) == east) do(
     # break if NPC 0 can see a certain zone or another NPC to its east
     # play a sound if the hero is seen, assuming a zoned tile/NPC wasn't detected first
     wait(1)
   )

This makes no sense and definitely never was working correctly.

Also you suggested that double triggering was an issue but didn't say how this script was triggered. Well, is it being called incorrectly? You can use the script debugger and the trace/tracevalue commands to work that out.

Finally, this code organisation is pretty bad. Never ever copy-paste a block of code 40 times and make a small change to each copy. Instead, put it in a separate script and call it 40 times with differing arguments. Then you can try to simplify further by using for loops instead of calling with 40 hand computed tile offsets.

Also, instead of casting lines out from the guard the alternative is to check every tile in a line between the guard and the player. That's what the [url=rpg.hamsterrepublic.com/ohrrpgce/Scripts:Line_of_sight]Line of sight script[/url] on the wiki does. By changing a few lines of that script it would be very similar to yours (including "rear view distance := 0"), however it doesn't give the guard a shorter line of sight at the edges of their vision. That would require some extra math.

Assuming that you don't want to switch to the script on the wiki, which has an awesome debug mode, the first thing you need to do is cleanup your script as I said, to fix all the erroneous 'break's.
Liquid Metal Slime
Send private message
 
 PostSun Oct 19, 2014 2:58 pm
Send private message Reply with quote
Thank you very much. I guess I didn't test the code with zones yet so didn't realize the code was breaking part way through.

Is there a simple way I can replace "break" with some sort of feature which skips a chunk of code and continues on with checking the next "ray of sight"?

Also I wrote this awhile ago when I was a bit inexperienced with coding. I will eventually clean up the code into something less ridiculously repetitive.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
⍠ C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
Metal King Slime
Send private message
 
 PostMon Oct 20, 2014 9:39 am
Send private message Reply with quote
I suggested moving the repeated block into a script because the cleanest way to write that kind of flow control (what you want is basically a goto past all the logic, if HamsterSpeak had it) is to break out of a script early. In this case where you have a whole chain of scripts that should be aborted you can connect them with 'if's. For example write a script that returns true if you should stop the ray-cast, and false otherwise, and also checks whether the player is on that tile. Then you can write
Code:

if (check sight blocked (x+1, y))
else if (check sight blocked (x+2, y))
else if (check sight blocked (x+3, y))
else if (check sight blocked (x+4, y+1))
else (check sight blocked (x+5, y+1))


But to give you the quick and dirty method you were looking for, you can emulate a goto by breaking out of a loop that loops just once.
Code:

while(true) do(
   #... some code
   if (something) then (break)
   #... more code
   break  #end of loop
)
Liquid Metal Slime
Send private message
 
 PostTue Oct 21, 2014 1:50 pm
Send private message Reply with quote
TMC wrote:
Code:

if (check sight blocked (x+1, y))
else if (check sight blocked (x+2, y))
else if (check sight blocked (x+3, y))
else if (check sight blocked (x+4, y+1))
else (check sight blocked (x+5, y+1))



Thanks again. This bit of code looks like it should be key to pulling this off efficiently. It'll be a lot of work cleaning up my script and using this, but it'll make things a whole lot more organized after I do.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
⍠ C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
Liquid Metal Slime
Send private message
 
 PostTue Oct 21, 2014 1:59 pm
Send private message Reply with quote
On a related note I was wondering if there's a suggested way to go about using this LOS script for many guards at once?

I was considering a few ways myself and think using a timer may be suitable. I'd likely have 4 guards (maximum) at once, so I was thinking of setting up a timer so it first check if guard #1 exists and is in range, then runs LOS for that guard. Checks if guard #2 is in range, then runs the LOS for that guard... etc... I figure so long as my areas aren't super dense with guards that this method should not show too much flaw.

Even so, I'd like to hear suggestions from others because once I find one solution I can find it hard to look past it for better solutions.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
⍠ C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
Metal King Slime
Send private message
 
 PostTue Oct 21, 2014 2:51 pm
Send private message Reply with quote
If you don't have any waits anywhere, then there's no problem and timers aren't needed. Aside from the commented out debug stuff and the wait at the end of the while loop which I said shouldn't be there, there are no waits. Personally I would just loop through all the guards each tick, checking their line of sight. The non-trivial part is what happens when a guard sees the player. Presumably they do something and switch to a different state.
Display posts from previous: