init
[df_autofly.git] / wpforms.lua
blob8a5f1ea631d3216bb2b71feb06effc4e8aa3182f
4 -- ADVMARKERS Stuff
5 -- Get the waypoints formspec
6 local formspec_list = {}
7 local selected_name = false
9 local storage = minetest.get_mod_storage()
10 local wpr=false;
11 local twpname=nil
12 local info=minetest.get_server_info()
13 local stprefix="autofly-".. info['address'] .. '-'
15 autofly = {}
16 wps={}
17 local tspeed = 20 -- speed in blocks per second
18 local speed=0;
19 local ltime=0
20 function autofly.display_formspec()
21 local formspec = 'size[5.25,8]' ..
22 'label[0,0;Waypoint list]' ..
23 'button_exit[0,7.5;1,0.5;goto;GO]' ..
24 'button_exit[0.8,7.5;1,0.5;display;SHO]' ..
25 'button[1.5125,7.5;0.9,0.5;warp;wrp]' ..
26 'button[2.125,7.5;1.2,0.5;autotp;w+e]' ..
27 'button[2.625,7.5;1.3,0.5;rename;Rename]' ..
28 'button[3.9375,7.5;1.3,0.5;delete;Delete]' ..
29 'textlist[0,0.75;5,6;marker;'
31 -- Iterate over all the waypoints
32 local selected = 1
33 formspec_list = {}
35 local waypoints = autofly.getwps()
38 for id, name in ipairs(waypoints) do
39 if id > 1 then
40 formspec = formspec .. ','
41 end
42 if not selected_name then
43 selected_name = name
44 end
45 if name == selected_name then
46 selected = id
47 end
48 formspec_list[#formspec_list + 1] = name
49 formspec = formspec .. '##' .. minetest.formspec_escape(name)
50 end
52 -- Close the text list and display the selected waypoint position
53 formspec = formspec .. ';' .. tostring(selected) .. ']'
54 if selected_name then
55 local pos = autofly.get_waypoint(selected_name)
56 if pos then
57 pos = minetest.formspec_escape(tostring(pos.x) .. ', ' ..
58 tostring(pos.y) .. ', ' .. tostring(pos.z))
59 pos = 'Waypoint position: ' .. pos
60 formspec = formspec .. 'label[0,6.75;' .. pos .. ']'
61 end
62 else
63 -- Draw over the buttons
64 formspec = formspec .. 'button_exit[0,7.5;5.25,0.5;quit;Close dialog]' ..
65 'label[0,6.75;No waypoints. Add one with ".add_mrkr".]'
66 end
68 -- Display the formspec
69 return minetest.show_formspec('autofly-csm', formspec)
70 end
72 minetest.register_on_formspec_input(function(formname, fields)
73 if formname == 'autofly-ignore' then
74 return true
75 elseif formname ~= 'autofly-csm' then
76 return
77 end
78 local name = false
79 if fields.marker then
80 local event = minetest.explode_textlist_event(fields.marker)
81 if event.index then
82 name = formspec_list[event.index]
83 end
84 else
85 name = selected_name
86 end
88 if name then
89 if fields.display then
90 if not autofly.display_waypoint(name) then
91 minetest.display_chat_message('Error displaying waypoint!')
92 end
93 elseif fields.goto then
94 if not autofly.goto_waypoint(name) then
95 minetest.display_chat_message('Error displaying waypoint!')
96 end
97 elseif fields.warp then
98 if not autofly.warp(name) then
99 minetest.display_chat_message('warp error')
101 elseif fields.autotp then
102 if not autofly.autotp(name) then
103 minetest.display_chat_message('warpandexit error')
105 elseif fields.rename then
106 minetest.show_formspec('autofly-csm', 'size[6,3]' ..
107 'label[0.35,0.2;Rename waypoint]' ..
108 'field[0.3,1.3;6,1;new_name;New name;' ..
109 minetest.formspec_escape(name) .. ']' ..
110 'button[0,2;3,1;cancel;Cancel]' ..
111 'button[3,2;3,1;rename_confirm;Rename]')
112 elseif fields.rename_confirm then
113 if fields.new_name and #fields.new_name > 0 then
114 if autofly.rename_waypoint(name, fields.new_name) then
115 selected_name = fields.new_name
116 else
117 minetest.display_chat_message('Error renaming waypoint!')
119 autofly.display_formspec()
120 else
121 minetest.display_chat_message(
122 'Please enter a new name for the marker.'
125 elseif fields.delete then
126 minetest.show_formspec('autofly-csm', 'size[6,2]' ..
127 'label[0.35,0.25;Are you sure you want to delete this waypoint?]' ..
128 'button[0,1;3,1;cancel;Cancel]' ..
129 'button[3,1;3,1;delete_confirm;Delete]')
130 elseif fields.delete_confirm then
131 autofly.delete_waypoint(name)
132 selected_name = false
133 autofly.display_formspec()
134 elseif fields.cancel then
135 autofly.display_formspec()
136 elseif name ~= selected_name then
137 selected_name = name
138 autofly.display_formspec()
140 elseif fields.display or fields.delete then
141 minetest.display_chat_message('Please select a waypoint.')
143 return true
144 end)
147 -- Export waypoints
148 function autofly.export(raw)
149 local s = storage:to_table().fields
150 if raw == 'M' then
151 s = minetest.compress(minetest.serialize(s))
152 s = 'M' .. minetest.encode_base64(s)
153 elseif not raw then
154 s = minetest.compress(minetest.write_json(s))
155 s = 'J' .. minetest.encode_base64(s)
157 return s
160 -- Allow string exporting
161 minetest.register_chatcommand('wpexp', {
162 params = '[old]',
163 description = 'Exports an autofly string containing all your markers.',
164 func = function(param)
165 local export
166 if param == 'old' then
167 export = autofly.export('M')
168 else
169 export = autofly.export()
171 minetest.show_formspec('autofly-ignore',
172 'field[_;Your waypoint export string;' ..
173 minetest.formspec_escape(export) .. ']')
177 --register_chatcommand_alias('wpexp', 'wp_export', 'waypoint_export')
179 -- String importing
180 minetest.register_chatcommand('wpimp', {
181 params = '<autofly string>',
182 description = 'Imports an autofly string. This will not overwrite ' ..
183 'existing markers that have the same name.',
184 func = function(param)
185 if autofly.import(param) then
186 return true, 'Waypoints imported!'
187 else
188 return false, 'Invalid autofly string!'
192 --register_chatcommand_alias('wpimp', 'wp_import', 'waypoint_import')
194 -- Import waypoints
195 function autofly.import(s)
196 if type(s) ~= 'table' then
197 local ver = s:sub(1, 1)
198 if ver ~= 'M' and ver ~= 'J' then return end
199 s = minetest.decode_base64(s:sub(2))
200 local success, msg = pcall(minetest.decompress, s)
201 if not success then return end
202 if ver == 'M' then
203 s = minetest.deserialize(msg, true)
204 else
205 s = minetest.parse_json(msg)
209 -- Iterate over waypoints to preserve existing ones and check for errors.
210 if type(s) == 'table' then
211 for name, pos in pairs(s) do
212 if type(name) == 'string' and type(pos) == 'string' and
213 name:sub(1, 7) == 'marker-' and minetest.string_to_pos(pos) and
214 storage:get_string(name) ~= pos then
215 -- Prevent collisions
216 local c = 0
217 while #storage:get_string(name) > 0 and c < 50 do
218 name = name .. '_'
219 c = c + 1
222 -- Sanity check
223 if c < 50 then
224 storage:set_string(name, pos)
228 return true