Ticket #4102: 4102.10.diff
File 4102.10.diff, 23.7 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 104 100 /** 105 101 * Return the list of entities garrisoned inside 106 102 */ … … 163 159 */ 164 160 GarrisonHolder.prototype.IsGarrisoningAllowed = function() 165 161 { 166 for each (var allow in this.allowGarrisoning) 167 { 168 if (!allow) 162 for (let allow in this.allowGarrisoning) 163 if (!this.allowGarrisoning[allow]) 169 164 return false; 170 } 165 171 166 return true; 172 167 }; 173 168 … … 176 171 */ 177 172 GarrisonHolder.prototype.GetGarrisonedEntitiesCount = function() 178 173 { 179 varcount = 0;180 for each (var ent inthis.entities)174 let count = 0; 175 for (let ent of this.entities) 181 176 { 182 count++;183 varcmpGarrisonHolder = Engine.QueryInterface(ent, IID_GarrisonHolder);177 ++count; 178 let cmpGarrisonHolder = Engine.QueryInterface(ent, IID_GarrisonHolder); 184 179 if (cmpGarrisonHolder) 185 180 count += cmpGarrisonHolder.GetGarrisonedEntitiesCount(); 186 181 } … … 196 191 if (!this.IsGarrisoningAllowed()) 197 192 return false; 198 193 199 varcmpIdentity = Engine.QueryInterface(entity, IID_Identity);194 let cmpIdentity = Engine.QueryInterface(entity, IID_Identity); 200 195 if (!cmpIdentity) 201 196 return false; 202 var entityClasses = cmpIdentity.GetClassesList(); 203 return MatchesClassList(entityClasses, this.template.List._string); 197 return MatchesClassList(cmpIdentity.GetClassesList(), this.template.List._string); 204 198 }; 205 199 206 200 /** … … 211 205 */ 212 206 GarrisonHolder.prototype.Garrison = function(entity, vgpEntity) 213 207 { 214 varcmpPosition = Engine.QueryInterface(entity, IID_Position);208 let cmpPosition = Engine.QueryInterface(entity, IID_Position); 215 209 if (!cmpPosition) 216 210 return false; 217 211 … … 250 244 return false; 251 245 252 246 // Check if the unit is allowed to be garrisoned inside the building 253 if (!this.AllowedToGarrison(entity))247 if (!this.AllowedToGarrison(entity)) 254 248 return false; 255 249 256 250 // check the capacity 257 varextraCount = 0;258 varcmpGarrisonHolder = Engine.QueryInterface(entity, IID_GarrisonHolder);251 let extraCount = 0; 252 let cmpGarrisonHolder = Engine.QueryInterface(entity, IID_GarrisonHolder); 259 253 if (cmpGarrisonHolder) 260 254 extraCount += cmpGarrisonHolder.GetGarrisonedEntitiesCount(); 261 255 if (this.GetGarrisonedEntitiesCount() + extraCount >= this.GetCapacity()) … … 263 257 264 258 if (!this.timer && this.GetHealRate() > 0) 265 259 { 266 var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 260 let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 261 if (!cmpTimer) 262 error("cmpTimer is undefined"); 267 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 return false; // Fail296 return false; 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()) && 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); 319 pos = cmpPosition.GetPosition(); 320 } 321 else 322 { // Fail 311 if (!forced) 323 312 return false; 324 } 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) 316 error("cmpPosition is undefined"); 317 pos = cmpPosition.GetPosition(); 325 318 } 326 319 327 varcmpNewPosition = Engine.QueryInterface(entity, IID_Position);320 let cmpNewPosition = Engine.QueryInterface(entity, IID_Position); 328 321 this.entities.splice(entityIndex, 1); 329 322 330 for (var vgp of this.visibleGarrisonPoints) 323 let cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI); 324 for (let vgp of this.visibleGarrisonPoints) 331 325 { 332 326 if (vgp.entity != entity) 333 327 continue; 334 328 cmpNewPosition.SetTurretParent(INVALID_ENTITY, new Vector3D()); 335 var cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI);336 329 if (cmpUnitAI) 337 330 cmpUnitAI.ResetTurretStance(); 338 331 vgp.entity = null; … … 339 332 break; 340 333 } 341 334 342 var cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI);343 335 if (cmpUnitAI) 344 336 cmpUnitAI.Ungarrison(); 345 337 346 varcmpProductionQueue = Engine.QueryInterface(entity, IID_ProductionQueue);338 let cmpProductionQueue = Engine.QueryInterface(entity, IID_ProductionQueue); 347 339 if (cmpProductionQueue) 348 340 cmpProductionQueue.UnpauseProduction(); 349 341 350 varcmpAura = Engine.QueryInterface(entity, IID_Auras);342 let cmpAura = Engine.QueryInterface(entity, IID_Auras); 351 343 if (cmpAura && cmpAura.HasGarrisonAura()) 352 344 cmpAura.RemoveGarrisonBonus(this.entity); 353 345 … … 356 348 cmpNewPosition.SetHeightOffset(0); 357 349 // TODO: what direction should they face in? 358 350 359 Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added" 351 Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added": [], "removed": [entity] }); 360 352 361 353 return true; 362 354 }; … … 366 358 */ 367 359 GarrisonHolder.prototype.OrderWalkToRallyPoint = function(entities) 368 360 { 369 varcmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);370 varcmpRallyPoint = Engine.QueryInterface(this.entity, IID_RallyPoint);361 let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); 362 let cmpRallyPoint = Engine.QueryInterface(this.entity, IID_RallyPoint); 371 363 if (cmpRallyPoint) 372 364 { 373 varrallyPos = cmpRallyPoint.GetPositions()[0];365 let rallyPos = cmpRallyPoint.GetPositions()[0]; 374 366 if (rallyPos) 375 367 { 376 varcommands = GetRallyPointCommands(cmpRallyPoint, entities);368 let commands = GetRallyPointCommands(cmpRallyPoint, entities); 377 369 // ignore the rally point if it is autogarrison 378 370 if (commands[0].type == "garrison" && commands[0].target == this.entity) 379 371 return; 380 for each (var com in commands) 381 { 382 ProcessCommand(cmpOwnership.GetOwner(), com); 383 } 372 for (let command of commands) 373 ProcessCommand(cmpOwnership.GetOwner(), command); 384 374 } 385 375 } 386 376 }; … … 396 386 if (!this.IsGarrisoningAllowed() && !forced) 397 387 return false; 398 388 399 varejectedEntities = [];400 varsuccess = true;401 varfailedRadius;402 for ( varentity of entities)389 let ejectedEntities = []; 390 let success = true; 391 let failedRadius; 392 for (let entity of entities) 403 393 { 404 394 if (failedRadius !== undefined) 405 395 { 406 varcmpObstruction = Engine.QueryInterface(entity, IID_Obstruction);407 varradius = cmpObstruction ? cmpObstruction.GetUnitRadius() : 0;396 let cmpObstruction = Engine.QueryInterface(entity, IID_Obstruction); 397 let radius = cmpObstruction ? cmpObstruction.GetUnitRadius() : 0; 408 398 if (radius >= failedRadius) 409 399 continue; 410 400 } … … 411 401 412 402 if (this.Eject(entity, forced)) 413 403 { 414 varcmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);415 varcmpEntOwnership = Engine.QueryInterface(entity, IID_Ownership);404 let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); 405 let cmpEntOwnership = Engine.QueryInterface(entity, IID_Ownership); 416 406 if (cmpOwnership && cmpEntOwnership && cmpOwnership.GetOwner() == cmpEntOwnership.GetOwner()) 417 407 ejectedEntities.push(entity); 418 408 } … … 423 413 failedRadius = Math.min(failedRadius, radius); 424 414 else 425 415 { 426 varcmpObstruction = Engine.QueryInterface(entity, IID_Obstruction);416 let cmpObstruction = Engine.QueryInterface(entity, IID_Obstruction); 427 417 failedRadius = cmpObstruction ? cmpObstruction.GetUnitRadius() : 0; 428 418 } 429 419 } … … 454 444 */ 455 445 GarrisonHolder.prototype.UnloadTemplate = function(extendedTemplate, all, forced) 456 446 { 457 varindex = extendedTemplate.indexOf("&");447 let index = extendedTemplate.indexOf("&"); 458 448 if (index == -1) 459 449 return false; 460 450 461 varowner = +extendedTemplate.slice(1,index);462 vartemplate = extendedTemplate.slice(index+1);451 let owner = +extendedTemplate.slice(1,index); 452 let template = extendedTemplate.slice(index+1); 463 453 464 varentities = [];465 varcmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);466 for each (var entity inthis.entities)454 let entities = []; 455 let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); 456 for (let entity of this.entities) 467 457 { 468 varcmpIdentity = Engine.QueryInterface(entity, IID_Identity);458 let cmpIdentity = Engine.QueryInterface(entity, IID_Identity); 469 459 470 460 // Units with multiple ranks are grouped together. 471 var name = cmpIdentity.GetSelectionGroupName() 472 || cmpTemplateManager.GetCurrentTemplateName(entity); 461 let name = cmpIdentity.GetSelectionGroupName() || cmpTemplateManager.GetCurrentTemplateName(entity); 473 462 474 463 if (name != template) 475 464 continue; … … 493 482 */ 494 483 GarrisonHolder.prototype.UnloadAllByOwner = function(owner, forced) 495 484 { 496 varentities = this.entities.filter(ent => {497 varcmpOwnership = Engine.QueryInterface(ent, IID_Ownership);485 let entities = this.entities.filter(ent => { 486 let cmpOwnership = Engine.QueryInterface(ent, IID_Ownership); 498 487 return cmpOwnership && cmpOwnership.GetOwner() == owner; 499 488 }); 500 489 return this.PerformEject(entities, forced); … … 507 496 */ 508 497 GarrisonHolder.prototype.UnloadAll = function(forced) 509 498 { 510 var entities = this.entities.slice(); 511 return this.PerformEject(entities, forced); 499 return this.PerformEject(this.entities.slice(), forced); 512 500 }; 513 501 514 502 /** … … 518 506 GarrisonHolder.prototype.OnHealthChanged = function(msg) 519 507 { 520 508 if (!this.HasEnoughHealth() && this.entities.length) 521 { 522 var entities = this.entities.slice(); 523 this.EjectOrKill(entities); 524 } 509 this.EjectOrKill(this.entities.slice()); 525 510 }; 526 511 527 512 /** … … 529 514 */ 530 515 GarrisonHolder.prototype.HasEnoughHealth = function() 531 516 { 532 varcmpHealth = Engine.QueryInterface(this.entity, IID_Health);533 varhitpoints = cmpHealth.GetHitpoints();534 varmaxHitpoints = cmpHealth.GetMaxHitpoints();535 varejectHitpoints = Math.floor((+this.template.EjectHealth) * maxHitpoints);517 let cmpHealth = Engine.QueryInterface(this.entity, IID_Health); 518 let hitpoints = cmpHealth.GetHitpoints(); 519 let maxHitpoints = cmpHealth.GetMaxHitpoints(); 520 let ejectHitpoints = Math.floor((+this.template.EjectHealth) * maxHitpoints); 536 521 return hitpoints > ejectHitpoints; 537 522 }; 538 523 … … 541 526 */ 542 527 GarrisonHolder.prototype.HealTimeout = function(data) 543 528 { 544 if (this.entities.length == 0) 529 let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 530 if (!this.entities.length) 545 531 { 546 var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);547 532 cmpTimer.CancelTimer(this.timer); 548 533 this.timer = undefined; 534 return; 549 535 } 550 else 536 537 for (let entity of this.entities) 551 538 { 552 for each (var entity in this.entities) 539 let cmpHealth = Engine.QueryInterface(entity, IID_Health); 540 if (cmpHealth) 553 541 { 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 } 542 // We do not want to heal unhealable units 543 if (!cmpHealth.IsUnhealable()) 544 cmpHealth.Increase(this.GetHealRate()); 561 545 } 562 var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);563 this.timer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "HealTimeout", 1000, {});564 546 } 547 this.timer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "HealTimeout", 1000, {}); 565 548 }; 566 549 567 550 GarrisonHolder.prototype.UpdateGarrisonFlag = function() … … 580 563 { 581 564 if (this.timer) 582 565 { 583 var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 584 cmpTimer.CancelTimer(this.timer); 566 let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 567 if (cmpTimer) 568 cmpTimer.CancelTimer(this.timer); 585 569 } 586 570 }; 587 571 … … 595 579 // the ownership change may be on the garrisonholder 596 580 if (this.entity == msg.entity) 597 581 { 598 varentities = [];599 for each (var entity inthis.entities)582 let entities = []; 583 for (let entity of this.entities) 600 584 { 601 585 if (msg.to == -1 || !IsOwnedByMutualAllyOfEntity(this.entity, entity)) 602 586 entities.push(entity); … … 607 591 } 608 592 609 593 // or on some of its garrisoned units 610 varentityIndex = this.entities.indexOf(msg.entity);594 let entityIndex = this.entities.indexOf(msg.entity); 611 595 if (entityIndex != -1) 612 596 { 613 597 // 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)598 let cmpHealth = Engine.QueryInterface(msg.entity, IID_Health); 599 if (cmpHealth && !cmpHealth.GetHitpoints()) 616 600 { 617 601 this.entities.splice(entityIndex, 1); 618 Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added" 602 Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added": [], "removed": [msg.entity] }); 619 603 this.UpdateGarrisonFlag(); 620 604 621 for ( varpt of this.visibleGarrisonPoints)605 for (let pt of this.visibleGarrisonPoints) 622 606 if (pt.entity == msg.entity) 623 607 pt.entity = null; 624 608 } … … 632 616 */ 633 617 GarrisonHolder.prototype.OnGlobalEntityRenamed = function(msg) 634 618 { 635 varentityIndex = this.entities.indexOf(msg.entity);619 let entityIndex = this.entities.indexOf(msg.entity); 636 620 if (entityIndex != -1) 637 621 { 638 622 let vgpRenamed; … … 665 649 } 666 650 }; 667 651 668 669 652 /** 670 653 * Eject all foreign garrisoned entities which are no more allied 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": (entity) => new Vector3D(4, 3, 30), 27 "PickSpawnPoint": (entity) => new Vector3D(4, 3, 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": (entity, offset) => {}, 38 "GetTurretParent": () => INVALID_ENTITY, 39 "GetPosition": () => new Vector3D(4, 3, 25), 40 "GetRotation": () => new Vector3D(4, 0, 6), 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": (entity) => true, 65 "IsUnderAlert": () => false, 66 "ResetTurretStance": () => {}, 67 "Ungarrison": () => {} 68 }); 69 AddMock(i, IID_Position, { 70 "GetHeightOffset": () => 0, 71 "GetPosition": () => new Vector3D(4, 3, 25), 72 "GetRotation": () => new Vector3D(4, 0, 6), 73 "GetTurretParent": () => INVALID_ENTITY, 74 "IsInWorld": () => true, 75 "JumpTo": (posX, posZ) => {}, 76 "MoveOutOfWorld": () => {}, 77 "SetTurretParent": (entity, offset) => {}, 78 "SetHeightOffset": (height) => {} 79 }); 80 } 81 82 let cmpGarrisonHolder = ConstructComponent(holder, "GarrisonHolder", { 83 "Max" : 4, 84 "List": { "_string": "Infantry+Cavalry" }, 85 "EjectHealth": 0.1, 86 "EjectClassesOnDestroy": { "_string": "Infantry" }, 87 "BuffHeal": 1, 88 "LoadingRange": 2.1, 89 "Pickup": false, 90 "VisibleGarrisonPoints": { 91 "archer1": { 92 "X": 12, 93 "Y": 5, 94 "Z": 6 95 }, 96 "archer2": { 97 "X": 15, 98 "Y": 5, 99 "Z": 6 100 } 101 } 102 }); 103 104 cmpGarrisonHolder.Init(); 105 106 TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetLoadingRange(), { "max": 2.1, "min": 0 }); 107 TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), []); 108 TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetHealRate(), 1); 109 TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetAllowedClasses(), classes); 110 TS_ASSERT_EQUALS(cmpGarrisonHolder.GetCapacity(), 4); 111 TS_ASSERT_EQUALS(cmpGarrisonHolder.GetGarrisonedEntitiesCount(), 0); 112 TS_ASSERT_EQUALS(cmpGarrisonHolder.CanPickup(unitToGarrisonId), false); 113 TS_ASSERT_EQUALS(cmpGarrisonHolder.IsFull(), false); 114 TS_ASSERT_EQUALS(cmpGarrisonHolder.Garrison(unitToGarrisonId), true); 115 for (let entity of garrisonedEntitiesList) 116 TS_ASSERT_EQUALS(cmpGarrisonHolder.Garrison(entity), true); 117 TS_ASSERT_EQUALS(cmpGarrisonHolder.IsFull(), true); 118 TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), [24, 25, 26, 27]); 119 TS_ASSERT_EQUALS(cmpGarrisonHolder.GetGarrisonedEntitiesCount(), 4); 120 TS_ASSERT_EQUALS(cmpGarrisonHolder.Unload(25), true); 121 TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), [24, 26, 27]); 122 TS_ASSERT_EQUALS(cmpGarrisonHolder.GetGarrisonedEntitiesCount(), 3); 123 TS_ASSERT_EQUALS(cmpGarrisonHolder.IsFull(), false); 124 TS_ASSERT_EQUALS(cmpGarrisonHolder.UnloadAll(), true); 125 TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), []); 126 127