Module:MagneticGunLister

From Terra Invicta Official Wiki

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

---
---Reads the Magnetic Gun data files and presents them in readable formats, with additional calculated statistics.
---
---@module MagneticGunLister
local MagneticGunLister = {}

--region Dependencies
local RestrictionChecker = require('Module:RestrictionChecker')
local IconModule = require('Module:Icon')
local MagneticGunData = mw.loadData('Module:CSVReader/MagneticGun')
local MagneticGunNames = mw.loadData('Module:ENReader/MagneticGun')
local ProjectNameBuilder = require('Module:ProjectLister/NameBuilder')
--endregion

--region Private constants
local floor = math.floor
local round = function(num, decimals)
	local mult = 10^(decimals or 0)
	return floor(num * mult + 0.5) / mult
end
--endregion

--region Private methods
local function getMountDisplayName(mount)
	local mountNames = {
		OneHull = "One Hull Slot",
		TwoHullHoriz = "Two Hull Slots", 
		FourHull = "Four Hull Slots",
		HalfNose = "Half Nose Slot",
		OneNose = "One Nose Slot",
		TwoNoseVert = "Two Nose Slots",
		ThreeNoseAngle = "Three Nose Slots",
		FourNose = "Four Nose Slots"
	}
	return mountNames[mount] or mount
end

local function calculateStationaryDamage(wmass, muzzle)
	return round(tonumber(wmass) * tonumber(muzzle) ^ 2 / 40,2)
end

local function calculateFireRate(salvo, cooldown, scooldown)
	if not salvo or tonumber(salvo) == 1 then
		return round(60 / tonumber(cooldown),1)
	else
		local fullCooldown = tonumber(cooldown) + (tonumber(salvo) - 1) * tonumber(scooldown)
		return round(math.max(1,tonumber(salvo)) * 60 / fullCooldown,1)
	end
end

local function calculateFullCooldown(salvo, cooldown, scooldown)
	if not salvo or salvo == 1 then
		return tonumber(cooldown)
	else
		return tonumber(cooldown) + (tonumber(salvo) - 1) * tonumber(scooldown)
	end
end

local function calculatePowerPerShot(amass, muzzle, eff)
	return round(tonumber(amass) * tonumber(muzzle) ^ 2 / 2000 / tonumber(eff), 3)
end

local function calculatePowerPerMinute(fireRate, powerPerShot)
	return round(fireRate * powerPerShot, 3)
end

local function calculateResourceCost(mass, resourceWeight, crew, crewResource)
	local baseCost = tonumber(mass) * (tonumber(resourceWeight) or 0) / 10
	local crewCost = tonumber(crew) * (tonumber(crewResource) or 0)
	return baseCost + crewCost
end

local function calculateAmmoResourceCost(magazine, amass, resourceWeight)
	return tonumber(magazine) * tonumber(amass) * (tonumber(resourceWeight) or 0) / 10000
end

-- Safe value access with fallback
local function getValue(row, key, default)
	local value = row[key]
	if value == nil or value == '' then
		return default or 0
	end
	return value
end
--endregion

--region Public methods
function MagneticGunLister.ListBasicMagneticGuns(frame)
	local returnString = '{| class="wikitable sortable mw-collapsible" style="text-align: center;"'
	returnString = returnString .. '\n! Magnetic Gun'
	returnString = returnString .. '\n! Warhead Mass<br>(kg)'
	returnString = returnString .. '\n! Muzzle Velocity<br>(km/s)'
	returnString = returnString .. '\n! Stationary Damage'
	returnString = returnString .. '\n! Fire Rate<br>(shots/min)'
	returnString = returnString .. '\n! Range<br>(km)'
	returnString = returnString .. '\n|-'
	
	for rowID, row in pairs(MagneticGunData) do
		if RestrictionChecker.AllPass(frame.args, row) then
			returnString = returnString .. '\n| ' .. (MagneticGunNames['TIMagneticGunTemplate.displayName.' .. rowID] or rowID)
			returnString = returnString .. '\n| ' .. getValue(row, 'warheadMass_kg', 0)
			returnString = returnString .. '\n| ' .. getValue(row, 'muzzleVelocity_kps', 0)
			
			local stationaryDamage = calculateStationaryDamage(getValue(row, 'warheadMass_kg'), getValue(row, 'muzzleVelocity_kps'))
			returnString = returnString .. '\n| ' .. stationaryDamage
			
			local fireRate = calculateFireRate(getValue(row, 'salvo_shots',1), getValue(row, 'cooldown_s'), getValue(row, 'intraSalvoCooldown_s'))
			returnString = returnString .. '\n| ' .. fireRate
			
			returnString = returnString .. '\n| ' .. getValue(row, 'targetingRange_km', 0)
			returnString = returnString .. '\n|-'
		end
	end
	
	returnString = returnString .. '\n|}'
	return returnString
