Use proper formspec formatter; clean up code

master
yw05 2022-01-24 15:23:06 +01:00
parent 0a5b327f46
commit eaadff49b6
5 changed files with 379 additions and 346 deletions

45
formspec.fnl Normal file
View File

@ -0,0 +1,45 @@
(require-macros :helpers)
(macro ensure-string [obj]
`(let [obj# ,obj]
(if (typep obj# :string) obj#
(typep obj# :number) (tostring obj#)
(list 'tostring obj#))))
(macro maybe-concat [tbl delim]
`(let [delim# ,delim tbl# ,tbl t# [] len# (length tbl#)]
(var i# 1)
(while (<= i# len#)
(if (typep (. tbl# i#) :string)
(let [start# i#]
(while (typep (. tbl# (+ 1 i#)) :string)
(set i# (+ 1 i#)))
(table.insert t# (table.concat tbl# delim# start# i#)))
(table.insert t# (. tbl# i#)))
(set i# (+ 1 i#)))
(if (not (. t# 1)) ""
(not (. t# 2)) (. t# 1)
(list 'table.concat t# delim#))))
(fn formspec [...]
`(table.concat
,(icollect [_ i (ipairs [...])]
(if (sequence? i)
`(string.format
"%s[%s]"
,(ensure-string (. i 1))
,(maybe-concat
(icollect [j v (ipairs i)]
(if (<= j 1)
nil
(sequence? v)
(maybe-concat
(icollect [_ k (ipairs v)]
(ensure-string k))
",")
(ensure-string v)))
";"))
(ensure-string i)))
" "))
{: formspec}

View File

@ -1,75 +1,69 @@
(fn S [...] `(minetest.translate "train_remote" ,...))
(fn Sf [...] `(minetest.formspec_escape (S ,...)))
(fn typep [obj typ]
`(= (type ,obj) ,typ))
(fn toint [n]
`(let [n# (tonumber ,n)]
(if n# (math.floor n#) nil)))
`(let [n# (tonumber ,n)]
(if n# (math.floor n#) nil)))
(fn check-wagon-access [pname wagon]
`(let [w# ,wagon]
(advtrains.check_driving_couple_protection pname w#.owner w#.whitelist)))
`(let [w# ,wagon]
(advtrains.check_driving_couple_protection pname w#.owner w#.whitelist)))
(fn check-train-access [pname trainid]
`(let [pn# ,pname tid# ,trainid]
(if (not (minetest.get_player_by_name pn#))
(values false (S "Player is not online"))
(let [t# (. advtrains.trains tid#)]
(if (not t#) (values false (S "No train with ID @1" tid#))
(let [tps# t#.trainparts]
(local n# (length tps#))
(local wagons# advtrains.wagons)
(var i# 0)
(var contp# true)
(while (and (<= i# n#) contp#)
(let [w# (. wagons# (. tps# i#))]
(if (and w# (check-wagon-access pn# w#))
(set contp# false)
(set i# (+ 1 i#)))))
(not contp#)))))))
`(let [pn# ,pname tid# ,trainid]
(if (not (minetest.get_player_by_name pn#))
(values false (S "Player is not online"))
(let [t# (. advtrains.trains tid#)]
(if (not t#) (values false (S "No train with ID @1" tid#))
(let [tps# t#.trainparts]
(local n# (length tps#))
(local wagons# advtrains.wagons)
(var i# 0)
(var contp# true)
(while (and (<= i# n#) contp#)
(let [w# (. wagons# (. tps# i#))]
(if (and w# (check-wagon-access pn# w#))
(set contp# false)
(set i# (+ 1 i#)))))
(not contp#)))))))
(fn leverof [train]
`(let [t# ,train]
(if (not t#) nil
(and (= t#.velocity 0) (not t#.active_control)) 1
t#.hud_lzb_effect_tmr 1
(or t#.lever 3))))
(fn fsformat [fmt ...]
(let [argv [...] oargs ['string.format fmt]]
(for [i 1 (length argv)]
(tset oargs (+ (length oargs) 1)
(if (sequence? (. argv i))
(list 'Sf (unpack (. argv i)))
(. argv i))))
(list (unpack oargs))))
`(let [t# ,train]
(if (not t#) nil
(and (= t#.velocity 0) (not t#.active_control)) 1
t#.hud_lzb_effect_tmr 1
(or t#.lever 3))))
(fn get-textlist [field max]
`(let [et# (minetest.explode_textlist_event ,field)]
(if (not et#) nil
(and (or (= et#.type :CHG) (= et#.type :DCL))
(>= et#.index 1) (<= et#.index ,max))
et#.index
nil)))
`(let [et# (minetest.explode_textlist_event ,field)]
(if (not et#) nil
(and (or (= et#.type :CHG) (= et#.type :DCL))
(>= et#.index 1) (<= et#.index ,max))
et#.index
nil)))
(fn has-scrollbar-event [field]
`(let [et# (minetest.explode_scrollbar_event ,field)]
(if (not et#) false
(= et#.type :CHG))))
`(let [et# (minetest.explode_scrollbar_event ,field)]
(if (not et#) false
(= et#.type :CHG))))
(fn get-scrollbar [field min max]
`(let [et# (minetest.explode_scrollbar_event ,field)]
(if (not et#) nil
(and (= et#.type :CHG) (>= et#.value ,min) (<= et#.value ,max)) et#.value
nil)))
`(let [et# (minetest.explode_scrollbar_event ,field)]
(if (not et#) nil
(and (= et#.type :CHG) (>= et#.value ,min) (<= et#.value ,max)) et#.value
nil)))
{:
S
{: S
: Sf
: typep
: toint
: check-wagon-access
: check-train-access
: leverof
: fsformat
: get-textlist
: has-scrollbar-event
: get-scrollbar}
: get-scrollbar
}

View File

@ -1,17 +1,18 @@
(require-macros :main)
(require-macros :helpers)
(require-macros :formspec)
(minetest.register_on_player_receive_fields
(fn [player formname fields]
(let [pname
(player:get_player_name)
trainid
(string.match formname "^train_remote:t(%S+)$")
wagonid
(string.match formname "^train_remote:w(%S+)$")]
(if trainid (handle-train-control-fields trainid pname fields)
wagonid (handle-wagon-control-fields wagonid pname fields)
nil))))
(let [pname
(player:get_player_name)
trainid
(string.match formname "^train_remote:t(%S+)$")
wagonid
(string.match formname "^train_remote:w(%S+)$")]
(if trainid (handle-train-control-fields trainid pname fields)
wagonid (handle-wagon-control-fields wagonid pname fields)
nil))))
(minetest.register_chatcommand
"train_remote"
@ -21,6 +22,6 @@
(S "Remotely control the train with the given ID")
:func
(fn [pname trainid]
(let [(accessp msg) (check-train-access pname trainid)]
(if (not accessp) (values false msg)
(do (show-train-remote-formspec pname trainid) nil))))})
(let [(accessp msg) (check-train-access pname trainid)]
(if (not accessp) (values false msg)
(do (show-train-remote-formspec pname trainid) nil))))})

388
init.lua
View File

@ -1,236 +1,238 @@
local function _0_(player, formname, fields)
local function _1_(player, formname, fields)
local pname = player:get_player_name()
local trainid = string.match(formname, "^train_remote:t(%S+)$")
local wagonid = string.match(formname, "^train_remote:w(%S+)$")
if trainid then
local trainid_0_ = trainid
local pname_0_ = pname
local fields_0_ = fields
if not trainid_0_ then
local trainid_5_auto = trainid
local pname_6_auto = pname
local fields_7_auto = fields
if not trainid_5_auto then
return nil
else
local _1_
local _2_
do
local pn_0_ = pname_0_
local tid_0_ = trainid_0_
if not minetest.get_player_by_name(pn_0_) then
local pn_3_auto = pname_6_auto
local tid_4_auto = trainid_5_auto
if not minetest.get_player_by_name(pn_3_auto) then
minetest.translate("train_remote", "Player is not online")
_1_ = false
_2_ = false
else
local t_0_ = advtrains.trains[tid_0_]
if not t_0_ then
minetest.translate("train_remote", "No train with ID @1", tid_0_)
_1_ = false
local t_5_auto = advtrains.trains[tid_4_auto]
if not t_5_auto then
minetest.translate("train_remote", "No train with ID @1", tid_4_auto)
_2_ = false
else
local tps_0_ = t_0_.trainparts
local n_0_ = #tps_0_
local wagons_0_ = advtrains.wagons
local i_0_ = 0
local contp_0_ = true
while ((i_0_ <= n_0_) and contp_0_) do
local w_0_ = (wagons_0_)[(tps_0_)[i_0_]]
local function _4_()
local w_0_0 = w_0_
return advtrains.check_driving_couple_protection(pname, w_0_0.owner, w_0_0.whitelist)
local tps_6_auto = t_5_auto.trainparts
local n_7_auto = #tps_6_auto
local wagons_8_auto = advtrains.wagons
local i_9_auto = 0
local contp_10_auto = true
while ((i_9_auto <= n_7_auto) and contp_10_auto) do
local w_11_auto = (wagons_8_auto)[(tps_6_auto)[i_9_auto]]
local function _5_()
local w_2_auto = w_11_auto
return advtrains.check_driving_couple_protection(pname, w_2_auto.owner, w_2_auto.whitelist)
end
if (w_0_ and _4_()) then
contp_0_ = false
if (w_11_auto and _5_()) then
contp_10_auto = false
else
i_0_ = (1 + i_0_)
i_9_auto = (1 + i_9_auto)
end
end
_1_ = not contp_0_
_2_ = not contp_10_auto
end
end
end
if not _1_ then
if not _2_ then
return nil
elseif fields_0_["close-formspec"] then
elseif fields_7_auto["close-formspec"] then
return nil
elseif minetest.is_yes(fields_0_.quit) then
elseif minetest.is_yes(fields_7_auto.quit) then
return nil
else
local t_0_ = advtrains.trains[trainid_0_]
local _2_
local t_8_auto = advtrains.trains[trainid_5_auto]
local _9_
do
local et_0_ = minetest.explode_scrollbar_event(fields_0_.door)
if not et_0_ then
_2_ = false
local et_14_auto = minetest.explode_scrollbar_event(fields_7_auto.door)
if not et_14_auto then
_9_ = false
else
_2_ = (et_0_.type == "CHG")
_9_ = (et_14_auto.type == "CHG")
end
end
if _2_ then
local dside_0_
if _9_ then
local dside_9_auto
do
local et_0_ = minetest.explode_scrollbar_event(fields_0_.door)
if not et_0_ then
dside_0_ = nil
elseif ((et_0_.type == "CHG") and (et_0_.value >= 1) and (et_0_.value <= 3)) then
dside_0_ = et_0_.value
local et_15_auto = minetest.explode_scrollbar_event(fields_7_auto.door)
if not et_15_auto then
dside_9_auto = nil
elseif ((et_15_auto.type == "CHG") and (et_15_auto.value >= 1) and (et_15_auto.value <= 3)) then
dside_9_auto = et_15_auto.value
else
dside_0_ = nil
dside_9_auto = nil
end
end
if dside_0_ then
t_0_["door_open"] = (dside_0_ - 2)
if dside_9_auto then
t_8_auto["door_open"] = (dside_9_auto - 2)
end
else
local _3_
local _13_
do
local et_0_ = minetest.explode_scrollbar_event(fields_0_.lever)
if not et_0_ then
_3_ = false
local et_14_auto = minetest.explode_scrollbar_event(fields_7_auto.lever)
if not et_14_auto then
_13_ = false
else
_3_ = (et_0_.type == "CHG")
_13_ = (et_14_auto.type == "CHG")
end
end
if _3_ then
local tlev_0_
if _13_ then
local tlev_10_auto
do
local et_0_ = minetest.explode_scrollbar_event(fields_0_.lever)
if not et_0_ then
tlev_0_ = nil
elseif ((et_0_.type == "CHG") and (et_0_.value >= 1) and (et_0_.value <= 5)) then
tlev_0_ = et_0_.value
local et_15_auto = minetest.explode_scrollbar_event(fields_7_auto.lever)
if not et_15_auto then
tlev_10_auto = nil
elseif ((et_15_auto.type == "CHG") and (et_15_auto.value >= 1) and (et_15_auto.value <= 5)) then
tlev_10_auto = et_15_auto.value
else
tlev_0_ = nil
tlev_10_auto = nil
end
end
if tlev_0_ then
t_0_["ctrl_user"] = (5 - tlev_0_)
if tlev_10_auto then
t_8_auto["ctrl_user"] = (5 - tlev_10_auto)
end
elseif fields_0_.reverse then
if (t_0_.velocity <= 0) then
advtrains.invert_train(trainid_0_)
advtrains.atc.train_reset_command(t_0_)
elseif fields_7_auto.reverse then
if (t_8_auto.velocity <= 0) then
advtrains.invert_train(trainid_5_auto)
advtrains.atc.train_reset_command(t_8_auto)
end
elseif fields_0_.ars then
elseif fields_7_auto.ars then
if advtrains.interlocking then
advtrains.interlocking.ars_set_disable(t_0_, not minetest.is_yes(fields_0_.ars))
advtrains.interlocking.ars_set_disable(t_8_auto, not minetest.is_yes(fields_7_auto.ars))
end
end
end
if fields_0_.outtext then
t_0_["text_outside"] = fields_0_.outtext
if fields_7_auto.outtext then
t_8_auto["text_outside"] = fields_7_auto.outtext
end
if fields_0_.intext then
t_0_["text_inside"] = fields_0_.intext
if fields_7_auto.intext then
t_8_auto["text_inside"] = fields_7_auto.intext
end
if fields_0_.lntext then
t_0_["line"] = fields_0_.lntext
minetest.after(0, advtrains.invalidate_path, trainid_0_)
if fields_7_auto.lntext then
t_8_auto["line"] = fields_7_auto.lntext
minetest.after(0, advtrains.invalidate_path, trainid_5_auto)
end
if fields_0_.rctext then
t_0_["routingcode"] = fields_0_.rctext
minetest.after(0, advtrains.invalidate_path, trainid_0_)
if fields_7_auto.rctext then
t_8_auto["routingcode"] = fields_7_auto.rctext
minetest.after(0, advtrains.invalidate_path, trainid_5_auto)
end
do
local pn_0_ = pname_0_
local tid_0_ = trainid_0_
local _7_
local pn_1_auto = pname_6_auto
local tid_2_auto = trainid_5_auto
local _24_
do
local pn_0_0 = pn_0_
local tid_0_0 = tid_0_
if not minetest.get_player_by_name(pn_0_0) then
local pn_3_auto = pn_1_auto
local tid_4_auto = tid_2_auto
if not minetest.get_player_by_name(pn_3_auto) then
minetest.translate("train_remote", "Player is not online")
_7_ = false
_24_ = false
else
local t_0_0 = advtrains.trains[tid_0_0]
if not t_0_0 then
minetest.translate("train_remote", "No train with ID @1", tid_0_0)
_7_ = false
local t_5_auto = advtrains.trains[tid_4_auto]
if not t_5_auto then
minetest.translate("train_remote", "No train with ID @1", tid_4_auto)
_24_ = false
else
local tps_0_ = t_0_0.trainparts
local n_0_ = #tps_0_
local wagons_0_ = advtrains.wagons
local i_0_ = 0
local contp_0_ = true
while ((i_0_ <= n_0_) and contp_0_) do
local w_0_ = (wagons_0_)[(tps_0_)[i_0_]]
local function _10_()
local w_0_0 = w_0_
return advtrains.check_driving_couple_protection(pname, w_0_0.owner, w_0_0.whitelist)
local tps_6_auto = t_5_auto.trainparts
local n_7_auto = #tps_6_auto
local wagons_8_auto = advtrains.wagons
local i_9_auto = 0
local contp_10_auto = true
while ((i_9_auto <= n_7_auto) and contp_10_auto) do
local w_11_auto = (wagons_8_auto)[(tps_6_auto)[i_9_auto]]
local function _27_()
local w_2_auto = w_11_auto
return advtrains.check_driving_couple_protection(pname, w_2_auto.owner, w_2_auto.whitelist)
end
if (w_0_ and _10_()) then
contp_0_ = false
if (w_11_auto and _27_()) then
contp_10_auto = false
else
i_0_ = (1 + i_0_)
i_9_auto = (1 + i_9_auto)
end
end
_7_ = not contp_0_
_24_ = not contp_10_auto
end
end
end
if not _7_ then
if not _24_ then
else
local t_0_0 = advtrains.trains[tid_0_]
local fs_0_
local function _8_()
local t_0_1 = t_0_0
if not t_0_1 then
return nil
elseif ((t_0_1.velocity == 0) and not t_0_1.active_control) then
return 1
elseif t_0_1.hud_lzb_effect_tmr then
return 1
local t_3_auto = advtrains.trains[tid_2_auto]
local fs_4_auto
local _31_
do
local t_12_auto = t_3_auto
if not t_12_auto then
_31_ = nil
elseif ((t_12_auto.velocity == 0) and not t_12_auto.active_control) then
_31_ = 1
elseif t_12_auto.hud_lzb_effect_tmr then
_31_ = 1
else
return (t_0_1.lever or 3)
_31_ = (t_12_auto.lever or 3)
end
end
local _9_
if (advtrains.interlocking and t_0_0.ars_disable) then
_9_ = "false"
else
_9_ = "true"
local function _33_()
if (advtrains.interlocking and t_3_auto.ars_disable) then
return "false"
else
return "true"
end
end
fs_0_ = string.format("%s\nsize[11.5,11]\nlabel[0.5,0.75;%s]\nlabel[0.5,1.25;%s]\nbutton[6,0.5;4,1;update;%s]\nbutton_exit[10,0.5;1,1;close-formspec;X]\nlabel[1.25,2;%s]label[1.25,2.5;%s]label[1.25,3;%s]label[1.25,3.5;%s]label[1.25,4;%s]\nscrollbaroptions[min=1;max=5;smallstep=1;largestep=1;thumbsize=1;arrows=hide]\nscrollbar[0.5,1.75;0.5,2.5;vertical;lever;%d]\nlabel[6,2;%s]scrollbaroptions[min=1;max=3;smallstep=1;largestep=1;thumbsize=1;arrows=hide]\nscrollbar[8.5,1.75;2.5,0.5;horizontal;door;%d]\ncheckbox[6,2.75;ars;%s;%s]\nbutton[6,3.25;5,1;reverse;%s]\nfield[0.5,5;9.5,1;outtext;%s;%s]\nbutton[10,5;1,1;outset;%s]\nfield[0.5,6.5;9.5,1;intext;%s;%s]\nbutton[10,6.5;1,1;inset;%s]\nfield[0.5,8;9.5,1;lntext;%s;%s]\nbutton[10,8;1,1;lnset;%s]\nfield[0.5,9.5;9.5,1;rctext;%s;%s]\nbutton[10,9.5;1,1;rcset;%s]\n", "formspec_version[3]", minetest.formspec_escape(minetest.translate("train_remote", "Remote control for @1", tid_0_)), minetest.formspec_escape(minetest.translate("train_remote", "Wagon(s): @1", #t_0_0.trainparts)), minetest.formspec_escape(minetest.translate("train_remote", "Update")), minetest.formspec_escape(minetest.translate("train_remote", "Accelerate")), minetest.formspec_escape(minetest.translate("train_remote", "Neutral")), minetest.formspec_escape(minetest.translate("train_remote", "Roll")), minetest.formspec_escape(minetest.translate("train_remote", "Regular brake")), minetest.formspec_escape(minetest.translate("train_remote", "Emergency brake")), (5 - _8_()), minetest.formspec_escape(minetest.translate("train_remote", "Door control")), ((t_0_0.door_open or 0) + 2), minetest.formspec_escape(minetest.translate("train_remote", "Automatic routesetting")), _9_, minetest.formspec_escape(minetest.translate("train_remote", "Reverse train")), minetest.formspec_escape(minetest.translate("train_remote", "External display")), minetest.formspec_escape((t_0_0.text_outside or "")), minetest.formspec_escape(minetest.translate("train_remote", "Set")), minetest.formspec_escape(minetest.translate("train_remote", "Internal display")), minetest.formspec_escape((t_0_0.text_inside or "")), minetest.formspec_escape(minetest.translate("train_remote", "Set")), minetest.formspec_escape(minetest.translate("train_remote", "Line")), minetest.formspec_escape((t_0_0.line or "")), minetest.formspec_escape(minetest.translate("train_remote", "Set")), minetest.formspec_escape(minetest.translate("train_remote", "Routing code")), minetest.formspec_escape((t_0_0.routingcode or "")), minetest.formspec_escape(minetest.translate("train_remote", "Set")))
minetest.show_formspec(pn_0_, ("train_remote:t" .. tid_0_), fs_0_)
fs_4_auto = table.concat({"formspec_version[3]", string.format("%s[%s]", "size", "11.5,11"), string.format("%s[%s]", "label", table.concat({"0.5,0.75", tostring(minetest.translate("train_remote", "Remote control for @1", tid_2_auto))}, ";")), string.format("%s[%s]", "label", table.concat({"0.5,1.25", tostring(minetest.translate("train_remote", "Wagon(s): @1", #t_3_auto.trainparts))}, ";")), string.format("%s[%s]", "button", table.concat({"6,0.5;4,1;update", tostring(minetest.translate("train_remote", "Update"))}, ";")), string.format("%s[%s]", "button_exit", "10,0.5;1,1;close-formspec;X"), string.format("%s[%s]", "label", table.concat({"1.25,2", tostring(minetest.translate("train_remote", "Accelerate"))}, ";")), string.format("%s[%s]", "label", table.concat({"1.25,2.5", tostring(minetest.translate("train_remote", "Neutral"))}, ";")), string.format("%s[%s]", "label", table.concat({"1.25,3", tostring(minetest.translate("train_remote", "Roll"))}, ";")), string.format("%s[%s]", "label", table.concat({"1.25,3.5", tostring(minetest.translate("train_remote", "Regular brake"))}, ";")), string.format("%s[%s]", "label", table.concat({"1.25,4", tostring(minetest.translate("train_remote", "Emergency brake"))}, ";")), string.format("%s[%s]", "scrollbaroptions", "min=1;max=5;smallstep=1;largestep=1;thumbsize=1;arrows=hide"), string.format("%s[%s]", "scrollbar", table.concat({"0.5,1.75;0.5,2.5;vertical;lever", tostring((5 - _31_))}, ";")), string.format("%s[%s]", "label", table.concat({"6,2", tostring(minetest.translate("train_remote", "Door control"))}, ";")), string.format("%s[%s]", "scrollbaroptions", "min=1;max=3;smallstep=1;largestep=1;thumbsize=1;arrows=hide"), string.format("%s[%s]", "scrollbar", table.concat({"8.5,1.75;2.5,0.5;horizontal;door", tostring(((t_3_auto.door_open or 0) + 2))}, ";")), string.format("%s[%s]", "checkbox", table.concat({"6,2.75;ars", tostring(minetest.translate("train_remote", "Automatic routesetting")), tostring(_33_())}, ";")), string.format("%s[%s]", "button", table.concat({"6,3.25;5,1;reverse", tostring(minetest.translate("train_remote", "Reverse train"))}, ";")), string.format("%s[%s]", "field", table.concat({"0.5,5;9.5,1;outtext", tostring(minetest.translate("train_remote", "External display")), tostring(minetest.formspec_escape((t_3_auto.text_outside or "")))}, ";")), string.format("%s[%s]", "button", table.concat({"10,5;1,1;outset", tostring(minetest.translate("train_remote", "Set"))}, ";")), string.format("%s[%s]", "field", table.concat({"0.5,6.5;9.5,1;intext", tostring(minetest.translate("train_remote", "Internal display")), tostring(minetest.formspec_escape((t_3_auto.text_inside or "")))}, ";")), string.format("%s[%s]", "button", table.concat({"10,6.5;1,1;inset", tostring(minetest.translate("train_remote", "Set"))}, ";")), string.format("%s[%s]", "field", table.concat({"0.5,8;9.5,1;lntext", tostring(minetest.translate("train_remote", "Line")), tostring(minetest.formspec_escape((t_3_auto.line or "")))}, ";")), string.format("%s[%s]", "button", table.concat({"10,8;1,1;lnset", tostring(minetest.translate("train_remote", "Set"))}, ";")), string.format("%s[%s]", "field", table.concat({"0.5,9.5;9.5,1;rctext", tostring(minetest.translate("train_remote", "Routing code")), tostring(minetest.formspec_escape((t_3_auto.routingcode or "")))}, ";")), string.format("%s[%s]", "button", table.concat({"10,9.5;1,1;rcset", tostring(minetest.translate("train_remote", "Set"))}, ";"))}, " ")
minetest.show_formspec(pn_1_auto, ("train_remote:t" .. tid_2_auto), fs_4_auto)
end
end
return nil
end
end
elseif wagonid then
local wagonid_0_ = wagonid
local pname_0_ = pname
local fields_0_ = fields
local wagonid_11_auto = wagonid
local pname_12_auto = pname
local fields_13_auto = fields
return nil
else
return nil
end
end
minetest.register_on_player_receive_fields(_0_)
local function _1_(pname, trainid)
minetest.register_on_player_receive_fields(_1_)
local function _37_(pname, trainid)
local accessp, msg = nil, nil
do
local pn_0_ = pname
local tid_0_ = trainid
if not minetest.get_player_by_name(pn_0_) then
local pn_3_auto = pname
local tid_4_auto = trainid
if not minetest.get_player_by_name(pn_3_auto) then
accessp, msg = false, minetest.translate("train_remote", "Player is not online")
else
local t_0_ = advtrains.trains[tid_0_]
if not t_0_ then
accessp, msg = false, minetest.translate("train_remote", "No train with ID @1", tid_0_)
local t_5_auto = advtrains.trains[tid_4_auto]
if not t_5_auto then
accessp, msg = false, minetest.translate("train_remote", "No train with ID @1", tid_4_auto)
else
local tps_0_ = t_0_.trainparts
local n_0_ = #tps_0_
local wagons_0_ = advtrains.wagons
local i_0_ = 0
local contp_0_ = true
while ((i_0_ <= n_0_) and contp_0_) do
local w_0_ = (wagons_0_)[(tps_0_)[i_0_]]
local function _2_()
local w_0_0 = w_0_
return advtrains.check_driving_couple_protection(pname, w_0_0.owner, w_0_0.whitelist)
local tps_6_auto = t_5_auto.trainparts
local n_7_auto = #tps_6_auto
local wagons_8_auto = advtrains.wagons
local i_9_auto = 0
local contp_10_auto = true
while ((i_9_auto <= n_7_auto) and contp_10_auto) do
local w_11_auto = (wagons_8_auto)[(tps_6_auto)[i_9_auto]]
local function _38_()
local w_2_auto = w_11_auto
return advtrains.check_driving_couple_protection(pname, w_2_auto.owner, w_2_auto.whitelist)
end
if (w_0_ and _2_()) then
contp_0_ = false
if (w_11_auto and _38_()) then
contp_10_auto = false
else
i_0_ = (1 + i_0_)
i_9_auto = (1 + i_9_auto)
end
end
accessp, msg = not contp_0_
accessp, msg = not contp_10_auto
end
end
end
@ -238,69 +240,71 @@ local function _1_(pname, trainid)
return false, msg
else
do
local pn_0_ = pname
local tid_0_ = trainid
local _2_
local pn_1_auto = pname
local tid_2_auto = trainid
local _42_
do
local pn_0_0 = pn_0_
local tid_0_0 = tid_0_
if not minetest.get_player_by_name(pn_0_0) then
local pn_3_auto = pn_1_auto
local tid_4_auto = tid_2_auto
if not minetest.get_player_by_name(pn_3_auto) then
minetest.translate("train_remote", "Player is not online")
_2_ = false
_42_ = false
else
local t_0_ = advtrains.trains[tid_0_0]
if not t_0_ then
minetest.translate("train_remote", "No train with ID @1", tid_0_0)
_2_ = false
local t_5_auto = advtrains.trains[tid_4_auto]
if not t_5_auto then
minetest.translate("train_remote", "No train with ID @1", tid_4_auto)
_42_ = false
else
local tps_0_ = t_0_.trainparts
local n_0_ = #tps_0_
local wagons_0_ = advtrains.wagons
local i_0_ = 0
local contp_0_ = true
while ((i_0_ <= n_0_) and contp_0_) do
local w_0_ = (wagons_0_)[(tps_0_)[i_0_]]
local function _5_()
local w_0_0 = w_0_
return advtrains.check_driving_couple_protection(pname, w_0_0.owner, w_0_0.whitelist)
local tps_6_auto = t_5_auto.trainparts
local n_7_auto = #tps_6_auto
local wagons_8_auto = advtrains.wagons
local i_9_auto = 0
local contp_10_auto = true
while ((i_9_auto <= n_7_auto) and contp_10_auto) do
local w_11_auto = (wagons_8_auto)[(tps_6_auto)[i_9_auto]]
local function _45_()
local w_2_auto = w_11_auto
return advtrains.check_driving_couple_protection(pname, w_2_auto.owner, w_2_auto.whitelist)
end
if (w_0_ and _5_()) then
contp_0_ = false
if (w_11_auto and _45_()) then
contp_10_auto = false
else
i_0_ = (1 + i_0_)
i_9_auto = (1 + i_9_auto)
end
end
_2_ = not contp_0_
_42_ = not contp_10_auto
end
end
end
if not _2_ then
if not _42_ then
else
local t_0_ = advtrains.trains[tid_0_]
local fs_0_
local function _3_()
local t_0_0 = t_0_
if not t_0_0 then
return nil
elseif ((t_0_0.velocity == 0) and not t_0_0.active_control) then
return 1
elseif t_0_0.hud_lzb_effect_tmr then
return 1
local t_3_auto = advtrains.trains[tid_2_auto]
local fs_4_auto
local _49_
do
local t_12_auto = t_3_auto
if not t_12_auto then
_49_ = nil
elseif ((t_12_auto.velocity == 0) and not t_12_auto.active_control) then
_49_ = 1
elseif t_12_auto.hud_lzb_effect_tmr then
_49_ = 1
else
return (t_0_0.lever or 3)
_49_ = (t_12_auto.lever or 3)
end
end
local _4_
if (advtrains.interlocking and t_0_.ars_disable) then
_4_ = "false"
else
_4_ = "true"
local function _51_()
if (advtrains.interlocking and t_3_auto.ars_disable) then
return "false"
else
return "true"
end
end
fs_0_ = string.format("%s\nsize[11.5,11]\nlabel[0.5,0.75;%s]\nlabel[0.5,1.25;%s]\nbutton[6,0.5;4,1;update;%s]\nbutton_exit[10,0.5;1,1;close-formspec;X]\nlabel[1.25,2;%s]label[1.25,2.5;%s]label[1.25,3;%s]label[1.25,3.5;%s]label[1.25,4;%s]\nscrollbaroptions[min=1;max=5;smallstep=1;largestep=1;thumbsize=1;arrows=hide]\nscrollbar[0.5,1.75;0.5,2.5;vertical;lever;%d]\nlabel[6,2;%s]scrollbaroptions[min=1;max=3;smallstep=1;largestep=1;thumbsize=1;arrows=hide]\nscrollbar[8.5,1.75;2.5,0.5;horizontal;door;%d]\ncheckbox[6,2.75;ars;%s;%s]\nbutton[6,3.25;5,1;reverse;%s]\nfield[0.5,5;9.5,1;outtext;%s;%s]\nbutton[10,5;1,1;outset;%s]\nfield[0.5,6.5;9.5,1;intext;%s;%s]\nbutton[10,6.5;1,1;inset;%s]\nfield[0.5,8;9.5,1;lntext;%s;%s]\nbutton[10,8;1,1;lnset;%s]\nfield[0.5,9.5;9.5,1;rctext;%s;%s]\nbutton[10,9.5;1,1;rcset;%s]\n", "formspec_version[3]", minetest.formspec_escape(minetest.translate("train_remote", "Remote control for @1", tid_0_)), minetest.formspec_escape(minetest.translate("train_remote", "Wagon(s): @1", #t_0_.trainparts)), minetest.formspec_escape(minetest.translate("train_remote", "Update")), minetest.formspec_escape(minetest.translate("train_remote", "Accelerate")), minetest.formspec_escape(minetest.translate("train_remote", "Neutral")), minetest.formspec_escape(minetest.translate("train_remote", "Roll")), minetest.formspec_escape(minetest.translate("train_remote", "Regular brake")), minetest.formspec_escape(minetest.translate("train_remote", "Emergency brake")), (5 - _3_()), minetest.formspec_escape(minetest.translate("train_remote", "Door control")), ((t_0_.door_open or 0) + 2), minetest.formspec_escape(minetest.translate("train_remote", "Automatic routesetting")), _4_, minetest.formspec_escape(minetest.translate("train_remote", "Reverse train")), minetest.formspec_escape(minetest.translate("train_remote", "External display")), minetest.formspec_escape((t_0_.text_outside or "")), minetest.formspec_escape(minetest.translate("train_remote", "Set")), minetest.formspec_escape(minetest.translate("train_remote", "Internal display")), minetest.formspec_escape((t_0_.text_inside or "")), minetest.formspec_escape(minetest.translate("train_remote", "Set")), minetest.formspec_escape(minetest.translate("train_remote", "Line")), minetest.formspec_escape((t_0_.line or "")), minetest.formspec_escape(minetest.translate("train_remote", "Set")), minetest.formspec_escape(minetest.translate("train_remote", "Routing code")), minetest.formspec_escape((t_0_.routingcode or "")), minetest.formspec_escape(minetest.translate("train_remote", "Set")))
minetest.show_formspec(pn_0_, ("train_remote:t" .. tid_0_), fs_0_)
fs_4_auto = table.concat({"formspec_version[3]", string.format("%s[%s]", "size", "11.5,11"), string.format("%s[%s]", "label", table.concat({"0.5,0.75", tostring(minetest.translate("train_remote", "Remote control for @1", tid_2_auto))}, ";")), string.format("%s[%s]", "label", table.concat({"0.5,1.25", tostring(minetest.translate("train_remote", "Wagon(s): @1", #t_3_auto.trainparts))}, ";")), string.format("%s[%s]", "button", table.concat({"6,0.5;4,1;update", tostring(minetest.translate("train_remote", "Update"))}, ";")), string.format("%s[%s]", "button_exit", "10,0.5;1,1;close-formspec;X"), string.format("%s[%s]", "label", table.concat({"1.25,2", tostring(minetest.translate("train_remote", "Accelerate"))}, ";")), string.format("%s[%s]", "label", table.concat({"1.25,2.5", tostring(minetest.translate("train_remote", "Neutral"))}, ";")), string.format("%s[%s]", "label", table.concat({"1.25,3", tostring(minetest.translate("train_remote", "Roll"))}, ";")), string.format("%s[%s]", "label", table.concat({"1.25,3.5", tostring(minetest.translate("train_remote", "Regular brake"))}, ";")), string.format("%s[%s]", "label", table.concat({"1.25,4", tostring(minetest.translate("train_remote", "Emergency brake"))}, ";")), string.format("%s[%s]", "scrollbaroptions", "min=1;max=5;smallstep=1;largestep=1;thumbsize=1;arrows=hide"), string.format("%s[%s]", "scrollbar", table.concat({"0.5,1.75;0.5,2.5;vertical;lever", tostring((5 - _49_))}, ";")), string.format("%s[%s]", "label", table.concat({"6,2", tostring(minetest.translate("train_remote", "Door control"))}, ";")), string.format("%s[%s]", "scrollbaroptions", "min=1;max=3;smallstep=1;largestep=1;thumbsize=1;arrows=hide"), string.format("%s[%s]", "scrollbar", table.concat({"8.5,1.75;2.5,0.5;horizontal;door", tostring(((t_3_auto.door_open or 0) + 2))}, ";")), string.format("%s[%s]", "checkbox", table.concat({"6,2.75;ars", tostring(minetest.translate("train_remote", "Automatic routesetting")), tostring(_51_())}, ";")), string.format("%s[%s]", "button", table.concat({"6,3.25;5,1;reverse", tostring(minetest.translate("train_remote", "Reverse train"))}, ";")), string.format("%s[%s]", "field", table.concat({"0.5,5;9.5,1;outtext", tostring(minetest.translate("train_remote", "External display")), tostring(minetest.formspec_escape((t_3_auto.text_outside or "")))}, ";")), string.format("%s[%s]", "button", table.concat({"10,5;1,1;outset", tostring(minetest.translate("train_remote", "Set"))}, ";")), string.format("%s[%s]", "field", table.concat({"0.5,6.5;9.5,1;intext", tostring(minetest.translate("train_remote", "Internal display")), tostring(minetest.formspec_escape((t_3_auto.text_inside or "")))}, ";")), string.format("%s[%s]", "button", table.concat({"10,6.5;1,1;inset", tostring(minetest.translate("train_remote", "Set"))}, ";")), string.format("%s[%s]", "field", table.concat({"0.5,8;9.5,1;lntext", tostring(minetest.translate("train_remote", "Line")), tostring(minetest.formspec_escape((t_3_auto.line or "")))}, ";")), string.format("%s[%s]", "button", table.concat({"10,8;1,1;lnset", tostring(minetest.translate("train_remote", "Set"))}, ";")), string.format("%s[%s]", "field", table.concat({"0.5,9.5;9.5,1;rctext", tostring(minetest.translate("train_remote", "Routing code")), tostring(minetest.formspec_escape((t_3_auto.routingcode or "")))}, ";")), string.format("%s[%s]", "button", table.concat({"10,9.5;1,1;rcset", tostring(minetest.translate("train_remote", "Set"))}, ";"))}, " ")
minetest.show_formspec(pn_1_auto, ("train_remote:t" .. tid_2_auto), fs_4_auto)
end
end
return nil
end
end
return minetest.register_chatcommand("train_remote", {description = minetest.translate("train_remote", "Remotely control the train with the given ID"), func = _1_, params = "<ID>"})
return minetest.register_chatcommand("train_remote", {description = minetest.translate("train_remote", "Remotely control the train with the given ID"), func = _37_, params = "<ID>"})

171
main.fnl
View File

@ -1,103 +1,92 @@
(local m {})
(require-macros :helpers)
(require-macros :formspec)
(local fsheader "formspec_version[3]")
(local fstext-train "%s
size[11.5,11]
label[0.5,0.75;%s]
label[0.5,1.25;%s]
button[6,0.5;4,1;update;%s]
button_exit[10,0.5;1,1;close-formspec;X]
label[1.25,2;%s]label[1.25,2.5;%s]label[1.25,3;%s]label[1.25,3.5;%s]label[1.25,4;%s]
scrollbaroptions[min=1;max=5;smallstep=1;largestep=1;thumbsize=1;arrows=hide]
scrollbar[0.5,1.75;0.5,2.5;vertical;lever;%d]
label[6,2;%s]scrollbaroptions[min=1;max=3;smallstep=1;largestep=1;thumbsize=1;arrows=hide]
scrollbar[8.5,1.75;2.5,0.5;horizontal;door;%d]
checkbox[6,2.75;ars;%s;%s]
button[6,3.25;5,1;reverse;%s]
field[0.5,5;9.5,1;outtext;%s;%s]
button[10,5;1,1;outset;%s]
field[0.5,6.5;9.5,1;intext;%s;%s]
button[10,6.5;1,1;inset;%s]
field[0.5,8;9.5,1;lntext;%s;%s]
button[10,8;1,1;lnset;%s]
field[0.5,9.5;9.5,1;rctext;%s;%s]
button[10,9.5;1,1;rcset;%s]
")
(fn show-train-remote-formspec [pname trainid]
`(let [pn# ,pname tid# ,trainid]
(if (not (check-train-access pn# tid#)) nil
(let [t# (. advtrains.trains tid#)]
(local fs# (fsformat ,fstext-train
,fsheader
["Remote control for @1" tid#]
["Wagon(s): @1" (length t#.trainparts)]
["Update"]
["Accelerate"]
["Neutral"]
["Roll"]
["Regular brake"]
["Emergency brake"]
(- 5 (leverof t#))
["Door control"]
(+ (or t#.door_open 0) 2)
["Automatic routesetting"]
(if (and advtrains.interlocking t#.ars_disable) "false" "true")
["Reverse train"]
["External display"]
(minetest.formspec_escape (or t#.text_outside ""))
["Set"]
["Internal display"]
(minetest.formspec_escape (or t#.text_inside ""))
["Set"]
["Line"]
(minetest.formspec_escape (or t#.line ""))
["Set"]
["Routing code"]
(minetest.formspec_escape (or t#.routingcode ""))
["Set"]))
(minetest.show_formspec pn# (.. "train_remote:t" tid#) fs#)))))
`(let [pn# ,pname tid# ,trainid]
(if (not (check-train-access pn# tid#)) nil
(let [t# (. advtrains.trains tid#)]
(local fs# (formspec
,fsheader
[:size [11.5 11]]
[:label [0.5 0.75] (S "Remote control for @1" tid#)]
[:label [0.5 1.25] (S "Wagon(s): @1" (length t#.trainparts))]
[:button [6 0.5] [4 1] :update (S "Update")]
[:button_exit [10 0.5] [1 1] "close-formspec" "X"]
[:label [1.25 2] (S "Accelerate")]
[:label [1.25 2.5] (S "Neutral")]
[:label [1.25 3] (S "Roll")]
[:label [1.25 3.5] (S "Regular brake")]
[:label [1.25 4] (S "Emergency brake")]
[:scrollbaroptions
"min=1" "max=5"
"smallstep=1" "largestep=1"
"thumbsize=1" "arrows=hide"]
[:scrollbar [0.5 1.75] [0.5 2.5] :vertical :lever (- 5 (leverof t#))]
[:label [6 2] (S "Door control")]
[:scrollbaroptions
"min=1" "max=3"
"smallstep=1" "largestep=1"
"thumbsize=1" "arrows=hide"]
[:scrollbar [8.5 1.75] [2.5 0.5] :horizontal :door (+ (or t#.door_open 0) 2)]
[:checkbox [6 2.75] :ars (S "Automatic routesetting")
(if (and advtrains.interlocking t#.ars_disable) "false" "true")]
[:button [6 3.25] [5 1] :reverse (S "Reverse train")]
[:field [0.5 5] [9.5 1] :outtext (S "External display")
(minetest.formspec_escape (or t#.text_outside ""))]
[:button [10 5] [1 1] :outset (S "Set")]
[:field [0.5 6.5] [9.5 1] :intext (S "Internal display")
(minetest.formspec_escape (or t#.text_inside ""))]
[:button [10 6.5] [1 1] :inset (S "Set")]
[:field [0.5 8] [9.5 1] :lntext (S "Line")
(minetest.formspec_escape (or t#.line ""))]
[:button [10 8] [1 1] :lnset (S "Set")]
[:field [0.5 9.5] [9.5 1] :rctext (S "Routing code")
(minetest.formspec_escape (or t#.routingcode ""))]
[:button [10 9.5] [1 1] :rcset (S "Set")]
))
(minetest.show_formspec pn# (.. "train_remote:t" tid#) fs#)))))
(fn handle-train-control-fields [trainid pname fields]
`(let [trainid# ,trainid pname# ,pname fields# ,fields]
(if (not trainid#) nil
(not (check-train-access pname# trainid#)) nil
fields#.close-formspec nil
(minetest.is_yes fields#.quit) nil
(let [t# (. advtrains.trains trainid#)]
(if (has-scrollbar-event fields#.door)
(let [dside# (get-scrollbar fields#.door 1 3)]
(if dside# (tset t# :door_open (- dside# 2))))
(has-scrollbar-event fields#.lever)
(let [tlev# (get-scrollbar fields#.lever 1 5)]
(if tlev# (tset t# :ctrl_user (- 5 tlev#))))
fields#.reverse
(when (<= t#.velocity 0)
(advtrains.invert_train trainid#)
(advtrains.atc.train_reset_command t#))
fields#.ars
(if advtrains.interlocking
(advtrains.interlocking.ars_set_disable
t# (not (minetest.is_yes fields#.ars)))))
(when fields#.outtext
(tset t# :text_outside fields#.outtext))
(when fields#.intext
(tset t# :text_inside fields#.intext))
(when fields#.lntext
(tset t# :line fields#.lntext)
(minetest.after 0 advtrains.invalidate_path trainid#))
(when fields#.rctext
(tset t# :routingcode fields#.rctext)
(minetest.after 0 advtrains.invalidate_path trainid#))
(show-train-remote-formspec pname# trainid#)
nil))))
`(let [trainid# ,trainid pname# ,pname fields# ,fields]
(if (not trainid#) nil
(not (check-train-access pname# trainid#)) nil
fields#.close-formspec nil
(minetest.is_yes fields#.quit) nil
(let [t# (. advtrains.trains trainid#)]
(if (has-scrollbar-event fields#.door)
(let [dside# (get-scrollbar fields#.door 1 3)]
(if dside# (tset t# :door_open (- dside# 2))))
(has-scrollbar-event fields#.lever)
(let [tlev# (get-scrollbar fields#.lever 1 5)]
(if tlev# (tset t# :ctrl_user (- 5 tlev#))))
fields#.reverse
(when (<= t#.velocity 0)
(advtrains.invert_train trainid#)
(advtrains.atc.train_reset_command t#))
fields#.ars
(if advtrains.interlocking
(advtrains.interlocking.ars_set_disable
t# (not (minetest.is_yes fields#.ars)))))
(when fields#.outtext
(tset t# :text_outside fields#.outtext))
(when fields#.intext
(tset t# :text_inside fields#.intext))
(when fields#.lntext
(tset t# :line fields#.lntext)
(minetest.after 0 advtrains.invalidate_path trainid#))
(when fields#.rctext
(tset t# :routingcode fields#.rctext)
(minetest.after 0 advtrains.invalidate_path trainid#))
(show-train-remote-formspec pname# trainid#)
nil))))
;; Stub
(fn handle-wagon-control-fields [wagonid pname fields]
`(let [wagonid# ,wagonid pname# ,pname fields# ,fields] nil))
`(let [wagonid# ,wagonid pname# ,pname fields# ,fields] nil))
{:
show-train-remote-formspec
{: show-train-remote-formspec
: handle-train-control-fields
: handle-wagon-control-fields}
: handle-wagon-control-fields
}