Pngcrush all Textures
[oweals/minetest_game.git] / mods / screwdriver / init.lua
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)
6         return itemstack
7         end,
8 })
9
10 for i=1,4,1 do
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)
18         return itemstack
19         end,
20 })
21 end
22 faces_table=
23 {
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 ,
29
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 ,
34
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 ,
39
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 ,
44
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 ,  
49
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 ,  
53                         4 , 1 , 2 ,    3 , 0 , 5  
54 }
55
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
69                         end
70                 if node.param2==nil  then return end
71                 -- Get ready to set the param2
72                         local n = node.param2
73                         local axisdir=math.floor(n/4)
74                         local rotation=n-axisdir*4
75                         if mode==1 then 
76                                 rotation=rotation+1
77                                 if rotation>3 then rotation=0 end
78                                 n=axisdir*4+rotation
79                         end
80
81                         if mode==2 then 
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]
88                                 local found = 0
89                                 while found == 0 do
90                                         n=n+1
91                                         if n>23 then n=0 end
92                                         if faces_table[n*6+index+1]==face1 then found=1 end
93                                 end
94                         end
95                                 
96                         if mode==3 then 
97                                 axisdir=axisdir+1
98                                 if axisdir>5 then axisdir=0 end
99                                 n=axisdir*4
100                         end
101
102                         if mode==4 then 
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
108                                         rotation=rotation+1
109                                 if rotation>3 then rotation=0 end
110                                         n=axisdir*4+rotation
111                                 else
112                                         n=face*4
113                                 end
114                         end
115                         --print (dump(axisdir..", "..rotation))
116                         local meta = minetest.get_meta(pos)
117                         local meta0 = meta:to_table()
118                         node.param2 = n
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)
128                         return itemstack
129         end
130 end
131
132 mode_text={
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."},
137 }
138
139 function screwdriver_setmode(user,itemstack)
140 local player_name=user:get_player_name()
141 local item=itemstack:to_table()
142 local mode
143 if item["metadata"]=="" then
144         minetest.chat_send_player(player_name,"Hold shift and use to change screwdriwer modes.")
145         mode=0
146 else mode=tonumber((item["metadata"]))
147 end
148 mode=mode+1
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)
154 return itemstack
155 end
156
157 minetest.register_craft({
158 output = "screwdriver:screwdriver",
159 recipe = {
160 {"default:steel_ingot"},
161 {"default:stick"}
162 }
163 })
164
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}
167         if pvect.x>0 then
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 
177         end
178         if pvect.y>0 then
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 
188         end
189         if pvect.z>0 then
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 
199         end
200 end
201
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
209 end
210