Commit "Map"
This commit is contained in:
parent
80c385039e
commit
7a2bfae5ae
|
@ -0,0 +1,322 @@
|
|||
#include "HexMap.h"
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#define MAX_INT 2147483647
|
||||
|
||||
const int primes[20] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71};
|
||||
|
||||
void HexMap::initialize(string arquivo){
|
||||
cout << "Inicializando..." << endl;
|
||||
caracteres[0] = ' ';
|
||||
ambientes[0] = "Void";
|
||||
caracteres[95] = 168; // interrogacao invertida
|
||||
ambientes[95] = "Desconhecido";
|
||||
for (int i = 1; i < 95; i++){
|
||||
ambientes[i] = "\033[0;31;1mdados faltando\033[0m"; // TODO: adaptar para usar colormap ou descartar completamente
|
||||
caracteres[i] = i+32;
|
||||
}
|
||||
|
||||
ifstream file;
|
||||
file.open(arquivo);
|
||||
while(file.good()){
|
||||
char caractere;
|
||||
file >> caractere;
|
||||
int index = (int) caractere - 32;
|
||||
if (file.eof()) break;
|
||||
string nome;
|
||||
file >> nome;
|
||||
caracteres[index] = caractere;
|
||||
ambientes[index] = nome;
|
||||
// cout << caracteres[index] << " -> " << ambientes[index]<<endl; DEBUG
|
||||
}
|
||||
// cout << "Finalizado" << endl; DEBUG
|
||||
initialized = 1;
|
||||
}
|
||||
|
||||
HexMap::HexMap(int size) : size(size){
|
||||
this->mapa = new Tile**[2*size + 1];
|
||||
|
||||
for(int i = 0; i < size; i++){
|
||||
this->mapa[i] = new Tile*[size+1+i];
|
||||
}
|
||||
|
||||
this->mapa[size] = new Tile*[2*size + 1];
|
||||
|
||||
for(int i = 0; i < size; i++){
|
||||
this->mapa[2*size - i] = new Tile*[size+1+i];
|
||||
}
|
||||
// this->qtdTiles = 0;
|
||||
this->zeraVetores();
|
||||
|
||||
}
|
||||
|
||||
void HexMap::zeraVetores(){
|
||||
for(int i = 0; i < 2*(this->size)+1; i++){
|
||||
|
||||
int tamanho = (i < size) ? size+1+i : 2*size+1-(i-size);
|
||||
|
||||
for (int j = 0; j < tamanho; j++){
|
||||
mapa[i][j] = NULL;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
bool HexMap::inBounds(int r, int q){
|
||||
// inBounds = 1 se
|
||||
// r < 0, (-size + r) <= q <= size OU
|
||||
// r > 0, -size <= q <= size - r
|
||||
|
||||
return ((r < 0 && (q >= -(this->size +(r)) && q <= this->size)) || (r >= 0 && q >= -this->size && q <= this->size-r));
|
||||
}
|
||||
|
||||
bool HexMap::mark(int r, int q){
|
||||
if (!inBounds(r,q)) return 0;
|
||||
getTile(r,q)->alternaMarcado();
|
||||
return 1;
|
||||
}
|
||||
bool HexMap::reveal(int r, int q){
|
||||
if (!inBounds(r,q)) return 0;
|
||||
getTile(r,q)->alternaRevela();
|
||||
return 1;
|
||||
}
|
||||
|
||||
string HexMap::getAmb(int r, int q){
|
||||
cout << "Buscando Informacao..." << endl;
|
||||
|
||||
Tile* alvo = getTile(r,q);
|
||||
|
||||
if(alvo != NULL && initialized){
|
||||
|
||||
cout << "Informacao existe!" << endl;
|
||||
|
||||
if(alvo->isRevelado()){
|
||||
|
||||
cout << "Informacao disponivel!" << endl;
|
||||
|
||||
return ambientes[(int) alvo->getSimbolo() - 32];
|
||||
}
|
||||
|
||||
cout << "Informacao oculta..." << endl;
|
||||
|
||||
return ambientes[95];
|
||||
}
|
||||
|
||||
cout << "Informacao nao encontrada!!" << endl;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
string HexMap::getAmb(int index){
|
||||
return ambientes[index];
|
||||
}
|
||||
|
||||
|
||||
char HexMap::getChar(int index){
|
||||
return caracteres[index];
|
||||
}
|
||||
|
||||
|
||||
int* HexMap::coordToIndex(int r, int q){
|
||||
|
||||
if (!this->inBounds(r,q)) return NULL;
|
||||
|
||||
static int indexes[2];
|
||||
|
||||
indexes[0] = r + this->size;
|
||||
|
||||
indexes[1] = (r < 0) ? q + (this->size+r) : q+this->size;
|
||||
|
||||
return indexes;
|
||||
}
|
||||
|
||||
Tile* HexMap::getTile(int r, int q){
|
||||
if(!this->inBounds(r,q)) return NULL;
|
||||
|
||||
int* indexes = coordToIndex(r,q);
|
||||
|
||||
return mapa[indexes[0]][indexes[1]];
|
||||
}
|
||||
|
||||
void HexMap::insertTile(Tile* val, int r, int q){
|
||||
if(this->inBounds(r,q)){
|
||||
|
||||
int* indexes = coordToIndex(r,q);
|
||||
|
||||
mapa[indexes[0]][indexes[1]] = val;
|
||||
|
||||
mapa[indexes[0]][indexes[1]]->setCoords(r,q);
|
||||
|
||||
setAdjacencias(r,q);
|
||||
}
|
||||
}
|
||||
|
||||
void HexMap::setAdjacencias(int r, int q){
|
||||
|
||||
Tile* centro = getTile(r,q);
|
||||
|
||||
bool nBordaRneg = (r > -this->size); // nao esta na "borda" para R negativo
|
||||
bool nBordaRpos = (r < this->size); // nao esta na "borda" para R positivo
|
||||
bool nBordaQneg = (q > -this->size); // nao esta na "borda" para Q negativo
|
||||
bool nBordaQpos = (q < this->size); // nao esta na "borda" para Q positivo
|
||||
|
||||
// cout << "Centro: " << r << ',' << q << " -> "; DEBUG
|
||||
if(nBordaQneg) {
|
||||
|
||||
if (this->inBounds(r,q-1)) {
|
||||
// DEBUG
|
||||
/*{
|
||||
cout << "N";
|
||||
if(this->getTile(r,q-1) != NULL) cout << "y ";
|
||||
else cout << "n ";
|
||||
}*/
|
||||
centro->setAdjacencia(0, this->getTile(r,q-1));
|
||||
|
||||
|
||||
}
|
||||
} // else cout << "Q- "; DEBUG
|
||||
|
||||
if(nBordaQpos) {
|
||||
|
||||
if (this->inBounds(r,q+1)) {
|
||||
centro->setAdjacencia(3, this->getTile(r,q+1));
|
||||
// DEBUG
|
||||
/*{
|
||||
cout << "S";
|
||||
if(this->getTile(r,q+1) != NULL) cout << "y ";
|
||||
else cout << "n ";
|
||||
}*/
|
||||
}
|
||||
} // else cout << "Q+ "; DEBUG
|
||||
|
||||
if (nBordaRpos){
|
||||
|
||||
if(nBordaQneg){
|
||||
|
||||
if (this->inBounds(r+1,q-1)) {
|
||||
centro -> setAdjacencia(1, this->getTile(r+1,q-1));
|
||||
// DEBUG
|
||||
/*{
|
||||
cout << "NE";
|
||||
if(this->getTile(r+1,q-1) != NULL) cout << "y ";
|
||||
else cout << "n ";
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
if (this->inBounds(r+1,q)) {
|
||||
|
||||
centro->setAdjacencia(2, this->getTile(r+1,q));
|
||||
// DEBUG
|
||||
/*{
|
||||
cout << "SE";
|
||||
if(this->getTile(r+1,q) != NULL) cout << "y ";
|
||||
else cout << "n ";
|
||||
}*/
|
||||
}
|
||||
|
||||
} // else cout << "R+ "; DEBUG
|
||||
|
||||
if (nBordaRneg){
|
||||
|
||||
if (this->inBounds(r-1,q)) {
|
||||
|
||||
centro->setAdjacencia(5, this->getTile(r-1,q));
|
||||
// DEBUG
|
||||
/*{
|
||||
cout << "NW";
|
||||
if(this->getTile(r-1,q) != NULL) cout << "y ";
|
||||
else cout << "n ";
|
||||
}*/
|
||||
}
|
||||
|
||||
if (this->inBounds(r-1,q+1)) {
|
||||
|
||||
if(nBordaQpos) centro->setAdjacencia(4, this->getTile(r-1,q+1));
|
||||
// DEBUG
|
||||
/*{
|
||||
cout << "SW";
|
||||
if(this->getTile(r-1,q+1) != NULL) cout << "y ";
|
||||
else cout << "n ";
|
||||
}*/
|
||||
}
|
||||
} // else cout << "R- "; DEBUG
|
||||
// cout << endl; DEBUG
|
||||
}
|
||||
|
||||
void HexMap::printMap(){
|
||||
|
||||
cout << setw(4) << " R |";
|
||||
|
||||
for(int i = -size; i <= size; i++){
|
||||
cout << setw(2) << i << " ";
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
cout << " Q +________________________________" << endl;
|
||||
|
||||
for(int r = (this->size); r >= -(this->size); r--){
|
||||
|
||||
cout << setw(4) << r << "| ";
|
||||
|
||||
for(int q = -(this->size); q <= this->size; q++){
|
||||
|
||||
if (q == -size) for(int i = 0; i < r && i < size; i++) cout << " ";
|
||||
|
||||
if((this->inBounds(r,q))){
|
||||
|
||||
this->getTile(r,q)->showStatus();
|
||||
|
||||
cout << " ";
|
||||
} else cout <<" ";
|
||||
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
void HexMap::pingAdj(int r, int q){
|
||||
|
||||
this->getTile(r,q)->alternaRevela();
|
||||
|
||||
for(int i = 0; i < 6; i++){
|
||||
|
||||
if (this->getTile(r,q)->getAdjacente(i) != NULL){
|
||||
|
||||
this->getTile(r,q)->getAdjacente(i)->alternaMarcado();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HexMap::drawLine (int dir, int rStart, int qStart){
|
||||
|
||||
Tile* startTile = this->getTile(rStart,qStart);
|
||||
|
||||
// Explicitando o ponto de inicio
|
||||
this->getTile(rStart,qStart)->alternaRevela(); this->getTile(rStart,qStart)->alternaMarcado();
|
||||
|
||||
Tile* prevTile = startTile;
|
||||
Tile* nextTile = startTile->getAdjacente(dir);
|
||||
|
||||
while(nextTile != NULL){
|
||||
|
||||
nextTile->alternaMarcado();
|
||||
prevTile = nextTile;
|
||||
nextTile = nextTile->getAdjacente(dir);
|
||||
}
|
||||
// Explicitando o ponto de fim
|
||||
prevTile->alternaMarcado(); prevTile->alternaRevela();
|
||||
}
|
||||
|
||||
HexMap::~HexMap(){
|
||||
for(int i = 0; i < 2*this->size + 1; i++){
|
||||
int tamanho = (i < size) ? size+1+i : 2*size+1-(i-size);
|
||||
for (int j = 0; j < tamanho; j++){
|
||||
delete mapa[i][j];
|
||||
}
|
||||
delete mapa[i];
|
||||
}
|
||||
delete mapa;
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
#ifndef HEXMAP_H
|
||||
#define HEXMAP_H
|
||||
|
||||
#include "..\Tiles\Tile.h"
|
||||
|
||||
// Nomes dos ambientes
|
||||
static string ambientes[96];
|
||||
// Caracteres de representacao
|
||||
static char caracteres[96];
|
||||
static bool initialized = 0;
|
||||
|
||||
// Classe que implementa o mapa de hexagonos
|
||||
// Mapa Hexagonal com coordenadas axiais/trapezoidais
|
||||
// referencia aa dimensao r.
|
||||
class HexMap {
|
||||
public:
|
||||
|
||||
HexMap(int size);
|
||||
// Inicializa os vetores de caracteres e ambientes
|
||||
static void initialize(string file);
|
||||
// Retorna caracteres[index]
|
||||
static char getChar (int index);
|
||||
// Insere "val" na posicao [r,q]
|
||||
void insertTile(Tile* val, int r, int q);
|
||||
|
||||
// Retorna o ambiente na posicao [r,q]
|
||||
string getAmb(int r, int q);
|
||||
// Retorna ambientes[index]
|
||||
string getAmb(int index);
|
||||
|
||||
// Retorna a Tile na posicao [r,q]
|
||||
Tile* getTile(int r, int q);
|
||||
// Imprime o mapa
|
||||
void printMap();
|
||||
// Alterna o atributo "revelado" na Tile em [r,q]
|
||||
bool reveal(int r, int q);
|
||||
// Alterna o atributo "marcado" na Tile em [r,q]
|
||||
bool mark(int r, int q);
|
||||
|
||||
// Ferramentas de debug por enquanto:
|
||||
|
||||
// "Desenha" uma linha partindo de [rS,qS] na direcao
|
||||
void drawLine (int dir, int rS, int qS);
|
||||
// NAO IMPLEMENTADO
|
||||
// "Desenha" uma linha entre [rS,qS] e [rF,qF]
|
||||
void drawLine (int rStart, int qStart, int rFinish, int qFinish);
|
||||
|
||||
// Mostra as Tiles adjacentes aa Tile em [r,q]
|
||||
void pingAdj (int r, int q);
|
||||
|
||||
// Converte coordenadas [r,q] para indices [i,j] na matriz "mapa"
|
||||
int* coordToIndex(int r, int q);
|
||||
|
||||
// Checa se a Tile em [r,q] eh uma Tile valida
|
||||
bool inBounds(int r, int q);
|
||||
~HexMap();
|
||||
private:
|
||||
// atualiza as adjacencias da tile r,q
|
||||
void setAdjacencias(int r, int q);
|
||||
|
||||
Tile*** mapa;
|
||||
// Garante que mapa inicia apontando para Tile* = NULL
|
||||
void zeraVetores();
|
||||
|
||||
int size;
|
||||
|
||||
|
||||
};
|
||||
#endif
|
Loading…
Reference in New Issue