From 10e8c732ffa0ce62611018a60736c43a69de17da Mon Sep 17 00:00:00 2001
From: leper <leper@wildfiregames.com>
Date: Fri, 26 Aug 2016 18:59:00 +0200
Subject: [PATCH 5/5] Add GetPlayersByDiplomacy to cmpPlayer.
This is in turn used by GetEnemies and the new GetAllies and GetMutualAllies.
Use these in a few places as those are common queries.
---
.../public/simulation/components/BuildingAI.js | 8 ++--
.../data/mods/public/simulation/components/Gate.js | 6 +--
.../mods/public/simulation/components/Player.js | 45 +++++++++++++---------
.../mods/public/simulation/components/UnitAI.js | 32 +++------------
.../simulation/components/tests/test_Player.js | 2 +
.../simulation/components/tests/test_UnitAI.js | 10 +++--
6 files changed, 43 insertions(+), 60 deletions(-)
diff --git a/binaries/data/mods/public/simulation/components/BuildingAI.js b/binaries/data/mods/public/simulation/components/BuildingAI.js
index 0bb06f5..efacf79 100644
a
|
b
|
BuildingAI.prototype.SetupRangeQuery = function()
|
121 | 121 | if (!cmpPlayer) |
122 | 122 | return; |
123 | 123 | |
124 | | var enemies = []; |
125 | | var numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers(); |
126 | | for (let i = 1; i < numPlayers; ++i) |
127 | | if (cmpPlayer.IsEnemy(i)) |
128 | | enemies.push(i); |
| 124 | var enemies = cmpPlayer.GetEnemies(); |
| 125 | if (enemies.length && enemies[0] == 0) |
| 126 | enemies.shift(); // remove gaia |
129 | 127 | |
130 | 128 | if (!enemies.length) |
131 | 129 | return; |
diff --git a/binaries/data/mods/public/simulation/components/Gate.js b/binaries/data/mods/public/simulation/components/Gate.js
index c322c17..faa5c11 100644
a
|
b
|
Gate.prototype.OnDestroy = function()
|
65 | 65 | Gate.prototype.SetupRangeQuery = function(owner) |
66 | 66 | { |
67 | 67 | var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); |
68 | | var cmpPlayer = QueryPlayerIDInterface(owner); |
69 | 68 | |
70 | 69 | if (this.unitsQuery) |
71 | 70 | cmpRangeManager.DestroyActiveQuery(this.unitsQuery); |
72 | 71 | |
73 | 72 | // Only allied units can make the gate open. |
74 | | var players = []; |
75 | | for (var i = 0; i < cmpPlayer.GetDiplomacy().length; ++i) |
76 | | if (cmpPlayer.IsAlly(i)) |
77 | | players.push(i); |
| 73 | var players = QueryPlayerIDInterface(owner).GetAllies(); |
78 | 74 | |
79 | 75 | var range = this.GetPassRange(); |
80 | 76 | if (range > 0) |
diff --git a/binaries/data/mods/public/simulation/components/Player.js b/binaries/data/mods/public/simulation/components/Player.js
index 7959dd3..89986af 100644
a
|
b
|
Player.prototype.SetDiplomacyIndex = function(idx, value)
|
497 | 497 | Player.prototype.UpdateSharedLos = function() |
498 | 498 | { |
499 | 499 | let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); |
500 | | let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); |
501 | 500 | let cmpTechnologyManager = Engine.QueryInterface(this.entity, IID_TechnologyManager); |
502 | | if (!cmpRangeManager || !cmpPlayerManager || !cmpTechnologyManager) |
| 501 | if (!cmpRangeManager || !cmpTechnologyManager) |
503 | 502 | return; |
504 | 503 | |
505 | | let sharedLos = []; |
506 | 504 | if (!cmpTechnologyManager.IsTechnologyResearched(this.template.SharedLosTech)) |
507 | 505 | { |
508 | 506 | cmpRangeManager.SetSharedLos(this.playerID, [this.playerID]); |
509 | 507 | return; |
510 | 508 | } |
511 | | for (var i = 0; i < cmpPlayerManager.GetNumPlayers(); ++i) |
512 | | if (this.IsMutualAlly(i)) |
513 | | sharedLos.push(i); |
514 | 509 | |
515 | | cmpRangeManager.SetSharedLos(this.playerID, sharedLos); |
| 510 | cmpRangeManager.SetSharedLos(this.playerID, this.GetMutualAllies()); |
516 | 511 | }; |
517 | 512 | |
518 | 513 | Player.prototype.GetFormations = function() |
… |
… |
Player.prototype.IsAI = function()
|
575 | 570 | return this.isAI; |
576 | 571 | }; |
577 | 572 | |
| 573 | Player.prototype.GetPlayersByDiplomacy = function(func) |
| 574 | { |
| 575 | var players = []; |
| 576 | for (var i = 0; i < this.diplomacy.length; ++i) |
| 577 | if (this[func](i)) |
| 578 | players.push(i); |
| 579 | return players; |
| 580 | }; |
| 581 | |
578 | 582 | Player.prototype.SetAlly = function(id) |
579 | 583 | { |
580 | 584 | this.SetDiplomacyIndex(id, 1); |
… |
… |
Player.prototype.IsAlly = function(id)
|
588 | 592 | return this.diplomacy[id] > 0; |
589 | 593 | }; |
590 | 594 | |
| 595 | Player.prototype.GetAllies = function() |
| 596 | { |
| 597 | return this.GetPlayersByDiplomacy("IsAlly"); |
| 598 | }; |
| 599 | |
591 | 600 | /** |
592 | 601 | * Check if given player is our ally excluding ourself |
593 | 602 | */ |
… |
… |
Player.prototype.IsMutualAlly = function(id)
|
605 | 614 | return this.IsAlly(id) && cmpPlayer && cmpPlayer.IsAlly(this.playerID); |
606 | 615 | }; |
607 | 616 | |
| 617 | Player.prototype.GetMutualAllies = function() |
| 618 | { |
| 619 | return this.GetPlayersByDiplomacy("IsMutualAlly"); |
| 620 | }; |
| 621 | |
608 | 622 | /** |
609 | 623 | * Check if given player is our ally, and we are its ally, excluding ourself |
610 | 624 | */ |
… |
… |
Player.prototype.SetEnemy = function(id)
|
619 | 633 | }; |
620 | 634 | |
621 | 635 | /** |
622 | | * Get all enemies of a given player. |
623 | | */ |
624 | | Player.prototype.GetEnemies = function() |
625 | | { |
626 | | var enemies = []; |
627 | | for (var i = 0; i < this.diplomacy.length; ++i) |
628 | | if (this.diplomacy[i] < 0) |
629 | | enemies.push(i); |
630 | | return enemies; |
631 | | }; |
632 | | |
633 | | /** |
634 | 636 | * Check if given player is our enemy |
635 | 637 | */ |
636 | 638 | Player.prototype.IsEnemy = function(id) |
… |
… |
Player.prototype.IsEnemy = function(id)
|
638 | 640 | return this.diplomacy[id] < 0; |
639 | 641 | }; |
640 | 642 | |
| 643 | Player.prototype.GetEnemies = function() |
| 644 | { |
| 645 | return this.GetPlayersByDiplomacy("IsEnemy"); |
| 646 | }; |
| 647 | |
641 | 648 | Player.prototype.SetNeutral = function(id) |
642 | 649 | { |
643 | 650 | this.SetDiplomacyIndex(id, 0); |
diff --git a/binaries/data/mods/public/simulation/components/UnitAI.js b/binaries/data/mods/public/simulation/components/UnitAI.js
index 0276d21..b0591fa 100644
a
|
b
|
UnitAI.prototype.SetupRangeQuery = function(enable = true)
|
3473 | 3473 | if (!cmpPlayer) |
3474 | 3474 | return; |
3475 | 3475 | |
3476 | | var players = []; |
3477 | | |
3478 | | var numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers(); |
3479 | | for (var i = 0; i < numPlayers; ++i) |
3480 | | { |
3481 | | // Exclude allies, and self |
3482 | | // TODO: How to handle neutral players - Special query to attack military only? |
3483 | | if (cmpPlayer.IsEnemy(i)) |
3484 | | players.push(i); |
3485 | | } |
3486 | | |
| 3476 | // Exclude allies, and self |
| 3477 | // TODO: How to handle neutral players - Special query to attack military only? |
| 3478 | var players = cmpPlayer.GetEnemies(); |
3487 | 3479 | var range = this.GetQueryRange(IID_Attack); |
3488 | 3480 | |
3489 | 3481 | this.losRangeQuery = cmpRangeManager.CreateActiveQuery(this.entity, range.min, range.max, players, IID_DamageReceiver, cmpRangeManager.GetEntityFlagMask("normal")); |
… |
… |
UnitAI.prototype.SetupHealRangeQuery = function(enable = true)
|
3510 | 3502 | if (!cmpPlayer) |
3511 | 3503 | return; |
3512 | 3504 | |
3513 | | var players = []; |
3514 | | |
3515 | | var numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers(); |
3516 | | for (var i = 0; i < numPlayers; ++i) |
3517 | | { |
3518 | | // Exclude gaia and enemies |
3519 | | if (cmpPlayer.IsAlly(i)) |
3520 | | players.push(i); |
3521 | | } |
3522 | | |
| 3505 | var players = cmpPlayer.GetAllies(); |
3523 | 3506 | var range = this.GetQueryRange(IID_Heal); |
3524 | 3507 | |
3525 | 3508 | this.losHealRangeQuery = cmpRangeManager.CreateActiveQuery(this.entity, range.min, range.max, players, IID_Health, cmpRangeManager.GetEntityFlagMask("injured")); |
… |
… |
UnitAI.prototype.FindNearestDropsite = function(genericType)
|
4080 | 4063 | var players = [owner]; |
4081 | 4064 | var cmpPlayer = QueryOwnerInterface(this.entity); |
4082 | 4065 | if (cmpPlayer && cmpPlayer.HasSharedDropsites()) |
4083 | | { |
4084 | | let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); |
4085 | | for (let i = 1; i < cmpPlayerManager.GetNumPlayers(); ++i) |
4086 | | if (i != owner && cmpPlayer.IsMutualAlly(i)) |
4087 | | players.push(i); |
4088 | | } |
| 4066 | players = cmpPlayer.GetMutualAllies(); |
4089 | 4067 | |
4090 | 4068 | var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); |
4091 | 4069 | var nearby = cmpRangeManager.ExecuteQuery(this.entity, 0, -1, players, IID_ResourceDropsite); |
diff --git a/binaries/data/mods/public/simulation/components/tests/test_Player.js b/binaries/data/mods/public/simulation/components/tests/test_Player.js
index f8650c9..abb2d73 100644
a
|
b
|
TS_ASSERT_EQUALS(cmpPlayer.GetPopulationCount(), 0);
|
9 | 9 | TS_ASSERT_EQUALS(cmpPlayer.GetPopulationLimit(), 0); |
10 | 10 | |
11 | 11 | cmpPlayer.SetDiplomacy([-1, 1, 0, 1, -1]); |
| 12 | TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetAllies(), [1, 3]); |
| 13 | TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetEnemies(), [0, 4]); |
12 | 14 | |
13 | 15 | var diplo = cmpPlayer.GetDiplomacy(); |
14 | 16 | diplo[0] = 1; |
diff --git a/binaries/data/mods/public/simulation/components/tests/test_UnitAI.js b/binaries/data/mods/public/simulation/components/tests/test_UnitAI.js
index add4cc8..e899f0e 100644
a
|
b
|
function TestFormationExiting(mode)
|
58 | 58 | }); |
59 | 59 | |
60 | 60 | AddMock(playerEntity, IID_Player, { |
61 | | IsAlly: function() { return []; }, |
62 | | IsEnemy: function() { return []; }, |
| 61 | IsAlly: function() { return false; }, |
| 62 | IsEnemy: function() { return true; }, |
| 63 | GetEnemies: function() { return []; }, |
63 | 64 | }); |
64 | 65 | |
65 | 66 | |
… |
… |
function TestMoveIntoFormationWhileAttacking()
|
204 | 205 | }); |
205 | 206 | |
206 | 207 | AddMock(playerEntity, IID_Player, { |
207 | | IsAlly: function() { return []; }, |
208 | | IsEnemy: function() { return []; }, |
| 208 | IsAlly: function() { return false; }, |
| 209 | IsEnemy: function() { return true; }, |
| 210 | GetEnemies: function() { return []; }, |
209 | 211 | }); |
210 | 212 | |
211 | 213 | // create units |