Ticket #3164: aura_manager_maps.patch
File aura_manager_maps.patch, 6.7 KB (added by , 9 years ago) |
---|
-
binaries/data/mods/public/simulation/components/AuraManager.js
5 5 6 6 AuraManager.prototype.Init = function() 7 7 { 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(); 12 12 }; 13 13 14 14 AuraManager.prototype.ensureExists = function(name, value, id, key, defaultData) 15 15 { 16 16 var cacheName = name + "Cache"; 17 var v = this[name] [value];17 var v = this[name].get(value); 18 18 if (!v) 19 19 { 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()); 23 23 } 24 24 25 var i = v [id];25 var i = v.get(id); 26 26 if (!i) 27 27 { 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); 31 31 } 32 32 33 var k = i [key];33 var k = i.get(key); 34 34 if (!k) 35 35 { 36 36 k = []; 37 i [key] = k;37 i.set(key, k); 38 38 } 39 39 return k; 40 40 }; 41 41 42 /** 43 * Helper function preventing data objects from being copied 44 * by reference, which causes (de)serialization problems here 45 */ 46 AuraManager.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 42 57 AuraManager.prototype.ApplyBonus = function(value, ents, data, key) 43 58 { 44 59 for (let ent of ents) … … 45 60 { 46 61 var dataList = this.ensureExists("modifications", value, ent, key, {"add":0, "multiply":1}); 47 62 48 dataList.push( data);63 dataList.push(this.GetDataClone(data)); 49 64 50 65 if (dataList.length > 1) 51 66 continue; 52 67 53 68 // first time added this aura 69 if (data.add) 70 this.modificationsCache.get(value).get(ent).add += data.add; 54 71 if (data.multiply) 55 this.modificationsCache [value][ent].multiply *= data.multiply;72 this.modificationsCache.get(value).get(ent).multiply *= data.multiply; 56 73 57 if (data.add)58 this.modificationsCache[value][ent].add += data.add;59 74 // post message to the entity to notify it about the change 60 75 Engine.PostMessage(ent, MT_ValueModification, { "entities": [ent], "component": value.split("/")[0], "valueNames": [value] }); 61 76 } … … 63 78 64 79 AuraManager.prototype.ApplyTemplateBonus = function(value, player, classes, data, key) 65 80 { 66 var dataList = this.ensureExists("templateModifications", value, player, key, {});81 var dataList = this.ensureExists("templateModifications", value, player, key, new Map()); 67 82 68 dataList.push( data);83 dataList.push(this.GetDataClone(data)); 69 84 70 85 if (dataList.length > 1) 71 86 return; 72 87 73 88 // 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()); 77 92 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}); 80 95 96 if (data.add) 97 cache.get(classes).get(key).add += data.add; 81 98 if (data.multiply) 82 cache [classes][key].multiply *= data.multiply;99 cache.get(classes).get(key).multiply *= data.multiply; 83 100 84 if (data.add)85 cache[classes][key].add += data.add;86 101 Engine.PostMessage(SYSTEM_ENTITY, MT_TemplateModification, { "player": player, "component": value.split("/")[0], "valueNames": [value] }); 87 102 }; 88 103 89 104 AuraManager.prototype.RemoveBonus = function(value, ents, key) 90 105 { 91 var v = this.modifications [value];106 var v = this.modifications.get(value); 92 107 if (!v) 93 108 return; 94 109 95 110 for (let ent of ents) 96 111 { 97 var e = v [ent];112 var e = v.get(ent); 98 113 if (!e) 99 114 continue; 100 var dataList = e [key];115 var dataList = e.get(key); 101 116 if (!dataList || !dataList.length) 102 117 continue; 103 118 … … 109 124 110 125 // out of last aura of this kind, remove modifications 111 126 if (data.add) 112 this.modificationsCache [value][ent].add -= data.add;127 this.modificationsCache.get(value).get(ent).add -= data.add; 113 128 114 129 if (data.multiply) 115 this.modificationsCache[value][ent].multiply /= data.multiply; 130 this.modificationsCache.get(value).get(ent).multiply /= data.multiply; 131 116 132 // post message to the entity to notify it about the change 117 133 Engine.PostMessage(ent, MT_ValueModification, { "entities": [ent], "component": value.split("/")[0], "valueNames": [value] }); 118 134 } … … 120 136 121 137 AuraManager.prototype.RemoveTemplateBonus = function(value, player, classes, key) 122 138 { 123 var v = this.templateModifications [value];139 var v = this.templateModifications.get(value); 124 140 if (!v) 125 141 return; 126 var p = v [player];142 var p = v.get(player); 127 143 if (!p) 128 144 return; 129 var dataList = p [key];145 var dataList = p.get(key); 130 146 if (!dataList || !dataList.length) 131 147 return; 132 148 … … 135 151 if (dataList.length > 0) 136 152 return; 137 153 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; 140 156 141 157 Engine.PostMessage(SYSTEM_ENTITY, MT_TemplateModification, { "player": player, "component": value.split("/")[0], "valueNames": [value] }); 142 158 }; … … 143 159 144 160 AuraManager.prototype.ApplyModifications = function(valueName, value, ent) 145 161 { 146 var v = this.modificationsCache [valueName];162 var v = this.modificationsCache.get(valueName); 147 163 if (!v) 148 164 return value; 149 var cache = v [ent];165 var cache = v.get(ent); 150 166 if (!cache) 151 167 return value; 152 168 … … 157 173 158 174 AuraManager.prototype.ApplyTemplateModifications = function(valueName, value, player, template) 159 175 { 160 var v = this.templateModificationsCache [valueName];176 var v = this.templateModificationsCache.get(valueName); 161 177 if (!v) 162 178 return value; 163 var cache = v [player];179 var cache = v.get(player); 164 180 if (!cache) 165 181 return value; 166 182 … … 176 192 if (!MatchesClassList(classes, c)) 177 193 continue; 178 194 179 for (let key in cache [c])195 for (let key in cache.get(c)) 180 196 { 181 197 // don't add an aura with the same key twice 182 198 if (usedKeys.has(key)) 183 199 continue; 184 200 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; 187 203 usedKeys.add(key); 188 204 } 189 205 }