A little change was filed yesterday, about the cost of the terrain.
Https://git.oschina.net/dubenju/encv
■ in the Astarconstants.java is appended to the definition of the terrain.
---A/src/java/astar/astarconstants.java
+ + B/src/java/astar/astarconstants.java
@@ -8,10 +8,15 @@ -8,10 Astar;
* @author DBJ ([email protected])
*/
public class Astarconstants {
+ public static int cost_none = 0;
/** The path score of the orthogonal movement of one lattice */
public static int cost_orthogonal = 10;
/** Diagonal Move a grid of PATH scores */
public static int cost_diagonal = 14;
+ public static int cost_grass = n;// Lawn
+ public static int cost_hill =; Hills
+ public static int cost_swamp =; Swamp
+ public static int cost_river = +; River
■ in the Terrain class Terrain , additional considerations for topography are added.
---A/src/java/astar/terrain.java
+ + B/src/java/astar/terrain.java
@@ -10,6 +10,7 @@ -10,6 class Terrain {
private int val;
private int walkable;
+ private int cost;
/**
* constructor function
@@ -17,19 +18,54 @@ -17,19 class Terrain {
Public Terrain (int val) {
This.val = val;
if (This.val = = 0) {
+//0:unwalkable
this.walkable = astarconstants.note_unwalkable;
+ This.cost = Astarconstants.cost_none;
}
if (This.val = = 1) {
+//1:walkbale,ground
this.walkable = astarconstants.note_walkable;
+ This.cost = Astarconstants.cost_none;
}
if (This.val = = 2) {
+//2: wall
this.walkable = astarconstants.note_unwalkable;
+ This.cost = Astarconstants.cost_none;
}
if (This.val = = 3) {
+//3: side wall
this.walkable = astarconstants.note_unwalkable;
+ This.cost = Astarconstants.cost_none;
}
if (This.val = = 4) {
+//4:target: target
this.walkable = astarconstants.note_walkable;
+ This.cost = Astarconstants.cost_none;
+ }
+ if (This.val = = 5) {
+//5:grass : Lawn
+ this.walkable = astarconstants.note_walkable;
+ This.cost = Astarconstants.cost_grass;
+ }
+ if (This.val = = 6) {
+//6:hill : Hilly
+ this.walkable = astarconstants.note_walkable;
+ This.cost = Astarconstants.cost_hill;
+ }
+ if (This.val = = 7) {
+//7:swamp: Swamp
+ this.walkable = astarconstants.note_walkable;
+ This.cost = Astarconstants.cost_swamp;
+ }
+ if (This.val = = 8) {
+//8:river : River
+ this.walkable = astarconstants.note_walkable;
+ This.cost = astarconstants.cost_river;
+ }
+ if (This.val = = 9) {
+//9:bridge : Bridge
+ this.walkable = astarconstants.note_walkable;
+ This.cost = Astarconstants.cost_none;
}
}
@@ -60,4 +96,18 @@ -60,4 class Terrain {
public void setwalkable (int walkable) {
This.walkable = walkable;
}
+
+ /**
+ * @return Cost
+ */
+ public int Getcost () {
+ return cost;
+ }
+
+ /**
+ * @param cost
+ */
+ public void Setcost (int. cost) {
+ This.cost = Cost;
+ }
in the consider the cost of the terrain when calculating costs in AStar.
---A/src/java/astar/astar.java
+ + B/src/java/astar/astar.java
NODE.SETG (PARENT.GETG () + step);
// consider the cost of the terrain
NODE.SETG (PARENT.GETG () + step + node.getterrain (). Getcost ());
■ test program result diagram:
about the A * please refer to here.
The realization of the terrain cost of a star