map now supports the team information, coord is now in a separate header

This commit is contained in:
Anselme 2016-05-17 17:15:07 +02:00
parent 7c30b16357
commit b40adcc3db
9 changed files with 109 additions and 91 deletions

View File

@ -1,25 +1,10 @@
#include <map.h> #include <map.h>
#include <cmath> #include <cmath>
#include <cstdlib> #include <cstdlib>
#include <algorithm>
// g++ -shared anselme.cpp -o anselme.dll -I../src // g++ -shared anselme.cpp -o anselme.dll -I../src
// functions
int distance_manhattan(int x1,int y1, int x2, int y2)
{
return abs(x1-x2) + abs(y1-y2);
}
inline int custom_min(int a, int b)
{
return a < b ? a : b;
}
inline int custom_max(int a, int b)
{
return a > b ? a : b;
}
extern "C" void generate(Map *mapPtr) extern "C" void generate(Map *mapPtr)
{ {
Map &map = *mapPtr; Map &map = *mapPtr;
@ -30,21 +15,18 @@ extern "C" void generate(Map *mapPtr)
int r = (w/n < h ? w/n : h)/2; int r = (w/n < h ? w/n : h)/2;
int *teamCoord = new int[n*2]; int *teamCoord = new int[n*2];
for(i=0; i<n; i++){ for(i=0; i<n; i++)
teamCoord[i*2] = (w/(n*2))*(1+i*2); map.team(i) = Coord((w/(n*2))*(1+i*2), h/2);
teamCoord[i*2 +1] = h/2;
}
//génération de la carte //génération de la carte
for (i=0;i<w;i++){ for (i=0;i<w;i++){
for(j=0;j<h;j++){ for(j=0;j<h;j++){
//map[i][j].data = NULL; if (i == 0 || j == 0 || i == w-1 || j == h-1 || n == 0){
if (i == 0 || j == 0 || i == w-1 || j == h-1){
map[i][j].type = Pixel::BEDROCK; map[i][j].type = Pixel::BEDROCK;
}else{ }else{
int d = custom_max(w, h); int d = std::max(w, h);
for(k=0; k<n; k++){ for(k=0; k<n; k++){
d = custom_min(d, distance_manhattan(teamCoord[k*2], teamCoord[k*2 +1], i, j)); d = std::min(d, map.team(k).dist(i, j));
if(!d) if(!d)
break; break;
} }

Binary file not shown.

Binary file not shown.

68
src/coord.h Normal file
View File

@ -0,0 +1,68 @@
#ifndef COORD_H
#define COORD_H
#include <cmath>
enum Dir {
NORTH,
EAST,
SOUTH,
WEST,
NO_DIR = -1
};
struct Coord
{
int x;
int y;
Coord(const Coord &c) :
x(c.x), y(c.y) {}
Coord(int x, int y) :
x(x), y(y) {}
Coord(int val) :
x(val), y(val) {}
Coord(Dir d = NO_DIR)
{
switch(d)
{
case NORTH : x= 0; y=-1; break;
case SOUTH : x= 0; y= 1; break;
case WEST : x=-1; y= 0; break;
case EAST : x= 1; y= 0; break;
default : x= 0; y= 0; break;
}
}
Coord operator+(const Coord &c) const
{ return Coord(x+c.x, y+c.y);}
Coord& operator+=(const Coord &c)
{ x+=c.x; y+=c.y; return *this; }
Coord operator+(Dir d) const
{ return *this + Coord(d); }
Coord& operator+=(Dir d)
{ return *this += Coord(d); }
Coord operator-(const Coord &c) const
{ return Coord(x-c.x, y-c.y); }
Coord& operator-=(const Coord &c)
{ x-=c.x; y-=c.y; return *this; }
Coord operator*(const Coord &c) const
{ return Coord(x*c.x, y*c.y); }
Coord& operator*=(const Coord &c)
{ x*=c.x; y*=c.y; return *this; }
Coord operator/(const Coord &c) const
{ return Coord(x/c.x, y/c.y); }
Coord& operator/=(const Coord &c)
{ x/=c.x; y/=c.y; return *this; }
int dist() const // manhatthan distance
{ return abs(x) + abs(y); }
int operator~() const
{ return dist(); }
static dist(const Coord &c1, const Coord &c2)
{ return ~(c1-c2); }
int dist(const Coord &c) const
{ return dist(*this, c); }
int dist(int x, int y) const
{ return dist(*this, Coord(x, y)); }
};
#endif // COORD_H

View File

@ -8,14 +8,16 @@ Map::Map(int nbTeams, int width, int height) :
{ {
if(m_height == 0) if(m_height == 0)
m_height = m_width/2; m_height = m_width/2;
map = new Pixel*[m_width]; m_map = new Pixel[m_width*m_height];
Pixel *ptr = new Pixel[m_width*m_height]; if(m_nbTeams)
for(int i=0; i<m_width; ++i) m_teams = new Coord[m_nbTeams];
map[i] = ptr + i*m_height; else
m_teams = NULL;
} }
Map::~Map() Map::~Map()
{ {
delete map[0]; delete m_map;
delete map; if(m_nbTeams)
delete m_teams;
} }

View File

@ -1,6 +1,8 @@
#ifndef MAP_H #ifndef MAP_H
#define MAP_H #define MAP_H
#include "coord.h"
struct Pixel struct Pixel
{ {
enum Type { enum Type {
@ -23,7 +25,9 @@ struct Pixel
class Map class Map
{ {
Pixel **map; Pixel *m_map;
Coord *m_teams;
int m_width; int m_width;
int m_height; int m_height;
int m_nbTeams; int m_nbTeams;
@ -36,12 +40,32 @@ public:
int getHeight() const { return m_height; } int getHeight() const { return m_height; }
int getNbTeams() const { return m_nbTeams; } int getNbTeams() const { return m_nbTeams; }
/**
* Teams accessers :
*/
Coord & team(int i)
{ return m_teams[i]; }
/**
* Pixel accessers :
*/
Pixel &getPixel(int x, int y)
{ return m_map[m_height*x + y]; }
Pixel &getPixel(const Coord &c)
{ return m_map[m_height*c.x + c.y]; }
/** /**
* @brief operator [] allows to access a pixel of the map with the following syntax : * @brief operator [] allows to access a pixel of the map with the following syntax :
* Pixel &p = map[x][y]; * Pixel &p = map[x][y];
* or
* Pixel &p = map[Coord(x, y)];
*/ */
Pixel* operator[](int i) Pixel* operator[](int i)
{ return map[i]; } { return m_map + m_height*i; }
Pixel &operator[](const Coord &c)
{ return m_map[m_height*c.x + c.y]; }
}; };
typedef void (*GenerateFunction)(Map *map); typedef void (*GenerateFunction)(Map *map);

View File

@ -1,70 +1,12 @@
#ifndef BEHAVIOR_H #ifndef BEHAVIOR_H
#define BEHAVIOR_H #define BEHAVIOR_H
#include <cmath> #include "coord.h"
#define DUDE_MEMORY_SIZE 128 #define DUDE_MEMORY_SIZE 128
#define LIBRARY_SIZE 128 #define LIBRARY_SIZE 128
#define COM_SIZE 32 #define COM_SIZE 32
enum Dir {
NORTH,
EAST,
SOUTH,
WEST,
NO_DIR = -1
};
struct Coord
{
int x;
int y;
Coord(const Coord &c) :
x(c.x), y(c.y) {}
Coord(int x, int y) :
x(x), y(y) {}
Coord(Dir d = NO_DIR)
{
switch(d)
{
case NORTH : x= 0; y=-1; break;
case SOUTH : x= 0; y= 1; break;
case WEST : x=-1; y= 0; break;
case EAST : x= 1; y= 0; break;
default : x= 0; y= 0; break;
}
}
Coord operator+(const Coord &c) const
{ return Coord(x+c.x, y+c.y);}
Coord& operator+=(const Coord &c)
{ x+=c.x; y+=c.y; return *this; }
Coord operator+(Dir d) const
{ return *this + Coord(d); }
Coord& operator+=(Dir d)
{ return *this += Coord(d); }
Coord operator-(const Coord &c) const
{ return Coord(x-c.x, y-c.y); }
Coord& operator-=(const Coord &c)
{ x-=c.x; y-=c.y; return *this; }
Coord operator*(const Coord &c) const
{ return Coord(x*c.x, y*c.y); }
Coord& operator*=(const Coord &c)
{ x*=c.x; y*=c.y; return *this; }
Coord operator/(const Coord &c) const
{ return Coord(x/c.x, y/c.y); }
Coord& operator/=(const Coord &c)
{ x/=c.x; y/=c.y; return *this; }
int dist() const // manhatthan distance
{ return abs(x) + abs(y); }
int operator~() const
{ return dist(); }
static dist(const Coord &c1, const Coord &c2)
{ return ~(c1-c2); }
int dist(const Coord &c) const
{ return dist(*this, c); }
};
struct Com struct Com
{ {
enum Flags { enum Flags {

Binary file not shown.

Binary file not shown.