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