File size: 3,581 Bytes
87b3b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<h2>Pathfinding algorithms</h2>

<p>The <code>ROT.Path</code> namespace provides utils for pathfinding: retrieving a set of coordinates represeting shortest route between two points. All pathfinding algorithms share the same usage paradigm:</p>
<ol>
	<li>Create an <em>input callback</em>; function that returns passability info (true/false) for a given coordinates</li>
	<li>Instantialize the pathfinder; pass the values of <code>targetX</code>, <code>targetY</code> and <code>inputCallback</code> to the constructor
		<ul>
			<li>Fourth constructor option is a configuration object; so far, only one key is supported: <code>topology</code> (values 4/6/8)</li>
		</ul>
	</li>
	<li>Call the <code>compute</code> method with three arguments: <code>sourceX</code>, <code>sourceY</code> and <code>outputCallback</code></li>
</ol>

<p>The pathfinder will periodically call your <code>outputCallback</code>; once for every cell in the path from <code>[sourceX, sourceY]</code> to <code>[targetX, targetY]</code>. 
If no such path exists, <code>outputCallback</code> will never be called.</p>

<p><strong>NOTE:</strong> you can call the <code>compute</code> method multiple times 
(which is especially useful, performance-wise, when using <code>ROT.Path.Dijkstra</code>), 
but you must make sure that the passability information of the map never changes once 
the pathfinder is created.</p>

<h3>Dijkstra's algorithm</h3>

<p>Simplified version of <a href="http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm">Dijkstra's algorithm</a>: all edges have a length of 1.</p>

<div class="example">
var w = 150, h = 80;
ROT.RNG.setSeed(12345);
var display = new ROT.Display({width:w, height:h, fontSize:6});
SHOW(display.getContainer());

/* generate map and store its data */
var data = {};
var map = new ROT.Map.Uniform(w, h);
map.create(function(x, y, value) {
	data[x+","+y] = value;
	display.DEBUG(x, y, value);
});

/* input callback informs about map structure */
var passableCallback = function(x, y) {
	return (data[x+","+y] === 0);
}

/* prepare path to given coords */
var dijkstra = new ROT.Path.Dijkstra(98, 38, passableCallback);

/* compute from given coords #1 */
dijkstra.compute(8, 45, function(x, y) {
	display.draw(x, y, "", "", "#800");
});

/* compute from given coords #2 */
dijkstra.compute(130, 8, function(x, y) {
	display.draw(x, y, "", "", "#800");
});

/* highlight */
display.draw(8,  45, "", "", "#3f3");
display.draw(130, 8, "", "", "#3f3");
display.draw(98, 38, "", "", "#f33");
</div>

<h3>A* algorithm</h3>

<p>Simplified version of <a href="http://en.wikipedia.org/wiki/A*">A* algorithm</a>: all edges have a length of 1.</p>

<div class="example">
var w = 150, h = 80;
ROT.RNG.setSeed(12345);
var display = new ROT.Display({width:w, height:h, fontSize:6});
SHOW(display.getContainer());

/* generate map and store its data */
var data = {};
var map = new ROT.Map.Uniform(w, h);
map.create(function(x, y, value) {
	data[x+","+y] = value;
	display.DEBUG(x, y, value);
});

/* input callback informs about map structure */
var passableCallback = function(x, y) {
	return (data[x+","+y] === 0);
}

/* prepare path to given coords */
var astar = new ROT.Path.AStar(98, 38, passableCallback);

/* compute from given coords #1 */
astar.compute(8, 45, function(x, y) {
	display.draw(x, y, "", "", "#800");
});

/* compute from given coords #2 */
astar.compute(130, 8, function(x, y) {
	display.draw(x, y, "", "", "#800");
});

/* highlight */
display.draw(8,  45, "", "", "#3f3");
display.draw(130, 8, "", "", "#3f3");
display.draw(98, 38, "", "", "#f33");
</div>