Ticket #3812: 3812.3.diff

File 3812.3.diff, 13.3 KB (added by fatherbushido, 8 years ago)
  • binaries/data/mods/public/gui/session/utility_functions.js

    function getRankIconSprite(entState)  
    7272/**
    7373 * Returns a message with the details of the trade gain.
    7474 */
    7575function getTradingTooltip(gain)
    7676{
     77    if (!gain)
     78        return "";
    7779
    7880    var playerID = Engine.GetPlayerID();
    7981    var simState = GetSimState();
    8082   
    8183    var gainString = gain.traderGain;
  • binaries/data/mods/public/simulation/components/GuiInterface.js

    GuiInterface.prototype.GetTradingDetails  
    16651665        result = {
    16661666            "type": "is first",
    16671667            "hasBothMarkets": cmpEntityTrader.HasBothMarkets()
    16681668        };
    16691669        if (cmpEntityTrader.HasBothMarkets())
    1670             result.gain = cmpEntityTrader.GetGain();
     1670            result.gain = cmpEntityTrader.GetGoods().amount;
    16711671    }
    16721672    else if (data.target === secondMarket)
    16731673    {
    16741674        result = {
    16751675            "type": "is second",
    1676             "gain": cmpEntityTrader.GetGain(),
     1676            "gain": cmpEntityTrader.GetGoods().amount,
    16771677        };
    16781678    }
    16791679    else if (!firstMarket)
    16801680    {
    16811681        result = { "type": "set first" };
  • binaries/data/mods/public/simulation/components/Trader.js

    Trader.prototype.Schema =  
    1919        "<ref name='positiveDecimal'/>" +
    2020    "</element>";
    2121
    2222Trader.prototype.Init = function()
    2323{
    24     this.firstMarket = INVALID_ENTITY;
    25     this.secondMarket = INVALID_ENTITY;
    26     // Gain from one pass between markets
    27     this.gain = null;
     24    this.markets = [];
     25    this.index = -1;
    2826    // Selected resource for trading
    2927    this.requiredGoods = undefined;
    3028    // Currently carried goods
    3129    this.goods = { "type": null, "amount": null, "origin": null };
    3230};
    3331
    34 Trader.prototype.CalculateGain = function(firstMarket, secondMarket)
     32Trader.prototype.CalculateGain = function(currentMarket, nextMarket)
    3533{
    36     var gain = CalculateTraderGain(firstMarket, secondMarket, this.template, this.entity);
     34    let gain = CalculateTraderGain(currentMarket, nextMarket, this.template, this.entity);
    3735    if (!gain)  // One of our markets must have been destroyed
    3836        return null;
    3937
    4038    // For ship increase gain for each garrisoned trader
    4139    // Calculate this here to save passing unnecessary stuff into the CalculateTraderGain function
    Trader.prototype.CalculateGain = functio  
    6563    }
    6664   
    6765    return gain;
    6866};
    6967
    70 Trader.prototype.GetGain = function()
    71 {
    72     return this.gain;
    73 };
    74 
    7568// Set target as target market.
    7669// Return true if at least one of markets was changed.
    7770Trader.prototype.SetTargetMarket = function(target, source)
    7871{
    7972    // Check that target is a market
    Trader.prototype.SetTargetMarket = funct  
    8982        cmpTargetIdentity = Engine.QueryInterface(source, IID_Identity);
    9083        if (!cmpTargetIdentity)
    9184            return false;
    9285        if (!cmpTargetIdentity.HasClass("Market") && !cmpTargetIdentity.HasClass("NavalMarket"))
    9386            return false;
    94 
    95         this.firstMarket = source;
    96         this.secondMarket = INVALID_ENTITY;
     87        this.markets = [source];
    9788    }
    98 
    99     if (this.secondMarket)
     89    if (this.markets.length >= 2)
    10090    {
    10191        // If we already have both markets - drop them
    10292        // and use the target as first market
    103         this.firstMarket = target;
    104         this.secondMarket = INVALID_ENTITY;
     93        this.index = 0;
     94        this.markets = [target];
    10595    }
    106     else if (this.firstMarket)
     96    else if (this.markets.length == 1)
    10797    {
    10898        // If we have only one market and target is different from it,
    10999        // set the target as second one
    110         if (target == this.firstMarket)
     100        if (target == this.markets[0])
    111101            marketsChanged = false;
    112102        else
    113103        {
    114             this.secondMarket = target;
    115             this.gain = this.CalculateGain(this.firstMarket, this.secondMarket);
     104            this.index = 0;
     105            this.markets.push(target);
     106            this.goods.amount = this.CalculateGain(this.markets[0], this.markets[1]);
    116107        }
    117108    }
    118109    else
    119110    {
    120111        // Else we don't have target markets at all,
    121112        // set the target as first market
    122         this.firstMarket = target;
     113        this.index = 0;
     114        this.markets = [target];
    123115    }
    124116    if (marketsChanged)
    125117    {
    126118        // Drop carried goods
    127119        this.goods.amount = null;
    Trader.prototype.SetTargetMarket = funct  
    129121    return marketsChanged;
    130122};
    131123
    132124Trader.prototype.GetFirstMarket = function()
    133125{
    134     return this.firstMarket;
     126    return this.markets[0] || null;
    135127};
    136128
    137129Trader.prototype.GetSecondMarket = function()
    138130{
    139     return this.secondMarket;
     131    return this.markets[1] || null;
    140132};
    141133
    142134Trader.prototype.HasBothMarkets = function()
    143135{
    144     return this.firstMarket && this.secondMarket;
     136    return this.markets.length >= 2;
    145137};
    146138
    147139Trader.prototype.GetRequiredGoods = function()
    148140{
    149141    return this.requiredGoods;
    Trader.prototype.CanTrade = function(tar  
    184176    return true;
    185177};
    186178
    187179Trader.prototype.PerformTrade = function(currentMarket)
    188180{
     181    let previousMarket = this.markets[(this.index+this.markets.length) % this.markets.length];
     182
     183    this.index = ++this.index % this.markets.length;
     184
     185    let nextMarket = this.markets[(this.index+this.markets.length) % this.markets.length];
     186
     187    if (previousMarket != currentMarket)
     188        warn("Markets are not matching.");
     189
    189190    if (this.goods.amount && this.goods.amount.traderGain)
    190191    {
    191192        var cmpPlayer = QueryOwnerInterface(this.entity);
    192193        if (cmpPlayer)
    193194            cmpPlayer.AddResource(this.goods.type, this.goods.amount.traderGain);
    Trader.prototype.PerformTrade = function  
    196197        if (cmpStatisticsTracker)
    197198            cmpStatisticsTracker.IncreaseTradeIncomeCounter(this.goods.amount.traderGain);
    198199
    199200        if (this.goods.amount.market1Gain)
    200201        {
    201             var cmpPlayer = QueryOwnerInterface(this.firstMarket);
     202            let cmpPlayer = QueryOwnerInterface(previousMarket);
    202203            if (cmpPlayer)
    203204                cmpPlayer.AddResource(this.goods.type, this.goods.amount.market1Gain);
    204205
    205             var cmpStatisticsTracker = QueryOwnerInterface(this.firstMarket, IID_StatisticsTracker);
     206            let cmpStatisticsTracker = QueryOwnerInterface(previousMarket, IID_StatisticsTracker);
    206207            if (cmpStatisticsTracker)
    207208                cmpStatisticsTracker.IncreaseTradeIncomeCounter(this.goods.amount.market1Gain);
    208209        }
    209210
    210211        if (this.goods.amount.market2Gain)
    211212        {
    212             var cmpPlayer = QueryOwnerInterface(this.secondMarket);
     213            let cmpPlayer = QueryOwnerInterface(nextMarket);
    213214            if (cmpPlayer)
    214215                cmpPlayer.AddResource(this.goods.type, this.goods.amount.market2Gain);
    215216
    216             var cmpStatisticsTracker = QueryOwnerInterface(this.secondMarket, IID_StatisticsTracker);
     217            let cmpStatisticsTracker = QueryOwnerInterface(nextMarket, IID_StatisticsTracker);
    217218            if (cmpStatisticsTracker)
    218219                cmpStatisticsTracker.IncreaseTradeIncomeCounter(this.goods.amount.market2Gain);
    219220        }
    220221    }
    221222
    Trader.prototype.PerformTrade = function  
    232233
    233234        if (!nextGoods || RESOURCES.indexOf(nextGoods) == -1)
    234235            nextGoods = "metal";
    235236    }
    236237    this.goods.type = nextGoods;
    237     this.goods.amount = this.CalculateGain(this.firstMarket, this.secondMarket);
     238    this.goods.amount = this.CalculateGain(currentMarket, nextMarket);
    238239    this.goods.origin = currentMarket;
    239240};
    240241
    241242Trader.prototype.GetGoods = function()
    242243{
    243244    return this.goods;
    244245};
    245246
    246 Trader.prototype.GetNextMarket = function()
    247 {
    248     if (this.goods.amount && this.goods.origin == this.firstMarket)
    249         return this.secondMarket;
    250 
    251     if (this.goods.amount && this.goods.origin != this.secondMarket)
    252         this.goods.amount = null;   // leftover from previous trading
    253     return this.firstMarket;
    254 };
    255 
    256247Trader.prototype.StopTrading = function()
    257248{
     249    this.index = -1;
     250    this.markets = [];
    258251    // Drop carried goods
    259252    this.goods.amount = null;
    260253    // Reset markets
    261     this.firstMarket = INVALID_ENTITY;
    262     this.secondMarket = INVALID_ENTITY;
     254    this.markets = [];
    263255};
    264256
    265257// Get range in which deals with market are available,
    266258// i.e. trader should be in no more than MaxDistance from market
    267259// to be able to trade with it.
    Trader.prototype.GetRange = function()  
    275267};
    276268
    277269Trader.prototype.OnGarrisonedUnitsChanged = function()
    278270{
    279271    if (this.HasBothMarkets())
    280         this.gain = this.CalculateGain(this.firstMarket, this.secondMarket);
     272        this.goods.amount = this.CalculateGain(this.markets[0], this.markets[1]);
    281273};
    282274
    283275Engine.RegisterComponentType(IID_Trader, "Trader", Trader);
  • binaries/data/mods/public/simulation/components/UnitAI.js

    UnitAI.prototype.UnitFsmSpec = {  
    652652        {
    653653            this.FinishOrder();
    654654            return;
    655655        }
    656656
    657         var nextMarket = cmpTrader.GetNextMarket();
    658         if (nextMarket == this.order.data.firstMarket)
    659             var state = "TRADE.APPROACHINGFIRSTMARKET";
    660         else
    661             var state = "TRADE.APPROACHINGSECONDMARKET";
    662 
    663657        // TODO find the nearest way-point from our position, and start with it
    664658        this.waypoints = undefined;
    665         if (this.MoveToMarket(nextMarket))
     659        if (this.MoveToMarket(this.order.data.target))
    666660            // We've started walking to the next market
    667             this.SetNextState(state);
     661            this.SetNextState("TRADE.APPROACHINGMARKET");
    668662        else
    669663            this.FinishOrder();
    670664    },
    671665
    672666    "Order.Repair": function(msg) {
    UnitAI.prototype.UnitFsmSpec = {  
    26032597            "Attacked": function(msg) {
    26042598                // Ignore attack
    26052599                // TODO: Inform player
    26062600            },
    26072601
    2608             "APPROACHINGFIRSTMARKET": {
     2602            "APPROACHINGMARKET": {
    26092603                "enter": function () {
    26102604                    this.SelectAnimation("move");
    26112605                },
    26122606
    26132607                "MoveCompleted": function() {
    26142608                    if (this.waypoints && this.waypoints.length)
    26152609                    {
    2616                         if (!this.MoveToMarket(this.order.data.firstMarket))
     2610                        if (!this.MoveToMarket(this.order.data.target))
    26172611                            this.StopTrading();
    26182612                    }
    26192613                    else
    2620                         this.PerformTradeAndMoveToNextMarket(this.order.data.firstMarket, this.order.data.secondMarket, "APPROACHINGSECONDMARKET");
    2621                 },
    2622             },
    2623 
    2624             "APPROACHINGSECONDMARKET": {
    2625                 "enter": function () {
    2626                     this.SelectAnimation("move");
    2627                 },
    2628 
    2629                 "MoveCompleted": function() {
    2630                     if (this.waypoints && this.waypoints.length)
    2631                     {
    2632                         if (!this.MoveToMarket(this.order.data.secondMarket))
    2633                             this.StopTrading();
    2634                     }
    2635                     else
    2636                         this.PerformTradeAndMoveToNextMarket(this.order.data.secondMarket, this.order.data.firstMarket, "APPROACHINGFIRSTMARKET");
     2614                        this.PerformTradeAndMoveToNextMarket(this.order.data.target);
    26372615                },
    26382616            },
    26392617        },
    26402618
    26412619        "REPAIR": {
    UnitAI.prototype.SetupTradeRoute = funct  
    51685146        return;
    51695147
    51705148    var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
    51715149    if (cmpTrader.HasBothMarkets())
    51725150    {
    5173         var data = { "firstMarket": cmpTrader.GetFirstMarket(), "secondMarket": cmpTrader.GetSecondMarket(), "route": route, "force": false };
     5151        let data = {
     5152            "target": cmpTrader.GetFirstMarket(),
     5153            "firstMarket": cmpTrader.GetFirstMarket(),
     5154            "secondMarket": cmpTrader.GetSecondMarket(),
     5155            "route": route, "force": false };
    51745156
    51755157        if (this.expectedRoute)
    51765158        {
    51775159            if (!route && this.expectedRoute.length)
    51785160                data.route = this.expectedRoute.slice();
    UnitAI.prototype.MoveToMarket = function  
    52285210    }
    52295211
    52305212    return ok;
    52315213};
    52325214
    5233 UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket, nextMarket, nextFsmStateName)
     5215UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket)
    52345216{
    52355217    if (!this.CanTrade(currentMarket))
    52365218    {
    52375219        this.StopTrading();
    52385220        return;
    52395221    }
    52405222
    5241     if (this.CheckTargetRange(currentMarket, IID_Trader))
     5223    if (!this.CheckTargetRange(currentMarket, IID_Trader))
    52425224    {
    5243         var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
    5244         cmpTrader.PerformTrade(currentMarket);
    5245         if (!cmpTrader.GetGain().traderGain)
    5246         {
     5225        if (!this.MoveToMarket(currentMarket))  // If the current market is not reached try again
    52475226            this.StopTrading();
    5248             return;
    5249         }
    5250 
    5251         if (this.order.data.route && this.order.data.route.length)
    5252         {
    5253             this.waypoints = this.order.data.route.slice();
    5254             if (nextFsmStateName == "APPROACHINGSECONDMARKET")
    5255                 this.waypoints.reverse();
    5256             this.waypoints.unshift(null);  // additionnal dummy point for the market
    5257         }
     5227        return;
     5228    }
    52585229
    5259         if (this.MoveToMarket(nextMarket))  // We've started walking to the next market
    5260             this.SetNextState(nextFsmStateName);
    5261         else
    5262             this.StopTrading();
     5230    let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
     5231    cmpTrader.PerformTrade(currentMarket);
     5232    let nextMarket = cmpTrader.markets[cmpTrader.index];
     5233    if (!cmpTrader.GetGoods().amount.traderGain)
     5234    {
     5235        this.StopTrading();
     5236        return;
    52635237    }
    5264     else
     5238
     5239    this.order.data.target = nextMarket;
     5240
     5241    if (this.order.data.route && this.order.data.route.length)
    52655242    {
    5266         if (!this.MoveToMarket(currentMarket))  // If the current market is not reached try again
    5267             this.StopTrading();
     5243        this.waypoints = this.order.data.route.slice();
     5244        if (this.order.data.target == cmpTrader.GetSecondMarket())
     5245            this.waypoints.reverse();
     5246        this.waypoints.unshift(null);  // additionnal dummy point for the market
    52685247    }
     5248
     5249    if (this.MoveToMarket(nextMarket))  // We've started walking to the next market
     5250        this.SetNextState("APPROACHINGMARKET");
     5251    else
     5252        this.StopTrading();
    52695253};
    52705254
    52715255UnitAI.prototype.StopTrading = function()
    52725256{
    52735257    this.StopMoving();