| [20357] | 1 | /**
|
|---|
| [20400] | 2 | * @file An Object tries to find locations around a location and returns an array of Entity items holding the template names, owners and locations on success.
|
|---|
| [20357] | 3 | */
|
|---|
| 4 |
|
|---|
| 5 | /**
|
|---|
| 6 | * The SimpleObject attempts to find locations for a random amount of entities with a random distance to the given center.
|
|---|
| 7 | */
|
|---|
| [21283] | 8 | function SimpleObject(templateName, minCount, maxCount, minDistance, maxDistance, minAngle = 0, maxAngle = 2 * Math.PI, avoidDistance = 1)
|
|---|
| [20357] | 9 | {
|
|---|
| 10 | this.templateName = templateName;
|
|---|
| 11 | this.minCount = minCount;
|
|---|
| 12 | this.maxCount = maxCount;
|
|---|
| 13 | this.minDistance = minDistance;
|
|---|
| 14 | this.maxDistance = maxDistance;
|
|---|
| 15 | this.minAngle = minAngle;
|
|---|
| 16 | this.maxAngle = maxAngle;
|
|---|
| [21425] | 17 | this.avoidDistanceSquared = Math.square(avoidDistance);
|
|---|
| [20357] | 18 |
|
|---|
| 19 | if (minCount > maxCount)
|
|---|
| 20 | throw new Error("SimpleObject: minCount should be less than or equal to maxCount");
|
|---|
| 21 |
|
|---|
| 22 | if (minDistance > maxDistance)
|
|---|
| 23 | throw new Error("SimpleObject: minDistance should be less than or equal to maxDistance");
|
|---|
| 24 |
|
|---|
| 25 | if (minAngle > maxAngle)
|
|---|
| 26 | throw new Error("SimpleObject: minAngle should be less than or equal to maxAngle");
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| [21283] | 29 | SimpleObject.prototype.place = function(centerPosition, playerID, avoidPositions, constraint, maxRetries)
|
|---|
| [20357] | 30 | {
|
|---|
| [28036] | 31 | const entitySpecs = [];
|
|---|
| [21278] | 32 | let numRetries = 0;
|
|---|
| [28036] | 33 | const validTile = pos => this.templateName.startsWith(g_ActorPrefix) ? g_Map.validTile(pos) : g_Map.validTilePassable(pos);
|
|---|
| [20357] | 34 |
|
|---|
| 35 | for (let i = 0; i < randIntInclusive(this.minCount, this.maxCount); ++i)
|
|---|
| 36 | while (true)
|
|---|
| 37 | {
|
|---|
| [28036] | 38 | const distance = randFloat(this.minDistance, this.maxDistance);
|
|---|
| 39 | const angle = randomAngle();
|
|---|
| [20357] | 40 |
|
|---|
| [28036] | 41 | const position = Vector2D.sum([centerPosition, new Vector2D(0.5, 0.5), new Vector2D(distance, 0).rotate(-angle)]);
|
|---|
| [20357] | 42 |
|
|---|
| [21278] | 43 | if (validTile(position) &&
|
|---|
| [21283] | 44 | (!avoidPositions ||
|
|---|
| [21425] | 45 | entitySpecs.every(entSpec => entSpec.position.distanceToSquared(position) >= this.avoidDistanceSquared) &&
|
|---|
| 46 | avoidPositions.every(avoid => avoid.position.distanceToSquared(position) >= Math.max(this.avoidDistanceSquared, avoid.distanceSquared))) &&
|
|---|
| [21069] | 47 | constraint.allows(position.clone().floor()))
|
|---|
| [20357] | 48 | {
|
|---|
| [21069] | 49 | entitySpecs.push({
|
|---|
| 50 | "templateName": this.templateName,
|
|---|
| 51 | "playerID": playerID,
|
|---|
| 52 | "position": position,
|
|---|
| 53 | "angle": randFloat(this.minAngle, this.maxAngle)
|
|---|
| 54 | });
|
|---|
| [20357] | 55 | break;
|
|---|
| 56 | }
|
|---|
| [21408] | 57 |
|
|---|
| 58 | if (numRetries++ > maxRetries)
|
|---|
| [20357] | 59 | return undefined;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| [21069] | 62 | return entitySpecs;
|
|---|
| [20357] | 63 | };
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Same as SimpleObject, but choses one of the given templates at random.
|
|---|
| 67 | */
|
|---|
| [21413] | 68 | function RandomObject(templateNames, minCount, maxCount, minDistance, maxDistance, minAngle, maxAngle, avoidDistance = 1)
|
|---|
| [20357] | 69 | {
|
|---|
| [21329] | 70 | this.templateNames = templateNames;
|
|---|
| 71 | this.minCount = minCount;
|
|---|
| 72 | this.maxCount = maxCount;
|
|---|
| 73 | this.minDistance = minDistance;
|
|---|
| 74 | this.maxDistance = maxDistance;
|
|---|
| 75 | this.minAngle = minAngle;
|
|---|
| 76 | this.maxAngle = maxAngle;
|
|---|
| [21413] | 77 | this.avoidDistance = avoidDistance;
|
|---|
| [20357] | 78 | }
|
|---|
| 79 |
|
|---|
| [21283] | 80 | RandomObject.prototype.place = function(centerPosition, player, avoidPositions, constraint, maxRetries)
|
|---|
| [20357] | 81 | {
|
|---|
| [21413] | 82 | return new SimpleObject(pickRandom(this.templateNames), this.minCount, this.maxCount, this.minDistance, this.maxDistance, this.minAngle, this.maxAngle, this.avoidDistance).place(
|
|---|
| [21329] | 83 | centerPosition, player, avoidPositions, constraint, maxRetries);
|
|---|
| [20357] | 84 | };
|
|---|