forked from mirrors/0ad
Move function to perform trade to substate.
Don't pretend it to be a callable function, while it is certainly part of the FSM. (Also makes it easier for us to implement a duration for the trading in the future, might we ever want to.) Differential revision: D3813 Comment from: @Angen Closes: #6135 This was SVN commit r25216.
This commit is contained in:
@@ -2896,6 +2896,12 @@ UnitAI.prototype.UnitFsmSpec = {
|
||||
// TODO: Inform player
|
||||
},
|
||||
|
||||
"leave": function() {
|
||||
let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
|
||||
if (cmpTrader)
|
||||
cmpTrader.StopTrading();
|
||||
},
|
||||
|
||||
"APPROACHINGMARKET": {
|
||||
"enter": function() {
|
||||
if (!this.MoveToMarket(this.order.data.target))
|
||||
@@ -2916,10 +2922,50 @@ UnitAI.prototype.UnitFsmSpec = {
|
||||
if (this.waypoints && this.waypoints.length)
|
||||
{
|
||||
if (!this.MoveToMarket(this.order.data.target))
|
||||
this.StopTrading();
|
||||
this.FinishOrder();
|
||||
}
|
||||
else
|
||||
this.PerformTradeAndMoveToNextMarket(this.order.data.target);
|
||||
this.SetNextState("TRADING");
|
||||
},
|
||||
},
|
||||
|
||||
"TRADING": {
|
||||
"enter": function() {
|
||||
if (!this.CanTrade(this.order.data.target))
|
||||
{
|
||||
this.FinishOrder();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!this.CheckTargetRange(this.order.data.target, IID_Trader))
|
||||
{
|
||||
this.SetNextState("APPROACHINGMARKET");
|
||||
return true;
|
||||
}
|
||||
|
||||
let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
|
||||
let nextMarket = cmpTrader.PerformTrade(this.order.data.target);
|
||||
let amount = cmpTrader.GetGoods().amount;
|
||||
if (!nextMarket || !amount || !amount.traderGain)
|
||||
{
|
||||
this.FinishOrder();
|
||||
return true;
|
||||
}
|
||||
|
||||
this.order.data.target = nextMarket;
|
||||
|
||||
if (this.order.data.route && this.order.data.route.length)
|
||||
{
|
||||
this.waypoints = this.order.data.route.slice();
|
||||
if (this.order.data.target == cmpTrader.GetSecondMarket())
|
||||
this.waypoints.reverse();
|
||||
}
|
||||
|
||||
this.SetNextState("APPROACHINGMARKET");
|
||||
return true;
|
||||
},
|
||||
|
||||
"leave": function() {
|
||||
},
|
||||
},
|
||||
|
||||
@@ -2928,9 +2974,10 @@ UnitAI.prototype.UnitFsmSpec = {
|
||||
return;
|
||||
let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
|
||||
let otherMarket = cmpTrader && cmpTrader.GetFirstMarket();
|
||||
this.StopTrading();
|
||||
if (otherMarket)
|
||||
this.WalkToTarget(otherMarket);
|
||||
else
|
||||
this.FinishOrder();
|
||||
},
|
||||
},
|
||||
|
||||
@@ -5740,55 +5787,12 @@ UnitAI.prototype.MoveToMarket = function(targetMarket)
|
||||
return this.MoveTo(this.order.data.nextTarget, IID_Trader);
|
||||
};
|
||||
|
||||
UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket)
|
||||
{
|
||||
if (!this.CanTrade(currentMarket))
|
||||
{
|
||||
this.StopTrading();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.CheckTargetRange(currentMarket, IID_Trader))
|
||||
{
|
||||
if (!this.MoveToMarket(currentMarket)) // If the current market is not reached try again
|
||||
this.StopTrading();
|
||||
return;
|
||||
}
|
||||
|
||||
let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
|
||||
let nextMarket = cmpTrader.PerformTrade(currentMarket);
|
||||
let amount = cmpTrader.GetGoods().amount;
|
||||
if (!nextMarket || !amount || !amount.traderGain)
|
||||
{
|
||||
this.StopTrading();
|
||||
return;
|
||||
}
|
||||
|
||||
this.order.data.target = nextMarket;
|
||||
|
||||
if (this.order.data.route && this.order.data.route.length)
|
||||
{
|
||||
this.waypoints = this.order.data.route.slice();
|
||||
if (this.order.data.target == cmpTrader.GetSecondMarket())
|
||||
this.waypoints.reverse();
|
||||
}
|
||||
|
||||
this.SetNextState("APPROACHINGMARKET");
|
||||
};
|
||||
|
||||
UnitAI.prototype.MarketRemoved = function(market)
|
||||
{
|
||||
if (this.order && this.order.data && this.order.data.target && this.order.data.target == market)
|
||||
this.UnitFsm.ProcessMessage(this, { "type": "TradingCanceled", "market": market });
|
||||
};
|
||||
|
||||
UnitAI.prototype.StopTrading = function()
|
||||
{
|
||||
this.FinishOrder();
|
||||
var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
|
||||
cmpTrader.StopTrading();
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds repair/build order to the queue, forced by the player
|
||||
* until the target is reached
|
||||
|
||||
Reference in New Issue
Block a user