forked from 0ad/0ad
Use Maps instead of Objects in the Auramanager to improve performance. Also remove the IonMonkey v24 workaround. Refs #2684
This was SVN commit r16512.
This commit is contained in:
parent
481cf0f754
commit
b0c74699b0
@ -5,45 +5,55 @@ AuraManager.prototype.Schema =
|
||||
|
||||
AuraManager.prototype.Init = function()
|
||||
{
|
||||
this.modificationsCache = {};
|
||||
this.modifications = {};
|
||||
this.templateModificationsCache = {};
|
||||
this.templateModifications = {};
|
||||
this.modificationsCache = new Map();
|
||||
this.modifications = new Map();
|
||||
this.templateModificationsCache = new Map();
|
||||
this.templateModifications = new Map();
|
||||
};
|
||||
|
||||
AuraManager.prototype.ensureExists = function(name, value, id, key, defaultData)
|
||||
{
|
||||
if (!this[name][value])
|
||||
var cacheName = name + "Cache";
|
||||
var v = this[name].get(value);
|
||||
if (!v)
|
||||
{
|
||||
this[name][value] = {};
|
||||
this[name+'Cache'][value] = {};
|
||||
v = new Map();
|
||||
this[name].set(value, v);
|
||||
this[cacheName].set(value, new Map());
|
||||
}
|
||||
|
||||
if (!this[name][value][id])
|
||||
var i = v.get(id);
|
||||
if (!i)
|
||||
{
|
||||
this[name][value][id] = {};
|
||||
this[name+'Cache'][value][id] = defaultData;
|
||||
i = new Map();
|
||||
v.set(id, i);
|
||||
this[cacheName].get(value).set(id, defaultData);
|
||||
}
|
||||
|
||||
if (!this[name][value][id][key])
|
||||
this[name][value][id][key] = [];
|
||||
var k = i.get(key);
|
||||
if (!k)
|
||||
{
|
||||
k = [];
|
||||
i.set(key, k);
|
||||
}
|
||||
return k;
|
||||
};
|
||||
|
||||
AuraManager.prototype.ApplyBonus = function(value, ent, data, key)
|
||||
{
|
||||
this.ensureExists("modifications", value, ent, key, {"add":0, "multiply":1});
|
||||
var dataList = this.ensureExists("modifications", value, ent, key, {"add":0, "multiply":1});
|
||||
|
||||
this.modifications[value][ent][key].push(data);
|
||||
dataList.push(data);
|
||||
|
||||
if (this.modifications[value][ent][key].length > 1)
|
||||
if (dataList.length > 1)
|
||||
return;
|
||||
|
||||
// first time added this aura
|
||||
if (data.multiply)
|
||||
this.modificationsCache[value][ent].multiply *= data.multiply;
|
||||
this.modificationsCache.get(value).get(ent).multiply *= data.multiply;
|
||||
|
||||
if (data.add)
|
||||
this.modificationsCache[value][ent].add += data.add;
|
||||
this.modificationsCache.get(value).get(ent).add *= data.add;
|
||||
|
||||
// post message to the entity to notify it about the change
|
||||
Engine.PostMessage(ent, MT_ValueModification, { "entities": [ent], "component": value.split("/")[0], "valueNames": [value] });
|
||||
@ -51,125 +61,127 @@ AuraManager.prototype.ApplyBonus = function(value, ent, data, key)
|
||||
|
||||
AuraManager.prototype.ApplyTemplateBonus = function(value, player, classes, data, key)
|
||||
{
|
||||
this.ensureExists("templateModifications", value, player, key, {});
|
||||
var dataList = this.ensureExists("templateModifications", value, player, key, new Map());
|
||||
|
||||
this.templateModifications[value][player][key].push(data);
|
||||
dataList.push(data);
|
||||
|
||||
if (this.templateModifications[value][player][key].length > 1)
|
||||
if (dataList.length > 1)
|
||||
return;
|
||||
|
||||
// first time added this aura
|
||||
for each (var c in classes)
|
||||
for (let c of classes)
|
||||
{
|
||||
if (!this.templateModificationsCache[value][player][c])
|
||||
this.templateModificationsCache[value][player][c] = [];
|
||||
let cache = this.templateModificationsCache.get(value).get(player);
|
||||
if (!cache.get(c))
|
||||
cache.set(c, new Map());
|
||||
|
||||
if (!this.templateModificationsCache[value][player][c][key])
|
||||
this.templateModificationsCache[value][player][c][key] = { "add": 0, "multiply": 1};
|
||||
if (!cache.get(c).get(key))
|
||||
cache.get(c).set(key, { "add": 0, "multiply": 1});
|
||||
|
||||
if (data.multiply)
|
||||
this.templateModificationsCache[value][player][c][key].multiply *= data.multiply;
|
||||
cache.get(c).get(key).multiply *= data.multiply;
|
||||
|
||||
if (data.add)
|
||||
this.templateModificationsCache[value][player][c][key].add += data.add;
|
||||
|
||||
Engine.PostMessage(SYSTEM_ENTITY, MT_TemplateModification, { "player": player, "component": value.split("/")[0], "valueNames": [value] });
|
||||
cache.get(c).get(key).add += data.add;
|
||||
}
|
||||
Engine.PostMessage(SYSTEM_ENTITY, MT_TemplateModification, { "player": player, "component": value.split("/")[0], "valueNames": [value] });
|
||||
};
|
||||
|
||||
AuraManager.prototype.RemoveBonus = function(value, ent, key)
|
||||
{
|
||||
if (!this.modifications[value] ||
|
||||
!this.modifications[value][ent] ||
|
||||
!this.modifications[value][ent][key] ||
|
||||
!this.modifications[value][ent][key].length)
|
||||
var v = this.modifications.get(value);
|
||||
if (!v)
|
||||
return;
|
||||
var e = v.get(ent);
|
||||
if (!e)
|
||||
return;
|
||||
var dataList = e.get(key);
|
||||
if (!dataList || !dataList.length)
|
||||
return;
|
||||
|
||||
// get the applied data to remove again
|
||||
var data = this.modifications[value][ent][key].pop();
|
||||
var data = dataList.pop();
|
||||
|
||||
if (this.modifications[value][ent][key].length > 0)
|
||||
if (dataList.length > 0)
|
||||
return;
|
||||
|
||||
// out of last aura of this kind, remove modifications
|
||||
if (data.add)
|
||||
this.modificationsCache[value][ent].add -= data.add;
|
||||
this.modificationsCache.get(value).get(ent).add -= data.add;
|
||||
|
||||
if (data.multiply)
|
||||
this.modificationsCache[value][ent].multiply /= data.multiply;
|
||||
this.modificationsCache.get(value).get(ent).multiply /= data.multiply;
|
||||
|
||||
// post message to the entity to notify it about the change
|
||||
var effects = {};
|
||||
effects[value] = this.modificationsCache[value][ent];
|
||||
Engine.PostMessage(ent, MT_ValueModification, { "entities": [ent], "component": value.split("/")[0], "valueNames": [value] });
|
||||
};
|
||||
|
||||
AuraManager.prototype.RemoveTemplateBonus = function(value, player, classes, key)
|
||||
{
|
||||
if (!this.templateModifications[value] ||
|
||||
!this.templateModifications[value][player] ||
|
||||
!this.templateModifications[value][player][key] ||
|
||||
!this.templateModifications[value][player][key].length)
|
||||
var v = this.templateModifications.get(value);
|
||||
if (!v)
|
||||
return;
|
||||
var p = v.get(player);
|
||||
if (!p)
|
||||
return;
|
||||
var dataList = p.get(key);
|
||||
if (!dataList || !dataList.length)
|
||||
return;
|
||||
|
||||
this.templateModifications[value][player][key].pop();
|
||||
dataList.pop();
|
||||
|
||||
if (this.templateModifications[value][player][key].length > 0)
|
||||
if (dataList.length > 0)
|
||||
return;
|
||||
|
||||
for each (var c in classes)
|
||||
{
|
||||
this.templateModificationsCache[value][player][c][key].multiply = 1;
|
||||
this.templateModificationsCache[value][player][c][key].add = 0;
|
||||
}
|
||||
for (let c of classes)
|
||||
this.templateModificationsCache.get(value).get(player).get(c).delete(key);
|
||||
|
||||
Engine.PostMessage(SYSTEM_ENTITY, MT_TemplateModification, { "player": player, "component": value.split("/")[0], "valueNames": [value] });
|
||||
};
|
||||
|
||||
AuraManager.prototype.ApplyModifications = function(valueName, value, ent)
|
||||
{
|
||||
// FIXME Due do a bug in IonMonkey v24, we must interprete this method
|
||||
// When we upgrade to v31, remove the try-catch line.
|
||||
// See #2684
|
||||
try {} catch(e) {}
|
||||
|
||||
if (!this.modificationsCache[valueName] || !this.modificationsCache[valueName][ent])
|
||||
var v = this.modificationsCache.get(valueName)
|
||||
if (!v)
|
||||
return value;
|
||||
var cache = v.get(ent);
|
||||
if (!cache)
|
||||
return value;
|
||||
|
||||
value *= this.modificationsCache[valueName][ent].multiply;
|
||||
value += this.modificationsCache[valueName][ent].add;
|
||||
value *= cache.multiply;
|
||||
value += cache.add;
|
||||
return value;
|
||||
};
|
||||
|
||||
AuraManager.prototype.ApplyTemplateModifications = function(valueName, value, player, template)
|
||||
{
|
||||
// FIXME Due do a bug in IonMonkey v24, we must interprete this method
|
||||
// When we upgrade to v31, remove the try-catch line.
|
||||
// See #2684
|
||||
try {} catch(e) {}
|
||||
|
||||
if (!this.templateModificationsCache[valueName] || !this.templateModificationsCache[valueName][player])
|
||||
var v = this.templateModificationsCache.get(valueName)
|
||||
if (!v)
|
||||
return value;
|
||||
var cache = v.get(player);
|
||||
if (!cache)
|
||||
return value;
|
||||
|
||||
var classes = [];
|
||||
if (template && template.Identity)
|
||||
classes = GetIdentityClasses(template.Identity);
|
||||
if (!template || !template.Identity)
|
||||
return value;
|
||||
var classes = GetIdentityClasses(template.Identity);
|
||||
|
||||
var keyList = [];
|
||||
var usedKeys = new Set();
|
||||
|
||||
for (var c in this.templateModificationsCache[valueName][player])
|
||||
for (let c in cache.keys())
|
||||
{
|
||||
if (classes.indexOf(c) == -1)
|
||||
continue;
|
||||
|
||||
for (var key in this.templateModificationsCache[valueName][player][c])
|
||||
for (let key in cache.get(c).keys())
|
||||
{
|
||||
// don't add an aura with the same key twice
|
||||
if (keyList.indexOf(key) != -1)
|
||||
if (usedKeys.has(key))
|
||||
continue;
|
||||
|
||||
value *= this.templateModificationsCache[valueName][player][c][key].multiply;
|
||||
value += this.templateModificationsCache[valueName][player][c][key].add;
|
||||
keyList.push(key);
|
||||
value *= cache.get(c).get(key).multiply;
|
||||
value += cache.get(c).get(key).add;
|
||||
usedKeys.add(key);
|
||||
}
|
||||
}
|
||||
return value;
|
||||
|
Loading…
Reference in New Issue
Block a user