]> git.lizzy.rs Git - Crafter.git/commitdiff
Implement first prototype of pathfinding
authoroilboi <47129783+oilboi@users.noreply.github.com>
Sat, 23 May 2020 03:43:13 +0000 (23:43 -0400)
committeroilboi <47129783+oilboi@users.noreply.github.com>
Sat, 23 May 2020 03:43:13 +0000 (23:43 -0400)
mods/mob/api/api_hook.lua
mods/mob/api/interaction.lua
mods/mob/api/movement.lua
mods/mob/init.lua

index 4d4ee838d8f8b526c431c5e6e1a7263543aec7ae..40b062697d773a1cb9b310ee8b685ab31205e7e0 100644 (file)
@@ -127,7 +127,8 @@ mob_register.mob = true
 mob_register.collision_boundary = def.collision_boundary or 1
 
 if def.pathfinds then
-       mob_register.path = {}
+       --mob_register.path = {}
+       mob_register.pathfinding_timer = 0
 end
 
 mobs.create_movement_functions(def,mob_register)
@@ -159,6 +160,10 @@ mob_register.on_step = function(self, dtime,moveresult)
                        self.look_around(self,dtime)
                end
                
+               if self.pathfinding then
+                       self.pathfinding(self,dtime)
+               end
+
                if self.handle_friendly_in_daylight_timer then
                        self.handle_friendly_in_daylight_timer(self,dtime)
                end
index 713fd7a4b7d75559c2500458fb7b96a104050ea3..c59c43818eae061583701f05c4207f09ecb56d76 100644 (file)
@@ -262,7 +262,8 @@ mobs.create_interaction_functions = function(def,mob_register)
                                --print(self.hostile)
                                if self.hostile == true then
                                        local distance = vector.distance(pos,pos2)
-                                       self.following_pos = vector.new(pos.x,pos.y-1.625,pos.z)
+                                       self.following_pos = vector.new(pos2.x,pos2.y-1.625,pos2.z)
+
                                        --punch the player
                                        if self.attack_type == "punch" then
                                                if distance < 2.5 and self.punch_timer <= 0 and object:get_hp() > 0 then
@@ -304,7 +305,13 @@ mobs.create_interaction_functions = function(def,mob_register)
                                                        end
                                                end
                                        end
-                                       self.direction = vector.direction(vector.new(pos.x,0,pos.z),vector.new(pos2.x,0,pos2.z))
+                                       --smart
+                                       if self.path_data and table.getn(self.path_data) > 0 then
+                                               self.direction = vector.direction(vector.new(pos.x,0,pos.z), vector.new(self.path_data[1].x,0,self.path_data[1].z))
+                                       --dumb
+                                       else
+                                               self.direction = vector.direction(vector.new(pos.x,0,pos.z),vector.new(pos2.x,0,pos2.z))
+                                       end
                                        self.speed = self.max_speed
                                        self.following = true
                                end
index 86fb26895bb48c0859834506e335818b1c94e96f..934e9716b449011a9642bb9a184d5f72b79fe0ad 100644 (file)
@@ -74,39 +74,55 @@ mobs.create_movement_functions = function(def,mob_register)
                        if moveresult and moveresult.touching_ground and self.direction then
                                local pos = self.object:get_pos()
                                pos.y = pos.y+0.1
-                               --assume collisionbox is even x and z
-                               local modifier = self.object:get_properties().collisionbox[4]*3
-                               
 
-                               local pos2 = vector.add(vector.multiply(self.direction,modifier),pos)
+                               if self.path_data and table.getn(self.path_data) > 0 then
+                                       --smart jump
+                                       local y = math.floor(pos.y+0.5)
+                                       local vel = self.object:get_velocity()
+                                       if y < self.path_data[1].y then
+                                               self.object:set_velocity(vector.new(vel.x,5,vel.z))
+                                       elseif self.path_data[2] and y < self.path_data[2].y then
+                                               self.object:set_velocity(vector.new(vel.x,5,vel.z))
+                                       elseif self.path_data[3] and y < self.path_data[3].y then
+                                               self.object:set_velocity(vector.new(vel.x,5,vel.z))
+                                       elseif ((vel.x == 0 and self.direction.x ~= 0) or (vel.z == 0 and self.direction.z ~= 0)) then
+                                               self.object:set_velocity(vector.new(vel.x,5,vel.z))
+                                       end
+                               else
+                                       --assume collisionbox is even x and z
+                                       local modifier = self.object:get_properties().collisionbox[4]*3
+                                       
 
