Ticket #4102: 4102.4.diff
File 4102.4.diff, 23.5 KB (added by , 8 years ago) |
---|
-
binaries/data/mods/public/simulation/components/GarrisonHolder.js
65 65 this.allowGarrisoning = {}; 66 66 this.visibleGarrisonPoints = []; 67 67 if (this.template.VisibleGarrisonPoints) 68 { 69 for each (var offset in this.template.VisibleGarrisonPoints) 70 { 71 var o = {}; 72 o.x = +offset.X; 73 o.y = +offset.Y; 74 o.z = +offset.Z; 75 this.visibleGarrisonPoints.push({"offset":o, "entity": null}); 76 } 77 } 68 for (let name in this.template.VisibleGarrisonPoints) 69 this.visibleGarrisonPoints.push({ 70 "offset": { 71 "x": +this.template.VisibleGarrisonPoints[name].X, 72 "y": +this.template.VisibleGarrisonPoints[name].Y, 73 "z": +this.template.VisibleGarrisonPoints[name].Z 74 }, 75 "entity": null 76 }); 78 77 }; 79 78 80 79 /** … … 82 81 */ 83 82 GarrisonHolder.prototype.GetLoadingRange = function() 84 83 { 85 var max = +this.template.LoadingRange; 86 return { "max": max, "min": 0 }; 84 return { "max": +this.template.LoadingRange, "min": 0 }; 87 85 }; 88 86 89 87 /** … … 93 91 { 94 92 if (!this.template.Pickup || this.IsFull()) 95 93 return false; 96 varcmpOwner = Engine.QueryInterface(this.entity, IID_Ownership);94 let cmpOwner = Engine.QueryInterface(this.entity, IID_Ownership); 97 95 if (!cmpOwner) 98 96 return false; 99 var player = cmpOwner.GetOwner(); 100 return IsOwnedByPlayer(player, ent); 97 return IsOwnedByPlayer(cmpOwner.GetOwner(), ent); 101 98 }; 102 99 103 100 … … 163 160 */ 164 161 GarrisonHolder.prototype.IsGarrisoningAllowed = function() 165 162 { 166 for each (var allow in this.allowGarrisoning) 167 { 168 if (!allow) 163 for (let allow in this.allowGarrisoning) 164 if (!this.allowGarrisoning[allow]) 169 165 return false; 170 } 166 171 167 return true; 172 168 }; 173 169 … … 176 172 */ 177 173 GarrisonHolder.prototype.GetGarrisonedEntitiesCount = function() 178 174 { 179 varcount = 0;180 for each (var ent inthis.entities)175 let count = 0; 176 for (let ent of this.entities) 181 177 { 182 count++;183 varcmpGarrisonHolder = Engine.QueryInterface(ent, IID_GarrisonHolder);178 ++count; 179 let cmpGarrisonHolder = Engine.QueryInterface(ent, IID_GarrisonHolder); 184 180 if (cmpGarrisonHolder) 185 181 count += cmpGarrisonHolder.GetGarrisonedEntitiesCount(); 186 182 } … … 196 192 if (!this.IsGarrisoningAllowed()) 197 193 return false; 198 194 199 varcmpIdentity = Engine.QueryInterface(entity, IID_Identity);195 let cmpIdentity = Engine.QueryInterface(entity, IID_Identity); 200 196 if (!cmpIdentity) 201 197 return false; 202 var entityClasses = cmpIdentity.GetClassesList(); 203 return MatchesClassList(entityClasses, this.template.List._string); 198 return MatchesClassList(cmpIdentity.GetClassesList(), this.template.List._string); 204 199 }; 205 200 206 201 /** … … 211 206 */ 212 207 GarrisonHolder.prototype.Garrison = function(entity, vgpEntity) 213 208 { 214 varcmpPosition = Engine.QueryInterface(entity, IID_Position);209 let cmpPosition = Engine.QueryInterface(entity, IID_Position); 215 210 if (!cmpPosition) 216 211 return false; 217 212 … … 250 245 return false; 251 246 252 247 // Check if the unit is allowed to be garrisoned inside the building 253 if (!this.AllowedToGarrison(entity))248 if (!this.AllowedToGarrison(entity)) 254 249 return false; 255 250 256 251 // check the capacity 257 varextraCount = 0;258 varcmpGarrisonHolder = Engine.QueryInterface(entity, IID_GarrisonHolder);252 let extraCount = 0; 253 let cmpGarrisonHolder = Engine.QueryInterface(entity, IID_GarrisonHolder); 259 254 if (cmpGarrisonHolder) 260 255 extraCount += cmpGarrisonHolder.GetGarrisonedEntitiesCount(); 261 256 if (this.GetGarrisonedEntitiesCount() + extraCount >= this.GetCapacity()) … … 263 258 264 259 if (!this.timer && this.GetHealRate() > 0) 265 260 { 266 var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 267 this.timer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "HealTimeout", 1000, {}); 261 let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 262 if (cmpTimer) 263 this.timer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "HealTimeout", 1000, {}); 268 264 } 269 265 270 266 // Actual garrisoning happens here 271 267 this.entities.push(entity); 272 268 this.UpdateGarrisonFlag(); 273 varcmpProductionQueue = Engine.QueryInterface(entity, IID_ProductionQueue);269 let cmpProductionQueue = Engine.QueryInterface(entity, IID_ProductionQueue); 274 270 if (cmpProductionQueue) 275 271 cmpProductionQueue.PauseProduction(); 276 272 277 varcmpAura = Engine.QueryInterface(entity, IID_Auras);273 let cmpAura = Engine.QueryInterface(entity, IID_Auras); 278 274 if (cmpAura && cmpAura.HasGarrisonAura()) 279 275 cmpAura.ApplyGarrisonBonus(this.entity); 280 276 281 Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added" 277 Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added": [entity], "removed": [] }); 282 278 283 varcmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI);279 let cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI); 284 280 if (cmpUnitAI && cmpUnitAI.IsUnderAlert()) 285 Engine.PostMessage(cmpUnitAI.GetAlertRaiser(), MT_UnitGarrisonedAfterAlert, { "holder": this.entity, "unit": entity});281 Engine.PostMessage(cmpUnitAI.GetAlertRaiser(), MT_UnitGarrisonedAfterAlert, { "holder": this.entity, "unit": entity }); 286 282 287 283 return true; 288 284 }; … … 294 290 */ 295 291 GarrisonHolder.prototype.Eject = function(entity, forced) 296 292 { 297 varentityIndex = this.entities.indexOf(entity);293 let entityIndex = this.entities.indexOf(entity); 298 294 // Error: invalid entity ID, usually it's already been ejected 299 295 if (entityIndex == -1) 300 296 return false; // Fail 301 297 302 298 // Find spawning location 303 varcmpFootprint = Engine.QueryInterface(this.entity, IID_Footprint);304 varcmpHealth = Engine.QueryInterface(this.entity, IID_Health);305 varcmpIdentity = Engine.QueryInterface(this.entity, IID_Identity);299 let cmpFootprint = Engine.QueryInterface(this.entity, IID_Footprint); 300 let cmpHealth = Engine.QueryInterface(this.entity, IID_Health); 301 let cmpIdentity = Engine.QueryInterface(this.entity, IID_Identity); 306 302 // If the garrisonHolder is a sinking ship, restrict the location to the intersection of both passabilities 307 303 // TODO: should use passability classes to be more generic 308 if ((!cmpHealth || cmpHealth.GetHitpoints() == 0) && cmpIdentity && cmpIdentity.HasClass("Ship")) 309 var pos = cmpFootprint.PickSpawnPointBothPass(entity); 310 else 311 var pos = cmpFootprint.PickSpawnPoint(entity); 304 let pos = (!cmpHealth || cmpHealth.GetHitpoints() === 0) && cmpIdentity && cmpIdentity.HasClass("Ship") ? 305 cmpFootprint.PickSpawnPointBothPass(entity) : 306 cmpFootprint.PickSpawnPoint(entity); 312 307 313 308 if (pos.y < 0) 314 309 { 315 310 // Error: couldn't find space satisfying the unit's passability criteria 316 if (forced) 317 { // If ejection is forced, we need to continue, so use center of the building 318 var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); 311 if (!forced) 312 return false; 313 // If ejection is forced, we need to continue, so use center of the building 314 let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); 315 if (cmpPosition) 319 316 pos = cmpPosition.GetPosition(); 320 }321 else322 { // Fail323 return false;324 }325 317 } 326 318 327 varcmpNewPosition = Engine.QueryInterface(entity, IID_Position);319 let cmpNewPosition = Engine.QueryInterface(entity, IID_Position); 328 320 this.entities.splice(entityIndex, 1); 329 321 330 for (var vgp of this.visibleGarrisonPoints) 322 let cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI); 323 for (let vgp of this.visibleGarrisonPoints) 331 324 { 332 325 if (vgp.entity != entity) 333 326 continue; 334 327 cmpNewPosition.SetTurretParent(INVALID_ENTITY, new Vector3D()); 335 var cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI);336 328 if (cmpUnitAI) 337 329 cmpUnitAI.ResetTurretStance(); 338 330 vgp.entity = null; … … 339 331 break; 340 332 } 341 333 342 var cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI);343 334 if (cmpUnitAI) 344 335 cmpUnitAI.Ungarrison(); 345 336 346 varcmpProductionQueue = Engine.QueryInterface(entity, IID_ProductionQueue);337 let cmpProductionQueue = Engine.QueryInterface(entity, IID_ProductionQueue); 347 338 if (cmpProductionQueue) 348 339 cmpProductionQueue.UnpauseProduction(); 349 340 350 varcmpAura = Engine.QueryInterface(entity, IID_Auras);341 let cmpAura = Engine.QueryInterface(entity, IID_Auras); 351 342 if (cmpAura && cmpAura.HasGarrisonAura()) 352 343 cmpAura.RemoveGarrisonBonus(this.entity); 353 344 … … 356 347 cmpNewPosition.SetHeightOffset(0); 357 348 // TODO: what direction should they face in? 358 349 359 Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added" 350 Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added": [], "removed": [entity] }); 360 351 361 352 return true; 362 353 }; … … 366 357 */ 367 358 GarrisonHolder.prototype.OrderWalkToRallyPoint = function(entities) 368 359 { 369 varcmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);370 varcmpRallyPoint = Engine.QueryInterface(this.entity, IID_RallyPoint);360 let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); 361 let cmpRallyPoint = Engine.QueryInterface(this.entity, IID_RallyPoint); 371 362 if (cmpRallyPoint) 372 363 { 373 varrallyPos = cmpRallyPoint.GetPositions()[0];364 let rallyPos = cmpRallyPoint.GetPositions()[0]; 374 365 if (rallyPos) 375 366 { 376 varcommands = GetRallyPointCommands(cmpRallyPoint, entities);367 let commands = GetRallyPointCommands(cmpRallyPoint, entities); 377 368 // ignore the rally point if it is autogarrison 378 369 if (commands[0].type == "garrison" && commands[0].target == this.entity) 379 370 return; 380 for each (var com in commands) 381 { 382 ProcessCommand(cmpOwnership.GetOwner(), com); 383 } 371 for (let command of commands) 372 ProcessCommand(cmpOwnership.GetOwner(), command); 384 373 } 385 374 } 386 375 }; … … 396 385 if (!this.IsGarrisoningAllowed() && !forced) 397 386 return false; 398 387 399 varejectedEntities = [];400 varsuccess = true;401 varfailedRadius;402 for ( varentity of entities)388 let ejectedEntities = []; 389 let success = true; 390 let failedRadius; 391 for (let entity of entities) 403 392 { 404 393 if (failedRadius !== undefined) 405 394 { 406 varcmpObstruction = Engine.QueryInterface(entity, IID_Obstruction);407 varradius = cmpObstruction ? cmpObstruction.GetUnitRadius() : 0;395 let cmpObstruction = Engine.QueryInterface(entity, IID_Obstruction); 396 let radius = cmpObstruction ? cmpObstruction.GetUnitRadius() : 0; 408 397 if (radius >= failedRadius) 409 398 continue; 410 399 } … … 411 400 412 401 if (this.Eject(entity, forced)) 413 402 { 414 varcmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);415 varcmpEntOwnership = Engine.QueryInterface(entity, IID_Ownership);403 let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); 404 let cmpEntOwnership = Engine.QueryInterface(entity, IID_Ownership); 416 405 if (cmpOwnership && cmpEntOwnership && cmpOwnership.GetOwner() == cmpEntOwnership.GetOwner()) 417 406 ejectedEntities.push(entity); 418 407 } … … 423 412 failedRadius = Math.min(failedRadius, radius); 424 413 else 425 414 { 426 varcmpObstruction = Engine.QueryInterface(entity, IID_Obstruction);415 let cmpObstruction = Engine.QueryInterface(entity, IID_Obstruction); 427 416 failedRadius = cmpObstruction ? cmpObstruction.GetUnitRadius() : 0; 428 417 } 429 418 } … … 454 443 */ 455 444 GarrisonHolder.prototype.UnloadTemplate = function(extendedTemplate, all, forced) 456 445 { 457 varindex = extendedTemplate.indexOf("&");446 let index = extendedTemplate.indexOf("&"); 458 447 if (index == -1) 459 448 return false; 460 449 461 varowner = +extendedTemplate.slice(1,index);462 vartemplate = extendedTemplate.slice(index+1);450 let owner = +extendedTemplate.slice(1,index); 451 let template = extendedTemplate.slice(index+1); 463 452 464 varentities = [];465 varcmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);466 for each (var entity inthis.entities)453 let entities = []; 454 let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); 455 for (let entity of this.entities) 467 456 { 468 varcmpIdentity = Engine.QueryInterface(entity, IID_Identity);457 let cmpIdentity = Engine.QueryInterface(entity, IID_Identity); 469 458 470 459 // Units with multiple ranks are grouped together. 471 var name = cmpIdentity.GetSelectionGroupName() 472 || cmpTemplateManager.GetCurrentTemplateName(entity); 460 let name = cmpIdentity.GetSelectionGroupName() || cmpTemplateManager.GetCurrentTemplateName(entity); 473 461 474 462 if (name != template) 475 463 continue; … … 493 481 */ 494 482 GarrisonHolder.prototype.UnloadAllByOwner = function(owner, forced) 495 483 { 496 varentities = this.entities.filter(ent => {497 varcmpOwnership = Engine.QueryInterface(ent, IID_Ownership);484 let entities = this.entities.filter(ent => { 485 let cmpOwnership = Engine.QueryInterface(ent, IID_Ownership); 498 486 return cmpOwnership && cmpOwnership.GetOwner() == owner; 499 487 }); 500 488 return this.PerformEject(entities, forced); … … 507 495 */ 508 496 GarrisonHolder.prototype.UnloadAll = function(forced) 509 497 { 510 var entities = this.entities.slice(); 511 return this.PerformEject(entities, forced); 498 return this.PerformEject(this.entities.slice(), forced); 512 499 }; 513 500 514 501 /** … … 518 505 GarrisonHolder.prototype.OnHealthChanged = function(msg) 519 506 { 520 507 if (!this.HasEnoughHealth() && this.entities.length) 521 { 522 var entities = this.entities.slice(); 523 this.EjectOrKill(entities); 524 } 508 this.EjectOrKill(this.entities.slice()); 525 509 }; 526 510 527 511 /** … … 529 513 */ 530 514 GarrisonHolder.prototype.HasEnoughHealth = function() 531 515 { 532 varcmpHealth = Engine.QueryInterface(this.entity, IID_Health);533 varhitpoints = cmpHealth.GetHitpoints();534 varmaxHitpoints = cmpHealth.GetMaxHitpoints();535 varejectHitpoints = Math.floor((+this.template.EjectHealth) * maxHitpoints);516 let cmpHealth = Engine.QueryInterface(this.entity, IID_Health); 517 let hitpoints = cmpHealth.GetHitpoints(); 518 let maxHitpoints = cmpHealth.GetMaxHitpoints(); 519 let ejectHitpoints = Math.floor((+this.template.EjectHealth) * maxHitpoints); 536 520 return hitpoints > ejectHitpoints; 537 521 }; 538 522 … … 541 525 */ 542 526 GarrisonHolder.prototype.HealTimeout = function(data) 543 527 { 544 if (this.entities.length == 0) 528 let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 529 if (!this.entities.length) 545 530 { 546 var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);547 531 cmpTimer.CancelTimer(this.timer); 548 532 this.timer = undefined; 533 return; 549 534 } 550 else 535 536 for (let entity of this.entities) 551 537 { 552 for each (var entity in this.entities) 538 let cmpHealth = Engine.QueryInterface(entity, IID_Health); 539 if (cmpHealth) 553 540 { 554 var cmpHealth = Engine.QueryInterface(entity, IID_Health); 555 if (cmpHealth) 556 { 557 // We do not want to heal unhealable units 558 if (!cmpHealth.IsUnhealable()) 559 cmpHealth.Increase(this.GetHealRate()); 560 } 541 // We do not want to heal unhealable units 542 if (!cmpHealth.IsUnhealable()) 543 cmpHealth.Increase(this.GetHealRate()); 561 544 } 562 var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);563 this.timer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "HealTimeout", 1000, {});564 545 } 546 this.timer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "HealTimeout", 1000, {}); 565 547 }; 566 548 567 549 GarrisonHolder.prototype.UpdateGarrisonFlag = function() … … 580 562 { 581 563 if (this.timer) 582 564 { 583 var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 584 cmpTimer.CancelTimer(this.timer); 565 let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 566 if (cmpTimer) 567 cmpTimer.CancelTimer(this.timer); 585 568 } 586 569 }; 587 570 … … 595 578 // the ownership change may be on the garrisonholder 596 579 if (this.entity == msg.entity) 597 580 { 598 varentities = [];599 for each (var entity inthis.entities)581 let entities = []; 582 for (let entity of this.entities) 600 583 { 601 584 if (msg.to == -1 || !IsOwnedByMutualAllyOfEntity(this.entity, entity)) 602 585 entities.push(entity); … … 607 590 } 608 591 609 592 // or on some of its garrisoned units 610 varentityIndex = this.entities.indexOf(msg.entity);593 let entityIndex = this.entities.indexOf(msg.entity); 611 594 if (entityIndex != -1) 612 595 { 613 596 // If the entity is dead, remove it directly instead of ejecting the corpse 614 varcmpHealth = Engine.QueryInterface(msg.entity, IID_Health);615 if (cmpHealth && cmpHealth.GetHitpoints() == 0)597 let cmpHealth = Engine.QueryInterface(msg.entity, IID_Health); 598 if (cmpHealth && cmpHealth.GetHitpoints() === 0) 616 599 { 617 600 this.entities.splice(entityIndex, 1); 618 Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added" 601 Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added": [], "removed": [msg.entity] }); 619 602 this.UpdateGarrisonFlag(); 620 603 621 for ( varpt of this.visibleGarrisonPoints)604 for (let pt of this.visibleGarrisonPoints) 622 605 if (pt.entity == msg.entity) 623 606 pt.entity = null; 624 607 } … … 632 615 */ 633 616 GarrisonHolder.prototype.OnGlobalEntityRenamed = function(msg) 634 617 { 635 varentityIndex = this.entities.indexOf(msg.entity);618 let entityIndex = this.entities.indexOf(msg.entity); 636 619 if (entityIndex != -1) 637 620 { 638 621 let vgpRenamed; … … 671 654 */ 672 655 GarrisonHolder.prototype.OnDiplomacyChanged = function() 673 656 { 674 varentities = this.entities.filter(ent => !IsOwnedByMutualAllyOfEntity(this.entity, ent));657 let entities = this.entities.filter(ent => !IsOwnedByMutualAllyOfEntity(this.entity, ent)); 675 658 this.EjectOrKill(entities); 676 659 }; 677 660 … … 686 669 // is inside a holder which kills its entities, so do not eject) 687 670 if (cmpPosition.IsInWorld()) 688 671 { 689 var cmpGarrisonHolder = this; 690 var ejectables = entities.filter(function(ent) { return cmpGarrisonHolder.IsEjectable(ent); }); 672 let ejectables = entities.filter(ent => this.IsEjectable(ent), this); 691 673 if (ejectables.length) 692 674 this.PerformEject(ejectables, false); 693 675 } 694 676 695 677 // And destroy all remaining entities 696 varkilledEntities = [];697 for each (var entity inentities)678 let killedEntities = []; 679 for (let entity of entities) 698 680 { 699 varentityIndex = this.entities.indexOf(entity);681 let entityIndex = this.entities.indexOf(entity); 700 682 if (entityIndex == -1) 701 683 continue; 702 varcmpHealth = Engine.QueryInterface(entity, IID_Health);684 let cmpHealth = Engine.QueryInterface(entity, IID_Health); 703 685 if (cmpHealth) 704 686 cmpHealth.Kill(); 705 687 this.entities.splice(entityIndex, 1); … … 707 689 } 708 690 709 691 if (killedEntities.length > 0) 710 Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added" : [], "removed": killedEntities });692 Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added": [], "removed": killedEntities }); 711 693 this.UpdateGarrisonFlag(); 712 694 }; 713 695 … … 716 698 */ 717 699 GarrisonHolder.prototype.IsEjectable = function(entity) 718 700 { 719 varejectableClasses = this.template.EjectClassesOnDestroy._string;701 let ejectableClasses = this.template.EjectClassesOnDestroy._string; 720 702 ejectableClasses = ejectableClasses ? ejectableClasses.split(/\s+/) : []; 721 var entityClasses = (Engine.QueryInterface(entity, IID_Identity)).GetClassesList(); 722 for each (var ejectableClass in ejectableClasses) 723 if (entityClasses.indexOf(ejectableClass) != -1) 724 return true; 725 726 return false; 703 let entityClasses = (Engine.QueryInterface(entity, IID_Identity)).GetClassesList(); 704 return ejectableClasses.some(ejectableClass => entityClasses.indexOf(ejectableClass) != -1); 727 705 }; 728 706 729 707 /** … … 744 722 }; 745 723 746 724 Engine.RegisterComponentType(IID_GarrisonHolder, "GarrisonHolder", GarrisonHolder); 747 -
binaries/data/mods/public/simulation/components/tests/test_GarrisonHolder.js
1 Engine.LoadHelperScript("ValueModification.js"); 2 Engine.LoadHelperScript("Player.js"); 3 Engine.LoadComponentScript("interfaces/GarrisonHolder.js"); 4 Engine.LoadComponentScript("GarrisonHolder.js"); 5 Engine.LoadComponentScript("interfaces/AuraManager.js"); 6 Engine.LoadComponentScript("interfaces/Auras.js"); 7 Engine.LoadComponentScript("interfaces/Heal.js"); 8 Engine.LoadComponentScript("interfaces/Health.js"); 9 Engine.LoadComponentScript("interfaces/ProductionQueue.js") 10 Engine.LoadComponentScript("interfaces/TechnologyManager.js"); 11 Engine.LoadComponentScript("interfaces/Timer.js"); 12 Engine.LoadComponentScript("interfaces/UnitAI.js") 13 Engine.LoadComponentScript("interfaces/RallyPoint.js"); 14 15 const garrisonedEntitiesList = [25, 26, 27]; 16 const garrisonHolderId = 15; 17 const unitToGarrisonId = 24; 18 const classes = "Infantry+Cavalry"; 19 20 ResetState(); 21 22 let holder = garrisonHolderId; 23 let unit = unitToGarrisonId; 24 25 AddMock(holder, IID_Footprint, { 26 "PickSpawnPointBothPass": () => ({ "x": 4, "y": 3, "z": 30 }), 27 "PickSpawnPoint": () => ({ "x": 4, "y": 3, "z": 30 }), 28 }); 29 AddMock(holder, IID_Health, { 30 "GetHitpoints": () => 500, 31 "GetMaxHitpoints": () => 600, 32 }); 33 AddMock(holder, IID_Obstruction, { 34 "GetUnitRadius": () => 0, 35 }); 36 AddMock(holder, IID_Position, { 37 "SetTurretParent": () => {}, 38 "GetTurretParent": () => INVALID_ENTITY, 39 "GetPosition": () => ({ "x": 4, "y": 3, "z": 25 }), 40 "GetRotation": () => ({ "y": 0 }), 41 "IsInWorld": () => true, 42 "GetHeightOffset": () => 0, 43 }); 44 45 AddMock(SYSTEM_ENTITY, IID_Timer, { 46 GetTime: () => 0, 47 SetTimeout: (ent, iid, funcname, time, data) => 0, 48 }); 49 50 for (let i = 24; i < 28; ++i) 51 { 52 AddMock(holder, IID_Health, { 53 "GetHitpoints": () => 190, 54 "GetMaxHitpoints": () => 200, 55 }); 56 AddMock(i, IID_Identity, { 57 "GetClassesList": () => "Infantry+Cavalry", 58 }); 59 AddMock(i, IID_Ownership, { 60 "GetOwner": () => 1, 61 }); 62 AddMock(i, IID_UnitAI, { 63 "SetTurretStance": () => {}, 64 "CanGarrison": () => true, 65 "IsUnderAlert": () => false, 66 "ResetTurretStance": () => {}, 67 "Ungarrison" : () => {}, 68 }); 69 AddMock(i, IID_Position, { 70 "GetHeightOffset": () => 0, 71 "GetPosition": () => ({ "x": 4, "y": 3, "z": 25 }), 72 "GetRotation": () => ({ "y": 0 }), 73 "GetTurretParent": () => INVALID_ENTITY, 74 "IsInWorld": () => true, 75 "JumpTo": () => {}, 76 "MoveOutOfWorld": () => {}, 77 "SetHeightOffset":() => {}, 78 "SetTurretParent": () => {}, 79 }); 80 81 } 82 83 84 85 86 let cmpGarrisonHolder = ConstructComponent(holder, "GarrisonHolder", { 87 "Max" : 4, 88 "List": {"_string": "Infantry+Cavalry"}, 89 "EjectHealth": 0.1, 90 "EjectClassesOnDestroy": {"_string": "Infantry"}, 91 "BuffHeal": 1, 92 "LoadingRange": 2.1, 93 "Pickup": false, 94 "VisibleGarrisonPoints": { 95 "archer1": { 96 "X": 12, 97 "Y": 5, 98 "Z": 6 99 }, 100 "archer2": { 101 "X": 15, 102 "Y": 5, 103 "Z": 6 104 } 105 } 106 }); 107 108 cmpGarrisonHolder.Init(); 109 110 111 TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetLoadingRange(), { "max": 2.1, "min": 0 }); 112 TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), []); 113 TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetHealRate(), 1); 114 TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetAllowedClasses(), classes); 115 TS_ASSERT_EQUALS(cmpGarrisonHolder.GetCapacity(), 4); 116 TS_ASSERT_EQUALS(cmpGarrisonHolder.GetGarrisonedEntitiesCount(), 0); 117 TS_ASSERT_EQUALS(cmpGarrisonHolder.CanPickup(unitToGarrisonId), false); 118 TS_ASSERT_EQUALS(cmpGarrisonHolder.IsFull(), false); 119 TS_ASSERT_EQUALS(cmpGarrisonHolder.Garrison(unitToGarrisonId), true); 120 for (let entity of garrisonedEntitiesList) 121 TS_ASSERT_EQUALS(cmpGarrisonHolder.Garrison(entity), true); 122 TS_ASSERT_EQUALS(cmpGarrisonHolder.IsFull(), true); 123 TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), [24,25,26,27]); 124 TS_ASSERT_EQUALS(cmpGarrisonHolder.GetGarrisonedEntitiesCount(), 4); 125 TS_ASSERT_EQUALS(cmpGarrisonHolder.Unload(25), true); 126 TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), [24,26,27]); 127 TS_ASSERT_EQUALS(cmpGarrisonHolder.GetGarrisonedEntitiesCount(), 3); 128 TS_ASSERT_EQUALS(cmpGarrisonHolder.IsFull(), false); 129 TS_ASSERT_EQUALS(cmpGarrisonHolder.UnloadAll(), true); 130 TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), []); 131 132