Source

libtcod / doc / html2 / path_read.html

Full commit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>libtcod documentation | Reading path information</title>
<script type="text/javascript" src="../js/doctcod.js"></script>
<link href="../css/style.css" rel="stylesheet" type="text/css"></head>
<body><div class="header">
<p><span class="title1">libtcod</span><span class="title2">documentation</span></p>
</div>
<div class="breadcrumb"><div class="breadcrumbtext"><p>
you are here: <a onclick="link('../index2.html')">Index</a> &gt; <a onclick="link('path.html')">16. Path finding</a> &gt; <a onclick="link('path_read.html')">16.3. Reading path information</a><br>
<a class="prev" onclick="link('path_compute.html')">16.2. Computing the path</a> 
</p></div></div>
<div class="filter"><input type="checkbox" id="chk_c" name="chk_c" onchange="enable('c',this.checked)" checked='checked' ><label  for='chk_c'> C   </label><input type="checkbox" id="chk_cpp" name="chk_cpp" onchange="enable('cpp',this.checked)" checked='checked' ><label  for='chk_cpp'> C++   </label><input type="checkbox" id="chk_cs" name="chk_cs" onchange="enable('cs',this.checked)" checked='checked' ><label  for='chk_cs'> C#   </label><input type="checkbox" id="chk_py" name="chk_py" onchange="enable('py',this.checked)" checked='checked' ><label  for='chk_py'> Py   </label><input type="checkbox" id="chk_lua" name="chk_lua" onchange="enable('lua',this.checked)" checked='checked' ><label  for='chk_lua'> Lua   </label></div>
<div class="main"><div class="maintext">
<h1>16.3. Reading path information</h1>
<div id="toc"><ul>
<li><a href="#0">Getting the path origin and destination</a></li><li><a href="#1">Getting the path length</a></li><li><a href="#2">Read the path cells' coordinates	</a></li><li><a href="#3">Checking if the path is empty</a></li><li><a href="#4">Walking the path</a></li><li><a href="#5">Getting the distance from a cell to the root node</a></li></ul></div>
<p>esc Once the path has been computed, you can get information about it using of one those functions.<br />	</p>
<a name="0"></a><h3>Getting the path origin and destination</h3>
<p>You can read the current origin and destination cells with getOrigin/getDestination.<br />		Note that when you walk the path, the origin changes at each step.<br />	</p>
<div class="code"><p class="cpp"><span class="code-keyword">void</span> TCODPath<span class="code-symbol">::</span>getOrigin<span class="code-symbol">(</span><span class="code-keyword">int</span><span class="code-symbol"> *</span>x<span class="code-symbol">,</span><span class="code-keyword">int</span><span class="code-symbol"> *</span>y<span class="code-symbol">)</span><span class="code-keyword"> const</span><span class="code-keyword"><br />void</span> TCODPath<span class="code-symbol">::</span>getDestination<span class="code-symbol">(</span><span class="code-keyword">int</span><span class="code-symbol"> *</span>x<span class="code-symbol">,</span><span class="code-keyword">int</span><span class="code-symbol"> *</span>y<span class="code-symbol">)</span><span class="code-keyword"> const</span></p>
<p class="c"><span class="code-keyword">void</span> TCOD_path_get_origin<span class="code-symbol">(</span><span class="code-tcod">TCOD_path_t</span> path<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>x<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>y<span class="code-symbol">)</span><span class="code-keyword"><br />void</span> TCOD_path_get_destination<span class="code-symbol">(</span><span class="code-tcod">TCOD_path_t</span> path<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>x<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>y<span class="code-symbol">)</span></p>
<p class="py">path_get_origin<span class="code-symbol">(</span>path<span class="code-symbol">)</span><span class="code-comment"> # returns x,y<br /></span>path_get_destination<span class="code-symbol">(</span>path<span class="code-symbol">)</span></p>
<p class="cs"><span class="code-keyword">void</span> TCODPath<span class="code-symbol">:</span><span class="code-symbol">:</span>getOrigin<span class="code-symbol">(</span><span class="code-keyword">out</span><span class="code-keyword"> int</span> x<span class="code-symbol">,</span><span class="code-keyword"> out</span><span class="code-keyword"> int</span> y<span class="code-symbol">)</span><span class="code-keyword"><br />void</span> TCODPath<span class="code-symbol">:</span><span class="code-symbol">:</span>getDestination<span class="code-symbol">(</span><span class="code-keyword">out</span><span class="code-keyword"> int</span> x<span class="code-symbol">,</span><span class="code-keyword"> out</span><span class="code-keyword"> int</span> y<span class="code-symbol">)</span></p>
</div>
<table class="param"><tbody><tr><th>Parameter</th><th>Description</th></tr><tr class="hilite"><td>path</td><td>In the C version, the path handler returned by a creation function.<br />	</td></tr>
<tr><td>x,y</td><td>The function returns the cell coordinates in these variables<br />	</td></tr>
</tbody></table><a name="1"></a><h3>Getting the path length</h3>
<p>You can get the number of steps needed to reach destination :<br />	</p>
<div class="code"><p class="cpp"><span class="code-keyword">int</span> TCODPath<span class="code-symbol">::</span>size<span class="code-symbol">(</span><span class="code-symbol">)</span><span class="code-keyword"> const</span><span class="code-keyword"><br />int</span> TCODDijkstra<span class="code-symbol">::</span>size<span class="code-symbol">(</span><span class="code-symbol">)</span><span class="code-keyword"> const</span></p>
<p class="c"><span class="code-keyword">int</span> TCOD_path_size<span class="code-symbol">(</span><span class="code-tcod">TCOD_path_t</span> path<span class="code-symbol">)</span><span class="code-keyword"><br />int</span> TCOD_dijkstra_size<span class="code-symbol">(</span><span class="code-tcod">TCOD_dijkstra_t</span> dijkstra<span class="code-symbol">)</span></p>
<p class="py">path_size<span class="code-symbol">(</span>path<span class="code-symbol">)</span><br />dijkstra_size<span class="code-symbol">(</span>dijkstra<span class="code-symbol">)</span></p>
<p class="cs"><span class="code-keyword">int</span> TCODPath<span class="code-symbol">:</span><span class="code-symbol">:</span>size<span class="code-symbol">(</span><span class="code-symbol">)</span><span class="code-keyword"><br />int</span> TCODDijkstra<span class="code-symbol">:</span><span class="code-symbol">:</span>size<span class="code-symbol">(</span><span class="code-symbol">)</span></p>
</div>
<table class="param"><tbody><tr><th>Parameter</th><th>Description</th></tr><tr class="hilite"><td>path,</td><td>dijkstra	In the C version, the path handler returned by a creation function.<br />	</td></tr>
</tbody></table><a name="2"></a><h3>Read the path cells' coordinates	</h3>
<p>You can get the coordinates of each point along the path :<br />	</p>
<div class="code"><p class="cpp"><span class="code-keyword">void</span> TCODPath<span class="code-symbol">::</span>get<span class="code-symbol">(</span><span class="code-keyword">int</span> index<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>x<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>y<span class="code-symbol">)</span><span class="code-keyword"> const</span><span class="code-keyword"><br />void</span> TCODDijkstra<span class="code-symbol">::</span>get<span class="code-symbol">(</span><span class="code-keyword">int</span> index<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>x<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>y<span class="code-symbol">)</span><span class="code-keyword"> const</span></p>
<p class="c"><span class="code-keyword">void</span> TCOD_path_get<span class="code-symbol">(</span><span class="code-tcod">TCOD_path_t</span> path<span class="code-symbol">,</span><span class="code-keyword"> int</span> index<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>x<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>y<span class="code-symbol">)</span><span class="code-keyword"><br />void</span> TCOD_dijkstra_get<span class="code-symbol">(</span><span class="code-tcod">TCOD_dijkstra_t</span> dijkstra<span class="code-symbol">,</span><span class="code-keyword"> int</span> index<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>x<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>y<span class="code-symbol">)</span></p>
<p class="py">path_get<span class="code-symbol">(</span>path<span class="code-symbol">,</span> index<span class="code-symbol">)</span><span class="code-comment"> # returns x,y<br /></span>dijkstra_get<span class="code-symbol">(</span>dijkstra<span class="code-symbol">,</span> index<span class="code-symbol">)</span></p>
<p class="cs"><span class="code-keyword">int</span> TCODPath<span class="code-symbol">:</span><span class="code-symbol">:</span>size<span class="code-symbol">(</span><span class="code-symbol">)</span><span class="code-keyword"><br />int</span> TCODDijkstra<span class="code-symbol">:</span><span class="code-symbol">:</span>size<span class="code-symbol">(</span><span class="code-symbol">)</span></p>
</div>
<table class="param"><tbody><tr><th>Parameter</th><th>Description</th></tr><tr class="hilite"><td>path,</td><td>dijkstra	In the C version, the path handler returned by a creation function.<br />	</td></tr>
<tr><td>index</td><td>Step number.<br />		0 <= index < path size<br />	</td></tr>
<tr class="hilite"><td>x,y</td><td>Address of the variables receiving the coordinates of the point.<br />	</td></tr>
</tbody></table><h6>Example:</h6><div class="code">
<p class="cpp"><span class="code-keyword">for</span><span class="code-symbol"> (</span><span class="code-keyword">int</span> i<span class="code-symbol">=</span><span class="code-value">0</span><span class="code-symbol">;</span> i<span class="code-symbol"> <</span> path<span class="code-symbol">-></span>size<span class="code-symbol">(</span><span class="code-symbol">)</span><span class="code-symbol">;</span> i<span class="code-symbol">++</span><span class="code-symbol"> )</span><span class="code-symbol"> {</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;int</span> x<span class="code-symbol">,</span>y<span class="code-symbol">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;path<span class="code-symbol">-></span>get<span class="code-symbol">(</span>i<span class="code-symbol">,</span><span class="code-symbol">&</span>x<span class="code-symbol">,</span><span class="code-symbol">&</span>y<span class="code-symbol">)</span><span class="code-symbol">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;printf<span class="code-symbol"> (</span><span class="code-string">"Astar coord : %d %d\n"</span><span class="code-symbol">,</span> x<span class="code-symbol">,</span>y<span class="code-symbol"> )</span><span class="code-symbol">;</span><span class="code-symbol"><br />}</span><span class="code-keyword"><br />for</span><span class="code-symbol"> (</span><span class="code-keyword">int</span> i<span class="code-symbol">=</span><span class="code-value">0</span><span class="code-symbol">;</span> i<span class="code-symbol"> <</span> dijkstra<span class="code-symbol">-></span>size<span class="code-symbol">(</span><span class="code-symbol">)</span><span class="code-symbol">;</span> i<span class="code-symbol">++</span><span class="code-symbol"> )</span><span class="code-symbol"> {</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;int</span> x<span class="code-symbol">,</span>y<span class="code-symbol">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;dijkstra<span class="code-symbol">-></span>get<span class="code-symbol">(</span>i<span class="code-symbol">,</span><span class="code-symbol">&</span>x<span class="code-symbol">,</span><span class="code-symbol">&</span>y<span class="code-symbol">)</span><span class="code-symbol">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;printf<span class="code-symbol"> (</span><span class="code-string">"Dijkstra coord : %d %d\n"</span><span class="code-symbol">,</span> x<span class="code-symbol">,</span>y<span class="code-symbol"> )</span><span class="code-symbol">;</span><span class="code-symbol"><br />}</span></p>
<p class="c"><span class="code-keyword">int</span> i<span class="code-symbol">;</span><span class="code-keyword"><br />for</span><span class="code-symbol"> (</span>i<span class="code-symbol">=</span><span class="code-value">0</span><span class="code-symbol">;</span> i<span class="code-symbol"> <</span> TCOD_path_size<span class="code-symbol">(</span>path<span class="code-symbol">)</span><span class="code-symbol">;</span> i<span class="code-symbol">++</span><span class="code-symbol"> )</span><span class="code-symbol"> {</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;int</span> x<span class="code-symbol">,</span>y<span class="code-symbol">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;TCOD_path_get<span class="code-symbol">(</span>path<span class="code-symbol">,</span>i<span class="code-symbol">,</span><span class="code-symbol">&</span>x<span class="code-symbol">,</span><span class="code-symbol">&</span>y<span class="code-symbol">)</span><span class="code-symbol">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;printf<span class="code-symbol"> (</span><span class="code-string">"Astar coord : %d %d\n"</span><span class="code-symbol">,</span> x<span class="code-symbol">,</span>y<span class="code-symbol"> )</span><span class="code-symbol">;</span><span class="code-symbol"><br />}</span><span class="code-keyword"><br />for</span><span class="code-symbol"> (</span>i<span class="code-symbol">=</span><span class="code-value">0</span><span class="code-symbol">;</span> i<span class="code-symbol"> <</span> TCOD_dijkstra_size<span class="code-symbol">(</span>dijkstra<span class="code-symbol">)</span><span class="code-symbol">;</span> i<span class="code-symbol">++</span><span class="code-symbol"> )</span><span class="code-symbol"> {</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;int</span> x<span class="code-symbol">,</span>y<span class="code-symbol">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;TCOD_dijkstra_get<span class="code-symbol">(</span>dijkstra<span class="code-symbol">,</span>i<span class="code-symbol">,</span><span class="code-symbol">&</span>x<span class="code-symbol">,</span><span class="code-symbol">&</span>y<span class="code-symbol">)</span><span class="code-symbol">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;printf<span class="code-symbol"> (</span><span class="code-string">"Dijsktra coord : %d %d\n"</span><span class="code-symbol">,</span> x<span class="code-symbol">,</span>y<span class="code-symbol"> )</span><span class="code-symbol">;</span><span class="code-symbol"><br />}</span></p>
<p class="py"><span class="code-keyword">for</span> i<span class="code-keyword"> in</span> range<span class="code-symbol"> (</span>libtcod<span class="code-symbol">.</span>path_size<span class="code-symbol">(</span>path<span class="code-symbol">)</span><span class="code-symbol">)</span><span class="code-symbol"> :</span><br />&nbsp;&nbsp;&nbsp;&nbsp;x<span class="code-symbol">,</span>y<span class="code-symbol">=</span>libtcod<span class="code-symbol">.</span>path_get<span class="code-symbol">(</span>path<span class="code-symbol">,</span>i<span class="code-symbol">)</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;print</span><span class="code-string"> 'Astar coord : '</span><span class="code-symbol">,</span>x<span class="code-symbol">,</span>y<span class="code-keyword"><br />for</span> i<span class="code-keyword"> in</span> range<span class="code-symbol"> (</span>libtcod<span class="code-symbol">.</span>dijkstra_size<span class="code-symbol">(</span>dijkstra<span class="code-symbol">)</span><span class="code-symbol">)</span><span class="code-symbol"> :</span><br />&nbsp;&nbsp;&nbsp;&nbsp;x<span class="code-symbol">,</span>y<span class="code-symbol">=</span>libtcod<span class="code-symbol">.</span>dijkstra_get<span class="code-symbol">(</span>dijkstra<span class="code-symbol">,</span>i<span class="code-symbol">)</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;print</span><span class="code-string"> 'Dijkstra coord : '</span><span class="code-symbol">,</span>x<span class="code-symbol">,</span>y</p>
</div><hr>
<a name="3"></a><h3>Checking if the path is empty</h3>
<p>If you want a creature to follow the path, a more convenient way is to walk the path :<br />		You know when you reached destination when the path is empty :<br />	</p>
<div class="code"><p class="cpp"><span class="code-keyword">bool</span> TCODPath<span class="code-symbol">::</span>isEmpty<span class="code-symbol">(</span><span class="code-symbol">)</span><span class="code-keyword"> const</span><span class="code-keyword"><br />bool</span> TCODDijkstra<span class="code-symbol">::</span>isEmpty<span class="code-symbol">(</span><span class="code-symbol">)</span><span class="code-keyword"> const</span></p>
<p class="c"><span class="code-keyword">bool</span> TCOD_path_is_empty<span class="code-symbol">(</span><span class="code-tcod">TCOD_path_t</span> path<span class="code-symbol">)</span><span class="code-keyword"><br />bool</span> TCOD_dijkstra_is_empty<span class="code-symbol">(</span><span class="code-tcod">TCOD_dijkstra_t</span> dijkstra<span class="code-symbol">)</span></p>
<p class="py">path_is_empty<span class="code-symbol">(</span>path<span class="code-symbol">)</span><br />dijkstra_is_empty<span class="code-symbol">(</span>dijkstra<span class="code-symbol">)</span></p>
<p class="cs"><span class="code-keyword">bool</span> TCODPath<span class="code-symbol">:</span><span class="code-symbol">:</span>isEmpty<span class="code-symbol">(</span><span class="code-symbol">)</span><span class="code-keyword"><br />bool</span> TCODDijkstra<span class="code-symbol">:</span><span class="code-symbol">:</span>isEmpty<span class="code-symbol">(</span><span class="code-symbol">)</span></p>
</div>
<table class="param"><tbody><tr><th>Parameter</th><th>Description</th></tr><tr class="hilite"><td>path,</td><td>dijkstra	In the C version, the path handler returned by a creation function.	<br />	</td></tr>
</tbody></table><a name="4"></a><h3>Walking the path</h3>
<p>You can walk the path and go to the next step with :<br />		Note that walking the path consume one step (and decrease the path size by one). The function returns false if recalculateWhenNeeded is false and the next cell on the path is no longer walkable, or if recalculateWhenNeeded is true, the next cell on the path is no longer walkable and no other path has been found. Also note that recalculateWhenNeeded only applies to A*.<br />	</p>
<div class="code"><p class="cpp"><span class="code-keyword">bool</span> TCODPath<span class="code-symbol">::</span>walk<span class="code-symbol">(</span><span class="code-keyword">int</span><span class="code-symbol"> *</span>x<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>y<span class="code-symbol">,</span><span class="code-keyword"> bool</span> recalculateWhenNeeded<span class="code-symbol">)</span><span class="code-keyword"><br />bool</span> TCODDijkstra<span class="code-symbol">::</span>walk<span class="code-symbol">(</span><span class="code-keyword">int</span><span class="code-symbol"> *</span>x<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>y<span class="code-symbol">)</span></p>
<p class="c"><span class="code-keyword">bool</span> TCOD_path_walk<span class="code-symbol">(</span><span class="code-tcod">TCOD_path_t</span> path<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>x<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>y<span class="code-symbol">,</span><span class="code-keyword"> bool</span> recalculate_when_needed<span class="code-symbol">)</span><span class="code-keyword"><br />bool</span> TCOD_dijkstra_walk<span class="code-symbol">(</span><span class="code-tcod">TCOD_dijkstra_t</span> dijkstra<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>x<span class="code-symbol">,</span><span class="code-keyword"> int</span><span class="code-symbol"> *</span>y<span class="code-symbol">)</span></p>
<p class="py">path_walk<span class="code-symbol">(</span><span class="code-tcod">TCOD_path_t</span> path<span class="code-symbol">,</span> recalculate_when_needed<span class="code-symbol">)</span><span class="code-comment"> # returns x,y or None,None if no path<br /></span>dijkstra_walk<span class="code-symbol">(</span><span class="code-tcod">TCOD_dijkstra_t</span> dijkstra<span class="code-symbol">)</span></p>
<p class="cs"><span class="code-keyword">bool</span> TCODPath<span class="code-symbol">:</span><span class="code-symbol">:</span>walk<span class="code-symbol">(</span><span class="code-keyword">ref</span><span class="code-keyword"> int</span> x<span class="code-symbol">,</span><span class="code-keyword"> ref</span><span class="code-keyword"> int</span> y<span class="code-symbol">,</span><span class="code-keyword"> bool</span> recalculateWhenNeeded<span class="code-symbol">)</span><span class="code-keyword"><br />bool</span> TCODDijkstra<span class="code-symbol">:</span><span class="code-symbol">:</span>walk<span class="code-symbol">(</span><span class="code-keyword">ref</span><span class="code-keyword"> int</span> x<span class="code-symbol">,</span><span class="code-keyword"> ref</span><span class="code-keyword"> int</span> y<span class="code-symbol">)</span></p>
</div>
<table class="param"><tbody><tr><th>Parameter</th><th>Description</th></tr><tr class="hilite"><td>path,</td><td>dijkstra	In the C version, the path handler returned by a creation function.<br />	</td></tr>
<tr><td>x,y</td><td>Address of the variables receiving the coordinates of the next point.<br />	</td></tr>
<tr class="hilite"><td>recalculateWhenNeeded</td><td>If the next point is no longer walkable (another creature may be in the way), recalculate a new path and walk it.<br />	</td></tr>
</tbody></table><h6>Example:</h6><div class="code">
<p class="cpp"><span class="code-keyword">while</span><span class="code-symbol"> (</span><span class="code-symbol">!</span> path<span class="code-symbol">-></span>isEmpty<span class="code-symbol">(</span><span class="code-symbol">)</span><span class="code-symbol">)</span><span class="code-symbol"> {</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;int</span> x<span class="code-symbol">,</span>y<span class="code-symbol">;</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;if</span><span class="code-symbol"> (</span>path<span class="code-symbol">-></span>walk<span class="code-symbol">(</span><span class="code-symbol">&</span>x<span class="code-symbol">,</span><span class="code-symbol">&</span>y<span class="code-symbol">,</span><span class="code-keyword">true</span><span class="code-symbol">)</span><span class="code-symbol">)</span><span class="code-symbol"> {</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf<span class="code-symbol"> (</span><span class="code-string">"Astar coord: %d %d\n"</span><span class="code-symbol">,</span>x<span class="code-symbol">,</span>y<span class="code-symbol"> )</span><span class="code-symbol">;</span><span class="code-symbol"><br />&nbsp;&nbsp;&nbsp;&nbsp;}</span><span class="code-keyword"> else</span><span class="code-symbol"> {</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf<span class="code-symbol"> (</span><span class="code-string">"I'm stuck!\n"</span><span class="code-symbol"> )</span><span class="code-symbol">;</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break</span><span class="code-symbol">;</span><span class="code-symbol"><br />&nbsp;&nbsp;&nbsp;&nbsp;}</span><span class="code-symbol"><br />}</span><span class="code-keyword"><br />while</span><span class="code-symbol"> (</span><span class="code-symbol">!</span> dijkstra<span class="code-symbol">-></span>isEmpty<span class="code-symbol">(</span><span class="code-symbol">)</span><span class="code-symbol">)</span><span class="code-symbol"> {</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;int</span> x<span class="code-symbol">,</span>y<span class="code-symbol">;</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;if</span><span class="code-symbol"> (</span>dijkstra<span class="code-symbol">-></span>walk<span class="code-symbol">(</span><span class="code-symbol">&</span>x<span class="code-symbol">,</span><span class="code-symbol">&</span>y<span class="code-symbol">)</span><span class="code-symbol">)</span><span class="code-symbol"> {</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf<span class="code-symbol"> (</span><span class="code-string">"Dijkstra coord: %d %d\n"</span><span class="code-symbol">,</span>x<span class="code-symbol">,</span>y<span class="code-symbol"> )</span><span class="code-symbol">;</span><span class="code-symbol"><br />&nbsp;&nbsp;&nbsp;&nbsp;}</span><span class="code-keyword"> else</span><span class="code-symbol"> {</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf<span class="code-symbol"> (</span><span class="code-string">"I'm stuck!\n"</span><span class="code-symbol"> )</span><span class="code-symbol">;</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break</span><span class="code-symbol">;</span><span class="code-symbol"><br />&nbsp;&nbsp;&nbsp;&nbsp;}</span><span class="code-symbol"><br />}</span></p>
<p class="c"><span class="code-keyword">while</span><span class="code-symbol"> (</span><span class="code-symbol">!</span> TCOD_path_is_empty<span class="code-symbol">(</span>path<span class="code-symbol">)</span><span class="code-symbol">)</span><span class="code-symbol"> {</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;int</span> x<span class="code-symbol">,</span>y<span class="code-symbol">;</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;if</span><span class="code-symbol"> (</span>TCOD_path_walk<span class="code-symbol">(</span>path<span class="code-symbol">,</span><span class="code-symbol">&</span>x<span class="code-symbol">,</span><span class="code-symbol">&</span>y<span class="code-symbol">,</span>true<span class="code-symbol">)</span><span class="code-symbol">)</span><span class="code-symbol"> {</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf<span class="code-symbol"> (</span><span class="code-string">"Astar coord: %d %d\n"</span><span class="code-symbol">,</span>x<span class="code-symbol">,</span>y<span class="code-symbol"> )</span><span class="code-symbol">;</span><span class="code-symbol"><br />&nbsp;&nbsp;&nbsp;&nbsp;}</span><span class="code-keyword"> else</span><span class="code-symbol"> {</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf<span class="code-symbol"> (</span><span class="code-string">"I'm stuck!\n"</span><span class="code-symbol"> )</span><span class="code-symbol">;</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break</span><span class="code-symbol">;</span><span class="code-symbol"><br />&nbsp;&nbsp;&nbsp;&nbsp;}</span><span class="code-symbol"><br />}</span><span class="code-keyword"><br />while</span><span class="code-symbol"> (</span><span class="code-symbol">!</span> TCOD_dijkstra_is_empty<span class="code-symbol">(</span>dijkstra<span class="code-symbol">)</span><span class="code-symbol">)</span><span class="code-symbol"> {</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;int</span> x<span class="code-symbol">,</span>y<span class="code-symbol">;</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;if</span><span class="code-symbol"> (</span>TCOD_dijkstra_walk<span class="code-symbol">(</span>dijkstra<span class="code-symbol">,</span><span class="code-symbol">&</span>x<span class="code-symbol">,</span><span class="code-symbol">&</span>y<span class="code-symbol">)</span><span class="code-symbol">)</span><span class="code-symbol"> {</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf<span class="code-symbol"> (</span><span class="code-string">"Dijkstra coord: %d %d\n"</span><span class="code-symbol">,</span>x<span class="code-symbol">,</span>y<span class="code-symbol"> )</span><span class="code-symbol">;</span><span class="code-symbol"><br />&nbsp;&nbsp;&nbsp;&nbsp;}</span><span class="code-keyword"> else</span><span class="code-symbol"> {</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf<span class="code-symbol"> (</span><span class="code-string">"I'm stuck!\n"</span><span class="code-symbol"> )</span><span class="code-symbol">;</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break</span><span class="code-symbol">;</span><span class="code-symbol"><br />&nbsp;&nbsp;&nbsp;&nbsp;}</span><span class="code-symbol"><br />}</span></p>
<p class="py"><span class="code-keyword">while</span><span class="code-keyword"> not</span> libtcod<span class="code-symbol">.</span>path_is_empty<span class="code-symbol">(</span>path<span class="code-symbol">)</span><span class="code-symbol">)</span><span class="code-symbol"> :</span><br />&nbsp;&nbsp;&nbsp;&nbsp;x<span class="code-symbol">,</span>y<span class="code-symbol">=</span>libtcod<span class="code-symbol">.</span>path_walk<span class="code-symbol">(</span>path<span class="code-symbol">,</span><span class="code-keyword">True</span><span class="code-symbol">)</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;if</span><span class="code-keyword"> not</span> x<span class="code-keyword"> is</span> None<span class="code-symbol"> :</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print</span><span class="code-string"> 'Astar coord: '</span><span class="code-symbol">,</span>x<span class="code-symbol">,</span>y<span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;else</span><span class="code-symbol"> :</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print</span><span class="code-string"> "I'm stuck!"</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break</span><span class="code-keyword"><br />while</span><span class="code-keyword"> not</span> libtcod<span class="code-symbol">.</span>dijkstra_is_empty<span class="code-symbol">(</span>dijkstra<span class="code-symbol">)</span><span class="code-symbol">)</span><span class="code-symbol"> :</span><br />&nbsp;&nbsp;&nbsp;&nbsp;x<span class="code-symbol">,</span>y<span class="code-symbol">=</span>libtcod<span class="code-symbol">.</span>dijkstra_walk<span class="code-symbol">(</span>dijkstra<span class="code-symbol">,</span><span class="code-keyword">True</span><span class="code-symbol">)</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;if</span><span class="code-keyword"> not</span> x<span class="code-keyword"> is</span> None<span class="code-symbol"> :</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print</span><span class="code-string"> 'Dijkstra coord: '</span><span class="code-symbol">,</span>x<span class="code-symbol">,</span>y<span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;else</span><span class="code-symbol"> :</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print</span><span class="code-string"> "I'm stuck!"</span><span class="code-keyword"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break</span></p>
</div><hr>
<a name="5"></a><h3>Getting the distance from a cell to the root node</h3>
<p>You can get the distance of any set of coordinates from the root node:<br />			Note that if the coordinates x,y are outside of the map or are a non-walkable position, the function will return -1.0f. This functionality is only available for Dijkstra's algorithm.        <br />		</p>
<div class="code"><p class="cpp"><span class="code-keyword">float</span> TCODDijkstra<span class="code-symbol">::</span>getDistance<span class="code-symbol">(</span><span class="code-keyword">int</span> x<span class="code-symbol">,</span><span class="code-keyword"> int</span> y<span class="code-symbol">)</span></p>
<p class="c"><span class="code-keyword">float</span> TCOD_dijkstra_get_distance<span class="code-symbol">(</span><span class="code-tcod">TCOD_dijkstra_t</span> dijkstra<span class="code-symbol">,</span><span class="code-keyword"> int</span> x<span class="code-symbol">,</span><span class="code-keyword"> int</span> y<span class="code-symbol">)</span></p>
<p class="py">dijkstra_get_distance<span class="code-symbol">(</span>dijkstra<span class="code-symbol">,</span> x<span class="code-symbol">,</span> y<span class="code-symbol">)</span></p>
<p class="cs"><span class="code-keyword">float</span> TCODDijkstra<span class="code-symbol">:</span><span class="code-symbol">:</span>getDistance<span class="code-symbol">(</span><span class="code-keyword">int</span> x<span class="code-symbol">,</span><span class="code-keyword"> int</span> y<span class="code-symbol">)</span></p>
</div>
<table class="param"><tbody><tr><th>Parameter</th><th>Description</th></tr><tr class="hilite"><td>dijkstra</td><td>In the C version, the path handler returned by a creation function.<br />		</td></tr>
<tr><td>x,y</td><td>The coordinates whose distance from the root node are to be checked<br />        </td></tr>
</tbody></table>
</div></div>
<div class="footer"><div class="footertext">
	<p>libtcod 1.5.1, &copy; 2008, 2009, 2010 Jice & Mingos<br>
	This file has been generated by doctcod.</p>
	<p><table width='100%'><tr><td><a href="http://doryen.eptalys.net/libtcod">libtcod website</a></td>
	<td><a href="http://doryen.eptalys.net/forum/index.php?board=12.0">libtcod on Roguecentral forums</a></td>
  <td><a href="http://www.youtube.com/watch?v=PY9lJnVQbXs&feature=PlayList&p=553E3D00A1460331&playnext_from=PL&index=0&playnext=1">libtcod HOWTO video tutorials</a></td></tr></table></p>
</div></div>
</body>
<script>
initFilter();
</script>
</html>