Line of Sight Script

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
guo
Metal Slime
Posts: 749
Joined: Fri Dec 04, 2009 9:12 pm

Line of Sight Script

Post by guo »

Hi! I want to use this script:

https://rpg.hamsterrepublic.com/ohrrpgc ... upon_it%3F

Code: Select all

plotscript, vigilant guards activate, begin 
  variable (this map) 

  this map := current map 

  while (this map == current map) do, begin 
    vigilant guard (0)  #change '0' to the correct NPC ID
    #vigilant guard (1) #repeat as desired
    wait (1)
  end 
end 

script, vigilant guard, NPC ID, begin
  variable (guard) 

  guard := NPC reference (NPC ID)

  variable (x1, x2, y1, y2, dir, dist, start chasing)

  x1 := NPC X (guard)
  x2 := hero X (me)
  y1 := NPC Y (guard)
  y2 := hero Y (me)
  dir := NPC direction (guard)
  
  dist := x2 -- x1
  if (y1 == y2) then (
    if (dir == right && dist <= 8 && dist >= 0) then (start chasing := true)
    if (dir == left && dist >= -8 && dist <= 0) then (start chasing := true)
  )
  dist := y2 -- y1
  if (x1 == x2) then (
    if (dir == down && dist <= 8 && dist >= 0) then (start chasing := true)
    if (dir == up && dist >= -8 && dist <= 0) then (start chasing := true)
  )

  if (start chasing) then ( 
    wait for NPC (NPC ID)
    Alter NPC (NPC ID, NPCstat:move type, NPCmovetype:chaseyoupathfinding) #you can change the NPCmovetype to suite your needs
    Alter NPC (NPC ID, NPCstat:move speed, 5) 
  ) 
end
The way enemies work in my game (most of the time) is that they trigger a battle when touched, then that copy of the NPC is destroyed after battle. I use multiple copies of the same NPC on a map. Is there a way I can have this script work on multiple copies of the same NPC? Or do I have to create a new NPC definition for each separate enemy roaming the map?
I should also mention that I am using a script that only allows NPCs to move when the player moves (roguelike-ish movement).

Any help appreciated!

Cheers.
vvight.wordpress.com
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Re: Line of Sight Script

Post by TMC »

Well, yes, using "alter npc" would cause all copies of the NPC to chase you. But what you can do is have just two copies of each NPC definition, which differ only by move type. Instead of using "alter npc", change the NPC's ID to the one that chases you.

Let's say the base copy of each NPC definition has ID X, and the chasing version has ID X+1. Change the last block of the "vigilant guard" script to:

Code: Select all

  if (start chasing) then ( 
    #wait for NPC (guard)   # This line ought to be unnecessary since recent OHR versions
    change NPC ID (guard, NPC ID + 1)
  ) 
In order to check all copies of each NPC definition, you could simply replace the top of the script:

Code: Select all

script, vigilant guard, NPC ID, begin
  variable (guard) 

  guard := NPC reference (NPC ID)
  
  ...
with

Code: Select all

script, vigilant guard, NPC ID, begin
  variable (guard, number) 
  for (number, 0, npc copy count(NPC ID) -- 1) do (
    guard := NPC reference (NPC ID, copy)
    check one vigilant guard (guard, NPC ID)
  )
end

script, check one vigilant guard, guard, NPC ID, begin
  ...
Make sure you only call "vigilant guard" on each non-alert NPC ID, not the alerted versions.
User avatar
guo
Metal Slime
Posts: 749
Joined: Fri Dec 04, 2009 9:12 pm

Re: Line of Sight Script

Post by guo »

This is amazing, thankyou!
vvight.wordpress.com
User avatar
guo
Metal Slime
Posts: 749
Joined: Fri Dec 04, 2009 9:12 pm

Re: Line of Sight Script

Post by guo »

In this part:

Code: Select all

script, vigilant guard, NPC ID, begin
  variable (guard, number) 
  for (number, 0, npc copy count(NPC ID) -- 1) do (
    guard := NPC reference (NPC ID, copy)
    check one vigilant guard (guard, NPC ID)
  )
end
specifically "guard := NPC reference (NPC ID, copy)" what should I have for "copy"? Should that be "number"?
Sorry if I'm missing something obvious.

Regards
vvight.wordpress.com
User avatar
Bob the Hamster
Lord of the Slimes
Posts: 7658
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Re: Line of Sight Script

Post by Bob the Hamster »

Looks like a typo, use "number" instead of "copy"
User avatar
guo
Metal Slime
Posts: 749
Joined: Fri Dec 04, 2009 9:12 pm

Re: Line of Sight Script

Post by guo »

Thankyou for the fast reply James
vvight.wordpress.com
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Re: Line of Sight Script

Post by TMC »

I don't test my code when I know it to be correct (excepting typos) ; )
User avatar
guo
Metal Slime
Posts: 749
Joined: Fri Dec 04, 2009 9:12 pm

Re: Line of Sight Script

Post by guo »

It is a talent I envy!

Would this script be ok to run as an each step instead of autorun? Or would it need modifying?

Also looking at adding an "else" condition... Awful pseudocode follows:

Code: Select all

If player is within x squares - change to chasing npc
ELSE change chasing npc back to idle npc
Would this be easy enough to plug in? Or am I going to run into pitfalls?

Regards
vvight.wordpress.com
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Re: Line of Sight Script

Post by TMC »

If NPCs can only move when the player moves, then (player) each-step would probably work, though it does make a difference what the player and NPC speeds are. Whoever has the higher speed finishs their step first, if they both start at the same time. It might lead to some quirks but probably won't break it.

You're very likely going to want to go back to idle on some condition other than "hero isn't immediately in front of the NPC", so wouldn't use an 'else'. For example, you could do so if the player is more than 9 tiles from the guard in any direction including diagonally. Decide what shape and size you want the area around the guard in which the player has to be to remain alert: square, diamond, circular. Or you could do it based on time since the NPC saw the player.
Post Reply