Skip to content

Misc and utility functions

inkoalawetrust edited this page Sep 14, 2024 · 9 revisions

This page covers all the miscellaneous and utility functions included in the base NPC class. Functions that don't really fall under the other categories.

VelInterceptEx()

Parameters:

  • Projectile: The actor whose trajectory must be adjusted.
  • SpreadHorz: A vector2 whose X and Y components are how much random spread the Projectile incurs. Similar to A_CustomBulletAttack()'s horz_spread parameter.
  • SpreadVert: Same as SpreadHorz but for vertical spread.
  • MinDist: If the callers' target is closer than this specified distance, then the projectile will be fired straight ahead.
  • FOV: The field of view the target must be in for the intercept to occur, useful for preventing NPCs from doing things like firing from their backs.

Return type(s):

  • None

Function:

This function is one of the ways target prediction is performed, it's a more advanced version of VelIntercept() that can also account for normal monster movement. It also includes an FOV check to prevent the shot from being led if it would stray too far off the shooters' current angle.

GetTeleportMoveDivision()

Return type(s):

Function:

Used by the Target prediction code to estimate the trajectory of normal monsters that call TryMove() to move.

AimAtTarget()

Parameters:

  • Max_Turn, Max_Pitch, Ang_Offset, Pitch_Offset, Flags, Z_Ofs: These arguments are passed to A_Face() to control how fast the turret turns.
  • Tics: A multiplier that extrapolates the trajectory by Tics amount into the future. Default is -1, meaning that this value is automatically determined using Distance3D(Target)/AttackSpeed. Can be used to add custom rangefinding code to actors (e.g to add very small inaccuracies to the rangefinding).
  • AttackSpeed, FastAttackSpeed: Passes how fast the projectile attack moves at, to allow the function to make the actor face towards the correct direction. FastAttackSpeed is what the projectiles' speed is with -fastmonsters enabled.

Return type(s):

  • None

Function:

This is the function that handles the advanced target prediction of actors, based on how fast the target is going, and the travel time of the actors' attack. Allowing the NPC to actually move ahead in anticipation of the future trajectory of the target, similar to IRL anti-aircraft guns and CIWS systems.

AimingAheadOfTarget()

Return type(s):

  • Returns true if the actor is ahead of its' target while leading its' shots. Also always returns true if target prediction is off.

Function:

Checks if the direction the actor is moving at matches with the direction the target is at, relative to the turrets' angle. If it's not, then the turret is ahead of its' actor while leading its' attack direction.

AddProjectileSpread()

Parameters:

  • Projectile: The actor whose trajectory must be adjusted.
  • SpreadHorz: A vector2 whose X and Y components are how much random spread the Projectile incurs. Similar to A_CustomBulletAttack()'s horz_spread parameter.
  • SpreadVert: Same as SpreadHorz but for vertical spread.

Return type(s):

  • None

Function:

Adds randomized projectile spread to the actor, which should normally be a projectile, then adjusts their velocity accordingly. Used to add spread to projectile attacks in a manner similar to A_CustomBulletAttack().

ValidPositionAt()

Parameters:

  • Offset: The Vector2 offsets relative to the calling actor from which the check should run.
  • TempRadius: The hitbox radius that should be checked at the specified position. Default is 32
  • TempHeight: The hitbox height that should be checked at the specified position. Default is 56.

Return type(s):

  • Returns true if the position at the specified offset is valid, false otherwise.

Function:

Checks if the specified position relative to the caller is valid (Does not intersect with any level geometry or solid actors.). A radius and height are also specified, so that you can check if an actor with the specified dimensions would fit in that position.

Example:

This bit of code from the MVPs' Army Truck uses ValidPositionAt() to check if the truck can deploy marines behind itself, to avoid spawning them inside walls and obstacles.

		//The trucks' marine deploying loop.
		DeployMarines:
			TNT1 A 0 A_JumpIf (User_MarineAmount <= 0,"See"); //No marines to drop.
			TNT1 A 0
			{
				If (Recursions >= MARINE_RECURSIONS)
				{
					DeployTimer = 35;
					Return ResolveState ("See");
				}
				
				If (!ValidPositionAt( (-128,0) ) ) //If they would be stuck by spawning right behind the truck, then cancel the drop.
				{
					Recursions++;
					Return ResolveState ("See");
				}
				
				Return State (Null);
			}

GetNearestCardinalAngle()

Parameters:

  • SixteenAngles: If the returned angle should snap in 16 increments, for 16-angled sprites.

Return type(s):

  • A normalized angle that matches the nearest sprite angle.

Function:

This function gets the callers' angle, and returns the sprite angle that the callers' angle is the closest to. For example, if the callers' angle is currently 78 degrees, the function will return a 90 degree angle, since that's the closest sprite angle.

GetAngleTo()

Parameters:

  • Other: The actor to get the angle too.
  • AngleLimit: The maximum angle that can be returned, if set to 0, it just returns the value of AngleTo().

Return type(s):

  • The angle to the Other actor.

Function:

This function behaves like AngleTo(), with the exception of AngleLimit which if non-zero, will limit the max angle returned, if the angle to the other actor exceeds the limit. Which makes the function act similar to A_Face() when it has a max_turn property passed. The library only uses this function to limit the turn radius of KAI_MoveTowards().

GetAngleToPos()

Parameters:

  • Position: The Vector3 coordinates to get the angle too.
  • AngleLimit: The maximum angle that can be returned, if set to 0, it just returns the X member of the Vector3 SphericalCoords() outputs.

Return type(s):

  • The angle to the target position.

Function:

This function behaves exactly like GetAngleTo(), except that instead of needing the position of a specific class, it allows for passing exact Vector3 coordinates.

GetSpriteAngleFromPos()

Parameters:

  • CamPos: The position to look at the callers' sprite from. Usually this is the playerinfo camera of a player.
  • SixteenAngles: Do the callers' sprite have 16 angles instead of 8 ?

Return type(s):

  • The sprite angle from the CamPos to the caller. This is the actual value the renderer returns to know what sprite angle to render on the actor. So it's a value between 0 and 8 or 16, if SixteenAngles is on.
  • An actual sprite angle in 360 degrees. Such as 22.5 or 315. This value should be more useful than the direct render value.

Function:

Returns the sprite angle that is visible from CamPos. The code of this function is a direct rip of the code that GZDooms' renderer uses to determine what angle sprite to show from the perspective of the players' viewport.

GetClosestActor()

Parameters:

  • ActorList: The array of actor pointers to process. Spitting out a pointer to the actor closest to the specified position.
  • Pos: The position to use as the frame of reference.
  • MaxRange: If not 0, any actors that are further than this distance from Pos are automatically excluded.

Return type(s):

  • A pointer to the actor that is closest to the Pos.

Function:

This function finds the actor that is the closest to Pos from an array of actor pointers.

Example:

This is how FindNearestEnemy() gets the enemy closest to the calling NPC:

	Actor FindNearestEnemy (Double Range = 256)
	{
		If (Range <= 0) Return Null;
		
		Array <Actor> NearbyEnemies;
		BlockThingsIterator FindEnemies = BlockThingsIterator.Create (Self,Range);
		
		//Find all living, targetable, and visible enemies in range. And put them in the NearbyEnemies array.
		While (FindEnemies.Next())
		{
			Actor Mobj = FindEnemies.Thing;
			
			//Skip this actor if it's dead, cannot be targeted, is not a monster nor a player, or is not even an enemy.
			If (!Mobj || IsDead (Mobj) || IsInanimateObject(Mobj) || !CanBeTargeted (Self, Mobj) || !IsActorHostile (Mobj)) Continue;
			
			If (!CheckSight (Mobj)) Continue; //And at last, check if the enemy is even visible.
			
			NearbyEnemies.Push(Mobj); //Add it to the list.
		}
		Return GetClosestActor (NearbyEnemies,Self.Pos,Range); //Now that the blockmap search is done, pass the array to GetClosestActor to find the closest enemy that is within the specified range, relative to the callers' position.
	}

GetFurthestActor()

Parameters:

  • ActorList: The array of actor pointers to process. Spitting out a pointer to the actor furthest from the specified position.
  • Pos: The position to use as the frame of reference.
  • MinDist: If not 0, any actors that are closer than this distance from Pos are automatically excluded.

Return type(s):

  • A pointer to the actor that is furthest from Pos.

Function:

Behaves exactly the same as GetClosestActor(), except that it returns the furthest actor from an array of actors, instead of the closest.

Clone this wiki locally