Ticket #3277: trading_v2.patch

File trading_v2.patch, 10.6 KB (added by otero, 8 years ago)
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    16601660    let firstMarket = cmpEntityTrader.GetFirstMarket();
    16611661    let secondMarket = cmpEntityTrader.GetSecondMarket();
    16621662    let result = null;
    1663     if (data.target === firstMarket)
     1663    if (!firstMarket)
    16641664    {
     1665        result = { "type": "set first" };
     1666    }
     1667    else if (!secondMarket)
     1668    {
    16651669        result = {
     1670            "type": "set second",
     1671            "gain": cmpEntityTrader.CalculateGain(firstMarket, data.target),
     1672        };
     1673    }
     1674    else if (data.target === firstMarket)
     1675    {
     1676        result = {
    16661677            "type": "is first",
    16671678            "hasBothMarkets": cmpEntityTrader.HasBothMarkets()
    16681679        };
     
    16761687            "gain": cmpEntityTrader.GetGain(),
    16771688        };
    16781689    }
    1679     else if (!firstMarket)
    1680     {
    1681         result = { "type": "set first" };
    1682     }
    1683     else if (!secondMarket)
    1684     {
    1685         result = {
    1686             "type": "set second",
    1687             "gain": cmpEntityTrader.CalculateGain(firstMarket, data.target),
    1688         };
    1689     }
    16901690    else
    16911691    {
    16921692        // Else both markets are not null and target is different from them
  • binaries/data/mods/public/simulation/components/Market.js

     
     1/**
     2 @file Market.js
     3 Component used for the management of market and trade units in the field
     4*/
     5function Market() {}
     6
     7Market.prototype.Schema = "<a:component type='system'/><empty/>";
     8
     9Market.prototype.Init = function()
     10{
     11    this.tradeEntities = []; // List of trade entities from which this market works as a target
     12};
     13
     14/**
     15 Subscribes a new trader to this market.
     16 @param {Object} trader - The trader object expected being subscribed inside the market.
     17*/
     18
     19Market.prototype.SubscribeTrader = function(trader)
     20{
     21    // First check that this trader has not been already registered inside the market.
     22    // If that is the case then do nothing and return true
     23    if (trader in this.tradeEntities)
     24        return true;
     25
     26    // If the entity has not been registered for this market,
     27    // add it to the list of traders
     28    this.tradeEntities.push(trader);
     29
     30    // If everything was correct return true
     31    return true;
     32};
     33
     34Market.prototype.OnDestroy = function()
     35{
     36    this.NotifySubscribedTraders();
     37};
     38
     39Market.prototype.OnOwnershipChanged = function(msg)
     40{
     41    if (msg.to != -1 && msg.from != -1)
     42    {
     43        let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     44        let cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(msg.to), IID_Player);
     45
     46        // If the new owner is an not an ally the trader should display a behavior
     47        // similar when the market is destroyed.
     48        if(!cmpPlayer.IsAlly(msg.from))
     49        {
     50            this.NotifySubscribedTraders();
     51        }
     52    }
     53};
     54
     55Market.prototype.OnDiplomacyChanged = function(msg)
     56{
     57    let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
     58    let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     59    let cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(msg.player), IID_Player);
     60
     61    // If the new diplomatic position of the player is not an ally return to
     62    // respective ally markets
     63    if (!cmpPlayer.IsAlly(cmpOwnership.GetOwner()))
     64    {
     65        this.NotifySubscribedTraders();
     66    }
     67}
     68
     69/**
     70 Notifies traders subscribed to this market to change their trading behavior
     71*/
     72Market.prototype.NotifySubscribedTraders = function() {
     73
     74    let cmpTraderUnitAI = undefined;
     75    for(let i=0, l=this.tradeEntities.length; i<l; ++i)
     76    {
     77        if (this.tradeEntities[i])
     78        {
     79            cmpTraderUnitAI = Engine.QueryInterface(this.tradeEntities[i], IID_UnitAI);
     80            if (cmpTraderUnitAI)
     81                cmpTraderUnitAI.InvalidateTradingMarket(this.entity);
     82        }
     83    }
     84}
     85
     86Engine.RegisterComponentType(IID_Market, "Market", Market);
  • binaries/data/mods/public/simulation/components/Trader.js

     
    1 // See helpers/TraderGain.js for the CalculateTaderGain() function which works out how many 
    2 // resources a trader gets 
     1// See helpers/TraderGain.js for the CalculateTaderGain() function which works out how many
     2// resources a trader gets
    33
    44// Additional gain for ships for each garrisoned trader, in percents
    55const GARRISONED_TRADER_ADDITION = 20;
     
    6363                gain.market2Gain = Math.round(garrisonMultiplier * gain.market2Gain);
    6464        }
    6565    }
    66    
     66
    6767    return gain;
    6868};
    6969
     
    280280        this.gain = this.CalculateGain(this.firstMarket, this.secondMarket);
    281281};
    282282
     283Trader.prototype.InvalidateFirstMarket = function()
     284{
     285    this.firstMarket = INVALID_ENTITY;
     286}
     287
     288Trader.prototype.InvalidateSecondMarket = function()
     289{
     290    this.secondMarket = INVALID_ENTITY;
     291}
     292
    283293Engine.RegisterComponentType(IID_Trader, "Trader", Trader);
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    27462746                    let dropsiteTypes = cmpResourceDropsite.GetTypes();
    27472747                    cmpResourceGatherer.CommitResources(dropsiteTypes);
    27482748                    this.SetGathererAnimationOverride();
    2749                 } 
     2749                }
    27502750
    27512751                // We finished building it.
    27522752                // Switch to the next order (if any)
     
    34583458    var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
    34593459    if (this.losRangeQuery)
    34603460        this.SetupRangeQuery(cmpRangeManager.IsActiveQueryEnabled(this.losRangeQuery));
    3461  
     3461
    34623462    if (this.IsHealer() && this.losHealRangeQuery)
    34633463        this.SetupHealRangeQuery(cmpRangeManager.IsActiveQueryEnabled(this.losHealRangeQuery));
    34643464};
     
    51635163        return;
    51645164    }
    51655165
    5166     var marketsChanged = this.SetTargetMarket(target, source);
     5166    let marketsChanged = this.SetTargetMarket(target, source);
    51675167    if (!marketsChanged)
    51685168        return;
    51695169
    5170     var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
    5171     if (cmpTrader.HasBothMarkets())
     5170    let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
     5171    let cmpMarket = Engine.QueryInterface(target, IID_Market);
     5172
     5173    if (cmpMarket.SubscribeTrader(this.entity) && cmpTrader.HasBothMarkets())
    51725174    {
    5173         var data = { "firstMarket": cmpTrader.GetFirstMarket(), "secondMarket": cmpTrader.GetSecondMarket(), "route": route, "force": false };
     5175        let data = { "firstMarket": cmpTrader.GetFirstMarket(), "secondMarket": cmpTrader.GetSecondMarket(), "route": route, "force": false };
    51745176
    51755177        if (this.expectedRoute)
    51765178        {
     
    51825184        if (this.IsFormationController())
    51835185        {
    51845186            this.CallMemberFunction("AddOrder", ["Trade", data, queued]);
    5185             var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation);
     5187            let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation);
    51865188            if (cmpFormation)
    51875189                cmpFormation.Disband();
    51885190        }
     
    52305232    return ok;
    52315233};
    52325234
     5235/**
     5236 * Makes a routing trade invalid sending the trader unit to the next
     5237 * possible market.
     5238 */
     5239UnitAI.prototype.InvalidateTradingMarket = function(market)
     5240{
     5241    // Find the entity inside the one of the list used in the market
     5242    //
     5243    // If the trader is found in the SourceMarket list then
     5244    // send him to his target market.
     5245    //
     5246    // If the trader is found in the TargetMarket list then
     5247    // send him to his source market
     5248    //
     5249    // If the source and target have been captured then stop
     5250    // trading
     5251    let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
     5252
     5253    if(!cmpTrader)
     5254        return false;
     5255
     5256    if (!cmpTrader.GetFirstMarket() || !cmpTrader.GetSecondMarket())
     5257    {
     5258        // At the moment of writing this this.StopTrading generate errors
     5259        // because it calls this.FinishOrder but the order queue is empty in
     5260        // certain conditions. If that error is fixed this two lines
     5261        // should be changed to this.StopTrading()
     5262        //
     5263        // The reason of why the queue is empty is because MoveCompleted is
     5264        // called when the two markets are destroyed, captured or change diplomacy stattus
     5265        //  and inside the method, this.FinishOrder is called cleaning the order queue.
     5266        // This generates the problem that the trader finish its order but it doesn't stop
     5267        // moving or trading.
     5268        //
     5269        // Here we stop moving and trading
     5270        this.StopMoving();
     5271        cmpTrader.StopTrading();
     5272    }
     5273    else if(cmpTrader.GetFirstMarket() === market)
     5274    {
     5275        cmpTrader.InvalidateFirstMarket();
     5276        this.MoveToMarket(cmpTrader.GetSecondMarket());
     5277    }
     5278    else if(cmpTrader.GetSecondMarket() === market)
     5279    {
     5280        cmpTrader.InvalidateSecondMarket();
     5281        this.MoveToMarket(cmpTrader.GetFirstMarket());
     5282    }
     5283
     5284    return true;
     5285};
     5286
    52335287UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket, nextMarket, nextFsmStateName)
    52345288{
    52355289    if (!this.CanTrade(currentMarket))
     
    52725326{
    52735327    this.StopMoving();
    52745328    this.FinishOrder();
    5275     var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
     5329    let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
    52765330    cmpTrader.StopTrading();
    52775331};
    52785332
  • binaries/data/mods/public/simulation/components/interfaces/Market.js

     
     1Engine.RegisterInterface("Market");
  • binaries/data/mods/public/simulation/templates/template_structure_economic_market.xml

     
    6363  <Vision>
    6464    <Range>32</Range>
    6565  </Vision>
     66  <Market/>
    6667  <VisualActor>
    6768    <FoundationActor>structures/fndn_5x5.xml</FoundationActor>
    6869  </VisualActor>
  • binaries/data/mods/public/simulation/templates/template_structure_military_dock.xml

     
    7474  <Vision>
    7575    <Range>40</Range>
    7676  </Vision>
     77  <Market/>
    7778  <VisualActor>
    7879    <FoundationActor>structures/fndn_4x4_dock.xml</FoundationActor>
    7980  </VisualActor>