local modname = minetest.get_current_modname() local function trans(st,...) return minetest.translate(modname,st,...) end local function fs_t(st,...) return minetest.formspec_escape(trans(st,...)) end local function show_formspec(itemstack,pname) local formspec = "" formspec = formspec.."formspec_version[3]" formspec = formspec.."size[10.5,7.5]" formspec = formspec.."label[0.2,0.4;Advanced Trains Remote Control v1]" formspec = formspec.."button_exit[6.6,0.2;3.7,0.8;bind_train;Bind To New Train]" local train_id = itemstack:get_meta():get("bound_train") if not train_id or not advtrains.trains[train_id] then formspec = formspec.."label[0.2,1.2;Remote Control: Not Bound]" formspec = formspec.."label[0.2,1.6;Punch a train with the controller to bind it]" else formspec = formspec.."label[0.2,1.2;Remote Control: Train %s]" formspec = formspec.."label[0.2,1.6;Train Length: %s wagon(s)]" formspec = formspec.."label[0.2,2;Train Length: %sm]" formspec = formspec.."textarea[0.2,2.9;5.8,0.8;ext_disp;External Display;%s]" formspec = formspec.."textarea[0.2,4.1;5.8,0.8;int_disp;Internal Display;%s]" formspec = formspec.."field[0.2,6.5;2.8,0.8;line;Line;%s]" formspec = formspec.."field[0.2,5.3;5.8,0.8;rc;Routing Code;%s]" formspec = formspec.."button[3.2,6.5;2.8,0.8;update_info;Update Train Info]" formspec = formspec.."label[6.6,1.6;Maximum Speed: %s m/s]" formspec = formspec.."field[6.6,2.9;1.9,0.8;speed_input;Set Speed;%d]" formspec = formspec.."button[8.8,2.9;1.5,0.8;send_speed;Confirm]" formspec = formspec.."field[6.6,4.1;1.9,0.8;atc_input;Send ATC Command;]" formspec = formspec.."button[8.8,4.1;1.5,0.8;send_atc;Send]" formspec = formspec.."button[6.6,5.3;3.7,0.8;reverse;Reverse]" formspec = formspec.."label[6.6,2;Door Control]" formspec = formspec.."scrollbaroptions[min=1;max=3;smallstep=1;largestep=1;thumbsize=1;arrows=hide]scrollbar[6.6,2.2;3.7,0.2;horizontal;door;%d]" formspec = formspec.."checkbox[6.6,6.6;ars_disable;Disable ARS;%s]" formspec = formspec.."checkbox[6.6,7.1;autocouple;Autocouple;%s]" formspec = formspec.."button[9.1,6.5;1.2,0.8;estop;STOP!]" local train = advtrains.trains[train_id] formspec = string.format(formspec, train_id, #train.trainparts, train.trainlen, train.text_outside or "", train.text_inside or "", train.line or "", train.routingcode or "", train.max_speed, train.velocity, ((train.door_open or 0) + 2), tostring(train.ars_disable), tostring(train.autocouple or train.atc_wait_autocouple) ) end minetest.show_formspec(pname,modname..":controller",formspec) end minetest.register_on_player_receive_fields(function(player,formname,fields) if formname ~= modname..":controller" then return end local pname = player:get_player_name() if not pname then return end local itemstack = player:get_wielded_item() if itemstack:get_name() ~= modname..":remote_control" then return end local item_meta = itemstack:get_meta() local train_id = item_meta:get("bound_train") if not train_id and not fields.bind_train then return end local train = advtrains.trains[train_id] local door_scroll = minetest.explode_scrollbar_event(fields.door) --[[list of fields: active fields: bind_train update_info send_speed send_atc reverse door ars_disable autocouple estop data fields: ext_disp int_disp line rc speed_input atc_input --]] --bind train if fields.bind_train then item_meta:set_string("bound_train","") item_meta:set_string("description","Unbound Controller") player:set_wielded_item(itemstack) minetest.chat_send_player(pname,trans("Punch a train with the controller to bind it")) return --update info elseif fields.update_info then train.text_outside = fields.ext_disp or "" train.text_inside = fields.int_disp or "" train.line = fields.line or "" train.routingcode = fields.rc or "" minetest.after(0, advtrains.invalidate_path, train_id) return --send speed command elseif fields.send_speed then local current_v,speed_input = train.velocity,tonumber(fields.speed_input) if not tonumber(speed_input) then minetest.chat_send_player(pname,trans("Speed must be a number")) return end local command local acc speed_input = tonumber(speed_input) if current_v == speed_input then minetest.chat_send_player(pname,trans("Speed unchanged")) return elseif current_v < speed_input then command = "S"..speed_input acc = "accelerating" elseif current_v > speed_input then command = "B"..speed_input acc = "braking" end advtrains.atc.train_set_command(train,command,true) minetest.chat_send_player(pname,trans("Train "..acc.." to "..speed_input)) return --send atc command elseif fields.send_atc then if fields.atc_input == "" then minetest.chat_send_player(pname,trans("No ATC command to send")) return end advtrains.atc.train_set_command(train,fields.atc_input,true) return -- send reverse command elseif fields.reverse then if train.velocity ~= 0 then minetest.chat_send_player(pname,trans("Please stop the train before reversing")) return end advtrains.invert_train(train_id) advtrains.train_ensure_init(train_id,train) -- advtrains.atc.train_reset_command(train) minetest.chat_send_player(pname,trans("Train reversed")) return --door control elseif door_scroll.type == "CHG" then train.door_open = door_scroll.value-2 return -- ars_disable elseif fields.ars_disable then if not advtrains.interlocking then return end advtrains.interlocking.ars_set_disable(train,minetest.is_yes(fields.ars_disable)) return -- autocouple elseif fields.autocouple == "true" then train.autocouple = true return elseif fields.autocouple == "false" then train.autocouple = nil return -- emergency stop button elseif fields.estop then advtrains.atc.train_set_command(train,"BB",true) minetest.chat_send_player(pname,trans("Emergency Brake applied")) return end end) minetest.register_craftitem(modname..":remote_control",{ description = trans("Train Controller"), short_description = trans("Unbound Train Controller"), groups = {}, inventory_image = "advtrains_rc_controller.png", wield_image = "advtrains_rc_controller.png", stack_max = 1, on_use = function(itemstack, user, pointed_thing) local pname = user:get_player_name() if not pname or pname == "" then return end if not minetest.check_player_privs(pname, {train_admin=true}) then minetest.chat_send_player(pname, "Insufficient privileges to use this!") return end local meta = itemstack:get_meta() local bound_train = meta:get_string("bound_train") -- set to nil to enable bind-mode if bound_train == "" then bound_train = nil end if pointed_thing.type == "object" then local object = pointed_thing.ref:get_luaentity() local data = advtrains.wagons[object.id] if data == nil then return end if not bound_train then -- bind to this train meta:set_string("bound_train",data.train_id) meta:set_string("description",string.format("Train Controller\nBound to %s",data.train_id)) end end show_formspec(itemstack,pname) return itemstack end, })