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
302
303
304
305
|
local F = advtrains.formspec
local players_aspsel = {}
local function describe_t1_main_aspect(spv)
if spv == 0 then
return attrans("Danger (halt)")
elseif spv == -1 then
return attrans("Continue at maximum speed")
elseif not spv then
return attrans("Continue with current speed limit")
else
return attrans("Continue with the speed limit of @1", tostring(spv))
end
end
local function describe_t1_shunt_aspect(shunt)
if shunt then
return attrans("Shunting allowed")
else
return attrans("No shunting")
end
end
local function describe_t1_distant_aspect(spv)
if spv == 0 then
return attrans("Expect to stop at the next signal")
elseif spv == -1 then
return attrans("Expect to continue at maximum speed")
elseif not spv then
return attrans("No distant signal information")
else
return attrans("Expect to continue with a speed limit of @1", tostring(spv))
end
end
advtrains.interlocking.describe_t1_main_aspect = describe_t1_main_aspect
advtrains.interlocking.describe_t1_shunt_aspect = describe_t1_shunt_aspect
advtrains.interlocking.describe_t1_distant_aspect = describe_t1_distant_aspect
local function dsel(p, q, x, y)
if p == nil then
if q then
return x
else
return y
end
elseif p then
return x
else
return y
end
end
local function describe_supported_aspects_t1(suppasp, isasp)
local t = {}
local entries = {}
local selid = 1
for idx, spv in ipairs(suppasp.main) do
if isasp and spv == (isasp.main or false) then
selid = idx
end
entries[idx] = describe_t1_main_aspect(spv)
end
t.main = entries
t.main_current = selid
t.main_string = tostring(isasp.main)
if t.main == nil then
t.main_string = ""
end
t.shunt = {
attrans("No shunting"),
attrans("Shunting allowed"),
attrans("Proceed as main"),
}
t.shunt_current = dsel(suppasp.shunt, isasp.shunt, 2, 1)
if dsel(suppasp.proceed_as_main, isasp.proceed_as_main, t.shunt_current == 1) then
t.shunt_current = 3
end
t.shunt_const = suppasp.shunt ~= nil
entries = {}
selid = 1
for idx, spv in ipairs(suppasp.dst) do
if isasp and spv == (isasp.dst or false) then
selid = idx
end
entries[idx] = describe_t1_distant_aspect(spv)
end
t.dst = entries
t.dst_current = selid
return t
end
advtrains.interlocking.describe_supported_aspects_t1 = describe_supported_aspects_t1
local function make_signal_aspect_selector_t1(suppasp, purpose, isasp)
local t = describe_supported_aspects_t1(suppasp, isasp)
local formmode = 1
local pos
if type(purpose) == "table" then
formmode = 2
pos = purpose.pos
end
local form = {
"formspec_version[4]",
string.format("size[8,%f]", ({5.75, 9.25})[formmode]),
F.S_label(0.5, 0.5, "Select signal aspect"),
}
if formmode == 1 then
form[#form+1] = F.label(0.5, 1, purpose)
else
form[#form+1] = F.S_label(0.5, 1, "Signal at @1", minetest.pos_to_string(pos))
end
form[#form+1] = F.S_label(0.5, 1.5, "Main aspect")
if formmode == 1 then
form[#form+1] = F.field(0.5, 2, 7, "asp_mainval", "", t.main_string)
else
form[#form+1] = F.dropdown(0.5, 2, 7, "asp_mainsel", t.main, t.main_current, true)
end
form[#form+1] = F.S_label(0.5, 3, "Shunt aspect")
if formmode == 2 and t.shunt_const then
form[#form+1] = F.label(0.5, 3.5, t.shunt[t.shunt_current])
form[#form+1] = F.S_label(0.5, 4, "The shunt aspect cannot be changed.")
else
form[#form+1] = F.dropdown(0.5, 3.5, 7, "asp_shunt", t.shunt, t.shunt_current, true)
end
form[#form+1] = F.S_button_exit(0.5, 4.5, 7, "asp_save", "Save signal aspect")
if formmode == 2 then
form[#form+1] = advtrains.interlocking.make_ip_formspec_component(pos, 0.5, 5.5, 7)
form[#form+1] = advtrains.interlocking.make_short_dst_formspec_component(pos, 0.5, 7, 7)
end
return table.concat(form)
end
local function make_signal_aspect_selector_t2(suppasp, purpose, isasp)
local def = advtrains.interlocking.aspects.get_type2_definition(suppasp.group)
if not def then
return nil
end
local formmode = 1
local pos
if type(purpose) == "table" then
formmode = 2
pos = purpose.pos
end
local form = {
"formspec_version[4]",
string.format("size[8,%f]", ({4.25, 10.25})[formmode]),
F.S_label(0.5, 0.5, "Select signal aspect")
}
if formmode == 1 then
form[#form+1] = F.label(0.5, 1, purpose)
else
form[#form+1] = F.S_label(0.5, 1, "Signal at @1", minetest.pos_to_string(pos))
end
local entries = {}
local selid = #def.main
if isasp then
if isasp.type2name ~= def.main[selid].name then
selid = 1
end
end
if selid > 1 then
selid = 2
end
local entries = {
def.main[1].label,
def.main[#def.main].label,
}
form[#form+1] = F.S_label(0.5, 1.5, "Signal group: @1", def.label)
form[#form+1] = F.dropdown(0.5, 2, 7, "asp_sel", entries, selid, true)
form[#form+1] = F.S_button_exit(0.5, 3, 7, "asp_save", "Save signal aspect")
if formmode == 2 then
form[#form+1] = advtrains.interlocking.make_ip_formspec_component(pos, 0.5, 4, 7)
form[#form+1] = advtrains.interlocking.make_dst_formspec_component(pos, 0.5, 5.5, 7, 4.25)
end
return table.concat(form)
end
function advtrains.interlocking.show_signal_aspect_selector(pname, p_suppasp, p_purpose, callback, isasp)
local suppasp = p_suppasp or {
main = {0, -1},
dst = {false},
shunt = false,
info = {},
}
local purpose = p_purpose or ""
if type(p_purpose) == "table" then
purpose = {pname = pname, pos = p_purpose}
end
local form
if suppasp.type == 2 then
form = make_signal_aspect_selector_t2(suppasp, purpose, isasp)
else
form = make_signal_aspect_selector_t1(suppasp, purpose, isasp)
end
if not form then
return
end
local token = advtrains.random_id()
minetest.show_formspec(pname, "at_il_sigaspdia_"..token, form)
minetest.after(0, function()
players_aspsel[pname] = {
purpose = purpose,
suppasp = suppasp,
callback = callback,
token = token,
}
end)
end
local function usebool(sup, val, free)
if sup == nil then
return val == free
else
return sup
end
end
local function get_aspect_from_formspec_t1(suppasp, fields, psl)
local maini = tonumber(fields.asp_mainsel)
local main = suppasp.main[maini]
if not maini then
local mainval = fields.asp_mainval
if mainval == "-1" then
main = -1
elseif string.match(mainval, "^%d+$") then
main = tonumber(mainval)
else
main = nil
end
end
local shunti = tonumber(fields.asp_shunt)
local shunt = suppasp.shunt
if shunt == nil then
shunt = shunti == 2
end
local proceed_as_main = suppasp.proceed_as_main
if proceed_as_main == nil then
proceed_as_main = shunti == 3
end
return {
main = main,
shunt = shunt,
proceed_as_main = proceed_as_main,
info = {},
}
end
local function get_aspect_from_formspec_t2(suppasp, fields, psl)
local sel = tonumber(fields.asp_sel)
local def = advtrains.interlocking.aspects.get_type2_definition(suppasp.group)
if not (sel and def) then
return
end
if sel ~= 1 then
sel = #def.main
end
local asp = advtrains.interlocking.aspects.type2_to_type1(suppasp, sel)
return asp
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
local pname = player:get_player_name()
local psl = players_aspsel[pname]
if psl then
if formname == "at_il_sigaspdia_"..psl.token then
local suppasp = psl.suppasp
if fields.asp_save then
local asp
if suppasp.type == 2 then
asp = get_aspect_from_formspec_t2(suppasp, fields, psl)
else
asp = get_aspect_from_formspec_t1(suppasp, fields, psl)
end
if asp then
psl.callback(pname, asp)
end
end
if type(psl.purpose) == "table" then
local pos = psl.purpose.pos
advtrains.interlocking.handle_ip_formspec_fields(pname, pos, fields)
advtrains.interlocking.handle_dst_formspec_fields(pname, pos, fields)
end
else
players_aspsel[pname] = nil
end
end
end)
|