2005-05-12 07:16:59 +02:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "rmgen.h"
|
|
|
|
#include "map.h"
|
2005-05-23 04:52:37 +02:00
|
|
|
#include "entity.h"
|
2005-05-12 07:16:59 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2005-06-06 09:46:28 +02:00
|
|
|
Map::Map(int size, Terrain* baseTerrain, float baseHeight) {
|
2005-05-23 05:51:37 +02:00
|
|
|
if(size<0 || size>1024) {
|
|
|
|
JS_ReportError(cx, "init: map size out of range");
|
|
|
|
}
|
|
|
|
else if(size%16 != 0) {
|
|
|
|
JS_ReportError(cx, "init: map size must be divisible by 16");
|
|
|
|
}
|
|
|
|
|
|
|
|
this->size = size;
|
|
|
|
|
2005-06-06 09:46:28 +02:00
|
|
|
texture = new int*[size];
|
|
|
|
for(int i=0; i<size; i++) {
|
|
|
|
texture[i] = new int[size];
|
|
|
|
}
|
2005-05-23 05:51:37 +02:00
|
|
|
|
2005-06-06 09:46:28 +02:00
|
|
|
terrainEntities = new vector<Entity*>*[size];
|
2005-05-23 05:51:37 +02:00
|
|
|
for(int i=0; i<size; i++) {
|
2005-06-06 09:46:28 +02:00
|
|
|
terrainEntities[i] = new vector<Entity*>[size];
|
2005-05-23 05:51:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
area = new Area**[size];
|
|
|
|
for(int i=0; i<size; i++) {
|
|
|
|
area[i] = new Area*[size];
|
|
|
|
for(int j=0; j<size; j++) {
|
|
|
|
area[i][j] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
height = new float*[size+1];
|
|
|
|
for(int i=0; i<size+1; i++) {
|
|
|
|
height[i] = new float[size+1];
|
|
|
|
for(int j=0; j<size+1; j++) {
|
|
|
|
height[i][j] = baseHeight;
|
|
|
|
}
|
|
|
|
}
|
2005-06-06 09:46:28 +02:00
|
|
|
|
|
|
|
for(int i=0; i<size; i++) {
|
|
|
|
for(int j=0; j<size; j++) {
|
|
|
|
baseTerrain->place(this, i, j);
|
|
|
|
}
|
|
|
|
}
|
2005-05-12 07:16:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Map::~Map() {
|
2005-05-23 05:51:37 +02:00
|
|
|
for(int i=0; i<size; i++) {
|
2005-06-06 09:46:28 +02:00
|
|
|
delete[] texture[i];
|
2005-05-23 05:51:37 +02:00
|
|
|
}
|
2005-06-06 09:46:28 +02:00
|
|
|
delete[] texture;
|
2005-05-23 05:51:37 +02:00
|
|
|
|
|
|
|
for(int i=0; i<size; i++) {
|
2005-06-06 09:46:28 +02:00
|
|
|
delete[] terrainEntities[i];
|
2005-05-23 05:51:37 +02:00
|
|
|
}
|
2005-06-06 09:46:28 +02:00
|
|
|
delete[] terrainEntities;
|
2005-05-23 05:51:37 +02:00
|
|
|
|
|
|
|
for(int i=0; i<size+1; i++) {
|
|
|
|
delete[] height[i];
|
|
|
|
}
|
|
|
|
delete[] height;
|
|
|
|
|
2005-06-06 09:46:28 +02:00
|
|
|
for(int i=0; i<size; i++) {
|
|
|
|
delete[] area[i];
|
|
|
|
}
|
|
|
|
delete[] area;
|
|
|
|
|
2005-05-23 05:51:37 +02:00
|
|
|
for(int i=0; i<entities.size(); i++) {
|
|
|
|
delete entities[i];
|
|
|
|
}
|
2005-05-12 07:16:59 +02:00
|
|
|
}
|
|
|
|
|
2005-06-06 09:46:28 +02:00
|
|
|
int Map::getId(string texture) {
|
|
|
|
if(nameToId.find(texture) != nameToId.end()) {
|
|
|
|
return nameToId[texture];
|
2005-05-23 05:51:37 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int newId = nameToId.size();
|
2005-06-06 09:46:28 +02:00
|
|
|
nameToId[texture] = newId;
|
|
|
|
idToName[newId] = texture;
|
2005-05-23 05:51:37 +02:00
|
|
|
return newId;
|
|
|
|
}
|
2005-05-13 02:39:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Map::validT(int x, int y) {
|
2005-05-23 05:51:37 +02:00
|
|
|
return x>=0 && y>=0 && x<size && y<size;
|
2005-05-13 02:39:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Map::validH(int x, int y) {
|
2005-05-23 05:51:37 +02:00
|
|
|
return x>=0 && y>=0 && x<size+1 && y<size+1;
|
2005-05-13 02:39:13 +02:00
|
|
|
}
|
|
|
|
|
2005-06-06 09:46:28 +02:00
|
|
|
string Map::getTexture(int x, int y) {
|
|
|
|
if(!validT(x,y)) JS_ReportError(cx, "getTexture: invalid tile position");
|
|
|
|
return idToName[texture[x][y]];
|
2005-05-13 02:39:13 +02:00
|
|
|
}
|
|
|
|
|
2005-06-06 09:46:28 +02:00
|
|
|
void Map::setTexture(int x, int y, const string& t) {
|
|
|
|
if(!validT(x,y)) JS_ReportError(cx, "setTexture: invalid tile position");
|
|
|
|
texture[x][y] = getId(t);
|
2005-05-13 02:39:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
float Map::getHeight(int x, int y) {
|
2005-05-23 05:51:37 +02:00
|
|
|
if(!validH(x,y)) JS_ReportError(cx, "getHeight: invalid point position");
|
|
|
|
return height[x][y];
|
2005-05-13 02:39:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Map::setHeight(int x, int y, float h) {
|
2005-05-23 05:51:37 +02:00
|
|
|
if(!validH(x,y)) JS_ReportError(cx, "setHeight: invalid point position");
|
|
|
|
height[x][y] = h;
|
2005-05-23 04:52:37 +02:00
|
|
|
}
|
|
|
|
|
2005-06-06 09:46:28 +02:00
|
|
|
void Map::placeTerrain(int x, int y, Terrain* t) {
|
|
|
|
t->place(this, x, y);
|
|
|
|
}
|
|
|
|
|
2005-05-23 04:52:37 +02:00
|
|
|
void Map::addEntity(Entity* ent) {
|
2005-05-23 05:51:37 +02:00
|
|
|
entities.push_back(ent);
|
2005-05-23 04:52:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Area* Map::createArea(AreaPlacer* placer, AreaPainter* painter, Constraint* constr) {
|
2005-05-23 05:51:37 +02:00
|
|
|
vector<Point> points;
|
|
|
|
if(!placer->place(this, constr, points)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Area* a = new Area(points);
|
|
|
|
for(int i=0; i<points.size(); i++) {
|
|
|
|
area[points[i].x][points[i].y] = a;
|
|
|
|
}
|
|
|
|
painter->paint(this, a);
|
2005-06-06 09:46:28 +02:00
|
|
|
areas.push_back(a);
|
2005-05-23 05:51:37 +02:00
|
|
|
return a;
|
2005-05-12 07:16:59 +02:00
|
|
|
}
|