Skip to content

Keep NPC move speed and stop range on SetPos/SetRot - #1300

Open
Vito-URP wants to merge 1 commit into
openmultiplayer:masterfrom
Vito-URP:fix/npc-keep-move-speed
Open

Vito-URP wants to merge 1 commit into
openmultiplayer:masterfrom
Vito-URP:fix/npc-keep-move-speed

Conversation

@Vito-URP

Copy link
Copy Markdown

Calling NPC_SetPos, NPC_SetRot, NPC_SetVehiclePos or NPC_SetVehicleRot on a moving NPC resets the speed and stop range it was given in NPC_Move.

The setters re-run move(targetPosition_, moveType_) to aim the NPC at its target again, but they don't pass the speed or the stop range, so the defaults are used. move() also wrote the speed into a local float moveSpeed_ that shadowed the member, so the member never had the speed to pass anyway.

In game:

  • an NPC sent with NPC_MOVE_TYPE_AUTO, NPC_MOVE_SPEED_WALK starts jogging after the first NPC_SetRot
  • a driver sent with NPC_MOVE_TYPE_DRIVE, 0.3 speeds up to the default 1.0 after NPC_SetVehicleRot

This stores the speed in the member and passes moveSpeed_ and stopRange_ from the four setters.

Test

A and B get the same NPC_Move. Every 2 s, B is given the rotation it already has. Distance covered:

t=4s t=8s t=12s
on foot, master A 6.1 m, B 14.0 m A 12.3 m, B 36.2 m A 18.5 m, B 39.7 m (arrived)
on foot, this PR A 6.1 m, B 6.0 m A 12.3 m, B 12.1 m A 18.5 m, B 18.2 m
driving, master A 11.8 m, B 25.3 m A 23.8 m, B 64.6 m A 35.8 m, B 103.7 m
driving, this PR A 11.8 m, B 11.7 m A 23.7 m, B 23.5 m A 35.7 m, B 35.2 m

The on-foot case in game:

master vs this PR

repro.pwn
// A moving NPC loses its NPC_Move speed after NPC_SetRot / NPC_SetVehicleRot.
// A and B get the same NPC_Move. Every 2 s B is given the rotation it already
// has, so A and B should stay level.
#include <open.mp>

new walkerA, walkerB, driverA, driverB;
new carA, carB;
new tick;

main() {}

public OnGameModeInit()
{
	carA = CreateVehicle(560, 1500.0, -2485.0, 14.35, 270.0, 1, 1, -1);
	carB = CreateVehicle(560, 1500.0, -2497.0, 14.35, 270.0, 1, 1, -1);

	walkerA = NPC_Create("WalkerA");
	walkerB = NPC_Create("WalkerB");
	driverA = NPC_Create("DriverA");
	driverB = NPC_Create("DriverB");
	NPC_Spawn(walkerA);
	NPC_Spawn(walkerB);
	NPC_Spawn(driverA);
	NPC_Spawn(driverB);

	SetTimer("Start", 2000, false);
	return 1;
}

public OnNPCSpawn(npcid)
{
	if (npcid == walkerA) NPC_SetPos(npcid, 1608.0, -2489.0, 13.5469);
	else if (npcid == walkerB) NPC_SetPos(npcid, 1608.0, -2493.0, 13.5469);
	else if (npcid == driverA) NPC_PutInVehicle(npcid, carA, 0);
	else if (npcid == driverB) NPC_PutInVehicle(npcid, carB, 0);
	return 1;
}

forward Start();
public Start()
{
	NPC_Move(walkerA, 1648.0, -2489.0, 13.5469, NPC_MOVE_TYPE_AUTO, NPC_MOVE_SPEED_WALK);
	NPC_Move(walkerB, 1648.0, -2493.0, 13.5469, NPC_MOVE_TYPE_AUTO, NPC_MOVE_SPEED_WALK);
	NPC_Move(driverA, 1900.0, -2485.0, 13.5469, NPC_MOVE_TYPE_DRIVE, 0.3);
	NPC_Move(driverB, 1900.0, -2497.0, 13.5469, NPC_MOVE_TYPE_DRIVE, 0.3);
	SetTimer("Tick", 500, true);
}

Float:TravelledX(npcid, Float:startX)
{
	new Float:x, Float:y, Float:z;
	NPC_GetPos(npcid, x, y, z);
	return x - startX;
}

forward Tick();
public Tick()
{
	tick++;
	if (tick % 4 == 0)
	{
		new Float:rx, Float:ry, Float:rz;
		NPC_GetRot(walkerB, rx, ry, rz);
		NPC_SetRot(walkerB, rx, ry, rz);
		NPC_GetVehicleRot(driverB, rx, ry, rz);
		NPC_SetVehicleRot(driverB, rx, ry, rz);
	}
	if (tick % 8 == 0 && tick <= 24)
	{
		printf("t=%ds  on foot: A %.1f m, B %.1f m  |  driving: A %.1f m, B %.1f m", tick / 2,
			TravelledX(walkerA, 1608.0), TravelledX(walkerB, 1608.0),
			TravelledX(driverA, 1500.0), TravelledX(driverB, 1500.0));
	}
}

Tested on a 1.6.7 server with the NPCs component built from master and from this branch.

Setting the position or rotation of a moving NPC re-runs move() to aim it
at its target again, but it passed only the target and move type, so the
speed and stop range fell back to the defaults. An NPC told to walk at
walking speed (auto type) started jogging after the first NPC_SetRot, and
a driver moving at a custom speed jumped to speed 1.0.

move() also wrote the speed into a local that shadowed the moveSpeed_
member, so the member was never set. Store it in the member and pass it,
with stopRange_, from setPosition, setVehiclePosition, setRotation and
setVehicleRotation.
@GuiBoneVowt

Copy link
Copy Markdown

Tested on top of master (72bf89c), NPCs only, going for the setters that aren't in your table. Same idea, A as the reference and B getting the setter every second, distance after 6.5 s:

                                      master           this PR
walk speed + NPC_SetPos               A 9.9   B 30.9   A 9.8   B 9.4
custom 0.05 + NPC_SetFacingAngle      A 3.1   B 8.6    A 3.1   B 3.1
jog, stop range 8, NPC_SetRot once    A 12.2  B 19.7   A 12.2  B 12.2
drive 0.3 + NPC_SetVehiclePos         A 19.2  B 55.0   A 19.0  B 18.3

So the stop range is kept too, on master B went 19.7 of the 20 m instead of stopping 8 m before the target.

The small gap that's left (B a bit behind A, it's in your numbers too) comes from move() setting lastMove_ to now, so every setter call drops the time since the last update. Not a big deal, just saying where it comes from. LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants