v50 Steam/Premium information for editors
  • v50 information can now be added to pages in the main namespace. v0.47 information can still be found in the DF2014 namespace. See here for more details on the new versioning policy.
  • Use this page to report any issues related to the migration.
This notice may be cached—the current version can be found here.

Difference between revisions of "User:Fleeting Frames/itemage"

From Dwarf Fortress Wiki
Jump to navigation Jump to search
(Initial writeup)
 
m (parsing)
 
(One intermediate revision by the same user not shown)
Line 8: Line 8:
 
pcall(function() Widget_itemage = dfhack.script_environment('itemage').Widget_itemage end)
 
pcall(function() Widget_itemage = dfhack.script_environment('itemage').Widget_itemage end)
 
</source>
 
</source>
into /hack/lua/plugins/dwarfmonitor.lua on an empty line somewhere before the function load_config line near the end.
+
into /hack/lua/plugins/dwarfmonitor.lua on an empty line somewhere before the return _ENV line on newline near the very end.  
 
2. Add  
 
2. Add  
 
<source lang="javascript">
 
<source lang="javascript">
Line 46: Line 46:
 
local args = {...}
 
local args = {...}
 
local item = nil
 
local item = nil
if #args>0 and type(args[1])=="Number" then  
+
if #args>0 and type(tonumber(args[1]))=="number" then  
item = df.item.find(args[1])
+
item = df.item.find(tonumber(args[1]))
 
else
 
else
 
item = dfhack.gui.getSelectedItem(true)
 
item = dfhack.gui.getSelectedItem(true)

Latest revision as of 20:18, 4 June 2021

Tested in 43.03 only at present. Usage as console command requires only adding the bottom code chunk on this page into itemage.lua file as text, then selecting an item and typing itemage into console or typing itemage itemid into console (where itemid is number given by item's id).

Usage as dwarfmonitor plugin needs these additional steps at present:

1. Add the line

pcall(function() Widget_itemage = dfhack.script_environment('itemage').Widget_itemage end)

into /hack/lua/plugins/dwarfmonitor.lua on an empty line somewhere before the return _ENV line on newline near the very end. 2. Add

		,{
		"memory": true,
		"type": "itemage",
		"x": -17,
		"y": 2
	}

Into dfhack-config/dwarfmonitor.json after the last widget.

Either way, you need the below in itemage.lua to have it work:

--Prints the selected item age or provided age
--Or provides a widget for dwarfmonitor

--TODO toggle enable/disable on click while displaying

--[[

		{
		"memory": false,
		"type": "itemage",
		"x": -17,
		"y": 2
	},

]]

	base_colour = COLOR_CYAN
	dump_colour = COLOR_MAGENTA
	melt_colour = COLOR_RED
	TSK_bonus = 8

local args = {...}
local item = nil
if #args>0 and type(tonumber(args[1]))=="number" then 
	item = df.item.find(tonumber(args[1]))
	else
	item = dfhack.gui.getSelectedItem(true)
end

if not moduleMode and not item then qerror("itemage.lua needs an item selected or provided by id!") end

function finditemage(age)
	local result = ""
	if(age>40320) then
	result = result .. math.floor(age/40320) .. " years, " .. finditemage(age%40320)
	elseif(age>3360) then
	result = result .. math.floor(age/3360) .. " months, " .. finditemage(age%3360)
	else
	result = result .. math.floor(age/120) .. " days and " .. (12*(age%120)) .. " minutes old."
	end
	return result
end

function finditemcreation(age)
local creationyear = df.global.cur_year - age/40320 + df.global.cur_year_tick/403200
local creationmonth = (creationyear%1)*12
local creationday = (creationmonth%1)*28
local creationminute = (creationday%1)*1440
return math.floor(creationyear) .. "y" .. math.floor(creationmonth+1) .. "m" .. math.floor(creationday+1) .. "d" .. math.floor(creationminute) .. "m"
end


local gps = df.global.gps
local gui = require 'gui'
local dwarfmonitor = require('plugins.dwarfmonitor')

local debugverbose = false

local function printd(...)
	if debugverbose then print(...) end
end

pstringt = {}
local spos

Widget_itemage = defclass(Widget_itemage, dwarfmonitor.Widget)

function cursormismatch(update)
	printd("checking mismatch", update)
	if not spos then spos = copyall(df.global.cursor) end
	if not same_xyz(spos,df.global.cursor) then 
		if update then spos = copyall(df.global.cursor) end
		return true
	end
end

EmptyTable = {str="", colour=0}

function Widget_itemage:update()
	if (dfhack.gui.getCurFocus():match("dwarfmode/LookAround/Item")) then
	--used to be == Lookaround, meant it wasn't used on Lookaround/Item but also meant it didn't reset on brief look away
		printd("updating")
		if cursormismatch(true) then
			--local count = 0
			for i,v in pairs(pstringt) do
				pstringt[i].str=EmptyTable.str
				pstringt[i].colour=EmptyTable.colour
				--count = count+1 
			end 
			--if count > 0 then pstringt = {} end
		end 
	end
end

function Widget_itemage:get_width()	return 16 end

local function getItemTextColour(item)
	return ((item.flags.dump and dump_colour or (item.flags.melt and melt_colour or base_colour))+(item.flags.in_job and TSK_bonus or 0))
end



function Widget_itemage:render_body(p)
	if (dfhack.gui.getCurFocus():match("dwarfmode/LookAround/Item")) and df.global.enabler.mouse_lbut == 0 then
		item = dfhack.gui.getSelectedItem(true)
		printd("rendering body at ", self:get_pos())
		if item then printd("Found item ", item.id, " age is " .. finditemcreation(item.age), "colour is " .. getItemTextColour(item), "memory is ", self.opts.memory) end
		if self.opts.memory and not cursormismatch() then
			if item then 
				if not pstringt[df.global.ui_look_cursor] then 
					pstringt[df.global.ui_look_cursor] = {str=finditemcreation(item.age), colour=getItemTextColour(item)}
				else 
					pstringt[df.global.ui_look_cursor].str = finditemcreation(item.age)
					pstringt[df.global.ui_look_cursor].colour = getItemTextColour(item)
				end
			end
			for i,v in pairs(pstringt) do
				if v and v.str and math.floor(df.global.ui_look_cursor / (gps.dimy-9)) == math.floor(i/(gps.dimy-9)) then
					p:seek(0,(i)%(gps.dimy-9)):pen(v.colour):string(v.str)
					printd("Should have printed string ", v.str)
				end
			end
		else
			if item then p:seek(0,(df.global.ui_look_cursor)%(gps.dimy-9)):pen(getItemTextColour(item)):string(finditemcreation(item.age)) end
		end
	end
end

function Widget_itemage:render()
    if dwarfmonitor.monitor_state(self.opts.type) == false then
        return
    end
    self:update()
    local x, y = self:get_pos()
    local p = gui.Painter.new_xy(x, y, gps.dimx - 1, gps.dimy-y)
    self:render_body(p)
end

if item and not moduleMode then print(finditemcreation(item.age), "Item is " .. finditemage(item.age)) end