Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/ResourceSupply.js
Show All 34 Lines | ResourceSupply.prototype.Init = function() | ||||
// List of IDs for each player | // List of IDs for each player | ||||
this.gatherers = []; | this.gatherers = []; | ||||
let numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers(); | let numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers(); | ||||
for (let i = 0; i < numPlayers; ++i) | for (let i = 0; i < numPlayers; ++i) | ||||
this.gatherers.push([]); | this.gatherers.push([]); | ||||
let [type, subtype] = this.template.Type.split('.'); | let [type, subtype] = this.template.Type.split('.'); | ||||
this.cachedType = { "generic": type, "specific": subtype }; | this.cachedType = { "generic": type, "specific": subtype }; | ||||
this.UpdateProperties(); | |||||
}; | |||||
ResourceSupply.prototype.UpdateProperties = function() | |||||
{ | |||||
this.diminishingReturns = ApplyValueModificationsToEntity("ResourceSupply/DiminishingReturns", +(this.template.DiminishingReturns || 1), this.entity); | |||||
}; | }; | ||||
ResourceSupply.prototype.IsInfinite = function() | ResourceSupply.prototype.IsInfinite = function() | ||||
{ | { | ||||
return !isFinite(+this.template.Amount); | return !isFinite(+this.template.Amount); | ||||
}; | }; | ||||
ResourceSupply.prototype.GetKillBeforeGather = function() | ResourceSupply.prototype.GetKillBeforeGather = function() | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | |||||
* Each additional gatherer decreases the rate following a geometric sequence, with diminishingReturns as ratio. | * Each additional gatherer decreases the rate following a geometric sequence, with diminishingReturns as ratio. | ||||
* @return {number} The diminishing return if any, null otherwise. | * @return {number} The diminishing return if any, null otherwise. | ||||
*/ | */ | ||||
ResourceSupply.prototype.GetDiminishingReturns = function() | ResourceSupply.prototype.GetDiminishingReturns = function() | ||||
{ | { | ||||
if (!this.template.DiminishingReturns) | if (!this.template.DiminishingReturns) | ||||
return null; | return null; | ||||
let diminishingReturns = ApplyValueModificationsToEntity("ResourceSupply/DiminishingReturns", +this.template.DiminishingReturns, this.entity); | |||||
if (!diminishingReturns) | |||||
return null; | |||||
let numGatherers = this.GetNumGatherers(); | let numGatherers = this.GetNumGatherers(); | ||||
if (numGatherers > 1) | if (numGatherers > 1) | ||||
return diminishingReturns == 1 ? 1 : (1 - Math.pow(diminishingReturns, numGatherers)) / (1 - diminishingReturns) / numGatherers; | return diminishingReturns == 1 ? 1 : (1 - Math.pow(this.diminishingReturns, numGatherers)) / (1 - this.diminishingReturns) / numGatherers; | ||||
return null; | return null; | ||||
}; | }; | ||||
/** | /** | ||||
* @param {number} amount The amount of resources that should be taken from the resource supply. The amount must be positive. | * @param {number} amount The amount of resources that should be taken from the resource supply. The amount must be positive. | ||||
* @return {{ "amount": number, "exhausted": boolean }} The current resource amount in the entity and whether it's exhausted or not. | * @return {{ "amount": number, "exhausted": boolean }} The current resource amount in the entity and whether it's exhausted or not. | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | ResourceSupply.prototype.RemoveGatherer = function(gathererID, player) | ||||
if (index == -1) | if (index == -1) | ||||
return; | return; | ||||
this.gatherers[player].splice(index, 1); | this.gatherers[player].splice(index, 1); | ||||
// Broadcast message, mainly useful for the AIs. | // Broadcast message, mainly useful for the AIs. | ||||
Engine.PostMessage(this.entity, MT_ResourceSupplyNumGatherersChanged, { "to": this.GetNumGatherers() }); | Engine.PostMessage(this.entity, MT_ResourceSupplyNumGatherersChanged, { "to": this.GetNumGatherers() }); | ||||
}; | }; | ||||
ResourceSupply.prototype.OnValueModification = function(msg) | |||||
{ | |||||
if (msg.component != "ResourceSupply") | |||||
return; | |||||
this.UpdateProperties(); | |||||
}; | |||||
ResourceSupply.prototype.OnOwnershipChanged = function(msg) | |||||
{ | |||||
if (msg.to == INVALID_PLAYER) | |||||
return; | |||||
this.UpdateProperties(); | |||||
}; | |||||
Engine.RegisterComponentType(IID_ResourceSupply, "ResourceSupply", ResourceSupply); | Engine.RegisterComponentType(IID_ResourceSupply, "ResourceSupply", ResourceSupply); |
Wildfire Games · Phabricator