2 mobs.create_movement_functions = function(def,mob_register)
4 mob_register.swim = function(self,dtime)
5 local pos = self.object:get_pos()
7 local node = minetest.get_node(pos).name
9 if node == "main:water" or node =="main:waterflow" then
10 local vel = self.object:getvelocity()
12 local acceleration = vector.new(0,goal-vel.y,0)
13 --jump out of the water
14 if (vel.x == 0 and self.direction.x ~= 0) or (vel.z == 0 and self.direction.z ~= 0) then
15 self.object:set_velocity(vector.new(vel.x,5,vel.z))
18 self.object:add_velocity(acceleration)
24 local get_group = minetest.get_item_group
25 local get_node = minetest.get_node
26 mob_register.hurt_inside = function(self,dtime)
27 if self.hp > 0 and self.hurt_inside_timer <= 0 then
28 local pos = self.object:get_pos()
29 local hurty = get_group(get_node(pos).name, "hurt_inside")
31 self.object:punch(self.object, 2,
33 full_punch_interval=1.5,
34 damage_groups = {damage=hurty},
38 self.hurt_inside_timer = self.hurt_inside_timer - dtime
42 --This makes the mob walk at a certain speed and jump
43 if def.movement_type == "walk" then
44 mob_register.move = function(self,dtime,moveresult)
45 self.manage_jump_timer(self,dtime)
46 self.timer = self.timer - dtime
49 self.jump(self,moveresult)
55 --direction state change
56 if self.timer <= 0 and not self.following == true then
57 --print("changing direction")
58 self.timer = math.random(2,7)
59 self.direction = vector.new(math.random()*math.random(-1,1),0,math.random()*math.random(-1,1))
60 --local yaw = self.object:get_yaw() + dtime
61 self.speed = math.random(0,self.max_speed)
62 --self.object:set_yaw(yaw)
65 self.hurt_inside(self,dtime)
67 local currentvel = self.object:get_velocity()
68 local goal = vector.multiply(self.direction,self.speed)
69 local acceleration = vector.new(goal.x-currentvel.x,0,goal.z-currentvel.z)
70 acceleration = vector.multiply(acceleration, 0.05)
71 self.object:add_velocity(acceleration)
73 mob_register.jump = function(self,moveresult)
74 if moveresult and moveresult.touching_ground and self.direction then
75 local pos = self.object:get_pos()
78 if self.path_data and table.getn(self.path_data) > 0 then
80 local y = math.floor(pos.y+0.5)
81 local vel = self.object:get_velocity()
82 if y < self.path_data[1].y then
83 self.object:set_velocity(vector.new(vel.x,5,vel.z))
84 elseif self.path_data[2] and y < self.path_data[2].y then
85 self.object:set_velocity(vector.new(vel.x,5,vel.z))
86 elseif self.path_data[3] and y < self.path_data[3].y then
87 self.object:set_velocity(vector.new(vel.x,5,vel.z))
88 elseif ((vel.x == 0 and self.direction.x ~= 0) or (vel.z == 0 and self.direction.z ~= 0)) then
89 self.object:set_velocity(vector.new(vel.x,5,vel.z))
92 --assume collisionbox is even x and z
93 local modifier = self.object:get_properties().collisionbox[4]*3
96 local pos2 = vector.add(vector.multiply(self.direction,modifier),pos)
98 local ray = minetest.raycast(pos, pos2, false, false)
103 pointed_thing = ray:next()
106 if pointed_thing then
107 if minetest.get_nodedef(minetest.get_node(pointed_thing.under).name, "walkable") then
109 local vel = self.object:get_velocity()
110 --self.jump_timer = 1+math.random()
111 self.object:set_velocity(vector.new(vel.x,5,vel.z))
113 --print("velocity check")
114 local vel = self.object:get_velocity()
115 if (vel.x == 0 and self.direction.x ~= 0) or (vel.z == 0 and self.direction.z ~= 0) then
116 self.object:set_velocity(vector.new(vel.x,5,vel.z))
120 --print("velcheck 2")
121 local vel = self.object:get_velocity()
122 if (vel.x == 0 and self.direction.x ~= 0) or (vel.z == 0 and self.direction.z ~= 0) then
123 self.object:set_velocity(vector.new(vel.x,5,vel.z))
129 elseif def.movement_type == "jump" then
130 mob_register.move = function(self,dtime,moveresult)
131 self.manage_jump_timer(self,dtime)
132 self.timer = self.timer - dtime
135 self.jump(self,moveresult)
138 self.swim(self,dtime)
141 --direction state change
142 if self.timer <= 0 and not self.following == true then
143 --print("changing direction")
144 self.timer = math.random(2,7)
145 self.direction = vector.new(math.random()*math.random(-1,1),0,math.random()*math.random(-1,1))
146 --local yaw = self.object:get_yaw() + dtime
147 self.speed = math.random(0,self.max_speed)
148 --self.object:set_yaw(yaw)
151 self.hurt_inside(self,dtime)
153 local currentvel = self.object:get_velocity()
154 if currentvel.y ~= 0 then
155 local goal = vector.multiply(self.direction,self.speed)
156 local acceleration = vector.new(goal.x-currentvel.x,0,goal.z-currentvel.z)
157 acceleration = vector.multiply(acceleration, 0.05)
158 self.object:add_velocity(acceleration)
162 mob_register.jump = function(self,moveresult)
163 if moveresult and moveresult.touching_ground and self.direction then
164 if self.jump_timer <= 0 then
165 if self.make_jump_noise then
166 minetest.sound_play("slime_splat", {object=self.object, gain = 1.0, max_hear_distance = 10,pitch = math.random(80,100)/100})
168 local vel = self.object:get_velocity()
169 self.object:set_velocity(vector.new(vel.x,5,vel.z))
170 if self.following == true then
171 self.jump_timer = 0.5
173 self.jump_timer = 1+math.random()
176 self.object:set_velocity(vector.new(0,0,0))
182 if def.pathfinds then
183 mob_register.pathfinding = function(self,dtime)
184 if self.following and self.following_pos then
185 self.pathfinding_timer = self.pathfinding_timer + dtime
186 if self.pathfinding_timer > 1 then
187 self.pathfinding_timer = 0
189 local path = minetest.find_path(self.object:get_pos(),self.following_pos,self.view_distance*2,1,1,"A*_noprefetch")
191 if path and (not self.path_data or (self.path_data and table.getn(self.path_data) < 3)) then
192 self.path_data = path
195 if self.path_data then
196 for index,pos_data in pairs(self.path_data) do
197 --print(dump(pos_data))
198 minetest.add_particle({
200 velocity = {x=0, y=0, z=0},
201 acceleration = {x=0, y=0, z=0},
204 texture = "dirt.png",
210 local selfpos = self.object:get_pos()
211 local pos1 = vector.new(selfpos.x,0,selfpos.z)
212 if self.path_data and table.getn(self.path_data) > 0 and vector.distance(pos1,vector.new(self.path_data[1].x,0,self.path_data[1].z)) < 1 then
213 --shift whole list down
214 for i = 2,table.getn(self.path_data) do
215 self.path_data[i-1] = self.path_data[i]
217 self.path_data[table.getn(self.path_data)] = nil