]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/api/head_code.lua
Hack in jumping fix for mobs that walk - makes mobs randomly jump though
[Crafter.git] / mods / mob / api / head_code.lua
1 --
2 mobs.create_head_functions = function(def,mob_register)
3         --converts the degrees to radians
4         local degrees_to_radians = function(degrees)
5                 --print(d)
6                 return(degrees/180.0*math.pi)
7         end
8         
9         --converts yaw to degrees
10         local degrees = function(yaw)
11                 return(yaw*180.0/math.pi)
12         end
13         
14         --rounds it up to an integer
15         local degree_round = function(degree)
16                 return(degree + 0.5 - (degree + 0.5) % 1)
17         end
18         --turns radians into degrees - not redundant
19         --doesn't add math.pi
20         local radians_to_degrees = function(radians)
21                 return(radians*180.0/math.pi)
22         end
23         
24         
25         --make sure this is redefined as shown below aka
26         --don't run mob_rotation_degree_to_radians(rotation)
27         --run local radians = mob_rotation_degree_to_radians(rotation)
28         --or the mobs head rotation will become overwritten
29         local head_rotation_to_radians = function(rotation)
30                 return{
31                         x = 0, --roll should never be changed
32                         y = degrees_to_radians(180 - rotation.y)*-1,
33                         z = degrees_to_radians(90 - rotation.z)
34                 }
35         end
36         
37         --a movement test to move the head
38         mob_register.move_head = function(self,pos2,dtime)
39                 if self.head_bone then
40                         if self.head_coord == "horizontal" then
41                                 --print(self.head_bone)
42                                 local head_position,head_rotation = self.object:get_bone_position(self.head_bone)
43                                 --[[ debug
44                                 if rotation then
45                                         --print("--------------------------------")
46                                         --rotation.x = rotation.x + 1
47                                         rotation.z = rotation.z + 1
48                                         rotation.y = 0
49                                         
50                                         if rotation.x > 90 then
51                                                  rotation.x = -90
52                                         end
53                                         if rotation.z > 90 then
54                                                  rotation.z = -90
55                                         end
56                                         
57                                         --print(rotation.x)
58                                         self.object:set_bone_position(self.head_bone, head_position, rotation)
59                                 end
60                                 ]]--
61                                 
62                                 --print(self.head_rotation.y)
63                                 --if passed a direction to look
64                                 local pos = self.object:get_pos()
65                                 local body_yaw = self.object:get_yaw()-math.pi/2
66                                                                 
67                                 local dir = vector.multiply(minetest.yaw_to_dir(body_yaw),self.head_directional_offset)
68                                 
69                                 
70                                 body_yaw = minetest.dir_to_yaw(dir)
71                                 
72                                 --pos is where the head actually is
73                                 pos = vector.add(pos,dir)
74                                 pos.y = pos.y + self.head_height_offset
75                                 
76                                 --use this to literally look around
77                                 self.head_pos = pos
78                                 
79                                 if self.debug_head_pos == true then
80                                         minetest.add_particle({
81                                                 pos = pos,
82                                                 velocity = {x=0, y=0, z=0},
83                                                 acceleration = {x=0, y=0, z=0},
84                                                 expirationtime = 0.2,
85                                                 size = 1,
86                                                 texture = "dirt.png",
87                                         })
88                                 end
89                                 
90                                 --if the function was given a pos
91                                 if pos2 then
92                                         --compare the head yaw to the body
93                                         --we must do a bunch of calculations to correct
94                                         --strange function returns
95                                         --for some reason get_yaw is offset 90 degrees
96                                         local head_yaw = minetest.dir_to_yaw(vector.direction(pos,pos2))
97                                         head_yaw = minetest.dir_to_yaw(minetest.yaw_to_dir(head_yaw))
98                                         head_yaw = degrees(head_yaw)-degrees(body_yaw)
99
100                                         if head_yaw < -180 then
101                                                 head_yaw = head_yaw + 360
102                                         elseif head_yaw > 180 then
103                                                 head_yaw = head_yaw - 360
104                                         end
105
106                                         --if within range then do calculations
107                                         if head_yaw >= -90 and head_yaw <= 90 then
108                                                 ---begin pitch calculation
109                                                 --feed a 2D coordinate flipped into dir to yaw to calculate pitch
110                                                 head_rotation.x = degrees(minetest.dir_to_yaw(vector.new(vector.distance(vector.new(pos.x,0,pos.z),vector.new(pos2.x,0,pos2.z)),0,pos.y-pos2.y))+(math.pi/2))
111                                                 head_rotation.z = head_yaw
112                                                 self.object:set_bone_position(self.head_bone, head_position, head_rotation)
113                                                 return(true)
114                                         --nothing to look at
115                                         else
116                                                 self.return_head_to_origin(self,dtime)
117                                                 return(false)
118                                         end
119                                         
120                                 --if nothing to look at
121                                 else
122                                         self.return_head_to_origin(self,dtime)
123                                         return(false)
124                                 end
125
126                         elseif self.head_coord == "vertical" then
127                                 --print(self.head_bone)
128                                 local head_position,head_rotation = self.object:get_bone_position(self.head_bone)
129                                 --[[ debug
130                                 if rotation then
131                                         --print("--------------------------------")
132                                         --rotation.x = rotation.x + 1
133                                         rotation.z = rotation.z + 1
134                                         rotation.y = 0
135                                         
136                                         if rotation.x > 90 then
137                                                  rotation.x = -90
138                                         end
139                                         if rotation.z > 90 then
140                                                  rotation.z = -90
141                                         end
142                                         
143                                         --print(rotation.x)
144                                         self.object:set_bone_position(self.head_bone, head_position, rotation)
145                                 end
146                                 ]]--
147                                 
148                                 --print(self.head_rotation.y)
149                                 --if passed a direction to look
150                                 local pos = self.object:get_pos()
151                                 local body_yaw = self.object:get_yaw()-math.pi/2
152                                                                 
153                                 local dir = vector.multiply(minetest.yaw_to_dir(body_yaw),self.head_directional_offset)
154                                 
155                                 
156                                 body_yaw = minetest.dir_to_yaw(dir)
157                                 
158                                 --pos is where the head actually is
159                                 pos = vector.add(pos,dir)
160                                 pos.y = pos.y + self.head_height_offset
161                                 
162                                 --use this to literally look around
163                                 self.head_pos = pos
164                                 
165                                 if self.debug_head_pos == true then
166                                         minetest.add_particle({
167                                                 pos = pos,
168                                                 velocity = {x=0, y=0, z=0},
169                                                 acceleration = {x=0, y=0, z=0},
170                                                 expirationtime = 0.2,
171                                                 size = 1,
172                                                 texture = "dirt.png",
173                                         })
174                                 end
175                                 
176                                 --if the function was given a pos
177                                 if pos2 then
178                                         --compare the head yaw to the body
179                                         --we must do a bunch of calculations to correct
180                                         --strange function returns
181                                         --for some reason get_yaw is offset 90 degrees
182                                         local head_yaw = minetest.dir_to_yaw(vector.direction(pos,pos2))
183                                         head_yaw = minetest.dir_to_yaw(minetest.yaw_to_dir(head_yaw))
184                                         head_yaw = degrees(head_yaw)-degrees(body_yaw)
185
186                                         if head_yaw < -180 then
187                                                 head_yaw = head_yaw + 360
188                                         elseif head_yaw > 180 then
189                                                 head_yaw = head_yaw - 360
190                                         end
191
192                                         --if within range then do calculations
193                                         if head_yaw >= -90 and head_yaw <= 90 then
194                                                 ---begin pitch calculation
195                                                 --feed a 2D coordinate flipped into dir to yaw to calculate pitch
196                                                 head_rotation.x = degrees(minetest.dir_to_yaw(vector.new(vector.distance(vector.new(pos.x,0,pos.z),vector.new(pos2.x,0,pos2.z)),0,pos.y-pos2.y))+(math.pi/2))
197                                                 head_rotation.y = -head_yaw
198                                                 self.object:set_bone_position(self.head_bone, head_position, head_rotation)
199                                                 return(true)
200                                         --nothing to look at
201                                         else
202                                                 self.return_head_to_origin(self,dtime)
203                                                 return(false)
204                                         end
205                                         
206                                 --if nothing to look at
207                                 else
208                                         self.return_head_to_origin(self,dtime)
209                                         return(false)
210                                 end
211                         end
212                 end
213         end
214         
215         
216         --this sets the mob to move it's head back to pointing forwards
217
218         mob_register.return_head_to_origin = function(self,dtime)
219                 local head_position,head_rotation = self.object:get_bone_position(self.head_bone)
220                 
221                 if self.head_coord == "horizontal" then 
222                         --make the head yaw move back
223                         if head_rotation.x > 0 then
224                                 head_rotation.x = head_rotation.x - (dtime*100)
225                         elseif head_rotation.x < 0 then
226                                 head_rotation.x = head_rotation.x + (dtime*100)
227                         end
228                         
229                         if math.abs(head_rotation.x) < (dtime*100) then
230                                 head_rotation.x = 0
231                         end
232                         
233                         
234                         --move up down (pitch) back to center
235                         if head_rotation.z > 0 then
236                                 head_rotation.z = head_rotation.z - (dtime*100)
237                         elseif head_rotation.z < 0 then
238                                 head_rotation.z = head_rotation.z + (dtime*100)
239                         end
240                         
241                         if math.abs(head_rotation.z) < (dtime*100) then
242                                 head_rotation.z = 0
243                         end
244                 elseif self.head_coord == "vertical" then
245                         --make the head yaw move back
246                         if head_rotation.x > 0 then
247                                 head_rotation.x = head_rotation.x - (dtime*100)
248                         elseif head_rotation.x < 0 then
249                                 head_rotation.x = head_rotation.x + (dtime*100)
250                         end
251                         
252                         if math.abs(head_rotation.x) < (dtime*100) then
253                                 head_rotation.x = 0
254                         end
255                         
256                         
257                         --move up down (pitch) back to center
258                         if head_rotation.y > 0 then
259                                 head_rotation.y = head_rotation.y - (dtime*100)
260                         elseif head_rotation.y < 0 then
261                                 head_rotation.y = head_rotation.y + (dtime*100)
262                         end
263                         
264                         if math.abs(head_rotation.y) < (dtime*100) then
265                                 head_rotation.y = 0
266                         end
267                 end
268                 self.object:set_bone_position(self.head_bone, head_position, head_rotation)
269         end
270         return(mob_register)
271 end