Module:Item/Mod: Difference between revisions

From Growtopia
Jump to navigationJump to search
>HashJona
m add ghost param
>HashJona
m add Cosmetic Clothes category
 
(18 intermediate revisions by the same user not shown)
Line 1: Line 1:
local effect = {
local mods = require('Module:Item/ModList')
['Double Jump'] = 'jump a second time in mid-air',
['Speedy'] = 'move faster on land',
['Punch Damage'] = 'break blocks faster',
['Punch Range'] = function(args)
return args['punch_range_custom'] or 'punch one tile further'
end,
['Punch Power'] = 'knock back other players further',
['Skin Color'] = function(args)
local skin = args['skin'] or 'a specific tone'
return 'change their skin color to ' .. skin
end,
['Miscellaneous'] = function(args)
return args['misc'] or ""
end,
['Light Source'] = 'emit light in [[Dark Background Block (block category)|dark areas]]',
['Build Range'] = function(args)
return args['build_range_custom'] or 'build one tile further'
end,
['Punch Pull'] = 'pull other players towards them upon punching them',
['High Jump'] = 'jump one tile higher',
['Slow Fall'] = 'glide slowly to the ground when holding the jump button',
['Wall Climbing'] = 'cling onto [[Climbing Wall|Climbing Walls]] by pressing the jump button',
['Fire Hose'] = 'extinguish [[Fire|fires]]',
['Fireproof'] = 'take 50% less damage from [[Lava Pain Block (block category)|lava pain blocks]]',
['Ghost Immunity'] = function(args)
local ghost = args['ghost'] or 'all types of [[Ghost|ghosts]]'
return 'immunity against' .. ghost
end,
['Healing'] = 'heal faster than usual after receiving damage from hitting a [[Pain Block (block category)|pain block]]',
['XP Buff'] = 'gain more [[Leveling|XP]]',
['Slippery'] = 'glide on any surface as if it is [[Slippery Block (block category)|slippery]]',
['Putt Putt Putt'] = 'move slower, but allows the player to harvest trees by walking through them',
['Damage Reduction'] = 'break blocks more slowly',
['Knock Back Reduction'] = 'resist a percentage of knockback when [[Punching|punched]] by other players',
['Speedy in Water'] = 'move faster in water',
['Float on Water'] = 'float on [[Water Bucket|water]]',
['Grow Effect'] = 'expand in size',
['Zombie Weapon'] = 'kill [[G-Virus|zombies]] during a Pandemic',
['Low Jump'] = 'decrease the player\'s jump height',
['Card Battle Health'] = 'adds 2 additional health to the player during card battles against a [[Villains|villain]] or another player',
['Card Battle Strength'] = 'reduce 2 health from the [[Villains|villain]] or another player',
['Cookie Hunter'] = 'provides a significant chance of doubling fruit drops from [[Fortune Cookie]] and [[Lucky Fortune Cookie|Lucky Fortune Cookie Trees]]',
['Night Vision'] = 'eliminate the darkness effect created by [[Dark Background Block (block category)|dark background blocks]]',
['Extra Gems'] = function (args)
local chance = args['gem_chance'] or ""
if chance ~= "" then
chance = chance .. " "
end
return 'get extra ' .. chance .. 'gems from breaking blocks'
end,
['Extra Blocks'] = function (args)
local chance = args['block_chance'] or ""
if chance ~= "" then
chance = chance .. " "
end
return 'get an extra ' .. chance .. 'chance for a block broken to drop itself'
end,
}
 
local item = {}
local item = {}


