Character dump code
Posted: Thu Jul 01, 2010 5:24 pm
				
				Charactger textual dump version 1.0   
 
/tome/dialogs/CharacterSheet.lua
			 
 /tome/dialogs/CharacterSheet.lua
Code: Select all
function _M:dump()
	fs.mkdir("/character-dumps")
	local file = "/character-dumps/"..(game.player.name:gsub("[^a-zA-Z0-9_-.]", "_")).."-"..os.date("%Y%m%d-%H%M%S")..".txt"
	local fff = fs.open(file, "w")
	local labelwidth = 17
	local nl = function(s) fff:write(s or "") fff:write("\n") end
	local nnl = function(s) fff:write(s or "") end
	--prepare label and value
	local makelabel = function(s,r) while s:len() < labelwidth do s = s.." " end return ("%s: %s"):format(s, r) end	
	local cur_exp, max_exp = game.player.exp, game.player:getExpChart(game.player.level+1)
	nl("  [Tome 4.00 @ www.te4.org Character Dump]")
	nl()
		
	nnl(("%-32s"):format(makelabel("Sex", game.player.descriptor.sex)))		
	nl(("STR:  %d"):format(game.player:getStr()))
	
	nnl(("%-32s"):format(makelabel("Race", game.player.descriptor.subrace)))	
	nl(("DEX:  %d"):format(game.player:getDex()))
	
	nnl(("%-32s"):format(makelabel("Class", game.player.descriptor.subclass)))	
	nl(("MAG:  %d"):format(game.player:getMag()))
	
	nnl(("%-32s"):format(makelabel("Level", ("%d"):format(game.player.level))))
	nl(("WIL:  %d"):format(game.player:getWil()))
	nnl(("%-32s"):format(makelabel("Exp", ("%d%%"):format(100 * cur_exp / max_exp))))	
	nl(("CUN:  %d"):format(game.player:getCun()))
	
    nnl(("%-32s"):format(makelabel("Gold", ("%.2f"):format(game.player.money))))	
	nl(("CON:  %d"):format(game.player:getCon()))		
    -- All weapons in main hands
	local strings = {}
	for i = 1, 5 do strings[i]="" end
	if self.actor:getInven(self.actor.INVEN_MAINHAND) then
		for i, o in ipairs(self.actor:getInven(self.actor.INVEN_MAINHAND)) do
			if o.combat then				
				strings[1] = ("Attack(Main Hand): %3d"):format(game.player:combatAttack(o.combat))
				strings[2] = ("Damage(Main Hand): %3d"):format(game.player:combatDamage(o.combat))
				strings[3] = ("APR   (Main Hand): %3d"):format(game.player:combatAPR(o.combat))
				strings[4] = ("Crit  (Main Hand): %3d%%"):format(game.player:combatCrit(o.combat))
				strings[5]= ("Speed (Main Hand): %0.2f"):format(game.player:combatSpeed(o.combat))
			end
		end
	end
	
	nl()
	nnl(("%-32s"):format(strings[1]))
	nl(makelabel("Life", ("    %d/%d"):format(game.player.life, game.player.max_life)))
	
	nnl(("%-32s"):format(strings[2]))
	if game.player:knowTalent(game.player.T_STAMINA_POOL) then
		nl(makelabel("Stamina", ("    %d/%d"):format(game.player:getStamina(), game.player.max_stamina)))
	else
	    nl()	
	end
	nnl(("%-32s"):format(strings[3]))
	if game.player:knowTalent(game.player.T_MANA_POOL) then
		nl(makelabel("Mana", ("    %d/%d"):format(game.player:getMana(), game.player.max_mana)))
	else
		nl()
	end
	nnl(("%-32s"):format(strings[4]))
	if game.player:knowTalent(game.player.T_SOUL_POOL) then
		nl(makelabel("Soul", ("    %d/%d"):format(game.player:getSoul(), game.player.max_soul)))
    else
		nl()
	end
	nnl(("%-32s"):format(strings[5]))
	if game.player:knowTalent(game.player.T_EQUILIBRIUM_POOL) then
		nl((makelabel("Equi", ("    %d/%d"):format(game.player:getEquilibrium()))))
	else
		nl()	
	end
			
	-- All wpeaons in off hands
	-- Offhand atatcks are with a damage penality, taht can be reduced by talents
	if self.actor:getInven(self.actor.INVEN_OFFHAND) then
		local offmult = (mult or 1) / 2
		if self.actor:knowTalent(Talents.T_DUAL_WEAPON_TRAINING) then
			offmult = (mult or 1) / (2 - (self.actor:getTalentLevel(Talents.T_DUAL_WEAPON_TRAINING) / 6))
		end
		for i, o in ipairs(self.actor:getInven(self.actor.INVEN_OFFHAND)) do
			if o.combat then
				nl()
				nl(("Attack (Off Hand): %3d"):format(game.player:combatAttack(o.combat)))
				nl(("Damage (Off Hand): %3d"):format(game.player:combatDamage(o.combat) * offmult))
				nl(("APR    (Off Hand): %3d"):format(game.player:combatAPR(o.combat)))
				nl(("Crit   (Off Hand): %3d%%"):format(game.player:combatCrit(o.combat)))
				nl(("Speed  (Off Hand): %0.2f"):format(game.player:combatSpeed(o.combat)))
			end
		end
	end
	nl()
	nnl(("%-32s"):format(makelabel("Fatigue", game.player.fatigue .. "%")))
	nl(("Spellpower:  %3d"):format(game.player:combatSpellpower()))
	nnl(("%-32s"):format(makelabel("Armor", game.player:combatArmor() .. "")))
	nl(("Spell Crit:  %3d%%"):format(game.player:combatSpellCrit()))
	nnl(("%-32s"):format(makelabel("Class", game.player:combatDefense() .. "")))
	nl(("Spell Speed: %3d"):format(game.player:combatSpellSpeed()))
	nnl(("%-32s"):format(makelabel("Class", game.player:combatDefenseRanged() .. "")))
	nl()
	nl()
	for i, t in ipairs(DamageType.dam_def) do
		if self.actor.inc_damage[DamageType[t.type]] and self.actor.inc_damage[DamageType[t.type]] ~= 0 then
			nl(("%s damage: %3d%%"):format(t.name:capitalize(), self.actor.inc_damage[DamageType[t.type]]))
		end
	end	
	nl()
	nl(("Physical Resist: %3d"):format(game.player:combatPhysicalResist()))
	nl(("Spell Resist:    %3d"):format(game.player:combatSpellResist()))
	nl(("Mental Resist:   %3d"):format(game.player:combatMentalResist()))
	nl()
	for i, t in ipairs(DamageType.dam_def) do
		if self.actor.resists[DamageType[t.type]] and self.actor.resists[DamageType[t.type]] ~= 0 then
			nl(("%s Resist: %3d%%"):format(t.name:capitalize(), self.actor.resists[DamageType[t.type]]))
		end
	end
	nl()
	local most_kill, most_kill_max = "none", 0
	local total_kill = 0
	for name, nb in pairs(game.player.all_kills or {}) do
		if nb > most_kill_max then most_kill_max = nb most_kill = name end
		total_kill = total_kill + nb
	end
	nl(("Number of NPC killed: %s"):format(total_kill))
	nl(("Most killed NPC: %s (%d)"):format(most_kill, most_kill_max))
	
	--All Equipment
	nl()
	nl("  [Character Equipment]")
	nl()
	local index = 0
	for inven_id =  1, #self.actor.inven_def do
		if self.actor.inven[inven_id] and self.actor.inven_def[inven_id].is_worn then			
			nl((" %s"):format(self.actor.inven_def[inven_id].name))
			for item, o in ipairs(self.actor.inven[inven_id]) do
				if not self.filter or self.filter(o) then
					local char = string.char(string.byte('a') + index)
					nl(("%s) %s"):format(char, o:getName{force_id=true}))					
					index = index + 1
				end
			end
		end
	end
	
	nl()
	nl("  [Character Inventory]")
	nl()
	for item, o in ipairs(self.actor:getInven("INVEN")) do
		if not self.filter or self.filter(o) then
			local char = string.char(string.byte('a') + item - 1)
			nl(("%s) %s"):format(char, o:getName{force_id=true}))			
		end
	end
	fff:close()
	Dialog:simplePopup("Character dump complete", "File: "..fs.getRealPath(file))
end


