Module:AmmoTable

From BTAWiki
Revision as of 23:30, 19 April 2021 by Revostae (talk | contribs)
Jump to navigation Jump to search

Documentation for this module may be created at Module:AmmoTable/doc

-- Module:AmmoTable creates a fancy-pants table of ammo

-- declare p at the top, return p at the bottom. Functions of p are what's
-- exposed to #invoke calls.
local p = {}

-- we use the getArgs function from the args module to get the args out
-- of either the invoke call, or the template that called invoke.
local getArgs = require('Module:Arguments').getArgs
-- import the Gear module so that we can use its core.cargo.query function,
-- which does the table joins for you. we'll only import 'core', which has
-- the kind of library functions that we want.
local gear = require('Module:Gear').core

-- functions always take a frame as an argument. The frame describes the
-- page that's calling the function
function p.make_ammo(frame)
  -- just always start with this, trust me.
  local tpl_args = getArgs(frame, {parentFirst=true})

  -- now, let's say the parameter on the template for this is just category,
  -- and the template gets a whole category of ammunition.
  local category = tpl_args['Category']

  -- now we are going to query the ammo table. We want to replicate something
  -- similar to the ammo tables created by hand earlier, so we'll use the fields
  -- UIName, Tonnage, Capacity, and Effects.
  -- the database has both fields Name and UIName, and they're slightly different.
  -- check the database first and pick the one that fits best.
  local fields = 'Gear.UIName,Gear.Tonnage,Ammunition.Capacity,Gear.Bonuses,Ammunition.PerUnitCost'
  -- we only want the ammo types that match the category that the caller
  -- specified, so we'll set up a where argument. to make this easier, we
  -- will use string.format.
  local args = { where = string.format('Ammunition.Category="%s"', category) }
  local ammoRows = gear.cargo.query(gear.schema.ammunition, fields, args)

  -- now, we'll use scribunto's html library to build a pretty table
  -- start with, obviously, a table
  local ammoTable = mw.html.create('table')
  -- note here we use a colon : instead of a period. that's because this is
  -- a method call in Lua, not a function call.
  ammoTable:addClass('wikitable')

  -- now, we can set up the table headings
  local heading = ammoTable:tag('tr')
  heading:tag('th'):wikitext('Name')
  heading:tag('th'):wikitext('Tonnage')
  heading:tag('th'):wikitext('Capacity')
  heading:tag('th'):wikitext('Cost per shot')
  heading:tag('th'):wikitext('Effects')

  -- now we get to the meat of the code, where we're looping through the
  -- query results
  for _, ammo in ipairs(ammoRows) do
    local row = ammoTable:tag('tr')
    row:tag('td'):wikitext(ammo['Gear.UIName'])
    row:tag('td'):wikitext(ammo['Gear.Tonnage'])
    row:tag('td'):wikitext(ammo['Ammunition.Capacity'])
    row:tag('td'):wikitext(ammo['Ammunition.PerUnitCost'])

    -- now here comes a bit of a weird part. In blatant disregard for 
    -- the First Normal Form, the Bonuses field is a comma-separated list.
    -- we want to make it pretty though, so what we'll do is put the bonuses
    -- in a ul tag
    local bonusList = row:tag('td'):tag('ul')
    for _, bonus in ipairs(mw.text.split(ammo['Gear.Bonuses'], ',')) do
      -- skip including CostPerShot in the bonuses list, because it's covered
      -- in its own column in the table.
	  if (bonus == 'Caseless') then
	    bonusList:tag('li'):wikitext([[Caseless Ammo increases recoil but fits more ammo per bin.]])
	  
	  elseif (bonus == 'AlwaysStartsFire') then
	    bonusList:tag('li'):wikitext([[Always ignites forests.]])

	  elseif (bonus == 'AlwaysStartsFireAnywhere') then
	    bonusList:tag('li'):wikitext([[Always ignites all terrain.]])

	  elseif (bonus == 'SabotDmg: 20') then
	    bonusList:tag('li'):wikitext([[HEAP does 20 more damage per shot.]])

	  elseif (bonus == 'SabotPercent: 25') then
	    bonusList:tag('li'):wikitext([[HEAP does 25% more damage to armor and 25% less damage to structure.]])

	  elseif (bonus == 'FireTerrainSize: 1') then
	    bonusList:tag('li'):wikitext([[Area Of Burning Terrain: 1]])

	  elseif (bonus == 'FireTerrainSize: 10') then
	    bonusList:tag('li'):wikitext([[Area Of Burning Terrain: 10]])

	  elseif (bonus == 'FireTerrainDuration: 2') then
	    bonusList:tag('li'):wikitext([[Duration Of Burning Terrain: 2]])

	  elseif (bonus == 'FireTerrainDuration: 4') then
	    bonusList:tag('li'):wikitext([[Duration Of Burning Terrain: 4]])

	  elseif (bonus == 'FireTerrainStrength: 2') then
	    bonusList:tag('li'):wikitext([[Strength Of Burning Terrain: 2]])

	  elseif (bonus == 'FireTerrainStrength: 20') then
	    bonusList:tag('li'):wikitext([[Strength Of Burning Terrain: 20]])

	  elseif (bonus == 'OHDamage: 25%') then
	    bonusList:tag('li'):wikitext([[25% bonus Damage against overheating Targets.]])
		
	  elseif (bonus == 'OHDamage: 10%') then
	    bonusList:tag('li'):wikitext([[10% bonus Damage against overheating Targets.]])
		
	  elseif (bonus == 'ImpSight: -75%') then
	    bonusList:tag('li'):wikitext([[Target has 75% reduced Sight for 2 Turns]])

	  elseif (bonus == 'ImpSensors: -30%') then
	    bonusList:tag('li'):wikitext([[Target has 30% reduced Sensors for 2 Turns]])

	  elseif (bonus == 'Thunder') then
	    bonusList:tag('li'):wikitext([[FASCAM munitions will create a minefield wherever a missile hit the ground.]])
	  
  	  elseif (bonus == 'ThunderCripple') then
		bonusList:tag('li'):wikitext([[FASCAM munitions automatically damage the triggering unit's legs and will stop movement through them if they damage leg structure directly.]])

	  elseif (bonus == 'Airburst') then
	    bonusList:tag('li'):wikitext([[Airburst shells split into 4 bomblets after 210m, scattering their payload over the target area.]])

	  elseif (bonus == 'AirburstThumper') then
	    bonusList:tag('li'):wikitext([[Airburst shells split into 10 bomblets after 300m, scattering their payload over the target area.]])

	  elseif (bonus == 'AirburstArrowIV') then
	    bonusList:tag('li'):wikitext([[Airburst shells split into 20 bomblets after 300m, scattering their payload over the target area.]])

	  elseif (bonus == 'AirburstPenalty: 40%') then
	    bonusList:tag('li'):wikitext([[40% damage penalty to direct hits before the airburst shell splits normally, so under 300m.]])

	  elseif (bonus == 'PlasmaNoBoom') then
		bonusList:tag('li'):wikitext([[Plasma Ammo does not explode.]])

	  elseif (bonus == 'ImpAccuracy: 1') then
		bonusList:tag('li'):wikitext([[Target has 1 reduced Accuracy for 2 Turns.]])

	  elseif (bonus == 'Acid: 20%') then
		bonusList:tag('li'):wikitext([[Targets take 20% more damage for 2 Turns - Multiple effects do not stack.]])

	  elseif (bonus == 'ThunderMines: 1') then
		bonusList:tag('li'):wikitext([[Mines Per Projectile: 1]])

	  elseif (bonus == 'ThunderMines: 2') then
		bonusList:tag('li'):wikitext([[Mines Per Projectile: 2]])
		
	  elseif (bonus == 'ThunderMines: 3') then
		bonusList:tag('li'):wikitext([[Mines Per Projectile: 3]])

	  elseif (bonus == 'ThunderChance: 80%') then
		bonusList:tag('li'):wikitext([[Mine Trigger Chance: 80%]])

	  elseif (bonus == 'ThunderChance: 20%') then
		bonusList:tag('li'):wikitext([[Mine Trigger Chance: 20%]])

	  elseif (bonus == 'ThunderAOERadius: 30') then
		bonusList:tag('li'):wikitext([[AOE Mine Explosion Radius: 30]])

	  elseif (bonus == 'ThunderRadius: 5') then
		bonusList:tag('li'):wikitext([[Mine Trigger Radius: 5]])

	  elseif (bonus == 'ThunderRadius: 6') then
		bonusList:tag('li'):wikitext([[Mine Trigger Radius: 6]])

	  elseif (bonus == 'ThunderDamage: 1') then
		bonusList:tag('li'):wikitext([[Direct Mine Damage: 1]])
		
	  elseif (bonus == 'ThunderDamage: 3') then
		bonusList:tag('li'):wikitext([[Direct Mine Damage: 3]])

	  elseif (bonus == 'ThunderDamage: 4') then
		bonusList:tag('li'):wikitext([[Direct Mine Damage: 4]])

	  elseif (bonus == 'ThunderDamage: 5') then
		bonusList:tag('li'):wikitext([[Direct Mine Damage: 5]])
		
	  elseif (bonus == 'ThunderHeatDamage: 1') then
		bonusList:tag('li'):wikitext([[Direct Mine Heat Damage: 1]])	  

	  elseif (bonus == 'ThunderHeatDamage: 3') then
		bonusList:tag('li'):wikitext([[Direct Mine Heat Damage: 3]])	 
		
	  elseif (bonus == 'ThunderHeatDamage: 5') then
		bonusList:tag('li'):wikitext([[Direct Mine Heat Damage: 5]])	 

	  elseif (bonus == 'ThunderAOEDamage: 5') then
		bonusList:tag('li'):wikitext([[AOE Mine Damage: 5]]) 	

	  elseif (bonus == 'ThunderAOEDamage: 10') then
		bonusList:tag('li'):wikitext([[AOE Mine Damage: 10]]) 

	  elseif (bonus == 'ThunderAOEHeatDamage: 5') then
		bonusList:tag('li'):wikitext([[AOE Mine Heat Damage: 5]]) 	

	  elseif (bonus == 'ThunderAOEStabDamage: 1') then
		bonusList:tag('li'):wikitext([[AOE Mine Stability Damage: 1]]) 
		
	  elseif not string.match(bonus, 'CostPerShot') then
		bonusList:tag('li'):wikitext(bonus)
      end
    end
  end

  -- finally, at the end, return the table we made
  return ammoTable
end

-- return p at the bottom, declare p at the top
return p