Differential D960 Diff 4008 ps/trunk/binaries/data/mods/public/simulation/components/EntityLimits.js
Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/components/EntityLimits.js
Show First 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | "<zeroOrMore>" + | ||||
"</element>" + | "</element>" + | ||||
"</oneOrMore>" + | "</oneOrMore>" + | ||||
"</element>" + | "</element>" + | ||||
"</zeroOrMore>" + | "</zeroOrMore>" + | ||||
"</element>"; | "</element>"; | ||||
const TRAINING = "training"; | const TRAINING = "training"; | ||||
const BUILD = "build"; | const BUILD = "build"; | ||||
elexis: (Perhaps these two would be better off as prototype constants as they are available in the… | |||||
fatherbushidoAuthorUnsubmitted Not Done Inline ActionsPersonally I just don't see why they exist. fatherbushido: Personally I just don't see why they exist. | |||||
EntityLimits.prototype.Init = function() | EntityLimits.prototype.Init = function() | ||||
{ | { | ||||
this.limit = {}; | this.limit = {}; | ||||
this.count = {}; // counts entities which change the limit of the given category | this.count = {}; // counts entities which change the limit of the given category | ||||
this.changers = {}; | this.changers = {}; | ||||
this.removers = {}; | this.removers = {}; | ||||
this.classCount = {}; // counts entities with the given class, used in the limit removal | this.classCount = {}; // counts entities with the given class, used in the limit removal | ||||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | for (var category in this.removers) | ||||
if (nolimit && this.limit[category] !== undefined) | if (nolimit && this.limit[category] !== undefined) | ||||
this.limit[category] = undefined; | this.limit[category] = undefined; | ||||
else if (!nolimit && this.limit[category] === undefined) | else if (!nolimit && this.limit[category] === undefined) | ||||
this.limit[category] = this.removedLimit[category]; | this.limit[category] = this.removedLimit[category]; | ||||
} | } | ||||
}; | }; | ||||
EntityLimits.prototype.AllowedToCreate = function(limitType, category, count) | EntityLimits.prototype.AllowedToCreate = function(limitType, category, count) | ||||
{ | { | ||||
// Allow unspecified categories and those with no limit | // Allow unspecified categories and those with no limit | ||||
if (this.count[category] === undefined || this.limit[category] === undefined) | if (this.count[category] === undefined || this.limit[category] === undefined) | ||||
return true; | return true; | ||||
if (this.count[category] + count > this.limit[category]) | if (this.count[category] + count > this.limit[category]) | ||||
{ | { | ||||
Show All 30 Lines | EntityLimits.prototype.AllowedToBuild = function(category) | ||||
return this.AllowedToCreate(BUILD, category, 0); | return this.AllowedToCreate(BUILD, category, 0); | ||||
}; | }; | ||||
EntityLimits.prototype.AllowedToTrain = function(category, count) | EntityLimits.prototype.AllowedToTrain = function(category, count) | ||||
{ | { | ||||
return this.AllowedToCreate(TRAINING, category, count); | return this.AllowedToCreate(TRAINING, category, count); | ||||
}; | }; | ||||
/** | |||||
* @param {number} ent - id of the entity which would be replaced. | |||||
* @param {string} template - name of the new template. | |||||
* @return {boolean} - whether we can replace ent. | |||||
*/ | |||||
EntityLimits.prototype.AllowedToReplace = function(ent, template) | |||||
{ | |||||
let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); | |||||
let templateFrom = cmpTemplateManager.GetTemplate(cmpTemplateManager.GetCurrentTemplateName(ent)); | |||||
let templateTo = cmpTemplateManager.GetTemplate(template); | |||||
if (templateTo.TrainingRestrictions) | |||||
{ | |||||
let category = templateTo.TrainingRestrictions.Category; | |||||
return this.AllowedToCreate(TRAINING, category, templateFrom.TrainingRestrictions && templateFrom.TrainingRestrictions.Category == category ? 0 : 1); | |||||
} | |||||
if (templateTo.BuildRestrictions) | |||||
{ | |||||
let category = templateTo.BuildRestrictions.Category; | |||||
return this.AllowedToCreate(BUILD, category, templateFrom.BuildRestrictions && templateFrom.BuildRestrictions.Category == category ? 0 : 1); | |||||
} | |||||
return true; | |||||
}; | |||||
EntityLimits.prototype.OnGlobalOwnershipChanged = function(msg) | EntityLimits.prototype.OnGlobalOwnershipChanged = function(msg) | ||||
{ | { | ||||
// check if we are adding or removing an entity from this player | // check if we are adding or removing an entity from this player | ||||
var cmpPlayer = Engine.QueryInterface(this.entity, IID_Player); | var cmpPlayer = Engine.QueryInterface(this.entity, IID_Player); | ||||
if (!cmpPlayer) | if (!cmpPlayer) | ||||
{ | { | ||||
error("EntityLimits component is defined on a non-player entity"); | error("EntityLimits component is defined on a non-player entity"); | ||||
return; | return; | ||||
▲ Show 20 Lines • Show All 49 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
(Perhaps these two would be better off as prototype constants as they are available in the entire simulation context. Also the name sounds improveable, something like EntityLimitTypeTraining or so.)