var/MaxAISteps = 100
var/obj/Cheese = null
turf
Floor
icon = 'Icons.dmi'
icon_state = "Floor"
Wall
icon = 'Icons.dmi'
icon_state = "Wall"
density = 1
mob
AI
icon = 'Icons.dmi'
icon_state = "AI"
verb
Run()
set src in world
AI_WalkTo(src,Cheese.loc,Step = 0)
obj
Cheese
icon = 'Icons.dmi'
icon_state = "Cheese"
New()
..()
Cheese = src
proc
GetH(turf/A,turf/B)
return 10*(abs(A.x - B.x) + abs(A.y - B.y))
AI_WalkTo(mob/AI/Source,turf/Location,list/Open,list/Closed,list/NParent,Step)
if(!Closed)
Closed = list()
if(!(isturf(Source)))
Source = Source.loc
Closed[Source] = list()
Closed[Source]["Cost"] = 0
Closed[Source]["GCost"] = 0
if(!Open)
Open = list()
if(Source in Open)
Closed[Source] = list()
Closed[Source]["Parent"] = Open[Source]["Parent"]
Closed[Source]["GCost"] = Open[Source]["Cost"]
Closed[Source]["Cost"] = Open[Source]["Cost"]
Open -= Source
Source.icon_state = "Path"
var/turf/N = get_step(Source,NORTH)
var/turf/E = get_step(Source,EAST)
var/turf/S = get_step(Source,SOUTH)
var/turf/W = get_step(Source,WEST)
var/C
var/CCost
if(N)
if(!(N.density) && !(N in Closed))
Open[N] = list()
Open[N]["Parent"] = Source
Open[N]["GCost"] = (Closed[Source]["GCost"]+10)
Open[N]["Cost"] = GetH(N,Location) + Open[N]["GCost"]
world << "[GetH(N,Location)] + [Closed[Source]["Cost"]+10] = [Open[N]["Cost"]]"
if(!C)
C = N
CCost = Open[N]["Cost"]
else if(Open[N]["Cost"] <= CCost)
C = N
CCost = Open[N]["Cost"]
if(E)
if(!(E.density) && !(E in Closed))
Open[E] = list()
Open[E]["Parent"] = Source
Open[E]["GCost"] = (Closed[Source]["GCost"]+10)
Open[E]["Cost"] = GetH(E,Location) + Open[E]["GCost"]
world << "[GetH(N,Location)] + [Closed[Source]["Cost"]+10] = [Open[E]["Cost"]]"
if(!C)
C = E
CCost = Open[E]["Cost"]
else if(Open[E]["Cost"] <= CCost)
C = E
CCost = Open[E]["Cost"]
if(S)
if(!(S.density) && !(S in Closed))
Open[S] = list()
Open[S]["Parent"] = Source
Open[S]["GCost"] = (Closed[Source]["GCost"]+10)
Open[S]["Cost"] = GetH(S,Location) + Open[S]["GCost"]
world << "[GetH(N,Location)] + [Closed[Source]["Cost"]+10] = [Open[S]["Cost"]]"
if(!C)
C = S
CCost = Open[S]["Cost"]
else if(Open[S]["Cost"] <= CCost)
C = S
CCost = Open[S]["Cost"]
if(W)
if(!(W.density) && !(W in Closed))
Open[W] = list()
Open[W]["Parent"] = Source
Open[W]["GCost"] = (Closed[Source]["GCost"]+10)
Open[W]["Cost"] = GetH(W,Location) + Open[W]["GCost"]
world << "[GetH(N,Location)] + [Closed[Source]["Cost"]+10] = [Open[W]["Cost"]]"
if(!C)
C = W
CCost = Open[W]["Cost"]
else if(Open[W]["Cost"] <= CCost)
C = W
CCost = Open[W]["Cost"]
if(Location in Closed)
return
if(!C)
for(var/a in Open)
if(!C)
world << "Set to [Open[a]["Cost"]]"
C = a
CCost = Open[a]["Cost"]
else if(Open[a]["Cost"] <= CCost)
world << "Set to [Open[a]["Cost"]]"
C = a
CCost = Open[a]["Cost"]
Step++
spawn(10) AI_WalkTo(C,Location,Open,Closed,Step)
Long story short, i wanted to create an advanced AI, i researched it and found out about A* and so i tried it out, after a bit of research and learning this was my code in the end(i read no other source code so i had to code it while i read about it with no reference or hint of what the code should look like so sorry if its a bit sloppy) what this code is suppose to do is make a green path from AI to Cheese, its no suppose to make AI move to cheese or anything just show the green outline("Path" is a icon_state in 'icons.dmi' thats pure green) the code works great but theres a small problem, at the beginning its the AI is in a "canyon" and it fills up said canyon before going along the outside of the wall to the cave entrance and heading straight to the cheese, anyone know why its doing this, ive read and read and tried to iron it out but my attempts just make it worse, very tired atm since its 3:00AM here, but if anyone could help that would be great :)