Spring.SetUnitArmoured

Spring.SetUnitArmoured

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

Moderator: Moderators

Post Reply
User avatar
Zealot
Posts: 94
Joined: 03 Dec 2012, 13:53

Spring.SetUnitArmoured

Post by Zealot »

Hey I finished writing my gadget to alter speed and steath states.

I was wondering if some one could give me some advice on how to also use this to alter the armor state of the units.

Does anyone have an example of how to use "Spring.SetUnitArmored"?



My code currently toggles between 3 states.

Slow and small decloak distance
medium and medium decloak distance
fast and large decloak distance

I want to to also affect the armor of the units. So that:

Slow and small decloak distance, damage x 0.33
medium and medium decloak distance, normal damage
fast and large decloak distance, damage x 1.33


I was planning on just setting up 3 armor defs, and then using Spring.SetUnitArmoured to change the units armor def.

Would this be the best way to go about it?

Are there any examples of Spring.SetUnitArmoured in use?


Code: Select all

function gadget:GetInfo()
  return {
    name      = "Speed / Stealth state",
    desc      = "Toggles a units speed and decloak range.",
    author    = "Zealot",
    date      = "13th July 2013",
    license   = "GNU LGPL, v2.1 or later",
    layer     = 101,  -------------------------What does this do?
    enabled   = true  --  loaded by default?
  }
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

if (not gadgetHandler:IsSyncedCode()) then
  return
end


local EditUnitCmdDesc = Spring.EditUnitCmdDesc
local FindUnitCmdDesc = Spring.FindUnitCmdDesc
local InsertUnitCmdDesc = Spring.InsertUnitCmdDesc
local GiveOrderToUnit = Spring.GiveOrderToUnit
local SetUnitNeutral = Spring.SetUnitNeutral
local GetUnitMoveTypeData = Spring.GetUnitMoveTypeData
local SetGroundMoveTypeData = Spring.MoveCtrl.SetGroundMoveTypeData
local SetUnitCloak = Spring.SetUnitCloak
local GetUnitCmdDescs = Spring.GetUnitCmdDescs

-----setup speedstealth  command



local STEALTHY = {
	[UnitDefNames["armcv"].id] = true,
	[UnitDefNames["corak"].id] = true,
	[UnitDefNames["armpw"].id] = true,
	[UnitDefNames["armwar"].id] = true,
	[UnitDefNames["armrock"].id] = true,
	[UnitDefNames["corstorm"].id] = true,
	[UnitDefNames["corthud"].id] = true,
	[UnitDefNames["armzeus"].id] = true,
	[UnitDefNames["cornecro"].id] = true,
	[UnitDefNames["corpyro"].id] = true,
	[UnitDefNames["armsnipe"].id] = true,
	[UnitDefNames["cormort"].id] = true,
	
}

local stealthyList = {}

local CMD_SPEEDSTEALTH = 34583

local speedstealthCmdDesc = {
	id 		= CMD_SPEEDSTEALTH,
	name 	= "togglespeedstealth",
	action	= "togglespeedstealth",
	type   	= CMDTYPE.ICON_MODE,
	tooltip = 'Toggle slow and stealthy or fast and non stealthy',
	params 	= {1, 'Slow', 'Normal', 'Fast'}
}





function gadget:UnitCreated(unitID, unitDefID, teamID, builderID)
	if STEALTHY[unitDefID] then
   		InsertUnitCmdDesc(unitID, 501, speedstealthCmdDesc)
    	local ms = UnitDefs[unitDefID].speed
    	stealthyList[unitID] = {orgspeed=ms}
  end
end


function gadget:UnitDestroyed(unitID, unitDefID, unitTeam)
  stealthyList[unitID] = nil
end



-----------enable the command


