Page 1 of 1
Performing calculations in unitdefs
Posted: 04 May 2014, 10:46
by Forboding Angel
Is this something that should be done in post?
I essentially want to take a units total damage, divide it by 20 and set that as the energy cost, for pretty much all units, but in a lot of ways it would be better if the calculation could be done in the unitdef (giving me more direct control than having to define a table of units to ignore in weapondefs post).
Any thoughts on the best way to go about doing it?
Re: Performing calculations in unitdefs
Posted: 04 May 2014, 10:52
by FLOZi
Code: Select all
local HP = 100
local fooDef = {
...
maxHealth = HP,
energycost = HP / 20,
...
}
return lowerkeys({ ["foo"] = fooDef })
Or do it in _post but have it triggered by a customParam rather than white/blacklisting.
Re: Performing calculations in unitdefs
Posted: 04 May 2014, 11:05
by Forboding Angel
Ahh ok. As a quick test I did this in post:
Code: Select all
weapondamage = tonumber(wd.damage.default)
energycost = wd.energyPerShot
if (weapondamage > 0) then
energycost = 1 * 0 + weapondamage / 20 --fugly math to make 100% sure that the original value is zeroed out so no surprises
end
Spring.Echo(wd.name, energycost)
And it works:
Code: Select all
[f=0000000] elighttank3weapon, 2.1500000953674
But when I fire the weapon, it uses the value that is set in the weapondef (30), not the value calculated in post. I was thinking that maybe I'm setting the wrong param, but I don't see how that would be so. I looked up energyPerShot in the wiki and didn't see any lua name alternative or anything funky like that. Am I missing something here?
I'll check out your method flozi, thanks

Re: Performing calculations in unitdefs
Posted: 04 May 2014, 11:17
by Forboding Angel
Your method works perfectly. Can you not declare locals in tables? I guess not. For some reason I was thinking you could.
Anyway, this gives me ideas for how I can automate some things.
This is essentially what I ended up with:
Code: Select all
local weapon1Damage = 43
local weaponDefs = {
lighttankweapon = {
badTargetCategory = [[ARMORED BUILDING]],
AreaOfEffect = 1,
avoidFriendly = false,
avoidFeature = false,
collideFriendly = false,
collideFeature = false,
craterBoost = 0,
craterMult = 0,
explosionGenerator = "custom:genericshellexplosion-medium-lightning",
energypershot = weapon1Damage / 20,
impulseBoost = 0,
impulseFactor = 0,
interceptedByShieldType = 4,
lineOfSight = true,
name = "elighttank3weapon",
noSelfDamage = true,
range = 500,
reloadtime = 1,
WeaponType = "LightningCannon",
rgbColor = "0.1 0.2 0.5",
rgbColor2 = "0 0 1",
soundStart = "jacobs.wav",
startsmoke = "1",
texture1 = "lightning",
thickness = 5,
turret = true,
weaponVelocity = 400,
customparams = {
damagetype = "elighttank3",
--Upgrades--
upgradeClass = "groundweapons",
},
damage = {
default = weapon1Damage,
},
},
}
unitDef.weaponDefs = weaponDefs
It struck me that it might be a good idea to have a lot of this in configs. Like common weapon things, but tbh for now this works perfectly. I'd much rather it be on a per unit basis other than in post, because something like this in post could easily get very tedious to maintain.
Re: Performing calculations in unitdefs
Posted: 04 May 2014, 11:33
by FLOZi
Hodor, clear misunderstanding of what you meant but yes will work just as well for weapons.
No, you cannot declare variables within a table.
Not sure what you are doing in _post, there.
Re: Performing calculations in unitdefs
Posted: 04 May 2014, 11:39
by Forboding Angel
In post I was trying to do exactly what I'm doing in the weapondef.
Sorry, my weapondefs are in the unitdef file. Topic title fail.
For the sake of interest, I also use the same calculation for shields:
Code: Select all
local shield1PowerRegen = 25
local weaponDefs = {
shield = {
IsShield = true,
Smartshield = true,
Exteriorshield = true,
Visibleshield = true,
Visibleshieldrepulse = true,
ShieldStartingPower = 0,
Shieldenergyuse = 0,
Shieldradius = 200,
Shieldpower = 1000,
Shieldpowerregen = shield1PowerRegen,
Shieldpowerregenenergy = shield1PowerRegen / 20,
Shieldintercepttype = 4,
Shieldgoodcolor = "0.0 0.2 1.0",
Shieldbadcolor = "1.0 0 0",
Shieldalpha = 0.3,
ShieldRepulsor = false,
texture1 = "shield4",
visibleShieldHit = true,
visibleShieldHitFrames = 50,
weaponType = [[Shield]],
damage = {
default = 1,
},
},
}
unitDef.weaponDefs = weaponDefs
In evo, energy = damage
energy = metal
So technically, to do 2000 damage, it costs you 10 metal.