int BasicMonster::navigateTo(Point target) { // TODO: One centralized dijkstra should improve performance significantly TCODPath path = TCODPath(level->getWidth(), level->getHeight(), new PathFindingCallback(), level); path.compute(position.x, position.y, target.x, target.y); Point step; if (!path.isEmpty() && path.walk(&step.x, &step.y, true)) { Creature* c = level->creatureAt(step); if (c != NULL) { // TODO: more elegant solution? return 10; } else if (getStatusStrength(STATUS_BEARTRAP) > 0) { // TODO: resolve bear trap and other statuses somewhere else if (rng->getInt(0,9) == 0) endStatus(STATUS_BEARTRAP); return 10; } else { float diagonal = (step.x - position.x != 0 && step.y - position.y != 0) ? std::sqrt(2.f) : 1.f; moveTo(step); return static_cast<int>(getWalkingSpeed() * diagonal); } } else { return 0; } }
void Dungeon::drawPath(int y0, int y1, int x0, int x1, int width, char *tiles, void (*tileCallback)(char *)) { TCODMap *map = new TCODMap(m_width, m_height); for(int y=0;y<m_height;y++){ for(int x=0;x<m_width;x++){ map->setProperties(x,y,true,tiles[x+y*width] != 127); } } TCODPath *path = new TCODPath(map,0.0f); bool success = path->compute(x0,y0,x1,y1); if(success){ int x; int y; while(!path->isEmpty()){ path->walk(&x,&y,false); tileCallback(&(tiles[x+y*width])); } } else{ drawLine(y0,y1,x0,x1,width,tiles,tileCallback); } }