increase couple validity radius
[advtrains.git] / trainhud.lua
1 --trainhud.lua: holds all the code for train controlling
2
3 advtrains.hud = {}
4
5 minetest.register_on_leaveplayer(function(player)
6 advtrains.hud[player:get_player_name()] = nil
7 end)
8
9 local mletter={[1]="F", [-1]="R", [0]="N"}
10
11 function advtrains.on_control_change(pc, train, flip)
12 if pc.sneak then
13 if pc.up then
14 train.tarvelocity = advtrains.all_traintypes[train.traintype].max_speed or 10
15 end
16 if pc.down then
17 train.tarvelocity = 0
18 end
19 if pc.left then
20 train.tarvelocity = 4
21 end
22 if pc.right then
23 train.tarvelocity = 8
24 end
25 if pc.jump then
26 train.brake = true
27 --0: released, 1: brake and pressed, 2: released and brake, 3: pressed and brake
28 if not train.brake_hold_state or train.brake_hold_state==0 then
29 train.brake_hold_state = 1
30 elseif train.brake_hold_state==2 then
31 train.brake_hold_state = 3
32 end
33 elseif train.brake_hold_state==1 then
34 train.brake_hold_state = 2
35 elseif train.brake_hold_state==3 then
36 train.brake = false
37 train.brake_hold_state = 0
38 end
39 --shift+use:see wagons.lua
40 else
41 if pc.up then
42 train.tarvelocity = train.tarvelocity + 1
43 end
44 if pc.down then
45 if train.velocity>0 then
46 train.tarvelocity = math.max(train.tarvelocity - 1, 0)
47 else
48 train.movedir = -train.movedir
49 end
50 end
51 if train.brake_hold_state~=2 then
52 train.brake = false
53 end
54 if pc.jump then
55 train.brake = true
56 end
57 if pc.aux1 then
58 --horn
59 end
60 end
61 end
62 function advtrains.update_driver_hud(pname, train, flip)
63 advtrains.set_trainhud(pname, advtrains.hud_train_format(train, flip))
64 end
65 function advtrains.clear_driver_hud(pname)
66 advtrains.set_trainhud(pname, "")
67 end
68
69 function advtrains.set_trainhud(name, text)
70 local hud = advtrains.hud[name]
71 local player=minetest.get_player_by_name(name)
72 if not player then
73 return
74 end
75 if not hud then
76 hud = {}
77 advtrains.hud[name] = hud
78 hud.id = player:hud_add({
79 hud_elem_type = "text",
80 name = "ADVTRAINS",
81 number = 0xFFFFFF,
82 position = {x=0.5, y=0.7},
83 offset = {x=0, y=0},
84 text = text,
85 scale = {x=200, y=60},
86 alignment = {x=0, y=0},
87 })
88 hud.oldText=text
89 return
90 elseif hud.oldText ~= text then
91 player:hud_change(hud.id, "text", text)
92 hud.oldText=text
93 end
94 end
95 function advtrains.hud_train_format(train, flip)
96 local fct=flip and -1 or 1
97 if not train or not train.traintype then return "" end
98
99 local max=advtrains.all_traintypes[train.traintype].max_speed or 10
100 local vel=advtrains.abs_ceil(train.velocity)
101 local tvel=advtrains.abs_ceil(train.tarvelocity)
102 local topLine, firstLine, secondLine
103
104 topLine=train.traintype.." ["..mletter[fct*train.movedir].."] "..(train.brake and "="..( train.brake_hold_state==2 and "^" or "" ).."B=" or "")
105 firstLine="Speed: |"..string.rep("+", vel)..string.rep("_", max-vel)..">"
106 secondLine="Target: |"..string.rep("+", tvel)..string.rep("_", max-tvel)..">"
107
108 return topLine.."\n"..firstLine.."\n"..secondLine
109 end