Fix SimpleObject assuming positions on the impassable map border to be passable while...
[0ad.git] / binaries / data / mods / public / maps / random / rmgen / group.js
bloba0f4a00cb3f55923758c9214da02c4e02bef821a
1 /**
2  * @file A Group tests if a set of entities specified in the constructor can be placed and
3  * potentially places some of them (typically all or none).
4  *
5  * The location is defined by the x and z property of the Group instance and can be modified externally.
6  * The Group is free to determine whether, where exactly and how many entities to place.
7  *
8  * The Constraint to test against and the future owner of the entities are passed by the caller.
9  * Typically Groups are called from createObjectGroup with the location set in the constructor or
10  * from createObjectGroups that randomizes the x and z property of the Group before calling place.
11  */
13 /**
14  * Places all of the given Objects.
15  *
16  * @param objects - An array of Objects, for instance SimpleObjects.
17  * @param avoidSelf - Objects will not overlap.
18  * @param tileClass - Optional TileClass that tiles with placed entities are marked with.
19  * @param centerPosition - The location the group is placed around. Can be omitted if the property is set externally.
20  */
21 function SimpleGroup(objects, avoidSelf = false, tileClass = undefined, centerPosition = undefined)
23         this.objects = objects;
24         this.tileClass = tileClass;
25         this.avoidSelf = avoidSelf;
26         this.centerPosition = undefined;
28         if (centerPosition)
29                 this.setCenterPosition(centerPosition);
32 SimpleGroup.prototype.setCenterPosition = function(position)
34         this.centerPosition = deepfreeze(position.clone().round());
37 SimpleGroup.prototype.place = function(player, constraint)
39         let entitySpecsResult = [];
41         // Test if the Objects can be placed at the given location
42         // Place none of them if one can't be placed.
43         for (let object of this.objects)
44         {
45                 let entitySpecs = object.place(this.centerPosition, player, this.avoidSelf, constraint);
47                 if (!entitySpecs)
48                         return undefined;
50                 entitySpecsResult = entitySpecsResult.concat(entitySpecs);
51         }
54         // Create and place entities as specified
55         let entities = [];
56         for (let entitySpecs of entitySpecsResult)
57         {
58                 // The Object must ensure that non-actor entities are not placed at the impassable map-border
59                 entities.push(
60                         g_Map.placeEntityAnywhere(entitySpecs.templateName, entitySpecs.playerID, entitySpecs.position, entitySpecs.angle));
62                 if (this.tileClass)
63                         this.tileClass.add(entitySpecs.position.clone().floor());
64         }
66         return entities;
69 /**
70  * Randomly choses one of the given Objects and places it just like the SimpleGroup.
71  */
72 function RandomGroup(objects, avoidSelf = false, tileClass = undefined, centerPosition = undefined)
74         this.simpleGroup = new SimpleGroup([pickRandom(objects)], avoidSelf, tileClass, centerPosition);
77 RandomGroup.prototype.setCenterPosition = function(position)
79         this.simpleGroup.setCenterPosition(position);
82 RandomGroup.prototype.place = function(player, constraint)
84         return this.simpleGroup.place(player, constraint);