-
Notifications
You must be signed in to change notification settings - Fork 7
/
form.missionblock_stepeditor.lua
145 lines (117 loc) · 3.81 KB
/
form.missionblock_stepeditor.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
local FORMNAME = "mission_block_stepeditor"
missions.form.missionblock_stepeditor = function(pos, node, player)
local selected_step = missions.get_selected_list_item(player)
local steps = missions.get_steps(pos)
-- steps list
local steps_list = "textlist[0,1;5,6;steps;"
for i,step in ipairs(steps) do
steps_list = steps_list .. i .. ": " .. minetest.formspec_escape(step.name)
if i < #steps then
steps_list = steps_list .. ","
end
end
steps_list = steps_list .. ";" .. selected_step .. "]";
local formspec = "size[8,8;]" ..
--left
"label[0,0;Mission editor]" ..
"button[5.5,1;2,1;add;Add]" ..
"button[5.5,2;2,1;edit;Edit]" ..
"button[5.5,3;2,1;up;Up]" ..
"button[5.5,4;2,1;down;Down]" ..
"button[5.5,5;2,1;remove;Remove]" ..
steps_list ..
"button_exit[0,7;8,1;save;Save and validate]" ..
missions.FORMBG
minetest.show_formspec(player:get_player_name(),
FORMNAME .. ";" .. minetest.pos_to_string(pos),
formspec
)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
local parts = formname:split(";")
local name = parts[1]
if name ~= FORMNAME then
return
end
local pos = minetest.string_to_pos(parts[2])
local meta = minetest.get_meta(pos)
local node = minetest.get_node(pos)
if not missions.check_owner(pos, player) then
return
end
if fields.steps then
parts = fields.steps:split(":")
local command = parts[1]
local selected_step = tonumber(parts[2])
if "DCL" == command then -- double clicked
missions.set_selected_list_item(player, selected_step)
fields.edit = true
end
end
if fields.add then
missions.form.newstep(pos, node, player)
return true
end
if fields.remove then
local steps = missions.get_steps(pos)
local selected_step = missions.get_selected_list_item(player)
local last_step = selected_step == #steps
table.remove(steps, selected_step)
missions.set_steps(pos, steps)
missions.form.missionblock_stepeditor(pos, node, player)
if last_step then
missions.set_selected_list_item(player, math.max(selected_step - 1, 1))
end
return true
end
if fields.edit then
local stepnumber = missions.get_selected_list_item(player)
local steps = missions.get_steps(pos)
local step = steps[stepnumber]
if step then
local stepdata = step.data
missions.show_step_editor(pos, node, player, stepnumber, step, stepdata)
end
end
if fields.up then
local steps = missions.get_steps(pos)
local selected_step = missions.get_selected_list_item(player)
if selected_step > 1 then
local tmp = steps[selected_step-1]
steps[selected_step-1] = steps[selected_step]
steps[selected_step] = tmp
missions.set_steps(pos, steps)
missions.set_selected_list_item(player, selected_step - 1)
end
missions.form.missionblock_stepeditor(pos, node, player)
end
if fields.down then
local steps = missions.get_steps(pos)
local selected_step = missions.get_selected_list_item(player)
if selected_step < #steps then
local tmp = steps[selected_step+1]
steps[selected_step+1] = steps[selected_step]
steps[selected_step] = tmp
missions.set_steps(pos, steps)
missions.set_selected_list_item(player, selected_step + 1)
end
missions.form.missionblock_stepeditor(pos, node, player)
end
if fields.steps then
parts = fields.steps:split(":")
if parts[1] == "CHG" then
local selected_step = tonumber(parts[2])
missions.set_selected_list_item(player, selected_step)
end
end
if fields.save then
local result = missions.validate_mission(pos, player)
if result.success then
meta:set_string("infotext", "Mission: " .. meta:get_string("name"))
minetest.chat_send_player(player:get_player_name(), "Mission valid")
else
meta:set_string("infotext", "Mission: (invalid: " .. result.msg .. ")")
minetest.chat_send_player(player:get_player_name(), "Mission invalid: " .. result.msg)
end
end
end)