Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/ProductionQueue.js
Show First 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | ProductionQueue.prototype.Init = function() | ||||
// "timeRemaining": 10000, // msecs | // "timeRemaining": 10000, // msecs | ||||
// } | // } | ||||
this.timer = undefined; // g_ProgressInterval msec timer, active while the queue is non-empty | this.timer = undefined; // g_ProgressInterval msec timer, active while the queue is non-empty | ||||
this.paused = false; | this.paused = false; | ||||
this.entityCache = []; | this.entityCache = []; | ||||
this.spawnNotified = false; | this.spawnNotified = false; | ||||
this.compositionCount = {}; | |||||
this.compositionSpawned = {}; | |||||
this.compositionEntries = {}; | |||||
}; | }; | ||||
/* | /* | ||||
* Returns list of entities that can be trained by this building. | * Returns list of entities that can be trained by this building. | ||||
*/ | */ | ||||
ProductionQueue.prototype.GetEntitiesList = function() | ProductionQueue.prototype.GetEntitiesList = function() | ||||
{ | { | ||||
return this.entitiesList; | return this.entitiesList; | ||||
▲ Show 20 Lines • Show All 317 Lines • ▼ Show 20 Lines | |||||
*/ | */ | ||||
ProductionQueue.prototype.RemoveBatch = function(id) | ProductionQueue.prototype.RemoveBatch = function(id) | ||||
{ | { | ||||
// Destroy any cached entities (those which didn't spawn for some reason) | // Destroy any cached entities (those which didn't spawn for some reason) | ||||
for (let ent of this.entityCache) | for (let ent of this.entityCache) | ||||
Engine.DestroyEntity(ent); | Engine.DestroyEntity(ent); | ||||
this.entityCache = []; | this.entityCache = []; | ||||
this.compositionCount = {}; | |||||
this.compositionSpawned = {}; | |||||
this.compositionEntries = {}; | |||||
for (var i = 0; i < this.queue.length; ++i) | for (var i = 0; i < this.queue.length; ++i) | ||||
{ | { | ||||
var item = this.queue[i]; | var item = this.queue[i]; | ||||
if (item.id != id) | if (item.id != id) | ||||
continue; | continue; | ||||
// Now we've found the item to remove | // Now we've found the item to remove | ||||
▲ Show 20 Lines • Show All 309 Lines • ▼ Show 20 Lines | if (item.timeRemaining > time) | ||||
item.timeRemaining -= time; | item.timeRemaining -= time; | ||||
// send a message for the AIs. | // send a message for the AIs. | ||||
Engine.PostMessage(this.entity, MT_ProductionQueueChanged, { }); | Engine.PostMessage(this.entity, MT_ProductionQueueChanged, { }); | ||||
break; | break; | ||||
} | } | ||||
if (item.unitTemplate) | if (item.unitTemplate) | ||||
{ | { | ||||
var numSpawned = this.SpawnUnits(item.unitTemplate, item.count, item.metadata); | let cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); | ||||
template = cmpGuiInterface.GetTemplateData(item.player, item.unitTemplate); | |||||
let numSpawned = 0; | |||||
if (template.composition) | |||||
{ | |||||
if (Object.keys(this.compositionEntries).length == 0) | |||||
{ | |||||
this.compositionEntries = template.composition.Entries; | |||||
if (template.composition.MutuallyExclusive != "false") | |||||
{ | |||||
let weightedListOfTemplates = []; | |||||
for (let entry in this.compositionEntries) | |||||
{ | |||||
// Construct something of a weighted list. | |||||
let chance = +(this.compositionEntries[entry].Chance || 1); | |||||
for (let i = 0; i < chance; i++) | |||||
weightedListOfTemplates.push(this.compositionEntries[entry]); | |||||
} | |||||
this.compositionEntries = {}; | |||||
for (let i = 0; i < item.count; i++) | |||||
this.compositionEntries[i] = pickRandom(weightedListOfTemplates); | |||||
item.count = 1; | |||||
} | |||||
} | |||||
let finished = true; | |||||
for (let entry in this.compositionEntries) | |||||
{ | |||||
let amount = +this.compositionEntries[entry].Amount * item.count - (this.compositionSpawned[entry] || 0); | |||||
if (this.compositionCount[entry] == undefined) | |||||
{ | |||||
this.compositionCount[entry] = amount; | |||||
this.compositionSpawned[entry] = 0; | |||||
} | |||||
this.compositionSpawned[entry] += this.SpawnUnits(this.compositionEntries[entry].Template, amount, {}); | |||||
if (finished) | |||||
finished = this.compositionSpawned[entry] == this.compositionCount[entry]; | |||||
} | |||||
if (finished) | |||||
{ | |||||
numSpawned = item.count; | |||||
this.compositionCount = {}; | |||||
this.compositionSpawned = {}; | |||||
this.compositionEntries = {}; | |||||
} | |||||
} | |||||
else | |||||
numSpawned = this.SpawnUnits(item.unitTemplate, item.count, item.metadata); | |||||
if (numSpawned == item.count) | if (numSpawned == item.count) | ||||
{ | { | ||||
// All entities spawned, this batch finished | // All entities spawned, this batch finished | ||||
cmpPlayer.UnReservePopulationSlots(item.population * numSpawned); | cmpPlayer.UnReservePopulationSlots(item.population * numSpawned); | ||||
time -= item.timeRemaining; | time -= item.timeRemaining; | ||||
this.queue.shift(); | this.queue.shift(); | ||||
// Unset flag that training is blocked | // Unset flag that training is blocked | ||||
cmpPlayer.UnBlockTraining(); | cmpPlayer.UnBlockTraining(); | ||||
▲ Show 20 Lines • Show All 96 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator