Ticket #3189: QueryMiragedInterface.diff
File QueryMiragedInterface.diff, 20.6 KB (added by , 9 years ago) |
---|
-
binaries/data/mods/public/gui/session/selection_details.js
206 206 Engine.GetGUIObjectByName("resourceCarryingIcon").hidden = false; 207 207 Engine.GetGUIObjectByName("resourceCarryingText").hidden = false; 208 208 Engine.GetGUIObjectByName("resourceCarryingIcon").sprite = "stretched:session/icons/repair.png"; 209 Engine.GetGUIObjectByName("resourceCarryingText").caption = sprintf(translate("%(amount)s / %(max)s"), { amount: entState.resourceSupply. gatherers.length, max: entState.resourceSupply.maxGatherers }) + " ";209 Engine.GetGUIObjectByName("resourceCarryingText").caption = sprintf(translate("%(amount)s / %(max)s"), { amount: entState.resourceSupply.numGatherers, max: entState.resourceSupply.maxGatherers }) + " "; 210 210 Engine.GetGUIObjectByName("resourceCarryingIcon").tooltip = translate("Current/max gatherers"); 211 211 } 212 212 else -
binaries/data/mods/public/simulation/components/Fogging.js
115 115 // Store valuable information into the mirage component (especially for the GUI) 116 116 var cmpFoundation = Engine.QueryInterface(this.entity, IID_Foundation); 117 117 if (cmpFoundation) 118 cmpMirage.CopyFoundation(cmpFoundation .GetBuildPercentage());118 cmpMirage.CopyFoundation(cmpFoundation); 119 119 120 120 var cmpHealth = Engine.QueryInterface(this.entity, IID_Health); 121 121 if (cmpHealth) 122 cmpMirage.CopyHealth( 123 cmpHealth.GetMaxHitpoints(), 124 cmpHealth.GetHitpoints(), 125 cmpHealth.IsRepairable() && (cmpHealth.GetHitpoints() < cmpHealth.GetMaxHitpoints()) 126 ); 122 cmpMirage.CopyHealth(cmpHealth); 127 123 128 124 var cmpCapturable = Engine.QueryInterface(this.entity, IID_Capturable); 129 125 if (cmpCapturable) 130 cmpMirage.CopyCapturable( 131 cmpCapturable.GetCapturePoints(), 132 cmpCapturable.GetMaxCapturePoints() 133 ); 126 cmpMirage.CopyCapturable(cmpCapturable); 134 127 135 128 var cmpResourceSupply = Engine.QueryInterface(this.entity, IID_ResourceSupply); 136 129 if (cmpResourceSupply) 137 cmpMirage.CopyResourceSupply( 138 cmpResourceSupply.GetMaxAmount(), 139 cmpResourceSupply.GetCurrentAmount(), 140 cmpResourceSupply.GetType(), 141 cmpResourceSupply.IsInfinite() 142 ); 130 cmpMirage.CopyResourceSupply(cmpResourceSupply); 143 131 144 132 // Notify the GUI the entity has been replaced by a mirage, in case it is selected at this moment 145 133 var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); -
binaries/data/mods/public/simulation/components/GuiInterface.js
258 258 ret.rotation = cmpPosition.GetRotation(); 259 259 } 260 260 261 var cmpHealth = Engine.QueryInterface(ent, IID_Health);261 var cmpHealth = QueryMiragedInterface(ent, IID_Health); 262 262 if (cmpHealth) 263 263 { 264 264 ret.hitpoints = Math.ceil(cmpHealth.GetHitpoints()); … … 266 266 ret.needsRepair = cmpHealth.IsRepairable() && (cmpHealth.GetHitpoints() < cmpHealth.GetMaxHitpoints()); 267 267 ret.needsHeal = !cmpHealth.IsUnhealable(); 268 268 } 269 if (cmpMirage && cmpMirage.Health())270 {271 ret.hitpoints = cmpMirage.GetHitpoints();272 ret.maxHitpoints = cmpMirage.GetMaxHitpoints();273 ret.needsRepair = cmpMirage.NeedsRepair();274 }275 269 276 var cmpCapturable = Engine.QueryInterface(ent, IID_Capturable);270 var cmpCapturable = QueryMiragedInterface(ent, IID_Capturable); 277 271 if (cmpCapturable) 278 272 { 279 273 ret.capturePoints = cmpCapturable.GetCapturePoints(); 280 274 ret.maxCapturePoints = cmpCapturable.GetMaxCapturePoints(); 281 275 } 282 if (cmpMirage && cmpMirage.Capturable())283 {284 ret.capturePoints = cmpMirage.GetCapturePoints();285 ret.maxCapturePoints = cmpMirage.GetMaxCapturePoints();286 }287 276 288 277 var cmpBuilder = Engine.QueryInterface(ent, IID_Builder); 289 278 if (cmpBuilder) … … 326 315 ret.fogging = {"mirage": null}; 327 316 } 328 317 329 var cmpFoundation = Engine.QueryInterface(ent, IID_Foundation);318 var cmpFoundation = QueryMiragedInterface(ent, IID_Foundation); 330 319 if (cmpFoundation) 331 320 { 332 321 ret.foundation = { … … 334 323 "numBuilders": cmpFoundation.GetNumBuilders() 335 324 }; 336 325 } 337 if (cmpMirage && cmpMirage.Foundation())338 {339 ret.foundation = {340 "progress": cmpMirage.GetBuildPercentage()341 };342 }343 326 344 327 var cmpOwnership = Engine.QueryInterface(ent, IID_Ownership); 345 328 if (cmpOwnership) … … 523 506 if (cmpPosition && cmpPosition.GetTurretParent() != INVALID_ENTITY) 524 507 ret.turretParent = cmpPosition.GetTurretParent(); 525 508 526 var cmpResourceSupply = Engine.QueryInterface(ent, IID_ResourceSupply);509 var cmpResourceSupply = QueryMiragedInterface(ent, IID_ResourceSupply); 527 510 if (cmpResourceSupply) 528 511 { 529 512 ret.resourceSupply = { … … 533 516 "type": cmpResourceSupply.GetType(), 534 517 "killBeforeGather": cmpResourceSupply.GetKillBeforeGather(), 535 518 "maxGatherers": cmpResourceSupply.GetMaxGatherers(), 536 "gatherers": cmpResourceSupply.GetGatherers() 537 }; 538 } 539 if (cmpMirage && cmpMirage.ResourceSupply()) 540 { 541 ret.resourceSupply = { 542 "max": cmpMirage.GetMaxAmount(), 543 "amount": cmpMirage.GetAmount(), 544 "type": cmpMirage.GetType(), 545 "isInfinite": cmpMirage.IsInfinite() 519 "numGatherers": cmpResourceSupply.GetNumGatherers() 546 520 }; 547 521 } 548 522 … … 1712 1686 1713 1687 var owner = QueryOwnerInterface(data.entity).GetPlayerID(); 1714 1688 1715 var cmp = Engine.QueryInterface(data.target, IID_Mirage); 1716 if (cmp && !cmp.Capturable()) 1717 return false 1718 else if (!cmp) 1719 var cmp = Engine.QueryInterface(data.target, IID_Capturable); 1720 1721 if (cmp && cmp.CanCapture(owner) && cmpAttack.GetAttackTypes().indexOf("Capture") != -1) 1689 var cmpCapturable = QueryMiragedInterface(data.target, IID_Capturable); 1690 if (cmpCapturable && cmpCapturable.CanCapture(owner) && cmpAttack.GetAttackTypes().indexOf("Capture") != -1) 1722 1691 return cmpAttack.CanAttack(data.target); 1723 1692 1724 1693 return false; -
binaries/data/mods/public/simulation/components/Mirage.js
13 13 this.player = null; 14 14 this.parent = INVALID_ENTITY; 15 15 16 this.foundation = false; 17 this.buildPercentage = null; 16 this.miraged_iids = new Set(); 17 18 this.buildPercentage = 0; 19 this.numBuilders = 0; 18 20 19 this.health = false;20 21 this.maxHitpoints = null; 21 22 this.hitpoints = null; 22 this.needsRepair = null; 23 this.repairable = null; 24 this.unhealable = null; 23 25 24 this.capturable = false;25 26 this.capturePoints = []; 26 27 this.maxCapturePoints = 0; 27 28 28 this.resourceSupply = false;29 29 this.maxAmount = null; 30 30 this.amount = null; 31 31 this.type = null; 32 32 this.isInfinite = null; 33 this.killBeforeGather = null; 34 this.maxGatherers = null; 35 this.numGatherers = null; 33 36 }; 34 37 35 38 Mirage.prototype.SetParent = function(ent) … … 47 50 this.player = player; 48 51 }; 49 52 53 Mirage.prototype.Mirages = function(iid) 54 { 55 return this.miraged_iids.has(iid); 56 }; 57 50 58 // ============================ 51 59 // Parent entity data 52 60 53 61 // Foundation data 54 62 55 Mirage.prototype.CopyFoundation = function(buildPercentage) 56 { 57 this.foundation = true; 58 this.buildPercentage = buildPercentage; 59 }; 60 61 Mirage.prototype.Foundation = function() 63 Mirage.prototype.CopyFoundation = function(cmpFoundation) 62 64 { 63 return this.foundation; 65 this.miraged_iids.add(IID_Foundation); 66 this.buildPercentage = cmpFoundation.GetBuildPercentage(); 67 this.numBuilders = cmpFoundation.GetNumBuilders(); 64 68 }; 65 69 66 Mirage.prototype.GetBuildPercentage = function() 67 { 68 return this.buildPercentage; 69 }; 70 Mirage.prototype.GetBuildPercentage = function() { return this.buildPercentage; }; 71 Mirage.prototype.GetNumBuilders = function() { return this.numBuilders; }; 70 72 71 73 // Health data 72 74 73 Mirage.prototype.CopyHealth = function( maxHitpoints, hitpoints, needsRepair)75 Mirage.prototype.CopyHealth = function(cmpHealth) 74 76 { 75 this.health = true; 76 this.maxHitpoints = maxHitpoints; 77 this.hitpoints = Math.ceil(hitpoints); 78 this.needsRepair = needsRepair; 77 this.miraged_iids.add(IID_Health); 78 this.maxHitpoints = cmpHealth.GetMaxHitpoints(); 79 this.hitpoints = cmpHealth.GetHitpoints(); 80 this.repairable = cmpHealth.IsRepairable(); 81 this.unhealable = cmpHealth.IsUnhealable(); 79 82 }; 80 83 81 Mirage.prototype.Health = function() 82 { 83 return this.health; 84 }; 85 86 Mirage.prototype.GetMaxHitpoints = function() 87 { 88 return this.maxHitpoints; 89 }; 90 91 Mirage.prototype.GetHitpoints = function() 92 { 93 return this.hitpoints; 94 }; 95 96 Mirage.prototype.NeedsRepair = function() 97 { 98 return this.needsRepair; 99 }; 84 Mirage.prototype.GetMaxHitpoints = function() { return this.maxHitpoints; }; 85 Mirage.prototype.GetHitpoints = function() { return this.hitpoints; }; 86 Mirage.prototype.IsRepairable = function() { return this.repairable; }; 87 Mirage.prototype.IsUnhealable = function() { return this.unhealable; }; 100 88 101 89 // Capture data 102 90 103 Mirage.prototype.CopyCapturable = function(c apturePoints, maxCapturePoints)91 Mirage.prototype.CopyCapturable = function(cmpCapturable) 104 92 { 105 this. capturable = true;106 this.capturePoints = c apturePoints;107 this.maxCapturePoints = maxCapturePoints;93 this.miraged_iids.add(IID_Capturable); 94 this.capturePoints = cmpCapturable.GetCapturePoints(); 95 this.maxCapturePoints = cmpCapturable.GetMaxCapturePoints(); 108 96 }; 109 97 110 Mirage.prototype.Capturable = function() 111 { 112 return this.capturable; 113 }; 114 115 Mirage.prototype.GetMaxCapturePoints = function() 116 { 117 return this.maxCapturePoints; 118 }; 119 120 Mirage.prototype.GetCapturePoints = function() 121 { 122 return this.capturePoints; 123 }; 98 Mirage.prototype.GetMaxCapturePoints = function() { return this.maxCapturePoints; }; 99 Mirage.prototype.GetCapturePoints = function() { return this.capturePoints; }; 124 100 125 101 Mirage.prototype.CanCapture = Capturable.prototype.CanCapture; 126 102 127 103 // ResourceSupply data 128 104 129 Mirage.prototype.CopyResourceSupply = function(maxAmount, amount, type, isInfinite) 130 { 131 this.resourceSupply = true; 132 this.maxAmount = maxAmount; 133 this.amount = amount; 134 this.type = type; 135 this.isInfinite = isInfinite; 136 }; 137 138 Mirage.prototype.ResourceSupply = function() 139 { 140 return this.resourceSupply; 141 }; 142 143 Mirage.prototype.GetMaxAmount = function() 144 { 145 return this.maxAmount; 146 }; 147 148 Mirage.prototype.GetAmount = function() 149 { 150 return this.amount; 151 }; 152 153 Mirage.prototype.GetType = function() 154 { 155 return this.type; 156 }; 157 158 Mirage.prototype.IsInfinite = function() 159 { 160 return this.isInfinite; 161 }; 105 Mirage.prototype.CopyResourceSupply = function(cmpResourceSupply) 106 { 107 this.miraged_iids.add(IID_ResourceSupply); 108 this.maxAmount = cmpResourceSupply.GetMaxAmount(); 109 this.amount = cmpResourceSupply.GetCurrentAmount(); 110 this.type = cmpResourceSupply.GetType(); 111 this.isInfinite = cmpResourceSupply.IsInfinite(); 112 this.killBeforeGather = cmpResourceSupply.GetKillBeforeGather(); 113 this.maxGatherers = cmpResourceSupply.GetMaxGatherers(); 114 this.numGatherers = cmpResourceSupply.GetNumGatherers(); 115 }; 116 117 Mirage.prototype.GetMaxAmount = function() { return this.maxAmount; }; 118 Mirage.prototype.GetCurrentAmount = function() { return this.amount; }; 119 Mirage.prototype.GetType = function() { return this.type; }; 120 Mirage.prototype.IsInfinite = function() { return this.isInfinite; }; 121 Mirage.prototype.GetKillBeforeGather = function() { return this.killBeforeGather; }; 122 Mirage.prototype.GetMaxGatherers = function() { return this.maxGatherers; }; 123 Mirage.prototype.GetNumGatherers = function() { return this.numGatherers; }; 162 124 163 125 // ============================ 164 126 -
binaries/data/mods/public/simulation/components/ResourceGatherer.js
253 253 */ 254 254 ResourceGatherer.prototype.GetTargetGatherRate = function(target) 255 255 { 256 let type; 257 let cmpResourceSupply = Engine.QueryInterface(target, IID_ResourceSupply); 258 let cmpMirage = Engine.QueryInterface(target, IID_Mirage); 259 if (cmpResourceSupply) 260 type = cmpResourceSupply.GetType(); 261 else if (cmpMirage && cmpMirage.ResourceSupply()) 262 type = cmpMirage.GetType(); 263 else 256 let cmpResourceSupply = QueryMiragedInterface(target, IID_ResourceSupply); 257 if (!cmpResourceSupply) 264 258 return 0; 265 259 260 let type = cmpResourceSupply.GetType(); 261 266 262 let rate = 0; 267 263 if (type.specific) 268 264 rate = this.GetGatherRate(type.generic+"."+type.specific); … … 273 269 let cheatMultiplier = cmpPlayer ? cmpPlayer.GetCheatTimeMultiplier() : 1; 274 270 rate = rate / cheatMultiplier; 275 271 276 if ( cmpMirage)272 if ("Mirages" in cmpResourceSupply) 277 273 return rate; 278 274 279 275 // Apply diminishing returns with more gatherers, for e.g. infinite farms. For most resources this has no effect. (GetDiminishingReturns will return null.) … … 294 290 // The cosine function is an oscillating curve, normally between -1 and 1. Multiplying by 0.5 squishes that down to 295 291 // between -0.5 and 0.5. Adding 0.5 to that changes the range to 0 to 1. The diminishingReturns constant 296 292 // adjusts the period of the curve. 297 // Alternatively, just find scythetwirler (who came up with the math here) or alpha123 (who wrote the code) on IRC.298 let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);299 293 let diminishingReturns = cmpResourceSupply.GetDiminishingReturns(); 300 294 if (diminishingReturns) 301 rate = (0.5 * Math.cos((cmpResourceSupply.Get Gatherers().length- 1) * Math.PI / diminishingReturns) + 0.5) * rate;295 rate = (0.5 * Math.cos((cmpResourceSupply.GetNumGatherers() - 1) * Math.PI / diminishingReturns) + 0.5) * rate; 302 296 303 297 return rate; 304 298 }; -
binaries/data/mods/public/simulation/components/ResourceSupply.js
78 78 return +this.template.MaxGatherers; 79 79 }; 80 80 81 ResourceSupply.prototype.GetNumGatherers = function() 82 { 83 return this.gatherers.map(p => p.length).reduce((a, b) => a+b); 84 }; 85 81 86 ResourceSupply.prototype.GetGatherers = function() 82 87 { 83 var numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers(); 84 var total = []; 85 for (var playerid = 0; playerid <= numPlayers; playerid++) 86 for (var gatherer = 0; gatherer < this.gatherers[playerid].length; gatherer++) 87 total.push(this.gatherers[playerid][gatherer]); 88 return total; 88 return this.gatherers.reduce((a, b) => a.concat(b)); 89 89 }; 90 90 91 91 ResourceSupply.prototype.GetDiminishingReturns = function() … … 130 130 131 131 ResourceSupply.prototype.IsAvailable = function(player, gathererID) 132 132 { 133 if (this.Get Gatherers().length< this.GetMaxGatherers() || this.gatherers[player].indexOf(gathererID) !== -1)133 if (this.GetNumGatherers() < this.GetMaxGatherers() || this.gatherers[player].indexOf(gathererID) !== -1) 134 134 return true; 135 135 return false; 136 136 }; -
binaries/data/mods/public/simulation/components/StatusBars.js
101 101 yoffset -= height * 1.2; 102 102 }; 103 103 104 var cmpMirage = Engine.QueryInterface(this.entity, IID_Mirage);105 106 104 var cmpPack = Engine.QueryInterface(this.entity, IID_Pack); 107 105 if (cmpPack && cmpPack.IsPacking()) 108 106 AddBar("pack", cmpPack.GetProgress()); 109 107 110 var cmpHealth = Engine.QueryInterface(this.entity, IID_Health);108 var cmpHealth = QueryMiragedInterface(this.entity, IID_Health); 111 109 if (cmpHealth && cmpHealth.GetHitpoints() > 0) 112 110 AddBar("health", cmpHealth.GetHitpoints() / cmpHealth.GetMaxHitpoints()); 113 else if (cmpMirage && cmpMirage.Health())114 AddBar("health", cmpMirage.GetHitpoints() / cmpMirage.GetMaxHitpoints());115 111 116 var cmpResourceSupply = Engine.QueryInterface(this.entity, IID_ResourceSupply);112 var cmpResourceSupply = QueryMiragedInterface(this.entity, IID_ResourceSupply); 117 113 if (cmpResourceSupply) 118 114 AddBar("supply", cmpResourceSupply.IsInfinite() ? 1 : cmpResourceSupply.GetCurrentAmount() / cmpResourceSupply.GetMaxAmount()); 119 else if (cmpMirage && cmpMirage.ResourceSupply())120 AddBar("supply", cmpMirage.IsInfinite() ? 1 : cmpMirage.GetAmount() / cmpMirage.GetMaxAmount());121 115 122 116 /* 123 117 // Rank icon disabled for now - see discussion around -
binaries/data/mods/public/simulation/components/UnitAI.js
1987 1987 var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); 1988 1988 var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); 1989 1989 var cmpMirage = Engine.QueryInterface(this.gatheringTarget, IID_Mirage); 1990 if ((!cmpMirage || !cmpMirage. ResourceSupply()) &&1990 if ((!cmpMirage || !cmpMirage.Mirages(IID_ResourceSupply)) && 1991 1991 (!cmpSupply || !cmpSupply.AddGatherer(cmpOwnership.GetOwner(), this.entity))) 1992 1992 { 1993 1993 // Save the current order's data in case we need it later … … 3903 3903 if (cmpFormation) 3904 3904 return true; 3905 3905 3906 var cmpMirage = Engine.QueryInterface(ent, IID_Mirage); 3907 if (cmpMirage) 3908 return true; 3909 3910 var cmpHealth = Engine.QueryInterface(ent, IID_Health); 3911 if (!cmpHealth) 3912 return false; 3913 3914 return (cmpHealth.GetHitpoints() != 0); 3906 var cmpHealth = QueryMiragedInterface(ent, IID_Health); 3907 return cmpHealth && cmpHealth.GetHitpoints() != 0; 3915 3908 }; 3916 3909 3917 3910 /** … … 5019 5012 // before we process the order then we still know what resource 5020 5013 // type to look for more of 5021 5014 var type; 5022 var cmpResourceSupply = Engine.QueryInterface(target, IID_ResourceSupply); 5023 var cmpMirage = Engine.QueryInterface(target, IID_Mirage); 5015 var cmpResourceSupply = QueryMiragedInterface(target, IID_ResourceSupply); 5024 5016 if (cmpResourceSupply) 5025 5017 type = cmpResourceSupply.GetType(); 5026 else if (cmpMirage && cmpMirage.ResourceSupply())5027 type = cmpMirage.GetType();5028 5018 else 5029 5019 error("CanGather allowed gathering from invalid entity"); 5030 5020 … … 5630 5620 if (this.IsTurret()) 5631 5621 return false; 5632 5622 // The target must be a valid resource supply, or the mirage of one. 5633 var cmpResourceSupply = Engine.QueryInterface(target, IID_ResourceSupply); 5634 var cmpMirage = Engine.QueryInterface(target, IID_Mirage); 5635 if (!cmpResourceSupply && !(cmpMirage && cmpMirage.ResourceSupply())) 5623 var cmpResourceSupply = QueryMiragedInterface(target, IID_ResourceSupply); 5624 if (!cmpResourceSupply) 5636 5625 return false; 5637 5626 5638 5627 // Formation controllers should always respond to commands -
binaries/data/mods/public/simulation/components/tests/test_GuiInterface.js
1 Engine.LoadHelperScript("Player.js"); 1 2 Engine.LoadComponentScript("interfaces/Attack.js"); 2 3 Engine.LoadComponentScript("interfaces/AlertRaiser.js"); 3 4 Engine.LoadComponentScript("interfaces/Auras.js"); -
binaries/data/mods/public/simulation/helpers/Player.js
223 223 } 224 224 225 225 /** 226 * Similar to Engine.QueryInterface but first checks if the entity 227 * mirages the interface. 228 */ 229 function QueryMiragedInterface(ent, iid) 230 { 231 var cmp = Engine.QueryInterface(ent, IID_Mirage); 232 if (cmp && !cmp.Mirages(iid)) 233 return null; 234 else if (!cmp) 235 cmp = Engine.QueryInterface(ent, iid); 236 237 return cmp; 238 } 239 240 /** 226 241 * Returns true if the entity 'target' is owned by an ally of 227 242 * the owner of 'entity'. 228 243 */ … … 379 394 Engine.RegisterGlobal("LoadPlayerSettings", LoadPlayerSettings); 380 395 Engine.RegisterGlobal("QueryOwnerInterface", QueryOwnerInterface); 381 396 Engine.RegisterGlobal("QueryPlayerIDInterface", QueryPlayerIDInterface); 397 Engine.RegisterGlobal("QueryMiragedInterface", QueryMiragedInterface); 382 398 Engine.RegisterGlobal("IsOwnedByAllyOfEntity", IsOwnedByAllyOfEntity); 383 399 Engine.RegisterGlobal("IsOwnedByMutualAllyOfEntity", IsOwnedByMutualAllyOfEntity); 384 400 Engine.RegisterGlobal("IsOwnedByPlayer", IsOwnedByPlayer);