#include #include #include #include #include #include #include "antsim.hh" #include "map.hh" MapCell::MapCell() : isNest(false), isWall(false), ants(0), food(0), nestScent(1), foodScent(1) { } MapCell::~MapCell() { } Map::Map() : w(0), h(0), data(NULL) { } Map::~Map() { if (data != NULL) delete [] data; } bool Map::load(char* filename) { ifstream file(filename); if (file.bad()) { cerr << "Cannot load file: " << filename << endl; return false; } file >> w >> h; if (w < 5 || w > 1024 || h < 5 || h > 1024) { cerr << "Map dimensions " << w << "x" << h << " are not valid" << endl; file.clear(file.rdstate() & ~ios::failbit); file.close(); return false; } data = new MapCell[w * h]; char c; int count = 0; while (file.get(c) && count < w * h) { if (isalnum(c) || c == '#' || c == '.') { if (c == 'N') data[count++].isNest = true; else if (c == 'F') data[count++].food = 10000; else if (c == '#') data[count++].isWall = true; else if (c == '.') count++; } } file.clear(file.rdstate() & ~ios::failbit); file.close(); return true; } bool Map::valid(int x, int y) { return (x >= 0 && x <= w-1 && y >= 0 && y <= h-1); } bool Map::getNest(int x, int y) { if (valid(x, y)) return data[x + y * w].isNest; else return false; } bool Map::getWall(int x, int y) { if (valid(x, y)) return data[x + y * w].isWall; else return false; } int Map::getAnts(int x, int y) { if (valid(x, y)) return data[x + y * w].ants; else return 0; } int Map::getFood(int x, int y) { if (valid(x, y)) return data[x + y * w].food; else return 0; } int Map::getNestScent(int x, int y) { if (valid(x, y)) return data[x + y * w].nestScent; else return 0; } int Map::getFoodScent(int x, int y) { if (valid(x, y)) return data[x + y * w].foodScent; else return 0; } int Map::getW() { return w; } int Map::getH() { return h; } void Map::setNest(int x, int y, bool value) { if (valid(x, y)) data[x + y * w].isNest = value; } void Map::setWall(int x, int y, bool value) { if (valid(x, y)) data[x + y * w].isWall = value; } void Map::setAnts(int x, int y, int value) { if (valid(x, y)) data[x + y * w].ants = value; } void Map::setFood(int x, int y, int value) { if (valid(x, y)) data[x + y * w].food = value; } void Map::setNestScent(int x, int y, int value) { if (valid(x, y)) data[x + y * w].nestScent = value; } void Map::setFoodScent(int x, int y, int value) { if (valid(x, y)) data[x + y * w].foodScent = value; } void Map::clear() { for (int i = 0; i < w * h; i++) { data[i].isNest = false; data[i].isWall = false; data[i].ants = 0; data[i].food = 0; data[i].nestScent = 0; data[i].foodScent = 0; } } void Map::evaporate(int nestRate, int foodRate) { for (int i = 0; i < w * h; i++) { if (data[i].nestScent > 1) { data[i].nestScent -= nestRate; if (data[i].nestScent < 1) data[i].nestScent = 1; } if (data[i].foodScent > 1) { data[i].foodScent -= foodRate; if (data[i].foodScent < 1) data[i].foodScent = 1; } } } void Map::draw() { cout << "Width: " << w << " Height: " << h << endl; for (int i = 0; i < w * h; i++) { if (i % w == 0) cout << endl; if (data[i].isNest) cout << "N"; else if (data[i].food > 0) cout << "F"; else if (data[i].ants > 0) cout << "*"; else if (data[i].isWall) cout << "#"; else cout << " "; } cout << "\n" << endl; }