]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/pig/head_code.lua
Add in group_attack setting for mobs
[Crafter.git] / mods / mob / pig / head_code.lua
1 --this is where all the code is stored for the pig mob's head
2
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 --this is the "eyes" of the mob
38 --[[
39 pig.raycast_look = function(self,dtime)
40         if self.head_rotation and self.head_pos and self.yaw then
41                 --clone the memory as to not overwrite
42                 local head_rotation = table.copy(self.head_rotation)
43                 
44                 local radians = head_rotation_to_radians(head_rotation)
45                 
46                 --get the real rotation of the head in radians
47                 local real_yaw = degrees_to_radians(self.yaw+180)+radians.y
48                 local dir = vector.multiply(minetest.yaw_to_dir(real_yaw),2)
49                 
50                 local convert_to_pitch = minetest.yaw_to_dir(radians.z)
51                 dir.y = convert_to_pitch.x * math.pi/1.5
52                 
53                 
54                 local pos = self.head_pos
55                 
56                 local pos2 = vector.add(pos,vector.multiply(dir,self.view_distance))
57                 
58                 return(minetest.raycast(pos, pos2, false, true))
59         end
60 end
61
62
63 --this makes a mob check if they're about to walk off a cliff
64 pig.look_below = function(self)
65         if self.yaw then
66                 local yaw = degrees_to_radians(self.yaw)
67                 local dir = minetest.yaw_to_dir(yaw)
68                 local pos = self.object:get_pos()
69                 
70                 local ray_pos = vector.add(dir,pos)
71                 
72                 local pos_below = vector.new(ray_pos.x,ray_pos.y - 5,ray_pos.z)
73                 
74                 minetest.add_particle({
75                         pos = ray_pos,
76                         velocity = {x=0, y=0, z=0},
77                         acceleration = {x=0, y=0, z=0},
78                         expirationtime = 1,
79                         size = 1,
80                         collisiondetection = false,
81                         vertical = false,
82                         texture = "wood.png",
83                         playername = "singleplayer"
84                 })
85                 
86                 return(minetest.raycast(ray_pos, pos_below, false, true))
87         end
88 end
89 ]]--
90 --a movement test to move the head
91 pig.move_head = function(self,pos2,dtime)
92         if self.child then
93                 --print(self.head_rotation.y)
94                 --if passed a direction to look
95                 local pos = self.object:get_pos()
96                 local body_yaw = self.object:get_yaw()
97                 local dir = vector.multiply(minetest.yaw_to_dir(body_yaw),0.58)
98                 local body_yaw = minetest.dir_to_yaw(dir)
99                 --save the yaw for debug
100                 
101                 --pos is where the head actually is
102                 pos = vector.add(pos,dir)
103                 pos.y = pos.y + 0.36
104                 --use this to literally look around
105                 self.head_pos = pos
106                                 
107                 --if the function was given a pos
108                 if pos2 then
109
110                         local pitch = 0
111                         --compare the head yaw to the body
112                         local head_yaw = minetest.dir_to_yaw(vector.direction(pos,pos2))
113                         local goal_yaw = body_yaw-head_yaw
114                         
115                         --if within range then do calculations
116                         if goal_yaw <= math.pi/2 and goal_yaw >= -math.pi/2 then
117                                 
118                                 local current_yaw = self.head_rotation.y
119                                 --smoothly move head using dtime
120                                 if current_yaw > goal_yaw then
121                                         current_yaw = current_yaw - (dtime*5)
122                                 elseif current_yaw < goal_yaw then
123                                         current_yaw = current_yaw + (dtime*5)
124                                 end
125                                 
126                                 --stop jittering
127                                 if math.abs(goal_yaw - current_yaw) <= (dtime*5) then
128                                         current_yaw = goal_yaw
129                                 end
130                                 
131                                 ---begin pitch calculation
132                                 
133                                 --feed a 2D coordinate flipped into dir to yaw to calculate pitch
134                                 local goal_pitch = (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))*-1
135                                 
136                                 local current_pitch = self.head_rotation.z
137                                                                 
138                                 --smoothly move head using dtime
139                                 if goal_pitch > current_pitch then
140                                         current_pitch = current_pitch + (dtime*5)
141                                 elseif goal_pitch < current_pitch then
142                                         current_pitch = current_pitch - (dtime*5)
143                                 end
144                                 
145                                 --stop jittering
146                                 if math.abs(goal_pitch - current_pitch) <= (dtime*5) then
147                                         current_pitch = goal_pitch
148                                 end
149                                 
150                                 --convert this into degrees for the attach code
151                                 local deg_yaw = degrees(current_yaw)
152                                 --this is rounded because it uses animation frames baked into the head model
153                                 local deg_pitch = math.floor(degrees(current_pitch) + 0.5)+90
154
155
156                                 self.child:set_attach(self.object, "", self.head_mount, vector.new(0,   deg_yaw , 0))
157                                 self.child:set_animation({x=deg_pitch,y=deg_pitch}, 15, 0, true)        
158                                 self.head_rotation = vector.new(0,    current_yaw,    current_pitch)
159                                 
160                                 return(true)
161                         --nothing to look at
162                         else
163                                 self.return_head_to_origin(self,dtime)
164                                 return(false)
165                         end
166                         
167                 --if nothing to look at
168                 else
169                         self.return_head_to_origin(self,dtime)
170                         return(false)
171                 end
172         end
173 end
174
175
176 --this sets the mob to move it's head back to pointing forwards
177 pig.return_head_to_origin = function(self,dtime)
178         local current_yaw = self.head_rotation.y
179         local current_pitch = self.head_rotation.z
180         
181         --make the head yaw move back
182         if current_yaw > 0 then
183                 current_yaw = current_yaw - (dtime*5)
184         elseif current_yaw < 0 then
185                 current_yaw = current_yaw + (dtime*5)
186         end
187         
188         --finish rotation
189         if math.abs(current_yaw) <= (dtime*5) then
190                 current_yaw = 0
191         end
192         
193         --move up down (pitch) back to center
194         if current_pitch > 0 then
195                 current_pitch = current_pitch - (dtime*5)
196         elseif current_pitch < 0 then
197                 current_pitch = current_pitch + (dtime*5)
198         end
199         
200         --finish rotation
201         if math.abs(current_pitch) <= (dtime*5) then
202                 current_pitch = 0
203         end
204         
205         --convert this into degrees for the attach code
206         local deg_yaw = degrees(current_yaw)
207         --this is rounded because it uses animation frames baked into the head model
208         local deg_pitch = math.floor(degrees(current_pitch) + 0.5)+90
209         
210         self.child:set_attach(self.object, "", self.head_mount, vector.new(0,   deg_yaw , 0))
211         self.child:set_animation({x=deg_pitch,y=deg_pitch}, 15, 0, true)        
212         self.head_rotation = vector.new(0,    current_yaw,    current_pitch)
213 end