Module:Falloff

From TeamSpec Wiki
Jump to navigation Jump to search

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

-- Draws a weapon's damage falloff as a bar chart. Hovering a bar shows the
-- damage at that distance, as the game's inspect window reads it out.
local data = mw.loadData('Module:Falloff/data')
local p = {}


function p.chart(frame)
	local name = frame.args[1]
	if not name or name == '' then
		name = mw.title.getCurrentTitle().text
	end
	local w = data[name]
	if not w then
		return ''
	end
	local top = 1
	for _, s in ipairs(w.samples) do
		top = math.max(top, s[2])
	end
	local out = {}
	table.insert(out, '<div class="ts-falloff" style="max-width:640px;margin:8px 0 4px 0;">')
	table.insert(out, '<div class="ts-falloff-plot">')
	-- The full-damage line.
	table.insert(out, string.format('<div class="ts-falloff-full" style="bottom:%.2f%%;"></div>', 100 / top))
	for _, t in ipairs(w.thresholds) do
		table.insert(out, string.format(
			'<div class="ts-falloff-threshold" style="left:%.2f%%;"></div>',
			t / w.max * 100))
	end
	for _, s in ipairs(w.samples) do
		table.insert(out, string.format(
			'<div class="ts-falloff-bar" title="%s" data-readout="%s" style="height:%.2f%%;"></div>',
			mw.text.encode(s[3]), mw.text.encode(s[3]), s[2] / top * 100))
	end
	table.insert(out, '</div>')
	-- Distances under the axis: the start, every threshold, the end.
	table.insert(out, '<div style="position:relative;height:1.4em;font-size:85%;">')
	table.insert(out, '<span style="position:absolute;left:0;">0 m</span>')
	for _, t in ipairs(w.thresholds) do
		table.insert(out, string.format('<span style="position:absolute;left:%.2f%%;transform:translateX(-50%%);">%s m</span>',
			t / w.max * 100, mw.language.getContentLanguage():formatNum(math.floor(t * 10 + 0.5) / 10)))
	end
	table.insert(out, string.format('<span style="position:absolute;right:0;">%s m</span>',
		mw.language.getContentLanguage():formatNum(math.floor(w.max * 10 + 0.5) / 10)))
	table.insert(out, '</div>')
	table.insert(out, '<div class="ts-falloff-caption">Hover or touch the chart to read the damage at each distance. The dashed line is full damage; dotted lines mark where the falloff changes.</div>')
	table.insert(out, '</div>')
	return table.concat(out)
end

return p