Page 1 of 1

Lua weapondefs with inheritance

Posted: 06 May 2012, 12:54
by FLOZi
Started this ~10 months ago, finally converted the last S44 main weapons. :lol:

May or may not find it relevant to your interests:

The magic (Thanks to jk)

The weapondefs

Re: Lua weapondefs with inheritance

Posted: 07 May 2012, 12:47
by Forboding Angel
I'm not sure I understand why you would do weapons this way (IE, not in the unitdef)? It seems grossly inefficient and difficult to maintain?

Re: Lua weapondefs with inheritance

Posted: 07 May 2012, 13:16
by FLOZi
S44 weapons have always been 'not in the unitdef' because they are shared across multiple units. It is far more efficient and much easier to maintain this way than defining the same machinegun 9 times. And with the inheritance it is now even easier.

Re: Lua weapondefs with inheritance

Posted: 07 May 2012, 18:06
by knorke
That is imo a good idea.
Would use something like that, if not for two reasons:
-not enought similiar weapons
-did not use it right from start, now too lazy to change it. easier to pile on top of the existing mess :wink:

Re: Lua weapondefs with inheritance

Posted: 07 May 2012, 18:25
by smoth
Forboding Angel wrote:I'm not sure I understand why you would do weapons this way (IE, not in the unitdef)? It seems grossly inefficient and difficult to maintain?
I don't store the weapons in the unitdef.

Re: Lua weapondefs with inheritance

Posted: 07 May 2012, 18:26
by smoth
flozi, what does this do? forbs post doesn't tell me anything more.

What would I be inheriting? can you give an example of how it works out?

Re: Lua weapondefs with inheritance

Posted: 07 May 2012, 18:39
by knorke
example:
http://spring1944.svn.sourceforge.net/v ... iew=markup

At top of file there is the "default" rifle, and under that the "variants" of that weapon.

All the sniper rifles are the same, except:
-each has a unique sound
-the british one reloads faster
-us rifle is more accurate when moving (fooking noscope)

So to make all snipers deal more damage (example) you only need to change one number instead of 4.

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

That is not so nice imo:

Code: Select all

return lowerkeys({
Enfield_T = Enfield_T,
K98kScope = K98kScope,
M1903Springfield = M1903Springfield,
MosinNagantPU = MosinNagantPU,
It just repeats things that already exist..

Would something like that work?

Code: Select all

return {
["K98kScope" = SniperRifleClass:New{
name = [[Karabiner 98k Scoped]],
soundStart = [[GER_K98K]],
},
["M1903Springfield"] = SniperRifleClass:New{
...},
}

Re: Lua weapondefs with inheritance

Posted: 07 May 2012, 18:52
by FLOZi
Yes knorke that would work if you prefer it.

Personally I think the mortars/arty and tank guns are more interesting examples as they use a form of multiple inheritance (Base class, Armour Piercing or Smoke or High Explosive class, then the weapon specific base class)

http://spring1944.svn.sourceforge.net/v ... iew=markup

So, here is code spam:

Basic 'LightGun' class, a class for all 40-50mm direct fire (tank & ship) guns in S44.

Code: Select all

local LightGunClass = Weapon:New{
  accuracy           = 100,
  collisionSize      = 4,
  impulseFactor      = 0,
  intensity          = 0.25,
  leadBonus          = 0.5,
  leadLimit          = 0,
  movingAccuracy     = 500, --590 for 2pdr?
  separation         = 2, 
  size               = 1,
  soundStart         = [[US_37mm]], -- move later?
  stages             = 50,
  tolerance          = 300,
  turret             = true,
  weaponType         = [[Cannon]],
}
Almost all of these weapons have two versions - AP and HE. So make additional classes with those shared attributes:

Code: Select all

-- HE Round Class
local LightGunHEClass = Weapon:New{
  edgeEffectiveness  = 0.2,
  explosionGenerator = [[custom:HE_Small]],
  explosionSpeed     = 30, -- needed?
  name               = [[HE Shell]],
  rgbColor           = [[0.5 0.5 0.0]],
  soundHit           = [[GEN_Explo_2]],
  customparams = {
    damagetype         = [[explosive]],
    fearaoe            = 40,
    fearid             = 301,
  },
}

-- AP Round Class
local LightGunAPClass = Weapon:New{
  areaOfEffect       = 5,
  canattackground    = false,
  colormap           = [[ap_colormap.png]],
  edgeEffectiveness  = 0.1,
  explosionGenerator = [[custom:AP_Small]],
  explosionSpeed     = 100, -- needed?
  name               = [[AP Shell]],
  soundHit           = [[GEN_Explo_1]],
  customparams = {
    damagetype         = [[kinetic]],
  },  
}
Now I want to make a particular implementation of a 40mm gun, the British 2pdr:

Code: Select all

-- QF 2Pdr 40mm (GBR)
local QF2Pdr40mm = LightGunClass:New{
  name               = [[QF 2 Pdr Mk.X]],
  range              = 1070,
  reloadTime         = 4.5,
}
It should have a HE and an AP version, with some additional specific tags that are not common to the 2pdr itself nor all AP or HE weapons:

Code: Select all

local QF2Pdr40mmHE = QF2Pdr40mm:New(LightGunHEClass, true):New{
  areaOfEffect       = 42,
  weaponVelocity     = 1584,
  damage = {
    default            = 350,
  },  
}
local QF2Pdr40mmAP = QF2Pdr40mm:New(LightGunAPClass, true):New{
  weaponVelocity     = 1616,
  customparams = {
    armor_penetration_1000m = 45,
    armor_penetration_100m  = 59,
  },
  damage = {
    default            = 1105,
  },
}
At the end I return all the weapons I want to be available in the game, but not their base classes:

Code: Select all

return lowerkeys({
  -- QF 2Pdr
  QF2Pdr40mmHE = QF2Pdr40mmHE,
  QF2Pdr40mmAP = QF2Pdr40mmAP,
}}
Essentially each call of :New just adds all of the new tags to the table, overwriting any that already exist.
The first parameter takes a table of the new values, so you can pass an existing table or a new one using {}.
The second parameter is whether or not to concatenate (true) or overwrite (false) the name string, I use it here to append ' AP Shell' and ' HE Shell' respectively.

edit:
knorke wrote:Would use something like that, if not for two reasons:
-not enought similiar weapons
Yes - Its really only useful if you have weapons used by multiple units that share many attributes. In that respect it's a solution to an S44 specific problem, but I thought others might find it useful / be inspired to do something similar / just interesting to see an alternative to the CA defs. 8)

Re: Lua weapondefs with inheritance

Posted: 23 Dec 2012, 23:15
by PicassoCT
Tried to port this to units: May be incomplete or completely wrong.

script:
http://paste.springfiles.com/view/664e1aca
example:
http://paste.springfiles.com/view/c1eea3b3