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