ShadowNinja
2013-10-30 be2f30a1a2f5b6c2aae7fd4cf8231aec2da0844d
commit | author | age
82cba9 1 -- original code comes from walkin_light mod by Echo http://minetest.net/forum/viewtopic.php?id=2621
R 2
ee0765 3 local flashlight_max_charge = 30000
be2f30 4
S 5 local S = technic.getter
6
ee0765 7 technic.register_power_tool("technic:flashlight", flashlight_max_charge)
82cba9 8       
e23f87 9 minetest.register_tool("technic:flashlight", {
be2f30 10     description = S("Flashlight"),
e23f87 11     inventory_image = "technic_flashlight.png",
82cba9 12     stack_max = 1,
e23f87 13     on_use = function(itemstack, user, pointed_thing)
R 14     end,
be2f30 15 })
S 16
e23f87 17 minetest.register_craft({
R 18 output = "technic:flashlight",
19 recipe = {
ee0765 20         {"technic:rubber",                "glass",           "technic:rubber"},
S 21         {"technic:stainless_steel_ingot", "technic:battery", "technic:stainless_steel_ingot"},
22         {"",                              "technic:battery", ""}
e23f87 23     }
R 24 })
25
82cba9 26 local players = {}
R 27 local player_positions = {}
28 local last_wielded = {}
29
30 function round(num) 
31     return math.floor(num + 0.5) 
32 end
33
34 minetest.register_on_joinplayer(function(player)
35     local player_name = player:get_player_name()
36     table.insert(players, player_name)
37     local pos = player:getpos()
38     local rounded_pos = {x=round(pos.x),y=round(pos.y)+1,z=round(pos.z)}
ee0765 39     player_positions[player_name] = rounded_pos
82cba9 40 end)
R 41
42 minetest.register_on_leaveplayer(function(player)
43     local player_name = player:get_player_name()
ee0765 44     for i, v in ipairs(players) do
82cba9 45         if v == player_name then 
R 46             table.remove(players, i)
47             last_wielded[player_name] = nil
48             -- Neuberechnung des Lichts erzwingen
49             local pos = player:getpos()
50             local rounded_pos = {x=round(pos.x),y=round(pos.y)+1,z=round(pos.z)}
ee0765 51             local nodename = minetest.get_node(rounded_pos).name
S 52             if nodename == "technic:light_off" or nodename == "technic:light" then
53                 minetest.remove_node(rounded_pos)
54             end
55             if player_positions[player_name] then
56                 player_positions[player_name] = nil
57             end
82cba9 58         end
R 59     end
60 end)
61
62 minetest.register_globalstep(function(dtime)
ee0765 63     for i, player_name in ipairs(players) do
S 64         local player = minetest.get_player_by_name(player_name)
82cba9 65         if player then
ee0765 66             flashlight_weared = check_for_flashlight(player)
S 67             local pos = player:getpos()
68             local rounded_pos = {x=round(pos.x), y=round(pos.y)+1, z=round(pos.z)}
69             local old_pos = vector.new(player_positions[player_name])
70             
71             if last_wielded[player_name] and not flashlight_weared then --remove light, flashlight weared out or was removed from hotbar
72                 local node = minetest.get_node_or_nil(old_pos)
73                 if node and node.name == "technic:light" then 
74                     minetest.add_node(old_pos,{name="air"})        
75                     last_wielded[player_name] = false
76                 end
82cba9 77
ee0765 78                 player_moved = not(old_pos.x == rounded_pos.x and old_pos.y == rounded_pos.y and old_pos.z == rounded_pos.z)
S 79                 if player_moved and last_wielded[player_name] and flashlight_weared  then
80                     
81                     local node=minetest.env:get_node_or_nil(rounded_pos)
82                     if node then
83                         if node.name=="air" then 
84                             minetest.env:add_node(rounded_pos,{type="node",name="technic:light"})
85                         end
86                     end
87                     local node=minetest.env:get_node_or_nil(old_pos)
88                     if node then
89                         if node.name=="technic:light" then 
90                             minetest.env:add_node(old_pos,{type="node",name="technic:light_off"})
91                             minetest.env:add_node(old_pos,{type="node",name="air"})        
92                         end
93                     end
94                     player_positions[player_name]["x"] = rounded_pos.x
95                     player_positions[player_name]["y"] = rounded_pos.y
96                     player_positions[player_name]["z"] = rounded_pos.z
97                     
98                 elseif not last_wielded[player_name] and flashlight_weared then
99                     local node=minetest.env:get_node_or_nil(rounded_pos)
100                     if node then
101                         if node.name=="air" then 
102                             minetest.env:add_node(rounded_pos,{type="node",name="technic:light"})
103                         end
104                     end
105                     player_positions[player_name]["x"] = rounded_pos.x
106                     player_positions[player_name]["y"] = rounded_pos.y
107                     player_positions[player_name]["z"] = rounded_pos.z
108                     last_wielded[player_name]=true
109                 end            
110                     
82cba9 111             end
ee0765 112         end
82cba9 113     end
R 114 end)
115
116 minetest.register_node("technic:light", {
117     drawtype = "glasslike",
118     tile_images = {"technic_light.png"},
119     paramtype = "light",
120     walkable = false,
121     buildable_to = true,
122     is_ground_content = true,
123     light_propagates = true,
124     sunlight_propagates = true,
125     light_source = 15,
126     selection_box = {
127         type = "fixed",
128         fixed = {0, 0, 0, 0, 0, 0},
129     },
130 })
131 minetest.register_node("technic:light_off", {
132     drawtype = "glasslike",
133     tile_images = {"technic_light.png"},
134     paramtype = "light",
135     walkable = false,
136     buildable_to = true,
137     is_ground_content = true,
138     light_propagates = true,
139     sunlight_propagates = true,
140     selection_box = {
141         type = "fixed",
142         fixed = {0, 0, 0, 0, 0, 0},
143     },
144 })
145
eac484 146 function check_for_flashlight(player)
S 147     if player == nil then
148         return false
149     end
150     local inv = player:get_inventory()
151     local hotbar = inv:get_list("main")
152     for i = 1, 8 do
153         if hotbar[i]:get_name() == "technic:flashlight" then
154             local meta = get_item_meta(hotbar[i]:get_metadata())
155             if not meta or not meta.charge then
156                 return false
82cba9 157             end
eac484 158             if meta.charge - 2 > 0 then
S 159                 meta.charge = meta.charge - 2;
160                 technic.set_RE_wear(hotbar[i], meta.charge, flashlight_max_charge)
161                 hotbar[i]:set_metadata(set_item_meta(meta))
162                 inv:set_stack("main", i, hotbar[i])
163                 return true
82cba9 164             end
R 165         end
eac484 166     end
S 167     return false
be2f30 168 end
S 169