Ticket #1173: rallypoint-trade.patch

File rallypoint-trade.patch, 9.0 KB (added by Deiz, 12 years ago)
  • binaries/data/mods/public/gui/session/input.js

     
    210210        var gaiaOwned = (targetState.player == 0);
    211211
    212212        var cursor = "";
     213        var tooltip;
    213214
    214215        // default to walking there
    215216        var data = {command: "walk"};
     
    254255            data.target = target;
    255256            cursor = "action-repair";
    256257        }
     258        else if (hasClass(entState, "Market") && hasClass(targetState, "Market") && entState.id != targetState.id &&
     259                (!hasClass(entState, "NavalMarket") || hasClass(targetState, "NavalMarket")))
     260        {
     261            // Find a trader (if any) that this building can produce.
     262            var trader;
     263            if (entState.production && entState.production.entities.length)
     264                for (var i = 0; i < entState.production.entities.length; ++i)
     265                    if ((trader = GetTemplateData(entState.production.entities[i]).trader))
     266                        break;
     267
     268            var traderData = { "firstMarket": entState.id, "secondMarket": targetState.id, "template": trader };
     269            var gain = Engine.GuiInterfaceCall("GetTradingRouteGain", traderData);
     270            if (gain !== null)
     271            {
     272                data.command = "trade";
     273                data.target = traderData.secondMarket;
     274                data.source = traderData.firstMarket;
     275                cursor = "action-setup-trade-route";
     276                tooltip = "Click to establish a default route for new traders. Gain: " + gain + " metal.";
     277            }
     278        }
    257279       
    258         return {"possible": true, "data": data, "position": targetState.position, "cursor": cursor};
     280        return {"possible": true, "data": data, "position": targetState.position, "cursor": cursor, "tooltip": tooltip};
    259281    }
    260282
    261283    for each (var entityID in selection)
     
    463485        else if (getActionInfo("repair", target).possible)
    464486            return {"type": "build", "cursor": "action-repair", "target": target};
    465487        else if ((actionInfo = getActionInfo("set-rallypoint", target)).possible)
    466             return {"type": "set-rallypoint", "cursor": actionInfo.cursor, "data": actionInfo.data, "position": actionInfo.position};
     488            return {"type": "set-rallypoint", "cursor": actionInfo.cursor, "data": actionInfo.data, "tooltip": actionInfo.tooltip, "position": actionInfo.position};
    467489        else if (getActionInfo("heal", target).possible)
    468490            return {"type": "heal", "cursor": "action-heal", "target": target};
    469491        else if (getActionInfo("attack", target).possible)
  • binaries/data/mods/public/simulation/helpers/RallyPointCommands.js

     
    6060                "queued": true
    6161            });
    6262            break;
     63        case "trade":
     64            ret.push( {
     65                "type": "setup-trade-route",
     66                "entities": spawnedEnts,
     67                "source": data[i].source,
     68                "target": data[i].target,
     69                "queued": true
     70            });
     71            break;
    6372        default:
    6473            ret.push( {
    6574                "type": "walk",
  • binaries/data/mods/public/simulation/helpers/Commands.js

     
    379379        {
    380380            var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
    381381            if (cmpUnitAI)
    382                 cmpUnitAI.SetupTradeRoute(cmd.target);
     382                cmpUnitAI.SetupTradeRoute(cmd.target, cmd.source);
    383383        }
    384384        break;
    385385
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    29192919 * Adds trade order to the queue. Either walk to the first market, or
    29202920 * start a new route. Not forced, so it can be interrupted by attacks.
    29212921 */
    2922 UnitAI.prototype.SetupTradeRoute = function(target, queued)
     2922UnitAI.prototype.SetupTradeRoute = function(target, source, queued)
    29232923{
    29242924    if (!this.CanTrade(target))
    29252925    {
     
    29282928    }
    29292929
    29302930    var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
    2931     var marketsChanged = cmpTrader.SetTargetMarket(target);
     2931    var marketsChanged = cmpTrader.SetTargetMarket(target, source);
    29322932    if (marketsChanged)
    29332933    {
    29342934        if (cmpTrader.HasBothMarkets())
  • binaries/data/mods/public/simulation/components/Trader.js

     
    3636    this.goods = { "type": null, "amount": 0 };
    3737}
    3838
    39 Trader.prototype.CalculateGain = function(firstMarket, secondMarket)
     39Trader.prototype.CalculateGain = function(firstMarket, secondMarket, template)
    4040{
    4141    var cmpFirstMarketPosition = Engine.QueryInterface(firstMarket, IID_Position);
    4242    var cmpSecondMarketPosition = Engine.QueryInterface(secondMarket, IID_Position);
     
    5959
    6060    // For ship increase gain for each garrisoned trader
    6161    var cmpIdentity = Engine.QueryInterface(this.entity, IID_Identity);
    62     if (cmpIdentity.HasClass("Ship"))
     62    if (cmpIdentity && cmpIdentity.HasClass("Ship"))
    6363    {
    6464        var cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder);
    6565        if (cmpGarrisonHolder)
     
    7575        }
    7676    }
    7777
    78     if (this.template.GainMultiplier)
    79         gain *= this.template.GainMultiplier;
     78    // A template may be passed as an argument, serving as an override.
     79    if (!template)
     80        template = this.template;
     81
     82    if (template.GainMultiplier)
     83        gain *= template.GainMultiplier;
    8084    gain = Math.round(gain);
    8185    return gain;
    8286}
     
    8892
    8993// Set target as target market.
    9094// Return true if at least one of markets was changed.
    91 Trader.prototype.SetTargetMarket = function(target)
     95Trader.prototype.SetTargetMarket = function(target, source)
    9296{
    9397    // Check that target is a market
    9498    var cmpTargetIdentity = Engine.QueryInterface(target, IID_Identity);
     
    96100        return false;
    97101    if (!cmpTargetIdentity.HasClass("Market") && !cmpTargetIdentity.HasClass("NavalMarket"))
    98102        return false;
    99     var marketsChanged = false;
     103    var marketsChanged = true;
     104    if (source)
     105    {
     106        // Establish a trade route with both markets in one go.
     107        cmpTargetIdentity = Engine.QueryInterface(source, IID_Identity);
     108        if (!cmpTargetIdentity)
     109            return false;
     110        if (!cmpTargetIdentity.HasClass("Market") && !cmpTargetIdentity.HasClass("NavalMarket"))
     111            return false;
     112
     113        this.firstMarket = source;
     114        this.secondMarket = INVALID_ENTITY;
     115    }
     116
    100117    if (this.secondMarket)
    101118    {
    102119        // If we already have both markets - drop them
    103120        // and use the target as first market
    104121        this.firstMarket = target;
    105122        this.secondMarket = INVALID_ENTITY;
    106         marketsChanged = true;
    107123    }
    108124    else if (this.firstMarket)
    109125    {
    110126        // If we have only one market and target is different from it,
    111127        // set the target as second one
    112         if (target != this.firstMarket)
     128        if (target == this.firstMarket)
     129            marketsChanged = false;
     130        else
    113131        {
    114132            this.secondMarket = target;
    115133            this.gain = this.CalculateGain(this.firstMarket, this.secondMarket);
    116             marketsChanged = true;
    117134        }
    118135    }
    119136    else
     
    121138        // Else we don't have target markets at all,
    122139        // set the target as first market
    123140        this.firstMarket = target;
    124         marketsChanged = true;
    125141    }
    126142    if (marketsChanged)
    127143    {
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    452452        if (template.UnitMotion.Run) ret.speed.run = +template.UnitMotion.Run.Speed;
    453453    }
    454454
     455    if (template.Trader)
     456        ret.trader = template.Trader;
     457
    455458    if (template.WallSet)
    456459    {
    457460        ret.wallSet = {
     
    14681471    return 0;
    14691472};
    14701473
     1474GuiInterface.prototype.GetTradingRouteGain = function(player, data)
     1475{
     1476    if (!data.firstMarket || !data.secondMarket)
     1477        return null;
     1478
     1479    var cmpTrader = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trader);
     1480    if (!cmpTrader)
     1481        return null;
     1482
     1483    return cmpTrader.CalculateGain(data.firstMarket, data.secondMarket, data.template);
     1484}
     1485
    14711486GuiInterface.prototype.GetTradingDetails = function(player, data)
    14721487{
    14731488    var cmpEntityTrader = Engine.QueryInterface(data.trader, IID_Trader);
     
    15881603    "GetFoundationSnapData": 1,
    15891604    "PlaySound": 1,
    15901605    "FindIdleUnit": 1,
     1606    "GetTradingRouteGain": 1,
    15911607    "GetTradingDetails": 1,
    15921608    "CanAttack": 1,
    15931609
  • source/simulation2/Simulation2.cpp

     
    121121
    122122            LOAD_SCRIPTED_COMPONENT("AIInterface");
    123123            LOAD_SCRIPTED_COMPONENT("Barter");
     124            LOAD_SCRIPTED_COMPONENT("Trader");
    124125            LOAD_SCRIPTED_COMPONENT("EndGameManager");
    125126            LOAD_SCRIPTED_COMPONENT("GuiInterface");
    126127            LOAD_SCRIPTED_COMPONENT("PlayerManager");