function joinWithConnector(list, connector)
function join_with_connector(list, connector)
connector = connector or "and"
connector = connector or 'and'
     local n = #list
     local n = #list
     if n == 0 then
     if n == 0 then
         return ""
         return ''
     elseif n == 1 then
     elseif n == 1 then
         return list[1]
         return list[1]
     elseif n == 2 then
     elseif n == 2 then
         return list[1] .. " " .. connector ..  " " .. list[2]
         return list[1] .. ' ' .. connector ..  ' ' .. list[2]
     elseif n >= 3 then
     elseif n >= 3 then
         local firstPart = table.concat(list, ", ", 1, n - 1)
         local first_part = table.concat(list, ', ', 1, n - 1)
         return firstPart .. ", " .. connector ..  " " .. list[n]
         return first_part .. ', ' .. connector ..  ' ' .. list[n]
     end
     end
end
end
 
function starts_with(str, prefix)
function parseArgs(args)
local keys = {}
for k, _ in pairs(args) do
if type(k) == 'number' then
table.insert(keys, k)
end
end
return keys
end
 
function debugResult(var, debugFlag)
  if debugFlag == "1" then
      return "<pre>" .. var .. "</pre>"
  else
      return mw.getCurrentFrame():preprocess(var)
  end
end
 
function startsWith(str, prefix)
return str:sub(1, #prefix) == prefix
return str:sub(1, #prefix) == prefix
end
end
function length(obj)
function length(obj)
local count = 0
local count = 0
Line 105: Line 26:
return count;
return count;
end
end
 
function non_empty(value, fallback)
function nonEmpty(value, fallback)
if value == nil or value == '' then
if value == nil or value == "" then
         return fallback
         return fallback
     else
     else
Line 114: Line 34:
end
end


function parseNotes(args)
function parse_args(args)
local notes = ''
local keys = {}
for k, _ in pairs(args) do
for i = 1, 3 do
if type(k) == 'number' then
local note = args['note_' .. i] or ""
table.insert(keys, k)
if note ~= "" then
notes = notes .. "|" .. note
end
end
end
end
return notes
return keys
end
function debug_result(var, debug_flag)
  if debug_flag == '1' then
      return '<pre>' .. var .. '</pre>'
  else
      return mw.getCurrentFrame():preprocess(var)
  end
end
end


function item.parseDescriptionEffects(args)
 
local parsedEffects = {}
function parse_notes(args)
local notes = ''
local categories = {}
local categories = {}
local addCat = not args['no_cat']
local keys = parse_args(args)
local keys = parseArgs(args)
local current_page = mw.title.getCurrentTitle()


for i = 2, #keys do
    for i = 2, #keys do
local argIndex = keys[i]
local argIndex = keys[i]
local key = args[argIndex]
local key = args[argIndex]
local mod = mods[key]
if effect[key] ~= nil then
 
local text = effect[key]
if mod ~= nil then
if type(effect[key]) == "function" then
if type(mod) == 'function' then
text = effect[key](args)
mod = mod(args)
end
if addCat then
table.insert(categories, "[[Category:" .. key .. " Mod Items]]")
end
end
table.insert(parsedEffects, text)
            if  mod['hide_mod_category'] ~= true
            and current_page
            and not starts_with(current_page.text, mod['mod_category'] .. '/') then
    table.insert(categories, "''[[" .. mod['mod_category'] .. '|'.. mod['label'] .."]]''")
            end
if mod.notes ~= '' then
                notes = notes .. '|' .. mod.notes
            end
end
end
end
end
    categories = join_with_connector(categories, args["connector"])
if (#keys - #parsedEffects) ~= 1 and addCat then
    if categories ~= '' then
table.insert(categories, "[[Category:ItemModError]]")
notes =  '|Also grants ' .. categories .. notes
end
end


return {
for i = 1, 2 do
description = joinWithConnector(parsedEffects, args["connector"]),
local note = args['note_' .. i] or ''
categories = table.concat(categories)
if note ~= '' then
}
notes = notes .. '|' .. note
end
end
return notes
end
end


function item.parseTableCategories(args)
function item.parse_description(args)
local parsed_effects = {}
local categories = {}
local categories = {}
local keys = parseArgs(args)
local add_cat = not args['no_cat']
local currentPage = mw.title.getCurrentTitle()
local keys = parse_args(args)
local is_mod_broken = false;


for i = 2, #keys do
for i = 2, #keys do
local argIndex = keys[i]
local argIndex = keys[i]
local key = args[argIndex]
local key = args[argIndex]
local modCategory = "Mods/" .. key
local mod = mods[key]
 
if key ~= "Miscellaneous"
if mod ~= nil then
and effect[key] ~= nil
if type(mod) == 'function' then
and currentPage
mod = mod(args)
and not startsWith(currentPage.text, modCategory .. "/") then
end
table.insert(categories, "''[[" .. modCategory .. "|".. key .."]]''")
if add_cat then
table.insert(categories, '[[' .. mod.category .. ']]')
end
if mod.description ~= '' then
table.insert(parsed_effects, mod.description)
end
else
is_mod_broken = true
end
end
end
end
return joinWithConnector(categories, args["connector"])
if is_mod_broken and add_cat then
table.insert(categories, '[[Category:Broken Mod]]')
end
 
return {
description = join_with_connector(parsed_effects, args['connector']),
categories = table.concat(categories)
}
end
end


Line 183: Line 132:
local title = parent['title'] or mw.title.getCurrentTitle().text or 'unknown'
local title = parent['title'] or mw.title.getCurrentTitle().text or 'unknown'
local label = parent['label'] or parent[1]
local label = parent['label'] or parent[1]
     local parsedEffects =  item.parseDescriptionEffects(parent)
     local parsed_effects =  item.parse_description(parent)
     local baseSentence = "When equipped, the '''" .. mw.text.nowiki(title) .. "''' grants the ''" .. mw.text.nowiki(label) .. "'' [[Mods|mod]]"
     local base_sentence = "When equipped, the '''" .. mw.text.nowiki(title) .. "'''"
     local fullSentence
     local full_sentence
      
      
     if parsedEffects.description == "" then  
     if label ~= '' and label ~= nil then
     fullSentence = baseSentence .. "."
    sentence_with_label = base_sentence .. " grants the ''" .. mw.text.nowiki(label) .. "'' [[Mods|mod]]"
    if parsed_effects.description == '' then
    full_sentence = sentence_with_label .. '.'
    elseif label == '-' then
    full_sentence = base_sentence .. ' allows the player to ' .. parsed_effects.description .. '.'
     else
    full_sentence = sentence_with_label .. ', which allows the player to ' .. parsed_effects.description .. '.'
    end
     else  
     else  
     fullSentence = baseSentence .. ", which allows the player to " .. parsedEffects.description .. "."
     full_sentence = base_sentence .. " serves a purely cosmetic purpose.[[Category:Cosmetic Clothes]]"
     end
     end
     return fullSentence .. parsedEffects.categories
     return full_sentence .. parsed_effects.categories
   
end
end


function item.table(frame)  
function item.table(frame)  
local parent = frame:getParent().args
local parent = frame:getParent().args
local name = parent['title'] or "%PAGE%"
local name = parent['title'] or '%PAGE%'
local label = parent['label'] or parent[1]
local label = parent['label'] or parent[1] or ''
local notes = parseNotes(parent)  
local notes = parse_notes(parent)  
local effects = item.parseTableCategories(parent) or ""
if effects ~= "" then
if label == '-' then
effects = "|Also grants " .. effects
label = ''
end
end


return "\n{{Mod|" .. name .. "|" .. label .. effects .. notes .. "}}"
return '\n{{Mod|' .. name .. '|' .. label .. notes .. '}}'
end
end


Line 224: Line 179:
table.insert(doc, '! scope="col" style="width:75%" |Meaning')
table.insert(doc, '! scope="col" style="width:75%" |Meaning')
table.insert(doc, '|-')
table.insert(doc, '|-')
table.insert(doc, '| rowspan=' .. length(effect) .. ' | <span style="background:#FF9999">Mod</span>')
table.insert(doc, '| rowspan=' .. length(mods) .. ' | <span style="background:#FF9999">Mod</span>')
for k, v in pairs(effect) do
for k, v in pairs(mods) do
table.insert(doc, "|'''" .. k .. "'''")
table.insert(doc, "|'''" .. k .. "'''")
     if type(v) == "function" then
     if type(v) == 'function' then
     local defaultValue = v({})
     local default_value = v({}).description
     local value = v(args)
     local value = v(args).description
     if(defaultValue == value) then
     if(default_value == value) then
     table.insert(doc, "|" .. nonEmpty(defaultValue, "''No default value''"))
     table.insert(doc, '|' .. non_empty(default_value, "''No default value''"))
     else  
     else  
     table.insert(doc, "|" .. "{{Bullet}} " .. nonEmpty(defaultValue, "''No default value''") .. "<br>{{Bullet}} " .. value)
     table.insert(doc, '|' .. '{{Bullet}} ' .. non_empty(default_value, "''No default value''") .. '<br>{{Bullet}} ' .. value)
     end
     end
         else
         else
             table.insert(doc, "|" .. v)
             table.insert(doc, '|' .. v.description)
     end
     end
     table.insert(doc, '|-')
     table.insert(doc, '|-')
end
end
table.insert(doc, '|}')
table.insert(doc, '|}')
return debugResult(table.concat(doc, '\n'), frame.args['debug'])
return debug_result(table.concat(doc, '\n'), frame.args['debug'])
end
end


return item
return item

Latest revision as of 15:21, 3 October 2025

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

local mods = require('Module:Item/ModList')
local item = {}

function join_with_connector(list, connector)
	connector = connector or 'and'
    local n = #list
    if n == 0 then
        return ''
    elseif n == 1 then
        return list[1]
    elseif n == 2 then
        return list[1] .. ' ' .. connector ..  ' ' .. list[2]
    elseif n >= 3 then
        local first_part = table.concat(list, ', ', 1, n - 1)
        return first_part .. ', ' .. connector ..  ' ' .. list[n]
    end
end
function starts_with(str, prefix)
	return str:sub(1, #prefix) == prefix
end
function length(obj)
	local count = 0
	for _ in pairs(obj) do 
		count = count + 1 
	end
	return count;
end
function non_empty(value, fallback)
	if value == nil or value == '' then
        return fallback
    else
        return value
    end
end

function parse_args(args)
	local keys = {}
	for k, _ in pairs(args) do
		if type(k) == 'number' then
			table.insert(keys, k)
		end
	end
	return keys
end
function debug_result(var, debug_flag)
   if debug_flag == '1' then 
       return '<pre>' .. var .. '</pre>'
   else
       return mw.getCurrentFrame():preprocess(var)
   end
end


function parse_notes(args)
	local notes = ''
	local categories = {}
	local keys = parse_args(args)
	local current_page = mw.title.getCurrentTitle()

    for i = 2, #keys do
		local argIndex = keys[i]
		local key = args[argIndex]
		local mod = mods[key]

		if mod ~= nil then
			if type(mod) == 'function' then
				mod = mod(args)
			end
            if  mod['hide_mod_category'] ~= true
            and current_page 
            and not starts_with(current_page.text, mod['mod_category'] .. '/') then
			    table.insert(categories, "''[[" .. mod['mod_category'] .. '|'.. mod['label'] .."]]''")
            end
			if mod.notes ~= '' then
                notes = notes .. '|' .. mod.notes
            end
		end
	end
    categories = join_with_connector(categories, args["connector"])
    if categories ~= '' then
		notes =  '|Also grants ' .. categories .. notes
	end

	for i = 1, 2 do
		local note = args['note_' .. i] or ''
		if note ~= '' then
			notes = notes .. '|' .. note
		end
	end
	return notes
end

function item.parse_description(args)
	local parsed_effects = {}
	local categories = {}
	local add_cat = not args['no_cat']
	local keys = parse_args(args)
	local is_mod_broken = false;

	for i = 2, #keys do
		local argIndex = keys[i]
		local key = args[argIndex]
		local mod = mods[key]

		if mod ~= nil then
			if type(mod) == 'function' then
				mod = mod(args)
			end
			if add_cat then
				table.insert(categories, '[[' .. mod.category .. ']]')
			end
			if mod.description ~= '' then
				table.insert(parsed_effects, mod.description)
			end
		else 
			is_mod_broken = true
		end
	end
	
	if is_mod_broken and add_cat then
		table.insert(categories, '[[Category:Broken Mod]]')
	end

	return {
		description = join_with_connector(parsed_effects, args['connector']),
		categories = table.concat(categories)
	}
end

function item.description(frame) 
	local parent = frame:getParent().args
	local title = parent['title'] or mw.title.getCurrentTitle().text or 'unknown'
	local label = parent['label'] or parent[1]
    local parsed_effects =  item.parse_description(parent)
    local base_sentence = "When equipped, the '''" .. mw.text.nowiki(title) .. "'''"
    local full_sentence
    
    if label ~= '' and label ~= nil then
    	sentence_with_label = base_sentence .. " grants the ''" .. mw.text.nowiki(label) .. "'' [[Mods|mod]]"
    	if parsed_effects.description == '' then 
    		full_sentence = sentence_with_label .. '.'
    	elseif label == '-' then
    		full_sentence = base_sentence .. ' allows the player to ' .. parsed_effects.description .. '.'
    	else
    		full_sentence = sentence_with_label .. ', which allows the player to ' .. parsed_effects.description .. '.'
    	end
    else 
    	full_sentence = base_sentence .. " serves a purely cosmetic purpose.[[Category:Cosmetic Clothes]]"
    end
    return full_sentence .. parsed_effects.categories
    
end

function item.table(frame) 
	local parent = frame:getParent().args
	local name = parent['title'] or '%PAGE%'
	local label = parent['label'] or parent[1] or ''
	local notes = parse_notes(parent) 
	
	if label == '-' then
		label = ''
	end

	return '\n{{Mod|' .. name .. '|' .. label  .. notes .. '}}'
end

function item.documentation(frame)
	local doc = {}
	local args = {
		misc = '<span style="background:#FF9999">misc</span>',
		skin = '<span style="background:#FF9999">skin</span>',
		gem_chance = '<span style="background:#FF9999">gem_chance</span>',
		block_chance = '<span style="background:#FF9999">block_chance</span>'
	}
	table.insert(doc, '{|class="article-table" border="0" cellpadding="1" cellspacing="1" width="100%"')
	table.insert(doc, '|-')
	table.insert(doc, '! scope="col" style="width:15%" |Parameter')
	table.insert(doc, '! scope="col" style="width:10%" |Value')
	table.insert(doc, '! scope="col" style="width:75%" |Meaning')
	table.insert(doc, '|-')
	table.insert(doc, '| rowspan=' .. length(mods) .. ' | <span style="background:#FF9999">Mod</span>')
	
	for k, v in pairs(mods) do
		table.insert(doc, "|'''" .. k .. "'''")
    	if type(v) == 'function' then
    		local default_value = v({}).description
    		local value = v(args).description
    		if(default_value == value) then
    			table.insert(doc, '|' .. non_empty(default_value, "''No default value''"))
    		else 
    			table.insert(doc, '|' .. '{{Bullet}} ' .. non_empty(default_value, "''No default value''") .. '<br>{{Bullet}} ' .. value)
    		end
        else
            table.insert(doc, '|' .. v.description)
    	end
    table.insert(doc, '|-')
	end
	table.insert(doc, '|}')
	return debug_result(table.concat(doc, '\n'), frame.args['debug'])
end

return item