Created
November 21, 2016 02:33
-
-
Save zacharycarter/81b21edd5eaa048ba9b49ed985c90d5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.carterza.ai.path; | |
import com.badlogic.gdx.ai.pfa.Connection; | |
import com.badlogic.gdx.ai.pfa.DefaultConnection; | |
import com.badlogic.gdx.ai.pfa.indexed.IndexedGraph; | |
import com.badlogic.gdx.utils.Array; | |
import com.badlogic.gdx.utils.ObjectIntMap; | |
import squidpony.squidgrid.Direction; | |
import squidpony.squidmath.Coord; | |
public class GridGraph implements IndexedGraph<Coord> | |
{ | |
private int dimension; | |
public ObjectIntMap<Coord> points; | |
public static char[][] map; | |
public GridGraph(char[][] map, Coord[] pts, int dimension) | |
{ | |
this.map = map; | |
this.dimension = dimension; | |
points = new ObjectIntMap<>(dimension * dimension); | |
for (int i = 0; i < pts.length; i++) { | |
points.put(pts[i], i); | |
} | |
} | |
@Override | |
public int getIndex(Coord node) { | |
return points.get(node, -1); | |
} | |
@Override | |
public int getNodeCount() { | |
return points.size; | |
} | |
@Override | |
public Array<Connection<Coord>> getConnections(Coord fromNode) { | |
Array<Connection<Coord>> conn = new Array<>(false, 8); | |
if(this.map[fromNode.x][fromNode.y] != '.') | |
return conn; | |
Coord t; | |
for (int i = 0; i < 8; i++) { | |
t = fromNode.translate(Direction.OUTWARDS[i]); | |
if (t.isWithin(this.dimension, this.dimension) && this.map[t.x][t.y] == '.') | |
conn.add(new DefaultConnection<>(fromNode, t)); | |
} | |
return conn; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.carterza.system.ai; | |
import com.artemis.ComponentMapper; | |
import com.artemis.Entity; | |
import com.badlogic.gdx.ai.pfa.DefaultGraphPath; | |
import com.badlogic.gdx.ai.pfa.GraphPath; | |
import com.badlogic.gdx.ai.pfa.Heuristic; | |
import com.badlogic.gdx.ai.pfa.indexed.IndexedAStarPathFinder; | |
import com.badlogic.gdx.math.Vector2; | |
import com.carterza.action.MoveToAction; | |
import com.carterza.ai.path.GridGraph; | |
import com.carterza.ai.path.Path; | |
import com.carterza.component.PositionComponent; | |
import com.carterza.event.EventBus; | |
import com.carterza.event.StartWanderingEvent; | |
import com.carterza.event.StopWanderingEvent; | |
import com.carterza.event.WanderEvent; | |
import com.carterza.generation.ship.grid.GridMap; | |
import com.carterza.system.action.ActionSystem; | |
import com.carterza.system.generation.ShipGenerationSystem; | |
import com.stewsters.util.math.Point2i; | |
import net.mostlyoriginal.api.event.common.Subscribe; | |
import net.mostlyoriginal.api.system.core.PassiveSystem; | |
import squidpony.squidgrid.mapping.DungeonUtility; | |
import squidpony.squidmath.Coord; | |
import squidpony.squidmath.GreasedRegion; | |
import squidpony.squidmath.LightRNG; | |
import squidpony.squidmath.StatefulRNG; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.Map; | |
/** | |
* Created by zachcarter on 11/20/16. | |
*/ | |
public class WanderSystem extends PassiveSystem { | |
private Map<Entity, Path> entityPathMap; | |
private EventBus eventBus; | |
private ActionSystem actionSystem; | |
private ShipGenerationSystem shipGenerationSystem; | |
private ComponentMapper<PositionComponent> positionComponentMapper; | |
GridMap gridMap; | |
GreasedRegion floors; | |
char[][] map; | |
@Override | |
protected void initialize() { | |
this.entityPathMap = new HashMap<>(); | |
this.gridMap = shipGenerationSystem.getSpacecraft().gridMap; | |
int width = gridMap.getWidthInTiles(), height = gridMap.getHeightInTiles(); | |
this.map = new char[width][height]; | |
for(int x = 0; x < width; x++) { | |
for(int y = 0; y < height; y++) { | |
switch(gridMap.getTile(x,y)) { | |
case AETHER: | |
map[x][y] = '#'; | |
break; | |
case VACUUM: | |
map[x][y] = '#'; | |
break; | |
case INTERNALS: | |
map[x][y] = '#'; | |
break; | |
case WALL: | |
map[x][y] = '#'; | |
break; | |
case REINFORCED_WALL: | |
map[x][y] = '#'; | |
break; | |
case ARMOR: | |
map[x][y] = '#'; | |
break; | |
case DOOR: | |
map[x][y] = '.'; | |
break; | |
case FLOOR: | |
map[x][y] = '.'; | |
break; | |
} | |
} | |
} | |
this.floors = new GreasedRegion(map, '.'); | |
} | |
@Subscribe | |
void handleEvent(StartWanderingEvent event) { | |
System.out.println("Starting to wander."); | |
Entity eventEntity = event.getEntity(); | |
PositionComponent positionComponent = positionComponentMapper.get(eventEntity); | |
Vector2 position = positionComponent.getPosition(); | |
int x = (int)position.x, y = (int)position.y; | |
Point2i pointToWanderTo = shipGenerationSystem.getSpacecraft().findRoomContainingPoint(new Point2i(x,y)).findRandomTileInRoom(); | |
IndexedAStarPathFinder<Coord> astar = new IndexedAStarPathFinder<Coord>(new GridGraph(this.map, floors.asCoords(), 1024)); | |
GraphPath<Coord> dgp = new DefaultGraphPath<Coord>((1024 - 2) * (1024 - 2)); | |
Heuristic<Coord> heu = new Heuristic<Coord>() { | |
@Override | |
public float estimate(Coord node, Coord endNode) { | |
return Math.abs(node.x - endNode.x) + Math.abs(node.y - endNode.y); | |
} | |
}; | |
DungeonUtility utility = new DungeonUtility(new StatefulRNG(new LightRNG(0x1337BEEFDEAL))); | |
dgp.clear(); | |
astar.searchNodePath(Coord.get(pointToWanderTo.x, pointToWanderTo.y), Coord.get(x,y), heu, dgp); | |
// entityPathMap.put(eventEntity, new Path(dijkstraMap.findPath(100, null, null, Coord.get(x,y), Coord.get(pointToWanderTo.x, pointToWanderTo.y)))); | |
} | |
@Subscribe | |
void handleEvent(WanderEvent event) { | |
System.out.println("Wandering."); | |
Entity eventEntity = event.getEntity(); | |
PositionComponent positionComponent = positionComponentMapper.get(eventEntity); | |
Vector2 position = positionComponent.getPosition(); | |
int x = (int)position.x, y = (int)position.y; | |
Coord nextCoord = null; | |
Path path = entityPathMap.get(eventEntity); | |
Iterator<Coord> pathIterator = path.getPath().iterator(); | |
while(pathIterator.hasNext()) { | |
Coord coord = pathIterator.next(); | |
if(coord.x == x && coord.y == y) { | |
if(pathIterator.hasNext()) | |
nextCoord = pathIterator.next(); | |
else | |
nextCoord = coord; | |
} | |
} | |
if(nextCoord == null) | |
nextCoord = path.getPath().get(0); | |
actionSystem.setNextAction(eventEntity, new MoveToAction(nextCoord)); | |
} | |
@Subscribe | |
void handleEvent(StopWanderingEvent event) { | |
System.out.println("Stopping wandering."); | |
Entity eventEntity = event.getEntity(); | |
entityPathMap.put(eventEntity, null); | |
} | |
public Map<Entity, Path> getEntityDestinationMap() { | |
return this.entityPathMap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment