Lua code:
-- bookmark script v1.00
-- Set and clears persistent bookmarks on the fort map.
-- Enter "bookmark help" for a list of reserved names.
local args = {...}
local bookmarks = dfhack.persistent.get_all("BOOKMARK",true)
local calibration = dfhack.persistent.get("_BOOKMARK")
if args[1] == "help" then
print([[bookmark.lua
Enter "bookmark foo" to create a bookmark named "foo" at the current location.
Any one-word string may be used except the following reserved words:
"bookmark help" prints this command summary.
"bookmark calibrate" sets the cursor location as the center of the screen.
"bookmark list" prints a list of existing bookmarks.
"bookmark drop foo" erases the bookmark named "foo".
"bookmark clear all" erases all current bookmarks, note the space between "clear" and "all".
]])
elseif args[1] == "list" then
if bookmarks then
for _, bookmark in ipairs(bookmarks) do
print(bookmark.value .. ": " .. bookmark.ints[1] .. " " .. bookmark.ints[2] .. " " .. bookmark.ints[3])
end
else
print("No bookmarks defined.")
end
elseif args[1] == "clear" and args[2] == "all" then
if bookmarks then
local count = #bookmarks
for _, bookmark in ipairs(bookmarks) do
bookmark:delete()
end
print("Deleted " .. count .. " bookmarks.")
else
print("No bookmarks are defined, so no action taken.")
end
elseif args[1] == "calibrate" and not args[2] then
local pos = {}
local offset_x
local offset_y
pos = copyall(df.global.cursor)
if pos.x == -30000 then
print("Cursor is not present. Cannot calibrate.")
else
offset_x = pos.x - df.global.window_x
offset_y = pos.y - df.global.window_y
dfhack.persistent.save({key="_BOOKMARK",value="Calibration data",ints={offset_x,offset_y}})
print("Calibration complete.")
end
elseif args[1] == "drop" and args[2] then
if bookmarks then
local del = false
for _, bookmark in ipairs(bookmarks) do
if bookmark.value == args[2] then
bookmark:delete()
print("Bookmark " .. args[2] .. " deleted.")
del = true
end
end
if del == false then print("Bookmark " .. args[2] .. " not defined, so no action taken.") end
else
print("No bookmarks are defined, so no action taken.")
end
elseif args[1] and not args[2] then
local pos = {}
pos = copyall(df.global.cursor)
local make_new = true
if pos.x == -30000 then
if calibration then
name = "Center:"
pos["x"] = df.global.window_x + calibration.ints[1]
pos["y"] = df.global.window_y + calibration.ints[2]
pos["z"] = df.global.window_z
else
print([[
Not calibrated. Either use the cursor or "bookmark calibrate" with the cursor in the center of the screen.
]])
end
end
if bookmarks then
for _, bookmark in ipairs(bookmarks) do
if bookmark.value == args[1] then
bookmark.ints[1] = pos.x
bookmark.ints[2] = pos.y
bookmark.ints[3] = pos.z
bookmark:save()
print("Bookmark " .. args[1] .. " updated.")
make_new = false
end
end
if make_new == true then
dfhack.persistent.save({key="BOOKMARK/"..args[1],value=args[1],ints={pos.x,pos.y,pos.z}})
print("Bookmark " .. args[1] .. " created.")
end
else
dfhack.persistent.save({key="BOOKMARK/"..args[1],value=args[1],ints={pos.x,pos.y,pos.z}})
print("Bookmark " .. args[1] .. " created.")
end
else
print([[
Invalid command. Use "bookmark help" for help.
]])
end
-- goto script v1.00
-- Move the screen to a bookmark or a set of XYZ coordinates.
-- Enter "bookmark help" for a list of reserved names.
local args = {...}
local bookmarks = dfhack.persistent.get_all("BOOKMARK",true)
local calibration = dfhack.persistent.get("_BOOKMARK")
if args[1] == "help" then
print([[bookmark.lua
Enter "goto # # #" to move the screen to a set of XYZ coordinates.
Enter "goto foo" to move the screen to be centered on bookmark "foo".
"goto help" prints this command summary.
Note that goto cannot function unless "bookmark calibrate" has been performed.
]])
elseif args[1] and not args[2] then
if bookmarks then
if calibration then
local pos = {}
local xcoord = df.global.cursor.x
for _, bookmark in ipairs(bookmarks) do
if bookmark.value == args[1] then
pos["x"] = bookmark.ints[1]
pos["y"] = bookmark.ints[2]
pos["z"] = bookmark.ints[3]
end
end
if pos.x then
if xcoord ~= -30000 then df.global.cursor.x = pos.x end
df.global.cursor.y = pos.y
df.global.cursor.z = pos.z
df.global.window_x = pos.x - calibration.ints[1]
df.global.window_y = pos.y - calibration.ints[2]
df.global.window_z = pos.z
print("Now at " .. args[1])
else
print("Bookmark " .. args[1] .. " not defined.")
end
else
print([[
Not calibrated. Use "bookmark calibrate" with the cursor in the center of the screen.
]])
end
else
print("No bookmarks defined.")
end
elseif args[1] and args[2] and args[3] then
if calibration then
local xcoord = df.global.cursor.x
if xcoord ~= -30000 then df.global.cursor.x = tonumber(args[1]) end
df.global.cursor.y = tonumber(args[2])
df.global.cursor.z = tonumber(args[3])
df.global.window_x = tonumber(args[1]) - calibration.ints[1]
df.global.window_y = df.global.cursor.y - calibration.ints[2]
df.global.window_z = df.global.cursor.z
print("Now at " .. args[1] .. " " .. args[2] .. " " .. args[3])
else
print([[
Not calibrated. Use "bookmark calibrate" with the cursor in the center of the screen.
]])
end
else
print([[
Invalid command. Use "goto help" for help.
]])
end
-- whereami script v1.00
-- Reports coordinates of the cursor if it is present, or the center of the screen if not.
-- Also lists any bookmarks set for these coordinates.
local bookmarks = dfhack.persistent.get_all("BOOKMARK",true)
local calibration = dfhack.persistent.get("_BOOKMARK")
local pos = {}
pos = copyall(df.global.cursor)
local valid_pos = true
local name = "Cursor:"
if pos.x == -30000 then
if calibration then
name = "Center:"
pos["x"] = df.global.window_x + calibration.ints[1]
pos["y"] = df.global.window_y + calibration.ints[2]
pos["z"] = df.global.window_z
else
print([[
Not calibrated. Either use the cursor or "bookmark calibrate" with the cursor in the center of the screen.
]])
valid_pos = false
end
end
if valid_pos == true then
print(name .. " " .. pos.x .. " " .. pos.y .. " " .. pos.z)
if bookmarks then
for _, bookmark in ipairs(bookmarks) do
if bookmark.ints[1] == pos.x and bookmark.ints[2] == pos.y and bookmark.ints[3] == pos.z then
print("Bookmarked as " .. bookmark.value)
end
end
end
end