ShadowNinja
2013-07-11 5d470cd753efe8f4640099165a7bfc0c6e181c35
commit | author | age
ee5c6c 1 -- LV Music player.
K 2 -- The playe can play music. But it is high ampage!
82cba9 3 minetest.register_alias("music_player", "technic:music_player")
R 4 minetest.register_craft({
5     output = 'technic:music_player',
6     recipe = {
3a3700 7         {'default:wood', 'default:wood', 'default:wood'},
R 8         {'default:diamond', 'default:diamond', 'default:diamond'},
9         {'default:stone', 'default:copper_ingot', 'default:stone'},
82cba9 10     }
R 11 })
12
13 minetest.register_craftitem("technic:music_player", {
14     description = "Music Player",
15     stack_max = 99,
16 })
17
ee5c6c 18 local music_player_formspec =
K 19    "invsize[8,9;]"..
20    "label[0,0;Music Player]"..
21    "button[4,1;1,1;track1;1]"..
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 --]"
82cba9 33
ee5c6c 34 minetest.register_node(
K 35    "technic:music_player",
36    {
37       description = "Music Player",
38       tiles = {"technic_music_player_top.png", "technic_machine_bottom.png", "technic_music_player_side.png",
39            "technic_music_player_side.png", "technic_music_player_side.png", "technic_music_player_side.png"},
40       groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
41       sounds = default.node_sound_wood_defaults(),
42       on_construct = function(pos)
43             local meta = minetest.env:get_meta(pos)
44             meta:set_string("infotext", "Music Player")
45             meta:set_float("technic_power_machine", 1)
46             meta:set_int("active",     0) -- Is the device on?
47             meta:set_int("music_player_current_track", 1)
48             meta:set_string("formspec", music_player_formspec)
49              end,
50       on_receive_fields = function(pos, formanme, fields, sender)
51                  local meta                 = minetest.env:get_meta(pos)
52                  music_handle               = meta:get_int("music_handle")
53                  music_player_current_track = meta:get_int("music_player_current_track")
54                  if fields.track1 then music_player_current_track = 1 end
55                  if fields.track2 then music_player_current_track = 2 end
56                  if fields.track3 then music_player_current_track = 3 end
57                  if fields.track4 then music_player_current_track = 4 end
58                  if fields.track5 then music_player_current_track = 5 end
59                  if fields.track6 then music_player_current_track = 6 end
60                  if fields.track7 then music_player_current_track = 7 end
61                  if fields.track8 then music_player_current_track = 8 end
62                  if fields.track9 then music_player_current_track = 9 end
63                  meta:set_int("music_player_current_track",music_player_current_track)
64                  if fields.play and meta:get_int("active") == 0 then
65                 if music_handle then minetest.sound_stop(music_handle) end
66                 music_handle = minetest.sound_play("technic_track"..music_player_current_track, {pos = pos, gain = 1.0,loop = true, max_hear_distance = 72,})
67                 meta:set_int("active",1)
68                  end
69                  if fields.stop then
70                 meta:set_int("active",0)
71                 if music_handle then minetest.sound_stop(music_handle) end
72                  end
73                  meta:set_int("music_handle",music_handle)
74               end,
75    })
76
77 minetest.register_abm(
78    { nodenames = {"technic:music_player"},
79      interval = 1,
80      chance   = 1,
81      action = function(pos, node, active_object_count, active_object_count_wider)
82          local meta         = minetest.env:get_meta(pos)
83          local eu_input     = meta:get_int("LV_EU_input")
84          local state        = meta:get_int("state")
85          local next_state   = state
86
87          -- Machine information
88          local machine_name         = "Music Player"
89          local machine_node         = "technic:music_player"
90          local machine_state_demand = { 10, 150 }
91              
92          local music_handle         = meta:get_int("music_handle")
93
94          -- Setup meta data if it does not exist. state is used as an indicator of this
95          if state == 0 then
96             meta:set_int("state", 1)
97             meta:set_int("LV_EU_demand", machine_state_demand[1])
98             meta:set_int("LV_EU_input", 0)
99             return
100          end
101              
102          -- Power off automatically if no longer connected to a switching station
103          technic.switching_station_timeout_count(pos, "LV")
104              
105          -- State machine
106          if eu_input == 0 then
107             -- unpowered - go idle
108             -- hacky_swap_node(pos, machine_node) -- if someday two nodes for this
109             meta:set_string("infotext", machine_name.." Unpowered")
110             next_state = 1
111          elseif eu_input == machine_state_demand[state] then
112             -- Powered - do the state specific actions
113             if state == 1 then
114                -- hacky_swap_node(pos, machine_node) -- if someday two nodes for this
115                meta:set_string("infotext", machine_name.." Idle")
116
117                if meta:get_int("active") == 1 then
118               next_state = 2
119                end
120
121             elseif state == 2 then
122                -- hacky_swap_node(pos, machine_node.."_active") -- if someday two nodes for this
123                meta:set_string("infotext", machine_name.." Active")
124
125                music_player_current_track=meta:get_int("music_player_current_track")
126                meta:set_string("formspec",
127                        "invsize[8,9;]"..
128                       "label[0,0;Music Player]"..
129                       "button[4,1;1,1;track1;1]"..
130                       "button[5,1;1,1;track2;2]"..
131                       "button[6,1;1,1;track3;3]"..
132                       "button[4,2;1,1;track4;4]"..
133                       "button[5,2;1,1;track5;5]"..
134                       "button[6,2;1,1;track6;6]"..
135                       "button[4,3;1,1;track7;7]"..
136                       "button[5,3;1,1;track8;8]"..
137                       "button[6,3;1,1;track9;9]"..
138                       "button[4,4;1,2;play;Play]"..
139                       "button[6,4;1,2;stop;Stop]"..
140                       "label[4,0;Current track "..tostring(music_player_current_track).."]"
141                     )
142                if meta:get_int("active") == 0 then
143               if music_handle then minetest.sound_stop(music_handle) end
144               next_state = 1
145                end
146             end
147          end
148          -- Change state?
149          if next_state ~= state then
150             meta:set_int("LV_EU_demand", machine_state_demand[next_state])
151             meta:set_int("state", next_state)
152          end
153           end
154    })
155
156 technic.register_LV_machine ("technic:music_player","RE")