ShadowNinja
2013-10-19 eac48441754260fe42c7a605e72141a79ed79bc1
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
S 4 technic.register_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 = {
ee0765 17         {"technic:rubber",                "glass",           "technic:rubber"},
S 18         {"technic:stainless_steel_ingot", "technic:battery", "technic:stainless_steel_ingot"},
19         {"",                              "technic:battery", ""}
e23f87 20     }
R 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)}
ee0765 36     player_positions[player_name] = rounded_pos
82cba9 37 end)
R 38
39 minetest.register_on_leaveplayer(function(player)
40     local player_name = player:get_player_name()
ee0765 41     for i, v in ipairs(players) do
82cba9 42         if v == player_name then 
R 43             table.remove(players, i)
44             last_wielded[player_name] = nil
45             -- Neuberechnung des Lichts erzwingen
46             local pos = player:getpos()
47             local rounded_pos = {x=round(pos.x),y=round(pos.y)+1,z=round(pos.z)}
ee0765 48             local nodename = minetest.get_node(rounded_pos).name
S 49             if nodename == "technic:light_off" or nodename == "technic:light" then
50                 minetest.remove_node(rounded_pos)
51             end
52             if player_positions[player_name] then
53                 player_positions[player_name] = nil
54             end
82cba9 55         end
R 56     end
57 end)
58
59 minetest.register_globalstep(function(dtime)
ee0765 60     for i, player_name in ipairs(players) do
S 61         local player = minetest.get_player_by_name(player_name)
82cba9 62         if player then
ee0765 63             flashlight_weared = check_for_flashlight(player)
S 64             local pos = player:getpos()
65             local rounded_pos = {x=round(pos.x), y=round(pos.y)+1, z=round(pos.z)}
66             local old_pos = vector.new(player_positions[player_name])
67             
68             if last_wielded[player_name] and not flashlight_weared then --remove light, flashlight weared out or was removed from hotbar
69                 local node = minetest.get_node_or_nil(old_pos)
70                 if node and node.name == "technic:light" then 
71                     minetest.add_node(old_pos,{name="air"})        
72                     last_wielded[player_name] = false
73                 end
82cba9 74
ee0765 75                 player_moved = not(old_pos.x == rounded_pos.x and old_pos.y == rounded_pos.y and old_pos.z == rounded_pos.z)
S 76                 if player_moved and last_wielded[player_name] and flashlight_weared  then
77                     
78                     local node=minetest.env:get_node_or_nil(rounded_pos)
79                     if node then
80                         if node.name=="air" then 
81                             minetest.env:add_node(rounded_pos,{type="node",name="technic:light"})
82                         end
83                     end
84                     local node=minetest.env:get_node_or_nil(old_pos)
85                     if node then
86                         if node.name=="technic:light" then 
87                             minetest.env:add_node(old_pos,{type="node",name="technic:light_off"})
88                             minetest.env:add_node(old_pos,{type="node",name="air"})        
89                         end
90                     end
91                     player_positions[player_name]["x"] = rounded_pos.x
92                     player_positions[player_name]["y"] = rounded_pos.y
93                     player_positions[player_name]["z"] = rounded_pos.z
94                     
95                 elseif not last_wielded[player_name] and flashlight_weared then
96                     local node=minetest.env:get_node_or_nil(rounded_pos)
97                     if node then
98                         if node.name=="air" then 
99                             minetest.env:add_node(rounded_pos,{type="node",name="technic:light"})
100                         end
101                     end
102                     player_positions[player_name]["x"] = rounded_pos.x
103                     player_positions[player_name]["y"] = rounded_pos.y
104                     player_positions[player_name]["z"] = rounded_pos.z
105                     last_wielded[player_name]=true
106                 end            
107                     
82cba9 108             end
ee0765 109         end
82cba9 110     end
R 111 end)
112
113 minetest.register_node("technic:light", {
114     drawtype = "glasslike",
115     tile_images = {"technic_light.png"},
116     paramtype = "light",
117     walkable = false,
118     buildable_to = true,
119     is_ground_content = true,
120     light_propagates = true,
121     sunlight_propagates = true,
122     light_source = 15,
123     selection_box = {
124         type = "fixed",
125         fixed = {0, 0, 0, 0, 0, 0},
126     },
127 })
128 minetest.register_node("technic:light_off", {
129     drawtype = "glasslike",
130     tile_images = {"technic_light.png"},
131     paramtype = "light",
132     walkable = false,
133     buildable_to = true,
134     is_ground_content = true,
135     light_propagates = true,
136     sunlight_propagates = true,
137     selection_box = {
138         type = "fixed",
139         fixed = {0, 0, 0, 0, 0, 0},
140     },
141 })
142
eac484 143 function check_for_flashlight(player)
S 144     if player == nil then
145         return false
146     end
147     local inv = player:get_inventory()
148     local hotbar = inv:get_list("main")
149     for i = 1, 8 do
150         if hotbar[i]:get_name() == "technic:flashlight" then
151             local meta = get_item_meta(hotbar[i]:get_metadata())
152             if not meta or not meta.charge then
153                 return false
82cba9 154             end
eac484 155             if meta.charge - 2 > 0 then
S 156                 meta.charge = meta.charge - 2;
157                 technic.set_RE_wear(hotbar[i], meta.charge, flashlight_max_charge)
158                 hotbar[i]:set_metadata(set_item_meta(meta))
159                 inv:set_stack("main", i, hotbar[i])
160                 return true
82cba9 161             end
R 162         end
eac484 163     end
S 164     return false
82cba9 165 end