diff --git a/init.lua b/init.lua index b306b99..6e72c76 100644 --- a/init.lua +++ b/init.lua @@ -41,6 +41,10 @@ local neighbors ={ -- UTILITY FUNCTIONS +function mobkit.dot(v1,v2) + return v1.x*v2.x+v1.y*v2.y+v1.z*v2.z +end + function mobkit.dir2neighbor(dir) dir.y=0 dir=vector.round(vector.normalize(dir)) @@ -285,8 +289,8 @@ function mobkit.turn2yaw(self,tyaw,rate) local nyaw = (yaw+step*dir)%(pi*2) self.object:set_yaw(nyaw-pi) - if nyaw==tyaw then return true - else return false end + if nyaw==tyaw then return true, nyaw-pi + else return false, nyaw-pi end end function mobkit.dir_to_rot(v,rot) @@ -571,13 +575,24 @@ function mobkit.goto_next_waypoint(self,tpos) return true end -function mobkit.go_forward_horizontal(self,yaw,speed) -- sets velocity in yaw direction, y component unaffected +function mobkit.go_forward_horizontal(self,speed) -- sets velocity in yaw direction, y component unaffected local y = self.object:get_velocity().y + local yaw = self.object:get_yaw() local vel = vector.multiply(minetest.yaw_to_dir(yaw),speed) vel.y = y self.object:set_velocity(vel) end +function mobkit.drive_to_pos(self,tpos,speed,turn_rate,dist) + local pos=self.object:get_pos() + dist = dist or 0.2 + if mobkit.isnear2d(pos,tpos,dist) then return true end + local tyaw = minetest.dir_to_yaw(vector.direction(pos,tpos)) + mobkit.turn2yaw(self,tyaw,turn_rate) + mobkit.go_forward_horizontal(self,speed) + return false +end + function mobkit.timer(self,s) -- returns true approx every s seconds local t1 = floor(self.time_total) local t2 = floor(self.time_total+self.dtime) @@ -1469,8 +1484,8 @@ function mobkit.hq_aqua_roam(self,prty,speed) end mobkit.turn2yaw(self,tyaw,3) - local yaw = self.object:get_yaw() - mobkit.go_forward_horizontal(self,yaw,speed) +-- local yaw = self.object:get_yaw() + mobkit.go_forward_horizontal(self,speed) end mobkit.queue_high(self,func,prty) end @@ -1478,8 +1493,8 @@ end function mobkit.hq_aqua_turn(self,prty,tyaw,speed) local func = function(self) local finished=mobkit.turn2yaw(self,tyaw) - local yaw = self.object:get_yaw() - mobkit.go_forward_horizontal(self,yaw,speed) +-- local yaw = self.object:get_yaw() + mobkit.go_forward_horizontal(self,speed) if finished then return true end end mobkit.queue_high(self,func,prty) @@ -1530,7 +1545,7 @@ function mobkit.hq_aqua_attack(self,prty,tgtobj,speed) mobkit.hq_aqua_turn(self,prty,yaw-pi,speed) return true end - mobkit.go_forward_horizontal(self,yaw,speed) + mobkit.go_forward_horizontal(self,speed) end mobkit.queue_high(self,func,prty) end diff --git a/mobkit_api.txt b/mobkit_api.txt index 3ff115b..bb12faf 100644 --- a/mobkit_api.txt +++ b/mobkit_api.txt @@ -241,6 +241,13 @@ function mobkit.get_terrain_height(pos,steps) -- surface height at pos, or nil if not found -- liquid flag: true if found surface is covered with liquid +function mobkit.turn2yaw(self,tyaw,rate) + -- gradually turns towards yaw + -- self: luaentity + -- tyaw: target yaw in radians + -- rate: turn rate in rads/s + --returns: true if facing tyaw; current yaw + function mobkit.timer(self,s) -- returns true approx every s seconds -- used to reduce execution of code that needn't necessarily be done on every engine step @@ -326,9 +333,15 @@ function mobkit.make_sound(self,sound) -- sound is string, see entity definition -- makes an entity play sound, or does nothing if not defined -function mobkit.go_forward_horizontal(self,yaw,speed) +function mobkit.go_forward_horizontal(self,speed) -- sets an entity's horizontal velocity in yaw direction. Vertical velocity unaffected. - + +function mobkit.drive_to_pos(self,tpos,speed,turn_rate,dist) + -- moves in facing direction while gradually turning towards tpos, returns true if in dist distance from tpos + -- tpos: target position + -- speed: in m/s + -- turn_rate: in rad/s + -- dist: in m. -- Memory functions.