]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/game/item_s.lua
Merge pull request #59 from PrairieAstronomer/readme_irrlicht_change
[dragonfireclient.git] / builtin / game / item_s.lua
1 -- Minetest: builtin/item_s.lua
2 -- The distinction of what goes here is a bit tricky, basically it's everything
3 -- that does not (directly or indirectly) need access to ServerEnvironment,
4 -- Server or writable access to IGameDef on the engine side.
5 -- (The '_s' stands for standalone.)
6
7 --
8 -- Item definition helpers
9 --
10
11 function core.inventorycube(img1, img2, img3)
12         img2 = img2 or img1
13         img3 = img3 or img1
14         return "[inventorycube"
15                         .. "{" .. img1:gsub("%^", "&")
16                         .. "{" .. img2:gsub("%^", "&")
17                         .. "{" .. img3:gsub("%^", "&")
18 end
19
20 function core.dir_to_facedir(dir, is6d)
21         --account for y if requested
22         if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then
23
24                 --from above
25                 if dir.y < 0 then
26                         if math.abs(dir.x) > math.abs(dir.z) then
27                                 if dir.x < 0 then
28                                         return 19
29                                 else
30                                         return 13
31                                 end
32                         else
33                                 if dir.z < 0 then
34                                         return 10
35                                 else
36                                         return 4
37                                 end
38                         end
39
40                 --from below
41                 else
42                         if math.abs(dir.x) > math.abs(dir.z) then
43                                 if dir.x < 0 then
44                                         return 15
45                                 else
46                                         return 17
47                                 end
48                         else
49                                 if dir.z < 0 then
50                                         return 6
51                                 else
52                                         return 8
53                                 end
54                         end
55                 end
56
57         --otherwise, place horizontally
58         elseif math.abs(dir.x) > math.abs(dir.z) then
59                 if dir.x < 0 then
60                         return 3
61                 else
62                         return 1
63                 end
64         else
65                 if dir.z < 0 then
66                         return 2
67                 else
68                         return 0
69                 end
70         end
71 end
72
73 -- Table of possible dirs
74 local facedir_to_dir = {
75         vector.new( 0,  0,  1),
76         vector.new( 1,  0,  0),
77         vector.new( 0,  0, -1),
78         vector.new(-1,  0,  0),
79         vector.new( 0, -1,  0),
80         vector.new( 0,  1,  0),
81 }
82 -- Mapping from facedir value to index in facedir_to_dir.
83 local facedir_to_dir_map = {
84         [0]=1, 2, 3, 4,
85         5, 2, 6, 4,
86         6, 2, 5, 4,
87         1, 5, 3, 6,
88         1, 6, 3, 5,
89         1, 4, 3, 2,
90 }
91 function core.facedir_to_dir(facedir)
92         return facedir_to_dir[facedir_to_dir_map[facedir % 32]]
93 end
94
95 function core.dir_to_wallmounted(dir)
96         if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
97                 if dir.y < 0 then
98                         return 1
99                 else
100                         return 0
101                 end
102         elseif math.abs(dir.x) > math.abs(dir.z) then
103                 if dir.x < 0 then
104                         return 3
105                 else
106                         return 2
107                 end
108         else
109                 if dir.z < 0 then
110                         return 5
111                 else
112                         return 4
113                 end
114         end
115 end
116
117 -- table of dirs in wallmounted order
118 local wallmounted_to_dir = {
119         [0] = vector.new( 0,  1,  0),
120         vector.new( 0, -1,  0),
121         vector.new( 1,  0,  0),
122         vector.new(-1,  0,  0),
123         vector.new( 0,  0,  1),
124         vector.new( 0,  0, -1),
125 }
126 function core.wallmounted_to_dir(wallmounted)
127         return wallmounted_to_dir[wallmounted % 8]
128 end
129
130 function core.dir_to_yaw(dir)
131         return -math.atan2(dir.x, dir.z)
132 end
133
134 function core.yaw_to_dir(yaw)
135         return vector.new(-math.sin(yaw), 0, math.cos(yaw))
136 end
137
138 function core.is_colored_paramtype(ptype)
139         return (ptype == "color") or (ptype == "colorfacedir") or
140                 (ptype == "colorwallmounted") or (ptype == "colordegrotate")
141 end
142
143 function core.strip_param2_color(param2, paramtype2)
144         if not core.is_colored_paramtype(paramtype2) then
145                 return nil
146         end
147         if paramtype2 == "colorfacedir" then
148                 param2 = math.floor(param2 / 32) * 32
149         elseif paramtype2 == "colorwallmounted" then
150                 param2 = math.floor(param2 / 8) * 8
151         elseif paramtype2 == "colordegrotate" then
152                 param2 = math.floor(param2 / 32) * 32
153         end
154         -- paramtype2 == "color" requires no modification.
155         return param2
156 end