Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/BuildingAI.js
Show First 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | BuildingAI.prototype.OnDiplomacyChanged = function(msg) | ||||
// Remove maybe now allied/neutral units | // Remove maybe now allied/neutral units | ||||
this.targetUnits = []; | this.targetUnits = []; | ||||
this.SetupRangeQuery(); | this.SetupRangeQuery(); | ||||
this.SetupGaiaRangeQuery(); | this.SetupGaiaRangeQuery(); | ||||
}; | }; | ||||
BuildingAI.prototype.OnDestroy = function() | BuildingAI.prototype.OnDestroy = function() | ||||
{ | { | ||||
if (this.timer) | this.StopTimer(); | ||||
{ | |||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | |||||
cmpTimer.CancelTimer(this.timer); | |||||
this.timer = undefined; | |||||
} | |||||
// Clean up range queries | // Clean up range queries | ||||
let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); | let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); | ||||
if (this.enemyUnitsQuery) | if (this.enemyUnitsQuery) | ||||
cmpRangeManager.DestroyActiveQuery(this.enemyUnitsQuery); | cmpRangeManager.DestroyActiveQuery(this.enemyUnitsQuery); | ||||
if (this.gaiaUnitsQuery) | if (this.gaiaUnitsQuery) | ||||
cmpRangeManager.DestroyActiveQuery(this.gaiaUnitsQuery); | cmpRangeManager.DestroyActiveQuery(this.gaiaUnitsQuery); | ||||
}; | }; | ||||
/** | /** | ||||
* React on Attack value modifications, as it might influence the range | * React on Attack value modifications, as it might influence the range | ||||
*/ | */ | ||||
BuildingAI.prototype.OnValueModification = function(msg) | BuildingAI.prototype.OnValueModification = function(msg) | ||||
{ | { | ||||
if (msg.component != "Attack") | if (msg.component != "Attack") | ||||
return; | return; | ||||
// Restart the timer if the attack repeat time has changed | |||||
if (this.timer && msg.valueNames.indexOf("Attack/" + attackType + "/RepeatTime") != -1) | |||||
{ | |||||
this.StopTimer(); | |||||
this.StartTimer(0); | |||||
} | |||||
this.targetUnits = []; | this.targetUnits = []; | ||||
this.SetupRangeQuery(); | this.SetupRangeQuery(); | ||||
this.SetupGaiaRangeQuery(); | this.SetupGaiaRangeQuery(); | ||||
}; | }; | ||||
/** | /** | ||||
* Setup the Range Query to detect units coming in & out of range | * Setup the Range Query to detect units coming in & out of range | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 91 Lines • ▼ Show 20 Lines | for (let entity of msg.removed) | ||||
if (index > -1) | if (index > -1) | ||||
this.targetUnits.splice(index, 1); | this.targetUnits.splice(index, 1); | ||||
} | } | ||||
if (this.targetUnits.length) | if (this.targetUnits.length) | ||||
this.StartTimer(); | this.StartTimer(); | ||||
}; | }; | ||||
BuildingAI.prototype.StartTimer = function() | BuildingAI.prototype.StartTimer = function(offset) | ||||
bb: perhaps rather than `offset` name `wasAttacking` and use it as an optional bool with default… | |||||
{ | { | ||||
if (this.timer) | if (this.timer) | ||||
return; | return; | ||||
var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack); | var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack); | ||||
if (!cmpAttack) | if (!cmpAttack) | ||||
return; | return; | ||||
var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | ||||
var attackTimers = cmpAttack.GetTimers(attackType); | var attackTimers = cmpAttack.GetTimers(attackType); | ||||
// Rather than figure out the correct offset when resetting the timer, use an estimate of half of the repeat time | |||||
this.timer = cmpTimer.SetInterval(this.entity, IID_BuildingAI, "FireArrows", | this.timer = cmpTimer.SetInterval(this.entity, IID_BuildingAI, "FireArrows", | ||||
attackTimers.prepare, attackTimers.repeat / roundCount, null); | offset === undefined ? attackTimers.prepare : attackTimers.repeat / roundCount / 2, | ||||
attackTimers.repeat / roundCount, null); | |||||
}; | |||||
BuildingAI.prototype.StopTimer = function() | |||||
{ | |||||
if (!this.timer) | |||||
return; | |||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | |||||
cmpTimer.CancelTimer(this.timer); | |||||
this.timer = undefined; | |||||
}; | }; | ||||
BuildingAI.prototype.GetDefaultArrowCount = function() | BuildingAI.prototype.GetDefaultArrowCount = function() | ||||
{ | { | ||||
var arrowCount = +this.template.DefaultArrowCount; | var arrowCount = +this.template.DefaultArrowCount; | ||||
return Math.round(ApplyValueModificationsToEntity("BuildingAI/DefaultArrowCount", arrowCount, this.entity)); | return Math.round(ApplyValueModificationsToEntity("BuildingAI/DefaultArrowCount", arrowCount, this.entity)); | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* Fire arrows with random temporal distribution on prefered targets. | * Fire arrows with random temporal distribution on prefered targets. | ||||
* Called 'roundCount' times every 'RepeatTime' seconds when there are units in the range. | * Called 'roundCount' times every 'RepeatTime' seconds when there are units in the range. | ||||
*/ | */ | ||||
BuildingAI.prototype.FireArrows = function() | BuildingAI.prototype.FireArrows = function() | ||||
{ | { | ||||
if (!this.targetUnits.length && !this.unitAITarget) | if (!this.targetUnits.length && !this.unitAITarget) | ||||
{ | { | ||||
if (!this.timer) | this.StopTimer(); | ||||
return; | |||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | |||||
cmpTimer.CancelTimer(this.timer); | |||||
this.timer = undefined; | |||||
return; | return; | ||||
} | } | ||||
let cmpAttack = Engine.QueryInterface(this.entity, IID_Attack); | let cmpAttack = Engine.QueryInterface(this.entity, IID_Attack); | ||||
if (!cmpAttack) | if (!cmpAttack) | ||||
return; | return; | ||||
if (this.currentRound > roundCount - 1) | if (this.currentRound > roundCount - 1) | ||||
▲ Show 20 Lines • Show All 87 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
perhaps rather than offset name wasAttacking and use it as an optional bool with default false