Fix a few view-item-info issues

* Make descriptions work
* Allow the script to be enabled/disabled
* Avoid appending an empty line to script output and add a DFHack
  label
develop
lethosor 2015-04-18 18:57:21 -04:00
parent 1a23ce136a
commit e3b7d61bc1
2 changed files with 29 additions and 19 deletions

@ -230,7 +230,7 @@ soundsense-season
log-region log-region
# add information to item viewscreens # add information to item viewscreens
view-item-info view-item-info enable
####################################################### #######################################################
# Apply binary patches at runtime # # Apply binary patches at runtime #

@ -1,6 +1,7 @@
-- Extended Item Viewscreens -- Extended Item Viewscreens
-- Shows information on material properties, weapon or armour stats, and more. -- Shows information on material properties, weapon or armour stats, and more.
-- By PeridexisErrant, adapted from nb_item_info by Raidau -- By PeridexisErrant, adapted from nb_item_info by Raidau
--@ enable = true
local help = [[Extended Item Viewscreen local help = [[Extended Item Viewscreen
@ -8,20 +9,33 @@ A script to extend the item or unit viewscreen with additional information
including properties such as material info, weapon and attack properties, including properties such as material info, weapon and attack properties,
armor thickness and coverage, and more - including custom item descriptions.]] armor thickness and coverage, and more - including custom item descriptions.]]
function isInList(list, item) vi_label = 'More information (DFHack):'
function isInList(list, item, helper)
if not helper then
helper = function(v) return v end
end
for k,v in pairs (list) do for k,v in pairs (list) do
if item == v then if item == helper(v) then
return true return true
end end
end end
end end
if dfhack_flags and dfhack_flags.enable then
enabled = dfhack_flags.enable_state
end
local args = {...} local args = {...}
local lastframe = df.global.enabler.frame_last local lastframe = df.global.enabler.frame_last
if isInList(args, "help") or isInList(args, "?") then if isInList(args, "help") or isInList(args, "?") then
print(help) return print(help)
return
elseif isInList(args, "enable") then
enabled = true
elseif isInList(args, "disable") then
enabled = false
end end
print ("view-item-info enabled")
function append (list, str, indent) function append (list, str, indent)
local str = str or " " local str = str or " "
@ -89,7 +103,6 @@ function GetMatPropertiesStringList (item)
local i_strain = mat.strength.strain_at_yield.IMPACT local i_strain = mat.strength.strain_at_yield.IMPACT
append(list, "elasticity: "..i_strain.." ("..GetStrainDescription(i_strain)..")", 2) append(list, "elasticity: "..i_strain.." ("..GetStrainDescription(i_strain)..")", 2)
end end
append(list)
return list return list
end end
@ -100,7 +113,6 @@ function GetArmorPropertiesStringList (item)
append(list,"Thickness: "..item.subtype.props.layer_size,1) append(list,"Thickness: "..item.subtype.props.layer_size,1)
append(list,"Coverage: "..item.subtype.props.coverage.."%",1) append(list,"Coverage: "..item.subtype.props.coverage.."%",1)
append(list,"Fit for "..df.creature_raw.find(item.maker_race).name[0],1) append(list,"Fit for "..df.creature_raw.find(item.maker_race).name[0],1)
append(list)
return list return list
end end
@ -110,14 +122,12 @@ function GetShieldPropertiesStringList (item)
append(list,"Shield properties:") append(list,"Shield properties:")
append(list,"Base block chance: "..item.subtype.blockchance,1) append(list,"Base block chance: "..item.subtype.blockchance,1)
append(list,"Fit for "..df.creature_raw.find(item.maker_race).name[0],1) append(list,"Fit for "..df.creature_raw.find(item.maker_race).name[0],1)
append(list)
return list return list
end end
function GetWeaponPropertiesStringList (item) function GetWeaponPropertiesStringList (item)
local mat = dfhack.matinfo.decode(item).material local mat = dfhack.matinfo.decode(item).material
local list = {} local list = {}
append(list)
if item._type == df.item_toolst and #item.subtype.attacks < 1 then if item._type == df.item_toolst and #item.subtype.attacks < 1 then
return list return list
end end
@ -163,14 +173,12 @@ function GetWeaponPropertiesStringList (item)
end end
append(list,"Prepare "..attack.prepare.." / recover "..attack.recover,3) append(list,"Prepare "..attack.prepare.." / recover "..attack.recover,3)
end end
append(list)
return list return list
end end
function GetAmmoPropertiesStringList (item) function GetAmmoPropertiesStringList (item)
local mat = dfhack.matinfo.decode(item).material local mat = dfhack.matinfo.decode(item).material
local list = {} local list = {}
append(list)
if item._type == df.item_toolst and #item.subtype.attacks < 1 then if item._type == df.item_toolst and #item.subtype.attacks < 1 then
return list return list
end end
@ -202,7 +210,6 @@ function GetAmmoPropertiesStringList (item)
end end
append(list,"Prepare "..attack.prepare.." / recover "..attack.recover,3) append(list,"Prepare "..attack.prepare.." / recover "..attack.recover,3)
end end
append(list)
return list return list
end end
@ -273,12 +280,10 @@ function GetFoodPropertiesStringList (item)
append(list,edible_string(mat)) append(list,edible_string(mat))
if item._type == df.item_foodst then if item._type == df.item_foodst then
append(list,"This is prepared meal") append(list,"This is prepared meal")
append(list)
return list return list
end end
if mat == dfhack.matinfo.find ("WATER") then if mat == dfhack.matinfo.find ("WATER") then
append(list,"Water is drinkable") append(list,"Water is drinkable")
append(list)
return list return list
end end
add_lines_to_list(list, get_plant_reaction_products(mat)) add_lines_to_list(list, get_plant_reaction_products(mat))
@ -297,7 +302,6 @@ function GetFoodPropertiesStringList (item)
end end
end end
end end
append(list)
return list return list
end end
@ -337,10 +341,10 @@ function get_custom_item_desc (item)
ID = item.subtype.id end ID = item.subtype.id end
if not ID then return nil end if not ID then return nil end
if dfhack.findScript("item-descriptions") then if dfhack.findScript("item-descriptions") then
local desc = dfhack.script_environment("item-descriptions").descriptions.ID desc = dfhack.script_environment("item-descriptions").descriptions[ID]
end end
if dfhack.findScript("more-item-descriptions") then if dfhack.findScript("more-item-descriptions") then
local desc = dfhack.script_environment("more-item-descriptions").descriptions.ID or desc desc = dfhack.script_environment("more-item-descriptions").descriptions[ID] or desc
end end
if desc then add_lines_to_list(desc, {""}) end if desc then add_lines_to_list(desc, {""}) end
return desc return desc
@ -358,19 +362,25 @@ function AddUsesString (viewscreen,line,indent)
end end
function dfhack.onStateChange.item_info (code) function dfhack.onStateChange.item_info (code)
if not enabled then return end
if code == SC_VIEWSCREEN_CHANGED and dfhack.isWorldLoaded() then if code == SC_VIEWSCREEN_CHANGED and dfhack.isWorldLoaded() then
standard = dfhack.matinfo.find("INORGANIC:IRON").material standard = dfhack.matinfo.find("INORGANIC:IRON").material
if not standard then return end if not standard then return end
local scr = dfhack.gui.getCurViewscreen() local scr = dfhack.gui.getCurViewscreen()
if scr._type == df.viewscreen_itemst then if scr._type == df.viewscreen_itemst then
if #scr.entry_string > 0 and scr.entry_string[#scr.entry_string-1].value == " " then if isInList(scr.entry_string, vi_label, function(v) return v.value end) then
return return
end end
if #scr.entry_string > 0 then
AddUsesString(scr, '')
end
AddUsesString(scr, vi_label)
AddUsesString(scr, '')
local description = get_custom_item_desc (scr.item) or "" local description = get_custom_item_desc (scr.item) or ""
for i = 1, #description do for i = 1, #description do
AddUsesString(scr,description[i]) AddUsesString(scr,description[i])
end end
local all_lines = get_all_uses_strings (scr.item) local all_lines = get_all_uses_strings(scr.item)
for i = 1, #all_lines do for i = 1, #all_lines do
AddUsesString(scr,all_lines[i][1],all_lines[i][2]) AddUsesString(scr,all_lines[i][1],all_lines[i][2])
end end