end

function MagneticGunLister.ListDetailedMagneticGuns(frame)
	local returnString = '{| class="wikitable sortable mw-collapsible" style="text-align: center;"'
	returnString = returnString .. '\n! rowspan=2|Magnetic Gun'
	returnString = returnString .. '\n! rowspan=2|Required Project'
	returnString = returnString .. '\n! rowspan=2|Hard Points'
	returnString = returnString .. '\n! rowspan=2|Can PD'
	returnString = returnString .. '\n! rowspan=2|Warhead Mass<br>(kg)'
	returnString = returnString .. '\n! rowspan=2|Muzzle Velocity<br>(km/s)'
	returnString = returnString .. '\n! rowspan=2|Stationary Damage'
	returnString = returnString .. '\n! rowspan=2|Chip %'
	returnString = returnString .. '\n! rowspan=2|Salvo'
	returnString = returnString .. '\n! rowspan=2|Full Cooldown<br>(s)'
	returnString = returnString .. '\n! rowspan=2|Fire Rate<br>(shots/min)'
	returnString = returnString .. '\n! rowspan=2|Range<br>(km)'
	returnString = returnString .. '\n! rowspan=2|Crew'
	returnString = returnString .. '\n! rowspan=2|Mass<br>(tons)'
	returnString = returnString .. '\n! colspan=5| Non-Ammo Build Resources'
	returnString = returnString .. '\n! rowspan=2|Magazine'
	returnString = returnString .. '\n! rowspan=2|Ammo Mass<br>(kg)'
	returnString = returnString .. '\n! colspan=3| Full Ammo Resources'
	returnString = returnString .. '\n! rowspan=2|Efficiency'
	returnString = returnString .. '\n! rowspan=2|Power Per Shot<br>(GJ)'
	returnString = returnString .. '\n! rowspan=2|Power Per Minute<br>(GJ/min)'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n!' .. IconModule.Icon({args={'water'}})
	returnString = returnString .. '\n!' .. IconModule.Icon({args={'volatiles'}})
	returnString = returnString .. '\n!' .. IconModule.Icon({args={'metals'}})
	returnString = returnString .. '\n!' .. IconModule.Icon({args={'nobles'}})
	returnString = returnString .. '\n!' .. IconModule.Icon({args={'exotics'}})
	returnString = returnString .. '\n!' .. IconModule.Icon({args={'volatiles'}})
	returnString = returnString .. '\n!' .. IconModule.Icon({args={'metals'}})
	returnString = returnString .. '\n!' .. IconModule.Icon({args={'nobles'}})
	returnString = returnString .. '\n|-'
	
	for rowID, row in pairs(MagneticGunData) do
		if RestrictionChecker.AllPass(frame.args, row) then
			returnString = returnString .. '\n| ' .. (MagneticGunNames['TIMagneticGunTemplate.displayName.' .. rowID] or rowID)
			
			returnString = returnString .. '\n| ' .. ProjectNameBuilder.buildName({args={row['requiredProjectName']}})
			
			returnString = returnString .. '\n| ' .. getMountDisplayName(getValue(row, 'mount'))
			if getValue(row, 'defenseMode', 'false') == 'true' then
				returnString = returnString .. '\n| Yes'
			else
				returnString = returnString .. '\n| No'
			end
			returnString = returnString .. '\n| ' .. getValue(row, 'warheadMass_kg', 0)
			returnString = returnString .. '\n| ' .. getValue(row, 'muzzleVelocity_kps', 0)
			
			local stationaryDamage = calculateStationaryDamage(getValue(row, 'warheadMass_kg'), getValue(row, 'muzzleVelocity_kps'))
			returnString = returnString .. '\n| ' .. stationaryDamage
			
			local chipPercent = (tonumber(getValue(row, 'flatChipping')) or 0) * 100
			returnString = returnString .. '\n| ' .. chipPercent .. '%'
			returnString = returnString .. '\n| ' .. getValue(row, 'salvo_shots',1)
			
			local fullCooldown = calculateFullCooldown(getValue(row, 'salvo_shots',1), getValue(row, 'cooldown_s'), getValue(row, 'intraSalvoCooldown_s'))
			returnString = returnString .. '\n| ' .. fullCooldown
			
			local fireRate = calculateFireRate(getValue(row, 'salvo_shots',1), getValue(row, 'cooldown_s'), getValue(row, 'intraSalvoCooldown_s'))
			returnString = returnString .. '\n| ' .. fireRate
			
			returnString = returnString .. '\n| ' .. getValue(row, 'targetingRange_km', 0)
			returnString = returnString .. '\n| ' .. getValue(row, 'crew', 0)
			
			local baseMass = tonumber(getValue(row, 'baseWeaponMass_tons', 0))
			local crewCount = tonumber(getValue(row, 'crew', 0))
			local massWithCrew = baseMass + crewCount * 4
			returnString = returnString .. '\n| ' .. massWithCrew
			
			-- Non-ammo build resources
			local waterCost = calculateResourceCost(getValue(row, 'baseWeaponMass_tons'), 0, getValue(row, 'crew'), 0.2)
			local volatilesCost = calculateResourceCost(getValue(row, 'baseWeaponMass_tons'), getValue(row, 'weightedBuildMaterials/volatiles'), getValue(row, 'crew'), 0.2)
			local metalsCost = calculateResourceCost(getValue(row, 'baseWeaponMass_tons'), getValue(row, 'weightedBuildMaterials/metals'), getValue(row, 'crew'), 0)
			local noblesCost = calculateResourceCost(getValue(row, 'baseWeaponMass_tons'), getValue(row, 'weightedBuildMaterials/nobleMetals'), getValue(row, 'crew'), 0)
			local exoticsCost = calculateResourceCost(getValue(row, 'baseWeaponMass_tons'), getValue(row, 'weightedBuildMaterials/exotics'), getValue(row, 'crew'), 0)
			
			returnString = returnString .. '\n| ' .. round(waterCost, 2)
			returnString = returnString .. '\n| ' .. round(volatilesCost, 2)
			returnString = returnString .. '\n| ' .. round(metalsCost, 2)
			returnString = returnString .. '\n| ' .. round(noblesCost, 2)
			returnString = returnString .. '\n| ' .. round(exoticsCost, 2)
			
			returnString = returnString .. '\n| ' .. getValue(row, 'magazine', 0)
			returnString = returnString .. '\n| ' .. getValue(row, 'ammoMass_kg', 0)
			
			-- Ammo resources
			local ammoVolatiles = calculateAmmoResourceCost(getValue(row, 'magazine'), getValue(row, 'ammoMass_kg'), getValue(row, 'ammoMaterials/volatiles'))
			local ammoMetals = calculateAmmoResourceCost(getValue(row, 'magazine'), getValue(row, 'ammoMass_kg'), getValue(row, 'ammoMaterials/metals'))
			local ammoNobles = calculateAmmoResourceCost(getValue(row, 'magazine'), getValue(row, 'ammoMass_kg'), getValue(row, 'ammoMaterials/nobleMetals'))
			
			returnString = returnString .. '\n| ' .. round(ammoVolatiles, 1)
			returnString = returnString .. '\n| ' .. round(ammoMetals, 1)
			returnString = returnString .. '\n| ' .. round(ammoNobles, 1)
			
			returnString = returnString .. '\n| ' .. getValue(row, 'efficiency', 1)
			
			local powerPerShot = calculatePowerPerShot(getValue(row, 'ammoMass_kg'), getValue(row, 'muzzleVelocity_kps'), getValue(row, 'efficiency'))
			returnString = returnString .. '\n| ' .. powerPerShot
			
			local powerPerMinute = calculatePowerPerMinute(fireRate, powerPerShot)
			returnString = returnString .. '\n| ' .. powerPerMinute
			
			returnString = returnString .. '\n|-'
		end
	end
	
	returnString = returnString .. '\n|}'
	
	return returnString
end

--endregion

return MagneticGunLister