Ubayla
|
  |
| Joined: 01 Dec 2008 |
| Total Posts: 157 |
|
|
| 19 Jun 2012 08:47 PM |
I think there's something wrong with my algorithm, because it basicly walks to random spots. It's very long, but I put markers to point out where the pathfinding functions are.
local ai = script.Parent local Torso = ai.Torso local Human = ai.Humanoid local Target = nil local ClosestDist = 50 local ClosestTarget = nil local Spaces = {} local db = false
------Settings------
local Speed = 14 local Damage = 10 local AttackSpeed = 0.5 local Mind = 0.5 local Distance = 50
------/Settings------
Human.WalkSpeed = Speed
function GetTarget() local ClosestDist = Distance local ClosestTarget = nil for a,b in pairs(game.Players:GetChildren()) do if b.Character then local TargetH = b.Character:FindFirstChild("Humanoid") if TargetH.Health > 0 then local d = b:DistanceFromCharacter(Torso.Position) if d < ClosestDist then ClosestDist = d ClosestTarget = b end end end end return ClosestTarget, ClosestDist end
function Stop() Human:MoveTo(Torso.Position, Torso) end
function Check() local Spot = FindPath(Torso.Position) if Spot then return Spot else return nil end end
function Attack(Hit) if db == false and Hit.Parent ~= nil and game.Players:GetPlayerFromCharacter(Hit.Parent) ~= nil then local h = Hit.Parent:FindFirstChild("Humanoid") if h then db = true h:TakeDamage(Damage) Delay(function () db = false end, AttackSpeed) end end end
------Pathfinding------
function CheckSpace(from, to) for i, v in pairs(Spaces) do if to == v then return false end end
------RAYS METHOD------ local Ray1 = Ray.new(from, (to - from).unit) local Block, Spot = game.Workspace:FindPartOnRay(Ray1, Torso, false) if Block then if Block.Parent == Target.Character then return true end if Ray1:Distance(Spot) > 1 then print(Ray1:Distance(Spot)) return true else return false end else return true end
------/RAYS METHOD------
------PARTS METHOD------ --[[ print("Checking (" .. to.x .. ", "..to.y..", " .. to.z .. ") from (" .. from.x .. ", " .. from.y .. ", " .. from.z .. ")") if game.Workspace:findFirstChild("Pathfinding Bricks") == nil then a = Instance.new("Model") a.Parent = game.Workspace a.Name = "Pathfinding Bricks" end a = Instance.new("Part") a.Parent = game.Workspace["Pathfinding Bricks"] a.Size = Vector3.new(1, 1, (from - to).magnitude) a.CFrame = CFrame.new(from + Vector3.new((CFrame.new(from, to).lookVector.x * Torso.Size.x/2) + a.Size.x/2,(CFrame.new(from, to).lookVector.y * (Torso.Size.y + script.Parent.Head.Size.y)/2) + a.Size.y/2,(CFrame.new(from, to).lookVector.z * Torso.Size.z/2) + a.Size.z/2), to) a.formFactor = "Symmetric" a.Transparency = 0.5 a.CanCollide = false a.Anchored = true a.TopSurface = "Smooth" a.BottomSurface = "Smooth" tesz = false a.Touched:connect(function(hit) if hit ~= Torso and hit ~= script.Parent.Head then tesz = true end end) wait() a:Remove() if tesz == false then -- print("Checking (" .. to.x .. ", "..to.y..", " .. to.z .. ") from (" .. from.x .. ", " .. from.y .. ", " .. from.z .. ") # Result: Free") return true else -- print("Checking (" .. to.x .. ", "..to.y..", " .. to.z .. ") from (" .. from.x .. ", " .. from.y .. ", " .. from.z .. ") # Result: Filled") return false end ]] ------/PARTS METHOD------ end
function CheckSpaces(s) if CheckSpace(s, s + Vector3.new(1,0,0)) == true then table.insert(Spaces, s + Vector3.new(1,0,0)) end -- if CheckSpace(s, s + Vector3.new(0,1,0)) == true then -- table.insert(Spaces, s + Vector3.new(0,1,0)) -- end if CheckSpace(s, s + Vector3.new(0,0,1)) == true then table.insert(Spaces, s + Vector3.new(0,0,1)) end if CheckSpace(s, s + Vector3.new(-1,0,0)) == true then table.insert(Spaces, s + Vector3.new(-1,0,0)) end -- if CheckSpace(s, s + Vector3.new(0,-1,0)) == true then -- table.insert(Spaces, s + Vector3.new(0,-1,0)) -- end if CheckSpace(s, s + Vector3.new(0,0,-1)) == true then table.insert(Spaces, s + Vector3.new(0,0,-1)) end end
function FindPath(s) Spaces = {} CheckSpaces(s) bestdiff = 9999 bestpos = {} for i, v in pairs(Spaces) do print(v) diff = 0 if v.x >= Target.Character.Torso.Position.x then diff = diff + (v.x - Target.Character.Torso.Position.x) else diff = diff + (Target.Character.Torso.Position.x - v.x) end if v.y >= Target.Character.Torso.Position.y then diff = diff + (v.y - Target.Character.Torso.Position.y) else diff = diff + (Target.Character.Torso.Position.y - v.y) end if v.z >= Target.Character.Torso.Position.z then diff = diff + (v.z - Target.Character.Torso.Position.z) else diff = diff + (Target.Character.Torso.Position.z - v.z) end if v.x >= Torso.Position.x then diff = diff + (v.x - Torso.Position.x) else diff = diff + (Torso.Position.x - v.x) end if v.y >= Torso.Position.y then diff = diff + (v.y - Torso.Position.y) else diff = diff + (Torso.Position.y - v.y) end if v.z >= Torso.Position.z then diff = diff + (v.z - Torso.Position.z) else diff = diff + (Torso.Position.z - v.z) end if diff <= bestdiff then print("(" ..v.x..", " ..v.y.. ", " ..v.z..") is the best spot so far") bestpos = {} table.insert(bestpos, v) bestdiff = diff end end if #bestpos > 1 then print("Choosing random spot") return bestpos[math.random(1, #bestpos)] elseif #bestpos == 1 then print("(" ..bestpos[1].x..", " ..bestpos[1].y.. ", " ..bestpos[1].z..") was the best spot") return bestpos[1] else print("No way") return nil end end
------/Pathfinding------
Torso.Touched:connect(Attack)
while true do wait(Mind) Target, TargetDist = GetTarget() if Target then local C = Check() if C then Stop() Human:MoveTo(C, Target.Character.Torso) else Stop() end end end
|
|
|
| Report Abuse |
|
|
Ubayla
|
  |
