Ticket #3894: marketVisibility.patch

File marketVisibility.patch, 6.4 KB (added by mimo, 8 years ago)

patch to allow making a trade route with a miraged market

  • binaries/data/mods/public/simulation/components/Fogging.js

     
    129129    if (cmpResourceSupply)
    130130        cmpMirage.CopyResourceSupply(cmpResourceSupply);
    131131
     132    var cmpMarket = Engine.QueryInterface(this.entity, IID_Market);
     133    if (cmpMarket)
     134        cmpMirage.CopyMarket(cmpMarket);
     135
    132136    // Notify the GUI the entity has been replaced by a mirage, in case it is selected at this moment
    133137    var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
    134138    cmpGuiInterface.AddMiragedEntity(player, this.entity, this.mirages[player]);
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    289289    if (cmpBuilder)
    290290        ret.builder = true;
    291291
    292     let cmpMarket = Engine.QueryInterface(ent, IID_Market);
     292    let cmpMarket = QueryMiragedInterface(ent, IID_Market);
    293293    if (cmpMarket)
    294294        ret.market = {
    295295            "land": cmpMarket.HasType("land"),
  • binaries/data/mods/public/simulation/components/Market.js

     
    3333    return this.tradeType.has(type);
    3434};
    3535
     36Market.prototype.GetType = function()
     37{
     38    return this.tradeType;
     39};
     40
    3641/**
    3742 * Check if all traders with a route on this market can still trade
    3843 */
     
    5964        if (!cmpTrader)
    6065            this.RemoveTrader(ent);
    6166        else if (msg.to == -1)
    62             cmpTrader.RemoveMarket(this.entity);
     67        {
     68            let cmpOwnership = Engine.QueryInterface(ent, IID_Ownership);
     69            let cmpFogging = Engine.QueryInterface(this.entity, IID_Fogging);
     70            if (cmpOwnership && cmpFogging && cmpFogging.IsMiraged(cmpOwnership.GetOwner()))
     71                cmpTrader.SwitchMarket(this.entity, cmpFogging.GetMirage(cmpOwnership.GetOwner()));
     72            else
     73                cmpTrader.RemoveMarket(this.entity);
     74        }
    6375        else if (!cmpTrader.CanTrade(this.entity))
    6476        {
    6577            this.RemoveTrader(ent);
  • binaries/data/mods/public/simulation/components/Mirage.js

     
    3434    this.killBeforeGather = null;
    3535    this.maxGatherers = null;
    3636    this.numGatherers = null;
     37
     38    this.marketType = null;
    3739};
    3840
     41Mirage.prototype.GetParent = function()
     42{
     43    return this.parent;
     44};
     45
    3946Mirage.prototype.SetParent = function(ent)
    4047{
    4148    this.parent = ent;
     
    125132Mirage.prototype.GetMaxGatherers = function() { return this.maxGatherers; };
    126133Mirage.prototype.GetNumGatherers = function() { return this.numGatherers; };
    127134
     135// Market data
     136
     137Mirage.prototype.CopyMarket = function(cmpMarket)
     138{
     139    this.miragedIids.add(IID_Market);
     140    this.marketType = cmpMarket.GetType();
     141};
     142
     143Mirage.prototype.HasType = function(type) { return this.marketType.has(type); };
     144
    128145// ============================
    129146
    130147Mirage.prototype.OnVisibilityChanged = function(msg)
  • binaries/data/mods/public/simulation/components/Trader.js

     
    4545        {
    4646            var garrisonMultiplier = 1;
    4747            var garrisonedTradersCount = 0;
    48             for each (var entity in cmpGarrisonHolder.GetEntities())
     48            for (var entity of cmpGarrisonHolder.GetEntities())
    4949            {
    5050                var cmpGarrisonedUnitTrader = Engine.QueryInterface(entity, IID_Trader);
    5151                if (cmpGarrisonedUnitTrader)
     
    156156{
    157157    var cmpTraderIdentity = Engine.QueryInterface(this.entity, IID_Identity);
    158158    // Check that the target exists
    159     var cmpTargetMarket = Engine.QueryInterface(target, IID_Market);
     159    var cmpTargetMarket = QueryMiragedInterface(target, IID_Market);
    160160    if (!cmpTargetMarket)
    161161        return false;
    162162    // Check that the target is not a foundation
     
    248248};
    249249
    250250/**
    251  * Called when this trader can no longer trade with the market
     251 * The two following functions are called when this trader can no longer trade with the market:
     252 * - if the market is destroyed and not visible, it is replaced by its mirage if it exists
     253 * - otherwise the market is removed
    252254 */
    253255Trader.prototype.RemoveMarket = function(market)
    254256{
     
    257259        this.markets.splice(index, 1);
    258260};
    259261
     262Trader.prototype.SwitchMarket = function(market, miragedMarket)
     263{
     264    if (miragedMarket == INVALID_ENTITY)
     265    {
     266        this.RemoveMarket(market);
     267        return;
     268    }
     269    let index = this.markets.indexOf(market);
     270    if (index != -1)
     271        this.markets[index] = miragedMarket;
     272};
     273
    260274Trader.prototype.StopTrading = function()
    261275{
    262276    for (let market of this.markets)
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    648648    "Order.Trade": function(msg) {
    649649        // We must check if this trader has both markets in case it was a back-to-work order
    650650        var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
    651         if (!cmpTrader || ! cmpTrader.HasBothMarkets())
     651        if (!cmpTrader || !cmpTrader.HasBothMarkets())
    652652        {
    653653            this.FinishOrder();
    654654            return;
     
    51565156        return;
    51575157    }
    51585158
     5159    // Register the parent when dealing with mirages
     5160    if (target)
     5161    {
     5162        let cmpMirage = Engine.QueryInterface(target, IID_Mirage);
     5163        if (cmpMirage)
     5164        {
     5165            let parent = cmpMirage.GetParent();
     5166            if (parent && Engine.QueryInterface(parent, IID_Market))
     5167                target = parent;
     5168        }
     5169    }
     5170    if (source)
     5171    {
     5172        let cmpMirage = Engine.QueryInterface(source, IID_Mirage);
     5173        if (cmpMirage)
     5174        {
     5175            let parent = cmpMirage.GetParent();
     5176            if (parent && Engine.QueryInterface(parent, IID_Market))
     5177                source = parent;
     5178        }
     5179    }
     5180
    51595181    var marketsChanged = this.SetTargetMarket(target, source);
    51605182    if (!marketsChanged)
    51615183        return;