Zefram
2014-08-14 1d0687556a52891aeadc0e8d9a58e44c53cb826b
commit | author | age
ee0765 1 -- LV Music player.
S 2 -- The player can play music. But it is high ampage!
3
be2f30 4 local S = technic.getter
S 5
ee0765 6 minetest.register_alias("music_player", "technic:music_player")
S 7 minetest.register_craft({
8     output = 'technic:music_player',
9     recipe = {
5e4a87 10         {'technic:chromium_ingot', 'default:diamond',        'technic:chromium_ingot'},
Z 11         {'default:diamond',        'technic:machine_casing', 'default:diamond'},
12         {'default:mossycobble',    'technic:lv_cable0',      'default:mossycobble'},
ee0765 13     }
S 14 })
15
0ea1bd 16 local music_handles = {}
S 17
ee0765 18 local music_player_formspec =
S 19     "invsize[8,9;]"..
7c4b70 20     "label[0,0;"..S("%s Music Player"):format("LV").."]"..
ee0765 21     "button[4,1;1,1;track1;1]"..
S 22     "button[5,1;1,1;track2;2]"..
23     "button[6,1;1,1;track3;3]"..
24     "button[4,2;1,1;track4;4]"..
25     "button[5,2;1,1;track5;5]"..
26     "button[6,2;1,1;track6;6]"..
27     "button[4,3;1,1;track7;7]"..
28     "button[5,3;1,1;track8;8]"..
29     "button[6,3;1,1;track9;9]"..
30     "button[4,4;1,2;play;Play]"..
31     "button[6,4;1,2;stop;Stop]"..
39c41a 32     "label[4,0;"..S("Current track %s"):format("--").."]"
ee0765 33
0ea1bd 34 local function play_track(pos, track)
ebc114 35     return minetest.sound_play("technic_track"..tostring(track),
0ea1bd 36             {pos = pos, gain = 1.0, loop = true, max_hear_distance = 72,})
S 37 end
38
563a4c 39 local run = function(pos, node)
N 40     local meta         = minetest.get_meta(pos)
41     local eu_input     = meta:get_int("LV_EU_input")
42     local machine_name = S("%s Music Player"):format("LV")
43     local machine_node = "technic:music_player"
44     local demand       = 150
45
46     local current_track = meta:get_int("current_track")
47     local pos_hash      = minetest.hash_node_position(pos)
48     local music_handle  = music_handles[pos_hash]
49
50     -- Setup meta data if it does not exist.
51     if not eu_input then
52         meta:set_int("LV_EU_demand", demand)
53         meta:set_int("LV_EU_input", 0)
54         return
55     end
56
57     if meta:get_int("active") == 0 then
58         meta:set_string("infotext", S("%s Idle"):format(machine_name))
59         meta:set_int("LV_EU_demand", 0)
60         return
61     end
62
63     if eu_input < demand then
64         meta:set_string("infotext", S("%s Unpowered"):format(machine_name))
65         if music_handle then
66             minetest.sound_stop(music_handle)
67             music_handle = nil
68         end
69     elseif eu_input >= demand then
70         meta:set_string("infotext", S("%s Active"):format(machine_name))
71         if not music_handle then
72             music_handle = play_track(pos, current_track)
73         end
74     end
75     music_handles[pos_hash] = music_handle
76     meta:set_int("LV_EU_demand", demand)
77 end
78
1d0687 79 local function stop_player(pos, node)
Z 80     local pos_hash = minetest.hash_node_position(pos)
81     local music_handle = music_handles[pos_hash]
82     if music_handle then
83         minetest.sound_stop(music_handle)
84         music_handles[pos_hash] = nil
85     end
86 end
87
ee0765 88 minetest.register_node("technic:music_player", {
7c4b70 89     description = S("%s Music Player"):format("LV"),
ee0765 90     tiles = {"technic_music_player_top.png", "technic_machine_bottom.png", "technic_music_player_side.png",
S 91              "technic_music_player_side.png", "technic_music_player_side.png", "technic_music_player_side.png"},
563a4c 92     groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, technic_machine=1},
ee0765 93     sounds = default.node_sound_wood_defaults(),
S 94     on_construct = function(pos)
95         local meta = minetest.get_meta(pos)
7c4b70 96         meta:set_string("infotext", S("%s Music Player"):format("LV"))
ee0765 97         meta:set_int("active", 0)
S 98         meta:set_int("current_track", 1)
99         meta:set_string("formspec", music_player_formspec)
100     end,
101     on_receive_fields = function(pos, formanme, fields, sender)
102         local meta          = minetest.get_meta(pos)
103         local current_track = meta:get_int("current_track")
104         if fields.track1 then current_track = 1 end
105         if fields.track2 then current_track = 2 end
106         if fields.track3 then current_track = 3 end
107         if fields.track4 then current_track = 4 end
108         if fields.track5 then current_track = 5 end
109         if fields.track6 then current_track = 6 end
110         if fields.track7 then current_track = 7 end
111         if fields.track8 then current_track = 8 end
112         if fields.track9 then current_track = 9 end
113         meta:set_int("current_track", current_track)
114         meta:set_string("formspec",
115                 "invsize[8,9;]"..
7c4b70 116                 "label[0,0;"..S("%s Music Player"):format("LV").."]"..
ee0765 117                 "button[4,1;1,1;track1;1]"..
S 118                 "button[5,1;1,1;track2;2]"..
119                 "button[6,1;1,1;track3;3]"..
120                 "button[4,2;1,1;track4;4]"..
121                 "button[5,2;1,1;track5;5]"..
122                 "button[6,2;1,1;track6;6]"..
123                 "button[4,3;1,1;track7;7]"..
124                 "button[5,3;1,1;track8;8]"..
125                 "button[6,3;1,1;track9;9]"..
126                 "button[4,4;1,2;play;Play]"..
127                 "button[6,4;1,2;stop;Stop]"..
39c41a 128                 "label[4,0;"..S("Current track %s")
X 129                     :format(current_track).."]")
1d0687 130         if fields.play or fields.stop then
Z 131             stop_player(pos)
132             meta:set_int("active", fields.play and 1 or 0)
ee0765 133         end
S 134     end,
1d0687 135     on_destruct = stop_player,
563a4c 136     technic_run = run,
1d0687 137     technic_on_disable = stop_player,
ee0765 138 })
S 139
140 technic.register_machine("LV", "technic:music_player", technic.receiver)
141