[Fix] Lots of fixes to pathfinding.

This commit is contained in:
Tamir Atias 2012-02-21 03:09:00 +02:00
parent 837ace3c2a
commit 57e3229baf
10 changed files with 31 additions and 94 deletions

View File

@ -5,9 +5,6 @@ SDL_Surface* errorTexture = NULL;
SDL_Rect camera;
SDL_Event event;
int levelWidth;
int levelHeight;
// Start off false. press 'p' to turn it on!
bool debugEnabled = false;

View File

@ -6,9 +6,6 @@ extern SDL_Surface* errorTexture;
extern SDL_Rect camera;
extern SDL_Event event;
extern int levelWidth;
extern int levelHeight;
extern bool debugEnabled;
class EventHistory;

View File

@ -118,21 +118,6 @@ void Player::SetName(string nameArg) {
_name = nameArg;
}
void Player::SetCamera(void) {
camera.x = (Sint16)((x + w / 2) - SCREEN_WIDTH / 2);
camera.y = (Sint16)((y + h / 2) - SCREEN_HEIGHT / 2);
if(camera.x < 0)
camera.x = 0;
if(camera.y < 0)
camera.y = 0;
if(camera.x > levelWidth - camera.w)
camera.x = levelWidth - camera.w;
if(camera.y > levelHeight - camera.h)
camera.y = levelHeight = camera.h;
}
void Player::Move() {
map->MoveIfPossible(this, xVel, yVel, true);
Character::HealthBarScroll();

View File

@ -37,7 +37,6 @@ protected:
void CheckTileCollisions(void);
private:
void SetCamera(void);
static const float PLAYER_SPEED;
string _name;

View File

@ -44,6 +44,10 @@ void NPC::Move(void) {
Character* player = map->GetPlayer();
if(fabs((player->GetX() - x)) > 256 || fabs((player->GetY() - y)) > 256) {
return;
}
SDL_Rect selfRect;
selfRect.x = x - 5;
selfRect.y = y - 5;
@ -96,13 +100,10 @@ void NPC::Move(void) {
}
void NPC::OnPlayerMove(Player* player) {
Vec2 selfPos(x, y);
Vec2 playerPos(player->GetX(), player->GetY());
if(Vec2::DistanceSquared(selfPos, playerPos) > 96*96) {
if(fabs((player->GetX() - x)) > 256 || fabs((player->GetY() - y)) > 256) {
return;
}
AStarTile& start = map->GetAStarTile(x / AStarTile::FAKE_SIZE, y / AStarTile::FAKE_SIZE);
AStarTile& goal = map->GetAStarTile(player->GetX() / AStarTile::FAKE_SIZE, player->GetY() / AStarTile::FAKE_SIZE);

View File

@ -34,7 +34,7 @@ bool AStarTile::GetSuccessors(AStarSearch<AStarTile>* search) {
}
}
if(x < (levelWidth / FAKE_SIZE)) {
if(x < (LevelGen::ASTAR_ARRAY_WIDTH - 2)) {
AStarTile& successor = _level->GetAStarTile(x + 1, y);
if(successor._passable) {
if(!search->AddSuccessor(successor)) {
@ -52,7 +52,7 @@ bool AStarTile::GetSuccessors(AStarSearch<AStarTile>* search) {
}
}
if(y < (levelHeight / FAKE_SIZE)) {
if(y < (LevelGen::ASTAR_ARRAY_HEIGHT - 2)) {
AStarTile& successor = _level->GetAStarTile(x, y + 1);
if(successor._passable) {
if(!search->AddSuccessor(successor)) {

View File

@ -7,7 +7,7 @@ class LevelGen;
class AStarTile {
public:
AStarTile() {}
AStarTile(LevelGen* level, int xArg, int yArg, bool passable) : _level(level), x(xArg), y(yArg), _passable(passable) {}
AStarTile(LevelGen* level, int xArg, int yArg, bool passable) : _level(level), _passable(passable), x(xArg), y(yArg) {}
bool IsSameState(AStarTile& tile);
bool IsGoal(AStarTile& tile);
@ -18,7 +18,7 @@ public:
int GetX() { return x * FAKE_SIZE; }
int GetY() { return y * FAKE_SIZE; }
static const int FAKE_SIZE = 16;
static const int FAKE_SIZE = 32;
private:
LevelGen* _level;

View File

@ -16,12 +16,9 @@ void LevelGen::New(void) {
Unload();
_world = WorldManager(this);
levelWidth = TILE_ARRAY_WIDTH;
levelHeight = TILE_ARRAY_HEIGHT;
for(x = 0; x < levelWidth; x++) {
for(y = 0; y < levelHeight; y++) {
for(x = 0; x < TILE_ARRAY_WIDTH; x++) {
for(y = 0; y < TILE_ARRAY_HEIGHT; y++) {
_tile[x][y].SetTileTextureName("grass");
stringstream tilePath;
@ -35,9 +32,6 @@ void LevelGen::New(void) {
}
}
levelWidth *= TILE_WIDTH;
levelHeight *= TILE_HEIGHT;
// procedural generation
DoMagic();
@ -143,8 +137,6 @@ void LevelGen::Load(const string& filename) {
// </line>
}
// </map>
levelWidth = x * TILE_WIDTH;
levelHeight = y * TILE_HEIGHT;
_world = WorldManager(this);
@ -158,13 +150,10 @@ void LevelGen::Save(const string& filename){
TiXmlElement* rootElem = new TiXmlElement("map");
int levelWidthTiles = levelWidth / TILE_WIDTH;
int levelHeightTiles = levelHeight / TILE_HEIGHT;
for(y = 0; y < levelHeightTiles; y++) {
for(y = 0; y < TILE_ARRAY_WIDTH; y++) {
TiXmlElement* lineElem = new TiXmlElement("line");
for(x = 0; x < levelWidthTiles; x++) {
for(x = 0; x < TILE_ARRAY_HEIGHT; x++) {
TiXmlElement* tileElem = new TiXmlElement("tile");
TiXmlElement* tileTextureElem = new TiXmlElement("tileTexture");
@ -217,37 +206,8 @@ void LevelGen::Update(void) {
}
void LevelGen::Render(void) {
int xOrig = (camera.x / TILE_WIDTH) - 1;
int yOrig = (camera.y / TILE_HEIGHT) - 1;
if (xOrig < 0) xOrig = 0;
if (yOrig < 0) yOrig = 0;
int xEnd = xOrig + (SCREEN_WIDTH / TILE_WIDTH) + 3;
int yEnd = yOrig + (SCREEN_HEIGHT / TILE_HEIGHT) + 3;
/* the fuck is this Allanis? --konom
if(xEnd < x)
xEnd++;
else
xEnd = x;
if(yEnd < y)
yEnd++;
else
yEnd = y;
*/
if (xEnd > x) xEnd = x;
if (yEnd > y) yEnd = y;
if (xEnd < 0) xEnd = 0;
if (yEnd < 0) yEnd = 0;
if (xOrig > xEnd) xOrig = xEnd - 1;
if (yOrig > yEnd) yOrig = yEnd - 1;
for(int i = xOrig; i < xEnd; i++) {
for(int j = yOrig; j < yEnd; j++) {
for(int i = 0; i < TILE_ARRAY_WIDTH; i++) {
for(int j = 0; j < TILE_ARRAY_HEIGHT; j++) {
_tile[i][j].Render();
}
}
@ -283,8 +243,8 @@ void LevelGen::GenerateEntities(const string& name, int frequency) {
int nextEntityGen = 1 + (rand() % frequency);
std::string filename = "../Data/Media/Images/Entities/" + name + ".png";
for(int x = 0; x < TILE_ARRAY_WIDTH; x++) {
for(int y = 0; y < TILE_ARRAY_HEIGHT; y++) {
for(int x = 0; x < (TILE_ARRAY_WIDTH - 1); x++) {
for(int y = 0; y < (TILE_ARRAY_HEIGHT - 1); y++) {
nextEntityGen--;
if(!_tile[x][y].GetTileSolidity() && !_tile[x][y].GetEntitySolitity() && nextEntityGen <= 0) {
_tile[x][y].SetEntityTextureName(name);
@ -324,8 +284,8 @@ void LevelGen::MakeWalkingPaths(void) {
}
void LevelGen::FindSpawnPoint(int& xArg, int& yArg, int objWidth, int objHeight) {
xArg = rand() % (TILE_ARRAY_WIDTH * TILE_WIDTH);
yArg = rand() % (TILE_ARRAY_HEIGHT * TILE_HEIGHT);
xArg = rand() % ((TILE_ARRAY_WIDTH - 1) * TILE_WIDTH);
yArg = rand() % ((TILE_ARRAY_HEIGHT - 1) * TILE_HEIGHT);
if((xArg + objWidth + 1) > SCREEN_WIDTH) {
xArg = SCREEN_WIDTH - objWidth - 1;
@ -494,11 +454,8 @@ bool LevelGen::AStarTilePassable(int xArg, int yArg) {
}
void LevelGen::UpdateAStarTiles(void) {
int maxX = levelWidth / AStarTile::FAKE_SIZE;
int maxY = levelHeight / AStarTile::FAKE_SIZE;
for(int x = 0; x < maxX; x++) {
for(int y = 0; y < maxY; y++) {
for(int x = 0; x < ASTAR_ARRAY_WIDTH; x++) {
for(int y = 0; y < ASTAR_ARRAY_HEIGHT; y++) {
_astarTile[x][y] = AStarTile(this, x, y, AStarTilePassable(x, y));
}
}

View File

@ -55,6 +55,11 @@ public:
void SetPlayer(Player* player) { _player = player; }
Player* GetPlayer() { return _player; }
static const int TILE_ARRAY_WIDTH = (SCREEN_WIDTH / TILE_WIDTH) + 1;
static const int TILE_ARRAY_HEIGHT = (SCREEN_HEIGHT / TILE_HEIGHT) + 1;
static const int ASTAR_ARRAY_WIDTH = TILE_ARRAY_WIDTH * (TILE_WIDTH / AStarTile::FAKE_SIZE);
static const int ASTAR_ARRAY_HEIGHT = TILE_ARRAY_HEIGHT * (TILE_HEIGHT / AStarTile::FAKE_SIZE);
private:
void Unload(void);
@ -69,12 +74,8 @@ private:
int x;
int y;
static const int TILE_ARRAY_WIDTH = (SCREEN_WIDTH / TILE_WIDTH) + 1;
static const int TILE_ARRAY_HEIGHT = (SCREEN_HEIGHT / TILE_HEIGHT) + 1;
MapTile _tile[TILE_ARRAY_WIDTH][TILE_ARRAY_HEIGHT];
static const int ASTAR_ARRAY_WIDTH = TILE_ARRAY_WIDTH * (TILE_WIDTH / AStarTile::FAKE_SIZE);
static const int ASTAR_ARRAY_HEIGHT = TILE_ARRAY_HEIGHT * (TILE_HEIGHT / AStarTile::FAKE_SIZE);
AStarTile _astarTile[ASTAR_ARRAY_WIDTH][ASTAR_ARRAY_HEIGHT];
TextureManager _tileTextures;

View File

@ -26,17 +26,17 @@ float Vec2::Length(void) {
}
float Vec2::LengthSquared(void) {
return (x * x) + (y + y);
return (x * x) + (y * y);
}
// Static.
float Vec2::Distance(const Vec2& value1, const Vec2& value2) {
return Vec2(value1 - value2).Length();
return (value1 - value2).Length();
}
// Static.
float Vec2::DistanceSquared(const Vec2& value1, const Vec2& value2) {
return Vec2(value1 - value2).LengthSquared();
return (value1 - value2).LengthSquared();
}
// Static.