Page 1 of 1

Code for Object.lua

Posted: Sat Jul 03, 2010 1:35 pm
by shani
Here's the new functions for /tome/class/object.lua to replace getDesc() and getTextualDesc()
to eliminiate the code duplication :)

Code: Select all

--- Gets the full textual desc of the object without the name and requirements
function _M:getTextualDesc()
    desc = { }
    if self.encumber then
		desc[#desc+1] = ("#67AD00#%0.2f Encumbrance."):format(self.encumber)
	end

	desc[#desc+1] = ("Type: %s / %s"):format(self.type, self.subtype)

	-- Stop here if unided
	if not self:isIdentified() then return table.concat(desc, "\n") end


	if self.combat then
		local dm = {}
		for stat, i in pairs(self.combat.dammod or {}) do
			dm[#dm+1] = ("+%d%% %s"):format(i * 100, Stats.stats_def[stat].name)
		end
		desc[#desc+1] = ("%d Power [Range %0.2f] (%s), %d Attack, %d Armor Penetration, Crit %d%%"):format(self.combat.dam or 0, self.combat.damrange or 1.1, table.concat(dm, ','), self.combat.atk or 0, self.combat.apr or 0, self.combat.physcrit or 0)
		if self.combat.range then desc[#desc+1] = "Firing range: "..self.combat.range end
		desc[#desc+1] = ""
	end

	local w = self.wielder or {}
	if w.combat_atk or w.combat_dam or w.combat_apr then desc[#desc+1] = ("Attack %d, Armor Penetration %d, Physical Crit %d%%, Physical power %d"):format(w.combat_atk or 0, w.combat_apr or 0, w.combat_physcrit or 0, w.combat_dam or 0) end
	if w.combat_armor or w.combat_def then desc[#desc+1] = ("Armor %d, Defense %d"):format(w.combat_armor or 0, w.combat_def or 0) end
	if w.fatigue then desc[#desc+1] = ("Fatigue %d%%"):format(w.fatigue) end

	if w.inc_stats then
		local dm = {}
		for stat, i in pairs(w.inc_stats) do
			dm[#dm+1] = ("%d %s"):format(i, Stats.stats_def[stat].name)
		end
		desc[#desc+1] = ("Increases stats: %s."):format(table.concat(dm, ','))
	end

	if w.melee_project then
		local rs = {}
		for typ, dam in pairs(w.melee_project) do
			rs[#rs+1] = ("%d %s"):format(dam, DamageType.dam_def[typ].name)
		end
		desc[#desc+1] = ("Damage on hit: %s."):format(table.concat(rs, ','))
	end

	if w.on_melee_hit then
		local rs = {}
		for typ, dam in pairs(w.on_melee_hit) do
			rs[#rs+1] = ("%d %s"):format(dam, DamageType.dam_def[typ].name)
		end
		desc[#desc+1] = ("Damage when hit: %s."):format(table.concat(rs, ','))
	end

	if w.resists then
		local rs = {}
		for res, i in pairs(w.resists) do
			rs[#rs+1] = ("%d%% %s"):format(i, DamageType.dam_def[res].name)
		end
		desc[#desc+1] = ("Increases resistances: %s."):format(table.concat(rs, ','))
	end

	if w.inc_damage then
		local rs = {}
		for res, i in pairs(w.inc_damage) do
			rs[#rs+1] = ("%d%% %s"):format(i, DamageType.dam_def[res].name)
		end
		desc[#desc+1] = ("Increases damage type: %s."):format(table.concat(rs, ','))
	end

	if w.esp then
		local rs = {}
		for type, i in pairs(w.esp) do
			if type == "all" then rs[#rs+1] = "all"
			elseif type == "range" then rs[#rs+1] = "increase range by "..i
			else
				local _, _, t, st = type:find("^([^/]+)/?(.*)$")
				if st then
					rs[#rs+1] = st
				else
					rs[#rs+1] = t
				end
			end
		end
		desc[#desc+1] = ("Grants telepathy: %s."):format(table.concat(rs, ','))
	end

	if w.talents_types_mastery then
		local tms = {}
		for ttn, i in pairs(w.talents_types_mastery) do
			local tt = Talents.talents_types_def[ttn]
			local cat = tt.type:gsub("/.*", "")
			local name = cat:capitalize().." / "..tt.name:capitalize()
			tms[#tms+1] = ("%0.2f %s"):format(i, name)
		end
		desc[#desc+1] = ("Increases talent masteries: %s."):format(table.concat(tms, ','))
	end

	if w.combat_physresist then desc[#desc+1] = ("Increases physical resistance: %s."):format(w.combat_physresist) end
	if w.combat_spellresist then desc[#desc+1] = ("Increases spell resistance: %s."):format(w.combat_spellresist) end
	if w.combat_mentalresist then desc[#desc+1] = ("Increases mental resistance: %s."):format(w.combat_mentalresist) end

	if w.blind_immune then desc[#desc+1] = ("Increases blindness immunity: %d%%."):format(w.blind_immune * 100) end
	if w.poison_immune then desc[#desc+1] = ("Increases poison immunity: %d%%."):format(w.poison_immune * 100) end
	if w.cut_immune then desc[#desc+1] = ("Increases cut immunity: %d%%."):format(w.cut_immune * 100) end
	if w.confusion_immune then desc[#desc+1] = ("Increases confusion immunity: %d%%."):format(w.confusion_immune * 100) end
	if w.stun_immune then desc[#desc+1] = ("Increases stun immunity: %d%%."):format(w.stun_immune * 100) end
	if w.fear_immune then desc[#desc+1] = ("Increases fear immunity: %d%%."):format(w.fear_immune * 100) end
	if w.knockback_immune then desc[#desc+1] = ("Increases knockback immunity: %d%%."):format(w.knockback_immune * 100) end
	if w.instakill_immune then desc[#desc+1] = ("Increases instant-death immunity: %d%%."):format(w.instakill_immune * 100) end

	if w.life_regen then desc[#desc+1] = ("Regenerates %0.2f hitpoints each turn."):format(w.life_regen) end
	if w.mana_regen then desc[#desc+1] = ("Regenerates %0.2f mana each turn."):format(w.mana_regen) end

	if w.max_life then desc[#desc+1] = ("Maximum life %d"):format(w.max_life) end
	if w.max_mana then desc[#desc+1] = ("Maximum mana %d"):format(w.max_mana) end
	if w.max_stamina then desc[#desc+1] = ("Maximum stamina %d"):format(w.max_stamina) end

	if w.combat_spellpower or w.combat_spellcrit then desc[#desc+1] = ("Spellpower %d, Spell Crit %d%%"):format(w.combat_spellpower or 0, w.combat_spellcrit or 0) end

	if w.lite then desc[#desc+1] = ("Light radius %d"):format(w.lite) end

	if w.see_invisible then desc[#desc+1] = ("See invisible: %d"):format(w.see_invisible) end
	if w.invisible then desc[#desc+1] = ("Invisibility: %d"):format(w.invisible) end

	local use_desc = self:getUseDesc()
	if use_desc then desc[#desc+1] = use_desc end

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

--- Gets the full desc of the object
function _M:getDesc()
	local _, c = self:getDisplayColor()
	local desc
	if not self:isIdentified() then
		desc = { c..self:getName().."#FFFFFF#" }
	else
		desc = { c..self:getName().."#FFFFFF#", self.desc }
	end

	local reqs = self:getRequirementDesc(game.player)
	if reqs then
		desc[#desc+1] = reqs
	end

	local textdesc = self:getTextualDesc()

	return table.concat(desc, "\n").."\n"..textdesc
end
Edit: fixed copy paste problem