Ticket #4334: TransformFix.patch

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

     
    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'
     
    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

     
    3131Promotion.prototype.Promote = function(promotedTemplateName)
    3232{
    3333    // If the unit is dead, don't promote it
    34     var cmpCurrentUnitHealth = Engine.QueryInterface(this.entity, IID_Health);
     34    let cmpCurrentUnitHealth = Engine.QueryInterface(this.entity, IID_Health);
    3535    if (cmpCurrentUnitHealth.GetHitpoints() == 0)
    3636        return;
    3737
    3838    // Create promoted unit entity
    39     var promotedUnitEntity = Engine.AddEntity(promotedTemplateName);
     39    this.promotedUnitEntity = ChangeEntityTemplate(this.entity, promotedTemplateName);
    4040
    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);
     41    // cheer
     42    let cmpCurrentUnitPosition = Engine.QueryInterface(this.entity, IID_Position);
     43    let cmpPromotedUnitAI = Engine.QueryInterface(this.promotedUnitEntity, IID_UnitAI);
    4444    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);
     45        cmpPromotedUnitAI.Cheer(); 
    5446
    55     var cmpCurrentUnitOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
    56     var cmpPromotedUnitOwnership = Engine.QueryInterface(promotedUnitEntity, IID_Ownership);
    57     cmpPromotedUnitOwnership.SetOwner(cmpCurrentUnitOwnership.GetOwner());
    58 
    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);
    64 
    65     var cmpPromotedUnitPromotion = Engine.QueryInterface(promotedUnitEntity, IID_Promotion);
     47    // add promotion XP.
     48    let cmpPromotedUnitPromotion = Engine.QueryInterface(this.promotedUnitEntity, IID_Promotion);
    6649    if (cmpPromotedUnitPromotion)
    6750        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;
    10751};
    10852
    10953Promotion.prototype.IncreaseXp = function(amount)
  • binaries/data/mods/public/simulation/helpers/Transform.js

     
    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    {
     
    3130    if (cmpOwnership && cmpNewOwnership)
    3231        cmpNewOwnership.SetOwner(cmpOwnership.GetOwner());
    3332
    34     // Copy control groups
    3533    CopyControlGroups(oldEnt, newEnt);
    3634
    3735    // Rescale capture points
     
    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)
     
    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 });