[SOLVED][ZSCRIPT] Monster Stuck With Custom Chase Function

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
Kan3x
Posts: 67
Joined: Tue Nov 08, 2022 4:19 pm

[SOLVED][ZSCRIPT] Monster Stuck With Custom Chase Function

Post by Kan3x »

Hello guys.

I was trying to have a custom "chasing" function for a monster.
What I wanted to achieve is a monster moving away from its target when it's too close, but with specific escaping routes and the possibility to attack like it was in a normal chasing (this is why I completely put aside the easy way of just using the FRIGHTENED flag and call it a day).
I got the "avoid the target" and "avoid obstacles" done, working pretty well too, but then I tested the creature with normal steps... and it broke.

Basically, what happens is that the monster doesn't get block by the step like an actual obstacle, but he won't move forward anymore and won't climb the step, but only if the total height of the steps to climb exceeds the standard Doom monster limit of 24... even though the monster MaxStepHeight is 32... I even tried raising it up to 64, but this behavior didn't change.
Not to mention that if the monster target gets closer, sometimes the monster will climb up the steps...

To me even weirder, is when the monster tries to go downstairs instead, because in this case it will recognize the "stepping down" as an obstacle and just avoid it like a wall etc.

This is the code I'm using:
Spoiler:
I tried different moving functions, but nothing really change except with TryMove, but I'm getting some jittering results for some reason.

I wonder if there's some hardcoded stuff within CheckMove(), because I'm clueless about this behavior.

This is the monster class:
Spoiler:
I'd like to know if what I'm trying to do is possible without having to rewrite the zdoom chasing functions, otherwise I'll just surrender and use the FRIGHTENED flag T_T
Last edited by Kan3x on Thu Apr 04, 2024 12:34 pm, edited 1 time in total.
Kan3x
Posts: 67
Joined: Tue Nov 08, 2022 4:19 pm

Re: [ZSCRIPT] Monster Stuck on Steps With Custom Chase Function

Post by Kan3x »

Interesting enough, I guess, if I replace CheckMove() with TryMove() it "almost" works, but the guy is having a lot of jittering while moving D:
Any help on this? I'm totally stuck T_T like the monster...
7Soul
Posts: 44
Joined: Sat Mar 13, 2021 6:47 pm

Re: [ZSCRIPT] Monster Stuck on Steps With Custom Chase Function

Post by 7Soul »

I coded my own chase function too and the way I fixed the TryMove jitteriness was to use variables to track the direction and distance the monster wants to move, then use those values to move the monster in its Tick() function. So for example if the monster wants to move 8 units and its animation frame lasts 4 frames, it'll move 2 unit per tick 4 times, so it moves as smoothly as possible

This is a function that calculates the distance from the current position to the position the enemy wants to get to (nextPos) and sets the variables. You call this instead of TryMove
animSpeed is the length of the walk animation, you pass that value when you call it. This makes so the monster's position changes faster or slower depending on the animation speed

Code: Select all

void SetWalkSteps(vector2 nextPos, int animSpeed){
	vector2 a = (nextPos - pos.xy);
	double dist = floor(Sqrt( (a.x * a.x) + (a.y * a.y)) + 0.1) ;

	walkDirection = Angle;
	walkDist = abs(dist / animSpeed);
	walkSteps = animSpeed;
}
Enemy variables:

Code: Select all

int walkDirection;
int walkSteps;
double walkDist;
And this goes into Tick():

Code: Select all

if (walkDist > 0 && walkSteps > 0) 
	TryMove(pos.xy + AngleToVector(walkDirection, walkDist), 1);
walkSteps = max(walkSteps - 1, 0);

Hope this helps
Kan3x
Posts: 67
Joined: Tue Nov 08, 2022 4:19 pm

Re: [ZSCRIPT] Monster Stuck on Steps With Custom Chase Function

Post by Kan3x »

7Soul wrote: Tue Mar 19, 2024 8:05 am I coded my own chase function too and the way I fixed the TryMove jitteriness was to use variables to track the direction and distance the monster wants to move, then use those values to move the monster in its Tick() function. So for example if the monster wants to move 8 units and its animation frame lasts 4 frames, it'll move 2 unit per tick 4 times, so it moves as smoothly as possible

This is a function that calculates the distance from the current position to the position the enemy wants to get to (nextPos) and sets the variables. You call this instead of TryMove
animSpeed is the length of the walk animation, you pass that value when you call it. This makes so the monster's position changes faster or slower depending on the animation speed

Code: Select all

void SetWalkSteps(vector2 nextPos, int animSpeed){
	vector2 a = (nextPos - pos.xy);
	double dist = floor(Sqrt( (a.x * a.x) + (a.y * a.y)) + 0.1) ;

	walkDirection = Angle;
	walkDist = abs(dist / animSpeed);
	walkSteps = animSpeed;
}
Enemy variables:

Code: Select all

int walkDirection;
int walkSteps;
double walkDist;
And this goes into Tick():

Code: Select all

if (walkDist > 0 && walkSteps > 0) 
	TryMove(pos.xy + AngleToVector(walkDirection, walkDist), 1);
walkSteps = max(walkSteps - 1, 0);

Hope this helps
Thanks for the help!
This is quite an interesting route and it actually solved the "step up" issue! (I still can't understand the "magic" behind it, why do I need to take this detour?)
Although it cannot get rid of the "step down" one D: Because that's caused directly by the CheckMove() (or TryMove()) checks for obstacles that will always detect a "step down" bigger than the common 24 units as a wall, no matter the MaxStepHeight the monster has.
Kan3x
Posts: 67
Joined: Tue Nov 08, 2022 4:19 pm

Re: [SOLVED][ZSCRIPT] Monster Stuck With Custom Chase Function

Post by Kan3x »

Ok, I finally found a solution to apparently every glitch that I had.
The major issue, which was the monster not being able to go upstairs, despite the MaxStepHeight property value being big, and downstairs has been solved by the use of the MaxDropOffHeight property
(which still doesn't really makes too much sense for the step up to me, I clearly understand the step down, of course, but to go up I should only need the Step height, or am I missing something?)

For the rest being a bit buggy, now I made some tweaks here and there and the function works just wonderfully fine:
Spoiler:
I'll leave it here for anyone that wants to use it
Post Reply

Return to “Scripting”