| Joined: 01 Dec 2008 |
| Total Posts: 157 |
|
|
| 19 Jun 2012 08:57 PM |
| Come on smart guys I need help! Bump |
|
|
| Report Abuse |
|
|
|
| 19 Jun 2012 09:37 PM |
| Sorry, I'm not experienced with this stuff so I can't help ya. Sorry man. |
|
|
| Report Abuse |
|
|
aboy5643
|
  |
| Joined: 08 Oct 2010 |
| Total Posts: 5458 |
|
|
| 19 Jun 2012 09:40 PM |
| It's a bit much to just read and find something in. It's not generally a skill you learn. Could you point out some spots where you THINK it might be going wrong?? At least then I can start somewhere and follow your thoughts a bit more. |
|
|
| Report Abuse |
|
|
| |
|
nate890
|
  |
| Joined: 22 Nov 2008 |
| Total Posts: 21686 |
|
|
| 19 Jun 2012 10:38 PM |
| Though I know my pathfinding (Very well, might I add), I can't simply find your error. |
|
|
| Report Abuse |
|
|
Ubayla
|
  |
| Joined: 01 Dec 2008 |
| Total Posts: 157 |
|
|
| 20 Jun 2012 12:24 AM |
| Check the FindPaths function, I think that's where I went wrong. If you want to see what is wrong with it, go to my place Bored. |
|
|
| Report Abuse |
|
|
|
| 20 Jun 2012 12:45 PM |
| I believe the first MoveTo's second argument should be the TARGET'S torso, you put the AI's torso. |
|
|
| Report Abuse |
|
|
Ubayla
|
  |
