Please note that some information on this wiki is still based on the demo version.
If you notice any errors, feel free to correct them.

Module:Abilities/Grid: Difference between revisions

From Super Fantasy Kingdom Official Wiki
wgg>Cadaei
off by 1 fix
var name fix
 
(17 intermediate revisions by 4 users not shown)
Line 2: Line 2:


function p.grid(f)
function p.grid(f)
-- when called via a template calling invoke
return p.create(f:getParent().args)
return p.create(f:getParent().args)
end
function p.gridInvoke(f)
-- when called with {{#invoke:Abilities/Grid|gridInvoke|mark=1}}
return p.create(f.args)
end
end
             
             
function p.create(args)
function p.create(args)
     local columns = args.columns or 5
-- columns and rows are fixed to 5 for now
     local rows = args.rows or 5
     local columns = 5 --args.columns or 5
     local rows = 5 --args.rows or 5
      
      
     -- indexing fields: starting top left with 1, counting up row by row
     -- indexing fields: starting top left with 1, counting up row by row
     local centerIndex = math.floor(columns * rows / 2) + 1
    local totalCellCount = columns * rows;
     local centerIndex = math.floor(totalCellCount / 2) + 1
      
      
     local classes = {
     local classDefinitions = {
     ['center'] = ' class="influence-center"',
     ['center'] = 'influence-center',
     ['passive'] = ' class="influence-passive"',
     ['1'] = 'influence-blue',
     ['active'] = ' class="influence-active"'
    ['all'] = 'influence-red',
     ['source'] = 'influence-source'
     }
     }
      
      
     local rowsResult = {}
     local mark = args['mark'] or ''
     local index = 0
     local markSource = args['markSource'] or ''
     for x=1, columns do
     local markAllCells = string.find(mark, 'all') ~= nil
    local fieldsResult = {}
    local markCells = {}
    for y=1, rows do
    for i in string.gmatch(mark, '%d+') do
    index = index + 1
    markCells[tonumber(i)] = true
    local class = ''
    end
    if index == centerIndex then
    local markCellsSource = {}
    class = classes['center']
    for i in string.gmatch(markSource, '%d+') do
    end
    markCellsSource[tonumber(i)] = true
if args[index] ~= nil and classes[args[index]] ~= nil then
    end
    class = classes[args[index]]
     end
local gridCells = {}
table.insert(fieldsResult, '<td'..class..'></td>')
for i = 1, totalCellCount do
local classes = {}
    if i == centerIndex then
     table.insert(classes, classDefinitions['center'])
     end
     end
     table.insert(rowsResult, '<tr>'..table.concat(fieldsResult)..'</tr>')
if markAllCells then
    end
    table.insert(classes, classDefinitions['all'])
     return '<table class="influence-table">'..table.concat(rowsResult)..'</table>'
     end
if markCells[i] then
    table.insert(classes, classDefinitions['1'])
end
local classStyling = ''
if #classes > 0 then
classStyling = ' class="'..table.concat(classes, ' ')..'"'
end
local sourceMarker = ''
if markCellsSource[i] then
sourceMarker = '<span class="'..classDefinitions['source']..'"/>'
end
table.insert(gridCells, '<div'..classStyling..'>'..sourceMarker..'</div>')
end
     return '<div class="influence-grid" title="The center square represents the unit.&#010;Colored squares represent units affected by the effect&#010;Circles represent units needed for the effect.">'..
    table.concat(gridCells)..'</div>'
end
end


return p
return p

Latest revision as of 18:52, 27 November 2025

Creates a grid where some cells can be highlighted. The center is always white unless highlighted by another color.

This module is used by Module:Abilities.

Arguments

  • mark: all or comma separated list of indices to highlight, e.g. 7,8,9.
  • markSource: comma separated list of indices to highlight as effect source, e.g. 7,8,9.

Indices

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

Examples

mark = 7,8,9,12,14

mark = 7,8,9,12,14 | markSource=18

mark = all


local p = {}

function p.grid(f)
	-- when called via a template calling invoke
	return p.create(f:getParent().args)
end

function p.gridInvoke(f)
	-- when called with {{#invoke:Abilities/Grid|gridInvoke|mark=1}}
	return p.create(f.args)
end
		             
function p.create(args)
	-- columns and rows are fixed to 5 for now
    local columns = 5 --args.columns or 5
    local rows = 5 --args.rows or 5
    
    -- indexing fields: starting top left with 1, counting up row by row
    local totalCellCount = columns * rows;
    local centerIndex = math.floor(totalCellCount / 2) + 1
    
    local classDefinitions = {
    	['center'] = 'influence-center',
    	['1'] = 'influence-blue',
    	['all'] = 'influence-red',
    	['source'] = 'influence-source'
    	}
    
    local mark = args['mark'] or ''
    local markSource = args['markSource'] or ''
    local markAllCells = string.find(mark, 'all') ~= nil
    local markCells = {}
    for i in string.gmatch(mark, '%d+') do
    	markCells[tonumber(i)] = true
    end
    local markCellsSource = {}
    for i in string.gmatch(markSource, '%d+') do
    	markCellsSource[tonumber(i)] = true
    end
	
	local gridCells = {}
	for i = 1, totalCellCount do
		local classes = {}
    	if i == centerIndex then
    		table.insert(classes, classDefinitions['center'])
    	end
		if markAllCells then
    		table.insert(classes, classDefinitions['all'])
    	end
		if markCells[i] then
    		table.insert(classes, classDefinitions['1'])
		end
		local classStyling = ''
		if #classes > 0 then
			classStyling = ' class="'..table.concat(classes, ' ')..'"'
		end
		
		local sourceMarker = ''
		if markCellsSource[i] then
			sourceMarker = '<span class="'..classDefinitions['source']..'"/>'
		end
		
		table.insert(gridCells, '<div'..classStyling..'>'..sourceMarker..'</div>')
	end
	
    return '<div class="influence-grid" title="The center square represents the unit.&#010;Colored squares represent units affected by the effect&#010;Circles represent units needed for the effect.">'..
    table.concat(gridCells)..'</div>'
end

return p