Ticket #3277: trading_v3.patch

File trading_v3.patch, 10.5 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)
     1664        result = { "type": "set first" };
     1665    else if (!secondMarket)
    16641666    {
    16651667        result = {
     1668            "type": "set second",
     1669            "gain": cmpEntityTrader.CalculateGain(firstMarket, data.target),
     1670        };
     1671    }
     1672    else if (data.target === firstMarket)
     1673    {
     1674        result = {
    16661675            "type": "is first",
    16671676            "hasBothMarkets": cmpEntityTrader.HasBothMarkets()
    16681677        };
     
    16761685            "gain": cmpEntityTrader.GetGoods().amount,
    16771686        };
    16781687    }
    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     }
    16901688    else
    16911689    {
    16921690        // 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
     23    if (!(trader in this.tradeEntities))
     24        // If the entity has not been registered for this market,
     25        // add it to the list of traders
     26        this.tradeEntities.push(trader);
     27};
     28
     29Market.prototype.OnDestroy = function()
     30{
     31    this.NotifySubscribedTraders();
     32};
     33
     34Market.prototype.OnOwnershipChanged = function(msg)
     35{
     36    if (msg.to != -1 && msg.from != -1)
     37    {
     38        let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     39        let cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(msg.to), IID_Player);
     40
     41        // If the new owner is an not an ally the trader should display a behavior
     42        // similar when the market is destroyed.
     43        if(!cmpPlayer.IsAlly(msg.from) || !cmpPlayer.isNeutral(msg.from))
     44            this.NotifySubscribedTraders();
     45    }
     46};
     47
     48Market.prototype.OnDiplomacyChanged = function(msg)
     49{
     50    let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
     51    let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     52    let cmpPlayer = undefined;
     53    if(cmpPlayerManager)
     54        cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(msg.player), IID_Player);
     55
     56    if(!cmpOwnership || !cmpPlayer)
     57        return;
     58
     59    // If the new diplomatic position of the player is not an ally return to
     60    // respective ally markets
     61    if (!cmpPlayer.IsAlly(cmpOwnership.GetOwner()) || !cmpPlayer.IsNeutral(cmpOwnership.GetOwner()))
     62        this.NotifySubscribedTraders();
     63}
     64
     65/**
     66 Notifies traders subscribed to this market to change their trading behavior
     67*/
     68Market.prototype.NotifySubscribedTraders = function() {
     69
     70    let cmpUnitAI = undefined;
     71    for(let i=0, l=this.tradeEntities.length; i<l; ++i)
     72    {
     73        if (this.tradeEntities[i])
     74        {
     75            cmpUnitAI = Engine.QueryInterface(this.tradeEntities[i], IID_UnitAI);
     76            if (cmpUnitAI)
     77                cmpUnitAI.InvalidateTradingMarket(this.entity);
     78        }
     79    }
     80}
     81
     82Engine.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;
     
    6161                gain.market2Gain = Math.round(garrisonMultiplier * gain.market2Gain);
    6262        }
    6363    }
    64    
     64
    6565    return gain;
    6666};
    6767
     
    273273        this.goods.amount = this.CalculateGain(this.markets[0], this.markets[1]);
    274274};
    275275
     276Trader.prototype.InvalidateFirstMarket = function()
     277{
     278    this.firstMarket = INVALID_ENTITY;
     279}
     280
     281Trader.prototype.InvalidateSecondMarket = function()
     282{
     283    this.secondMarket = INVALID_ENTITY;
     284}
     285
    276286Engine.RegisterComponentType(IID_Trader, "Trader", Trader);
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    353353        var needToMove = true;
    354354        var cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
    355355        if (this.lastShorelinePosition && cmpPosition && (this.lastShorelinePosition.x == cmpPosition.GetPosition().x)
    356             && (this.lastShorelinePosition.z == cmpPosition.GetPosition().z))
     356            && (this.lastShorelinePosition.z == cmpPosition.GetPosition().z))
    357357        {
    358358            // we were already on the shoreline, and have not moved since
    359359            if (DistanceBetweenEntities(this.entity, this.order.data.target) < 50)
     
    27242724                    let dropsiteTypes = cmpResourceDropsite.GetTypes();
    27252725                    cmpResourceGatherer.CommitResources(dropsiteTypes);
    27262726                    this.SetGathererAnimationOverride();
    2727                 } 
     2727                }
    27282728
    27292729                // We finished building it.
    27302730                // Switch to the next order (if any)
     
    34363436    var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
    34373437    if (this.losRangeQuery)
    34383438        this.SetupRangeQuery(cmpRangeManager.IsActiveQueryEnabled(this.losRangeQuery));
    3439  
     3439
    34403440    if (this.IsHealer() && this.losHealRangeQuery)
    34413441        this.SetupHealRangeQuery(cmpRangeManager.IsActiveQueryEnabled(this.losHealRangeQuery));
    34423442};
     
    51415141        return;
    51425142    }
    51435143
    5144     var marketsChanged = this.SetTargetMarket(target, source);
     5144    let marketsChanged = this.SetTargetMarket(target, source);
    51455145    if (!marketsChanged)
    51465146        return;
    51475147
    5148     var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
     5148    let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
     5149    let cmpMarket = Engine.QueryInterface(target, IID_Market);
     5150    if(!cmpTrader || !cmpMarket)
     5151        return;
     5152
     5153    cmpMarket.SubscribeTrader(this.entity);
     5154
    51495155    if (cmpTrader.HasBothMarkets())
    51505156    {
    51515157        let data = {
     
    51645170        if (this.IsFormationController())
    51655171        {
    51665172            this.CallMemberFunction("AddOrder", ["Trade", data, queued]);
    5167             var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation);
     5173            let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation);
    51685174            if (cmpFormation)
    51695175                cmpFormation.Disband();
    51705176        }
     
    52125218    return ok;
    52135219};
    52145220
     5221/**
     5222 * Makes a routing trade invalid sending the trader unit to the next
     5223 * possible market.
     5224 */
     5225UnitAI.prototype.InvalidateTradingMarket = function(market)
     5226{
     5227    // Find the entity inside the one of the markets in the trader
     5228    //
     5229    // If the market is found as the first market then send the trader to the
     5230    // second market.
     5231    //
     5232    // If the market is found in the second market then send the trader to the
     5233    // first market
     5234    //
     5235    // If the source and target have been captured then stop
     5236    // trading
     5237    let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
     5238
     5239    if(cmpTrader)
     5240    {
     5241        if (!cmpTrader.GetFirstMarket() || !cmpTrader.GetSecondMarket())
     5242            this.StopTrading();
     5243        else if(cmpTrader.GetFirstMarket() === market)
     5244        {
     5245            cmpTrader.InvalidateFirstMarket();
     5246            this.MoveToMarket(cmpTrader.GetSecondMarket());
     5247        }
     5248        else if(cmpTrader.GetSecondMarket() === market)
     5249        {
     5250            cmpTrader.InvalidateSecondMarket();
     5251            this.MoveToMarket(cmpTrader.GetFirstMarket());
     5252        }
     5253    }
     5254};
     5255
    52155256UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket)
    52165257{
    52175258    if (!this.CanTrade(currentMarket))
     
    52285269    }
    52295270
    52305271    let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
     5272    if(!cmpTrader)
     5273    {
     5274        this.StopTrading();
     5275        return;
     5276    }
     5277
    52315278    cmpTrader.PerformTrade(currentMarket);
    52325279    if (!cmpTrader.GetGoods().amount.traderGain)
    52335280    {
     
    52555302UnitAI.prototype.StopTrading = function()
    52565303{
    52575304    this.StopMoving();
    5258     this.FinishOrder();
    5259     var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
     5305    let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
    52605306    cmpTrader.StopTrading();
    52615307};
    52625308
  • 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>