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.

User:Fleeting Frames/UnsuspendSelectedBuilding

From Dwarf Fortress Wiki
< User:Fleeting Frames
Revision as of 00:52, 4 July 2019 by Fleeting Frames (talk | contribs) (pre to source lang lua)
Jump to navigation Jump to search

Save as UnsuspendSelectedBuilding.lua into /hack/scripts folder.

--auto-unsuspend selected building.

local bld = dfhack.gui.getSelectedBuilding(true)
utils = require 'utils';
if not bld.construction_stage then qerror("Building " .. utils.getBuildingName(bld) .. " isn't constructed in stages.") end


local function repeatfunction(rfunc)
	--repeats a function every 10 frames, until the function returns false or nil
	local myrfunc
	
	myrfunc = function ()
	  timeoutid = dfhack.timeout(10, "ticks", function(options)
		if rfunc() then myrfunc() end
	   end)
	end
	
	myrfunc()
end

function AnnounceStart(bld)
		dfhack.gui.makeAnnouncement(
			df.announcement_type.JOB_OVERWRITTEN,
			{D_DISPLAY = true},
			xyz2pos(bld.centerx,bld.centery, bld.z),
			"Started unsuspending " .. utils.getBuildingName(bld) .. ".",
			COLOR_GREEN,
			true)
end

function AnnounceEnd(bld)
		dfhack.gui.makeAnnouncement(
			df.announcement_type.QUOTA_FILLED,
			{D_DISPLAY = true},
			xyz2pos(bld.centerx,bld.centery, bld.z),
			"Completed building " .. utils.getBuildingName(bld) .. ".",
			COLOR_GREEN,
			true)
end

function getConstructionJob(building)
	for _, attachedjob in ipairs(building.jobs) do
		if attachedjob.job_type == 68 then
			return attachedjob
		end
	end
end

watchedbuildings = watchedbuildings or {}

if (bld and not watchedbuildings[tostring(bld)]) then
	if bld.flags.exists == false then
		local bldjob=getConstructionJob(bld)
		if bldjob then
			local myBldStr=tostring(bld)
			if(watchedbuildings[myBldStr] ~= false) then --don't set repeatfunc when there's already one
				repeatfunction(
					function()
						if(watchedbuildings[myBldStr]) then 
							local bld = df.building.find(watchedbuildings[myBldStr])
							local bldjob = bld and getConstructionJob(bld)
							if bld and bldjob then
								bldjob.flags.suspend=false
								if bld.flags.exists == false then 
									return true
								end
							end
						end
						watchedbuildings[tostring(bld)] = nil --if didn't return, end is at hand
						AnnounceEnd(bld) 
					end
				)
			end
			watchedbuildings[tostring(bld)]=bld.id
			AnnounceStart(bld)
		end
	end
elseif (bld and watchedbuildings[tostring(bld)]) then
	watchedbuildings[tostring(bld)] = false
	dfhack.gui.makeAnnouncement(
		df.announcement_type.JOB_OVERWRITTEN,
		{D_DISPLAY = true},
		xyz2pos(bld.centerx,bld.centery, bld.z),
		"Stopped unsuspending " .. utils.getBuildingName(bld) .. ".",
	COLOR_GREEN,
	true)
end