From 470d87c4899bae4197194859b87709262b0c66a4 Mon Sep 17 00:00:00 2001 From: Matei Date: Mon, 20 Jun 2005 03:18:51 +0000 Subject: [PATCH] Added simple elevation painter as well as array syntax for AndConstraint, RandomTerrain and the new MultiPainter. This was SVN commit r2407. --- .../data/mods/official/maps/random/test.js | 5 +- binaries/data/mods/official/maps/rmlibrary.js | 23 ++---- source/tools/rmgen/objparse.cpp | 73 +++++++++++-------- source/tools/rmgen/objparse.h | 11 ++- source/tools/rmgen/simpleconstraints.cpp | 18 ++++- source/tools/rmgen/simpleconstraints.h | 6 +- source/tools/rmgen/simplepainters.cpp | 33 +++++++++ source/tools/rmgen/simplepainters.h | 19 ++++- 8 files changed, 126 insertions(+), 62 deletions(-) diff --git a/binaries/data/mods/official/maps/random/test.js b/binaries/data/mods/official/maps/random/test.js index 8bef62270e..8d836c79c4 100644 --- a/binaries/data/mods/official/maps/random/test.js +++ b/binaries/data/mods/official/maps/random/test.js @@ -1,10 +1,11 @@ const SIZE = 64; -init(SIZE, new RandomTerrain("snow", "snow forest"), 0); +init(SIZE, ["snow", "snow forest"], 0); createMulti( new ClumpPlacer(20.0, 0.01, 0.01), - new LayeredPainter([1], ["snow grass 2", "snow grass 2|wrld_flora_pine"]), + [new LayeredPainter([1], ["snow grass 2", "snow grass 2|wrld_flora_pine"]), + new ElevationPainter(3.0)], new AvoidTextureConstraint("snow grass 2"), 150, 500 diff --git a/binaries/data/mods/official/maps/rmlibrary.js b/binaries/data/mods/official/maps/rmlibrary.js index 80007065d6..5df224918c 100644 --- a/binaries/data/mods/official/maps/rmlibrary.js +++ b/binaries/data/mods/official/maps/rmlibrary.js @@ -4,12 +4,11 @@ const TYPE_RECTPLACER = 1, TYPE_TERRAINPAINTER = 2, TYPE_NULLCONSTRAINT = 3, -TYPE_RANDOMTERRAIN = 4, -TYPE_LAYEREDPAINTER = 5, -TYPE_AVOIDAREACONSTRAINT = 6, -TYPE_CLUMPPLACER = 7, -TYPE_AVOIDTEXTURECONSTRAINT = 8, -TYPE_ANDCONSTRAINT = 9; +TYPE_LAYEREDPAINTER = 4, +TYPE_AVOIDAREACONSTRAINT = 5, +TYPE_CLUMPPLACER = 6, +TYPE_AVOIDTEXTURECONSTRAINT = 7, +TYPE_ELEVATIONPAINTER = 8; // Utility functions @@ -58,11 +57,6 @@ function NullConstraint() { this.TYPE = TYPE_NULLCONSTRAINT; } -function RandomTerrain() { - this.TYPE = TYPE_RANDOMTERRAIN; - this.terrains = argsToArray(arguments); -} - function LayeredPainter(widths, terrains) { this.TYPE = TYPE_LAYEREDPAINTER; this.widths = widths; @@ -88,10 +82,9 @@ function AvoidTextureConstraint(texture) { this.texture = texture; } -function AndConstraint(a, b) { - this.TYPE = TYPE_ANDCONSTRAINT; - this.a = a; - this.b = b; +function ElevationPainter(elevation) { + this.TYPE = TYPE_ELEVATIONPAINTER; + this.elevation = elevation; } function createMulti(centeredPlacer, painter, constraint, num, maxFail) { diff --git a/source/tools/rmgen/objparse.cpp b/source/tools/rmgen/objparse.cpp index 3f80555278..a222c2373f 100644 --- a/source/tools/rmgen/objparse.cpp +++ b/source/tools/rmgen/objparse.cpp @@ -79,12 +79,23 @@ bool ParseArray(JSContext* cx, jsval val, vector& ret) { } AreaPainter* ParsePainter(JSContext* cx, jsval val) { + vector array; jsval jsv, jsv2; Terrain* terrain = 0; - vector array; + float elevation; vector terrains; vector widths; + if(ParseArray(cx, val, array)) { + // MultiPainter is encoded as an array of painters + vector painters(array.size()); + for(int i=0; i array; int areaId; string texture; jsval jsv, jsv2; - Constraint* c1, *c2; + + if(JSVAL_IS_NULL(val)) { + // convenience way of specifying a NullConstraint + return new NullConstraint(); + } + + if(ParseArray(cx, val, array)) { + // AndConstraint is encoded as an array of constraints + vector constraints(array.size()); + for(int i=0; igetId(texture)); - case TYPE_ANDCONSTRAINT: - if(!GetJsvalField(cx, val, "a", jsv)) return 0; - if(!GetJsvalField(cx, val, "b", jsv2)) return 0; - if(!(c1 = ParseConstraint(cx, jsv))) return 0; - if(!(c2 = ParseConstraint(cx, jsv2))) return 0; - return new AndConstraint(c1, c2); - default: return 0; } } Terrain* ParseTerrain(JSContext* cx, jsval val) { + vector array; + if(JSVAL_IS_STRING(val)) { // simple terrains are just encoded as strings string str = JS_GetStringBytes(JS_ValueToString(cx, val)); return SimpleTerrain::parse(str); } - else { - // complex terrain type - Terrain* terrain = 0; - vector array; - vector terrains; - switch(GetType(cx, val)) { - case TYPE_RANDOMTERRAIN: - if(!GetArrayField(cx, val, "terrains", array)) return 0; - for(int i=0; i terrains(array.size()); + for(int i=0; ia = a; - this->b = b; +AndConstraint::AndConstraint(const vector& constraints) { + this->constraints = constraints; +} + +AndConstraint::~AndConstraint() { + for(int i=0; iallows(m,x,y) && b->allows(m,x,y); + for(int i=0; iallows(m, x, y)) { + return false; + } + } + return true; } \ No newline at end of file diff --git a/source/tools/rmgen/simpleconstraints.h b/source/tools/rmgen/simpleconstraints.h index 926de0d265..6f11bfca07 100644 --- a/source/tools/rmgen/simpleconstraints.h +++ b/source/tools/rmgen/simpleconstraints.h @@ -28,10 +28,10 @@ public: class AndConstraint : public Constraint { private: - Constraint* a; - Constraint* b; + std::vector constraints; public: - AndConstraint(Constraint* a, Constraint* b); + AndConstraint(const std::vector& constraints); + ~AndConstraint(); virtual bool allows(Map* m, int x, int y); }; diff --git a/source/tools/rmgen/simplepainters.cpp b/source/tools/rmgen/simplepainters.cpp index 539b9ec135..83e2e975a7 100644 --- a/source/tools/rmgen/simplepainters.cpp +++ b/source/tools/rmgen/simplepainters.cpp @@ -19,3 +19,36 @@ void TerrainPainter::paint(Map* m, Area* a) terrain->place(m, p.x, p.y); } } + +// ElevationPainter + +ElevationPainter::ElevationPainter(float elevation) +{ + this->elevation = elevation; +} + +void ElevationPainter::paint(Map* m, Area* a) +{ + for (int i=0; ipoints.size(); i++) { + Point p = a->points[i]; + static const int DX[4] = {0,1,1,0}; + static const int DY[4] = {0,0,1,1}; + for(int j=0; j<4; j++) { + m->height[p.x+DX[j]][p.y+DY[j]] = elevation; + } + } +} + +// MultiPainter + +MultiPainter::MultiPainter(const std::vector& painters) +{ + this->painters = painters; +} + +void MultiPainter::paint(Map* m, Area* a) +{ + for (int i=0; ipaint(m, a); + } +} \ No newline at end of file diff --git a/source/tools/rmgen/simplepainters.h b/source/tools/rmgen/simplepainters.h index 2c1a16739d..1abfa78a60 100644 --- a/source/tools/rmgen/simplepainters.h +++ b/source/tools/rmgen/simplepainters.h @@ -7,11 +7,24 @@ #include "terrain.h" class TerrainPainter : public AreaPainter { -public: Terrain* terrain; - +public: TerrainPainter(Terrain* terrain); - void paint(Map* m, Area* a); + virtual void paint(Map* m, Area* a); +}; + +class ElevationPainter : public AreaPainter { + float elevation; +public: + ElevationPainter(float elevation); + virtual void paint(Map* m, Area* a); +}; + +class MultiPainter : public AreaPainter { + std::vector painters; +public: + MultiPainter(const std::vector& painters); + virtual void paint(Map* m, Area* a); }; #endif \ No newline at end of file