ShooterBase python

From WiCWiki

Revision as of 14:17, 27 November 2007 by theMaatin (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

Contents

ShooterBase

This is the base of a shooter. New shooters should be derived from this class.


Members

BarrelOffset

Type
Vector3
Description
The offset from the Unit position where projectiles are spawned.


AimingForHeading

Type
float
Description
The heading the ShooterBase want to aim in.


AimingForPitch

Type
float
Description
The pitch the ShooterBase tries aim in.


CurrentAimingHeading

Type
float
Description
The ShooterBases current heading.


CurrentAimingPitch

Type
float
Description
The ShooterBases current aiming pitch.


AimHeadingSpeed

Type
float
Description
How fast the ShooterBase is aiming.


Host

Type
Unit
Description
The unit the ShooterBase belong to.


AmmoTypeIndex

Type
int
Description
The index of the ShooterBases ammunition type.


NumAmmoTypes

Type
int
Description
The number of ammunition types the ShooterBase have.



Methods

GetShooterOffset

Syntax
import wic
wic.game.ShooterBase.GetShooterOffset()
Description
-
Exceptions
-
See also
-




GetTargetPosition

Syntax
import wic
wic.game.ShooterBase.GetTargetPosition()
Description
-
Exceptions
-
See also
-



GetPredictedTargetPosition

Syntax
import wic
wic.game.ShooterBase.GetPredictedTargetPosition()
Description
-
Exceptions
-
See also
-



GetHost

Syntax
import wic
wic.game.ShooterBase.GetHost()
Description
-
Exceptions
-
See also
-



GetFiringPosition

Syntax
import wic
wic.game.ShooterBase.GetFiringPosition()
Description
-
Exceptions
-
See also
-



GetProjectileSpeed

Syntax
import wic
wic.game.ShooterBase.GetProjectileSpeed()
Description
-
Exceptions
-
See also
-




IsAgentMoving

Syntax
import wic
wic.game.ShooterBase.IsAgentMoving()
Description
-
Exceptions
-
See also
-



GetAgentAccuracyPenalty

Syntax
import wic
wic.game.ShooterBase.GetAgentAccuracyPenalty( aMovementSpeedFactor )
Description
-
Exceptions
-
See also
-



ToExperiencedMovementPenalty

Syntax
import wic
wic.game.ShooterBase.ToExperiencedMovementPenalty( anOriginalMovementPenalty )
Description
-
Exceptions
-
See also
-



GetAgentSlopeBonus

Syntax
import wic
wic.game.ShooterBase.GetAgentSlopeBonus( aTargetPosition, anAgentPosition )
Description
-
Exceptions
-
See also
-



ToExperiencedDeviance

Syntax
import wic
wic.game.ShooterBase.ToExperiencedDeviance( anOriginalDeviance )
Description
-
Exceptions
-
See also
-



GetRawAccuracy

Syntax
import wic
wic.game.ShooterBase.GetRawAccuracy()
Description
-
Exceptions
-
See also
-



CreateStraightProjectile

Syntax
import wic
wic.game.ShooterBase.CreateStraightProjectile( aStartPosition, aFiringVector )
Description
-
Exceptions
-
See also
-



CreateBallisticProjectile

Syntax
import wic
wic.game.ShooterBase.CreateBallisticProjectile( aStartPosition, aFiringVector )
Description
-
Exceptions
-
See also
-



CreateHomingProjectile

Syntax
import wic
wic.game.ShooterBase.CreateHomingProjectile( aStartPosition, aFiringVector )
Description
-
Exceptions
-
See also
-



Reload

Syntax
import wic
wic.game.ShooterBase.Reload( aShooterIce )
Description
-
Exceptions
-
See also
-



HaveTarget

Syntax
import wic
wic.game.ShooterBase.HaveTarget()
Description
-
Exceptions
-
See also
-



ChangeAmmoType

Syntax
import wic
wic.game.ShooterBase.ChangeAmmoType( anAmmoType )
Description
-
Exceptions
-
See also
-



Examples

To show how a shooter is created in python, lets mod the Humvee again. First we have to change the PrimaryShooter of the Humvee to a PythonShooter. In /units/unittypes_wic.juice right click /UnitTypes/US_HUMWEE/myParasites/MultipleShooter(0)/myPrimaryShooter and add a PythonShooter. Copy all the values from the current primary shooter, 50Cal. Two extra keys are located at the end of the PythonShooter. myPythonModule, and myPythonClass. Set those to straightshooter and StraightShooter. Make sure to mind the capitalization, windows don't mind but Python does. I try to keep python filenames in lower-case and use camel notation for class names. Now it's time for the python file needed. Create a file called straightshooter.py in the /python folder. Insert this code into it:

import wic
import wic.common.math as math


class StraightShooter( wic.game.ShooterBase ):
	""" This is a functional clone of the straight shooter. It
	fires rounds in a straight line from the mussel of the
	shooter to the center of the given target."""
	
	def Aim( self ):
		""" This function is mandatory. It is called when a new target is aquired.
		"""
		# Get the vector between the shooter and the target
		toTarget = math.Vector3( self.GetTargetPosition() - self.Host.Position )
		
		# Remove the shooter offset...
		toTarget -= self.GetShooterOffset() * math.GetRotationMatrix( math.Y_AXIS, self.Host.BodyHeading );
		
		# ...and the barrel offset from the vector
		toTarget -= self.BarrelOffset * math.GetRotationMatrix( math.Y_AXIS, self.Host.BodyHeading )
		
		# Calculate the heading...
		self.AimingForHeading = math.atan2( toTarget.X, toTarget.Z ) - self.Host.BodyHeading
		
		# ...and the pitch towards the target
		self.AimingForPitch = -math.asin( max( -1.0, min( 1.0, toTarget.Y / toTarget.Length() ) ) )
		
		# Crop the aim heading
		self.AimingForHeading = math.CropAngle( self.AimingForHeading )
		
		# And if the aim heading and the aim pitch is close enough to the current aim heading and pitch, just snap to the new aim heading and pitch
		if math.AngleDiff( self.AimingForHeading, self.CurrentAimingHeading ) <= self.AimHeadingSpeed * wic.common.GetElapsedTime() and math.AngleDiff( self.AimingForPitch, self.CurrentAimingPitch ) <= self.AIM_PITCH_SPEED * wic.common.GetElapsedTime():
			self.CurrentAimingHeading = self.AimingForHeading
			self.CurrentAimingPitch = self.AimingForPitch
			
			return True
		else:
			return False
	

	def FireProjectile( self ):
		""" This method is mandatory. It's called when the shooter is
		aimed at it's target.
		"""
		# Get the target position
		targetPosition = self.GetTargetPosition()
		
		# Compensate target position for target movement
		targetPosition = self.GetPredictedTargetPosition( ( self.GetFiringPosition() - targetPosition ).Length() / self.GetProjectileSpeed() )
		
		# Calculate the direction to the target position
		targetDirection = targetPosition - self.GetFiringPosition()
		targetDirection.NormalizeSafe()
		
		# Initialize accuracy penalty
		accuracyPenalty = 1.0
		
		# If the shooters host is moving, modify the penalty based on the speed of the host
		if self.Host.IsMoving():
			hostSpeed = self.Host.MaxSpeed
			
			# Speed under 0.1 doesn't affect the penalty
			if hostSpeed < 0.1:
				accuracyPenalty = self.GetAgentAccuracyPenalty( 0 )
			
			else:
				accuracyPenalty = self.GetAgentAccuracyPenalty( self.Host.GetCurrentSpeed() / hostSpeed )
			
			accuracyPenalty = self.ToExperiencedMovementPenalty( accPenalty )
		
		# Apply eventual slope bonus to the accuracy penalty
		accuracyPenalty *= self.GetAgentSlopeBonus( targetPosition, self.GetFiringPosition() )
		
		# Convert the target direction to polar angles
		angles = math.PolarAnglesFromVector( targetDirection )
		
		# Apply experience bonuses to the angles
		angles.X += self.ToExperiencedDeviance( wic.common.Random() * self.GetRawAccuracy() * accuracyPenalty * 2 - self.GetRawAccuracy() * accuracyPenalty )
		angles.Y += self.ToExperiencedDeviance( wic.common.Random() * self.GetRawAccuracy() * accuracyPenalty * 2 - self.GetRawAccuracy() * accuracyPenalty )
		
		# Convert the angles back to a vector
		targetDirection = math.VectorFromPolarAngles( angles )
		
		# Apply the speed
		targetDirection *= self.GetProjectileSpeed()
		
		# Now we return the result from creating the projectile
		return self.CreateStraightProjectile( self.GetFiringPosition(), targetDirection )
Personal tools
User Created Content