-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwaypoints.lua
301 lines (237 loc) · 6.9 KB
/
waypoints.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
function waypoint_find_by_id(id)
return utils.binsearch(df.global.ui.waypoints.points, id, 'id')
end
function route_find_by_id(id)
return utils.binsearch(df.global.ui.waypoints.routes, id, 'id')
end
--luacheck: in=
function waypoints_mode_points()
--todo: use reset_main()
df.global.ui.main.mode = df.ui_sidebar_mode.Default
local ws = dfhack.gui.getCurViewscreen()
gui.simulateInput(ws, K'D_NOTE')
end
--luacheck: in=bool
function waypoints_get_points(full)
local ret = {}
full = istrue(full)
for i,v in ipairs(df.global.ui.waypoints.points) do
local name = pointname(v)
local dx = v.pos.x - df.global.cursor.x
local dy = v.pos.y - df.global.cursor.y
local dz = v.pos.z - df.global.cursor.z
if full then
table.insert(ret, { name, v.id, v.comment, df.global.cursor.x ~= -30000 and { dx,dy,dz } or mp.NIL })
else
table.insert(ret, { name, v.id })
end
end
return ret
end
--luacheck: in=
function waypoints_nearest_point()
if df.global.cursor.x == -30000 then
return nil
end
local dist = 999999
local pt = nil
for i,v in ipairs(df.global.ui.waypoints.points) do
local dx = v.pos.x - df.global.cursor.x
local dy = v.pos.y - df.global.cursor.y
local dz = v.pos.z - df.global.cursor.z
local d = math.abs(dx*dx+dy*dy+dz*dz)
if d < dist then
dist = d
pt = v
end
end
if pt then
local dx = pt.pos.x - df.global.cursor.x
local dy = pt.pos.y - df.global.cursor.y
local dz = pt.pos.z - df.global.cursor.z
return { pointname(pt), pt.id, pt.comment, { dx,dy,dz } }
end
return nil
end
--luacheck: in=
function waypoints_nearest_and_all()
return { waypoints_nearest_point() or mp.NIL, waypoints_get_points(true) }
end
--luacheck: in=number,number,number,string,string
function waypoints_add_point(x, y, z, name, comment)
local pt = df.ui.T_waypoints.T_points:new()
pt.id = df.global.ui.waypoints.next_point_id
pt.name = dfhack.utf2df(name)
pt.comment = comment and dfhack.utf2df(comment) or ''
pt.pos.x = x
pt.pos.y = y
pt.pos.z = z
pt.tile = 33
pt.fg_color = 11
pt.bg_color = 1
df.global.ui.waypoints.points:insert(#df.global.ui.waypoints.points, pt)
df.global.ui.waypoints.next_point_id = df.global.ui.waypoints.next_point_id + 1
end
--luacheck: in=string,string
function waypoints_place_point(name, comment)
local ws = dfhack.gui.getCurViewscreen()
if ws._type ~= df.viewscreen_dwarfmodest then
return
end
if df.global.ui.main.mode ~= df.ui_sidebar_mode.NotesPoints then
return
end
local oldid = df.global.ui.waypoints.next_point_id
gui.simulateInput(ws, K'D_NOTE_PLACE')
-- protection from updating wrong point if a new one wasn't placed
if oldid == df.global.ui.waypoints.next_point_id then
return
end
local pt = df.global.ui.waypoints.points[df.global.ui.waypoints.cur_point_index]
pt.name = dfhack.utf2df(name)
pt.comment = comment and dfhack.utf2df(comment) or ''
pt.tile = 33
pt.fg_color = 11
pt.bg_color = 1
end
-- points may be referenced from squad orders, let's not try deleting them ourselves and use game ui instead
--luacheck: in=number
function waypoints_delete_point(id)
local ws = dfhack.gui.getCurViewscreen()
if ws._type ~= df.viewscreen_dwarfmodest then
return
end
return execute_with_main_mode(df.ui_sidebar_mode.Default, function(ws)
gui.simulateInput(ws, K'D_NOTE')
for i,v in ipairs(df.global.ui.waypoints.points) do
if v.id == id then
df.global.ui.waypoints.cur_point_index = i
gui.simulateInput(ws, K'D_NOTE_DELETE')
break
end
end
end)
end
--luacheck: in=number
function waypoints_zoom_to_point(id)
local pt = waypoint_find_by_id(id)
if not pt then
return
end
recenter_view(pt.pos.x, pt.pos.y, pt.pos.z)
df.global.cursor.x = pt.pos.x
df.global.cursor.y = pt.pos.y
df.global.cursor.z = pt.pos.z
end
--luacheck: in=number,string,string
function waypoints_set_name_comment(id, name, comment)
local pt = waypoint_find_by_id(id)
if not pt then
return
end
pt.name = dfhack.utf2df(name) or ''
pt.comment = comment and dfhack.utf2df(comment) or ''
end
--luacheck: in=bool
function routes_get_list(withpoints)
withpoints = istrue(withpoints)
local ret = {}
for i,route in ipairs(df.global.ui.waypoints.routes) do
local name = routename(route)
if withpoints then
local pts = {}
for j,v in ipairs(route.points) do
local pt = waypoint_find_by_id(v)
table.insert(pts, pt and pointname(pt) or '#invalid waypoint#')
end
table.insert(ret, { name, route.id, pts })
else
table.insert(ret, { name, route.id })
end
end
return ret
end
--luacheck: in=
function routes_add_route()
local route = df.ui.T_waypoints.T_routes:new()
route.id = df.global.ui.waypoints.next_route_id
df.global.ui.waypoints.routes:insert(#df.global.ui.waypoints.routes, route)
df.global.ui.waypoints.next_route_id = df.global.ui.waypoints.next_route_id + 1
end
-- routes may be referenced from squad orders, let's not try deleting them ourselves and use game ui instead
--luacheck: in=number
function route_delete(id)
local ws = dfhack.gui.getCurViewscreen()
if ws._type ~= df.viewscreen_dwarfmodest then
return
end
return execute_with_main_mode(df.ui_sidebar_mode.Default, function(ws)
gui.simulateInput(ws, K'D_NOTE')
df.global.ui.main.mode = df.ui_sidebar_mode.NotesRoutes
df.global.ui.waypoints.in_edit_waypts_mode = false
for i,v in ipairs(df.global.ui.waypoints.routes) do
if v.id == id then
df.global.ui.waypoints.sel_route_idx = i
gui.simulateInput(ws, K'D_NOTE_ROUTE_DELETE')
break
end
end
end)
end
--luacheck: in=number,string
function route_set_name(id, name)
local route = route_find_by_id(id)
if not route then
return
end
route.name = name and dfhack.utf2df(name) or ''
end
--luacheck: in=number
function route_get_info(id)
local route = route_find_by_id(id)
if not route then
return
end
local pts = {}
for i,v in ipairs(route.points) do
local pt = waypoint_find_by_id(v)
table.insert(pts, { pt and pointname(pt) or '#invalid point#', v })
end
return { routename(route), route.id, pts }
end
--luacheck: in=number,number[]
function route_add_points(id, ptids)
local route = route_find_by_id(id)
if not route then
return
end
for i,v in ipairs(ptids) do
--todo: check point id here?
route.points:insert(#route.points, v)
end
end
--luacheck: in=number,number,number
function route_reorder_points(id, fromidx, toidx)
local route = route_find_by_id(id)
if not route then
error('no route '..tostring(id))
end
local ptid = route.points[fromidx]
route.points:erase(fromidx)
route.points:insert(toidx, ptid)
return true
end
--luacheck: in=number,number
function route_delete_point(id, ptid)
local route = route_find_by_id(id)
if not route then
return
end
for i,v in ipairs(route.points) do
if v == ptid then
route.points:erase(i)
return true
end
end
end
--print(pcall(function() return json:encode(waypoints_place_point('qq', 'zz')) end))