Ticket #1207: trade.diff

File trade.diff, 10.5 KB (added by mimo, 10 years ago)
  • binaries/data/mods/public/gui/session/input.js

     
    14431443        return true;
    14441444
    14451445    case "setup-trade-route":
    1446         Engine.PostNetworkCommand({"type": "setup-trade-route", "entities": selection, "target": action.target});
     1446        Engine.PostNetworkCommand({"type": "setup-trade-route", "entities": selection, "target": action.target, "source": undefined, "route": undefined, "queued": queued});
    14471447        Engine.GuiInterfaceCall("PlaySound", { "name": "order_trade", "entity": selection[0] });
    14481448        return true;
    14491449x
  • binaries/data/mods/public/simulation/components/Trader.js

     
    247247{
    248248    if (this.goods.amount && this.goods.origin == this.firstMarket)
    249249        return this.secondMarket;
    250     else
    251         return this.firstMarket;
     250
     251    if (this.goods.amount && this.goods.origin != this.secondMarket)
     252        this.goods.amount = null;   // leftover from previous trading
     253    return this.firstMarket;
    252254};
    253255
    254256Trader.prototype.StopTrading = function()
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    641641
    642642        var nextMarket = cmpTrader.GetNextMarket();
    643643        if (nextMarket == this.order.data.firstMarket)
    644             var state = "INDIVIDUAL.TRADE.APPROACHINGFIRSTMARKET";
     644            var state = "TRADE.APPROACHINGFIRSTMARKET";
    645645        else
    646             var state = "INDIVIDUAL.TRADE.APPROACHINGSECONDMARKET";
     646            var state = "TRADE.APPROACHINGSECONDMARKET";
    647647
     648        // TODO find the nearest way-point from our position, and start with it
     649        this.waypoints = undefined;
    648650        if (this.MoveToMarket(nextMarket))
    649651        {
    650652            // We've started walking to the next market
    651653            this.SetNextState(state);
    652654        }
     655        else
     656            this.FinishOrder();
    653657    },
    654658
    655659    "Order.Repair": function(msg) {
     
    775779                this.FinishOrder();
    776780        },
    777781
     782        "Order.WalkToTarget": function(msg) {
     783            if (this.MoveToTarget(this.order.data.target))
     784                this.SetNextState("WALKING");
     785            else
     786                this.FinishOrder();
     787        },
     788
    778789        "Order.WalkToPointRange": function(msg) {
    779790            if (this.MoveToPointRange(this.order.data.x, this.order.data.z, this.order.data.min, this.order.data.max))
    780791                this.SetNextState("WALKING");
     
    24862497                },
    24872498
    24882499                "MoveCompleted": function() {
    2489                     this.PerformTradeAndMoveToNextMarket(this.order.data.firstMarket, this.order.data.secondMarket, "INDIVIDUAL.TRADE.APPROACHINGSECONDMARKET");
     2500                    if (this.waypoints && this.waypoints.length)
     2501                        this.MoveToMarket(this.order.data.firstMarket);
     2502                    else
     2503                        this.PerformTradeAndMoveToNextMarket(this.order.data.firstMarket, this.order.data.secondMarket, "APPROACHINGSECONDMARKET");
    24902504                },
    24912505            },
    24922506
     
    24962510                },
    24972511
    24982512                "MoveCompleted": function() {
    2499                     this.order.data.firstPass = false;
    2500                     this.PerformTradeAndMoveToNextMarket(this.order.data.secondMarket, this.order.data.firstMarket, "INDIVIDUAL.TRADE.APPROACHINGFIRSTMARKET");
     2513                    if (this.waypoints && this.waypoints.length)
     2514                        this.MoveToMarket(this.order.data.secondMarket);
     2515                    else
     2516                        this.PerformTradeAndMoveToNextMarket(this.order.data.secondMarket, this.order.data.firstMarket, "APPROACHINGFIRSTMARKET");
    25012517                },
    25022518            },
    25032519        },
     
    34383454        // If the order was rejected then immediately take it off
    34393455        // and process the remaining queue
    34403456        if (ret && ret.discardOrder)
    3441         {
    34423457            this.FinishOrder();
    3443         }
    34443458    }
    34453459};
    34463460
     
    46584672
    46594673UnitAI.prototype.AddOrder = function(type, data, queued)
    46604674{
     4675    if (this.expectedRoute)
     4676        this.expectedRoute = undefined;
     4677
    46614678    if (queued)
    46624679        this.PushOrder(type, data);
    46634680    else
     
    47624779 */
    47634780UnitAI.prototype.Walk = function(x, z, queued)
    47644781{
    4765     this.AddOrder("Walk", { "x": x, "z": z, "force": true }, queued);
     4782    if (this.expectedRoute && queued)
     4783        this.expectedRoute.push({ "x": x, "z": z });
     4784    else
     4785        this.AddOrder("Walk", { "x": x, "z": z, "force": true }, queued);
    47664786};
    47674787
    47684788/**
     
    49454965/**
    49464966 * Adds trade order to the queue. Either walk to the first market, or
    49474967 * start a new route. Not forced, so it can be interrupted by attacks.
     4968 * The possible route may be given directly as a SetupTradeRoute argument
     4969 * if coming from a RallyPoint, or through this.expectedRoute if a user command.
    49484970 */
    4949 UnitAI.prototype.SetupTradeRoute = function(target, source, queued)
     4971UnitAI.prototype.SetupTradeRoute = function(target, source, route, queued)
    49504972{
    49514973    if (!this.CanTrade(target))
    49524974    {
     
    49544976        return;
    49554977    }
    49564978
    4957     var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
    4958     var marketsChanged = cmpTrader.SetTargetMarket(target, source);
     4979    var marketsChanged = this.SetTargetMarket(target, source);
    49594980    if (marketsChanged)
    49604981    {
     4982        var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
    49614983        if (cmpTrader.HasBothMarkets())
    4962             this.AddOrder("Trade", { "firstMarket": cmpTrader.GetFirstMarket(), "secondMarket": cmpTrader.GetSecondMarket(), "force": false }, queued);
     4984        {
     4985            if (!route && this.expectedRoute && this.expectedRoute.length)
     4986            {
     4987                route = this.expectedRoute.slice();
     4988                this.expectedRoute = undefined;
     4989            }
     4990
     4991            var data = { "firstMarket": cmpTrader.GetFirstMarket(), "secondMarket": cmpTrader.GetSecondMarket(), "route": route, "force": false };
     4992            if (this.IsFormationController())
     4993                this.CallMemberFunction("AddOrder", ["Trade", data, queued]);
     4994            else
     4995                this.AddOrder("Trade", data, queued);
     4996        }
    49634997        else
    4964             this.WalkToTarget(cmpTrader.GetFirstMarket(), queued);
     4998        {
     4999            if (this.IsFormationController())
     5000                this.CallMemberFunction("WalkToTarget", [cmpTrader.GetFirstMarket(), queued]);
     5001            else
     5002                this.WalkToTarget(cmpTrader.GetFirstMarket(), queued);
     5003            this.expectedRoute = [];
     5004        }
    49655005    }
    49665006};
    49675007
     5008UnitAI.prototype.SetTargetMarket = function(target, source)
     5009{
     5010    var  cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
     5011    if (!cmpTrader)
     5012        return false;
     5013    var marketsChanged = cmpTrader.SetTargetMarket(target, source);
     5014
     5015    if (this.IsFormationController())
     5016        this.CallMemberFunction("SetTargetMarket", [target, source]);
     5017
     5018    return marketsChanged;
     5019};
     5020
    49685021UnitAI.prototype.MoveToMarket = function(targetMarket)
    49695022{
    4970     if (this.MoveToTarget(targetMarket))
     5023    if (this.waypoints && this.waypoints.length > 1)
    49715024    {
    4972         // We've started walking to the market
    4973         return true;
     5025        var point = this.waypoints.pop();
     5026        var ok = this.MoveToPoint(point.x, point.z);
     5027        if (!ok)
     5028            ok = this.MoveToMarket(targetMarket);
    49745029    }
    49755030    else
    49765031    {
    4977         // We can't reach the market.
    4978         // Give up.
     5032        this.waypoints = undefined;
     5033        var ok = this.MoveToTarget(targetMarket);
     5034    }
     5035
     5036    if (!ok)
     5037    {
     5038        // We can't reach the market. Give up.
    49795039        this.StopMoving();
    49805040        this.StopTrading();
    4981         return false;
    49825041    }
     5042
     5043    return ok;
    49835044};
    49845045
    49855046UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket, nextMarket, nextFsmStateName)
     
    49995060            this.StopTrading();
    50005061            return;
    50015062        }
    5002         if (this.MoveToMarket(nextMarket))
     5063
     5064        if (this.order.data.route && this.order.data.route.length)
    50035065        {
    5004             // We've started walking to the next market
     5066            this.waypoints = this.order.data.route.slice();
     5067            if (nextFsmStateName == "APPROACHINGSECONDMARKET")
     5068                this.waypoints.reverse();
     5069            this.waypoints.unshift(null);  // additionnal dummy point for the market
     5070        }
     5071
     5072        if (this.MoveToMarket(nextMarket))  // We've started walking to the next market
    50055073            this.SetNextState(nextFsmStateName);
    5006         }
     5074        else
     5075            this.StopTrading();
    50075076    }
    50085077    else
    50095078    {
    5010         // If the current market is not reached try again
    5011         this.MoveToMarket(currentMarket);
     5079        if (!this.MoveToMarket(currentMarket))  // If the current market is not reached try again
     5080            this.StopTrading();
    50125081    }
    50135082};
    50145083
  • binaries/data/mods/public/simulation/helpers/Commands.js

     
    489489        break;
    490490
    491491    case "setup-trade-route":
    492         for each (var ent in entities)
    493         {
    494             var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
    495             if (cmpUnitAI)
    496                 cmpUnitAI.SetupTradeRoute(cmd.target, cmd.source);
    497         }
    498         break;
     492        GetFormationUnitAIs(entities, player).forEach(function(cmpUnitAI) {
     493            cmpUnitAI.SetupTradeRoute(cmd.target, cmd.source, cmd.route, cmd.queued);
     494        });
    499495
    500496    case "select-required-goods":
    501497        for each (var ent in entities)
  • binaries/data/mods/public/simulation/helpers/RallyPointCommands.js

     
    8989            break;
    9090        }
    9191    }
     92
     93    // special case: trade route with waypoints
     94    // (we do not modify the RallyPoint before, as we want it to be displayed with all way-points)
     95    if (ret.length > 1 && ret[ret.length-1].type == "setup-trade-route")
     96    {
     97
     98        var route = [];
     99        var waypoints = ret.length - 1;
     100        for (var i = 0; i < waypoints; ++i)
     101        {
     102            if (ret[i].type != "walk")
     103            {
     104                route = undefined;
     105                break;
     106            }
     107            route.push( {"x": ret[i].x, "z": ret[i].z} );
     108        }
     109        if (route && route.length > 0)
     110        {
     111            ret.splice(0, waypoints);
     112            ret[0].route = route;
     113        }
     114    }
     115
    92116    return ret;
    93117}
    94118
  • binaries/data/mods/public/simulation/templates/template_formation.xml

     
    2828    <TurnRate>3.0</TurnRate>
    2929  </Position>
    3030  <UnitAI>
    31     <AlertReactiveLevel>2</AlertReactiveLevel>
     31    <AlertReactiveLevel>2</AlertReactiveLevel>
    3232    <DefaultStance>aggressive</DefaultStance>
    3333    <FleeDistance>12.0</FleeDistance>
    3434    <FormationController>true</FormationController>
     
    4040    <PassabilityClass>default</PassabilityClass>
    4141    <CostClass>default</CostClass>
    4242  </UnitMotion>
     43  <Trader>
     44    <MaxDistance>5.0</MaxDistance>
     45    <GainMultiplier>1.0</GainMultiplier>
     46  </Trader>
    4347</Entity>