Skip to content

KAI_BaseVehicle

inkoalawetrust edited this page Jul 4, 2024 · 8 revisions

KAI_BaseVehicle

Parent class: KAI_Actor

Child classes:

All the vehicles currently in my Military Vehicles Pack as of version 0.4.0.

Description

This is the base NPC class for all vehicle NPCs. This class covers any vehicles you may want to add to your mod, and has vehicle-specific behavior and code to make vehicles behave differently from normal monsters.

As of 5/5/2023, it is the most fully fledged NPC base, since the KAI library was made out of the base vehicle code of my MVP mod.

Features

Turrets

Vehicle.turret.mp4

NPC vehicles have the ability to have turrets attached to them, that move and act independent of the vehicle. The behavior of the turrets, how they are spawned and attached, etc, are completely customizable. Vehicle turrets can also be made to visually change their elevation based on their pitch, if you have the sprites for it. And it's not a visual effect either, turrets can have actually elevation limits to how far up or down they can look, so a turret could be made unable to just shoot straight up or down. And much more !

For more information on turrets, click here.

Headlights

Headlights.mp4

NPC vehicles have a configurable system that allows you to give the vehicle headlights, which it will turn on and off when it is inside dark sectors. The size, color, cone etc of the headlight can be customized as well, along with how dark a sector needs to be for the vehicle to turn its' headlights on.

Corpse crushing

Corpse crushing NPC vehicles have the ability to crush corpses they touch, with the maximum dimensions of a corpse they can crush being configurable through properties. Keep in mind that crushing corpses has to manually be handled with a function call however.

Pushing and crushing

In addition to being able to crush corpses, KAI vehicles also have the ability to push certain obstacles out of the way, as long as they are thrustable, and can also apply crushing damage to actors below a certain size. This behavior can be modified with several properties. Unlike corpse crushing, this mechanic is automatically handled for vehicles in KAI_MoveTowards(). It is handled using the

More features

  • Stay away from enemies by default instead of charging headfirst like Doom monsters normally do. And can retreat from targets above a certain threshold.
  • Friendly vehicles keep some amount of distance between themselves and the player when following them. So players aren't dogpiled by multiple vehicles.
  • Can stay in place to let their turrets fire.
  • Can stop chasing their target after not seeing it for a certain amount of time, in case they get stuck trying to head to an unreachable enemy.

Variables

These are the variables shared by all vehicles and turrets.

Constants

  • DEFAULT_HEARDISTANCE = 4096: The default hear distance the vehicles' and turrets' KAI_Look() calls use.
  • DEFAULT_SEEDISTANCE = 8192: The default sight distance the vehicles' and turrets' KAI_Look() calls use.

NearestEnemy

Type: Actor pointer

A pointer to the enemy that is closest to the vehicle, if any. Used in KAI_LandVehicleChase() to make vehicles keep a set distance from all enemies.

ChaseTimer

Type: Integer

How long the vehicle has been chasing its' target for since it last saw it. Used in conjunction with the SearchTime property.

StayStillDelay

Type: Integer

Used by the StayStillAndShoot virtual function to put a cap on how long until the vehicle can decided to stand around and shoot again.

RetreatState

Type: State

The vehicles' Retreat state label, used by KAI_LandVehicleChase(). This works like the SeeState of actors, so simply give your vehicle a state called called Retreat.

Headlights

Type: Dynamic array

A dynamic array that stores pointers to each of the vehicle's headlights. Only accepts pointers to KAI_Headlight actors. Used to keep track of the headlights to turn them on or off or remove them when needed.

HeadlightsOn

Type: Boolean

Returns if the headlights should currently be on, this updates every tick based on if the current sectors' light level is below the HeadlightLevel, so it should not be touched, only read.

Turret

Type: KAI_BaseTurret pointer

A pointer to the vehicles' turret, if any.

TurretOffsets

Type: Vector3

The relative offsets to pass on top of the turrets' own attachment offsets. Turrets stick to the top of the vehicle's hitbox, so this is a useful property to perform micro-adjustments to the attachment height to fix small sprite clipping issues. Should be set before SpawnVehicleTurret() is called. This will be made into a property once GZDoom 4.11 releases.

LightsOnAlready

Type: Boolean

Used in the headlight control functions to prevent the headlights from being turned on and off every tick. Like HeadlightsOn this should not be modified, only read.

Flags

These are the flags shared by all vehicles and turrets.

Transport

Does nothing by default. Just marks the vehicle as being able to transport/spawn NPCs.

NoHeadlights

Disables the headlight system for the vehicle entirely.

PassThreatLevelToTurrets

If the vehicle has a threat level, and its' turret doesn't. Then the vehicles' threat level will be passed to the turret.

NoCrushing

The vehicle will never crush enemies or corpses.

Properties

These are properties shared by all vehicles and turrets.

VehicleTurret

The class name of the vehicle turret to attach to the vehicle. This can only be an actor based on KAI_BaseTurret.

HeadlightLevel

If the light level of the sector the vehicle is currently in is below this threshold, the vehicle will turn on its' headlights. Default is 104.

RetreatRange

If an enemy is within this range of the vehicle, KAI_LandVehicleChase() should make it drive away from them. This range is 4 times longer for targets over the vehicle's ThreatLevelThreshold. If this is 0 or less, the vehicle will not drive away from enemies at all. Default is 768.

SearchTime

How long does the vehicle keep chasing its' target after losing sight of it, before giving up ? If this is 0 or less, vehicles never stop chasing their target after it goes out of sight, like normal Doom monsters. Keep in mind that this timer updates every time KAI_LandVehicleChase() is called, not every tick !

MaxCorpseRadius, MaxCorpseHeight

The maximum radius and height, respectively, of a corpse that the vehicle will be able to crush, anything over this will be uncrushable. Remember that corpse crushing has to be handled manually with the KAI_CrushCorpses() function.

MaxPushMass

The maximum mass an obstacle in the vehicles' way can have before it can no longer push it. The default is 0, which means the vehicle can't push anything when calling KAI_MoveTowards()

MaxPushRadius, MaxPushHeight

The maximum radius and height, respectively, that an obstacle can have before the vehicle can no longer push it away. If one if these values is 0, it will be ignored, allowing for example to only a define a max radius, but not height.

MaxCrushRadius, MaxCrushHeight

Same as the MaxPushRadius/Height, but for how large the actors the vehicle can do crush damage to can be. Used by KAI_VehicleCrushObstacles()

CrushDamage

How much Crush type damage the vehicle does to the actors it pushes away with each of its' steps.

Virtual functions

These are the virtual functions that can be accessed by KAI_BaseVehicle and any of its' descendants.

SpawnVehicleTurret()

Return type(s):

  • None

Function:

Handles spawning a turret for the vehicle based on the value of VehicleTurret. And stores it in the vehicles' turret pointer.

SpawnHeadlights()

Return type(s):

  • None

Function:

Override this to spawn headlights for your vehicle with SpawnVehicleHeadlight().

StayStillAndShoot()

Parameters:

  • CheckSightFrom: The actor to check their sight to their target.

Return type(s):

  • Returns true if the vehicle should stay still. False otherwise.

Function:

Checks if the vehicle should stay still to let its' turret shoot. By default this just checks if the vehicle has a turret that can shoot, and then checks if the vehicle is further from the enemy closest to it than its' RetreatRange. If these conditions are met, it returns true.

If you plan to make a vehicle that actually stays still to shoot, it is suggested that you use the base version of this virtual to just run the basic sight and distance checks, and write your own custom checks around it. The actual status changes, like putting the vehicle in its' StickAround state, should also be handled inside the virtual, similar to ShouldAttack()

Example

Here is an abridged example of a StayStillAndShoot() override from one of my (inkoalawetrust) mods. Showing how it's meant to be used by also applying status changes:

	Override Bool StayStillAndShoot (Actor CheckSightFrom)
	{
		//Other custom code
		
		//And at last, run the standard KAI vehicle checks.
		If (KAI_BaseVehicle.StayStillAndShoot(CheckSightFrom))
		{
			A_StopSound (CHAN_MOVING);
			A_StartSound("Vehicle/ArmyCar/Idle",CHAN_IDLING,CHANF_LOOPING,attenuation:0.8);
			//For the example, these are the important bits.
			//ChaseTimer is how long the vehicle will stay in StickAround.
			//And StayStillDelay is how much time the vehicle much wait before deciding to stop again.
			ChaseTimer = Random (16,32);
			StayStillDelay = ChaseTimer+(Random (2,4)*GameTicRate);
			SetStateLabel ("StickAround");
			Return True;
		}
		
		Return False;
	}

OnTurretDeath()

Parameters:

  • DeadTurret: The turret that died, normally this should just be the vehicles' turret.
  • Removed: Was the turret killed by being removed entirely ? Default is false.

Return type(s):

  • None

Function:

Called when the vehicles' turret dies, useful for executing actions when that happens.

States

StickAround

This is the stock state vehicles go to stay and shoot, it makes the vehicle face sideways away from its' target, and keeps it there until the ChaseTimer runs out, or until an enemy gets too close to the vehicle. It uses #### # sprites to keep the sprite from before the vehicle stopped, making it work with any vehicle actor.

Functions

See here.

See also

Clone this wiki locally