Skip to content

Commit a1cc6af

Browse files
You can now run gui/armies to see armies on the world map
1 parent 32bf2af commit a1cc6af

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

gui/armies.lua

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
2+
local gui = require('gui')
3+
local widgets = require('gui.widgets')
4+
local utils = require('utils')
5+
local repeatUtil = require('repeat-util')
6+
7+
local visible_armies = {}
8+
9+
local timerId = 'testoverlay'
10+
11+
local function getMapPortTopLeftXY()
12+
local player_army = df.army.find(df.global.adventure.player_army_id)
13+
if not player_army then return 0,0 end
14+
15+
local main_map_port = df.global.gps.main_map_port
16+
local screen_center_x, screen_center_y = main_map_port.screen_x, main_map_port.screen_y
17+
18+
local px, py = player_army.pos.x, player_army.pos.y
19+
20+
local site_level_zoom = df.global.adventure.site_level_zoom
21+
if site_level_zoom == 0 then
22+
px, py = math.floor(px/3), math.floor(py/3)
23+
end
24+
local top_left_x, top_left_y = px - screen_center_x, py - screen_center_y
25+
26+
return top_left_x, top_left_y
27+
end
28+
29+
local last_tick = 0
30+
local function onTick()
31+
last_tick = df.global.cur_year_tick_advmode
32+
visible_armies = {}
33+
34+
local site_level_zoom = df.global.adventure.site_level_zoom
35+
local top_left_x, top_left_y = getMapPortTopLeftXY()
36+
37+
for _, army in pairs(df.global.world.armies.all) do
38+
if army.flags.player then goto continue end
39+
local x, y = army.pos.x, army.pos.y
40+
if site_level_zoom == 0 then
41+
x, y = math.floor(x/3), math.floor(y/3)
42+
end
43+
local adjusted_x, adjusted_y = x - top_left_x, y - top_left_y
44+
table.insert(visible_armies, {x=adjusted_x, y=adjusted_y})
45+
:: continue ::
46+
end
47+
end
48+
49+
ArmyOverlay = defclass(ArmyOverlay, widgets.Window)
50+
ArmyOverlay.ATTRS {
51+
frame_title='Army Overlay',
52+
frame={b = 4, r = 4, w = 50, h = 12},
53+
visible=true
54+
}
55+
56+
function ArmyOverlay:init()
57+
self:addviews{
58+
widgets.HotkeyLabel{
59+
frame={l=0, t=0},
60+
label='Force Update',
61+
key='CUSTOM_U',
62+
-- auto_width=true,
63+
on_activate=function()
64+
onTick()
65+
end,
66+
},
67+
widgets.HotkeyLabel{
68+
frame={l=0, t=2},
69+
label='Teleport Self to Cursor',
70+
key='CUSTOM_T',
71+
on_activate=function()
72+
local player_army = df.army.find(df.global.adventure.player_army_id)
73+
if not player_army then return end
74+
local mouse_x, mouse_y = math.floor(df.global.gps.precise_mouse_x / 16), math.floor(df.global.gps.precise_mouse_y / 16)
75+
local site_level_zoom = df.global.adventure.site_level_zoom
76+
77+
local top_left_x, top_left_y = getMapPortTopLeftXY()
78+
local x, y = mouse_x + top_left_x, mouse_y + top_left_y
79+
if site_level_zoom == 0 then
80+
x, y = x*3, y*3
81+
end
82+
player_army.pos.x = x
83+
player_army.pos.y = y
84+
onTick()
85+
end,
86+
},
87+
}
88+
end
89+
90+
local army_pen = {ch='*', fg=COLOR_GRAY, tile=dfhack.screen.findGraphicsTile('WORLD_MAP_ARMIES', 2, 0)}
91+
function ArmyOverlay:onRenderFrame(painter, rect)
92+
ArmyOverlay.super.onRenderFrame(self, painter, rect)
93+
if last_tick ~= df.global.cur_year_tick_advmode then
94+
onTick()
95+
end
96+
for _, army in pairs(visible_armies) do
97+
dfhack.screen.paintTileMapPort(army_pen, army.x, army.y, army_pen.ch, army_pen.tile)
98+
end
99+
end
100+
101+
ArmyOverlayScreen = defclass(ArmyOverlayScreen, gui.ZScreen)
102+
ArmyOverlayScreen.ATTRS {
103+
focus_path = 'ArmyOverlay',
104+
pass_movement_keys = true,
105+
pass_mouse_clicks = true,
106+
}
107+
108+
function ArmyOverlayScreen:init()
109+
self:addviews{ArmyOverlay{}}
110+
onTick()
111+
end
112+
113+
function ArmyOverlayScreen:onDismiss()
114+
view = nil
115+
end
116+
117+
view = view and view:raise() or ArmyOverlayScreen{}:show()

0 commit comments

Comments
 (0)