Replaced tabs with 4 spaces each to be consisent.
This was SVN commit r2301.
This commit is contained in:
parent
b772559fbe
commit
08ada296f8
@ -5,82 +5,82 @@
|
||||
using namespace std;
|
||||
|
||||
Map::Map(int size, const string& baseTerrain, float baseHeight) {
|
||||
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 divisble by 16");
|
||||
}
|
||||
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 divisble by 16");
|
||||
}
|
||||
|
||||
this->size = size;
|
||||
this->size = size;
|
||||
|
||||
int baseId = getId(baseTerrain);
|
||||
int baseId = getId(baseTerrain);
|
||||
|
||||
terrain = new int*[size];
|
||||
for(int i=0; i<size; i++) {
|
||||
terrain[i] = new int[size];
|
||||
for(int j=0; j<size; j++) {
|
||||
terrain[i][j] = baseId;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
terrain = new int*[size];
|
||||
for(int i=0; i<size; i++) {
|
||||
terrain[i] = new int[size];
|
||||
for(int j=0; j<size; j++) {
|
||||
terrain[i][j] = baseId;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Map::~Map() {
|
||||
for(int i=0; i<size; i++) {
|
||||
delete[] terrain[i];
|
||||
}
|
||||
delete[] terrain;
|
||||
for(int i=0; i<size; i++) {
|
||||
delete[] terrain[i];
|
||||
}
|
||||
delete[] terrain;
|
||||
|
||||
for(int i=0; i<size+1; i++) {
|
||||
delete[] height[i];
|
||||
}
|
||||
delete[] height;
|
||||
for(int i=0; i<size+1; i++) {
|
||||
delete[] height[i];
|
||||
}
|
||||
delete[] height;
|
||||
}
|
||||
|
||||
int Map::getId(string terrain) {
|
||||
if(nameToId.find(terrain) != nameToId.end()) {
|
||||
return nameToId[terrain];
|
||||
}
|
||||
else {
|
||||
int newId = nameToId.size();
|
||||
nameToId[terrain] = newId;
|
||||
idToName[newId] = terrain;
|
||||
return newId;
|
||||
}
|
||||
if(nameToId.find(terrain) != nameToId.end()) {
|
||||
return nameToId[terrain];
|
||||
}
|
||||
else {
|
||||
int newId = nameToId.size();
|
||||
nameToId[terrain] = newId;
|
||||
idToName[newId] = terrain;
|
||||
return newId;
|
||||
}
|
||||
}
|
||||
|
||||
bool Map::validT(int x, int y) {
|
||||
return x>=0 && y>=0 && x<size && y<size;
|
||||
return x>=0 && y>=0 && x<size && y<size;
|
||||
}
|
||||
|
||||
bool Map::validH(int x, int y) {
|
||||
return x>=0 && y>=0 && x<size+1 && y<size+1;
|
||||
return x>=0 && y>=0 && x<size+1 && y<size+1;
|
||||
}
|
||||
|
||||
string Map::getTerrain(int x, int y) {
|
||||
if(!validT(x,y)) JS_ReportError(cx, "getTerrain: invalid tile position");
|
||||
else return idToName[terrain[x][y]];
|
||||
if(!validT(x,y)) JS_ReportError(cx, "getTerrain: invalid tile position");
|
||||
else return idToName[terrain[x][y]];
|
||||
}
|
||||
|
||||
void Map::setTerrain(int x, int y, const string& t) {
|
||||
if(!validT(x,y)) JS_ReportError(cx, "setTerrain: invalid tile position");
|
||||
else terrain[x][y] = getId(t);
|
||||
if(!validT(x,y)) JS_ReportError(cx, "setTerrain: invalid tile position");
|
||||
else terrain[x][y] = getId(t);
|
||||
}
|
||||
|
||||
float Map::getHeight(int x, int y) {
|
||||
if(!validH(x,y)) JS_ReportError(cx, "getHeight: invalid point position");
|
||||
else return height[x][y];
|
||||
if(!validH(x,y)) JS_ReportError(cx, "getHeight: invalid point position");
|
||||
else return height[x][y];
|
||||
}
|
||||
|
||||
void Map::setHeight(int x, int y, float h) {
|
||||
if(!validH(x,y)) JS_ReportError(cx, "setHeight: invalid point position");
|
||||
else height[x][y] = h;
|
||||
if(!validH(x,y)) JS_ReportError(cx, "setHeight: invalid point position");
|
||||
else height[x][y] = h;
|
||||
}
|
@ -3,25 +3,25 @@
|
||||
|
||||
class Map {
|
||||
public:
|
||||
int size;
|
||||
int** terrain;
|
||||
float** height;
|
||||
std::map<std::string, int> nameToId;
|
||||
std::map<int, std::string> idToName;
|
||||
int size;
|
||||
int** terrain;
|
||||
float** height;
|
||||
std::map<std::string, int> nameToId;
|
||||
std::map<int, std::string> idToName;
|
||||
|
||||
Map(int size, const std::string& baseTerrain, float baseHeight);
|
||||
~Map();
|
||||
Map(int size, const std::string& baseTerrain, float baseHeight);
|
||||
~Map();
|
||||
|
||||
int getId(std::string terrain);
|
||||
int getId(std::string terrain);
|
||||
|
||||
bool validT(int x, int y);
|
||||
bool validH(int x, int y);
|
||||
bool validT(int x, int y);
|
||||
bool validH(int x, int y);
|
||||
|
||||
std::string getTerrain(int x, int y);
|
||||
void setTerrain(int x, int y, const std::string& terrain);
|
||||
std::string getTerrain(int x, int y);
|
||||
void setTerrain(int x, int y, const std::string& terrain);
|
||||
|
||||
float getHeight(int x, int y);
|
||||
void setHeight(int x, int y, float height);
|
||||
float getHeight(int x, int y);
|
||||
void setHeight(int x, int y, float height);
|
||||
};
|
||||
|
||||
#endif
|
@ -10,27 +10,27 @@ typedef unsigned int u32;
|
||||
|
||||
|
||||
void OutputXml(Map* m, FILE* f) {
|
||||
const char* xml = "\
|
||||
const char* xml = "\
|
||||
<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n\
|
||||
<!DOCTYPE Scenario SYSTEM \"/maps/scenario_v4.dtd\">\n\
|
||||
<Scenario>\n\
|
||||
<Environment>\n\
|
||||
<SunColour r=\"1\" g=\"1\" b=\"1\" />\n\
|
||||
<SunElevation angle=\"0.785398\" />\n\
|
||||
<SunRotation angle=\"4.712389\" />\n\
|
||||
<TerrainAmbientColour r=\"0\" g=\"0\" b=\"0\" />\n\
|
||||
<UnitsAmbientColour r=\"0.4\" g=\"0.4\" b=\"0.4\" />\n\
|
||||
</Environment>\n\
|
||||
<Entities />\n\
|
||||
<Nonentities />\n\
|
||||
<Environment>\n\
|
||||
<SunColour r=\"1\" g=\"1\" b=\"1\" />\n\
|
||||
<SunElevation angle=\"0.785398\" />\n\
|
||||
<SunRotation angle=\"4.712389\" />\n\
|
||||
<TerrainAmbientColour r=\"0\" g=\"0\" b=\"0\" />\n\
|
||||
<UnitsAmbientColour r=\"0.4\" g=\"0.4\" b=\"0.4\" />\n\
|
||||
</Environment>\n\
|
||||
<Entities />\n\
|
||||
<Nonentities />\n\
|
||||
</Scenario>\n";
|
||||
fprintf(f, "%s", xml);
|
||||
fprintf(f, "%s", xml);
|
||||
}
|
||||
|
||||
struct Tile {
|
||||
u16 texture1; // index into terrain_textures[]
|
||||
u16 texture2; // index, or 0xFFFF for 'none'
|
||||
u32 priority; // ???
|
||||
u16 texture1; // index into terrain_textures[]
|
||||
u16 texture2; // index, or 0xFFFF for 'none'
|
||||
u32 priority; // ???
|
||||
};
|
||||
|
||||
void OutputPmp(Map* m, FILE* f) {
|
||||
@ -54,91 +54,91 @@ struct PMP {
|
||||
|
||||
Tile tiles[(mapsize*16)^2];
|
||||
};*/
|
||||
int size = m->size;
|
||||
int numTerrains = m->idToName.size();
|
||||
int size = m->size;
|
||||
int numTerrains = m->idToName.size();
|
||||
|
||||
// header
|
||||
fwrite("PSMP", sizeof(char), 4, f);
|
||||
// header
|
||||
fwrite("PSMP", sizeof(char), 4, f);
|
||||
|
||||
// file format version
|
||||
u32 version = 4;
|
||||
fwrite(&version, sizeof(u32), 1, f);
|
||||
// file format version
|
||||
u32 version = 4;
|
||||
fwrite(&version, sizeof(u32), 1, f);
|
||||
|
||||
// data size (write 0 for now, calculate it at the end)
|
||||
int temp = 0;
|
||||
fwrite(&temp, sizeof(u32), 1, f);
|
||||
// data size (write 0 for now, calculate it at the end)
|
||||
int temp = 0;
|
||||
fwrite(&temp, sizeof(u32), 1, f);
|
||||
|
||||
// map size in patches
|
||||
u32 sizeInPatches = size/16;
|
||||
fwrite(&sizeInPatches, sizeof(u32), 1, f);
|
||||
// map size in patches
|
||||
u32 sizeInPatches = size/16;
|
||||
fwrite(&sizeInPatches, sizeof(u32), 1, f);
|
||||
|
||||
// heightmap
|
||||
u16* heightmap = new u16[(size+1)*(size+1)];
|
||||
for(int x=0; x<size+1; x++) {
|
||||
for(int y=0; y<size+1; y++) {
|
||||
int intHeight = (int) (m->height[x][y] * 256.0f / 0.35f);
|
||||
if(intHeight > 0xFFFF) {
|
||||
intHeight = 0xFFFF;
|
||||
}
|
||||
else if(intHeight < 0) {
|
||||
intHeight = 0;
|
||||
}
|
||||
heightmap[y*(size+1)+x] = intHeight;
|
||||
}
|
||||
}
|
||||
fwrite(heightmap, sizeof(u16), (size+1)*(size+1), f);
|
||||
// heightmap
|
||||
u16* heightmap = new u16[(size+1)*(size+1)];
|
||||
for(int x=0; x<size+1; x++) {
|
||||
for(int y=0; y<size+1; y++) {
|
||||
int intHeight = (int) (m->height[x][y] * 256.0f / 0.35f);
|
||||
if(intHeight > 0xFFFF) {
|
||||
intHeight = 0xFFFF;
|
||||
}
|
||||
else if(intHeight < 0) {
|
||||
intHeight = 0;
|
||||
}
|
||||
heightmap[y*(size+1)+x] = intHeight;
|
||||
}
|
||||
}
|
||||
fwrite(heightmap, sizeof(u16), (size+1)*(size+1), f);
|
||||
|
||||
// num terrain textures
|
||||
fwrite(&numTerrains, sizeof(u32), 1, f);
|
||||
// num terrain textures
|
||||
fwrite(&numTerrains, sizeof(u32), 1, f);
|
||||
|
||||
// terrain names
|
||||
for(int i=0; i<numTerrains; i++) {
|
||||
string fname = m->idToName[i] + ".dds";
|
||||
int len = fname.length();
|
||||
fwrite(&len, sizeof(u32), 1, f);
|
||||
fwrite(fname.c_str(), sizeof(char), fname.length(), f);
|
||||
}
|
||||
// terrain names
|
||||
for(int i=0; i<numTerrains; i++) {
|
||||
string fname = m->idToName[i] + ".dds";
|
||||
int len = fname.length();
|
||||
fwrite(&len, sizeof(u32), 1, f);
|
||||
fwrite(fname.c_str(), sizeof(char), fname.length(), f);
|
||||
}
|
||||
|
||||
// terrain; note that this is an array of 16x16 patches for some reason
|
||||
Tile* tiles = new Tile[size*size];
|
||||
for(int x=0; x<size; x++) {
|
||||
for(int y=0; y<size; y++) {
|
||||
int patchX = x/16, patchY = y/16;
|
||||
int offX = x%16, offY = y%16;
|
||||
Tile& t = tiles[ (patchY*size/16 + patchX)*16*16 + (offY*16 + offX) ];
|
||||
t.texture1 = m->terrain[x][y];
|
||||
t.texture2 = 0xFFFF;
|
||||
t.priority = 0;
|
||||
}
|
||||
}
|
||||
fwrite(tiles, sizeof(Tile), size*size, f);
|
||||
// terrain; note that this is an array of 16x16 patches for some reason
|
||||
Tile* tiles = new Tile[size*size];
|
||||
for(int x=0; x<size; x++) {
|
||||
for(int y=0; y<size; y++) {
|
||||
int patchX = x/16, patchY = y/16;
|
||||
int offX = x%16, offY = y%16;
|
||||
Tile& t = tiles[ (patchY*size/16 + patchX)*16*16 + (offY*16 + offX) ];
|
||||
t.texture1 = m->terrain[x][y];
|
||||
t.texture2 = 0xFFFF;
|
||||
t.priority = 0;
|
||||
}
|
||||
}
|
||||
fwrite(tiles, sizeof(Tile), size*size, f);
|
||||
|
||||
// data size (file size - 12)
|
||||
fseek(f, 0, SEEK_END);
|
||||
int fsize = ftell(f);
|
||||
u32 dataSize = fsize-12;
|
||||
fseek(f, 8, SEEK_SET);
|
||||
fwrite(&dataSize, sizeof(u32), 1, f);
|
||||
// data size (file size - 12)
|
||||
fseek(f, 0, SEEK_END);
|
||||
int fsize = ftell(f);
|
||||
u32 dataSize = fsize-12;
|
||||
fseek(f, 8, SEEK_SET);
|
||||
fwrite(&dataSize, sizeof(u32), 1, f);
|
||||
}
|
||||
|
||||
void OutputMap(Map* m, const string& outputName) {
|
||||
string xmlName = outputName + ".xml";
|
||||
FILE* xmlFile = fopen(xmlName.c_str(), "w");
|
||||
if(!xmlFile) {
|
||||
cerr << "Cannot open " << xmlName << endl;
|
||||
Shutdown(1);
|
||||
}
|
||||
OutputXml(m, xmlFile);
|
||||
fclose(xmlFile);
|
||||
string xmlName = outputName + ".xml";
|
||||
FILE* xmlFile = fopen(xmlName.c_str(), "w");
|
||||
if(!xmlFile) {
|
||||
cerr << "Cannot open " << xmlName << endl;
|
||||
Shutdown(1);
|
||||
}
|
||||
OutputXml(m, xmlFile);
|
||||
fclose(xmlFile);
|
||||
|
||||
string pmpName = outputName + ".pmp";
|
||||
FILE* pmpFile = fopen(pmpName.c_str(), "wb");
|
||||
if(!pmpFile) {
|
||||
cerr << "Cannot open " << xmlName << endl;
|
||||
Shutdown(1);
|
||||
}
|
||||
OutputPmp(m, pmpFile);
|
||||
fclose(pmpFile);
|
||||
string pmpName = outputName + ".pmp";
|
||||
FILE* pmpFile = fopen(pmpName.c_str(), "wb");
|
||||
if(!pmpFile) {
|
||||
cerr << "Cannot open " << xmlName << endl;
|
||||
Shutdown(1);
|
||||
}
|
||||
OutputPmp(m, pmpFile);
|
||||
fclose(pmpFile);
|
||||
|
||||
cout << "Map outputted to " << outputName << ".*" << endl;
|
||||
cout << "Map outputted to " << outputName << ".*" << endl;
|
||||
}
|
||||
|
@ -14,223 +14,223 @@ Map* theMap = 0;
|
||||
// JS support functions
|
||||
|
||||
void ErrorReporter(JSContext *cx, const char *message, JSErrorReport *report) {
|
||||
cerr << "Error at " << report->filename << ":" << report->lineno << ":\n\t"
|
||||
<< message << endl;
|
||||
Shutdown(1);
|
||||
cerr << "Error at " << report->filename << ":" << report->lineno << ":\n\t"
|
||||
<< message << endl;
|
||||
Shutdown(1);
|
||||
}
|
||||
|
||||
JSFunctionSpec globalFunctions[] = {
|
||||
// {name, native, args}
|
||||
{"init", init, 3},
|
||||
{"print", print, 1},
|
||||
{"getTerrain", getTerrain, 2},
|
||||
{"setTerrain", setTerrain, 3},
|
||||
{"getHeight", getHeight, 2},
|
||||
{"setHeight", setHeight, 3},
|
||||
{0}
|
||||
{"init", init, 3},
|
||||
{"print", print, 1},
|
||||
{"getTerrain", getTerrain, 2},
|
||||
{"setTerrain", setTerrain, 3},
|
||||
{"getHeight", getHeight, 2},
|
||||
{"setHeight", setHeight, 3},
|
||||
{0}
|
||||
};
|
||||
|
||||
void InitJS() {
|
||||
rt = JS_NewRuntime(8L * 1024L * 1024L);
|
||||
cx = JS_NewContext(rt, 8192);
|
||||
|
||||
JS_SetErrorReporter(cx, ErrorReporter);
|
||||
|
||||
static JSClass globalClass = {
|
||||
"global",0,
|
||||
JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,
|
||||
JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub
|
||||
};
|
||||
JS_SetErrorReporter(cx, ErrorReporter);
|
||||
|
||||
static JSClass globalClass = {
|
||||
"global",0,
|
||||
JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,
|
||||
JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub
|
||||
};
|
||||
global = JS_NewObject(cx, &globalClass, NULL, NULL);
|
||||
JS_InitStandardClasses(cx, global);
|
||||
|
||||
JS_DefineFunctions(cx, global, globalFunctions);
|
||||
JS_DefineFunctions(cx, global, globalFunctions);
|
||||
}
|
||||
|
||||
void Shutdown(int status) {
|
||||
JS_DestroyContext(cx);
|
||||
JS_DestroyRuntime(rt);
|
||||
JS_ShutDown();
|
||||
system("pause");
|
||||
exit(status);
|
||||
JS_DestroyContext(cx);
|
||||
JS_DestroyRuntime(rt);
|
||||
JS_ShutDown();
|
||||
system("pause");
|
||||
exit(status);
|
||||
}
|
||||
|
||||
char* ValToString(jsval val) {
|
||||
return JS_GetStringBytes(JS_ValueToString(cx, val));
|
||||
return JS_GetStringBytes(JS_ValueToString(cx, val));
|
||||
}
|
||||
|
||||
jsval NewJSString(const string& str) {
|
||||
char* buf = (char*) JS_malloc(cx, str.length());
|
||||
memcpy(buf, str.c_str(), str.length());
|
||||
return STRING_TO_JSVAL(JS_NewString(cx, buf, str.length()));
|
||||
char* buf = (char*) JS_malloc(cx, str.length());
|
||||
memcpy(buf, str.c_str(), str.length());
|
||||
return STRING_TO_JSVAL(JS_NewString(cx, buf, str.length()));
|
||||
}
|
||||
|
||||
// JS API implementation
|
||||
|
||||
JSBool print(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
||||
{
|
||||
if(argc != 1) {
|
||||
JS_ReportError(cx, "print: expected 1 argument but got %d", argc);
|
||||
}
|
||||
|
||||
cout << JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
|
||||
return JS_TRUE;
|
||||
if(argc != 1) {
|
||||
JS_ReportError(cx, "print: expected 1 argument but got %d", argc);
|
||||
}
|
||||
|
||||
cout << JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
|
||||
return JS_TRUE;
|
||||
}
|
||||
|
||||
JSBool init(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
||||
{
|
||||
if(argc != 3) {
|
||||
JS_ReportError(cx, "init: expected 3 arguments but got %d", argc);
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[0])) {
|
||||
JS_ReportError(cx, "init: first argument must be an integer");
|
||||
}
|
||||
if(!JSVAL_IS_STRING(argv[1])) {
|
||||
JS_ReportError(cx, "init: second argument must be a string");
|
||||
}
|
||||
if(!JSVAL_IS_NUMBER(argv[2])) {
|
||||
JS_ReportError(cx, "init: third argument must be a number");
|
||||
}
|
||||
if(theMap != 0) {
|
||||
JS_ReportError(cx, "init: cannot be called twice");
|
||||
}
|
||||
if(argc != 3) {
|
||||
JS_ReportError(cx, "init: expected 3 arguments but got %d", argc);
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[0])) {
|
||||
JS_ReportError(cx, "init: first argument must be an integer");
|
||||
}
|
||||
if(!JSVAL_IS_STRING(argv[1])) {
|
||||
JS_ReportError(cx, "init: second argument must be a string");
|
||||
}
|
||||
if(!JSVAL_IS_NUMBER(argv[2])) {
|
||||
JS_ReportError(cx, "init: third argument must be a number");
|
||||
}
|
||||
if(theMap != 0) {
|
||||
JS_ReportError(cx, "init: cannot be called twice");
|
||||
}
|
||||
|
||||
int size = JSVAL_TO_INT(argv[0]);
|
||||
char* baseTerrain = JS_GetStringBytes(JSVAL_TO_STRING(argv[1]));
|
||||
jsdouble baseHeight;
|
||||
JS_ValueToNumber(cx, argv[2], &baseHeight);
|
||||
int size = JSVAL_TO_INT(argv[0]);
|
||||
char* baseTerrain = JS_GetStringBytes(JSVAL_TO_STRING(argv[1]));
|
||||
jsdouble baseHeight;
|
||||
JS_ValueToNumber(cx, argv[2], &baseHeight);
|
||||
|
||||
theMap = new Map(size, baseTerrain, (float) baseHeight);
|
||||
return JS_TRUE;
|
||||
theMap = new Map(size, baseTerrain, (float) baseHeight);
|
||||
return JS_TRUE;
|
||||
}
|
||||
|
||||
JSBool getTerrain(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
||||
{
|
||||
if(argc != 2) {
|
||||
JS_ReportError(cx, "getTerrain: expected 2 arguments but got %d", argc);
|
||||
}
|
||||
if(theMap == 0) {
|
||||
JS_ReportError(cx, "getTerrain: cannot be called before init()");
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[0])) {
|
||||
JS_ReportError(cx, "getTerrain: first argument must be an integer");
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[1])) {
|
||||
JS_ReportError(cx, "getTerrain: second argument must be an integer");
|
||||
}
|
||||
|
||||
int x = JSVAL_TO_INT(argv[0]);
|
||||
int y = JSVAL_TO_INT(argv[1]);
|
||||
string terrain = theMap->getTerrain(x, y);
|
||||
*rval = NewJSString(terrain);
|
||||
return JS_TRUE;
|
||||
if(argc != 2) {
|
||||
JS_ReportError(cx, "getTerrain: expected 2 arguments but got %d", argc);
|
||||
}
|
||||
if(theMap == 0) {
|
||||
JS_ReportError(cx, "getTerrain: cannot be called before init()");
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[0])) {
|
||||
JS_ReportError(cx, "getTerrain: first argument must be an integer");
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[1])) {
|
||||
JS_ReportError(cx, "getTerrain: second argument must be an integer");
|
||||
}
|
||||
|
||||
int x = JSVAL_TO_INT(argv[0]);
|
||||
int y = JSVAL_TO_INT(argv[1]);
|
||||
string terrain = theMap->getTerrain(x, y);
|
||||
*rval = NewJSString(terrain);
|
||||
return JS_TRUE;
|
||||
}
|
||||
|
||||
JSBool setTerrain(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
||||
{
|
||||
if(argc != 3) {
|
||||
JS_ReportError(cx, "setTerrain: expected 3 arguments but got %d", argc);
|
||||
}
|
||||
if(theMap == 0) {
|
||||
JS_ReportError(cx, "setTerrain: cannot be called before init()");
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[0])) {
|
||||
JS_ReportError(cx, "setTerrain: first argument must be an integer");
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[1])) {
|
||||
JS_ReportError(cx, "setTerrain: second argument must be an integer");
|
||||
}
|
||||
if(!JSVAL_IS_STRING(argv[2])) {
|
||||
JS_ReportError(cx, "setTerrain: third argument must be a string");
|
||||
}
|
||||
|
||||
int x = JSVAL_TO_INT(argv[0]);
|
||||
int y = JSVAL_TO_INT(argv[1]);
|
||||
char* terrain = JS_GetStringBytes(JSVAL_TO_STRING(argv[2]));
|
||||
theMap->setTerrain(x, y, terrain);
|
||||
return JS_TRUE;
|
||||
if(argc != 3) {
|
||||
JS_ReportError(cx, "setTerrain: expected 3 arguments but got %d", argc);
|
||||
}
|
||||
if(theMap == 0) {
|
||||
JS_ReportError(cx, "setTerrain: cannot be called before init()");
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[0])) {
|
||||
JS_ReportError(cx, "setTerrain: first argument must be an integer");
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[1])) {
|
||||
JS_ReportError(cx, "setTerrain: second argument must be an integer");
|
||||
}
|
||||
if(!JSVAL_IS_STRING(argv[2])) {
|
||||
JS_ReportError(cx, "setTerrain: third argument must be a string");
|
||||
}
|
||||
|
||||
int x = JSVAL_TO_INT(argv[0]);
|
||||
int y = JSVAL_TO_INT(argv[1]);
|
||||
char* terrain = JS_GetStringBytes(JSVAL_TO_STRING(argv[2]));
|
||||
theMap->setTerrain(x, y, terrain);
|
||||
return JS_TRUE;
|
||||
}
|
||||
|
||||
JSBool getHeight(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
||||
{
|
||||
if(argc != 2) {
|
||||
JS_ReportError(cx, "getHeight: expected 2 arguments but got %d", argc);
|
||||
}
|
||||
if(theMap == 0) {
|
||||
JS_ReportError(cx, "getHeight: cannot be called before init()");
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[0])) {
|
||||
JS_ReportError(cx, "getHeight: first argument must be an integer");
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[1])) {
|
||||
JS_ReportError(cx, "getHeight: second argument must be an integer");
|
||||
}
|
||||
|
||||
int x = JSVAL_TO_INT(argv[0]);
|
||||
int y = JSVAL_TO_INT(argv[1]);
|
||||
jsdouble height = theMap->getHeight(x, y);
|
||||
JS_NewDoubleValue(cx, height, rval);
|
||||
return JS_TRUE;
|
||||
if(argc != 2) {
|
||||
JS_ReportError(cx, "getHeight: expected 2 arguments but got %d", argc);
|
||||
}
|
||||
if(theMap == 0) {
|
||||
JS_ReportError(cx, "getHeight: cannot be called before init()");
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[0])) {
|
||||
JS_ReportError(cx, "getHeight: first argument must be an integer");
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[1])) {
|
||||
JS_ReportError(cx, "getHeight: second argument must be an integer");
|
||||
}
|
||||
|
||||
int x = JSVAL_TO_INT(argv[0]);
|
||||
int y = JSVAL_TO_INT(argv[1]);
|
||||
jsdouble height = theMap->getHeight(x, y);
|
||||
JS_NewDoubleValue(cx, height, rval);
|
||||
return JS_TRUE;
|
||||
}
|
||||
|
||||
JSBool setHeight(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
||||
{
|
||||
if(argc != 3) {
|
||||
JS_ReportError(cx, "setHeight: expected 3 arguments but got %d", argc);
|
||||
}
|
||||
if(theMap == 0) {
|
||||
JS_ReportError(cx, "setHeight: cannot be called before init()");
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[0])) {
|
||||
JS_ReportError(cx, "setHeight: first argument must be an integer");
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[1])) {
|
||||
JS_ReportError(cx, "setHeight: second argument must be an integer");
|
||||
}
|
||||
if(!JSVAL_IS_NUMBER(argv[2])) {
|
||||
JS_ReportError(cx, "setHeight: third argument must be a number");
|
||||
}
|
||||
|
||||
int x = JSVAL_TO_INT(argv[0]);
|
||||
int y = JSVAL_TO_INT(argv[1]);
|
||||
jsdouble height;
|
||||
JS_ValueToNumber(cx, argv[2], &height);
|
||||
theMap->setHeight(x, y, (float) height);
|
||||
return JS_TRUE;
|
||||
if(argc != 3) {
|
||||
JS_ReportError(cx, "setHeight: expected 3 arguments but got %d", argc);
|
||||
}
|
||||
if(theMap == 0) {
|
||||
JS_ReportError(cx, "setHeight: cannot be called before init()");
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[0])) {
|
||||
JS_ReportError(cx, "setHeight: first argument must be an integer");
|
||||
}
|
||||
if(!JSVAL_IS_INT(argv[1])) {
|
||||
JS_ReportError(cx, "setHeight: second argument must be an integer");
|
||||
}
|
||||
if(!JSVAL_IS_NUMBER(argv[2])) {
|
||||
JS_ReportError(cx, "setHeight: third argument must be a number");
|
||||
}
|
||||
|
||||
int x = JSVAL_TO_INT(argv[0]);
|
||||
int y = JSVAL_TO_INT(argv[1]);
|
||||
jsdouble height;
|
||||
JS_ValueToNumber(cx, argv[2], &height);
|
||||
theMap->setHeight(x, y, (float) height);
|
||||
return JS_TRUE;
|
||||
}
|
||||
// Program entry point
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
InitJS();
|
||||
InitJS();
|
||||
|
||||
if(argc!=3) {
|
||||
cerr << "Usage: rmgen <script> <output name without extension>" << endl;
|
||||
Shutdown(1);
|
||||
}
|
||||
if(argc!=3) {
|
||||
cerr << "Usage: rmgen <script> <output name without extension>" << endl;
|
||||
Shutdown(1);
|
||||
}
|
||||
|
||||
FILE* scriptFile = fopen(argv[1], "r");
|
||||
if(!scriptFile) {
|
||||
cerr << "Cannot open " << scriptFile << endl;
|
||||
Shutdown(1);
|
||||
}
|
||||
string script;
|
||||
char buf[1025];
|
||||
while(fgets(buf, 1024, scriptFile)) {
|
||||
script += buf;
|
||||
}
|
||||
FILE* scriptFile = fopen(argv[1], "r");
|
||||
if(!scriptFile) {
|
||||
cerr << "Cannot open " << scriptFile << endl;
|
||||
Shutdown(1);
|
||||
}
|
||||
string script;
|
||||
char buf[1025];
|
||||
while(fgets(buf, 1024, scriptFile)) {
|
||||
script += buf;
|
||||
}
|
||||
|
||||
jsval rval;
|
||||
JSBool ok = JS_EvaluateScript(cx, global, script.c_str(), script.length(), argv[1], 1, &rval);
|
||||
if(!ok) Shutdown(1);
|
||||
jsval rval;
|
||||
JSBool ok = JS_EvaluateScript(cx, global, script.c_str(), script.length(), argv[1], 1, &rval);
|
||||
if(!ok) Shutdown(1);
|
||||
|
||||
if(!theMap) {
|
||||
cerr << "Error:\n\tScript never called init!" << endl;
|
||||
Shutdown(1);
|
||||
}
|
||||
if(!theMap) {
|
||||
cerr << "Error:\n\tScript never called init!" << endl;
|
||||
Shutdown(1);
|
||||
}
|
||||
|
||||
string outputName = argv[2];
|
||||
OutputMap(theMap, outputName);
|
||||
string outputName = argv[2];
|
||||
OutputMap(theMap, outputName);
|
||||
|
||||
Shutdown(0);
|
||||
Shutdown(0);
|
||||
}
|
||||
|
@ -5,10 +5,10 @@ init(32, "grass1_a", 1.5);
|
||||
print(getHeight(0,0) + "\n");
|
||||
|
||||
for(x=0; x<3; x++) {
|
||||
for(y=0; y<20; y++) {
|
||||
setTerrain(x, y, "dirta");
|
||||
setHeight(x, y, 4.5);
|
||||
}
|
||||
for(y=0; y<20; y++) {
|
||||
setTerrain(x, y, "dirta");
|
||||
setHeight(x, y, 4.5);
|
||||
}
|
||||
}
|
||||
|
||||
print(getHeight(0,0) + "\n");
|
||||
print(getHeight(0,0) + "\n");
|
||||
|
Loading…
Reference in New Issue
Block a user