Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/components/UnitAI.js
Show First 20 Lines • Show All 602 Lines • ▼ Show 20 Lines | "Order.Trade": function(msg) { | ||||
// We must check if this trader has both markets in case it was a back-to-work order | // We must check if this trader has both markets in case it was a back-to-work order | ||||
var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); | var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); | ||||
if (!cmpTrader || !cmpTrader.HasBothMarkets()) | if (!cmpTrader || !cmpTrader.HasBothMarkets()) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return; | return; | ||||
} | } | ||||
// TODO find the nearest way-point from our position, and start with it | this.waypoints = []; | ||||
this.waypoints = undefined; | |||||
this.SetNextState("TRADE.APPROACHINGMARKET"); | this.SetNextState("TRADE.APPROACHINGMARKET"); | ||||
}, | }, | ||||
"Order.Repair": function(msg) { | "Order.Repair": function(msg) { | ||||
if (this.CheckTargetRange(this.order.data.target, IID_Builder)) | if (this.CheckTargetRange(this.order.data.target, IID_Builder)) | ||||
this.SetNextState("INDIVIDUAL.REPAIR.REPAIRING"); | this.SetNextState("INDIVIDUAL.REPAIR.REPAIRING"); | ||||
else | else | ||||
this.SetNextState("INDIVIDUAL.REPAIR.APPROACHING"); | this.SetNextState("INDIVIDUAL.REPAIR.APPROACHING"); | ||||
▲ Show 20 Lines • Show All 2,247 Lines • ▼ Show 20 Lines | "TRADE": { | ||||
return false; | return false; | ||||
}, | }, | ||||
"leave": function() { | "leave": function() { | ||||
this.StopMoving(); | this.StopMoving(); | ||||
}, | }, | ||||
"MovementUpdate": function(msg) { | "MovementUpdate": function(msg) { | ||||
if (!msg.likelyFailure && !this.CheckTargetRange(this.order.data.target, IID_Trader)) | if (!msg.likelyFailure && !this.CheckRange(this.order.data.nextTarget, IID_Trader)) | ||||
return; | return; | ||||
if (this.waypoints && this.waypoints.length) | if (this.waypoints && this.waypoints.length) | ||||
{ | { | ||||
if (!this.MoveToMarket(this.order.data.target)) | if (!this.MoveToMarket(this.order.data.target)) | ||||
this.StopTrading(); | this.StopTrading(); | ||||
} | } | ||||
else | else | ||||
this.PerformTradeAndMoveToNextMarket(this.order.data.target); | this.PerformTradeAndMoveToNextMarket(this.order.data.target); | ||||
}, | }, | ||||
▲ Show 20 Lines • Show All 2,798 Lines • ▼ Show 20 Lines | |||||
{ | { | ||||
let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); | let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); | ||||
if (!cmpTrader) | if (!cmpTrader) | ||||
return; | return; | ||||
cmpTrader.RemoveTargetMarket(target); | cmpTrader.RemoveTargetMarket(target); | ||||
if (this.IsFormationController()) | if (this.IsFormationController()) | ||||
this.CallMemberFunction("CancelSetupTradeRoute", [target]); | this.CallMemberFunction("CancelSetupTradeRoute", [target]); | ||||
} | }; | ||||
/** | /** | ||||
* Adds trade order to the queue. Either walk to the first market, or | * Adds trade order to the queue. Either walk to the first market, or | ||||
* start a new route. Not forced, so it can be interrupted by attacks. | * start a new route. Not forced, so it can be interrupted by attacks. | ||||
* The possible route may be given directly as a SetupTradeRoute argument | * The possible route may be given directly as a SetupTradeRoute argument | ||||
* if coming from a RallyPoint, or through this.expectedRoute if a user command. | * if coming from a RallyPoint, or through this.expectedRoute if a user command. | ||||
*/ | */ | ||||
UnitAI.prototype.SetupTradeRoute = function(target, source, route, queued) | UnitAI.prototype.SetupTradeRoute = function(target, source, route, queued) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | |||||
UnitAI.prototype.SwitchMarketOrder = function(oldMarket, newMarket) | UnitAI.prototype.SwitchMarketOrder = function(oldMarket, newMarket) | ||||
{ | { | ||||
if (this.order && this.order.data && this.order.data.target && this.order.data.target == oldMarket) | if (this.order && this.order.data && this.order.data.target && this.order.data.target == oldMarket) | ||||
this.order.data.target = newMarket; | this.order.data.target = newMarket; | ||||
}; | }; | ||||
UnitAI.prototype.MoveToMarket = function(targetMarket) | UnitAI.prototype.MoveToMarket = function(targetMarket) | ||||
{ | { | ||||
if (this.waypoints && this.waypoints.length > 1) | let nextTarget; | ||||
{ | if (this.waypoints && this.waypoints.length >= 1) | ||||
let point = this.waypoints.pop(); | nextTarget = this.waypoints.pop(); | ||||
return this.MoveToPoint(point.x, point.z) || this.MoveToMarket(targetMarket); | else | ||||
} | nextTarget = { "target": targetMarket }; | ||||
this.order.data.nextTarget = nextTarget; | |||||
this.waypoints = undefined; | return this.MoveTo(this.order.data.nextTarget, IID_Trader); | ||||
return this.MoveToTargetRange(targetMarket, IID_Trader); | |||||
}; | }; | ||||
UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket) | UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket) | ||||
{ | { | ||||
if (!this.CanTrade(currentMarket)) | if (!this.CanTrade(currentMarket)) | ||||
{ | { | ||||
this.StopTrading(); | this.StopTrading(); | ||||
return; | return; | ||||
Show All 17 Lines | UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket) | ||||
this.order.data.target = nextMarket; | this.order.data.target = nextMarket; | ||||
if (this.order.data.route && this.order.data.route.length) | if (this.order.data.route && this.order.data.route.length) | ||||
{ | { | ||||
this.waypoints = this.order.data.route.slice(); | this.waypoints = this.order.data.route.slice(); | ||||
if (this.order.data.target == cmpTrader.GetSecondMarket()) | if (this.order.data.target == cmpTrader.GetSecondMarket()) | ||||
this.waypoints.reverse(); | this.waypoints.reverse(); | ||||
this.waypoints.unshift(null); // additionnal dummy point for the market | |||||
} | } | ||||
if (this.MoveToMarket(nextMarket)) | |||||
this.SetNextState("APPROACHINGMARKET"); | this.SetNextState("APPROACHINGMARKET"); | ||||
else | |||||
this.StopTrading(); | |||||
}; | }; | ||||
UnitAI.prototype.MarketRemoved = function(market) | UnitAI.prototype.MarketRemoved = function(market) | ||||
{ | { | ||||
if (this.order && this.order.data && this.order.data.target && this.order.data.target == market) | if (this.order && this.order.data && this.order.data.target && this.order.data.target == market) | ||||
this.UnitFsm.ProcessMessage(this, { "type": "TradingCanceled", "market": market }); | this.UnitFsm.ProcessMessage(this, { "type": "TradingCanceled", "market": market }); | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 737 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator