How to check pushablility of NPC at spot(x,y)

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

Moderators: marionline, SDHawk

Post Reply
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

How to check pushablility of NPC at spot(x,y)

Post by sheamkennedy »

I am attempting to check if the NPC at a specified (x,y) is fully pushable. I want the code to work no matter if the NPC at (x,y) is a copy or not.

I have the following code:

Code: Select all

if(read NPC(NPC at spot (x, y), NPCstat:pushability) == true)) then(
...do stuff
)
But it's not working as intended and I'm pretty sure it's wrong since I don't know what "read NPC" is meant to return. What return value is considered "full" pushability? I just guessed and decided to see if the stat was == to true but that's not working out.
Last edited by sheamkennedy on Sat Dec 23, 2017 8:48 am, edited 1 time in total.
⊕ 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
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:

Post by Bob the Hamster »

"read npc" documentation is pretty vague, but it links to <a href="http://hamsterrepublic.com/ohrrpgce/doc ... npc">alter npc</a> which uses the same values, and explains them in more detail

Anyway, what you want is:

Code: Select all

if&#40;read NPC&#40;NPC at spot &#40;x, y&#41;, NPCstat&#58;pushability&#41; == NPCpush&#58;full&#41;&#41; then&#40;
...do stuff
&#41; 
Hmmm.... actually, looking more carefully, just by odd coincidence, true == 1 and NPCpush:full == 1

So your original code actually should have worked, even though it was for the wrong reason... So maybe there is something else wrong here...

If NPC at spot (x, y) fails to find the NPC, it will return false instead of an NPC reference. false == 0 and "read NPC" can take either a reference, or an NPC ID number, and 0 is a valid NPC ID, so it could be checking the pushability of the wrong NPC.

Let's try debugging it like this:

Code: Select all

variable&#40;ref&#41;
ref &#58;= NPC at spot &#40;x, y&#41;

if&#40;ref == false&#41; then&#40;
  show string&#40;string sprintf&#40;0, $1="No NPC at %d,%d", x, y&#41;&#41;
&#41;else&#40;
  # Found an NPC at x, y
  if&#40;read NPC&#40;ref, NPCstat&#58;pushability&#41; == NPCpush&#58;full&#41;&#41; then&#40;
    show string&#40;string sprintf&#40;0, $1="NPC is fully pushable"&#41;&#41;
  &#41;else&#40;
    show string&#40;string sprintf&#40;0, $1="NPC was found, but is not fully pushable"&#41;&#41;
  &#41;
&#41; 
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Post by sheamkennedy »

Bob the Hamster wrote:Anyway, what you want is...
Alright so I went ahead and debugged it and was able to construct the correct code to check if NPC at (x,y) is fully pushable. However I am still having an issue with using this code in my Line of Sight script.

I should explain, I want my LOS script such that my guard cannot see through zone #21 and cannot see through NPCs who have full pushability. Currently the LOS script seems to work fine with preventing sight through zone #21 but occasionally the guard can see my hero through the box (not always though).

This leads me to believe that my code is correct but since it's checking 2 different conditions (whether NPC exists at x,y && whether said NPC is pushable) it sometimes fails because the guard NPC is in motion and thus the conditions are referring to different x,y coordinates in that split-second.

Here's an idea of how my code is setup. It is called in the autorun while-loop:

Code: Select all

if &#40;&#40;read zone &#40;21, xNPC, yNPC--2&#41; == true&#41; 
    || &#40;&#40;&#40;NPC at spot &#40;xNPC, yNPC--2&#41; <> 0&#41;&#41; && &#40;read NPC&#40;get NPC ID&#40;NPC at spot &#40;xNPC, yNPC--2&#41;&#41;, NPCstat&#58;pushability&#41; == NPCpush&#58;full&#41;&#41;      
	&#41; then&#40;continue&#41; 
	else if &#40;&#40;xMe == xNPC&#41; && &#40;yMe == yNPC--2&#41;&#41; then&#40;
...CHASE THE HERO
Note: that "continue" in the code just allows the LOS script to escape a for-loop which resets the entire chain of sight iterations.

So now I'm wondering if this is the reason for my code failing is there any way I can rewrite it to avoid this issue? Or am I doing something else wrong all together.
⊕ 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
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

'continue' doesn't escape out of a for loop, 'break' does.

But the fact that you're using a hard-coded relative position, (xNPC, yNPC--2), makes me concerned that you aren't using a for loop to loop over a column of tiles in-between the guard and the hero when you should be.

Anyway, if it works for zones but not heroes, I guess that you're correct that the problem is an NPC in mid-movement not being considered to be at the tile that you expect it to be. 'npc x', 'npc y' and 'npc at spot' take an npc's position to be the tile that its top-left corner is on. Which is almost certainly not what you want in this case; instead you will want to ise the tile its centre is on. To do that, replace 'NPC at spot' with
NPC at pixel(x * 20 + 10, y * 20 + 10)
where x,y is the position measured in tiles.

Also, in the code you posted "get NPC ID" is redundant and can be removed, and you have lots of redundant brackets which make the code hard to read.
Last edited by TMC on Sun Dec 24, 2017 12:02 pm, edited 1 time in total.
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Post by sheamkennedy »

TMC wrote:'continue' doesn't escape out of a for loop, 'break' does.
Oh yeah by mistake. The continue has another purpose and the break comes later on. It's been a while since I wrote this so I kind of forgot. I'm pretty sure the overall structure of the code works properly up until I added the NPC at spot check.

I'll try referencing everything from the NPC at pixel centre point like you said. I'm sure that should solve things if the issue is what I suspect. Thanks.
⊕ 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
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Post by sheamkennedy »

TMC wrote:To do that, replace 'NPC at spot' with NPC at pixel(x * 20 + 10, y * 20 + 10)
Unfortunately I seem to be getting the same result after switching my code to this. I'm going to review the LOS script on the Wiki as I may be able to easily modify it for what I'm doing.
⊕ 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
Post Reply