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