diff --git a/binaries/data/mods/public/maps/random/rmgen/group.js b/binaries/data/mods/public/maps/random/rmgen/group.js new file mode 100644 index 0000000000..13688d9676 --- /dev/null +++ b/binaries/data/mods/public/maps/random/rmgen/group.js @@ -0,0 +1,73 @@ +/** + * A Group tests if a set of entities specified in the constructor can be placed and + * potentially places some of them (typically all or none). + * + * The location is defined by the x and z property of the Group instance and can be modified externally. + * The Group is free to determine whether, where exactly and how many entities to place. + * + * The Constraint to test against and the future owner of the entities are passed by the caller. + * Typically Groups are called from createObjectGroup with the location set in the constructor or + * from createObjectGroups that randomizes the x and z property of the Group before calling place. + */ + +/** + * Places all of the given Objects. + * + * @param objects - An array of Objects, for instance SimpleObjects. + * @param avoidSelf - Objects will not overlap. + * @param tileClass - Optional TileClass that tiles with placed entities are marked with. + * @param x, z - The location the group is placed around. Can be omitted if the x and z properties are set externally. + */ +function SimpleGroup(objects, avoidSelf = false, tileClass = undefined, x = -1, z = -1) +{ + this.objects = objects; + this.tileClass = tileClass; + this.avoidSelf = avoidSelf; + this.x = x; + this.z = z; +} + +SimpleGroup.prototype.place = function(player, constraint) +{ + let resultObjs = []; + + // Test if the Objects can be placed at the given location + // Place none of them if one can't be placed. + for (let object of this.objects) + { + let objs = object.place(this.x, this.z, player, this.avoidSelf, constraint); + + if (objs === undefined) + return undefined; + + resultObjs = resultObjs.concat(objs); + } + + // Add all objects to the map + for (let obj of resultObjs) + { + let x = obj.position.x / CELL_SIZE; + let z = obj.position.z / CELL_SIZE; + + if (g_Map.validT(x, z)) + g_Map.addObject(obj); + + if (this.tileClass !== undefined) + getTileClass(this.tileClass).add(Math.floor(x), Math.floor(z)); + } + + return resultObjs; +}; + +/** + * Randomly choses one of the given Objects and places it just like the SimpleGroup. + */ +function RandomGroup(objects, avoidSelf = false, tileClass = undefined, x = -1, z = -1) +{ + this.simpleGroup = new SimpleGroup([pickRandom(objects)], avoidSelf, tileClass, x, z); +} + +RandomGroup.prototype.place = function(player, constraint) +{ + return this.simpleGroup.place(player, constraint); +}; diff --git a/binaries/data/mods/public/maps/random/rmgen/placer.js b/binaries/data/mods/public/maps/random/rmgen/placer.js index ee92123599..c478e7f0d4 100644 --- a/binaries/data/mods/public/maps/random/rmgen/placer.js +++ b/binaries/data/mods/public/maps/random/rmgen/placer.js @@ -483,93 +483,3 @@ RandomObject.prototype.place = function(cx, cz, player, avoidSelf, constraint, m return resultObjs; }; - -///////////////////////////////////////////////////////////////////////////////////////// -// SimpleGroup -// -// Class for placing groups of different objects -// -// elements: Array of SimpleObjects -// avoidSelf: Objects will not overlap -// tileClass: Optional tile class to add with these objects -// x,z: Tile coordinates of center of placer -// -///////////////////////////////////////////////////////////////////////////////////////// - -function SimpleGroup(elements, avoidSelf, tileClass, x, z) -{ - this.elements = elements; - this.tileClass = tileClass !== undefined ? getTileClass(tileClass) : undefined; - this.avoidSelf = avoidSelf !== undefined ? avoidSelf : false; - this.x = x !== undefined ? x : -1; - this.z = z !== undefined ? z : -1; -} - -SimpleGroup.prototype.place = function(player, constraint) -{ - var resultObjs = []; - - // Try placement of objects - for (let element of this.elements) - { - var objs = element.place(this.x, this.z, player, this.avoidSelf, constraint); - - if (objs === undefined) - return undefined; - - resultObjs = resultObjs.concat(objs); - } - - // Add placed objects to map - for (let obj of resultObjs) - { - if (g_Map.validT(obj.position.x / CELL_SIZE, obj.position.z / CELL_SIZE)) - g_Map.addObject(obj); - - // Convert position to integer number of tiles - if (this.tileClass !== undefined) - this.tileClass.add(Math.floor(obj.position.x/CELL_SIZE), Math.floor(obj.position.z/CELL_SIZE)); - } - - return resultObjs; -}; - -///////////////////////////////////////////////////////////////////////////////////////// -// RandomGroup -// -// Class for placing group of a random simple object -// -// elements: Array of SimpleObjects -// avoidSelf: Objects will not overlap -// tileClass: Optional tile class to add with these objects -// x,z: Tile coordinates of center of placer -// -///////////////////////////////////////////////////////////////////////////////////////// - -function RandomGroup(elements, avoidSelf, tileClass, x, z) -{ - this.elements = elements; - this.tileClass = tileClass !== undefined ? getTileClass(tileClass) : undefined; - this.avoidSelf = avoidSelf !== undefined ? avoidSelf : false; - this.x = x !== undefined ? x : -1; - this.z = z !== undefined ? z : -1; -} - -RandomGroup.prototype.place = function(player, constraint) -{ - var resultObjs = pickRandom(this.elements).place(this.x, this.z, player, this.avoidSelf, constraint); - if (resultObjs === undefined) - return undefined; - - // Add placed objects to map - for (let obj of resultObjs) - { - g_Map.addObject(obj); - - // Convert position to integer number of tiles - if (this.tileClass !== undefined) - this.tileClass.add(Math.floor(obj.position.x/CELL_SIZE), Math.floor(obj.position.z/CELL_SIZE)); - } - - return resultObjs; -};