Module:Table Animals

From Eco - English Wiki
Revision as of 17:30, 22 February 2022 by Demian (talk | contribs) (Correct typo.)

Template:Doc/start Template:Template backend It creates an HTML table with data from Module:AnimalData

This module uses the following modules:

Template:Doc/end


local p = {}
local Utils = require("Module:Utils")
local AnimalUtils = require("Module:UtilsAnimalLists")
local HTMLUtils = require("Module:UtilsHTML")

local function addFootnotes(t)
	-- Sadly I could not find a way to add HTML to the table header generated with the Lua code below.
	-- <a id=\"cite_ref-1\" class=\"reference\" href=\"#cite_note-1\">[1]</a>
	-- <a id=\"cite_ref-2\" class=\"reference\" href=\"#cite_note-2\">[2]</a>
	table.insert(t, "<strong>Footnotes</strong>")
	table.insert(t, "<div class=\"mw-references-wrap\">")
	table.insert(t, "<ol class=\"references\">")
	table.insert(t, "<li id=\"cite_note-1\"><span class=\"mw-cite-backlink\"><a href=\"#cite_ref-1\" aria-label=\"Jump up\" title=\"Jump up\">&uarr;</a></span> <span class=\"reference-text\">Calories consumed by predator animals who eat this animal. Not applicable to players, see the <strong>Harvest Item</strong> column for details on the player consumable food items.</span></li>")
	table.insert(t, "<li id=\"cite_note-2\"><span class=\"mw-cite-backlink\"><a href=\"#cite_ref-2\" aria-label=\"Jump up\" title=\"Jump up\">&uarr;</a></span> <span class=\"reference-text\">The minimum and maximum delay between attacks. Lower values equal faster attacks.</span></li>")
	table.insert(t, "</ol></div>")
end

