ShadowNinja
2013-12-17 0ea1bd1fa2a4a540927b1e31ad5ca2be10e2c4a0
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 = {
10         {'group:wood',      'group:wood',           'group:wood'},
11         {'default:diamond', 'default:diamond',      'default:diamond'},
12         {'default:stone',   'default:copper_ingot', 'default:stone'},
13     }
14 })
15
0ea1bd 16 local music_handles = {}
S 17
ee0765 18 local music_player_formspec =
S 19     "invsize[8,9;]"..
be2f30 20     "label[0,0;"..S("Music Player").."]"..
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]"..
32     "label[4,0;Current track --]"
33
0ea1bd 34 local function play_track(pos, track)
S 35     return minetest.sound_play("technic_track"..track,
36             {pos = pos, gain = 1.0, loop = true, max_hear_distance = 72,})
37 end
38
ee0765 39 minetest.register_node("technic:music_player", {
be2f30 40     description = S("Music Player"),
ee0765 41     tiles = {"technic_music_player_top.png", "technic_machine_bottom.png", "technic_music_player_side.png",
S 42              "technic_music_player_side.png", "technic_music_player_side.png", "technic_music_player_side.png"},
43     groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
44     sounds = default.node_sound_wood_defaults(),
45     on_construct = function(pos)
46         local meta = minetest.get_meta(pos)
be2f30 47         meta:set_string("infotext", S("Music Player"))
ee0765 48         meta:set_int("active", 0)
S 49         meta:set_int("current_track", 1)
50         meta:set_string("formspec", music_player_formspec)
51     end,
52     on_receive_fields = function(pos, formanme, fields, sender)
53         local meta          = minetest.get_meta(pos)
0ea1bd 54         local pos_hash      = minetest.hash_node_position(pos)
S 55         local music_handle  = music_handles[pos_hash]
ee0765 56         local current_track = meta:get_int("current_track")
S 57         if fields.track1 then current_track = 1 end
58         if fields.track2 then current_track = 2 end
59         if fields.track3 then current_track = 3 end
60         if fields.track4 then current_track = 4 end
61         if fields.track5 then current_track = 5 end
62         if fields.track6 then current_track = 6 end
63         if fields.track7 then current_track = 7 end
64         if fields.track8 then current_track = 8 end
65         if fields.track9 then current_track = 9 end
66         meta:set_int("current_track", current_track)
67         meta:set_string("formspec",
68                 "invsize[8,9;]"..
be2f30 69                 "label[0,0;"..S("Music Player").."]"..
ee0765 70                 "button[4,1;1,1;track1;1]"..
S 71                 "button[5,1;1,1;track2;2]"..
72                 "button[6,1;1,1;track3;3]"..
73                 "button[4,2;1,1;track4;4]"..
74                 "button[5,2;1,1;track5;5]"..
75                 "button[6,2;1,1;track6;6]"..
76                 "button[4,3;1,1;track7;7]"..
77                 "button[5,3;1,1;track8;8]"..
78                 "button[6,3;1,1;track9;9]"..
79                 "button[4,4;1,2;play;Play]"..
80                 "button[6,4;1,2;stop;Stop]"..
81                 "label[4,0;Current track "
82                 ..current_track.."]")
83         if fields.play then
84             if music_handle then
85                 minetest.sound_stop(music_handle)
86             end
0ea1bd 87             music_handle = play_track(pos, current_track)
ee0765 88             meta:set_int("active", 1)
S 89         end
90         if fields.stop then
91             meta:set_int("active", 0)
92             if music_handle then
93                 minetest.sound_stop(music_handle)
94             end
95         end
0ea1bd 96         music_handles[pos_hash] = music_handle
ee0765 97     end,
S 98 })
99
100 minetest.register_abm({
101     nodenames = {"technic:music_player"},
102     interval = 1,
103     chance   = 1,
104     action = function(pos, node, active_object_count, active_object_count_wider)
105         local meta         = minetest.get_meta(pos)
106         local eu_input     = meta:get_int("LV_EU_input")
be2f30 107         local machine_name = S("Music Player")
ee0765 108         local machine_node = "technic:music_player"
S 109         local demand       = 150
110
111         local current_track = meta:get_int("current_track")
0ea1bd 112         local pos_hash      = minetest.hash_node_position(pos)
S 113         local music_handle  = music_handles[pos_hash]
ee0765 114
S 115         -- Setup meta data if it does not exist.
116         if not eu_input then
117             meta:set_int("LV_EU_demand", demand)
118             meta:set_int("LV_EU_input", 0)
119             return
120         end
121
122         -- Power off automatically if no longer connected to a switching station
123         technic.switching_station_timeout_count(pos, "LV")
124
125         if meta:get_int("active") == 0 then
be2f30 126             meta:set_string("infotext", S("%s Idle"):format(machine_name))
ee0765 127             meta:set_int("LV_EU_demand", 0)
S 128             return
129         end
130
131         if eu_input < demand then
be2f30 132             meta:set_string("infotext", S("%s Unpowered"):format(machine_name))
ee0765 133             if music_handle then
S 134                 minetest.sound_stop(music_handle)
0ea1bd 135                 music_handle = nil
ee0765 136             end
S 137         elseif eu_input >= demand then
be2f30 138             meta:set_string("infotext", S("%s Active"):format(machine_name))
0ea1bd 139             if not music_handle then
S 140                 music_handle = play_track(pos, current_track)
141             end
ee0765 142         end
0ea1bd 143         music_handles[pos_hash] = music_handle
ee0765 144         meta:set_int("LV_EU_demand", demand)
S 145     end
146 })
147
148 technic.register_machine("LV", "technic:music_player", technic.receiver)
149