| Joined: 01 Dec 2008 |
| Total Posts: 157 |
|
|
| 20 Jun 2012 01:01 PM |
| Oh! That may just be it, I'll try that... But the point is empty space, that's the whole reason it's moving there. Should I create a part for it to walk to? Or is there a way for me to skip the second argument? |
|
|
| Report Abuse |
|
|
aboy5643
|
  |
| Joined: 08 Oct 2010 |
| Total Posts: 5458 |
|
|
| 20 Jun 2012 01:03 PM |
| I've always just used a random part for MoveTo. Hmmmm |
|
|
| Report Abuse |
|
|
Ubayla
|
  |
| Joined: 01 Dec 2008 |
| Total Posts: 157 |
|
|
| 20 Jun 2012 01:29 PM |
| Well now it's not moving... But it does seem to be getting the best path, just not moving through it... |
|
|
| Report Abuse |
|
|
| |
|
Ubayla
|
  |
| Joined: 01 Dec 2008 |
| Total Posts: 157 |
|
|
| 20 Jun 2012 01:46 PM |
| Should I try making it move in increments of 2 instead of 1? Because I was thinking maybe it thought it was there already, being only 1 away. |
|
|
| Report Abuse |
|
|
| |
|
Ubayla
|
  |
| Joined: 01 Dec 2008 |
| Total Posts: 157 |
|
|
| 20 Jun 2012 01:54 PM |
| Worked. Now I just need to work out the smaller things like walking over small parts... And lag. |
|
|
| Report Abuse |
|
|
|
| 20 Jun 2012 01:55 PM |
| Add a jump script when touched. I would like to see this finished, keep updating that place, please, XD. |
|
|
| Report Abuse |
|
|
nate890
|
  |
| Joined: 22 Nov 2008 |
| Total Posts: 21686 |
|
| |
|
Ubayla
|
  |
| Joined: 01 Dec 2008 |
| Total Posts: 157 |
|
|
| 20 Jun 2012 02:23 PM |
| Ugh, I think I messed up. It can't make it walk in increments of 1 or 2 because then it's too close and it just won't move, but with 3 it somehow decides walls aren't there (even though I use rays, so it should detect the walls) and tries to walk through them... |
|
|
| Report Abuse |
|
|
MrNicNac
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 26567 |
|
|
| 20 Jun 2012 02:47 PM |
| To be honest, you have quite a messy API to be doing all this in. I really think it would be a good idea to rewrite it - with more of a dynamic library in mind. |
|
|
| Report Abuse |
|
|
Ubayla
|
  |
| Joined: 01 Dec 2008 |
| Total Posts: 157 |
|
|
| 20 Jun 2012 03:05 PM |
| Good point... I might just do that. |
|
|
| Report Abuse |
|
|
3lex33
|
  |
| Joined: 08 Oct 2008 |
| Total Posts: 5220 |
|
|
| 20 Jun 2012 03:31 PM |
For your last error, i think that your AI dont count walls, it just counts if where would it land. Examples(o = empty space, | = wall, x = AI):
Walkspeed = 2:
xo|
AI thinks that if he will walk right on 2, he will come into wall, so he dont moves even on one space
Walkspeed = 3:
xo|o
AI counts, that if he moves right on 3 spaces, he will come into empty space, so he ignore wall. That might be problem with walk speed of AI. |
|
|
| Report Abuse |
|
|
Ubayla
|
  |
| Joined: 01 Dec 2008 |
| Total Posts: 157 |
|
|
| 21 Jun 2012 01:07 AM |
| I don't think so, because I use rays to check if there are any parts in the way... I think it's the algorithm that's the problem at this point. |
|
|
| Report Abuse |
|
|