Ticket #1207: trade.diff
File trade.diff, 10.5 KB (added by , 10 years ago) |
---|
-
binaries/data/mods/public/gui/session/input.js
1443 1443 return true; 1444 1444 1445 1445 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}); 1447 1447 Engine.GuiInterfaceCall("PlaySound", { "name": "order_trade", "entity": selection[0] }); 1448 1448 return true; 1449 1449 x -
binaries/data/mods/public/simulation/components/Trader.js
247 247 { 248 248 if (this.goods.amount && this.goods.origin == this.firstMarket) 249 249 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; 252 254 }; 253 255 254 256 Trader.prototype.StopTrading = function() -
binaries/data/mods/public/simulation/components/UnitAI.js
641 641 642 642 var nextMarket = cmpTrader.GetNextMarket(); 643 643 if (nextMarket == this.order.data.firstMarket) 644 var state = " INDIVIDUAL.TRADE.APPROACHINGFIRSTMARKET";644 var state = "TRADE.APPROACHINGFIRSTMARKET"; 645 645 else 646 var state = " INDIVIDUAL.TRADE.APPROACHINGSECONDMARKET";646 var state = "TRADE.APPROACHINGSECONDMARKET"; 647 647 648 // TODO find the nearest way-point from our position, and start with it 649 this.waypoints = undefined; 648 650 if (this.MoveToMarket(nextMarket)) 649 651 { 650 652 // We've started walking to the next market 651 653 this.SetNextState(state); 652 654 } 655 else 656 this.FinishOrder(); 653 657 }, 654 658 655 659 "Order.Repair": function(msg) { … … 775 779 this.FinishOrder(); 776 780 }, 777 781 782 "Order.WalkToTarget": function(msg) { 783 if (this.MoveToTarget(this.order.data.target)) 784 this.SetNextState("WALKING"); 785 else 786 this.FinishOrder(); 787 }, 788 778 789 "Order.WalkToPointRange": function(msg) { 779 790 if (this.MoveToPointRange(this.order.data.x, this.order.data.z, this.order.data.min, this.order.data.max)) 780 791 this.SetNextState("WALKING"); … … 2486 2497 }, 2487 2498 2488 2499 "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"); 2490 2504 }, 2491 2505 }, 2492 2506 … … 2496 2510 }, 2497 2511 2498 2512 "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"); 2501 2517 }, 2502 2518 }, 2503 2519 }, … … 3438 3454 // If the order was rejected then immediately take it off 3439 3455 // and process the remaining queue 3440 3456 if (ret && ret.discardOrder) 3441 {3442 3457 this.FinishOrder(); 3443 }3444 3458 } 3445 3459 }; 3446 3460 … … 4658 4672 4659 4673 UnitAI.prototype.AddOrder = function(type, data, queued) 4660 4674 { 4675 if (this.expectedRoute) 4676 this.expectedRoute = undefined; 4677 4661 4678 if (queued) 4662 4679 this.PushOrder(type, data); 4663 4680 else … … 4762 4779 */ 4763 4780 UnitAI.prototype.Walk = function(x, z, queued) 4764 4781 { 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); 4766 4786 }; 4767 4787 4768 4788 /** … … 4945 4965 /** 4946 4966 * Adds trade order to the queue. Either walk to the first market, or 4947 4967 * 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. 4948 4970 */ 4949 UnitAI.prototype.SetupTradeRoute = function(target, source, queued)4971 UnitAI.prototype.SetupTradeRoute = function(target, source, route, queued) 4950 4972 { 4951 4973 if (!this.CanTrade(target)) 4952 4974 { … … 4954 4976 return; 4955 4977 } 4956 4978 4957 var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); 4958 var marketsChanged = cmpTrader.SetTargetMarket(target, source); 4979 var marketsChanged = this.SetTargetMarket(target, source); 4959 4980 if (marketsChanged) 4960 4981 { 4982 var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); 4961 4983 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 } 4963 4997 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 } 4965 5005 } 4966 5006 }; 4967 5007 5008 UnitAI.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 4968 5021 UnitAI.prototype.MoveToMarket = function(targetMarket) 4969 5022 { 4970 if (this. MoveToTarget(targetMarket))5023 if (this.waypoints && this.waypoints.length > 1) 4971 5024 { 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); 4974 5029 } 4975 5030 else 4976 5031 { 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. 4979 5039 this.StopMoving(); 4980 5040 this.StopTrading(); 4981 return false;4982 5041 } 5042 5043 return ok; 4983 5044 }; 4984 5045 4985 5046 UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket, nextMarket, nextFsmStateName) … … 4999 5060 this.StopTrading(); 5000 5061 return; 5001 5062 } 5002 if (this.MoveToMarket(nextMarket)) 5063 5064 if (this.order.data.route && this.order.data.route.length) 5003 5065 { 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 5005 5073 this.SetNextState(nextFsmStateName); 5006 } 5074 else 5075 this.StopTrading(); 5007 5076 } 5008 5077 else 5009 5078 { 5010 // If the current market is not reached try again5011 this.MoveToMarket(currentMarket);5079 if (!this.MoveToMarket(currentMarket)) // If the current market is not reached try again 5080 this.StopTrading(); 5012 5081 } 5013 5082 }; 5014 5083 -
binaries/data/mods/public/simulation/helpers/Commands.js
489 489 break; 490 490 491 491 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 }); 499 495 500 496 case "select-required-goods": 501 497 for each (var ent in entities) -
binaries/data/mods/public/simulation/helpers/RallyPointCommands.js
89 89 break; 90 90 } 91 91 } 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 92 116 return ret; 93 117 } 94 118 -
binaries/data/mods/public/simulation/templates/template_formation.xml
28 28 <TurnRate>3.0</TurnRate> 29 29 </Position> 30 30 <UnitAI> 31 31 <AlertReactiveLevel>2</AlertReactiveLevel> 32 32 <DefaultStance>aggressive</DefaultStance> 33 33 <FleeDistance>12.0</FleeDistance> 34 34 <FormationController>true</FormationController> … … 40 40 <PassabilityClass>default</PassabilityClass> 41 41 <CostClass>default</CostClass> 42 42 </UnitMotion> 43 <Trader> 44 <MaxDistance>5.0</MaxDistance> 45 <GainMultiplier>1.0</GainMultiplier> 46 </Trader> 43 47 </Entity>