Advertisement
Guest User

Mobf by sapier ( edited by Mallot1 )

a guest
Jun 21st, 2013
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.31 KB | None | 0 0
  1. -------------------------------------------------------------------------------
  2. -- Mob Framework Mod by Sapier
  3. --
  4. -- You may copy, use, modify or do nearly anything except removing this
  5. -- copyright notice.
  6. -- And of course you are NOT allow to pretend you have written it.
  7. --
  8. --! @file path_based_movement_gen.lua
  9. --! @brief component containing a path based movement generator
  10. --! @copyright Sapier
  11. --! @author Sapier
  12. --! @date 2012-08-09
  13. --
  14. --! @defgroup mgen_path_based MGEN: Path based movement generator
  15. --! @ingroup framework_int
  16. --! @{
  17. -- Contact sapier a t gmx net
  18. -------------------------------------------------------------------------------
  19.  
  20. --! @class p_mov_gen
  21. --! @brief a movement generator evaluating a path to a target and following it
  22. --!@}
  23. p_mov_gen = {}
  24.  
  25.  
  26.  
  27. --! @brief movement generator identifier
  28. --! @memberof p_mov_gen
  29. p_mov_gen.name = "mgen_path"
  30.  
  31. -------------------------------------------------------------------------------
  32. -- name: callback(entity,now)
  33. --
  34. --! @brief path based movement generator callback
  35. --! @memberof p_mov_gen
  36. --
  37. -- param1: mob to do movement
  38. -- param2: current time
  39. -- retval: -
  40. -------------------------------------------------------------------------------
  41. function p_mov_gen.callback(entity,now,dstep)
  42.  
  43.     mobf_assert_backtrace(entity ~= nil)
  44.     mobf_assert_backtrace(entity.dynamic_data ~= nil)
  45.     mobf_assert_backtrace(entity.dynamic_data.p_movement ~= nil)
  46.    
  47.     if entity.dynamic_data.p_movement.eta ~= nil then
  48.         if now < entity.dynamic_data.p_movement.eta then
  49.             return
  50.         end
  51.     end
  52.    
  53.     local current_pos = entity.object:getpos()
  54.     local handled = false
  55.    
  56.     if entity.dynamic_data.p_movement.path == nil then
  57.         dbg_mobf.path_mov_lvl1(
  58.                 "MOBF: path movement but mo path set!!")
  59.         return
  60.     end
  61.    
  62.    
  63.     local max_distance = entity.data.movement.max_distance
  64.    
  65.     if entity.dynamic_data.movement.max_distance ~= nil then
  66.         max_distance = entity.dynamic_data.movement.max_distance
  67.     end
  68.    
  69.     mobf_assert_backtrace(entity.dynamic_data.p_movement.next_path_index ~= nil)
  70.     mobf_assert_backtrace(max_distance ~= nil)
  71.    
  72.     --check if target is reached
  73.     if p_mov_gen.distance_to_next_point(entity,current_pos)
  74.                         < max_distance then
  75.         dbg_mobf.path_mov_lvl1("MOBF: pathmov next to next point switching target")
  76.         local update_target = true
  77.        
  78.         --return to begining of path
  79.         if entity.dynamic_data.p_movement.next_path_index
  80.                 == #entity.dynamic_data.p_movement.path then
  81.                
  82.             if entity.data.patrol ~= nil and
  83.                 entity.data.patrol.cycle_path then
  84.                 --0 is correct as it's incremented by one later
  85.                 entity.dynamic_data.p_movement.next_path_index = 0
  86.             else
  87.                 dbg_mobf.path_mov_lvl1("MOBF: cycle not set not updating point")
  88.                 update_target = false
  89.             end
  90.         end
  91.        
  92.         if update_target then
  93.             entity.dynamic_data.p_movement.next_path_index =
  94.                 entity.dynamic_data.p_movement.next_path_index + 1
  95.                
  96.             entity.dynamic_data.movement.target =
  97.                 entity.dynamic_data.p_movement.path
  98.                     [entity.dynamic_data.p_movement.next_path_index]
  99.            
  100.             dbg_mobf.path_mov_lvl1("MOBF: (1) setting new target to index: " ..
  101.                 entity.dynamic_data.p_movement.next_path_index .. " pos: " ..
  102.                 printpos(entity.dynamic_data.movement.target))
  103.             handled = true
  104.         end
  105.     end
  106.    
  107.     if not handled and
  108.         entity.dynamic_data.movement.target == nil then
  109.        
  110.         entity.dynamic_data.movement.target =
  111.                 entity.dynamic_data.p_movement.path
  112.                     [entity.dynamic_data.p_movement.next_path_index]
  113.                
  114.         dbg_mobf.path_mov_lvl1("MOBF: (2) setting new target to index: " ..
  115.                 entity.dynamic_data.p_movement.next_path_index .. " pos: " ..
  116.                 printpos(entity.dynamic_data.movement.target))
  117.     end
  118.    
  119.     mgen_follow.callback(entity,now)
  120. end
  121.  
  122.  
  123. -------------------------------------------------------------------------------
  124. -- name: distance_to_next_point(entity)
  125. --
  126. --! @brief get distance to next target point (2d only)
  127. --! @memberof p_mov_gen
  128. --! @private
  129. --
  130. --! @param entity mob to check
  131. --! @param current_pos position mob is atm
  132. --
  133. --! @retval distance
  134. -------------------------------------------------------------------------------
  135. function p_mov_gen.distance_to_next_point(entity,current_pos)
  136.     local index = entity.dynamic_data.p_movement.next_path_index
  137.     mobf_assert_backtrace(entity.dynamic_data.p_movement.path ~= nil)
  138.     mobf_assert_backtrace(index <= #entity.dynamic_data.p_movement.path)
  139.     return mobf_calc_distance_2d(current_pos,
  140.         entity.dynamic_data.p_movement.path[index])
  141. end
  142.  
  143. -------------------------------------------------------------------------------
  144. -- name: init_dynamic_data(entity,now)
  145. --
  146. --! @brief initialize dynamic data required by movement generator
  147. --! @memberof p_mov_gen
  148. --
  149. --! @param entity to initialize
  150. --! @param now current time
  151. --! @param restored_data data restored on activate
  152. -------------------------------------------------------------------------------
  153. function p_mov_gen.init_dynamic_data(entity,now,restored_data)
  154.  
  155.     local pos = entity.object:getpos()
  156.  
  157.     local data = {
  158.             path                = nil,
  159.             eta                 = nil,
  160.             last_move_stop      = now,
  161.             next_path_index     = 1,
  162.             force_target        = nil,
  163.             pathowner           = nil,
  164.             pathname            = nil,
  165.             }
  166.    
  167.     if restored_data ~= nil then
  168.         dbg_mobf.path_mov_lvl3(
  169.             "MOBF: path movement reading stored data: " .. dump(restored_data))
  170.         if restored_data.pathowner ~= nil and
  171.             restored_data.pathname ~= nil then
  172.             data.pathowner = restored_data.pathowner
  173.             data.pathname = restored_data.pathname
  174.            
  175.             data.path = mobf_path.getpoints(data.pathowner,data.pathname)
  176.             dbg_mobf.path_mov_lvl3(
  177.                 "MOBF: path movement restored points: " .. dump(data.path))
  178.         end
  179.        
  180.         if restored_data.pathindex ~= nil and
  181.             type(restored_data.pathindex) == "number" and
  182.             restored_data.pathindex > 0 and
  183.             data.path ~= nil and
  184.             restored_data.pathindex < #data.path then
  185.             data.next_path_index = restored_data.pathindex
  186.         end
  187.     end
  188.    
  189.     entity.dynamic_data.p_movement = data
  190.    
  191.     mgen_follow.init_dynamic_data(entity,now)
  192. end
  193.  
  194. -------------------------------------------------------------------------------
  195. -- name: set_target(entity,target)
  196. --
  197. --! @brief set target for movgen
  198. --! @memberof p_mov_gen
  199. --! @public
  200. --
  201. --! @param entity mob to apply to
  202. --! @param target to set
  203. -------------------------------------------------------------------------------
  204. function p_mov_gen.set_target(entity,target)
  205.     mobf_assert_backtrace(target ~= nil)
  206.    
  207.     local current_pos = entity.getbasepos(entity)
  208.     local targetpos = nil
  209.    
  210.     if not mobf_is_pos(target) then
  211.         if target:is_player() then
  212.             targetpos = target:getpos()
  213.             targetpos.y = targetpos.y +0.5
  214.         else
  215.             if type(target.getbasepos) == "function" then
  216.                 targetpos = target.getbasepos(target)
  217.             else
  218.                 targetpos = target:getpos()
  219.             end
  220.         end
  221.     else
  222.         targetpos = target
  223.     end
  224.    
  225.     if targetpos == nil then
  226.         return false
  227.     end
  228.    
  229.     if entity.dynamic_data.p_movement.lasttargetpos ~= nil then
  230.         if mobf_pos_is_same(entity.dynamic_data.p_movement.lasttargetpos,
  231.             targetpos) then
  232.             return true
  233.         end
  234.     end
  235.    
  236.     entity.dynamic_data.p_movement.lasttargetpos = targetpos
  237.    
  238.     entity.dynamic_data.p_movement.path = nil
  239.     entity.dynamic_data.p_movement.next_path_index = 1
  240.    
  241.     --on target mode max distance is always 0.5
  242.     entity.dynamic_data.movement.max_distance = 0.5
  243.    
  244.     --try to find path on our own
  245.     if type(minetest.env.find_path) == "function" then
  246.         print("trying to find path to: " .. printpos(targetpos))
  247.         entity.dynamic_data.p_movement.path =
  248.             minetest.env:find_path(current_pos,targetpos,5,1,1,"") --edited by Mallot1
  249.                    
  250.         if entity.dynamic_data.p_movement.path ~= nil then
  251.  
  252.             --a valid path has at least 2 positions
  253.             mobf_assert_backtrace(#entity.dynamic_data.p_movement.path > 1)
  254.             entity.dynamic_data.movement.target =
  255.                     entity.dynamic_data.p_movement.path[2]
  256.             return true
  257.         end
  258.     end
  259.    
  260.     if entity.dynamic_data.p_movement.path == nil then
  261.         print(
  262.             "MOBF: no pathfinding support/ no path found directly setting targetpos as path")
  263.            
  264.         entity.dynamic_data.p_movement.path = {}
  265.        
  266.         table.insert(entity.dynamic_data.p_movement.path,targetpos)
  267.         entity.dynamic_data.movement.target =
  268.                 entity.dynamic_data.p_movement.path[1]
  269.         return true
  270.     end
  271.    
  272.     return false
  273. end
  274.  
  275. --register this movement generator
  276. registerMovementGen(p_mov_gen.name,p_mov_gen)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement