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