public static int Main(string[] args) { TCODConsole.initRoot(100, 75, "my game", false); TCODMap map = new TCODMap(100, 75); // first, create a new map. look under fov toolkit for more on this for (int x = 0; x < 100; x++) { // time to fill in our map details for (int y = 0; y < 75; y++) { map.setProperties(x, y, true, true); // i'm setting the entire map to walkable. here is where you need to set the walkable status, based on the map generated } } TCODPath path = new TCODPath(map, 1.0f); // here we create the path object, using our map. the diagonal movement cost here is set to 1.0f int creatureX = 25; // TCODRandom.getInstance().getInt(0, 99); // let's start our creature at a random point int creatureY = 25; //TCODRandom.getInstance().getInt(0, 74); int destinationX = 75; //TCODRandom.getInstance().getInt(0, 99); // and let's give our beast a random destination int destinationY = 50; // TCODRandom.getInstance().getInt(0, 74); path.compute(creatureX, creatureY, destinationX, destinationY); // now we compute the path while (!path.isEmpty()) { // a little while loop that ends when the destination is reached TCODConsole.root.putCharEx(creatureX, creatureY, '@', TCODColor.white, TCODColor.black); TCODConsole.flush(); // here is where we draw the creature on the screen. we won't be overwriting anything, so he'll leave a trail path.walk(ref creatureX, ref creatureY, true); // here we walk the path. by setting the last arg to true, we'll automatically recompute the path if the path is blocked TCODConsole.waitForKeypress(true); // a little something so that you can watch the movement in a action. just click any key to move } return 0; }
public void OnPartialUpdate(int x, int y, bool walkable, bool visible) { _currentMap.setProperties(x, y, visible, walkable); if (_currentPath == null) { return; } _currentPath.Dispose(); _currentPath = new TCODPath(_currentMap, 1.14f); ComputePath(_currentX, _currentY, _destX, _destY); }
public void OnUpdate(TCODMap currentMap) { _currentMap.copy(currentMap); if (_currentPath == null) { return; } _currentPath.Dispose(); _currentPath = new TCODPath(_currentMap, 1.14f); ComputePath(_currentX, _currentY, _destX, _destY); }
public void ComputePath(int origX, int origY, int destX, int destY) { if (_currentPath == null) { _currentPath = new TCODPath(_currentMap, 1.14f); } _currentPath.compute(origX, origY, destX, destY); _currentX = origX; _currentY = origY; _destX = destX; _destY = destY; }
public bool moveAI(int plaX, int plaY, TCODMap map) { counter = counter - speed; if (counter <= 0) { walk = true; counter = counter + 4; } if (walk) { walk = false; switch (ai) { case AIType.unmoveable: return(checkAttack(plaX, plaY)); case AIType.forgetful: path = new TCODPath(map); path.compute(x, y, plaX, plaY); if (path.size() < 5 && path.size() > 1) { path.walk(ref x, ref y, false); return(false); } else { return(checkAttack(plaX, plaY)); } case AIType.guard: TCODPath tmpPath = new TCODPath(map); tmpPath.compute(x, y, origX, origY); path = new TCODPath(map); path.compute(x, y, plaX, plaY); if (path.size() < 5 && path.size() > 1 && tmpPath.size() < 3) { path.walk(ref x, ref y, false); return(false); } else if (path.size() == 1) { return(checkAttack(plaX, plaY)); } else { tmpPath.walk(ref x, ref y, false); return(false); } case AIType.warrior: path = new TCODPath(map); path.compute(x, y, plaX, plaY); if (path.size() < 8 && path.size() > 1) { path.walk(ref x, ref y, false); return(false); } else { return(checkAttack(plaX, plaY)); } case AIType.psycho: path = new TCODPath(map); path.compute(x, y, plaX, plaY); if (path.size() > 1) { path.walk(ref x, ref y, false); return(false); } else { return(checkAttack(plaX, plaY)); } } return(false); } return(false); }
public SubscribedFoVMap(TCODMap fovMap) { _currentPath = null; _currentMap = new TCODMap(1000, 1000); OnUpdate(fovMap); }
public Character() { LastLevel = Level; Pathfinder = new TCODPath(Area.Current.Map, 1.0f); Game.Current.Characters.Add(this); }
void render_path(bool first, TCODKey key) { if (map == null) { // initialize the map map = new TCODMap(SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT); for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; y++) { for (int x = 0; x < SAMPLE_SCREEN_WIDTH; x++) { if (smap[y][x] == ' ') map.setProperties(x, y, true, true);// ground else if (smap[y][x] == '=') map.setProperties(x, y, true, false); // window } } } if (first) { TCODSystem.setFps(30); // fps limited to 30 // we draw the foreground only the first time. // during the player movement, only the @ is redrawn. // the rest impacts only the background color // draw the help text & player @ sampleConsole.clear(); sampleConsole.setForegroundColor(TCODColor.white); sampleConsole.print(1, 1, "IJKL / mouse :\nmove destination\nTAB : A*/dijkstra"); sampleConsole.print(1, 4, "Using : A*"); sampleConsole.setForegroundColor(TCODColor.black); sampleConsole.putChar(px, py, '@'); // draw windows for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; y++) { for (int x = 0; x < SAMPLE_SCREEN_WIDTH; x++) { if (smap[y][x] == '=') { sampleConsole.putChar(x, y, '='); } } } recalculatePath = true; } if (recalculatePath) { if (usingAstar) { if (AStrPath == null) AStrPath = new TCODPath(map, 1.41f); AStrPath.compute(px, py, dx, dy); } else { if (DijkstraPath == null) DijkstraPath = new TCODDijkstra(map, 1.41f); dijkstraDist = 0.0f; /* compute the distance grid */ DijkstraPath.compute(px, py); /* get the maximum distance (needed for ground shading only) */ for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; y++) { for (int x = 0; x < SAMPLE_SCREEN_WIDTH; x++) { float d = DijkstraPath.getDistance(x, y); if (d > dijkstraDist) dijkstraDist = d; } } // compute the path DijkstraPath.setPath(dx, dy); } recalculatePath = false; busy = .2f; } // draw the dungeon for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; y++) { for (int x = 0; x < SAMPLE_SCREEN_WIDTH; x++) { bool wall = smap[y][x] == '#'; sampleConsole.setCharBackground(x, y, (wall ? darkWall : darkGround), TCODBackgroundFlag.Set); } } // draw the path if (usingAstar) { for (int i = 0; i < AStrPath.size(); i++) { int x, y; AStrPath.get(i, out x, out y); sampleConsole.setCharBackground(x, y, lightGround, TCODBackgroundFlag.Set); } } else { for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; y++) { for (int x = 0; x < SAMPLE_SCREEN_WIDTH; x++) { bool wall = smap[y][x] == '#'; if (!wall) { float d = DijkstraPath.getDistance(x, y); sampleConsole.setCharBackground(x, y, TCODColor.Interpolate(lightGround, darkGround, (float)0.9 * d / dijkstraDist), TCODBackgroundFlag.Set); } } } for (int i = 0; i < DijkstraPath.size(); i++) { int x, y; DijkstraPath.get(i, out x, out y); sampleConsole.setCharBackground(x, y, lightGround, TCODBackgroundFlag.Set); } } // move the creature busy -= TCODSystem.getLastFrameLength(); if (busy <= 0.0f) { busy += 0.2f; if (usingAstar) { if (!AStrPath.isEmpty()) { sampleConsole.putChar(px, py, ' ', TCODBackgroundFlag.None); AStrPath.walk(ref px, ref py, true); sampleConsole.putChar(px, py, '@', TCODBackgroundFlag.None); } } else { if (!DijkstraPath.isEmpty()) { sampleConsole.putChar(px, py, ' ', TCODBackgroundFlag.None); DijkstraPath.walk(ref px, ref py); sampleConsole.putChar(px, py, '@', TCODBackgroundFlag.None); recalculatePath = true; } } } if ((key.Character == 'I' || key.Character == 'i') && dy > 0) { // destination move north sampleConsole.putChar(dx, dy, oldChar, TCODBackgroundFlag.None); dy--; oldChar = sampleConsole.getChar(dx, dy); sampleConsole.putChar(dx, dy, '+', TCODBackgroundFlag.None); if (smap[dy][dx] == ' ') { recalculatePath = true; } } else if ((key.Character == 'K' || key.Character == 'k') && dy < SAMPLE_SCREEN_HEIGHT - 1) { // destination move south sampleConsole.putChar(dx, dy, oldChar, TCODBackgroundFlag.None); dy++; oldChar = sampleConsole.getChar(dx, dy); sampleConsole.putChar(dx, dy, '+', TCODBackgroundFlag.None); if (smap[dy][dx] == ' ') { recalculatePath = true; } } else if ((key.Character == 'J' || key.Character == 'j') && dx > 0) { // destination move west sampleConsole.putChar(dx, dy, oldChar, TCODBackgroundFlag.None); dx--; oldChar = sampleConsole.getChar(dx, dy); sampleConsole.putChar(dx, dy, '+', TCODBackgroundFlag.None); if (smap[dy][dx] == ' ') { recalculatePath = true; } } else if ((key.Character == 'L' || key.Character == 'l') && dx < SAMPLE_SCREEN_WIDTH - 1) { // destination move east sampleConsole.putChar(dx, dy, oldChar, TCODBackgroundFlag.None); dx++; oldChar = sampleConsole.getChar(dx, dy); sampleConsole.putChar(dx, dy, '+', TCODBackgroundFlag.None); if (smap[dy][dx] == ' ') { recalculatePath = true; } } else if (key.KeyCode == TCODKeyCode.Tab) { usingAstar = !usingAstar; sampleConsole.setForegroundColor(TCODColor.white); if (usingAstar) sampleConsole.print(1, 4, "Using : A* "); else sampleConsole.print(1, 4, "Using : Dijkstra"); sampleConsole.setForegroundColor(TCODColor.black); recalculatePath = true; } TCODMouseData mouse = TCODMouse.getStatus(); int mx = mouse.CellX - SAMPLE_SCREEN_X; int my = mouse.CellY - SAMPLE_SCREEN_Y; if (mx >= 0 && mx < SAMPLE_SCREEN_WIDTH && my >= 0 && my < SAMPLE_SCREEN_HEIGHT && (dx != mx || dy != my)) { sampleConsole.putChar(dx, dy, oldChar, TCODBackgroundFlag.None); dx = mx; dy = my; oldChar = sampleConsole.getChar(dx, dy); sampleConsole.putChar(dx, dy, '+', TCODBackgroundFlag.None); if (smap[dy][dx] == ' ') { recalculatePath = true; } } }
public bool moveAI(int plaX, int plaY, TCODMap map) { counter = counter - speed; if (counter <= 0) { walk = true; counter = counter + 4; } if (walk) { walk = false; switch (ai) { case AIType.unmoveable: return checkAttack(plaX, plaY); case AIType.forgetful: path = new TCODPath(map); path.compute(x, y, plaX, plaY); if (path.size() < 5 && path.size() > 1) { path.walk(ref x, ref y, false); return false; } else { return checkAttack(plaX, plaY); } case AIType.guard: TCODPath tmpPath = new TCODPath(map); tmpPath.compute(x, y, origX, origY); path = new TCODPath(map); path.compute(x, y, plaX, plaY); if (path.size() < 5 && path.size() > 1 && tmpPath.size() < 3) { path.walk(ref x, ref y, false); return false; } else if (path.size() == 1) { return checkAttack(plaX, plaY); } else { tmpPath.walk(ref x, ref y, false); return false; } case AIType.warrior: path = new TCODPath(map); path.compute(x, y, plaX, plaY); if (path.size() < 8 && path.size() > 1) { path.walk(ref x, ref y, false); return false; } else { return checkAttack(plaX, plaY); } case AIType.psycho: path = new TCODPath(map); path.compute(x, y, plaX, plaY); if (path.size() > 1) { path.walk(ref x, ref y, false); return false; } else { return checkAttack(plaX, plaY); } } return false; } return false; }