Zefram
2014-07-30 12d0c6522bbca906910aae0321cbaa7eb48db8c2
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
ee0765 79 minetest.register_node("technic:music_player", {
7c4b70 80     description = S("%s Music Player"):format("LV"),
ee0765 81     tiles = {"technic_music_player_top.png", "technic_machine_bottom.png", "technic_music_player_side.png",
S 82              "technic_music_player_side.png", "technic_music_player_side.png", "technic_music_player_side.png"},
563a4c 83     groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, technic_machine=1},
ee0765 84     sounds = default.node_sound_wood_defaults(),
S 85     on_construct = function(pos)
86         local meta = minetest.get_meta(pos)
7c4b70 87         meta:set_string("infotext", S("%s Music Player"):format("LV"))
ee0765 88         meta:set_int("active", 0)
S 89         meta:set_int("current_track", 1)
90         meta:set_string("formspec", music_player_formspec)
91     end,
92     on_receive_fields = function(pos, formanme, fields, sender)
93         local meta          = minetest.get_meta(pos)
0ea1bd 94         local pos_hash      = minetest.hash_node_position(pos)
S 95         local music_handle  = music_handles[pos_hash]
ee0765 96         local current_track = meta:get_int("current_track")
S 97         if fields.track1 then current_track = 1 end
98         if fields.track2 then current_track = 2 end
99         if fields.track3 then current_track = 3 end
100         if fields.track4 then current_track = 4 end
101         if fields.track5 then current_track = 5 end
102         if fields.track6 then current_track = 6 end
103         if fields.track7 then current_track = 7 end
104         if fields.track8 then current_track = 8 end
105         if fields.track9 then current_track = 9 end
106         meta:set_int("current_track", current_track)
107         meta:set_string("formspec",
108                 "invsize[8,9;]"..
7c4b70 109                 "label[0,0;"..S("%s Music Player"):format("LV").."]"..
ee0765 110                 "button[4,1;1,1;track1;1]"..
S 111                 "button[5,1;1,1;track2;2]"..
112                 "button[6,1;1,1;track3;3]"..
113                 "button[4,2;1,1;track4;4]"..
114                 "button[5,2;1,1;track5;5]"..
115                 "button[6,2;1,1;track6;6]"..
116                 "button[4,3;1,1;track7;7]"..
117                 "button[5,3;1,1;track8;8]"..
118                 "button[6,3;1,1;track9;9]"..
119                 "button[4,4;1,2;play;Play]"..
120                 "button[6,4;1,2;stop;Stop]"..
39c41a 121                 "label[4,0;"..S("Current track %s")
X 122                     :format(current_track).."]")
ee0765 123         if fields.play then
S 124             if music_handle then
125                 minetest.sound_stop(music_handle)
126             end
0ea1bd 127             music_handle = play_track(pos, current_track)
ee0765 128             meta:set_int("active", 1)
S 129         end
130         if fields.stop then
131             meta:set_int("active", 0)
132             if music_handle then
133                 minetest.sound_stop(music_handle)
134             end
135         end
0ea1bd 136         music_handles[pos_hash] = music_handle
ee0765 137     end,
563a4c 138     technic_run = run,
ee0765 139 })
S 140
141 technic.register_machine("LV", "technic:music_player", technic.receiver)
142