Zefram
2014-04-28 99fd5dfee5a70b0656e40787168d2c67ba417738
commit | author | age
dd2962 1 -- Original code comes from walkin_light mod by Echo
S 2 -- http://minetest.net/forum/viewtopic.php?id=2621
82cba9 3
ee0765 4 local flashlight_max_charge = 30000
be2f30 5
S 6 local S = technic.getter
7
ee0765 8 technic.register_power_tool("technic:flashlight", flashlight_max_charge)
dd2962 9
S 10 minetest.register_alias("technic:light_off", "air")
82cba9 11       
e23f87 12 minetest.register_tool("technic:flashlight", {
be2f30 13     description = S("Flashlight"),
e23f87 14     inventory_image = "technic_flashlight.png",
82cba9 15     stack_max = 1,
99fd5d 16     wear_represents = "technic_RE_charge",
be2f30 17 })
S 18
e23f87 19 minetest.register_craft({
dd2962 20     output = "technic:flashlight",
S 21     recipe = {
22         {"technic:rubber",                "default:glass",   "technic:rubber"},
ee0765 23         {"technic:stainless_steel_ingot", "technic:battery", "technic:stainless_steel_ingot"},
S 24         {"",                              "technic:battery", ""}
e23f87 25     }
R 26 })
27
82cba9 28
dd2962 29 local player_positions = {}
S 30 local was_wielding = {}
d8437f 31
S 32 local function check_for_flashlight(player)
33     if player == nil then
34         return false
35     end
36     local inv = player:get_inventory()
37     local hotbar = inv:get_list("main")
38     for i = 1, 8 do
39         if hotbar[i]:get_name() == "technic:flashlight" then
40             local meta = minetest.deserialize(hotbar[i]:get_metadata())
70fb21 41             if meta and meta.charge and meta.charge >= 2 then
d8437f 42                 meta.charge = meta.charge - 2;
S 43                 technic.set_RE_wear(hotbar[i], meta.charge, flashlight_max_charge)
44                 hotbar[i]:set_metadata(minetest.serialize(meta))
45                 inv:set_stack("main", i, hotbar[i])
46                 return true
47             end
48         end
49     end
50     return false
82cba9 51 end
R 52
53 minetest.register_on_joinplayer(function(player)
54     local player_name = player:get_player_name()
55     local pos = player:getpos()
dd2962 56     local rounded_pos = vector.round(pos)
db7967 57     rounded_pos.y = rounded_pos.y + 1
ee0765 58     player_positions[player_name] = rounded_pos
dd2962 59     was_wielding[player_name] = true
82cba9 60 end)
dd2962 61
82cba9 62
R 63 minetest.register_on_leaveplayer(function(player)
64     local player_name = player:get_player_name()
dd2962 65     local pos = player_positions[player_name]
S 66     local nodename = minetest.get_node(pos).name
db7967 67     if nodename == "technic:light" then
dd2962 68         minetest.remove_node(pos)
S 69     end
70     player_positions[player_name] = nil
71 end)
82cba9 72
R 73 minetest.register_globalstep(function(dtime)
dd2962 74     for i, player in pairs(minetest.get_connected_players()) do
S 75         local player_name = player:get_player_name()
76         local flashlight_weared = check_for_flashlight(player)
77         local pos = player:getpos()
78         local rounded_pos = vector.round(pos)
db7967 79         rounded_pos.y = rounded_pos.y + 1
dd2962 80         local old_pos = player_positions[player_name]
S 81         local player_moved = not vector.equals(old_pos, rounded_pos)
82cba9 82
dd2962 83         -- Remove light, flashlight weared out or was removed from hotbar
S 84         if was_wielding[player_name] and not flashlight_weared then
85             was_wielding[player_name] = false
86             local node = minetest.get_node_or_nil(old_pos)
87             if node and node.name == "technic:light" then
88                 minetest.remove_node(old_pos)
82cba9 89             end
dd2962 90         elseif (player_moved or not was_wielding[player_name]) and flashlight_weared then
S 91             local node = minetest.get_node_or_nil(rounded_pos)
92             if node and node.name == "air" then
93                 minetest.set_node(rounded_pos, {name="technic:light"})
94             end
95             local node = minetest.get_node_or_nil(old_pos)
96             if node and node.name == "technic:light" then
97                 minetest.remove_node(old_pos)
98             end
99             player_positions[player_name] = rounded_pos
100             was_wielding[player_name] = true
ee0765 101         end
82cba9 102     end
R 103 end)
104
105 minetest.register_node("technic:light", {
106     drawtype = "glasslike",
107     tile_images = {"technic_light.png"},
108     paramtype = "light",
60c75b 109     groups = {not_in_creative_inventory=1},
Z 110     drop = "",
82cba9 111     walkable = false,
R 112     buildable_to = true,
113     sunlight_propagates = true,
db7967 114     light_source = LIGHT_MAX,
dd2962 115     pointable = false,
82cba9 116 })