Ticket #3164: aura_manager_maps.patch

File aura_manager_maps.patch, 6.7 KB (added by Itms, 9 years ago)

Also includes some reordering of add/multiply to avoid any ordering mistake

  • binaries/data/mods/public/simulation/components/AuraManager.js

     
    55
    66AuraManager.prototype.Init = function()
    77{
    8     this.modificationsCache = {};
    9     this.modifications = {};
    10     this.templateModificationsCache = {};
    11     this.templateModifications = {};
     8    this.modificationsCache = new Map();
     9    this.modifications = new Map();
     10    this.templateModificationsCache = new Map();
     11    this.templateModifications = new Map();
    1212};
    1313
    1414AuraManager.prototype.ensureExists = function(name, value, id, key, defaultData)
    1515{
    1616    var cacheName = name + "Cache";
    17     var v = this[name][value];
     17    var v = this[name].get(value);
    1818    if (!v)
    1919    {
    20         v = {};
    21         this[name][value] = v;
    22         this[cacheName][value] = {};
     20        v = new Map();
     21        this[name].set(value, v);
     22        this[cacheName].set(value, new Map());
    2323    }
    2424
    25     var i = v[id];
     25    var i = v.get(id);
    2626    if (!i)
    2727    {
    28         i = {};
    29         v[id] = i;
    30         this[cacheName][value][id] = defaultData;
     28        i = new Map();
     29        v.set(id, i);
     30        this[cacheName].get(value).set(id, defaultData);
    3131    }
    3232
    33     var k = i[key];
     33    var k = i.get(key);
    3434    if (!k)
    3535    {
    3636        k = [];
    37         i[key] = k;
     37        i.set(key, k);
    3838    }
    3939    return k;
    4040};
    4141
     42/**
     43 * Helper function preventing data objects from being copied
     44 * by reference, which causes (de)serialization problems here
     45 */
     46AuraManager.prototype.GetDataClone = function(data)
     47{
     48    var ret = { "value": data.value };
     49    if (data.add)
     50        ret.add = data.add;
     51    if (data.multiply)
     52        ret.multiply = data.multiply;
     53
     54    return ret;
     55};
     56
    4257AuraManager.prototype.ApplyBonus = function(value, ents, data, key)
    4358{
    4459    for (let ent of ents)
     
    4560    {
    4661        var dataList = this.ensureExists("modifications", value, ent, key, {"add":0, "multiply":1});
    4762
    48         dataList.push(data);
     63        dataList.push(this.GetDataClone(data));
    4964
    5065        if (dataList.length > 1)
    5166            continue;
    5267
    5368        // first time added this aura
     69        if (data.add)
     70            this.modificationsCache.get(value).get(ent).add += data.add;
    5471        if (data.multiply)
    55             this.modificationsCache[value][ent].multiply *= data.multiply;
     72            this.modificationsCache.get(value).get(ent).multiply *= data.multiply;
    5673
    57         if (data.add)
    58             this.modificationsCache[value][ent].add += data.add;
    5974        // post message to the entity to notify it about the change
    6075        Engine.PostMessage(ent, MT_ValueModification, { "entities": [ent], "component": value.split("/")[0], "valueNames": [value] });
    6176    }
     
    6378
    6479AuraManager.prototype.ApplyTemplateBonus = function(value, player, classes, data, key)
    6580{
    66     var dataList = this.ensureExists("templateModifications", value, player, key, {});
     81    var dataList = this.ensureExists("templateModifications", value, player, key, new Map());
    6782
    68     dataList.push(data);
     83    dataList.push(this.GetDataClone(data));
    6984
    7085    if (dataList.length > 1)
    7186        return;
    7287
    7388    // first time added this aura
    74     let cache = this.templateModificationsCache[value][player];
    75     if (!cache[classes])
    76         cache[classes] = {};
     89    let cache = this.templateModificationsCache.get(value).get(player);
     90    if (!cache.get(classes))
     91        cache.set(classes, new Map());
    7792
    78     if (!cache[classes][key])
    79         cache[classes][key] = { "add": 0, "multiply": 1};
     93    if (!cache.get(classes).get(key))
     94        cache.get(classes).set(key, {"add": 0, "multiply": 1});
    8095
     96    if (data.add)
     97        cache.get(classes).get(key).add += data.add;
    8198    if (data.multiply)
    82         cache[classes][key].multiply *= data.multiply;
     99        cache.get(classes).get(key).multiply *= data.multiply;
    83100
    84     if (data.add)
    85         cache[classes][key].add += data.add;
    86101    Engine.PostMessage(SYSTEM_ENTITY, MT_TemplateModification, { "player": player, "component": value.split("/")[0], "valueNames": [value] });
    87102};
    88103
    89104AuraManager.prototype.RemoveBonus = function(value, ents, key)
    90105{
    91     var v = this.modifications[value];
     106    var v = this.modifications.get(value);
    92107    if (!v)
    93108        return;
    94109
    95110    for (let ent of ents)
    96111    {
    97         var e = v[ent];
     112        var e = v.get(ent);
    98113        if (!e)
    99114            continue;
    100         var dataList = e[key];
     115        var dataList = e.get(key);
    101116        if (!dataList || !dataList.length)
    102117            continue;
    103118
     
    109124
    110125        // out of last aura of this kind, remove modifications
    111126        if (data.add)
    112             this.modificationsCache[value][ent].add -= data.add;
     127            this.modificationsCache.get(value).get(ent).add -= data.add;
    113128
    114129        if (data.multiply)
    115             this.modificationsCache[value][ent].multiply /= data.multiply;
     130            this.modificationsCache.get(value).get(ent).multiply /= data.multiply;
     131
    116132        // post message to the entity to notify it about the change
    117133        Engine.PostMessage(ent, MT_ValueModification, { "entities": [ent], "component": value.split("/")[0], "valueNames": [value] });
    118134    }
     
    120136
    121137AuraManager.prototype.RemoveTemplateBonus = function(value, player, classes, key)
    122138{
    123     var v = this.templateModifications[value];
     139    var v = this.templateModifications.get(value);
    124140    if (!v)
    125141        return;
    126     var p = v[player];
     142    var p = v.get(player);
    127143    if (!p)
    128144        return;
    129     var dataList = p[key];
     145    var dataList = p.get(key);
    130146    if (!dataList || !dataList.length)
    131147        return;
    132148
     
    135151    if (dataList.length > 0)
    136152        return;
    137153
    138     this.templateModificationsCache[value][player][classes][key].multiply = 1;
    139     this.templateModificationsCache[value][player][classes][key].add = 0;
     154    this.templateModificationsCache.get(value).get(player).get(classes).get(key).add = 0;
     155    this.templateModificationsCache.get(value).get(player).get(classes).get(key).multiply = 1;
    140156
    141157    Engine.PostMessage(SYSTEM_ENTITY, MT_TemplateModification, { "player": player, "component": value.split("/")[0], "valueNames": [value] });
    142158};
     
    143159
    144160AuraManager.prototype.ApplyModifications = function(valueName, value, ent)
    145161{
    146     var v = this.modificationsCache[valueName];
     162    var v = this.modificationsCache.get(valueName);
    147163    if (!v)
    148164        return value;
    149     var cache = v[ent];
     165    var cache = v.get(ent);
    150166    if (!cache)
    151167        return value;
    152168
     
    157173
    158174AuraManager.prototype.ApplyTemplateModifications = function(valueName, value, player, template)
    159175{
    160     var v = this.templateModificationsCache[valueName];
     176    var v = this.templateModificationsCache.get(valueName);
    161177    if (!v)
    162178        return value;
    163     var cache = v[player];
     179    var cache = v.get(player);
    164180    if (!cache)
    165181        return value;
    166182
     
    176192        if (!MatchesClassList(classes, c))
    177193            continue;
    178194
    179         for (let key in cache[c])
     195        for (let key in cache.get(c))
    180196        {
    181197            // don't add an aura with the same key twice
    182198            if (usedKeys.has(key))
    183199                continue;
    184200
    185             multiply *= cache[c][key].multiply;
    186             add += cache[c][key].add;
     201            add += cache.get(c).get(key).add;
     202            multiply *= cache.get(c).get(key).multiply;
    187203            usedKeys.add(key);
    188204        }
    189205    }