1 minetest.register_tool("screwdriver:screwdriver", {
2 description = "Screwdriver",
3 inventory_image = "screwdriver.png",
4 on_use = function(itemstack, user, pointed_thing)
5 screwdriver_handler(itemstack,user,pointed_thing)
11 minetest.register_tool("screwdriver:screwdriver"..i, {
12 description = "Screwdriver in Mode "..i,
13 inventory_image = "screwdriver.png^tool_mode"..i..".png",
14 wield_image = "screwdriver.png",
15 groups = {not_in_creative_inventory=1},
16 on_use = function(itemstack, user, pointed_thing)
17 screwdriver_handler(itemstack,user,pointed_thing)
24 --look dir +X +Y +Z -Z -Y -X
25 2 , 0 , 4 , 5 , 1 , 3 , -- rotate around y+ 0 - 3
26 4 , 0 , 3 , 2 , 1 , 5 ,
27 3 , 0 , 5 , 4 , 1 , 2 ,
28 5 , 0 , 2 , 3 , 1 , 4 ,
30 2 , 5 , 0 , 1 , 4 , 3 , -- rotate around z+ 4 - 7
31 4 , 2 , 0 , 1 , 3 , 5 ,
32 3 , 4 , 0 , 1 , 5 , 2 ,
33 5 , 3 , 0 , 1 , 2 , 4 ,
35 2 , 4 , 1 , 0 , 5 , 3 , -- rotate around z- 8 - 11
36 4 , 3 , 1 , 0 , 2 , 5 ,
37 3 , 5 , 1 , 0 , 4 , 2 ,
38 5 , 2 , 1 , 0 , 3 , 4 ,
40 0 , 3 , 4 , 5 , 2 , 1 , -- rotate around x+ 12 - 15
41 0 , 5 , 3 , 2 , 4 , 1 ,
42 0 , 2 , 5 , 4 , 3 , 1 ,
43 0 , 4 , 2 , 3 , 5 , 1 ,
45 1 , 2 , 4 , 5 , 3 , 0 , -- rotate around x- 16 - 19
46 1 , 4 , 3 , 2 , 5 , 0 ,
47 1 , 3 , 5 , 4 , 2 , 0 ,
48 1 , 5 , 2 , 3 , 4 , 0 ,
50 3 , 1 , 4 , 5 , 0 , 2 , -- rotate around y- 20 - 23
51 5 , 1 , 3 , 2 , 0 , 4 ,
52 2 , 1 , 5 , 4 , 0 , 3 ,
56 function screwdriver_handler (itemstack,user,pointed_thing)
57 local keys=user:get_player_control()
58 local player_name=user:get_player_name()
59 local item=itemstack:to_table()
60 if item["metadata"]=="" or keys["sneak"]==true then return screwdriver_setmode(user,itemstack) end
61 local mode=tonumber((item["metadata"]))
62 if pointed_thing.type~="node" then return end
63 local pos=minetest.get_pointed_thing_position(pointed_thing,above)
64 local node=minetest.get_node(pos)
65 local node_name=node.name
66 if minetest.registered_nodes[node_name].paramtype2 == "facedir" then
67 if minetest.registered_nodes[node_name].drawtype == "nodebox" then
68 if minetest.registered_nodes[node_name].node_box["type"]~="fixed" then return end
70 if node.param2==nil then return end
71 -- Get ready to set the param2
73 local axisdir=math.floor(n/4)
74 local rotation=n-axisdir*4
77 if rotation>3 then rotation=0 end
82 local ppos=user:getpos()
83 local pvect=user:get_look_dir()
84 local face=get_node_face(pos,ppos,pvect)
85 if face == nil then return end
86 local index=convertFaceToIndex(face)
87 local face1=faces_table[n*6+index+1]
92 if faces_table[n*6+index+1]==face1 then found=1 end
98 if axisdir>5 then axisdir=0 end
103 local ppos=user:getpos()
104 local pvect=user:get_look_dir()
105 local face=get_node_face(pos,ppos,pvect)
106 if face == nil then return end
107 if axisdir == face then
109 if rotation>3 then rotation=0 end
115 --print (dump(axisdir..", "..rotation))
116 local meta = minetest.get_meta(pos)
117 local meta0 = meta:to_table()
119 minetest.set_node(pos,node)
120 meta = minetest.get_meta(pos)
121 meta:from_table(meta0)
122 local item=itemstack:to_table()
123 local item_wear=tonumber((item["wear"]))
124 item_wear=item_wear+327
125 if item_wear>65535 then itemstack:clear() return itemstack end
126 item["wear"]=tostring(item_wear)
127 itemstack:replace(item)
133 {"Change rotation, Don't change axisdir."},
134 {"Keep choosen face in front then rotate it."},
135 {"Change axis dir, Reset rotation."},
136 {"Bring top in front then rotate it."},
139 function screwdriver_setmode(user,itemstack)
140 local player_name=user:get_player_name()
141 local item=itemstack:to_table()
143 if item["metadata"]=="" then
144 minetest.chat_send_player(player_name,"Hold shift and use to change screwdriwer modes.")
146 else mode=tonumber((item["metadata"]))
149 if mode==5 then mode=1 end
150 minetest.chat_send_player(player_name, "Screwdriver mode : "..mode.." - "..mode_text[mode][1] )
151 item["name"]="screwdriver:screwdriver"..mode
152 item["metadata"]=tostring(mode)
153 itemstack:replace(item)
157 minetest.register_craft({
158 output = "screwdriver:screwdriver",
160 {"default:steel_ingot"},
165 function get_node_face(pos,ppos,pvect)
166 ppos={x=ppos.x-pos.x,y=ppos.y-pos.y+1.5,z=ppos.z-pos.z}
168 local t=(-0.5-ppos.x)/pvect.x
169 local y_int=ppos.y+t*pvect.y
170 local z_int=ppos.z+t*pvect.z
171 if y_int>-0.4 and y_int<0.4 and z_int>-0.4 and z_int<0.4 then return 4 end
172 elseif pvect.x<0 then
173 local t=(0.5-ppos.x)/pvect.x
174 local y_int=ppos.y+t*pvect.y
175 local z_int=ppos.z+t*pvect.z
176 if y_int>-0.4 and y_int<0.4 and z_int>-0.4 and z_int<0.4 then return 3 end
179 local t=(-0.5-ppos.y)/pvect.y
180 local x_int=ppos.x+t*pvect.x
181 local z_int=ppos.z+t*pvect.z
182 if x_int>-0.4 and x_int<0.4 and z_int>-0.4 and z_int<0.4 then return 5 end
183 elseif pvect.y<0 then
184 local t=(0.5-ppos.y)/pvect.y
185 local x_int=ppos.x+t*pvect.x
186 local z_int=ppos.z+t*pvect.z
187 if x_int>-0.4 and x_int<0.4 and z_int>-0.4 and z_int<0.4 then return 0 end
190 local t=(-0.5-ppos.z)/pvect.z
191 local x_int=ppos.x+t*pvect.x
192 local y_int=ppos.y+t*pvect.y
193 if x_int>-0.4 and x_int<0.4 and y_int>-0.4 and y_int<0.4 then return 2 end
194 elseif pvect.z<0 then
195 local t=(0.5-ppos.z)/pvect.z
196 local x_int=ppos.x+t*pvect.x
197 local y_int=ppos.y+t*pvect.y
198 if x_int>-0.4 and x_int<0.4 and y_int>-0.4 and y_int<0.4 then return 1 end
202 function convertFaceToIndex (face)
203 if face==0 then return 1 end
204 if face==1 then return 2 end
205 if face==2 then return 3 end
206 if face==3 then return 0 end
207 if face==4 then return 5 end
208 if face==5 then return 4 end