Guest User

Untitled

a guest
Aug 27th, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.75 KB | None | 0 0
  1. local char = script.Parent
  2. local head = char:WaitForChild("Head")
  3. local torso = char:WaitForChild("Torso")
  4. local hum = char:WaitForChild("Humanoid")
  5. local destReachRange = 3
  6. local losRange = 128
  7. local hearRange = 48
  8. local ai_max_range = math.huge
  9. local attackRange = 2 --65
  10. local runSpeed = 16
  11. local patrolSpeed = 6
  12.  
  13. local Orakel = require(game.ReplicatedStorage.Orakel.Main)
  14. local npcLib = Orakel.LoadModule("NpcLib")
  15. local pathLib = Orakel.LoadModule("PathLib")
  16.  
  17. local map = workspace.Map
  18. local masterTable, mnt_index = pathLib.CollectNodes(map.Nodes)
  19.  
  20.  
  21. math.randomseed(os.time())
  22.  
  23.  
  24. local gyro = Instance.new("BodyGyro", script.Parent.Torso)
  25. gyro.D = 100
  26. gyro.P = 400
  27. gyro.MaxTorque = Vector3.new(0, 400000, 0)
  28.  
  29.  
  30. function rotateTowards(goal)
  31.     if gyro ~= nil then
  32.         gyro.CFrame = CFrame.new(torso.Position, goal) - torso.Position
  33.     end
  34. end
  35.  
  36.  
  37. function shuffleTable(tab)
  38.     local t = tab
  39.     local rand = math.random
  40.     assert( t, "shuffleTable() expected a table, got nil" )
  41.     local iterations = #t
  42.     local j
  43.    
  44.     for i = iterations, 2, -1 do
  45.         j = rand(i)
  46.         t[i], t[j] = t[j], t[i]
  47.     end
  48.     return t
  49. end
  50.  
  51. function hasReachedDestination(dest)
  52.     if (torso.Position - dest.Position).magnitude <= destReachRange then
  53.         return true
  54.     else
  55.         return false
  56.     end
  57. end
  58.  
  59.  
  60. function lineOfSight(a, b)
  61.     local los = false
  62.     local vis = npcLib.LineOfSight(a, b, losRange, _G.ignorelist)
  63.     local behind = (b.CFrame:toObjectSpace(a.CFrame).p.Z < 0)
  64.     if vis and not behind then
  65.         los = true
  66.     end
  67.     return los
  68. end
  69.  
  70. function attack()
  71.  
  72.    
  73.    
  74. end
  75.  
  76.  
  77.  
  78. function chooseNodeInArea(areanode)
  79.     local validNodes = pathLib.GetNodesVisibleInRadius(areanode.Position, 32, true, workspace.Map.Nodes, masterTable)
  80.     return validNodes[math.random(1,#validNodes)]
  81. end
  82.  
  83.  
  84. function chooseTarget()
  85.     --[[
  86.     local targets = {}
  87.     --Find Player targets
  88.     for _, plrInGame in pairs(game.Players:GetPlayers()) do
  89.         local plr = plrInGame.Character
  90.         if plr ~= nil then
  91.             if plr.Humanoid.Health > 0 then
  92.                 local los = lineOfSight(head, plr.Head)
  93.                 if los then
  94.                     table.insert(targets, plr)
  95.                 else
  96.                     --[[if plr.EmittingSound.Value then
  97.                         local dist = (torso.Position - plr.Torso.Position).magnitude
  98.                         if dist <= hearRange then
  99.                             table.insert(targets, plr)
  100.                         end
  101.                     end]]
  102. --[[                end
  103.             end
  104.         end
  105.     end
  106.    
  107.     if #targets > 1 then
  108.         local nearest = targets[1]
  109.         for t = 2, #targets do
  110.             local old = (nearest.Torso.Position - torso.Position).magnitude
  111.             local new = (targets[t].Torso.Position - torso.Position).magnitude
  112.             if new < old then
  113.                 nearest = targets[t]
  114.             end
  115.         end
  116.     elseif #targets == 1 then
  117.         return targets[1]
  118.     end
  119.     ]]
  120.     return nil
  121. end
  122.  
  123.  
  124.  
  125.  
  126. function pathFind(target, startPos, goalPos)
  127.     local startID = pathLib.GetNearestNode(startPos, false, map.Nodes, masterTable)
  128.     local goalID = pathLib.GetNearestNode(goalPos, false, map.Nodes, masterTable)
  129.     local path = pathLib.AStar(masterTable, startID, goalID)
  130.  
  131.     for nodeNum, node in pairs(path) do
  132.         local eta = npcLib.EstimatedPathTime(torso.Position, node.Position, hum.WalkSpeed)
  133.         local timeWalked = 0
  134.        
  135.         while true do
  136.             hum:MoveTo(node.Position)
  137.             rotateTowards(node.Position)
  138.             local act = wait(1/20)
  139.             timeWalked = timeWalked + act
  140.             local newTarget = chooseTarget()
  141.             --Reached node, Move to next node
  142.             if hasReachedDestination(node) then
  143.                 break
  144.             end
  145.             --Target changed!
  146.             if newTarget ~= target and newTarget ~= nil then
  147.                 warn("-> Target changed from "..tostring(target).." to "..tostring(newTarget)..", aborting pathfind!")
  148.                 return false
  149.             end
  150.             --Timed out! Return false, acquire new path
  151.             if timeWalked > eta then
  152.                 warn("-> Timed out!")
  153.                 return false
  154.             end
  155.             --Got line of sight on target, Stop finding the path and just walk there
  156.             if target.Parent:FindFirstChild("Humanoid") then
  157.                 if npcLib.LineOfSight(head, target.Torso, losRange, _G.ignorelist) and (torso.Position - target.Torso.Position).magnitude <= 25 then
  158.                     warn("-> Got line of sight on target, Stop pathfinding")
  159.                     return false
  160.                 end
  161.             end
  162.         end
  163.     end
  164.     return true
  165. end
  166.  
  167.  
  168.  
  169. warn("NPC loaded")
  170.  
  171.  
  172. while true do
  173.     wait(1/30)
  174.     local target-- = chooseTarget()
  175.  
  176.     if target == nil then
  177.         --Area nodes are basically positions of rooms, each room will have one area node
  178.         local areanodes = map.AreaNodes:GetChildren()
  179.         areanodes = shuffleTable(areanodes)
  180.         for node = 1, #areanodes do
  181.             print("Patrolling area #"..node)
  182.             local patrolLocation = chooseNodeInArea(areanodes[node])
  183.             local success = pathFind(patrolLocation, torso.Position, patrolLocation.Position)
  184.             if not success then
  185.                 warn("Pathfind was not a success")
  186.                 break
  187.             else
  188.                 wait(math.random(3, 6))
  189.             end
  190.         end
  191.     else
  192.        
  193.     end
  194. end
Add Comment
Please, Sign In to add comment