| [20355] | 1 | /**
|
|---|
| [20400] | 2 | * @file A Group tests if a set of entities specified in the constructor can be placed and
|
|---|
| [20355] | 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 | */
|
|---|
| 12 |
|
|---|
| 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.
|
|---|
| [21069] | 19 | * @param centerPosition - The location the group is placed around. Can be omitted if the property is set externally.
|
|---|
| [20355] | 20 | */
|
|---|
| [21069] | 21 | function SimpleGroup(objects, avoidSelf = false, tileClass = undefined, centerPosition = undefined)
|
|---|
| [20355] | 22 | {
|
|---|
| 23 | this.objects = objects;
|
|---|
| 24 | this.tileClass = tileClass;
|
|---|
| 25 | this.avoidSelf = avoidSelf;
|
|---|
| [21069] | 26 | this.centerPosition = undefined;
|
|---|
| 27 |
|
|---|
| 28 | if (centerPosition)
|
|---|
| 29 | this.setCenterPosition(centerPosition);
|
|---|
| [20355] | 30 | }
|
|---|
| 31 |
|
|---|
| [21069] | 32 | SimpleGroup.prototype.setCenterPosition = function(position)
|
|---|
| 33 | {
|
|---|
| 34 | this.centerPosition = deepfreeze(position.clone().round());
|
|---|
| 35 | };
|
|---|
| 36 |
|
|---|
| [21283] | 37 | SimpleGroup.prototype.place = function(playerID, constraint)
|
|---|
| [20355] | 38 | {
|
|---|
| [21069] | 39 | let entitySpecsResult = [];
|
|---|
| [21283] | 40 | let avoidPositions = this.avoidSelf ? [] : undefined;
|
|---|
| [20355] | 41 |
|
|---|
| 42 | // Test if the Objects can be placed at the given location
|
|---|
| 43 | // Place none of them if one can't be placed.
|
|---|
| [28036] | 44 | for (const object of this.objects)
|
|---|
| [20355] | 45 | {
|
|---|
| [28036] | 46 | const entitySpecs = object.place(this.centerPosition, playerID, avoidPositions, constraint, 30);
|
|---|
| [20355] | 47 |
|
|---|
| [21069] | 48 | if (!entitySpecs)
|
|---|
| [20355] | 49 | return undefined;
|
|---|
| 50 |
|
|---|
| [21069] | 51 | entitySpecsResult = entitySpecsResult.concat(entitySpecs);
|
|---|
| [21283] | 52 |
|
|---|
| 53 | if (this.avoidSelf)
|
|---|
| 54 | avoidPositions = avoidPositions.concat(entitySpecs.map(entitySpec => ({
|
|---|
| 55 | "position": entitySpec.position,
|
|---|
| [21425] | 56 | "distanceSquared": object.avoidDistanceSquared
|
|---|
| [21283] | 57 | })));
|
|---|
| [20355] | 58 | }
|
|---|
| 59 |
|
|---|
| [21069] | 60 | // Create and place entities as specified
|
|---|
| [28036] | 61 | const entities = [];
|
|---|
| 62 | for (const entitySpecs of entitySpecsResult)
|
|---|
| [20355] | 63 | {
|
|---|
| [21278] | 64 | // The Object must ensure that non-actor entities are not placed at the impassable map-border
|
|---|
| [21069] | 65 | entities.push(
|
|---|
| [21278] | 66 | g_Map.placeEntityAnywhere(entitySpecs.templateName, entitySpecs.playerID, entitySpecs.position, entitySpecs.angle));
|
|---|
| [21025] | 67 |
|
|---|
| [21063] | 68 | if (this.tileClass)
|
|---|
| [21069] | 69 | this.tileClass.add(entitySpecs.position.clone().floor());
|
|---|
| [20355] | 70 | }
|
|---|
| 71 |
|
|---|
| [21069] | 72 | return entities;
|
|---|
| [20355] | 73 | };
|
|---|
| 74 |
|
|---|
| 75 | /**
|
|---|
| 76 | * Randomly choses one of the given Objects and places it just like the SimpleGroup.
|
|---|
| 77 | */
|
|---|
| [21069] | 78 | function RandomGroup(objects, avoidSelf = false, tileClass = undefined, centerPosition = undefined)
|
|---|
| [20355] | 79 | {
|
|---|
| [21069] | 80 | this.simpleGroup = new SimpleGroup([pickRandom(objects)], avoidSelf, tileClass, centerPosition);
|
|---|
| [20355] | 81 | }
|
|---|
| 82 |
|
|---|
| [21069] | 83 | RandomGroup.prototype.setCenterPosition = function(position)
|
|---|
| 84 | {
|
|---|
| 85 | this.simpleGroup.setCenterPosition(position);
|
|---|
| 86 | };
|
|---|
| 87 |
|
|---|
| [21283] | 88 | RandomGroup.prototype.place = function(playerID, constraint)
|
|---|
| [20355] | 89 | {
|
|---|
| [21283] | 90 | return this.simpleGroup.place(playerID, constraint);
|
|---|
| [20355] | 91 | };
|
|---|