Skip to content

Commit a76a39a

Browse files
authored
Merge pull request #1386 from Bumber64/position
Update position.lua
2 parents 70c54c4 + 8555938 commit a76a39a

3 files changed

Lines changed: 103 additions & 62 deletions

File tree

changelog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ Template for new versions:
3535
- `advtools`: new ``advtools.fastcombat`` overlay (enabled by default) allows you to skip combat animations and the announcement "More" button by mashing the movement keys
3636

3737
## Fixes
38+
- `position`: support for adv mode look cursor
3839

3940
## Misc Improvements
4041
- `hide-tutorials`: handle tutorial popups for adventure mode
4142
- `hide-tutorials`: new ``reset`` command that will re-enable popups in the current game (in case you hid them all and now want them back)
4243
- `gui/notify`: moody dwarf notification turns red when they can't reach workshop or items
4344
- `gui/confirm`: in the delete manager order confirmation dialog, show a description of which order you have selected to delete
45+
- `position`: display both adventurer and site pos simultaneously. Display map block pos+offset of selected tile.
4446

4547
## Removed
4648

docs/position.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ position
88
This tool reports the current date, clock time, month, season, and historical
99
era. It also reports the keyboard cursor position (or just the z-level if no
1010
active cursor), window size, and mouse location on the screen. If a site is
11-
loaded, it prints the world coordinates of the site. If not, it prints the world
11+
loaded, it prints the world coordinates of the site. It also prints the world
1212
coordinates of the adventurer (if applicable).
1313

1414
Can also be used to copy the current keyboard cursor position for later use.

position.lua

Lines changed: 100 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1+
-- Report cursor and mouse position, along with other info.
2+
--@ module = true
13

2-
local cursor = df.global.cursor
3-
local args = {...}
4-
if #args > 0 then --Copy keyboard cursor to clipboard
5-
if #args > 1 then
6-
qerror('Too many arguments!')
7-
elseif args[1] ~= '-c' and args[1] ~= '--copy' then
8-
qerror('Invalid argument "'..args[1]..'"!')
9-
elseif cursor.x < 0 then
10-
qerror('No keyboard cursor!')
4+
local argparse = require('argparse')
5+
local guidm = require('gui.dwarfmode')
6+
7+
local function parse_args(args)
8+
local opts = {}
9+
local positionals = argparse.processArgsGetopt(args,
10+
{
11+
{'c', 'copy', handler=function() opts.copy = true end},
12+
})
13+
14+
if #positionals > 0 then
15+
qerror('Too many positionals!')
1116
end
1217

13-
dfhack.internal.setClipboardTextCp437(('%d,%d,%d'):format(cursor.x, cursor.y, cursor.z))
14-
return
18+
return opts
1519
end
1620