function p.main()
	-- Import the data.
	local animals = require("Module:AnimalData").animals
	-- Table to build the rows to.
	local t = { }
	-- Sort animals by their name so the generated table will be pre-sorted on page load.
	local sortedNames = Utils.getSortedKeys(animals)
	-- Temporary variable to store animal data in.
	local data;
	local maxAttackDelay;

	-- The following values are left out as rather niche or unclear information.
	-- maturity: unclear.
	-- headDistance: distance between heads when sleeping in a pack.
	-- resourceBonus: unclear.

	-- Create the table header.
	table.insert(t, "<table class=\"wikitable sortable\">")

	table.insert(t, "<tr>")
	table.insert(t, "<th rowspan=\"3\">Animal</th>")
	table.insert(t, "<th colspan=\"5\">Movement</th>")
	table.insert(t, "<th colspan=\"2\">Nutrition</th>")
	table.insert(t, "<th colspan=\"10\">Hunting</th>")
	table.insert(t, string.format("<th rowspan=\"3\">Carbon<br>Released<br>(%s)</th>", HTMLUtils.textUnit_PartsPerMillion()))
	table.insert(t, "</tr>")

	table.insert(t, "<tr>")
	-- Header: Movement
	table.insert(t, "<th rowspan=\"2\">Swims?</th>")
	table.insert(t, "<th rowspan=\"2\">Flies?</th>")
	table.insert(t, string.format("<th rowspan=\"2\">Climb<br>Height (%s)</th>", HTMLUtils.textUnit_Meters()))
	table.insert(t, string.format("<th colspan=\"2\">Speed (%s)</th>", HTMLUtils.textUnit_MetersPerSecond()))

	-- Header: Nutrition
	table.insert(t, "<th rowspan=\"2\">Eats</th>")
	table.insert(t, "<th rowspan=\"2\">Calories</th>")

	-- Header: Hunting
	table.insert(t, "<th rowspan=\"2\">Attacks?</th>")
	table.insert(t, "<th rowspan=\"2\">Flees?</th>")
	table.insert(t, "<th rowspan=\"2\">Fear<br>Factor</th>")
	table.insert(t, "<th rowspan=\"2\">Health</th>")
	table.insert(t, string.format("<th rowspan=\"2\">Detect<br>Range (%s)</th>", HTMLUtils.textUnit_Meters()))
	table.insert(t, "<th colspan=\"4\">Attack</th>")
	table.insert(t, "<th rowspan=\"2\">Harvest Item</th>")
	table.insert(t, "</tr>")

	table.insert(t, "<tr>")
	-- Header: Speed
	table.insert(t, "<th>Idle</th>")
	table.insert(t, "<th>Run</th>")

	-- Header: Attack
	table.insert(t, "<th>Chance (%)</th>")
	table.insert(t, string.format("<th>Range (%s)</th>", HTMLUtils.textUnit_Meters()))
	table.insert(t, "<th>Damage</th>")
	table.insert(t, "<th>Delay</th>")
	table.insert(t, "</tr>")

	-- Create the table rows.
	for _, animalName in ipairs(sortedNames) do
		data = animals[animalName]

		table.insert(t, "<tr>")
		table.insert(t, string.format("<td>%s</td>", Utils.formatWikilink(Utils.getDirectPageName(animalName, "animal"), animalName, false)))

		-- Movement 5 columns
		table.insert(t, string.format("<td style=\"text-align:center;\">%s</td>", Utils.formatNilToYesNo(data.isSwimming)))
		table.insert(t, string.format("<td style=\"text-align:center;\">%s</td>", Utils.formatNilToYesNo(data.isFlying)))
		table.insert(t, string.format("<td style=\"text-align:right;\">%s</td>", AnimalUtils.formatClimbHeight(data.climbHeight)))
		table.insert(t, string.format("<td style=\"text-align:right;\">%.1f</td>", Utils.valueOrDash(tonumber(data.wanderingSpeed))))
		table.insert(t, string.format("<td style=\"text-align:right;\">%.1f</td>", Utils.valueOrDash(tonumber(data.speed))))

		-- Nutrition 2 columns
		table.insert(t, string.format("<td>%s</td>", AnimalUtils.formatFoodSources(data.foodSources)))
		table.insert(t, string.format("<td style=\"text-align:right;\">%s</td>", Utils.valueOrDash(tonumber(data.calorieValue))))

		-- Hunting 10 columns
		maxAttackDelay = tonumber(data.maxAttackDelay)
		table.insert(t, string.format("<td style=\"text-align:center;\">%s</td>", Utils.formatBoolToYesNo(0.0 ~= maxAttackDelay and 0.0 ~= tonumber(data.damage))))
		table.insert(t, string.format("<td style=\"text-align:center;\">%s</td>", Utils.formatNilToYesNo(data.flees)))
		table.insert(t, string.format("<td style=\"text-align:right;\">%.1f</td>", data.fearFactor))
		table.insert(t, string.format("<td style=\"text-align:right;\">%.1f</td>", data.health))
		table.insert(t, string.format("<td style=\"text-align:right;\">%.1f</td>", data.detectRange))
		table.insert(t, string.format("<td style=\"text-align:right;\">%.1f</td>", data.chanceToAttack))
		table.insert(t, string.format("<td style=\"text-align:right;\">%.1f</td>", data.attackRange))
		table.insert(t, string.format("<td style=\"text-align:right;\">%.1f</td>", data.damage))

		if 0.0 == maxAttackDelay then
			table.insert(t, string.format("<td style=\"text-align:right;\">—</td>"))
		else
			table.insert(t, string.format("<td style=\"text-align:right;\">%s</td>", Utils.toRangeString(data.minAttackDelay, data.maxAttackDelay, data.minAttackDelay, "%0.1f")))
		end

		if nil == data.resourceItem then
			table.insert(t, string.format("<td>—</td>"))
		else
			table.insert(t, string.format("<td>%s</td>", AnimalUtils.formatResourceItem(data.resourceItem, data.resourceMin, data.resourceMax)))
		end

		table.insert(t, string.format("<td style=\"text-align:right;\">%.4f</td>", data.carbonRelease))

		table.insert(t, "</tr>")
	end

	table.insert(t, "</table>")

	return table.concat(t," \n")
end

return p