]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/api/head_code.lua
1e79a251b7de5e07a8a3f8f903787cedd6008fd8
[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                         --print(self.head_bone)
41                         local head_position,head_rotation = self.object:get_bone_position(self.head_bone)
42                         --[[ debug
43                         if rotation then
44                                 --print("--------------------------------")
45                                 --rotation.x = rotation.x + 1
46                                 rotation.z = rotation.z + 1
47                                 rotation.y = 0
48                                 
49                                 if rotation.x > 90 then
50                                          rotation.x = -90
51                                 end
52                                 if rotation.z > 90 then
53                                          rotation.z = -90
54                                 end
55                                 
56                                 --print(rotation.x)
57                                 self.object:set_bone_position(self.head_bone, head_position, rotation)
58                         end
59                         ]]--
60                         
61                         --print(self.head_rotation.y)
62                         --if passed a direction to look
63                         local pos = self.object:get_pos()
64                         local body_yaw = self.object:get_yaw()
65                         if not body_yaw then
66                                 return
67                         end
68                         
69                         local dir = vector.multiply(minetest.yaw_to_dir(body_yaw+self.head_rotation_offset),self.head_directional_offset)
70                         
71                         
72                         local body_yaw = minetest.dir_to_yaw(dir)
73                         
74                         --pos is where the head actually is
75                         pos = vector.add(pos,dir)
76                         pos.y = pos.y + self.head_height_offset
77                         
78                         --use this to literally look around
79                         self.head_pos = pos
80                         
81                         if self.debug_head_pos == true then
82                                 minetest.add_particle({
83                                         pos = pos,
84                                         velocity = {x=0, y=0, z=0},
85                                         acceleration = {x=0, y=0, z=0},
86                                         expirationtime = 0.2,
87                                         size = 1,
88                                         texture = "dirt.png",
89                                 })
90                         end
91                         
92                         --if the function was given a pos
93                         if pos2 then
94                                 --compare the head yaw to the body
95                                 local head_yaw = minetest.dir_to_yaw(vector.direction(pos,pos2))
96                                 
97                                 local goal_yaw = body_yaw-head_yaw
98                                 
99                                 --if within range then do calculations
100                                 if goal_yaw <= math.pi/2 and goal_yaw >= -math.pi/2 then
101                                         ---begin pitch calculation
102                                         --feed a 2D coordinate flipped into dir to yaw to calculate pitch
103                                         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))
104                                         head_rotation.z = degrees(-goal_yaw)
105                                         self.object:set_bone_position(self.head_bone, head_position, head_rotation)
106                                         return(true)
107                                 --nothing to look at
108                                 else--dofile(path.."movement.lua")
109                                         self.return_head_to_origin(self,dtime)
110                                         return(false)
111                                 end
112                                 
113                         --if nothing to look at
114                         else
115                                 self.return_head_to_origin(self,dtime)
116                                 return(false)
117                         end
118                 end
119         end
120         
121         
122         --this sets the mob to move it's head back to pointing forwards
123
124         mob_register.return_head_to_origin = function(self,dtime)
125                 local head_position,head_rotation = self.object:get_bone_position(self.head_bone)               
126                 --make the head yaw move back
127                 if head_rotation.x > 0 then
128                         head_rotation.x = head_rotation.x - 1
129                 elseif head_rotation.x < 0 then
130                         head_rotation.x = head_rotation.x + 1
131                 end
132                 
133                 --move up down (pitch) back to center
134                 if head_rotation.z > 0 then
135                         head_rotation.z = head_rotation.z - 1
136                 elseif head_rotation.z < 0 then
137                         head_rotation.z = head_rotation.z + 1
138                 end
139                 
140                 self.object:set_bone_position(self.head_bone, head_position, head_rotation)
141         end
142         return(mob_register)
143 end