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