17-
local months = {
21+
local months =
22+
{
1823
'Granite, in early Spring.',
1924
'Slate, in mid Spring.',
2025
'Felsite, in late Spring.',
@@ -29,68 +34,102 @@ local months = {
2934
'Obsidian, in late Winter.',
3035
}
3136

32-
--Fortress mode counts 1200 ticks per day and 403200 per year
33-
--Adventurer mode counts 86400 ticks to a day and 29030400 ticks per year
34-
--Twelve months per year, 28 days to every month, 336 days per year
37+
function print_time_info()
38+
--Fortress mode counts 1200 ticks per day and 403200 per year
39+
--Adventurer mode counts 86400 ticks to a day and 29030400 ticks per year
40+
--Twelve months per year, 28 days to every month, 336 days per year
41+
local julian_day = df.global.cur_year_tick // 1200 + 1
42+
local month = julian_day // 28 + 1 --days and months are 1-indexed
43+
local day = julian_day % 28
3544

36-
local julian_day = df.global.cur_year_tick // 1200 + 1
37-
local month = julian_day // 28 + 1 --days and months are 1-indexed
38-
local day = julian_day % 28
45+
local time_of_day = df.global.cur_year_tick_advmode // 336
46+
local second = time_of_day % 60
47+
local minute = time_of_day // 60 % 60
48+
local hour = time_of_day // 3600 % 24
3949

40-
local time_of_day = df.global.cur_year_tick_advmode // 336
41-
local second = time_of_day % 60
42-
local minute = time_of_day // 60 % 60
43-
local hour = time_of_day // 3600 % 24
50+
print('Time:')
51+
print((' The time is %02d:%02d:%02d'):format(hour, minute, second))
52+
print((' The date is %03d-%02d-%02d'):format(df.global.cur_year, month, day))
53+
print(' It is the month of '..months[month])
4454

45-
print('Time:')
46-
print((' The time is %02d:%02d:%02d'):format(hour, minute, second))
47-
print((' The date is %03d-%02d-%02d'):format(df.global.cur_year, month, day))
48-
print(' It is the month of '..months[month])
55+
local eras = df.global.world.history.eras
56+
if #eras > 0 then
57+
print(' It is the '..eras[#eras-1].title.name..'.')
58+
end
59+
end
4960

50-
local eras = df.global.world.history.eras
51-
if #eras > 0 then
52-
print(' It is the '..eras[#eras-1].title.name..'.')
61+
function get_adv_region_pos() --Regional coords
62+
if not dfhack.world.getAdventurer() then --Army exists when unit doesn't
63+
local army = df.army.find(df.global.adventure.player_army_id)
64+
if army then
65+
return army.pos.x//48, army.pos.y//48
66+
end
67+
end
68+
local wd = df.global.world.world_data
69+
return wd.midmap_data.adv_region_x, wd.midmap_data.adv_region_y
5370
end
5471

55-
print('Place:')
56-
print(' The z-level is z='..df.global.window_z)
72+
local function print_world_info()
73+
local wd = df.global.world.world_data
74+
local site = dfhack.world.getCurrentSite()
75+
if site then
76+
print((' The current site is at x=%d, y=%d on the %dx%d world map.'):
77+
format(site.pos.x, site.pos.y, wd.world_width, wd.world_height))
78+
end
5779

58-
if cursor.x < 0 then
59-
print(' The keyboard cursor is inactive.')
60-
else
61-
print(' The keyboard cursor is at x='..cursor.x..', y='..cursor.y)
80+
if dfhack.world.isAdventureMode() then
81+
local x, y = get_adv_region_pos()
82+
print((' The adventurer is at x=%d, y=%d on the %dx%d world map.'):
83+
format(x, y, wd.world_width, wd.world_height))
84+
end
6285
end
6386

64-
local x, y = dfhack.screen.getWindowSize()
65-
print(' The window is '..x..' tiles wide and '..y..' tiles high.')
87+
function print_place_info(cursor)
88+
print('Place:')
89+
print(' The z-level is z='..df.global.window_z)
6690

67-
x, y = dfhack.screen.getMousePos()
68-
if x then
69-
print(' The mouse is at x='..x..', y='..y..' within the window.')
70-
local pos = dfhack.gui.getMousePos()
71-
if pos then
72-
print(' The mouse is over map tile x='..pos.x..', y='..pos.y)
91+
if cursor then
92+
local x, y = cursor.x, cursor.y
93+
print((' The keyboard cursor is at x=%d, y=%d (%d+%d, %d+%d)'):
94+
format(x, y, x//16*16, x%16, y//16*16, y%16))
95+
else
96+
print(' The keyboard cursor is inactive.')
7397
end
74-
else
75-
print(' The mouse is not in the DF window.')
76-
end
7798

78-
local wd = df.global.world.world_data
79-
local site = dfhack.world.getCurrentSite()
80-
if site then
81-
print((' The current site is at x=%d, y=%d on the %dx%d world map.'):
82-
format(site.pos.x, site.pos.y, wd.world_width, wd.world_height))
83-
elseif dfhack.world.isAdventureMode() then
84-
x, y = -1, -1
85-
for _,army in ipairs(df.global.world.armies.all) do
86-
if army.flags.player then
87-
x, y = army.pos.x // 48, army.pos.y // 48
88-
break
99+
local x, y = dfhack.screen.getWindowSize()
100+
print(' The window is '..x..' tiles wide and '..y..' tiles high.')
101+
102+
x, y = dfhack.screen.getMousePos()
103+
if x then
104+
print(' The mouse is at x='..x..', y='..y..' within the window.')
105+
local pos = dfhack.gui.getMousePos()
106+
if pos then
107+
print(' The mouse is over map tile x='..pos.x..', y='..pos.y)
89108
end
109+
else
110+
print(' The mouse is not in the DF window.')
90111
end
91-
if x < 0 then
92-
x, y = wd.midmap_data.adv_region_x, wd.midmap_data.adv_region_y
112+
113+
print_world_info()
114+
end
115+
116+
if dfhack_flags.module then
117+
return
118+
end
119+
120+
function main(opts)
121+
local cursor = guidm.getCursorPos()
122+
123+
if opts.copy then --Copy keyboard cursor to clipboard
124+
if not cursor then
125+
qerror('No keyboard cursor!')
126+
end
127+
dfhack.internal.setClipboardTextCp437(('%d,%d,%d'):format(cursor.x, cursor.y, cursor.z))
128+
return --Don't print anything
93129
end
94-
print((' The adventurer is at x=%d, y=%d on the %dx%d world map.'):
95-
format(x, y, wd.world_width, wd.world_height))
130+
131+
print_time_info()
132+
print_place_info(cursor)
96133
end
134+
135+
main(parse_args({...}))

0 commit comments

Comments
 (0)