Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/GarrisonHolder.js
Show All 25 Lines | "<optional>" + | ||||
"<element name='EjectHealth' a:help='Percentage of maximum health below which this holder no longer allows garrisoning'>" + | "<element name='EjectHealth' a:help='Percentage of maximum health below which this holder no longer allows garrisoning'>" + | ||||
"<ref name='nonNegativeDecimal'/>" + | "<ref name='nonNegativeDecimal'/>" + | ||||
"</element>" + | "</element>" + | ||||
"</optional>" + | "</optional>" + | ||||
"<optional>" + | "<optional>" + | ||||
"<element name='Pickup' a:help='This garrisonHolder will move to pick up units to be garrisoned'>" + | "<element name='Pickup' a:help='This garrisonHolder will move to pick up units to be garrisoned'>" + | ||||
"<data type='boolean'/>" + | "<data type='boolean'/>" + | ||||
"</element>" + | "</element>" + | ||||
"</optional>" + | |||||
"<optional>" + | |||||
"<element name='AllowedPlayers' a:help='What diplomatic attitude the owner of an entity has to have to be able to garrison.'>" + | |||||
"<list>" + | |||||
"<oneOrMore>" + | |||||
"<choice>" + | |||||
"<value>Player</value>" + | |||||
"<value>MutualAlly</value>" + | |||||
"<value>Neutral</value>" + | |||||
"<value>Enemy</value>" + | |||||
"</choice>" + | |||||
"</oneOrMore>" + | |||||
"</list>" + | |||||
"</element>" + | |||||
"</optional>"; | "</optional>"; | ||||
/** | /** | ||||
* Initialize GarrisonHolder Component | * Initialize GarrisonHolder Component | ||||
* Garrisoning when loading a map is set in the script of the map, by setting initGarrison | * Garrisoning when loading a map is set in the script of the map, by setting initGarrison | ||||
* which should contain the array of garrisoned entities. | * which should contain the array of garrisoned entities. | ||||
*/ | */ | ||||
GarrisonHolder.prototype.Init = function() | GarrisonHolder.prototype.Init = function() | ||||
{ | { | ||||
this.entities = []; | this.entities = []; | ||||
this.allowedClasses = ApplyValueModificationsToEntity("GarrisonHolder/List/_string", this.template.List._string, this.entity); | this.allowedClasses = ApplyValueModificationsToEntity("GarrisonHolder/List/_string", this.template.List._string, this.entity); | ||||
this.allowedPlayers = this.template.AllowedPlayers ? this.template.AllowedPlayers.split(/\s+/) : ["Player", "MutualAlly"]; | |||||
}; | }; | ||||
/** | /** | ||||
* @param {number} entity - The entity to verify. | * @param {number} entity - The entity to verify. | ||||
* @return {boolean} - Whether the given entity is garrisoned in this GarrisonHolder. | * @return {boolean} - Whether the given entity is garrisoned in this GarrisonHolder. | ||||
*/ | */ | ||||
GarrisonHolder.prototype.IsGarrisoned = function(entity) | GarrisonHolder.prototype.IsGarrisoned = function(entity) | ||||
{ | { | ||||
Show All 17 Lines | |||||
}; | }; | ||||
GarrisonHolder.prototype.GetEntities = function() | GarrisonHolder.prototype.GetEntities = function() | ||||
{ | { | ||||
return this.entities; | return this.entities; | ||||
}; | }; | ||||
/** | /** | ||||
* What diplomatic stances are allowd to garrison. | |||||
* @return {string[]} An array containing the allowed diplomacies to garrison. | |||||
*/ | |||||
GarrisonHolder.prototype.GetAllowedPlayers = function() | |||||
{ | |||||
return ApplyValueModificationsToEntity("GarrisonHolder/AllowedPlayers", this.allowedPlayers, this.entity); | |||||
}; | |||||
/** | |||||
* @return {Array} unit classes which can be garrisoned inside this | * @return {Array} unit classes which can be garrisoned inside this | ||||
* particular entity. Obtained from the entity's template. | * particular entity. Obtained from the entity's template. | ||||
*/ | */ | ||||
GarrisonHolder.prototype.GetAllowedClasses = function() | GarrisonHolder.prototype.GetAllowedClasses = function() | ||||
{ | { | ||||
return this.allowedClasses; | return this.allowedClasses; | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | GarrisonHolder.prototype.IsAllowedToGarrison = function(entity) | ||||
if (!cmpGarrisonable || this.OccupiedSlots() + cmpGarrisonable.TotalSize() > this.GetCapacity()) | if (!cmpGarrisonable || this.OccupiedSlots() + cmpGarrisonable.TotalSize() > this.GetCapacity()) | ||||
return false; | return false; | ||||
return this.IsAllowedToBeGarrisoned(entity); | return this.IsAllowedToBeGarrisoned(entity); | ||||
}; | }; | ||||
GarrisonHolder.prototype.IsAllowedToBeGarrisoned = function(entity) | GarrisonHolder.prototype.IsAllowedToBeGarrisoned = function(entity) | ||||
{ | { | ||||
if (!IsOwnedByMutualAllyOfEntity(entity, this.entity)) | if (!this.IsAllowedByDiplomacy(entity)) | ||||
return false; | return false; | ||||
let cmpIdentity = Engine.QueryInterface(entity, IID_Identity); | let cmpIdentity = Engine.QueryInterface(entity, IID_Identity); | ||||
return cmpIdentity && MatchesClassList(cmpIdentity.GetClassesList(), this.allowedClasses); | return cmpIdentity && MatchesClassList(cmpIdentity.GetClassesList(), this.allowedClasses); | ||||
}; | }; | ||||
/** | /** | ||||
* @param {number} entity - The entityID to garrison. | * @param {number} entity - The entityID to garrison. | ||||
▲ Show 20 Lines • Show All 202 Lines • ▼ Show 20 Lines | GarrisonHolder.prototype.HasEnoughHealth = function() | ||||
if (this.template.EjectHealth === undefined) | if (this.template.EjectHealth === undefined) | ||||
return true; | return true; | ||||
let cmpHealth = Engine.QueryInterface(this.entity, IID_Health); | let cmpHealth = Engine.QueryInterface(this.entity, IID_Health); | ||||
return !cmpHealth || cmpHealth.GetHitpoints() > Math.floor(+this.template.EjectHealth * cmpHealth.GetMaxHitpoints()); | return !cmpHealth || cmpHealth.GetHitpoints() > Math.floor(+this.template.EjectHealth * cmpHealth.GetMaxHitpoints()); | ||||
}; | }; | ||||
/** | /** | ||||
* Used to check if the garrisoning entity's diplomatic status is right for garrisoning this entity. | |||||
* @param {number} ent - The entity ID to check for. | |||||
* | |||||
* @return {boolean} - Whether garrisoning is allowed purely on diplomatic grounds. | |||||
*/ | |||||
GarrisonHolder.prototype.IsAllowedByDiplomacy = function(ent) | |||||
{ | |||||
if (!this.template.AllowedPlayers) | |||||
return IsOwnedByMutualAllyOfEntity(this.entity, ent); | |||||
let owner = INVALID_PLAYER; | |||||
let cmpOwner = Engine.QueryInterface(this.entity, IID_Ownership); | |||||
if (cmpOwner) | |||||
owner = cmpOwner.GetOwner(); | |||||
let allowedPlayers = this.GetAllowedPlayers(); | |||||
return allowedPlayers.indexOf("Player") != -1 && IsOwnedByPlayer(owner, ent) || | |||||
allowedPlayers.indexOf("MutualAlly") != -1 && IsOwnedByMutualAllyOfEntity(this.entity, ent) || | |||||
allowedPlayers.indexOf("Neutral") != -1 && IsOwnedByNeutralOfEntity(this.entity, ent) || | |||||
allowedPlayers.indexOf("Enemy") != -1 && IsOwnedByEnemyOfEntity(this.entity, ent); | |||||
}; | |||||
/** | |||||
* Called every second. Heals garrisoned units. | * Called every second. Heals garrisoned units. | ||||
*/ | */ | ||||
GarrisonHolder.prototype.HealTimeout = function(data) | GarrisonHolder.prototype.HealTimeout = function(data) | ||||
{ | { | ||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | ||||
if (!this.entities.length) | if (!this.entities.length) | ||||
{ | { | ||||
cmpTimer.CancelTimer(this.timer); | cmpTimer.CancelTimer(this.timer); | ||||
Show All 39 Lines | |||||
* If a garrisoned entity is captured, or about to be killed (so its owner changes to '-1'), | * If a garrisoned entity is captured, or about to be killed (so its owner changes to '-1'), | ||||
* remove it from the building so we only ever contain valid entities. | * remove it from the building so we only ever contain valid entities. | ||||
*/ | */ | ||||
GarrisonHolder.prototype.OnGlobalOwnershipChanged = function(msg) | GarrisonHolder.prototype.OnGlobalOwnershipChanged = function(msg) | ||||
{ | { | ||||
// The ownership change may be on the garrisonholder | // The ownership change may be on the garrisonholder | ||||
if (this.entity == msg.entity) | if (this.entity == msg.entity) | ||||
{ | { | ||||
let entities = this.entities.filter(ent => msg.to == INVALID_PLAYER || !IsOwnedByMutualAllyOfEntity(this.entity, ent)); | let entities = this.entities.filter(ent => msg.to == INVALID_PLAYER || !this.IsAllowedByDiplomacy(ent)); | ||||
if (entities.length) | if (entities.length) | ||||
this.EjectOrKill(entities); | this.EjectOrKill(entities); | ||||
return; | return; | ||||
} | } | ||||
// or on some of its garrisoned units | // or on some of its garrisoned units | ||||
let entityIndex = this.entities.indexOf(msg.entity); | let entityIndex = this.entities.indexOf(msg.entity); | ||||
if (entityIndex != -1 && (msg.to == INVALID_PLAYER || !IsOwnedByMutualAllyOfEntity(this.entity, msg.entity))) | if (entityIndex != -1 && (msg.to == INVALID_PLAYER || !this.IsAllowedByDiplomacy(msg.entity))) | ||||
this.EjectOrKill([msg.entity]); | this.EjectOrKill([msg.entity]); | ||||
}; | }; | ||||
/** | /** | ||||
* Update list of garrisoned entities when a game inits. | * Update list of garrisoned entities when a game inits. | ||||
*/ | */ | ||||
GarrisonHolder.prototype.OnGlobalSkirmishReplacerReplaced = function(msg) | GarrisonHolder.prototype.OnGlobalSkirmishReplacerReplaced = function(msg) | ||||
{ | { | ||||
Show All 14 Lines | GarrisonHolder.prototype.OnGlobalSkirmishReplacerReplaced = function(msg) | ||||
} | } | ||||
}; | }; | ||||
/** | /** | ||||
* Eject all foreign garrisoned entities which are no more allied. | * Eject all foreign garrisoned entities which are no more allied. | ||||
*/ | */ | ||||
GarrisonHolder.prototype.OnDiplomacyChanged = function() | GarrisonHolder.prototype.OnDiplomacyChanged = function() | ||||
{ | { | ||||
this.EjectOrKill(this.entities.filter(ent => !IsOwnedByMutualAllyOfEntity(this.entity, ent))); | this.EjectOrKill(this.entities.filter(ent => !this.IsAllowedByDiplomacy(ent))); | ||||
}; | }; | ||||
/** | /** | ||||
* Eject or kill a garrisoned unit which can no more be garrisoned | * Eject or kill a garrisoned unit which can no more be garrisoned | ||||
* (garrisonholder's health too small or ownership changed). | * (garrisonholder's health too small or ownership changed). | ||||
*/ | */ | ||||
GarrisonHolder.prototype.EjectOrKill = function(entities) | GarrisonHolder.prototype.EjectOrKill = function(entities) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 107 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator