Differential D4241 Diff 18796 ps/trunk/binaries/data/mods/public/simulation/components/ProductionQueue.js
Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/components/ProductionQueue.js
Show First 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | ProductionQueue.prototype.Init = function() | ||||
/** | /** | ||||
Queue items are: | Queue items are: | ||||
{ | { | ||||
"id": 1, | "id": 1, | ||||
"player": 1, // Who paid for this batch; we need this to cope with refunds cleanly. | "player": 1, // Who paid for this batch; we need this to cope with refunds cleanly. | ||||
"productionStarted": false, // true iff production has started (we have reserved population). | "productionStarted": false, // true iff production has started (we have reserved population). | ||||
"timeTotal": 15000, // msecs | "timeTotal": 15000, // msecs | ||||
"timeRemaining": 10000, // msecs | "timeRemaining": 10000, // msecs | ||||
"paused": false, // Whether the item is currently paused (e.g. not the first item or parent is garrisoned). | |||||
"resources": { "wood": 100, ... }, // Total resources of the item when queued. | "resources": { "wood": 100, ... }, // Total resources of the item when queued. | ||||
"entity": { | "entity": { | ||||
"template": "units/example", | "template": "units/example", | ||||
"count": 10, | "count": 10, | ||||
"neededSlots": 3, // Number of population slots missing for production to begin. | "neededSlots": 3, // Number of population slots missing for production to begin. | ||||
"population": 1, // Population per unit, multiply by count to get total. | "population": 1, // Population per unit, multiply by count to get total. | ||||
"resources": { "wood": 100, ... }, // Resources per entity, multiply by count to get total. | "resources": { "wood": 100, ... }, // Resources per entity, multiply by count to get total. | ||||
"entityCache": [189, ...], // The entities created but not spawned yet. | "entityCache": [189, ...], // The entities created but not spawned yet. | ||||
▲ Show 20 Lines • Show All 277 Lines • ▼ Show 20 Lines | |||||
}; | }; | ||||
/* | /* | ||||
* Adds a new batch of identical units to train or a technology to research to the production queue. | * Adds a new batch of identical units to train or a technology to research to the production queue. | ||||
* @param {string} templateName - The template to start production on. | * @param {string} templateName - The template to start production on. | ||||
* @param {string} type - The type of production (i.e. "unit" or "technology"). | * @param {string} type - The type of production (i.e. "unit" or "technology"). | ||||
* @param {number} count - The amount of units to be produced. Ignored for a tech. | * @param {number} count - The amount of units to be produced. Ignored for a tech. | ||||
* @param {any} metadata - Optionally any metadata to be attached to the item. | * @param {any} metadata - Optionally any metadata to be attached to the item. | ||||
* @param {boolean} pushFront - Whether to push the item to the front of the queue and pause any item(s) currently in progress. | |||||
* | * | ||||
* @return {boolean} - Whether the addition of the item has succeeded. | * @return {boolean} - Whether the addition of the item has succeeded. | ||||
*/ | */ | ||||
ProductionQueue.prototype.AddItem = function(templateName, type, count, metadata) | ProductionQueue.prototype.AddItem = function(templateName, type, count, metadata, pushFront = false) | ||||
{ | { | ||||
// TODO: there should be a way for the GUI to determine whether it's going | // TODO: there should be a way for the GUI to determine whether it's going | ||||
// to be possible to add a batch (based on resource costs and length limits). | // to be possible to add a batch (based on resource costs and length limits). | ||||
let cmpPlayer = QueryOwnerInterface(this.entity); | let cmpPlayer = QueryOwnerInterface(this.entity); | ||||
if (!cmpPlayer) | if (!cmpPlayer) | ||||
return false; | return false; | ||||
let player = cmpPlayer.GetPlayerID(); | let player = cmpPlayer.GetPlayerID(); | ||||
Show All 22 Lines | else if (this.queue.length >= this.MaxQueueSize) | ||||
return false; | return false; | ||||
} | } | ||||
const item = { | const item = { | ||||
"player": player, | "player": player, | ||||
"metadata": metadata, | "metadata": metadata, | ||||
"productionStarted": false, | "productionStarted": false, | ||||
"resources": {}, // The total resource costs. | "resources": {}, // The total resource costs. | ||||
"paused": false | |||||
}; | }; | ||||
// ToDo: Still some duplication here, some can might be combined, | // ToDo: Still some duplication here, some can might be combined, | ||||
// but requires some more refactoring. | // but requires some more refactoring. | ||||
if (type == "unit") | if (type == "unit") | ||||
{ | { | ||||
if (!Number.isInteger(count) || count <= 0) | if (!Number.isInteger(count) || count <= 0) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | else | ||||
return false; | return false; | ||||
} | } | ||||
// TrySubtractResources should report error to player (they ran out of resources). | // TrySubtractResources should report error to player (they ran out of resources). | ||||
if (!cmpPlayer.TrySubtractResources(item.resources)) | if (!cmpPlayer.TrySubtractResources(item.resources)) | ||||
return false; | return false; | ||||
item.id = this.nextID++; | item.id = this.nextID++; | ||||
if (pushFront) | |||||
{ | |||||
if (this.queue[0]) | |||||
this.queue[0].paused = true; | |||||
this.queue.unshift(item); | |||||
} | |||||
else | |||||
this.queue.push(item); | this.queue.push(item); | ||||
const cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger); | const cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger); | ||||
if (item.entity) | if (item.entity) | ||||
cmpTrigger.CallEvent("OnTrainingQueued", { | cmpTrigger.CallEvent("OnTrainingQueued", { | ||||
"playerid": player, | "playerid": player, | ||||
"unitTemplate": item.entity.template, | "unitTemplate": item.entity.template, | ||||
"count": count, | "count": count, | ||||
"metadata": metadata, | "metadata": metadata, | ||||
▲ Show 20 Lines • Show All 111 Lines • ▼ Show 20 Lines | ProductionQueue.prototype.GetQueue = function() | ||||
return this.queue.map(item => ({ | return this.queue.map(item => ({ | ||||
"id": item.id, | "id": item.id, | ||||
"unitTemplate": item.entity?.template, | "unitTemplate": item.entity?.template, | ||||
"technologyTemplate": item.technology?.template, | "technologyTemplate": item.technology?.template, | ||||
"count": item.entity?.count, | "count": item.entity?.count, | ||||
"neededSlots": item.entity?.neededSlots, | "neededSlots": item.entity?.neededSlots, | ||||
"progress": 1 - (item.timeRemaining / (item.timeTotal || 1)), | "progress": 1 - (item.timeRemaining / (item.timeTotal || 1)), | ||||
"timeRemaining": item.timeRemaining, | "timeRemaining": item.timeRemaining, | ||||
"paused": item.paused, | |||||
"metadata": item.metadata | "metadata": item.metadata | ||||
})); | })); | ||||
}; | }; | ||||
/* | /* | ||||
* Removes all existing batches from the queue. | * Removes all existing batches from the queue. | ||||
*/ | */ | ||||
ProductionQueue.prototype.ResetQueue = function() | ProductionQueue.prototype.ResetQueue = function() | ||||
▲ Show 20 Lines • Show All 163 Lines • ▼ Show 20 Lines | ProductionQueue.prototype.ProgressTimeout = function(data, lateness) | ||||
// until we've used up all the time (so that we work accurately | // until we've used up all the time (so that we work accurately | ||||
// with items that take fractions of a second). | // with items that take fractions of a second). | ||||
let time = this.ProgressInterval + lateness; | let time = this.ProgressInterval + lateness; | ||||
let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); | let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); | ||||
while (this.queue.length) | while (this.queue.length) | ||||
{ | { | ||||
let item = this.queue[0]; | let item = this.queue[0]; | ||||
if (item.paused) | |||||
item.paused = false; | |||||
if (!item.productionStarted) | if (!item.productionStarted) | ||||
{ | { | ||||
if (item.entity) | if (item.entity) | ||||
{ | { | ||||
const template = cmpTemplateManager.GetTemplate(item.entity.template); | const template = cmpTemplateManager.GetTemplate(item.entity.template); | ||||
item.entity.population = ApplyValueModificationsToTemplate( | item.entity.population = ApplyValueModificationsToTemplate( | ||||
"Cost/Population", | "Cost/Population", | ||||
+template.Cost.Population, | +template.Cost.Population, | ||||
▲ Show 20 Lines • Show All 111 Lines • ▼ Show 20 Lines | ProductionQueue.prototype.ProgressTimeout = function(data, lateness) | ||||
if (!this.queue.length) | if (!this.queue.length) | ||||
this.StopTimer(); | this.StopTimer(); | ||||
}; | }; | ||||
ProductionQueue.prototype.PauseProduction = function() | ProductionQueue.prototype.PauseProduction = function() | ||||
{ | { | ||||
this.StopTimer(); | this.StopTimer(); | ||||
this.paused = true; | this.paused = true; | ||||
if (this.queue[0]) | |||||
this.queue[0].paused = true; | |||||
}; | }; | ||||
ProductionQueue.prototype.UnpauseProduction = function() | ProductionQueue.prototype.UnpauseProduction = function() | ||||
{ | { | ||||
if (this.queue[0]) | |||||
this.queue[0].paused = false; | |||||
delete this.paused; | delete this.paused; | ||||
this.StartTimer(); | this.StartTimer(); | ||||
}; | }; | ||||
ProductionQueue.prototype.StartTimer = function() | ProductionQueue.prototype.StartTimer = function() | ||||
{ | { | ||||
if (this.timer) | if (this.timer) | ||||
return; | return; | ||||
▲ Show 20 Lines • Show All 63 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator