How is Built-in Chase Scripted?

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

Moderators: marionline, SDHawk

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

How is Built-in Chase Scripted?

Post by sheamkennedy »

I was wondering what the formula/script is for the engine's built-in chase move type?

I want to know if chase works in a completely predictable way or if there is some randomness to which direction the NPC moves when there are two equally good paths between the Hero and NPC.
⊕ 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 »

With 50% chance, it tries to move closer to the hero in the X direction, otherwise in the Y direction. Strangely, if it already has the same X or Y coordinate, then it moves in a random direction with uniform chance. (It would only be necessary to move in a random direction if there is actually an obstacle). This is all done before checking for obstacles.

So this means that chase NPCs chase you faster when they're moving diagonally rather than in a straight line.

Here's the code (from pick_npc_action in game.bas), with the parts for wrapping maps and Flee NPCs removed for simplicity

Code: Select all

    IF randint&#40;100&#41; < 50 THEN
     IF caty&#40;0&#41; < npci.y THEN temp = 0
     IF caty&#40;0&#41; > npci.y THEN temp = 2
     IF caty&#40;0&#41; = npci.y THEN temp = randint&#40;4&#41;
    ELSE
     IF catx&#40;0&#41; < npci.x THEN temp = 3
     IF catx&#40;0&#41; > npci.x THEN temp = 1
     IF catx&#40;0&#41; = npci.x THEN temp = randint&#40;4&#41;
    END IF
    npci.dir = temp
    IF temp = 0 THEN npci.ygo = 20
    IF temp = 2 THEN npci.ygo = -20
    IF temp = 3 THEN npci.xgo = 20
    IF temp = 1 THEN npci.xgo = -20
Last edited by TMC on Wed Mar 15, 2017 10:44 pm, edited 3 times in total.
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 »

I should make a few other NPC movement types. I know someday they will be scriptable, but that is no excuse not to add a few more hard-coded ones now
User avatar
Taco Bot
Meat, Cheese, and Silicon
Posts: 484
Joined: Fri Jul 18, 2014 12:15 am
Location: Santa Cruz
Contact:

Post by Taco Bot »

Ooh! If you add/change movement types, maybe add the ability to change the wait between steps with the wander move type. IIRC it's currently random? It feels a little twitchy, and it'd be nice to make NPCs seem slower without making their movement speed 1. (And in higher FPS games, that still seems fast)
Sent from my iPhone
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Ah yes, Wander NPCs have a 1/4 chance to start a step every tick that they're not already walking. At 18 fps, that means they only pause for a ~fifth of a second, at higher frame rates it's no time at all.
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 »

Starting with the current nightly wip build, there are two new NPC movement types

"Chase You (Direct)" and "Avoid You (Direct)"

Image

The old semi-random Chase/Avoid types have not been changed, but they are now named "Chase You (Meandering)" and "Avoid You (Meandering)"

Direct Chase prefers to close the shortest distance first.
Direct Avoid prefers to lengthen the longest distance first.

Neither is smart about going around obstacles. That sort of thing will have to wait for some other new movement type in the future.

EDIT: (My gif recorder peppers the bottom half of the screen with random white pixels. I also notice that when I try to upload to imgur, it gets almost done with the upload (I can even see the preview animating) and then it fails with an unknown error. Opening and re-saving the animating gif with the Gimp fixes my upload problem)
Last edited by Bob the Hamster on Tue Mar 21, 2017 6:00 pm, edited 1 time in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Seeing it in action, it seems more useful than I thought it would be!

(Oh dear. I knew that there's a bug in the gif encoder that very rarely inserts bad pixels, but this is the first I've seen where they're obvious; I guess I will have to look into it after all)
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:
Thanks so much for adding this. I think the direct movement types will work a lot better for what I'm currently working on. I have started developing a tactical rouge lite where the enemies move every time you move and I want the enemies to close in on you as fast as possible in a fairly predictable way. Obstacle avoidance is not necessary in most cases.
⊕ 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 »

And next up we have "Follow Walls (Right)" and "Follow Walls (Left)"

These were fun to implement!

Image
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post by Foxley »

Oh man. I love these.
User avatar
Taco Bot
Meat, Cheese, and Silicon
Posts: 484
Joined: Fri Jul 18, 2014 12:15 am
Location: Santa Cruz
Contact:

Post by Taco Bot »

The follow walls one looks cool! I gotta think up some use for it. Reminds me of the spiky guys who float along walls in Super Mario Land 2 (or was it Wario Land?)
Sent from my iPhone
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 »

And a super cool thing about follow walls, is if an NPC has its movement restricted by zones, then those zones count as walls too
User avatar
Taco Bot
Meat, Cheese, and Silicon
Posts: 484
Joined: Fri Jul 18, 2014 12:15 am
Location: Santa Cruz
Contact:

Post by Taco Bot »

Bob the Hamster wrote:And a super cool thing about follow walls, is if an NPC has its movement restricted by zones, then those zones count as walls too
So I just got the zaniest idea: could you use this, in combination with restricted zones, to make (somewhat) complex npc movement patterns without using scripts? Aka: you could give them a weaving perimeter to pace by surrounding them with avoid zones?
Sent from my iPhone
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post by Foxley »

Oh my god. James just made MGS 1 & 2 style stealth gameplay possible using built-in NPC behavior and light scripting.
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 »

Taco Bot wrote:
Bob the Hamster wrote:And a super cool thing about follow walls, is if an NPC has its movement restricted by zones, then those zones count as walls too
So I just got the zaniest idea: could you use this, in combination with restricted zones, to make (somewhat) complex npc movement patterns without using scripts? Aka: you could give them a weaving perimeter to pace by surrounding them with avoid zones?
Yep! :)
Post Reply