-                               local ray = minetest.raycast(pos, pos2, false, false)
-                               
-                               local pointed_thing
+                                       local pos2 = vector.add(vector.multiply(self.direction,modifier),pos)
 
-                               if ray then
-                                       pointed_thing = ray:next()
-                               end
+                                       local ray = minetest.raycast(pos, pos2, false, false)
                                        
-                               if pointed_thing then
-                                       if minetest.get_nodedef(minetest.get_node(pointed_thing.under).name, "walkable") then
-                                               --print("jump")
-                                               local vel = self.object:get_velocity()
-                                               --self.jump_timer = 1+math.random()
-                                               self.object:set_velocity(vector.new(vel.x,5,vel.z))
+                                       local pointed_thing
+
+                                       if ray then
+                                               pointed_thing = ray:next()
+                                       end
+                                               
+                                       if pointed_thing then
+                                               if minetest.get_nodedef(minetest.get_node(pointed_thing.under).name, "walkable") then
+                                                       --print("jump")
+                                                       local vel = self.object:get_velocity()
+                                                       --self.jump_timer = 1+math.random()
+                                                       self.object:set_velocity(vector.new(vel.x,5,vel.z))
+                                               else
+                                                       --print("velocity check")
+                                                       local vel = self.object:get_velocity()
+                                                       if (vel.x == 0 and self.direction.x ~= 0) or (vel.z == 0 and self.direction.z ~= 0) then
+                                                               self.object:set_velocity(vector.new(vel.x,5,vel.z))
+                                                       end
+                                               end
                                        else
-                                               --print("velocity check")
+                                               --print("velcheck 2")
                                                local vel = self.object:get_velocity()
                                                if (vel.x == 0 and self.direction.x ~= 0) or (vel.z == 0 and self.direction.z ~= 0) then
                                                        self.object:set_velocity(vector.new(vel.x,5,vel.z))
                                                end
                                        end
-                               else
-                                       --print("velcheck 2")
-                                       local vel = self.object:get_velocity()
-                                       if (vel.x == 0 and self.direction.x ~= 0) or (vel.z == 0 and self.direction.z ~= 0) then
-                                               self.object:set_velocity(vector.new(vel.x,5,vel.z))
-                                       end
                                end
                        end
                end
@@ -164,8 +180,43 @@ mobs.create_movement_functions = function(def,mob_register)
        end
        
        if def.pathfinds then
+               mob_register.pathfinding = function(self,dtime)
+                       if self.following and self.following_pos then
+                               self.pathfinding_timer = self.pathfinding_timer + dtime
+                               if self.pathfinding_timer > 1 then
+                                       self.pathfinding_timer = 0
+
+                                       local path = minetest.find_path(self.object:get_pos(),self.following_pos,self.view_distance*2,1,1,"A*_noprefetch")
 
+                                       if path and (not self.path_data or (self.path_data and table.getn(self.path_data) < 3)) then
+                                               self.path_data = path
+                                       end
 
+                                       if self.path_data then
+                                               for index,pos_data in pairs(self.path_data) do
+                                                       --print(dump(pos_data))
+                                                       minetest.add_particle({
+                                                               pos = pos_data,
+                                                               velocity = {x=0, y=0, z=0},
+                                                               acceleration = {x=0, y=0, z=0},
+                                                               expirationtime = 1,
+                                                               size = 1,
+                                                               texture = "dirt.png",
+                                                       })
+                                               end
+                                       end
+                               end
+                       end
+                       local selfpos = self.object:get_pos()
+                       local pos1 = vector.new(selfpos.x,0,selfpos.z)
+                       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
+                               --shift whole list down
+                               for i = 2,table.getn(self.path_data) do
+                                       self.path_data[i-1] = self.path_data[i]
+                               end
+                               self.path_data[table.getn(self.path_data)] = nil
+                       end
+               end
        end
        
        return(mob_register)
index 3b60a9fa2232ad70d41abc1e4b8e4043b095dc62..13e285dff7a1713bb22e56ee0f84cc272f1216d8 100644 (file)
@@ -630,8 +630,6 @@ mobs.register_mob(
         },
         
         --these are used to anchor a point to the head position
-
-
         -----
         head_bone = "body.head",
         debug_head_pos = false,
@@ -668,6 +666,8 @@ mobs.register_mob(
         die_sound = "spider_die",
         
         
+        pathfinds = true,
+
         hostile = true,
         friendly_in_daylight = true,
         attacked_hostile = true,