Ticket #3812: 3812.diff

File 3812.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
    113         {
    114             this.secondMarket = target;
    115             this.gain = this.CalculateGain(this.firstMarket, this.secondMarket);
     103        {   this.index = 0;
     104            this.markets.push(target);
     105            this.goods.amount = this.CalculateGain(this.markets[0], this.markets[1]);
    116106        }
    117107    }
    118108    else
    119109    {
    120110        // Else we don't have target markets at all,
    121111        // set the target as first market
    122         this.firstMarket = target;
     112        this.index = 0;
     113        this.markets = [target];
    123114    }
    124115    if (marketsChanged)
    125116    {
    126117        // Drop carried goods
    127118        this.goods.amount = null;
    Trader.prototype.SetTargetMarket = funct  
    129120    return marketsChanged;
    130121};
    131122
    132123Trader.prototype.GetFirstMarket = function()
    133124{
    134     return this.firstMarket;
     125    return this.markets[0] || null;
    135126};
    136127
    137128Trader.prototype.GetSecondMarket = function()
    138129{
    139     return this.secondMarket;
     130    return this.markets[1] || null;
    140131};
    141132
    142133Trader.prototype.HasBothMarkets = function()
    143134{
    144     return this.firstMarket && this.secondMarket;
     135    return this.markets.length >= 2;
    145136};
    146137
    147138Trader.prototype.GetRequiredGoods = function()
    148139{
    149140    return this.requiredGoods;
    Trader.prototype.CanTrade = function(tar  
    184175    return true;
    185176};
    186177
    187178Trader.prototype.PerformTrade = function(currentMarket)
    188179{
     180    let previousMarket = this.markets[(this.index+this.markets.length) % this.markets.length];
     181
     182    this.index = ++this.index % this.markets.length;
     183
     184    let nextMarket = this.markets[(this.index+this.markets.length) % this.markets.length];
     185
     186    if (previousMarket != currentMarket)
     187        warn("Markets are not matching.");
     188
    189189    if (this.goods.amount && this.goods.amount.traderGain)
    190190    {
    191191        var cmpPlayer = QueryOwnerInterface(this.entity);
    192192        if (cmpPlayer)
    193193            cmpPlayer.AddResource(this.goods.type, this.goods.amount.traderGain);
    Trader.prototype.PerformTrade = function  
    196196        if (cmpStatisticsTracker)
    197197            cmpStatisticsTracker.IncreaseTradeIncomeCounter(this.goods.amount.traderGain);
    198198
    199199        if (this.goods.amount.market1Gain)
    200200        {
    201             var cmpPlayer = QueryOwnerInterface(this.firstMarket);
     201            let cmpPlayer = QueryOwnerInterface(previousMarket);
    202202            if (cmpPlayer)
    203203                cmpPlayer.AddResource(this.goods.type, this.goods.amount.market1Gain);
    204204
    205             var cmpStatisticsTracker = QueryOwnerInterface(this.firstMarket, IID_StatisticsTracker);
     205            let cmpStatisticsTracker = QueryOwnerInterface(previousMarket, IID_StatisticsTracker);
    206206            if (cmpStatisticsTracker)
    207207                cmpStatisticsTracker.IncreaseTradeIncomeCounter(this.goods.amount.market1Gain);
    208208        }
    209209
    210210        if (this.goods.amount.market2Gain)
    211211        {
    212             var cmpPlayer = QueryOwnerInterface(this.secondMarket);
     212            let cmpPlayer = QueryOwnerInterface(currentMarket);
    213213            if (cmpPlayer)
    214214                cmpPlayer.AddResource(this.goods.type, this.goods.amount.market2Gain);
    215215
    216             var cmpStatisticsTracker = QueryOwnerInterface(this.secondMarket, IID_StatisticsTracker);
     216            let cmpStatisticsTracker = QueryOwnerInterface(currentMarket, IID_StatisticsTracker);
    217217            if (cmpStatisticsTracker)
    218218                cmpStatisticsTracker.IncreaseTradeIncomeCounter(this.goods.amount.market2Gain);
    219219        }
    220220    }
    221221
    Trader.prototype.PerformTrade = function  
    232232
    233233        if (!nextGoods || RESOURCES.indexOf(nextGoods) == -1)
    234234            nextGoods = "metal";
    235235    }
    236236    this.goods.type = nextGoods;
    237     this.goods.amount = this.CalculateGain(this.firstMarket, this.secondMarket);
     237    this.goods.amount = this.CalculateGain(currentMarket, nextMarket);
    238238    this.goods.origin = currentMarket;
    239239};
    240240
    241241Trader.prototype.GetGoods = function()
    242242{
    243243    return this.goods;
    244244};
    245245
    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 
    256246Trader.prototype.StopTrading = function()
    257247{
     248    this.index = -1;
     249    this.markets = [];
    258250    // Drop carried goods
    259251    this.goods.amount = null;
    260252    // Reset markets
    261     this.firstMarket = INVALID_ENTITY;
    262     this.secondMarket = INVALID_ENTITY;
     253    this.markets = [];
    263254};
    264255
    265256// Get range in which deals with market are available,
    266257// i.e. trader should be in no more than MaxDistance from market
    267258// to be able to trade with it.
    Trader.prototype.GetRange = function()  
    275266};
    276267
    277268Trader.prototype.OnGarrisonedUnitsChanged = function()
    278269{
    279270    if (this.HasBothMarkets())
    280         this.gain = this.CalculateGain(this.firstMarket, this.secondMarket);
     271        this.goods.amount = this.CalculateGain(this.markets[0], this.markets[1]);
    281272};
    282273
    283274Engine.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": {
    2609                 "enter": function () {
    2610                     this.SelectAnimation("move");
    2611                 },
    2612 
    2613                 "MoveCompleted": function() {
    2614                     if (this.waypoints && this.waypoints.length)
    2615                     {
    2616                         if (!this.MoveToMarket(this.order.data.firstMarket))
    2617                             this.StopTrading();
    2618                     }
    2619                     else
    2620                         this.PerformTradeAndMoveToNextMarket(this.order.data.firstMarket, this.order.data.secondMarket, "APPROACHINGSECONDMARKET");
    2621                 },
    2622             },
    2623 
    2624             "APPROACHINGSECONDMARKET": {
     2602            "APPROACHINGMARKET": {
    26252603                "enter": function () {
    26262604                    this.SelectAnimation("move");
    26272605                },
    26282606
    26292607                "MoveCompleted": function() {
    26302608                    if (this.waypoints && this.waypoints.length)
    26312609                    {
    2632                         if (!this.MoveToMarket(this.order.data.secondMarket))
     2610                        if (!this.MoveToMarket(this.order.data.target))
    26332611                            this.StopTrading();
    26342612                    }
    26352613                    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 = { "target": cmpTrader.GetFirstMarket(), "firstMarket": cmpTrader.GetFirstMarket(), "secondMarket": cmpTrader.GetSecondMarket(), "route": route, "force": false };
    51745152
    51755153        if (this.expectedRoute)
    51765154        {
    51775155            if (!route && this.expectedRoute.length)
    51785156                data.route = this.expectedRoute.slice();
    UnitAI.prototype.MoveToMarket = function  
    52285206    }
    52295207
    52305208    return ok;
    52315209};
    52325210
    5233 UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket, nextMarket, nextFsmStateName)
     5211UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket)
    52345212{
    52355213    if (!this.CanTrade(currentMarket))
    52365214    {
    52375215        this.StopTrading();
    52385216        return;
    52395217    }
    52405218
    5241     if (this.CheckTargetRange(currentMarket, IID_Trader))
     5219    if (!this.CheckTargetRange(currentMarket, IID_Trader))
    52425220    {
    5243         var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
    5244         cmpTrader.PerformTrade(currentMarket);
    5245         if (!cmpTrader.GetGain().traderGain)
    5246         {
     5221        if (!this.MoveToMarket(currentMarket))  // If the current market is not reached try again
    52475222            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         }
     5223        return;
     5224    }
    52585225
    5259         if (this.MoveToMarket(nextMarket))  // We've started walking to the next market
    5260             this.SetNextState(nextFsmStateName);
    5261         else
    5262             this.StopTrading();
     5226    let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
     5227    cmpTrader.PerformTrade(currentMarket);
     5228    let nextMarket = cmpTrader.markets[cmpTrader.index];
     5229    if (!cmpTrader.GetGoods().amount.traderGain)
     5230    {
     5231        this.StopTrading();
     5232        return;
    52635233    }
    5264     else
     5234
     5235    if (this.order.data.route && this.order.data.route.length)
    52655236    {
    5266         if (!this.MoveToMarket(currentMarket))  // If the current market is not reached try again
    5267             this.StopTrading();
     5237        this.waypoints = this.order.data.route.slice();
     5238        if (this.order.data.target == cmpTrader.GetSecondMarket())
     5239            this.waypoints.reverse();
     5240        this.waypoints.unshift(null);  // additionnal dummy point for the market
    52685241    }
     5242
     5243    this.order.data.target = nextMarket;
     5244    if (this.MoveToMarket(nextMarket))  // We've started walking to the next market
     5245        this.SetNextState("APPROACHINGMARKET");
     5246    else
     5247        this.StopTrading();
    52695248};
    52705249
    52715250UnitAI.prototype.StopTrading = function()
    52725251{
    52735252    this.StopMoving();