2016-11-24 11:25:07 -08:00
|
|
|
--trainhud.lua: holds all the code for train controlling
|
|
|
|
|
2016-05-29 11:27:30 -07:00
|
|
|
advtrains.hud = {}
|
2018-01-07 11:41:48 -08:00
|
|
|
advtrains.hhud = {}
|
2016-05-29 11:27:30 -07:00
|
|
|
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
advtrains.hud[player:get_player_name()] = nil
|
2018-01-07 11:41:48 -08:00
|
|
|
advtrains.hhud[player:get_player_name()] = nil
|
2016-05-29 11:27:30 -07:00
|
|
|
end)
|
|
|
|
|
2016-11-24 11:25:07 -08:00
|
|
|
local mletter={[1]="F", [-1]="R", [0]="N"}
|
2017-01-18 10:03:27 -08:00
|
|
|
local doorstr={[-1]="|<>| >|<", [0]=">|< >|<", [1]=">|< |<>|"}
|
2016-11-24 11:25:07 -08:00
|
|
|
|
|
|
|
function advtrains.on_control_change(pc, train, flip)
|
2017-08-01 04:45:05 -07:00
|
|
|
local maxspeed = train.max_speed or 10
|
2016-11-24 11:25:07 -08:00
|
|
|
if pc.sneak then
|
|
|
|
if pc.up then
|
2017-08-01 04:45:05 -07:00
|
|
|
train.tarvelocity = maxspeed
|
2016-11-24 11:25:07 -08:00
|
|
|
end
|
|
|
|
if pc.down then
|
|
|
|
train.tarvelocity = 0
|
|
|
|
end
|
|
|
|
if pc.left then
|
|
|
|
train.tarvelocity = 4
|
|
|
|
end
|
|
|
|
if pc.right then
|
|
|
|
train.tarvelocity = 8
|
|
|
|
end
|
|
|
|
if pc.jump then
|
|
|
|
train.brake = true
|
|
|
|
--0: released, 1: brake and pressed, 2: released and brake, 3: pressed and brake
|
|
|
|
if not train.brake_hold_state or train.brake_hold_state==0 then
|
|
|
|
train.brake_hold_state = 1
|
|
|
|
elseif train.brake_hold_state==2 then
|
|
|
|
train.brake_hold_state = 3
|
|
|
|
end
|
|
|
|
elseif train.brake_hold_state==1 then
|
|
|
|
train.brake_hold_state = 2
|
|
|
|
elseif train.brake_hold_state==3 then
|
|
|
|
train.brake = false
|
|
|
|
train.brake_hold_state = 0
|
|
|
|
end
|
2016-11-24 11:56:23 -08:00
|
|
|
--shift+use:see wagons.lua
|
2016-11-24 11:25:07 -08:00
|
|
|
else
|
2018-01-07 11:41:48 -08:00
|
|
|
local act=false
|
2016-11-24 11:25:07 -08:00
|
|
|
if pc.up then
|
2018-01-07 11:41:48 -08:00
|
|
|
train.lever=4
|
|
|
|
act=true
|
|
|
|
end
|
|
|
|
if pc.jump then
|
|
|
|
train.lever = 1
|
|
|
|
act=true
|
2016-11-24 11:25:07 -08:00
|
|
|
end
|
|
|
|
if pc.down then
|
|
|
|
if train.velocity>0 then
|
2018-01-07 11:41:48 -08:00
|
|
|
if pc.jump then
|
|
|
|
train.lever = 0
|
|
|
|
else
|
|
|
|
train.lever = 2
|
|
|
|
end
|
|
|
|
act=true
|
2016-11-24 11:25:07 -08:00
|
|
|
else
|
|
|
|
train.movedir = -train.movedir
|
|
|
|
end
|
|
|
|
end
|
2017-01-18 10:03:27 -08:00
|
|
|
if pc.left then
|
|
|
|
if train.door_open ~= 0 then
|
|
|
|
train.door_open = 0
|
|
|
|
else
|
|
|
|
train.door_open = -train.movedir
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if pc.right then
|
|
|
|
if train.door_open ~= 0 then
|
|
|
|
train.door_open = 0
|
|
|
|
else
|
|
|
|
train.door_open = train.movedir
|
|
|
|
end
|
|
|
|
end
|
2018-01-07 11:41:48 -08:00
|
|
|
train.active_control = act
|
2016-11-24 11:25:07 -08:00
|
|
|
if pc.aux1 then
|
|
|
|
--horn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
function advtrains.update_driver_hud(pname, train, flip)
|
2017-03-30 12:51:45 -07:00
|
|
|
local inside=train.text_inside or ""
|
|
|
|
advtrains.set_trainhud(pname, inside.."\n"..advtrains.hud_train_format(train, flip))
|
2016-11-24 11:25:07 -08:00
|
|
|
end
|
|
|
|
function advtrains.clear_driver_hud(pname)
|
|
|
|
advtrains.set_trainhud(pname, "")
|
|
|
|
end
|
|
|
|
|
2016-05-29 11:27:30 -07:00
|
|
|
function advtrains.set_trainhud(name, text)
|
|
|
|
local hud = advtrains.hud[name]
|
|
|
|
local player=minetest.get_player_by_name(name)
|
2016-10-05 06:27:16 -07:00
|
|
|
if not player then
|
|
|
|
return
|
|
|
|
end
|
2016-05-29 11:27:30 -07:00
|
|
|
if not hud then
|
|
|
|
hud = {}
|
|
|
|
advtrains.hud[name] = hud
|
|
|
|
hud.id = player:hud_add({
|
|
|
|
hud_elem_type = "text",
|
|
|
|
name = "ADVTRAINS",
|
|
|
|
number = 0xFFFFFF,
|
|
|
|
position = {x=0.5, y=0.7},
|
|
|
|
offset = {x=0, y=0},
|
|
|
|
text = text,
|
|
|
|
scale = {x=200, y=60},
|
|
|
|
alignment = {x=0, y=0},
|
|
|
|
})
|
|
|
|
hud.oldText=text
|
|
|
|
return
|
|
|
|
elseif hud.oldText ~= text then
|
|
|
|
player:hud_change(hud.id, "text", text)
|
|
|
|
hud.oldText=text
|
|
|
|
end
|
|
|
|
end
|
2018-01-07 11:41:48 -08:00
|
|
|
function advtrains.set_help_hud(name, text)
|
|
|
|
local hud = advtrains.hhud[name]
|
|
|
|
local player=minetest.get_player_by_name(name)
|
|
|
|
if not player then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if not hud then
|
|
|
|
hud = {}
|
|
|
|
advtrains.hhud[name] = hud
|
|
|
|
hud.id = player:hud_add({
|
|
|
|
hud_elem_type = "text",
|
|
|
|
name = "ADVTRAINS_HELP",
|
|
|
|
number = 0xFFFFFF,
|
|
|
|
position = {x=1, y=0.3},
|
|
|
|
offset = {x=0, y=0},
|
|
|
|
text = text,
|
|
|
|
scale = {x=200, y=60},
|
|
|
|
alignment = {x=1, y=0},
|
|
|
|
})
|
|
|
|
hud.oldText=text
|
|
|
|
return
|
|
|
|
elseif hud.oldText ~= text then
|
|
|
|
player:hud_change(hud.id, "text", text)
|
|
|
|
hud.oldText=text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--train.lever:
|
|
|
|
--Speed control lever in train, for new train control system.
|
|
|
|
--[[
|
|
|
|
Value Disp Control Meaning
|
|
|
|
0 BB S+Space Emergency Brake
|
|
|
|
1 B Space Normal Brake
|
|
|
|
2 - S Roll
|
|
|
|
3 o <none> Stay at speed
|
|
|
|
4 + W Accelerate
|
|
|
|
]]
|
|
|
|
|
2016-05-29 11:27:30 -07:00
|
|
|
function advtrains.hud_train_format(train, flip)
|
2016-11-24 11:25:07 -08:00
|
|
|
local fct=flip and -1 or 1
|
2016-12-22 09:55:10 -08:00
|
|
|
if not train then return "" end
|
2016-11-24 11:25:07 -08:00
|
|
|
|
2016-12-22 09:55:10 -08:00
|
|
|
local max=train.max_speed or 10
|
2016-11-24 11:25:07 -08:00
|
|
|
local vel=advtrains.abs_ceil(train.velocity)
|
|
|
|
local tvel=advtrains.abs_ceil(train.tarvelocity)
|
2017-07-15 08:04:08 -07:00
|
|
|
local vel_kmh=advtrains.abs_ceil(advtrains.ms_to_kmh(train.velocity))
|
|
|
|
local tvel_kmh=advtrains.abs_ceil(advtrains.ms_to_kmh(train.tarvelocity))
|
2018-01-07 11:41:48 -08:00
|
|
|
|
|
|
|
local levers = "B - o +"
|
|
|
|
local tlev=train.lever
|
|
|
|
if train.velocity==0 and not train.active_control then tlev=1 end
|
|
|
|
if tlev == 0 then levers = ">BB< - o +" end
|
|
|
|
if tlev == 1 then levers = ">B< - o +" end
|
|
|
|
if tlev == 2 then levers = "B >-< o +" end
|
|
|
|
if tlev == 3 then levers = "B - >o< +" end
|
|
|
|
if tlev == 4 then levers = "B - o >+<" end
|
|
|
|
|
2016-11-24 11:25:07 -08:00
|
|
|
local topLine, firstLine, secondLine
|
|
|
|
|
2018-01-07 11:41:48 -08:00
|
|
|
topLine=" ["..mletter[fct*train.movedir].."] {"..levers.."} "..doorstr[(train.door_open or 0) * train.movedir]
|
2017-07-15 08:04:08 -07:00
|
|
|
firstLine=attrans("Speed:").." |"..string.rep("+", vel)..string.rep("_", max-vel).."> "..vel_kmh.." km/h"
|
|
|
|
secondLine=attrans("Target:").." |"..string.rep("+", tvel)..string.rep("_", max-tvel).."> "..tvel_kmh.." km/h"
|
2016-11-24 11:25:07 -08:00
|
|
|
|
|
|
|
return topLine.."\n"..firstLine.."\n"..secondLine
|
2016-10-05 06:27:16 -07:00
|
|
|
end
|