Ticket #4334: TransformFix2.patch

File TransformFix2.patch, 9.3 KB (added by wraitii, 7 years ago)
  • binaries/data/mods/public/simulation/components/Gate.js

    diff --git a/binaries/data/mods/public/simulation/components/Gate.js b/binaries/data/mods/public/simulation/components/Gate.js
    index faa5c11..bce5fbe 100644
    a b Gate.prototype.IsLocked = function()  
    137137/**
    138138 * Lock the gate, with sound. It will close at the next opportunity.
    139139 */
    140 Gate.prototype.LockGate = function()
     140Gate.prototype.LockGate = function(quiet)
    141141{
    142142    this.locked = true;
    143143    // If the door is closed, enable 'block pathfinding'
    Gate.prototype.LockGate = function()  
    153153        this.OperateGate();
    154154
    155155    // TODO: Possibly move the lock/unlock sounds to UI? Needs testing
    156     PlaySound("gate_locked", this.entity);
     156    if (!quiet)
     157        PlaySound("gate_locked", this.entity);
    157158};
    158159
    159160/**
  • binaries/data/mods/public/simulation/components/Promotion.js

    diff --git a/binaries/data/mods/public/simulation/components/Promotion.js b/binaries/data/mods/public/simulation/components/Promotion.js
    index 9ad78c8..4760cd5 100644
    a b Promotion.prototype.GetPromotedTemplateName = function()  
    3030
    3131Promotion.prototype.Promote = function(promotedTemplateName)
    3232{
    33     // If the unit is dead, don't promote it
    34     var cmpCurrentUnitHealth = Engine.QueryInterface(this.entity, IID_Health);
     33    let cmpCurrentUnitHealth = Engine.QueryInterface(this.entity, IID_Health);
    3534    if (cmpCurrentUnitHealth.GetHitpoints() == 0)
    3635        return;
    3736
    38     // Create promoted unit entity
    39     var promotedUnitEntity = Engine.AddEntity(promotedTemplateName);
    40 
    41     // Copy parameters from current entity to promoted one
    42     var cmpCurrentUnitPosition = Engine.QueryInterface(this.entity, IID_Position);
    43     var cmpPromotedUnitPosition = Engine.QueryInterface(promotedUnitEntity, IID_Position);
    44     if (cmpCurrentUnitPosition.IsInWorld())
    45     {
    46         var pos = cmpCurrentUnitPosition.GetPosition2D();
    47         cmpPromotedUnitPosition.JumpTo(pos.x, pos.y);
    48     }
    49     var rot = cmpCurrentUnitPosition.GetRotation();
    50     cmpPromotedUnitPosition.SetYRotation(rot.y);
    51     cmpPromotedUnitPosition.SetXZRotation(rot.x, rot.z);
    52     var heightOffset = cmpCurrentUnitPosition.GetHeightOffset();
    53     cmpPromotedUnitPosition.SetHeightOffset(heightOffset);
     37    if (!CanGarrisonedChangeTemplate(this.entity,promotedTemplateName))
     38        return;
    5439
    55     var cmpCurrentUnitOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
    56     var cmpPromotedUnitOwnership = Engine.QueryInterface(promotedUnitEntity, IID_Ownership);
    57     cmpPromotedUnitOwnership.SetOwner(cmpCurrentUnitOwnership.GetOwner());
     40    this.promotedUnitEntity = ChangeEntityTemplate(this.entity, promotedTemplateName);
    5841
    59     // change promoted unit health to the same percent of hitpoints as unit had before promotion
    60     var cmpPromotedUnitHealth = Engine.QueryInterface(promotedUnitEntity, IID_Health);
    61     var healthFraction = Math.max(0, Math.min(1, cmpCurrentUnitHealth.GetHitpoints() / cmpCurrentUnitHealth.GetMaxHitpoints()));
    62     var promotedUnitHitpoints = Math.round(cmpPromotedUnitHealth.GetMaxHitpoints() * healthFraction);
    63     cmpPromotedUnitHealth.SetHitpoints(promotedUnitHitpoints);
     42    // cheer
     43    let cmpCurrentUnitPosition = Engine.QueryInterface(this.entity, IID_Position);
     44    let cmpPromotedUnitAI = Engine.QueryInterface(this.promotedUnitEntity, IID_UnitAI);
     45    if (cmpCurrentUnitPosition.IsInWorld() && !cmpPromotedUnitAI.IsGarrisoned())
     46        cmpPromotedUnitAI.Cheer();
    6447
    65     var cmpPromotedUnitPromotion = Engine.QueryInterface(promotedUnitEntity, IID_Promotion);
     48    // add promotion XP.
     49    let cmpPromotedUnitPromotion = Engine.QueryInterface(this.promotedUnitEntity, IID_Promotion);
    6650    if (cmpPromotedUnitPromotion)
    6751        cmpPromotedUnitPromotion.IncreaseXp(this.currentXp);
    68 
    69     var cmpCurrentUnitResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
    70     var cmpPromotedUnitResourceGatherer = Engine.QueryInterface(promotedUnitEntity, IID_ResourceGatherer);
    71     if (cmpCurrentUnitResourceGatherer && cmpPromotedUnitResourceGatherer)
    72     {
    73         var carriedResorces = cmpCurrentUnitResourceGatherer.GetCarryingStatus();
    74         cmpPromotedUnitResourceGatherer.GiveResources(carriedResorces);
    75     }
    76 
    77     var cmpCurrentUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI);
    78     var cmpPromotedUnitAI = Engine.QueryInterface(promotedUnitEntity, IID_UnitAI);
    79     var pos = cmpCurrentUnitAI.GetHeldPosition();
    80     if (pos)
    81         cmpPromotedUnitAI.SetHeldPosition(pos.x, pos.z);
    82     if (cmpCurrentUnitAI.GetStanceName())
    83         cmpPromotedUnitAI.SwitchToStance(cmpCurrentUnitAI.GetStanceName());
    84 
    85     var orders = cmpCurrentUnitAI.GetOrders();
    86     if (cmpCurrentUnitAI.IsGarrisoned())
    87         cmpPromotedUnitAI.SetGarrisoned();
    88     if (cmpCurrentUnitPosition.IsInWorld()) // do not cheer if not visibly garrisoned
    89         cmpPromotedUnitAI.Cheer(); 
    90     cmpPromotedUnitAI.AddOrders(orders);
    91 
    92     var workOrders = cmpCurrentUnitAI.GetWorkOrders();
    93     cmpPromotedUnitAI.SetWorkOrders(workOrders);
    94     cmpPromotedUnitAI.SetGuardOf(cmpCurrentUnitAI.IsGuardOf());
    95 
    96     var cmpCurrentUnitGuard = Engine.QueryInterface(this.entity, IID_Guard);
    97     var cmpPromotedUnitGuard = Engine.QueryInterface(promotedUnitEntity, IID_Guard);
    98     if (cmpCurrentUnitGuard && cmpPromotedUnitGuard)
    99         cmpPromotedUnitGuard.SetEntities(cmpCurrentUnitGuard.GetEntities());
    100 
    101     Engine.BroadcastMessage(MT_EntityRenamed, { entity: this.entity, newentity: promotedUnitEntity });
    102 
    103     // Destroy current entity
    104     Engine.DestroyEntity(this.entity);
    105     // save the entity id
    106     this.promotedUnitEntity = promotedUnitEntity;
    10752};
    10853
    10954Promotion.prototype.IncreaseXp = function(amount)
  • binaries/data/mods/public/simulation/components/UnitAI.js

    diff --git a/binaries/data/mods/public/simulation/components/UnitAI.js b/binaries/data/mods/public/simulation/components/UnitAI.js
    index e23138f..b370013 100644
    a b UnitAI.prototype.Flee = function(target, queued)  
    54235423 */
    54245424UnitAI.prototype.Cheer = function()
    54255425{
    5426     this.AddOrder("Cheering", { "force": true }, false);
     5426    this.AddOrder("Cheering", { "force": true }, true);
    54275427};
    54285428
    54295429UnitAI.prototype.Pack = function(queued)
  • binaries/data/mods/public/simulation/helpers/Transform.js

    diff --git a/binaries/data/mods/public/simulation/helpers/Transform.js b/binaries/data/mods/public/simulation/helpers/Transform.js
    index dbdeddd..9f6418d 100644
    a b  
    33// returns the ID of the new entity or INVALID_ENTITY.
    44function ChangeEntityTemplate(oldEnt, newTemplate)
    55{
    6     // Done un/packing, copy our parameters to the final entity
    76    var newEnt = Engine.AddEntity(newTemplate);
    87    if (newEnt == INVALID_ENTITY)
    98    {
    function ChangeEntityTemplate(oldEnt, newTemplate)  
    3130    if (cmpOwnership && cmpNewOwnership)
    3231        cmpNewOwnership.SetOwner(cmpOwnership.GetOwner());
    3332
    34     // Copy control groups
    3533    CopyControlGroups(oldEnt, newEnt);
    3634
    3735    // Rescale capture points
    function ChangeEntityTemplate(oldEnt, newTemplate)  
    5351        cmpNewHealth.SetHitpoints(Math.round(cmpNewHealth.GetMaxHitpoints() * healthLevel));
    5452    }
    5553
     54    var cmpUnitResourceGatherer = Engine.QueryInterface(oldEnt, IID_ResourceGatherer);
     55    var cmpNewUnitResourceGatherer = Engine.QueryInterface(newEnt, IID_ResourceGatherer);
     56    if (cmpUnitResourceGatherer && cmpNewUnitResourceGatherer)
     57        cmpNewUnitResourceGatherer.GiveResources(cmpUnitResourceGatherer.GetCarryingStatus());
     58
    5659    var cmpUnitAI = Engine.QueryInterface(oldEnt, IID_UnitAI);
    5760    var cmpNewUnitAI = Engine.QueryInterface(newEnt, IID_UnitAI);
    5861    if (cmpUnitAI && cmpNewUnitAI)
    function ChangeEntityTemplate(oldEnt, newTemplate)  
    6366        if (cmpUnitAI.GetStanceName())
    6467            cmpNewUnitAI.SwitchToStance(cmpUnitAI.GetStanceName());
    6568        cmpNewUnitAI.AddOrders(cmpUnitAI.GetOrders());
     69        cmpNewUnitAI.SetWorkOrders(cmpUnitAI.GetWorkOrders());
    6670        cmpNewUnitAI.SetGuardOf(cmpUnitAI.IsGuardOf());
     71        if (cmpUnitAI.IsGarrisoned())
     72            cmpNewUnitAI.SetGarrisoned();
    6773    }
    6874
    69     // Maintain the list of guards
    7075    var cmpGuard = Engine.QueryInterface(oldEnt, IID_Guard);
    7176    var cmpNewGuard = Engine.QueryInterface(newEnt, IID_Guard);
    7277    if (cmpGuard && cmpNewGuard)
    7378        cmpNewGuard.SetEntities(cmpGuard.GetEntities());
    7479
     80    var cmpGate = Engine.QueryInterface(oldEnt, IID_Gate);
     81    var cmpNewGate = Engine.QueryInterface(newEnt, IID_Gate);
     82    if (cmpGate && cmpNewGate)
     83    {
     84        cmpNewGate.opened = cmpGate.opened;
     85        if (cmpGate.IsLocked)
     86            cmpGate.LockGate(true);
     87    }
     88
     89    var cmpTrader = Engine.QueryInterface(oldEnt, IID_Trader);
     90    var cmpNewTrader = Engine.QueryInterface(newEnt, IID_Trader);
     91    if (cmpTrader && cmpNewTrader)
     92    {
     93        cmpNewTrader.markets = cmpTrader.markets;
     94        cmpNewTrader.index = cmpTrader.index;
     95        cmpNewTrader.goods = {};
     96        for (let p of cmpTrader.goods)
     97            cmpNewTrader.goods[p] = cmpTrader.goods[p];
     98    }
     99
    75100    TransferGarrisonedUnits(oldEnt, newEnt);
    76101
    77102    Engine.BroadcastMessage(MT_EntityRenamed, { "entity": oldEnt, "newentity": newEnt });
    function ChangeEntityTemplate(oldEnt, newTemplate)  
    83108
    84109function CanGarrisonedChangeTemplate(ent, template)
    85110{
    86     var cmpPosition = Engine.QueryInterface(ent, IID_Position);
    87111    var unitAI = Engine.QueryInterface(ent, IID_UnitAI);
    88     if (cmpPosition && !cmpPosition.IsInWorld() && unitAI && unitAI.IsGarrisoned())
     112    if (unitAI && unitAI.IsGarrisoned())
    89113    {
    90114        // We're a garrisoned unit, assume impossibility as I've been unable to find a way to get the holder ID.
    91115        // TODO: change this if that ever becomes possibles