function gadget:AllowCommand(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOptions)
if STEALTHY[unitDefID] then
    if cmdID == CMD_SPEEDSTEALTH then 
     		local cmdDescID = FindUnitCmdDesc(unitID, CMD_SPEEDSTEALTH)
     		--Spring.Echo("cmdparams[1]=" .. tostring(cmdParams[1]))
      		if not cmdDescID then return false end
      	
      		
       		 if  cmdParams[1] == 1 then 
        		
       				SetUnitCloak(unitID, true, 250)  
       				SetGroundMoveTypeData(unitID, {
                	maxSpeed=stealthyList[unitID].orgspeed * 1})
       			
        
     		elseif 
     				 cmdParams[1] == 2 then 
        		
        			SetUnitCloak(unitID, true, 750)   
        			SetGroundMoveTypeData(unitID, {
                	maxSpeed=stealthyList[unitID].orgspeed * 2 })
        			  
              	  
       		elseif
       				 cmdParams[1] == 0 then 
      			
        			SetUnitCloak(unitID, true, 125)  
        			SetGroundMoveTypeData(unitID, {
                	maxSpeed=stealthyList[unitID].orgspeed / 2 })
       		end
      
      --you can't edit a single value in the params table for
        --editUnitCmdDesc, so we generate a new table
        local updatedCmdParams = {
            cmdParams[1],
            speedstealthCmdDesc.params[2],
            speedstealthCmdDesc.params[3],
            speedstealthCmdDesc.params[4]
        }
		Spring.EditUnitCmdDesc(unitID, cmdDescID, { params = updatedCmdParams}) 
		return false 
    	end
    	return true
    end
  end
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: Spring.SetUnitArmoured

Post by FLOZi »

Very simple example here;

https://github.com/spring1944/spring194 ... d_lcgm.lua

Please use pastebin rather than

Code: Select all

 for whole gadgets.
User avatar
Zealot
Posts: 94
Joined: 03 Dec 2012, 13:53

Re: Spring.SetUnitArmoured

Post by Zealot »

Thanks.

You write some really cool gadgets.

I'll try and implement that, and use pastebin next time.

........

Ok I have tried to implement that, and it is not working. I will post the code when I get back from work tomorrow.

I spent about 2 hours trying out every possible way I could think of to make it work, and reading over the wiki but no luck at all.




Also as another option. Is there a way to modify the armordefs file in game?

It seems a bit strange that you cannot define a units armor class in its own unit def but you can only do it in the armordef file.

If all thes options fail, I'll give all the units a sheild that gets activated when they are on slow mode.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Spring.SetUnitArmoured

Post by zwzsg »

- SetUnitArmored only toggle between two state: a fixed damage multiplicator applied, or not applied.
- Armor classes are very static: weapons deal different amount of damage to different group of unitdefs, but all is defined before game starts.
Both could be considered a relic from Total Annihilation legacy.

If you want fine dynamic control over damage, use a gadget with the UnitPreDamaged or UnitDamaged callin instead.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: Spring.SetUnitArmoured

Post by FLOZi »

Since a while back SetUnitArmored can supply the damage multiplicator as 3rd argument, rather than using the one defined in the unitdef.

Still I agree fine control is done better via UnitPreDamaged
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Spring.SetUnitArmoured

Post by knorke »

http://springrts.com/wiki/Armored_when_close_scripts
this page explains it, except for the optional third parameter.

Also it does not that. This:
setting up 3 armor defs, and then using Spring.SetUnitArmoured to change the units armor def.
it can not do.

Advantage of SetUnitArmoured might* be that engines targeting system takes it into account when units select a target to shot, while for solution based on UnitPreDamaged() they will not.**

*too lazy to check
**add more lua and they do
User avatar
Zealot
Posts: 94
Joined: 03 Dec 2012, 13:53

Re: Spring.SetUnitArmoured

Post by Zealot »

Hi.

Thanks for the help.

It turns out that I had coded it correctly just that I had forgot to move an older copy of the script out of the gadgets folder.

Once I discovered that it worked fine.


Here is the Finished Gadget: http://pastebin.com/BesqZgMC


One last question on this on...

Is it possible to cycle both backwards and forwards through a CMD DESC ?

Is it possible / how would you go about binding each of the states to different keys?
Post Reply

Return to “Lua Scripts”