summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2005-02-02 21:00:59 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2005-02-02 21:00:59 +0000
commit7b53da553fdc1389a159703bfade87083155f15f (patch)
treebb508d379289aec01d342575d11da11c89b892f8
parentc4e5b796f722c3f65c5431ff2f67f4d23fcbf98d (diff)
downloadmana-client-7b53da553fdc1389a159703bfade87083155f15f.tar.gz
mana-client-7b53da553fdc1389a159703bfade87083155f15f.tar.bz2
mana-client-7b53da553fdc1389a159703bfade87083155f15f.tar.xz
mana-client-7b53da553fdc1389a159703bfade87083155f15f.zip
Turned MAP struct into Map class.
-rw-r--r--src/astar.cpp112
-rw-r--r--src/game.cpp12
-rw-r--r--src/graphic/graphic.cpp12
-rw-r--r--src/map.cpp98
-rw-r--r--src/map.h75
5 files changed, 156 insertions, 153 deletions
diff --git a/src/astar.cpp b/src/astar.cpp
index a1683699..601a2da9 100644
--- a/src/astar.cpp
+++ b/src/astar.cpp
@@ -1,39 +1,40 @@
#include "astar.h"
#include "being.h"
+#include "map.h"
const int numberPeople = 1;
- int onClosedList = 10;
- const int notfinished = 0;// path-related constants
+int onClosedList = 10;
+const int notfinished = 0;// path-related constants
- //Create needed arrays
- //char get_path_walk [MAP_WIDTH][MAP_HEIGHT];
- int openList[MAP_WIDTH*MAP_HEIGHT+2]; //1 dimensional array holding ID# of open list items
- int whichList[MAP_WIDTH+1][MAP_HEIGHT+1]; //2 dimensional array used to record
+//Create needed arrays
+//char tiledMap.getPathWalk [MAP_WIDTH][MAP_HEIGHT];
+int openList[MAP_WIDTH*MAP_HEIGHT+2]; //1 dimensional array holding ID# of open list items
+int whichList[MAP_WIDTH+1][MAP_HEIGHT+1]; //2 dimensional array used to record
// whether a cell is on the open list or on the closed list.
- int openX[MAP_WIDTH*MAP_HEIGHT+2]; //1d array stores the x location of an item on the open list
- int openY[MAP_WIDTH*MAP_HEIGHT+2]; //1d array stores the y location of an item on the open list
- int parentX[MAP_WIDTH+1][MAP_HEIGHT+1]; //2d array to store parent of each cell (x)
- int parentY[MAP_WIDTH+1][MAP_HEIGHT+1]; //2d array to store parent of each cell (y)
- int F_cost[MAP_WIDTH*MAP_HEIGHT+2]; //1d array to store F cost of a cell on the open list
- int G_cost[MAP_WIDTH+1][MAP_HEIGHT+1]; //2d array to store G_cost cost for each cell.
- int H_cost[MAP_WIDTH*MAP_HEIGHT+2]; //1d array to store H cost of a cell on the open list
- int pathLength; //stores length of the FOUND path for critter
- int pathLocation; //stores current position along the chosen path for critter
- int* path_bank ;
-
- //Path reading variables
- int pathStatus;
- int xPath;
- int yPath;
+int openX[MAP_WIDTH*MAP_HEIGHT+2]; //1d array stores the x location of an item on the open list
+int openY[MAP_WIDTH*MAP_HEIGHT+2]; //1d array stores the y location of an item on the open list
+int parentX[MAP_WIDTH+1][MAP_HEIGHT+1]; //2d array to store parent of each cell (x)
+int parentY[MAP_WIDTH+1][MAP_HEIGHT+1]; //2d array to store parent of each cell (y)
+int F_cost[MAP_WIDTH*MAP_HEIGHT+2]; //1d array to store F cost of a cell on the open list
+int G_cost[MAP_WIDTH+1][MAP_HEIGHT+1]; //2d array to store G_cost cost for each cell.
+int H_cost[MAP_WIDTH*MAP_HEIGHT+2]; //1d array to store H cost of a cell on the open list
+int pathLength; //stores length of the FOUND path for critter
+int pathLocation; //stores current position along the chosen path for critter
+int* path_bank ;
+
+//Path reading variables
+int pathStatus;
+int xPath;
+int yPath;
/** Initialize pathfinder */
void pathfinder_init() {
- path_bank = (int*)malloc(4);
+ path_bank = (int*)malloc(4);
}
/** Exit pathfinder */
void pathfinder_exit() {
- free(path_bank);
+ free(path_bank);
}
/** Find path */
@@ -49,7 +50,7 @@ PATH_NODE *find_path(int pathfinderID, int s_x, int s_y, int e_x, int e_y) {
else if (s_x==e_x && s_y==e_y && pathLocation==0)return NULL;
// If dest tile is NOT_WALKABLE, return that it's a NOT_FOUND path.
- if(get_path_walk(e_x, e_y)==NOT_WALKABLE) {
+ if (tiledMap.getPathWalk(e_x, e_y) == NOT_WALKABLE) {
xPath = s_x;
yPath = s_y;
return NULL;
@@ -121,31 +122,31 @@ PATH_NODE *find_path(int pathfinderID, int s_x, int s_y, int e_x, int e_y) {
// for later consideration if appropriate (see various if statements
// below).
- for(b=parentYval-1;b<=parentYval+1;b++) {
- for(a=parentXval-1;a<=parentXval+1;a++) {
+ for (b = parentYval - 1; b <= parentYval + 1; b++) {
+ for (a = parentXval - 1; a <= parentXval + 1; a++) {
// If not off the map (do this first to avoid array out-of-bounds errors)
- if(a!=-1 && b!=-1 && a!=MAP_WIDTH && b!=MAP_HEIGHT) {
+ if (a!=-1 && b!=-1 && a!=MAP_WIDTH && b!=MAP_HEIGHT) {
// If not already on the closed list (items on the closed list have
// already been considered and can now be ignored).
- if(whichList[a][b]!=onClosedList) {
+ if (whichList[a][b]!=onClosedList) {
// If not a wall/obstacle square.
- if (get_path_walk(a, b)!=NOT_WALKABLE) {
+ if (tiledMap.getPathWalk(a, b) != NOT_WALKABLE) {
// Don't cut across corners
corner = WALKABLE;
- if(a==parentXval-1) {
- if(b==parentYval-1) {
- if(get_path_walk(parentXval-1, parentYval)==NOT_WALKABLE || get_path_walk(parentXval, parentYval-1)==NOT_WALKABLE) // cera slash
+ if (a == parentXval-1) {
+ if (b == parentYval-1) {
+ if (tiledMap.getPathWalk(parentXval-1, parentYval)==NOT_WALKABLE || tiledMap.getPathWalk(parentXval, parentYval-1)==NOT_WALKABLE) // cera slash
corner = NOT_WALKABLE;
} else if (b==parentYval+1) {
- if(get_path_walk(parentXval, parentYval+1)==NOT_WALKABLE || get_path_walk(parentXval-1, parentYval)==NOT_WALKABLE)
+ if (tiledMap.getPathWalk(parentXval, parentYval+1)==NOT_WALKABLE || tiledMap.getPathWalk(parentXval-1, parentYval)==NOT_WALKABLE)
corner = NOT_WALKABLE;
}
- } else if(a==parentXval+1) {
- if(b==parentYval-1) {
- if(get_path_walk(parentXval, parentYval-1)==NOT_WALKABLE || get_path_walk(parentXval+1, parentYval)==NOT_WALKABLE)
+ } else if (a == parentXval+1) {
+ if (b == parentYval-1) {
+ if (tiledMap.getPathWalk(parentXval, parentYval-1)==NOT_WALKABLE || tiledMap.getPathWalk(parentXval+1, parentYval)==NOT_WALKABLE)
corner = NOT_WALKABLE;
- } else if(b==parentYval+1) {
- if(get_path_walk(parentXval+1, parentYval)==NOT_WALKABLE || get_path_walk(parentXval, parentYval+1)==NOT_WALKABLE)
+ } else if (b==parentYval+1) {
+ if (tiledMap.getPathWalk(parentXval+1, parentYval)==NOT_WALKABLE || tiledMap.getPathWalk(parentXval, parentYval+1)==NOT_WALKABLE)
corner = NOT_WALKABLE;
}
}
@@ -288,41 +289,36 @@ PATH_NODE *find_path(int pathfinderID, int s_x, int s_y, int e_x, int e_y) {
PATH_NODE *ret = NULL, *temp = NULL;
pathLocation = 1;
- ret = new PATH_NODE(s_x, s_y);
- temp = ret;
- //alert(stringa,"","","","",0,0);
+ ret = new PATH_NODE(s_x, s_y);
+ temp = ret;
+ //alert(stringa,"","","","",0,0);
while(pathLocation<pathLength) {
sprintf(stringa,"%i %i",path_bank[pathLocation*2-2], path_bank[pathLocation*2-1]);
- //alert(stringa,"","","","",0,0);
- temp->next = new PATH_NODE(
+ //alert(stringa,"","","","",0,0);
+ temp->next = new PATH_NODE(
path_bank[pathLocation * 2 - 2],
path_bank[pathLocation * 2 - 1]);
- if(temp->next==NULL) throw "Unable to create path node";
+ if(temp->next==NULL) throw "Unable to create path node";
temp = temp->next;
pathLocation++;
}
- if(temp!=NULL)temp->next = new PATH_NODE(e_x, e_y);
- else throw "Null reference";
+ if(temp!=NULL)temp->next = new PATH_NODE(e_x, e_y);
+ else throw "Null reference";
return ret;
}
return NULL; // Path not found
}
-
-
/** Read the path data */
void ReadPath(int pathfinderID) {
- //If a path exists, read the path data
- // from the pathbank.
- pathLocation = 1; //set pathLocation to 1st step
- while (pathLocation<pathLength) {
+ //If a path exists, read the path data
+ // from the pathbank.
+ pathLocation = 1; //set pathLocation to 1st step
+ while (pathLocation<pathLength) {
int a = path_bank [pathLocation*2-2];
int b = path_bank [pathLocation*2-1];
- pathLocation = pathLocation + 1;
- whichList[a][b] = 3;//draw dotted path
- }
+ pathLocation = pathLocation + 1;
+ whichList[a][b] = 3;//draw dotted path
+ }
}
-
-
-
diff --git a/src/game.cpp b/src/game.cpp
index 5228f80c..8f86d63f 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -120,7 +120,7 @@ void game() {
}
void do_init() {
- if (!load_map(map_path)) {
+ if (!tiledMap.load(map_path)) {
error("Could not find map file");
}
@@ -302,7 +302,7 @@ void do_input()
if (keys[SDLK_UP] || keys[SDLK_KP8])
{
// UP
- if (get_walk(x, y - 1) != 0)
+ if (tiledMap.getWalk(x, y - 1) != 0)
{
walk(x, y-1, NORTH);
walk_status = 1;
@@ -318,7 +318,7 @@ void do_input()
else if (keys[SDLK_DOWN] || keys[SDLK_KP2])
{
// Down
- if (get_walk(x, y + 1) != 0)
+ if (tiledMap.getWalk(x, y + 1) != 0)
{
walk(x, y + 1, SOUTH);
walk_status = 1;
@@ -333,7 +333,7 @@ void do_input()
}
else if (keys[SDLK_LEFT] || keys[SDLK_KP4])
{
- if (get_walk(x - 1, y) != 0) {
+ if (tiledMap.getWalk(x - 1, y) != 0) {
walk(x - 1, y, WEST);
walk_status = 1;
src_x = x;
@@ -347,7 +347,7 @@ void do_input()
}
else if (keys[SDLK_RIGHT] || keys[SDLK_KP6])
{
- if (get_walk(x + 1, y) != 0) {
+ if (tiledMap.getWalk(x + 1, y) != 0) {
walk(x + 1, y, EAST);
walk_status = 1;
src_x = x;
@@ -604,7 +604,7 @@ void do_parse() {
memset(map_path, '\0', 480);
strcat(map_path, "./data/map/");
strncat(map_path, RFIFOP(2), 497 - strlen(map_path));
- if (load_map(map_path)) {
+ if (tiledMap.load(map_path)) {
Being *temp;
temp = new Being();
memcpy(temp, player_node, sizeof(Being));
diff --git a/src/graphic/graphic.cpp b/src/graphic/graphic.cpp
index 3058ce56..591c7443 100644
--- a/src/graphic/graphic.cpp
+++ b/src/graphic/graphic.cpp
@@ -331,14 +331,14 @@ void Engine::draw()
// Draw tiles below nodes
for (int j = 0; j < 20; j++) {
for (int i = 0; i < 26; i++) {
- unsigned short tile0 = get_tile(i + camera_x, j + camera_y, 0);
- unsigned short tile1 = get_tile(i + camera_x, j + camera_y, 1);
+ int tile0 = tiledMap.getTile(i + camera_x, j + camera_y, 0);
+ int tile1 = tiledMap.getTile(i + camera_x, j + camera_y, 1);
- if (tile0 < tileset->spriteset.size()) {
+ if (tile0 < (int)tileset->spriteset.size()) {
tileset->spriteset[tile0]->draw(screen,
i * 32 - offset_x, j * 32 - offset_y);
}
- if (tile1 > 0 && tile1 < tileset->spriteset.size()) {
+ if (tile1 > 0 && tile1 < (int)tileset->spriteset.size()) {
tileset->spriteset[tile1]->draw(screen,
i * 32 - offset_x, j * 32 - offset_y);
}
@@ -461,9 +461,9 @@ void Engine::draw()
// Draw tiles above nodes
for (int j = 0; j < 20; j++) {
for (int i = 0; i < 26; i++) {
- unsigned short tile = get_tile(i + camera_x, j + camera_y, 2);
+ int tile = tiledMap.getTile(i + camera_x, j + camera_y, 2);
- if (tile > 0 && tile < tileset->spriteset.size()) {
+ if (tile > 0 && tile < (int)tileset->spriteset.size()) {
tileset->spriteset[tile]->draw(
screen, i * 32 - offset_x, j * 32 - offset_y);
}
diff --git a/src/map.cpp b/src/map.cpp
index a958b2e6..5a3efc1a 100644
--- a/src/map.cpp
+++ b/src/map.cpp
@@ -33,84 +33,86 @@
#include "./net/win2linux.h"
#endif
-MAP tiled_map;
+Map tiledMap;
-bool load_map(char *map_file) {
- FILE *file = fopen(map_file, "r");
+
+bool Map::load(char *mapFile) {
+ FILE *file = fopen(mapFile, "r");
if (!file) {
- warning(map_file);
+ warning(mapFile);
return false;
}
- fread(&tiled_map, sizeof(MAP), 1, file);
+ fread(this, sizeof(Map), 1, file);
fclose(file);
return true;
}
-void set_walk(short x_c, short y_c, bool walkable) {
- if (walkable == true) tiled_map.tiles[x_c][y_c].data[3] |= 0x0002;
- else tiled_map.tiles[x_c][y_c].data[3] &= 0x00fd;
+void Map::setWalk(int x, int y, bool walkable) {
+ if (walkable) tiles[x][y].data[3] |= 0x0002;
+ else tiles[x][y].data[3] &= 0x00fd;
}
-bool get_walk(short x_c, short y_c) {
- bool ret = (tiled_map.tiles[x_c][y_c].data[3] & 0x0002)>0;
- if (ret == true) {
- std::list<Being *>::iterator i = beings.begin();
- while (i != beings.end() && ret == true) {
+bool Map::getWalk(int x, int y) {
+ bool ret = (tiles[x][y].data[3] & 0x0002) > 0;
+
+ if (ret) {
+ // Check for colliding into a being
+ std::list<Being*>::iterator i = beings.begin();
+ while (i != beings.end() && ret) {
Being *being = (*i);
- if (being->x==x_c && being->y==y_c)
+ if (being->x == x && being->y == y) {
ret = false;
+ }
i++;
}
- return ret;
- } else return false;
-}
+ }
-unsigned char get_path_walk(unsigned short x, unsigned short y) {
- if (get_walk(x, y)) return 0;
- else return 1;
+ return ret;
}
-bool get_anim(short x_c, short y_c, char layer) {
- char temp = tiled_map.tiles[x_c][y_c].flags & 0x00C0;
- temp >>= 6;
- if(abs(temp)==layer)return (tiled_map.tiles[x_c][y_c].data[3] & 0x0001)>0;
- else return false;
+int Map::getPathWalk(int x, int y) {
+ if (getWalk(x, y)) return 0;
+ else return 1;
}
-void set_tile(short x_c, short y_c, char layer, unsigned short id) {
+void Map::setTile(int x, int y, int layer, unsigned short id) {
if (layer == 0) {
id <<= 6;
- tiled_map.tiles[x_c][y_c].data[0] = HIBYTE(id);
- tiled_map.tiles[x_c][y_c].data[1] &= 0x003f;
- tiled_map.tiles[x_c][y_c].data[1] |= LOBYTE(id);
- } else if (layer == 1) {
+ tiles[x][y].data[0] = HIBYTE(id);
+ tiles[x][y].data[1] &= 0x003f;
+ tiles[x][y].data[1] |= LOBYTE(id);
+ }
+ else if (layer == 1) {
id <<= 4;
- tiled_map.tiles[x_c][y_c].data[1] &= 0x00c0;
- tiled_map.tiles[x_c][y_c].data[1] |= HIBYTE(id);
- tiled_map.tiles[x_c][y_c].data[2] &= 0x000f;
- tiled_map.tiles[x_c][y_c].data[2] |= LOBYTE(id);
- } else if (layer == 2) {
+ tiles[x][y].data[1] &= 0x00c0;
+ tiles[x][y].data[1] |= HIBYTE(id);
+ tiles[x][y].data[2] &= 0x000f;
+ tiles[x][y].data[2] |= LOBYTE(id);
+ }
+ else if (layer == 2) {
id <<= 2;
- tiled_map.tiles[x_c][y_c].data[2] &= 0x00f0;
- tiled_map.tiles[x_c][y_c].data[2] |= HIBYTE(id);
- tiled_map.tiles[x_c][y_c].data[3] &= 0x0003;
- tiled_map.tiles[x_c][y_c].data[3] |= LOBYTE(id);
+ tiles[x][y].data[2] &= 0x00f0;
+ tiles[x][y].data[2] |= HIBYTE(id);
+ tiles[x][y].data[3] &= 0x0003;
+ tiles[x][y].data[3] |= LOBYTE(id);
}
}
-unsigned short get_tile(short x_c, short y_c, char layer) {
+int Map::getTile(int x, int y, int layer) {
unsigned short id = 0;
if (layer == 0) {
- id = MAKEWORD(tiled_map.tiles[x_c][y_c].data[1] & 0x00c0,
- tiled_map.tiles[x_c][y_c].data[0]);
+ id = MAKEWORD(tiles[x][y].data[1] & 0x00c0,
+ tiles[x][y].data[0]);
id >>= 6;
- } else if (layer == 1) {
- id = MAKEWORD(tiled_map.tiles[x_c][y_c].data[2] & 0x00f0,
- tiled_map.tiles[x_c][y_c].data[1] & 0x003f);
+ }
+ else if (layer == 1) {
+ id = MAKEWORD(tiles[x][y].data[2] & 0x00f0,
+ tiles[x][y].data[1] & 0x003f);
id >>= 4;
- } else if (layer == 2) {
- id = MAKEWORD(tiled_map.tiles[x_c][y_c].data[3] & 0x00fc,
- tiled_map.tiles[x_c][y_c].data[2] & 0x000f);
+ }
+ else if (layer == 2) {
+ id = MAKEWORD(tiles[x][y].data[3] & 0x00fc,
+ tiles[x][y].data[2] & 0x000f);
id >>= 2;
}
return id;
diff --git a/src/map.h b/src/map.h
index 323a00d0..a4c8e351 100644
--- a/src/map.h
+++ b/src/map.h
@@ -59,44 +59,49 @@ l - Animated layer
f - future use
*/
struct TILE {
- char data[4];
- char flags;
+ char data[4];
+ char flags;
};
-struct MAP {
- TILE tiles[MAP_WIDTH][MAP_HEIGHT];
- char tileset[20];
- char bg_music[20];
+class Map
+{
+ public:
+ /**
+ * Loads a map file
+ */
+ bool load(char *mapFile);
+
+ /**
+ * Set tile ID.
+ */
+ void setTile(int x, int y, int layer, unsigned short id);
+
+ /**
+ * Get tile ID.
+ */
+ int getTile(int x, int y, int layer);
+
+ /**
+ * Set walkability flag for a tile
+ */
+ void setWalk(int x, int y, bool walkable);
+
+ /**
+ * Tell if a tile is walkable or not
+ */
+ bool getWalk(int x, int y);
+
+ /**
+ * Tell if a tile is walkable or not (0=walkable,1=not walkable)
+ */
+ int getPathWalk(int x, int y);
+
+ private:
+ TILE tiles[MAP_WIDTH][MAP_HEIGHT];
+ char tileset[20];
+ char bg_music[20];
};
-/** Loads a map file */
-bool load_map(char *map_file);
-
-/** Set walkability flag for a tile */
-void set_walk(short x_c, short y_c, bool walkable);
-
-/** Tell if a tile is walkable or not */
-bool get_walk(short x_c, short y_c);
-
-/** Tell if a tile is walkable or not (0=walkable,1=not walkable) */
-unsigned char get_path_walk(unsigned short x, unsigned short y);
-
-/** Tell if a tile is animated or not */
-bool get_anim(short x_c, short y_c, char layer);
-
-/** Set tile ID */
-void set_tile(short x_c, short y_c, char layer, unsigned short id);
-
-/** Return tile ID */
-unsigned short get_tile(short x_c, short y_c, char layer);
-
-/** Return tile x coordinate in tileset */
-inline int get_tile_x(int x, int y, int layer) {
- return get_tile(x,y,layer)%TILESET_WIDTH;
-}
-/** Return tile y coordinate in tileset */
-inline int get_tile_y(int x, int y, int layer) {
- return get_tile(x,y,layer)/TILESET_WIDTH;
-}
+extern Map tiledMap;
#endif