Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/Player.js
Show First 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | Player.prototype.Init = function() | ||||||||||||
this.disabledTemplates = {}; | this.disabledTemplates = {}; | ||||||||||||
this.disabledTechnologies = {}; | this.disabledTechnologies = {}; | ||||||||||||
this.spyCostMultiplier = +this.template.SpyCostMultiplier; | this.spyCostMultiplier = +this.template.SpyCostMultiplier; | ||||||||||||
this.barterEntities = []; | this.barterEntities = []; | ||||||||||||
this.barterMultiplier = { | this.barterMultiplier = { | ||||||||||||
"buy": clone(this.template.BarterMultiplier.Buy), | "buy": clone(this.template.BarterMultiplier.Buy), | ||||||||||||
"sell": clone(this.template.BarterMultiplier.Sell) | "sell": clone(this.template.BarterMultiplier.Sell) | ||||||||||||
}; | }; | ||||||||||||
this.idleUnits = new Map(); // {ClassList: {"units": [Unit], "lastUpdate": 0, "idleTime": 0}; | |||||||||||||
// Initial resources. | // Initial resources. | ||||||||||||
let resCodes = Resources.GetCodes(); | let resCodes = Resources.GetCodes(); | ||||||||||||
for (let res of resCodes) | for (let res of resCodes) | ||||||||||||
{ | { | ||||||||||||
this.resourceCount[res] = 300; | this.resourceCount[res] = 300; | ||||||||||||
this.resourceNames[res] = Resources.GetResource(res).name; | this.resourceNames[res] = Resources.GetResource(res).name; | ||||||||||||
this.resourceGatherers[res] = 0; | this.resourceGatherers[res] = 0; | ||||||||||||
▲ Show 20 Lines • Show All 693 Lines • ▼ Show 20 Lines | |||||||||||||
{ | { | ||||||||||||
if (msg.from != this.playerID && msg.to != this.playerID) | if (msg.from != this.playerID && msg.to != this.playerID) | ||||||||||||
return; | return; | ||||||||||||
let cmpCost = Engine.QueryInterface(msg.entity, IID_Cost); | let cmpCost = Engine.QueryInterface(msg.entity, IID_Cost); | ||||||||||||
if (msg.from == this.playerID) | if (msg.from == this.playerID) | ||||||||||||
{ | { | ||||||||||||
this.IdleUnitChanged(msg.entity); | |||||||||||||
FreagarachUnsubmitted Not Done Inline Actions
Freagarach: | |||||||||||||
if (cmpCost) | if (cmpCost) | ||||||||||||
this.popUsed -= cmpCost.GetPopCost(); | this.popUsed -= cmpCost.GetPopCost(); | ||||||||||||
let panelIndex = this.panelEntities.indexOf(msg.entity); | let panelIndex = this.panelEntities.indexOf(msg.entity); | ||||||||||||
if (panelIndex >= 0) | if (panelIndex >= 0) | ||||||||||||
this.panelEntities.splice(panelIndex, 1); | this.panelEntities.splice(panelIndex, 1); | ||||||||||||
let barterIndex = this.barterEntities.indexOf(msg.entity); | let barterIndex = this.barterEntities.indexOf(msg.entity); | ||||||||||||
if (barterIndex >= 0) | if (barterIndex >= 0) | ||||||||||||
this.barterEntities.splice(barterIndex, 1); | this.barterEntities.splice(barterIndex, 1); | ||||||||||||
} | } | ||||||||||||
if (msg.to == this.playerID) | if (msg.to == this.playerID) | ||||||||||||
{ | { | ||||||||||||
this.IdleUnitChanged(msg.entity); | |||||||||||||
if (cmpCost) | if (cmpCost) | ||||||||||||
this.popUsed += cmpCost.GetPopCost(); | this.popUsed += cmpCost.GetPopCost(); | ||||||||||||
let cmpIdentity = Engine.QueryInterface(msg.entity, IID_Identity); | let cmpIdentity = Engine.QueryInterface(msg.entity, IID_Identity); | ||||||||||||
if (!cmpIdentity) | if (!cmpIdentity) | ||||||||||||
return; | return; | ||||||||||||
if (MatchesClassList(cmpIdentity.GetClassesList(), panelEntityClasses)) | if (MatchesClassList(cmpIdentity.GetClassesList(), panelEntityClasses)) | ||||||||||||
this.panelEntities.push(msg.entity); | this.panelEntities.push(msg.entity); | ||||||||||||
if (cmpIdentity.HasClass("Barter") && !Engine.QueryInterface(msg.entity, IID_Foundation)) | if (cmpIdentity.HasClass("Barter") && !Engine.QueryInterface(msg.entity, IID_Foundation)) | ||||||||||||
this.barterEntities.push(msg.entity); | this.barterEntities.push(msg.entity); | ||||||||||||
} | } | ||||||||||||
}; | }; | ||||||||||||
Player.prototype.IsOwnedIdleUnit = function(entity) | |||||||||||||
Not Done Inline Actions
Freagarach: | |||||||||||||
{ | |||||||||||||
const cmpOwnership = Engine.QueryInterface(entity, IID_Ownership); | |||||||||||||
if (!cmpOwnership || cmpOwnership.GetOwner() != this.GetPlayerID()) | |||||||||||||
return false; | |||||||||||||
const cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI); | |||||||||||||
if (!cmpUnitAI || !cmpUnitAI.IsIdle()) | |||||||||||||
return false; | |||||||||||||
const cmpGarrisonable = Engine.QueryInterface(entity, IID_Garrisonable); | |||||||||||||
if (cmpGarrisonable && cmpGarrisonable.IsGarrisoned()) | |||||||||||||
return false; | |||||||||||||
const cmpTurretable = Engine.QueryInterface(entity, IID_Turretable); | |||||||||||||
if (cmpTurretable && cmpTurretable.IsTurreted()) | |||||||||||||
return false; | |||||||||||||
return true; | |||||||||||||
}; | |||||||||||||
Player.prototype.IdleUnitsByClass = function(matchClasses) | |||||||||||||
{ | |||||||||||||
// Transform the string to an array | |||||||||||||
Not Done Inline Actionsprefer === as it's slightly faster for this type of check. wraitii: prefer === as it's slightly faster for this type of check. | |||||||||||||
if (typeof matchClasses === "string") | |||||||||||||
matchClasses = matchClasses.split(/\s+/); | |||||||||||||
const units = []; | |||||||||||||
Not Done Inline Actions
(At more places.) Freagarach: (At more places.) | |||||||||||||
for (const unitType of this.idleUnits.values()) | |||||||||||||
for (let i = 0; i < matchClasses.length; ++i) | |||||||||||||
if(MatchesClassList(unitType.classes, [matchClasses[i]])) | |||||||||||||
Not Done Inline ActionsWhy the double braces? Freagarach: Why the double braces? | |||||||||||||
SilierUnsubmitted Not Done Inline Actions
Silier: | |||||||||||||
{ | |||||||||||||
units.push(...unitType.units); | |||||||||||||
break; | |||||||||||||
} | |||||||||||||
return units; | |||||||||||||
}; | |||||||||||||
Player.prototype.CountIdleUnits = function(matchClasses) | |||||||||||||
{ | |||||||||||||
let count = 0; | |||||||||||||
for (const unitType of this.idleUnits.values()) | |||||||||||||
if(MatchesClassList(unitType.classes, matchClasses)) | |||||||||||||
SilierUnsubmitted Not Done Inline Actions
Silier: | |||||||||||||
count += unitType.units.size; | |||||||||||||
return count; | |||||||||||||
}; | |||||||||||||
Player.prototype.GetIdleTime = function(matchClasses) | |||||||||||||
{ | |||||||||||||
const cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | |||||||||||||
const time = cmpTimer.GetTime(); | |||||||||||||
Not Done Inline ActionsWe don't do this kind of functions in a component. This is small enough to be inlined. Freagarach: We don't do this kind of functions in a component. This is small enough to be inlined. | |||||||||||||
let idleTime = 0; | |||||||||||||
for(const unitType of this.idleUnits.values()) | |||||||||||||
SilierUnsubmitted Not Done Inline Actions
Silier: | |||||||||||||
if(MatchesClassList(unitType.classes, matchClasses)) | |||||||||||||
SilierUnsubmitted Not Done Inline Actions
Silier: | |||||||||||||
{ | |||||||||||||
unitType.idleTime += (time - unitType.lastUpdate) * unitType.units.size; | |||||||||||||
unitType.lastUpdate = time; | |||||||||||||
Not Done Inline ActionsIdem. Freagarach: Idem. | |||||||||||||
idleTime += unitType.idleTime; | |||||||||||||
} | |||||||||||||
return idleTime; | |||||||||||||
}; | |||||||||||||
Player.prototype.IdleUnitChanged = function(entity) | |||||||||||||
{ | |||||||||||||
const cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | |||||||||||||
const time = cmpTimer.GetTime(); | |||||||||||||
const cmpIdentity = Engine.QueryInterface(entity, IID_Identity); | |||||||||||||
SilierUnsubmitted Not Done Inline Actionsthis could go as first with the check below, so we can avoid querying timer Silier: this could go as first with the check below, so we can avoid querying timer | |||||||||||||
if (!cmpIdentity) | |||||||||||||
return; | |||||||||||||
const classesList = cmpIdentity.GetClassesList(); | |||||||||||||
const key = classesList.join(" "); | |||||||||||||
const idleUnitData = this.idleUnits.get(key); | |||||||||||||
if(this.IsOwnedIdleUnit(entity)) | |||||||||||||
SilierUnsubmitted Not Done Inline Actions
Silier: | |||||||||||||
{ | |||||||||||||
if(idleUnitData === undefined) | |||||||||||||
SilierUnsubmitted Not Done Inline Actions
Silier: | |||||||||||||
SilierUnsubmitted Not Done Inline ActionsAlso I would suggest to put this into braces because the else branch just for better readablility Silier: Also I would suggest to put this into braces because the `else` branch just for better… | |||||||||||||
this.idleUnits.set(key, { | |||||||||||||
"units": new Set([entity]), | |||||||||||||
"lastUpdate": time, | |||||||||||||
"classes": classesList, | |||||||||||||
"idleTime": 0 | |||||||||||||
}); | |||||||||||||
else | |||||||||||||
{ | |||||||||||||
idleUnitData.idleTime += (time - idleUnitData.lastUpdate) * idleUnitData.units.size; | |||||||||||||
idleUnitData.lastUpdate = time; | |||||||||||||
idleUnitData.units.add(entity); | |||||||||||||
} | |||||||||||||
} | |||||||||||||
else if(idleUnitData !== undefined && idleUnitData.units.has(entity)) | |||||||||||||
SilierUnsubmitted Not Done Inline Actions
Silier: | |||||||||||||
{ | |||||||||||||
idleUnitData.idleTime += (time - idleUnitData.lastUpdate) * idleUnitData.units.size; | |||||||||||||
idleUnitData.lastUpdate = time; | |||||||||||||
idleUnitData.units.delete(entity); | |||||||||||||
} | |||||||||||||
}; | |||||||||||||
Player.prototype.OnGlobalUnitIdleChanged = function(msg) | |||||||||||||
{ | |||||||||||||
this.IdleUnitChanged(msg.entity); | |||||||||||||
}; | |||||||||||||
FreagarachUnsubmitted Not Done Inline Actions
E.g. this ^ to profile. Freagarach: E.g. this ^ to profile. | |||||||||||||
SilierUnsubmitted Not Done Inline Actionswrong placement :P Silier: wrong placement :P
Better inside `IdleUnitChanged` | |||||||||||||
Player.prototype.OnGlobalGarrisonedStateChanged = function(msg) | |||||||||||||
{ | |||||||||||||
this.IdleUnitChanged(msg.entity); | |||||||||||||
}; | |||||||||||||
Player.prototype.OnGlobalTurretedStateChanged= function(msg) | |||||||||||||
{ | |||||||||||||
this.IdleUnitChanged(msg.entity); | |||||||||||||
}; | |||||||||||||
FreagarachUnsubmitted Not Done Inline ActionsWhy do you listen for these messages? Units that garrison or turret a position will send the idle message anyway? Freagarach: Why do you listen for these messages? Units that garrison or turret a position will send the… | |||||||||||||
Player.prototype.OnResearchFinished = function(msg) | Player.prototype.OnResearchFinished = function(msg) | ||||||||||||
{ | { | ||||||||||||
if (msg.tech == this.template.SharedLosTech) | if (msg.tech == this.template.SharedLosTech) | ||||||||||||
this.UpdateSharedLos(); | this.UpdateSharedLos(); | ||||||||||||
else if (msg.tech == this.template.SharedDropsitesTech) | else if (msg.tech == this.template.SharedDropsitesTech) | ||||||||||||
this.sharedDropsites = true; | this.sharedDropsites = true; | ||||||||||||
}; | }; | ||||||||||||
▲ Show 20 Lines • Show All 140 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator