Why your unit does not aim & fire...

Why your unit does not aim & fire...

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Why your unit does not aim & fire...

Post by PicassoCT »

So you have a unit - and it does not fire

You have a weapon configured, you copy pasted it from some - known to work example, from some other game. Keep it simple, keep it clean, modify away from working software in baby-steps.

For this Torturial, i will use a turret weapon with the following unitdef:

Code: Select all

local groundturretmg =  Turret:New{
	name = "Stationary Machinegun",
	Description = "Pillbox Emplacement ",
	
	objectName = "ground_turret_mg.dae",
		customParams        = {
		normaltex = "unittextures/component_atlas_normal.dds",
	},
	
	script = "ground_turretscript.lua",
	buildPic = "ground_turret_mg.png",
	iconType = "ground_turret_mg",
	--floater = true,
	--cost
	buildCostEnergy  = 500,
	buildCostMetal= 500,
	buildTime = 35,
	--Health
	maxDamage = 500,
	idleAutoHeal = 0,
	--Movement
	
	fireState=1,
	
	FootprintX = 1,
	FootprintZ = 1,
	maxSlope = 50,
	
	MaxWaterDepth = 0,
	MovementClass = "Default2x2",

	nanocolor=[[0.20 0.411 0.611]],
	sightDistance = 300,
	activateWhenBuilt   	= true,
	cantBeTransported = false,
	usepiececollisionvolumes = true,

	commandFire = true,
	canAttackGround = true,
	CanAttack = true,
	CanGuard = true,
	CanMove = true,
	CanPatrol = true,
	Canstop  = true,
	onOffable = false,
	LeaveTracks = false, 
	canCloak =false,	
	canManualFire = true, 
	
	Category = [[GROUND]],

	  customParams = {
	 	 baseclass = "turret"
	  },

	 sfxtypes = {
		explosiongenerators = {
								"custom:bigbulletimpact"
							  },
				},
				
		weapons = {
		[1]={name  = "machinegun",
			onlyTargetCategory = [[GROUND BUILDING]],
			turret = true
			},	
		[2]={name  = "aamachinegun",
			onlyTargetCategory = [[AIR]],
			turret = true
			},					
		},	
}
And the following weaponDef:

Code: Select all

local weaponName = "machinegun" 
local weaponDef = {
    name = "M27-64",
    weaponType = [[Cannon]],
    --damage
    damage = {
        default = 4,
        HeavyArmor = 1,
    },
    areaOfEffect = 8,
    explosionGenerator = "custom:gunimpact",
    cegTag = "gunprojectile",
    texture1 = "gunshot",

    --physics
    weaponVelocity = 850,
    reloadtime = 5,
    range = 350,
    sprayAngle = 300,
    tolerance = 8000,
    lineOfSight = true,
    turret = true,
   
	
    craterMult = 0,
    burst = 15,
    burstrate = 0.2,
    soundStart = "weapons/machinegun/salvo2.ogg",
    soundtrigger = 1,
    SweepFire = 1,
    interceptor=1,
    --apperance
    rgbColor = [[0.95 0.5  0.2]],
    size = 1.2,
    stages = 20,
    separation = 0.2,
    collideFirebase = false,
    avoidFriendly= false,

}

return lowerkeys({ [weaponName] = weaponDef })
So how do you find out why it does not fire? Lets get this done.

Step1: Check if the Lua - Aiming Calls.

Code: Select all

function script.AimWeapon1(Heading, pitch)
	Spring.Echo("Aiming weapon 1")
	return true
end
If that one does produced results, your lua code is not returning true at the end. Congratulations you are almost done.


Step 2: Looking for clue through the API aka MorePrintf Debuggery

Print the weaponstable.

Code: Select all

function printOutWeapon(weaponName)
    for name,param in weaponDef:pairs() do
    	if weaponName == name then
      		echo(name,param)
   		end
    end
end
One typo might be silently swallowed and your weapon turned into something completely different.
Spring will best effort brows your weapons def, adhering to the Linux Mantra "Fail early, fail loud" only if there are syntactic errors.

Code: Select all

function debugAimLoop(weaponID, sleepMS)
	restTime = sleepMS or 1
	while true do
		angleGood,  loaded,  reloadFrame,  salvoLeft,  numStockpiled =Spring.GetUnitWeaponState(unitID,weaponID)
		if angleGood then
			echo("Weapon: Anglegood->"..toString(angleGood).." Loaded->"..toString(loaded).." reloadFrame->"..toString(reloadFrame))
		end

		px,py,pz, dx,dy,dz =Spring.GetUnitWeaponVectors(unitID,weaponID)
		if px then
			echo("Weapon: Vector ->", {px,py,pz, dx,dy,dz})
		end
		commands = Spring.GetUnitCommands(unitID, weaponID)

		if commands and type(commands) =="table" and commands[1] and commands[1].id  and commands[1].id == CMD.ATTACK then
			attackedID = commands[1].params[1]
			
			boolWeaponCanFire = Spring.GetUnitWeaponCanFire(unitID, weaponID)
			echo("Units Weapon can fire: "..toString(boolWeaponCanFire))

			resultType, tID = Spring.GetUnitWeaponTarget(unitID, weaponID)
			if resultType == 1 and uID then
				echo("Target is Unit ->".. tID)
			end

			if attackedID then
				bSucces = Spring.GetUnitWeaponHaveFreeLineOfFire(unitID, weaponID)
				if bSucces then
					echo("Raytrace reaches Goal:"..toString(bSucces))
				end

				boolTargetInRange = Spring.GetUnitWeaponTestRange(unitID, weaponID, attackedID)
				if boolTargetInRange then
					echo("Target is in Range: "..toString(boolTargetInRange))
				end
			end
		end
		Sleep(restTime)
	end
end
Also check on the collissionmodel - for self intersections.
Yellow Dots are Points AimeFrom. Red/Orange Dots are FireEmitors,

Image

Right model is working, left is not working.

Image

Model is still not working

Image

To be continued
Attachments
screen00044.jpg
(275.65 KiB) Not downloaded yet
screen00042.jpg
(308.2 KiB) Not downloaded yet
screen00041.jpg
(430.51 KiB) Not downloaded yet
Post Reply

Return to “Lua Scripts”