Module:Npcinfo

De Terraria Wiki
Aller à la navigation Aller à la recherche
Voir aussi la page anglaise du module : Module:Npcinfo. Elle pourra contenir des informations plus complètes et actuelles.

Aucune sous-page de documentation n'existe déjà pour ce module. En créer une maintenant.


local data = mw.loadData( 'Module:npcinfo/data' ) -- loading data table.

local info = {
	version = '1.4.4.1', -- set this to the version displayed on Module:Npcinfo/data
	IDs = { -- min and max are both inclusive
		min = -65,
		max = 687,
	}
}
info.IDs.isValid = function(id)
	-- 76/146/408/547 are unused
	return id ~= nil and type(id) == 'number' and id >= info.IDs.min and id ~= 0 and id <= info.IDs.max
end

local getArg = function(frame, key)
	local value = frame.args[key]
	if not value then
		return nil
	end
	value = mw.text.trim(value)
	if value == '' then
		return nil
	else
		return value
	end
end

local fields = {'DeathSound',
'HitSound',
'Opacity',
'aiStyle',
'alpha',
'behindTiles',
'banner',
'boss',
'buffImmune',
'buffImmuneAll',
'buffImmuneAllNotWhips',
'catchItem',
'coldDamage',
'damage',
'damage_e',
'damage_e_hm',
'damage_e_pp',
'damage_m',
'damage_m_hm',
'damage_m_pp',
'defense',
'defense_e',
'defense_e_hm',
'defense_e_pp',
'defense_m',
'defense_m_hm',
'defense_m_pp',
'dontCountMe',
'dontTakeDamage',
'friendly',
'height',
'housingCategory',
'internalName',
'knockBackResist',
'knockBackResist_e',
'knockBackResist_e_hm',
'knockBackResist_e_pp',
'knockBackResist_m',
'knockBackResist_m_hm',
'knockBackResist_m_pp',
'lavaImmune',
'lifeMax',
'lifeMax_e',
'lifeMax_e_hm',
'lifeMax_e_pp',
'lifeMax_m',
'lifeMax_m_hm',
'lifeMax_m_pp',
'name',
'netAlways',
'netID',
'noGravity',
'noTileCollide',
'npcSlots',
'rarity',
'scale',
'timeLeft',
'townNPC',
'trapImmune',
'type',
'value',
'value_e',
'value_e_hm',
'value_e_pp',
'value_m',
'value_m_hm',
'value_m_pp',
'width',}


local statname = {}

local clear = function (frame)
		local prefix = getArg(frame, 'prefix') or '_npcinfo_'

		local args = {}
		for i,v in ipairs(fields) do
			table.insert(args, prefix .. v)
			table.insert(args, '')
		end

		frame:callParserFunction{ name = '#dplvar:set', args = args }
	end

local p = {
	-- info table for other modules
	info = info,
	
	-- info table data for templates
	v = function(frame)
		return info.version
	end,
	maxId = function(frame)
		return info.IDs.max
	end,
	isValidId = function(frame)
		return info.IDs.isValid(tonumber(getArg(frame, 1)) or 0) and 1 or 0
	end,

	getInfo = function (frame)
		local npcid = tonumber(getArg(frame, 'id') or 0)
		local prefix = getArg(frame, 'prefix') or '_npcinfo_'
		local count = tonumber(getArg(frame, 'players') or 1)
		
		if not npcid then
			return
		end

		if not info.IDs.isValid(npcid) then
			clear(frame) -- reset all dpl vars
			return
		end
		
		local result
		if count > 1 then
			local getinfo = require('Module:Npcinfo/datagen').getInfo
			result = getinfo(npcid, count)
			local buffImmune = nil
			for k, v in pairs(result.buffImmune) do
				if v then
					if buffImmune then
						buffImmune = buffImmune .. ', ' .. k
					else
						buffImmune = k
					end
				end
			end
			result.buffImmune = buffImmune			
		else
			result = {}
			for k,v in pairs(data[0]) do
				result[k] = v
			end
			for k,v in pairs(data[npcid]) do
				result[k] = v
			end
		end
		
		local args = {}
		for k,v in pairs(result) do
			args[#args + 1] = prefix .. k
			args[#args + 1] = v
		end
		
		args[#args + 1] = prefix .. '_fields'
		args[#args + 1] = table.concat(fields, ',')
		
		args[#args + 1] = prefix
		args[#args + 1] = 'InvalidNpcinfoStatName'

		frame:callParserFunction{ name = '#dplvar:set', args = args }

	end,

	clear = clear,


	getStat = function (frame)
		local npcid = tonumber(getArg('id')) or tonumber(getArg(1)) or 0
		local stat = getArg('stat') or getArg(2)
		stat = statname[stat] or stat

		if not info.IDs.isValid(npcid) then
			return 
		end

		if not stat then
			return
		end

		return data[npcid][stat] or data[0][stat]

	end,

	-- for module.
	stat = function(npcid, stat)
		stat = statname[stat] or stat

		if not info.IDs.isValid(npcid) then
			return 
		end

		if not stat then
			return
		end

		return data[npcid][stat] or data[0][stat]

	end,
}
p.getinfo = p.getInfo
p.GetInfo = p.getInfo
p.getstat = p.getStat
p.GetStat = p.getStat
return p