| 54 | Auras.prototype.Init = function() |
| 55 | { |
| 56 | var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); |
| 57 | this.templateName = cmpTemplateManager.GetCurrentTemplateName(this.entity); |
| 58 | }; |
| 59 | |
| 60 | Auras.prototype.GetAuraTypes = function() |
| 61 | { |
| 62 | return Object.keys(this.template); |
| 63 | }; |
| 64 | |
| 65 | Auras.prototype.GetRadius = function(type) |
| 66 | { |
| 67 | if (!this.IsRangeAura(type)) |
| 68 | return undefined; |
| 69 | if (this.IsTemplateAura(type)) |
| 70 | return -1; |
| 71 | var radius = +this.template[type].Radius; |
| 72 | return ApplyTechModificationsToEntity("Auras/"+type+"/Radius",radius, this.entity); |
| 73 | }; |
| 74 | |
| 75 | Auras.prototype.GetClasses = function(type) |
| 76 | { |
| 77 | var string = this.template[type].Classes || ""; |
| 78 | return string.split(/\s+/); |
| 79 | }; |
| 80 | |
| 81 | Auras.prototype.GetModification = function(type) |
| 82 | { |
| 83 | var ret = {}; |
| 84 | ret.values = this.template[type].Modification.Value.split(/\s+/); |
| 85 | if (this.template[type].Modification.Add) |
| 86 | { |
| 87 | ret.add = +this.template[type].Modification.Add; |
| 88 | ret.add = ApplyTechModificationsToEntity("Auras/"+type+"/Modification/Add", ret.add, this.entity); |
| 89 | } |
| 90 | if (this.template[type].Modification.Multiply) |
| 91 | { |
| 92 | ret.multiply = +this.template[type].Modification.Multiply; |
| 93 | ret.multiply = ApplyTechModificationsToEntity("Auras/"+type+"/Modification/Multiply", ret.multiply, this.entity); |
| 94 | } |
| 95 | return ret; |
| 96 | }; |
| 97 | |
| 98 | Auras.prototype.GetAffectedPlayers = function(type) |
| 99 | { |
| 100 | if (this.template[type].AffectedPlayers) |
| 101 | var affectedPlayers = this.template[type].AffectedPlayers.split(/\s+/); |
| 102 | else |
| 103 | var affectedPlayers = ["Player"]; |
| 104 | |
| 105 | var ret = []; |
| 106 | |
| 107 | var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player); |
| 108 | warn(uneval(cmpPlayer)); |
| 109 | |
| 110 | if (!cmpPlayer) |
| 111 | return ret; |
| 112 | |
| 113 | var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); |
| 114 | var numPlayers = cmpPlayerManager.GetNumPlayers(); |
| 115 | var enemies = []; |
| 116 | var mutualAllies = []; |
| 117 | |
| 118 | for (var i = 0; i < numPlayers; ++i) |
| 119 | { |
| 120 | if (affectedPlayers.indexOf("Player") != -1 && cmpPlayer.GetPlayerID() == i) |
| 121 | ret.push(i); |
| 122 | else if (affectedPlayers.indexOf("Enemy") != -1 && cmpPlayer.IsEnemy(i)) |
| 123 | ret.push(i); |
| 124 | else if (affectedPlayers.indexOf("MutualAlly") != -1 && cmpPlayer.IsMutualAlly(i)) |
| 125 | ret.push(i); |
| 126 | else if (affectedPlayers.indexOf("Ally") != -1 && cmpPlayer.IsAlly(i)) |
| 127 | ret.push(i); |
| 128 | else if (affectedPlayers.indexOf("Neutral") != -1 && cmpPlayer.IsNeutral(i)) |
| 129 | ret.push(i); |
| 130 | } |
| 131 | // return and store the value |
| 132 | this[type].affectedPlayers = ret; |
| 133 | return ret; |
| 134 | }; |
| 135 | |
| 136 | Auras.prototype.HasFormationAura = function() |
| 137 | { |
| 138 | var auraTypes = this.GetAuraTypes(); |
| 139 | for each (var type in auraTypes) |
| 140 | { |
| 141 | if (this.IsFormationAura(type)) |
| 142 | return true; |
| 143 | } |
| 144 | return false; |
| 145 | }; |
| 146 | |
| 147 | Auras.prototype.HasGarrisonAura = function() |
| 148 | { |
| 149 | var auraTypes = this.GetAuraTypes(); |
| 150 | for each (var type in auraTypes) |
| 151 | { |
| 152 | if (this.IsGarrisonAura(type)) |
| 153 | return true; |
| 154 | } |
| 155 | return false; |
| 156 | }; |
| 157 | |
| 158 | Auras.prototype.IsFormationAura = function(type) |
| 159 | { |
| 160 | return 'Formation' in this.template[type]; |
| 161 | }; |
| 162 | |
| 163 | Auras.prototype.IsGarrisonAura = function(type) |
| 164 | { |
| 165 | return 'GarrisoningStructure' in this.template[type]; |
| 166 | }; |
| 167 | |
| 168 | Auras.prototype.IsRangeAura = function(type) |
| 169 | { |
| 170 | return 'Radius' in this.template[type] || "Template" in this.template[type]; |
| 171 | }; |
| 172 | |
| 173 | Auras.prototype.IsTemplateAura = function(type) |
| 174 | { |
| 175 | return "Template" in this.template[type]; |
| 176 | }; |
| 177 | |
| 178 | /** |
| 179 | * clean all bonuses. Remove the old ones and re-apply the new ones |
| 180 | */ |
| 181 | Auras.prototype.Clean = function() |
| 182 | { |
| 183 | var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); |
| 184 | var auraTypes = this.GetAuraTypes(); |
| 185 | // remove all bonuses |
| 186 | for each (var type in auraTypes) |
| 187 | { |
| 188 | if (!this[type]) |
| 189 | continue; |
| 190 | |
| 191 | if (this.IsTemplateAura(type)) |
| 192 | this.RemoveTemplateBonus(type); |
| 193 | |
| 194 | for each(var ent in this[type].targetUnits) |
| 195 | this.RemoveBonus(type, ent); |
| 196 | |
| 197 | if (this[type].rangeQuery) |
| 198 | cmpRangeManager.DestroyActiveQuery(this[type].rangeQuery); |
| 199 | } |
| 200 | |
| 201 | for each (var type in auraTypes) |
| 202 | { |
| 203 | // initialise range query |
| 204 | this[type] = {}; |
| 205 | this[type].targetUnits = []; |
| 206 | var affectedPlayers = this.GetAffectedPlayers(type); |
| 207 | |
| 208 | if (!affectedPlayers.length) |
| 209 | continue; |
| 210 | |
| 211 | if (this.IsTemplateAura(type)) |
| 212 | this.ApplyTemplateBonus(type, affectedPlayers); |
| 213 | |
| 214 | if (!this.IsRangeAura(type)) |
| 215 | continue; |
| 216 | warn("add range query"); |
| 217 | this[type].rangeQuery = cmpRangeManager.CreateActiveQuery( |
| 218 | this.entity, |
| 219 | 0, |
| 220 | this.GetRadius(type), |
| 221 | affectedPlayers, |
| 222 | IID_Identity, |
| 223 | cmpRangeManager.GetEntityFlagMask("normal") |
| 224 | ); |
| 225 | cmpRangeManager.EnableActiveQuery(this[type].rangeQuery); |
| 226 | // add self to your own query for consistency with templates |
| 227 | this.OnRangeUpdate({"tag":this[type].rangeQuery, "added":[this.entity], "removed":[]}); |
| 228 | } |
| 229 | }; |
| 230 | |
| 231 | Auras.prototype.OnRangeUpdate = function(msg) |
| 232 | { |
| 233 | var auraTypes = this.GetAuraTypes(); |
| 234 | for each (var t in auraTypes) |
| 235 | { |
| 236 | if (msg.tag == this[t].rangeQuery) |
| 237 | { |
| 238 | var type = t; |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (!type) |
| 244 | return; |
| 245 | |
| 246 | var targetUnits = this[type].targetUnits; |
| 247 | var classes = this.GetClasses(type); |
| 248 | |
| 249 | if (msg.added.length > 0) |
| 250 | { |
| 251 | for each (var e in msg.added) |
| 252 | { |
| 253 | var cmpIdentity = Engine.QueryInterface(e, IID_Identity); |
| 254 | var targetClasses = cmpIdentity.GetClassesList(); |
| 255 | |
| 256 | if (targetClasses.some(function(c){return classes.indexOf(c) > -1;})) |
| 257 | { |
| 258 | targetUnits.push(e); |
| 259 | this.ApplyBonus(type,e); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | if (msg.removed.length > 0) |
| 265 | { |
| 266 | for each (var e in msg.removed) |
| 267 | { |
| 268 | targetUnits.splice(targetUnits.indexOf(e), 1); |
| 269 | this.RemoveBonus(type,e); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | }; |
| 274 | |
| 275 | Auras.prototype.ApplyFormationBonus = function(memberList) |
| 276 | { |
| 277 | var auraTypes = this.GetAuraTypes(); |
| 278 | for each (var type in auraTypes) |
| 279 | { |
| 280 | if (!this.IsFormationAura(type)) |
| 281 | continue; |
| 282 | var classes = this.GetClasses(type); |
| 283 | |
| 284 | for each (var ent in memberList) |
| 285 | { |
| 286 | var cmpIdentity = Engine.QueryInterface(ent, IID_Identity); |
| 287 | var targetClasses = cmpIdentity.GetClassesList(); |
| 288 | if (targetClasses.some(function(c){return classes.indexOf(c) > -1;})) |
| 289 | { |
| 290 | this[type].targetUnits.push(ent); |
| 291 | this.ApplyBonus(type,ent); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | }; |
| 296 | |
| 297 | Auras.prototype.ApplyGarrisonBonus = function(structure) |
| 298 | { |
| 299 | var auraTypes = this.GetAuraTypes(); |
| 300 | for each (var type in auraTypes) |
| 301 | { |
| 302 | if (!this.IsGarrisonAura(type)) |
| 303 | continue; |
| 304 | var classes = this.GetClasses(type); |
| 305 | |
| 306 | |
| 307 | var cmpIdentity = Engine.QueryInterface(structure, IID_Identity); |
| 308 | var targetClasses = cmpIdentity.GetClassesList(); |
| 309 | if (targetClasses.some(function(c){return classes.indexOf(c) > -1;})) |
| 310 | { |
| 311 | this[type].targetUnits.push(structure); |
| 312 | this.ApplyBonus(type,structure); |
| 313 | } |
| 314 | } |
| 315 | }; |
| 316 | |
| 317 | Auras.prototype.ApplyTemplateBonus = function(type, players) |
| 318 | { |
| 319 | if (!this.IsTemplateAura(type)) |
| 320 | return; |
| 321 | var modification = this.GetModification(type); |
| 322 | var cmpAuraManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_AuraManager); |
| 323 | var classes = this.GetClasses(type); |
| 324 | |
| 325 | for each (var value in modification.values) |
| 326 | for each (var player in players) |
| 327 | cmpAuraManager.ApplyTemplateBonus(value, player, classes, modification, this.templateName+"/"+type+"/"+value); |
| 328 | }; |
| 329 | |
| 330 | Auras.prototype.RemoveFormationBonus = function(memberList) |
| 331 | { |
| 332 | var auraTypes = this.GetAuraTypes(); |
| 333 | for each (var type in auraTypes) |
| 334 | { |
| 335 | if (!this.IsFormationAura(type)) |
| 336 | continue; |
| 337 | |
| 338 | for each (var ent in memberList) |
| 339 | { |
| 340 | this.RemoveBonus(type,ent); |
| 341 | this[type].targetUnits.splice(this[type].targetUnits.indexOf(ent), 1); |
| 342 | } |
| 343 | } |
| 344 | }; |
| 345 | |
| 346 | Auras.prototype.RemoveGarrisonBonus = function(structure) |
| 347 | { |
| 348 | var auraTypes = this.GetAuraTypes(); |
| 349 | for each (var type in auraTypes) |
| 350 | { |
| 351 | if (!this.IsGarrisonAura(type)) |
| 352 | continue; |
| 353 | |
| 354 | this.RemoveBonus(type,structure); |
| 355 | this[type].targetUnits.splice(this[type].targetUnits.indexOf(structure), 1); |
| 356 | } |
| 357 | }; |
| 358 | |
| 359 | Auras.prototype.RemoveTemplateBonus = function(type) |
| 360 | { |
| 361 | if (!this.IsTemplateAura(type)) |
| 362 | return; |
| 363 | |
| 364 | var modification = this.GetModification(type); |
| 365 | var cmpAuraManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_AuraManager); |
| 366 | var classes = this.GetClasses(type); |
| 367 | |
| 368 | for each (var value in modification.values) |
| 369 | for each (var player in this[type].affectedPlayers) |
| 370 | cmpAuraManager.RemoveTemplateBonus(value, player, classes, this.templateName+"/"+type+"/"+value); |
| 371 | }; |
| 372 | |
| 373 | Auras.prototype.ApplyBonus = function(type,ent) |
| 374 | { |
| 375 | var modification = this.GetModification(type); |
| 376 | var cmpAuraManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_AuraManager); |
| 377 | |
| 378 | for each (value in modification.values) |
| 379 | cmpAuraManager.ApplyBonus(value, ent,modification,this.templateName+"/"+type+"/"+value); |
| 380 | |
| 381 | }; |
| 382 | |
| 383 | Auras.prototype.RemoveBonus = function(type,ent) |
| 384 | { |
| 385 | var modification = this.GetModification(type); |
| 386 | var cmpAuraManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_AuraManager); |
| 387 | |
| 388 | for each (value in modification.values) |
| 389 | cmpAuraManager.RemoveBonus(value, ent, this.templateName+"/"+type+"/"+value); |
| 390 | |
| 391 | }; |
| 392 | |
| 393 | Auras.prototype.OnOwnershipChanged = function(msg) |
| 394 | { |
| 395 | warn("ownership" +uneval(msg)); |
| 396 | this.Clean(); |
| 397 | }; |
| 398 | |
| 399 | Auras.prototype.OnDiplomacyChanged = function(msg) |
| 400 | { |
| 401 | warn("diplo" +uneval(msg)); |
| 402 | var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); |
| 403 | if (cmpOwnership && cmpOwnership.GetOwner() == msg.player) |
| 404 | this.Clean(); |
| 405 | }; |
| 406 | |
| 407 | Auras.prototype.OnTechnologyModification = function(msg) |
| 408 | { |
| 409 | if (msg.component =="Auras") |
| 410 | this.Clean(); |
| 411 | }; |
| 412 | |