forked from 0ad/0ad
Added get/setTerrain and get/setHeight functions to the RMS API which allow control of each individual tile, and base height argument to init().
This was SVN commit r2300.
This commit is contained in:
parent
c8dc599ab8
commit
b772559fbe
@ -1,13 +1,3 @@
|
||||
?new Projectile( "props/weapon/weap_arrow_front.xml", Vector3D( this.position.x, this.position.y + 10.5, this.position.z ), target, 12 );
|
||||
?new Projectile( "props/weapon/weap_arrow_front.xml", Vector3D( this.position.x, this.position.y + 10.5, this.position.z ), target, 12 );
|
||||
?new Projectile( "props/weapon/weap_arrow_front.xml", Vector3D( this.position.x, this.position.y + 10.5, this.position.z ), target, 12 );
|
||||
?new Projectile( "props/weapon/weap_arrow_front.xml", Vector3D( this.position.x, this.position.y + 10.5, this.position.z ), target, 12 );
|
||||
?new Projectile( "props/weapon/weap_arrow_front.xml", Vector3D( this.position.x, this.position.y + 10.5, this.position.z ), target, 12 );
|
||||
?new Projectile( "props/weapon/weap_arrow_front.xml", Vector3D( this.position.x, this.position.y + 10.5, this.position.z ), target, 12 );
|
||||
?new Projectile( "props/weapon/weap_arrow_front.xml", Vector3D( this.position.x, this.position.y + 10.5, this.position.z ), target, 12 );
|
||||
?new Projectile( "props/weapon/weap_arrow_front.xml", Vector3D( this.position.x, this.position.y + 10.5, this.position.z ), target, 12 );
|
||||
?new Projectile( "props/weapon/weap_arrow_front.xml", Vector3D( this.position.x, this.position.y + 10.5, this.position.z ), target, 12, this );
|
||||
?new Projectile( "props/weapon/weap_arrow_front.xml", Vector3D( this.position.x, this.position.y + 10, this.position.z ), target, 12, this );
|
||||
?new Projectile( "props/weapon/weap_arrow_front.xml", Vector3D( this.position.x, this.position.y + 5, this.position.z ), target, 12, this );
|
||||
?new Projectile( "props/weapon/weap_arrow_front.xml", Vector3D( this.position.x, this.position.y + 2.5, this.position.z ), target, 12, this );
|
||||
?new Projectile( "props/weapon/weap_arrow_front.xml", Vector3D( this.position.x, this.position.y + 2.5, this.position.z ), target, 12, this );
|
||||
@ -48,3 +38,13 @@
|
||||
?entities.subset( function() { return( this.traits.health.cur < this.traits.health.max ); } );
|
||||
?entities.subset( function() { return( this.traits.health.curr < this.traits.health.max ); } );
|
||||
?selection = entities.subset( function() { return( this.traits.health.curr < this.traits.health.max ); } );
|
||||
?1+1
|
||||
?new Entity("wrld_flora_oak", new Vector3D(1, 1, 1))
|
||||
?new Entity("wrld_flora_oak", new Vector3D(1, 3, 1))
|
||||
?new Entity("wrld_flora_oak", new Vector3D(1, 30, 1))
|
||||
?new Entity("wrld_flora_oak", new Vector3D(3, 0, 1))
|
||||
?new Entity("wrld_flora_oak", new Vector3D(8, 0, 1))
|
||||
?new Entity("wrld_flora_oak", new Vector3D(8, 0, 8))
|
||||
quit
|
||||
quit()
|
||||
exit
|
||||
|
@ -4,11 +4,11 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
Map::Map(int size, string baseTerrain) {
|
||||
Map::Map(int size, const string& baseTerrain, float baseHeight) {
|
||||
if(size<0 || size>1024) {
|
||||
JS_ReportError(cx, "init: map size out of range");
|
||||
}
|
||||
if(size%16 != 0) {
|
||||
else if(size%16 != 0) {
|
||||
JS_ReportError(cx, "init: map size must be divisble by 16");
|
||||
}
|
||||
|
||||
@ -16,13 +16,21 @@ Map::Map(int size, string baseTerrain) {
|
||||
|
||||
int baseId = getId(baseTerrain);
|
||||
|
||||
terrain = new TerrainId*[size];
|
||||
terrain = new int*[size];
|
||||
for(int i=0; i<size; i++) {
|
||||
terrain[i] = new TerrainId[size];
|
||||
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() {
|
||||
@ -30,9 +38,14 @@ Map::~Map() {
|
||||
delete[] terrain[i];
|
||||
}
|
||||
delete[] terrain;
|
||||
|
||||
for(int i=0; i<size+1; i++) {
|
||||
delete[] height[i];
|
||||
}
|
||||
delete[] height;
|
||||
}
|
||||
|
||||
TerrainId Map::getId(string terrain) {
|
||||
int Map::getId(string terrain) {
|
||||
if(nameToId.find(terrain) != nameToId.end()) {
|
||||
return nameToId[terrain];
|
||||
}
|
||||
@ -43,3 +56,31 @@ TerrainId Map::getId(string terrain) {
|
||||
return newId;
|
||||
}
|
||||
}
|
||||
|
||||
bool Map::validT(int x, int y) {
|
||||
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;
|
||||
}
|
||||
|
||||
string Map::getTerrain(int x, int 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);
|
||||
}
|
||||
|
||||
float Map::getHeight(int x, int 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;
|
||||
}
|
@ -1,19 +1,27 @@
|
||||
#ifndef __MAP_H__
|
||||
#define __MAP_H__
|
||||
|
||||
typedef int TerrainId;
|
||||
|
||||
class Map {
|
||||
public:
|
||||
int size;
|
||||
TerrainId** terrain;
|
||||
int** terrain;
|
||||
float** height;
|
||||
std::map<std::string, int> nameToId;
|
||||
std::map<int, std::string> idToName;
|
||||
|
||||
Map(int size, std::string baseTerrain);
|
||||
Map(int size, const std::string& baseTerrain, float baseHeight);
|
||||
~Map();
|
||||
|
||||
TerrainId getId(std::string terrain);
|
||||
int getId(std::string terrain);
|
||||
|
||||
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);
|
||||
|
||||
float getHeight(int x, int y);
|
||||
void setHeight(int x, int y, float height);
|
||||
};
|
||||
|
||||
#endif
|
@ -64,17 +64,7 @@ struct PMP {
|
||||
u32 version = 4;
|
||||
fwrite(&version, sizeof(u32), 1, f);
|
||||
|
||||
// data size
|
||||
/*int numTextureChars = 0;
|
||||
for(int i=0; i<numTerrains; i++) {
|
||||
numTextureChars += (m->idToName[i]+".dds").length();
|
||||
}
|
||||
u32 dataSize = sizeof(u32) // size in patches
|
||||
+ (size+1)*(size+1)*sizeof(u16) // heightmap
|
||||
+ sizeof(u32) // num textures
|
||||
+ m->idToName.size()*sizeof(u32) // texture string lengths
|
||||
+ numTextureChars*sizeof(char) // texture string chars
|
||||
+ size*size*sizeof(Tile); // tiles*/
|
||||
// data size (write 0 for now, calculate it at the end)
|
||||
int temp = 0;
|
||||
fwrite(&temp, sizeof(u32), 1, f);
|
||||
|
||||
@ -84,7 +74,18 @@ struct PMP {
|
||||
|
||||
// heightmap
|
||||
u16* heightmap = new u16[(size+1)*(size+1)];
|
||||
memset(heightmap, 0, (size+1)*(size+1)*sizeof(u16));
|
||||
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
|
||||
@ -98,19 +99,21 @@ struct PMP {
|
||||
fwrite(fname.c_str(), sizeof(char), fname.length(), f);
|
||||
}
|
||||
|
||||
// terrains
|
||||
// terrain; note that this is an array of 16x16 patches for some reason
|
||||
Tile* tiles = new Tile[size*size];
|
||||
for(int i=0; i<size; i++) {
|
||||
for(int j=0; j<size; j++) {
|
||||
Tile& t = tiles[i*size+j];
|
||||
t.texture1 = m->terrain[i][j];
|
||||
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
|
||||
// data size (file size - 12)
|
||||
fseek(f, 0, SEEK_END);
|
||||
int fsize = ftell(f);
|
||||
u32 dataSize = fsize-12;
|
||||
@ -118,7 +121,7 @@ struct PMP {
|
||||
fwrite(&dataSize, sizeof(u32), 1, f);
|
||||
}
|
||||
|
||||
void OutputMap(Map* m, string outputName) {
|
||||
void OutputMap(Map* m, const string& outputName) {
|
||||
string xmlName = outputName + ".xml";
|
||||
FILE* xmlFile = fopen(xmlName.c_str(), "w");
|
||||
if(!xmlFile) {
|
||||
|
@ -3,6 +3,6 @@
|
||||
|
||||
#include "map.h"
|
||||
|
||||
void OutputMap(Map* map, std::string path);
|
||||
void OutputMap(Map* map, const std::string& path);
|
||||
|
||||
#endif
|
@ -11,12 +11,7 @@ JSObject *global = 0;
|
||||
|
||||
Map* theMap = 0;
|
||||
|
||||
JSFunctionSpec globalFunctions[] = {
|
||||
/* name native args */
|
||||
{"print", print, 1},
|
||||
{"init", init, 3},
|
||||
{0}
|
||||
};
|
||||
// JS support functions
|
||||
|
||||
void ErrorReporter(JSContext *cx, const char *message, JSErrorReport *report) {
|
||||
cerr << "Error at " << report->filename << ":" << report->lineno << ":\n\t"
|
||||
@ -24,6 +19,17 @@ void ErrorReporter(JSContext *cx, const char *message, JSErrorReport *report) {
|
||||
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}
|
||||
};
|
||||
|
||||
void InitJS() {
|
||||
rt = JS_NewRuntime(8L * 1024L * 1024L);
|
||||
cx = JS_NewContext(rt, 8192);
|
||||
@ -53,11 +59,18 @@ char* ValToString(jsval 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()));
|
||||
}
|
||||
|
||||
// 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);
|
||||
return JS_FALSE;
|
||||
}
|
||||
|
||||
cout << JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
|
||||
@ -66,8 +79,8 @@ JSBool print(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
||||
|
||||
JSBool init(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
||||
{
|
||||
if(argc != 2) {
|
||||
JS_ReportError(cx, "init: expected 2 arguments but got %d", argc);
|
||||
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");
|
||||
@ -75,17 +88,118 @@ JSBool init(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
||||
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);
|
||||
|
||||
theMap = new Map(size, baseTerrain);
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
// Program entry point
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
InitJS();
|
||||
@ -107,8 +221,8 @@ int main(int argc, char* argv[])
|
||||
}
|
||||
|
||||
jsval rval;
|
||||
JS_EvaluateScript(cx, global, script.c_str(), script.length(),
|
||||
argv[1], 1, &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;
|
||||
@ -120,4 +234,3 @@ int main(int argc, char* argv[])
|
||||
|
||||
Shutdown(0);
|
||||
}
|
||||
|
||||
|
@ -5,11 +5,17 @@ extern JSRuntime *rt;
|
||||
extern JSContext *cx;
|
||||
extern JSObject *global;
|
||||
|
||||
// Utility functions
|
||||
void Shutdown(int status);
|
||||
|
||||
char* ValToString(jsval val);
|
||||
jsval NewJSString(const std::string& str);
|
||||
|
||||
JSBool print(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
|
||||
// JS API implementation
|
||||
JSBool init(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
|
||||
JSBool print(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
|
||||
JSBool getTerrain(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
|
||||
JSBool setTerrain(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
|
||||
JSBool getHeight(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
|
||||
JSBool setHeight(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
|
||||
|
||||
#endif
|
@ -1,3 +1,14 @@
|
||||
print("Hello world!\n");
|
||||
|
||||
init(128, "grass1_a");
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
print(getHeight(0,0) + "\n");
|
Loading…
Reference in New Issue
Block a user