function path(mapid,sx,sy,ex,ey,length_)
	local cx,cy = 0,0
	
	local done = false
	local found = false
	local result = ""
	local costhere = 0
	local list = {}
	local cmap = {}
	
	local map = maps[mapid]
	local w = map.w
	local h = map.h
	
	table.insert(list, {0, sx, sy})
	local tid_ = sx + sy * w
	cmap[tid_] = {}
	local ctile_ = cmap[tid_]
	ctile_.cost = 0
	ctile_.result = ""
	
	local dir,ox,oy = 4,0,0
	local limit = 0
	local length = 60
	
	if (length_ ~= nil) then
		length = length_
	end
	
	while (done == false) do
		local c = list[1]
		cx = c[2]
		cy = c[3]
		
		table.remove(list, 1)
		
		local tid = cx + cy * w
		local ctile = cmap[tid]
		
		costhere = ctile.cost
		result = ctile.result
		
		dir,ox,oy = 0,1,0
		list,cmap = check(mapid,cx,cy,ox,oy,dir,result,costhere,list,cmap,ex,ey)
		
		dir,ox,oy = 1,0,-1
		list,cmap = check(mapid,cx,cy,ox,oy,dir,result,costhere,list,cmap,ex,ey)
		
		dir,ox,oy = 2,-1,0
		list,cmap = check(mapid,cx,cy,ox,oy,dir,result,costhere,list,cmap,ex,ey)
		
		dir,ox,oy = 3,0,1
		list,cmap = check(mapid,cx,cy,ox,oy,dir,result,costhere,list,cmap,ex,ey)
		
		limit = limit + 1
		
		if (cx == ex) and (cy == ey) then
			done = true
			found = true
			print("Found! " .. result)
		elseif (limit > length) then
			done = true
			print("Not found! " .. result)
		elseif (#list == 0) then
			done = true
			print("Not found! " .. result)
		end
	end
	
	return result,found
end

function valid(x,y,w,h)
	return (x >= 0) and (y >= 0) and (x < w) and (y < h)
end

function check(mapid,x,y,ox,oy,dir,result,costhere,list_,cmap_,ex,ey)
	local map = maps[mapid]
	local w,h = map.w,map.h
	
	local cx,cy = x+ox,y+oy
	
	local list = list_
	local cmap = cmap_
	
	if valid(cx,cy,w,h) then
		local tid = cx + cy * w
		local tiles = map.tiles
		
		local cost = math.max(tiles[tid] or 0, 1)
		
		if (cmap[tid] == nil) and (cost < 255) then
			cmap[tid] = {}
		
			local ctile = cmap[tid]
			
			ctile.cost = costhere + cost
			ctile.result = result .. tostring(dir)
			
			if (costhere + cost < 255) then
				local dist = math.abs(ey - cy) + math.abs(ex - cx)
				list = checkadd(dist * 0.5 + cost,cx,cy,list)
			end
		end
	end
	
	return list,cmap
end

function checkadd(c,x,y,l_)
	local l = l_
	local count = #l
	
	local u = 0
	
	if (#l > 0) then
		for i=1,count do
			u = u + 1
			local v = l[i]
			local c_ = v[1]
			
			if (c < c_) then
				table.insert(l, i, {c, x, y})
				break
			elseif (c >= c_) and (i == count) then
				table.insert(l, {c, x, y})
				break
			end
		end
	else
		table.insert(l, {c, x, y})
	end

	return l
end

function findpath_unitid(unitid,ex,ey,limit)
	local unit = mmf.newObject(unitid)

	unit.step = 0
	unit.path = path(unit.map,unit.xtile,unit.ytile,ex,ey,limit)
	
	if (string.len(unit.path) > 0) then
		unit.step = 1
		return true
	end
	
	return false
end

function findpath_unititem(unit,item,limit)
	local found = false
	
	unit.step = 0
	unit.path,found = path(unit.map,unit.xtile,unit.ytile,item.xtile,item.ytile,limit)
	
	if (string.len(unit.path) > 0) then
		unit.step = 1
		return found
	end
	
	return found
end