<<widget "pathfind">>\n\n/%input of arguments. 1. start coordinates (x.y) 2. end coordinates (x.y) 3. map%/\n\n<<set $pf_start = $args[0]>>\n<<set $pf_end = $args[1]>>\n<<set $pf_map to jQuery.extend(true, [], $args[2])>>\n\n/%convert map to collision map. positive numbers are converted to open nodes (passable), negative numbers to not closed nodes (not passable)%/\n\n<<for $pf_i_x to 0, $pf_i_y to 0; $pf_i_y lt $pf_map.length;>>\n\n<<if $pf_map[$pf_i_y][$pf_i_x] gt 0>>\n<<set $pf_map[$pf_i_y][$pf_i_x] to "O">>\n<<else>>\n<<set $pf_map[$pf_i_y][$pf_i_x] to "N">>\n<<endif>>\n\n<<if $pf_i_x lt $pf_map[$pf_i_y].length -1>>\n<<set $pf_i_x++>>\n<<else>>\n<<set $pf_i_y++>>\n<<set $pf_i_x to 0>>\n<<endif>>\n<</for>>\n\n/%calculates optimal number of steps necessary to reach each node%/\n\n<<for $pf_smallest to [], $pf_list to [[$pf_start[0],$pf_start[1]]], $pf_path to [$pf_start]; def $pf_list[0]; $pf_smallest to []>>\n\n\t<<if $pf_map[$pf_list[0][1]+1][$pf_list[0][0]] eq "O">>\n\t<<set $pf_map[$pf_list[0][1]+1][$pf_list[0][0]] to "Li">>\n\t<<set $pf_list.push([$pf_list[0][0],$pf_list[0][1]+1])>>\t\n\t<<elseif $pf_map[$pf_list[0][1]+1][$pf_list[0][0]] gte 0>>\n\t<<set $pf_smallest.push($pf_map[$pf_list[0][1]+1][$pf_list[0][0]])>>\n\t<<endif>>\n\n\t<<if $pf_map[$pf_list[0][1]-1][$pf_list[0][0]] eq "O">>\n\t<<set $pf_map[$pf_list[0][1]-1][$pf_list[0][0]] to "Li">>\n\t<<set $pf_list.push([$pf_list[0][0],$pf_list[0][1]-1]) >>\t\n\t<<elseif $pf_map[$pf_list[0][1]-1][$pf_list[0][0]] gte 0>>\n\t<<set $pf_smallest.push($pf_map[$pf_list[0][1]-1][$pf_list[0][0]])>>\n\t<<endif>>\n\n\t<<if $pf_map[$pf_list[0][1]][$pf_list[0][0]+1] eq "O">>\n\t<<set $pf_map[$pf_list[0][1]][$pf_list[0][0]+1] to "Li">>\n\t<<set $pf_list.push([$pf_list[0][0]+1,$pf_list[0][1]]) >>\t\n\t<<elseif $pf_map[$pf_list[0][1]][$pf_list[0][0]+1] gte 0>>\n\t<<set $pf_smallest.push($pf_map[$pf_list[0][1]][$pf_list[0][0]+1])>>\n\t<<endif>>\n\n\t<<if $pf_map[$pf_list[0][1]][$pf_list[0][0]-1] eq "O">>\n\t<<set $pf_map[$pf_list[0][1]][$pf_list[0][0]-1] to "Li">>\n\t<<set $pf_list.push([$pf_list[0][0]-1,$pf_list[0][1]]) >>\t\n\t<<elseif $pf_map[$pf_list[0][1]][$pf_list[0][0]-1] gte 0>>\n\t<<set $pf_smallest.push($pf_map[$pf_list[0][1]][$pf_list[0][0]-1])>>\n\t<<endif>>\n\n\t<<if def $pf_smallest[0]>>\n\t<<set $pf_smallest.sort(function(a, b){return a-b})>>\n\t<<set $pf_map[$pf_list[0][1]][$pf_list[0][0]] to $pf_smallest[0] +1>>\n\t<<else>>\n\t<<set $pf_map[$pf_list[0][1]][$pf_list[0][0]] to 0>>\n\t<<endif>>\n\n\t<<set $pf_list.shift()>>\n\n/%saves optimal path to the final note to a path (a list of coordinates) and ends loop%/\n\n<<if $pf_map[$pf_end[1]][$pf_end[0]] gte 0>>\n\n\t<<for $pf_steps to $pf_map[$pf_end[1]][$pf_end[0]], $pf_pointer to $pf_end, $pf_select to [], $pf_path to [$pf_end]; $pf_steps gt 1; $pf_steps--, $pf_select to []>>\n\n\t\t<<if $pf_map[$pf_pointer[1]][$pf_pointer[0]-1] eq $pf_steps -1>>\n\t\t<<set $pf_select.push([$pf_pointer[0]-1,$pf_pointer[1]])>>\n\t\t<<endif>>\n\n\t\t<<if $pf_map[$pf_pointer[1]][$pf_pointer[0]+1] eq $pf_steps -1>>\n\t\t<<set $pf_select.push([$pf_pointer[0]+1,$pf_pointer[1]])>>\n\t\t<<endif>>\n\n\t\t<<if $pf_map[$pf_pointer[1]+1][$pf_pointer[0]] eq $pf_steps -1>>\n\t\t<<set $pf_select.push([$pf_pointer[0],$pf_pointer[1]+1])>>\n\t\t<<endif>>\n\n\t\t<<if $pf_map[$pf_pointer[1]-1][$pf_pointer[0]] eq $pf_steps -1>>\n\t\t<<set $pf_select.push([$pf_pointer[0],$pf_pointer[1]-1])>>\n\t\t<<endif>>\n\n\t\t<<set $pf_path.unshift($pf_select.random())>>\n\t\n\t\t<<set $pf_pointer to $pf_path[0]>>\n\n\t<</for>>\n\n\t<<break>>\n\n<<endif>>\n\n<</for>>\n\n<<unset $pf_i_x, $pf_i_y, $pf_smallest, $pf_list, $pf_steps, $pf_pointer, $pf_select, $pf_map, $pf_start, $pf_end>>\n\n<</widget>>
/* Your story will use the CSS in this passage to style the page.\nGive this passage more tags, and it will only affect passages with those tags.\nExample selectors: */\n\nbody {\n\t/* This affects the entire page */\n\t\n\t\n}\n.passage {\nfont-family: "Courier New";\n\t\n\t\n}\n.passage a {\n\n\n\t\n\t\n}\n.passage a:hover {\n\t/* This affects links while the cursor is over them */\n\t\n\t\n}\n\n.passage:not(.transition-out) {\n\ttransition: none;\n\t-webkit-transition: none;\n}
\n<<set $m to [\n[-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0],\n[-0,-0,+1,+1,+1,+1,+1,+1,-0,+1,+1,+1,+1,-0,-0,+1,-0,+1,+1,-0],\n[-0,-0,+1,-0,-0,-0,-0,-0,-0,-0,-0,+1,-0,-0,-0,-0,-0,-0,-0,-0],\n[-0,-0,+1,-0,-0,-0,-0,-0,+1,+1,+1,+1,-0,-0,-0,-0,-0,-0,-0,-0],\n[-0,+1,+1,+1,-0,+1,+1,+1,+1,+1,+1,+1,+1,+1,-0,+1,+1,+1,+1,-0],\n[-0,+1,+1,+1,+1,+1,-0,+1,+1,+1,+1,+1,+1,+1,+1,-0,+1,+1,-0,-0],\n[-0,+1,-0,-0,-0,+1,-0,-0,+0,+0,+0,+0,+1,-0,+1,+1,-0,+1,-0,-0],\n[-0,+1,+1,+1,+1,+1,+1,+1,+0,+1,+1,+1,+1,-0,-0,+1,-0,+1,+1,-0],\n[-0,-0,-0,-0,-0,-0,-0,+1,+0,+1,+0,+0,+0,-0,-0,-0,-0,-0,-0,-0],\n[-0,+1,+1,+1,-0,+1,+1,+1,+0,+1,+1,+1,+1,+1,-0,+1,+1,+1,+1,-0],\n[-0,+1,+1,+1,-0,+1,-0,+1,+1,+1,+1,+1,+1,+1,+1,-0,+1,+1,-0,-0],\n[-0,+1,-0,-0,-0,-0,-0,-0,-0,+1,+1,+1,+1,+1,+1,+1,-0,+1,-0,-0],\n[-0,+1,+1,+1,+1,+1,+1,+1,+0,+1,+1,+1,+1,-0,-0,+1,-0,+1,+1,-0],\n[-0,-0,-0,-0,-0,-0,-0,+1,-0,-0,-0,-0,-0,-0,-0,+1,-0,-0,-0,-0],\n[-0,-0,-0,-0,-0,-0,-0,+1,-0,-0,-0,-0,-0,-0,-0,+1,-0,-0,-0,-0],\n[-0,+1,+1,+1,-0,+1,+1,+1,-0,+1,+1,+1,+1,+1,+1,+1,+1,+1,+1,-0],\n[-0,+1,+1,+1,-0,+1,-0,+1,-0,+1,+1,-0,+1,+1,+1,-0,+1,+1,-0,-0],\n[-0,+1,-0,-0,+1,-0,-0,+1,-0,-0,-0,-0,+1,-0,+1,+1,+1,+1,-0,-0],\n[-0,+1,+1,+1,+1,+1,+1,+1,+1,+1,+1,+1,+1,-0,-0,+1,-0,+1,+1,-0],\n[-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0]]>>\n\n<<set $ik to [11,1]>>
/% starts the pathfinding, uses coordinates of "$ik" as starting point %/\n\n<<pathfind $ik [1,12] $m>>\n\n/% used to move "$ik" around %/\n\n<<set $m[$ik[1]][$ik[0]] to +1>> \n<<set $ik to $pf_path[0]>>\n<<set $m[$ik[1]][$ik[0]] to +2>> \n\n/% used to refresh the site %/\n\n[[Refresh|Start]]\n<br>\n\n\n/% prints the map %/\n\n<<for $i_x to 0, $i_y to 0; $i_y lt $m.length;>>\n\n<<if $m[$i_y][$i_x] eq +1>><span style="color:#000000">OO</span>\n<<elseif $m[$i_y][$i_x] eq 0>>XX\n<<elseif $m[$i_y][$i_x] eq +2>><span style="color:#FF0000">ik</span>\n<<endif>>\n\n<<if $i_x lt $m[$i_y].length -1>>\n<<set $i_x++>><<else>><br><<set $i_y++>>\n<<set $i_x to 0>><<endif>>\n<</for>>\n\n\n
everengine pathfinding demo
everengine.de