summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormichi_cc <michi_cc@openttd.org>2011-11-05 15:31:21 +0000
committermichi_cc <michi_cc@openttd.org>2011-11-05 15:31:21 +0000
commita22b4f1f9154a508aef79cd6d8cc21d5c84eb559 (patch)
treeef3b508833f20176662b033ec94120a0ada9aa12
parent6a2735d24ed54158b93eac217e803dcd89f67a9e (diff)
downloadopenttd-a22b4f1f9154a508aef79cd6d8cc21d5c84eb559.tar.xz
(svn r23120) -Feature: [NoAI] Support for refit orders.
-rw-r--r--src/ai/api/ai_cargo.hpp8
-rw-r--r--src/ai/api/ai_cargo.hpp.sq4
-rw-r--r--src/ai/api/ai_changelog.hpp5
-rw-r--r--src/ai/api/ai_order.cpp29
-rw-r--r--src/ai/api/ai_order.hpp36
-rw-r--r--src/ai/api/ai_order.hpp.sq3
6 files changed, 85 insertions, 0 deletions
diff --git a/src/ai/api/ai_cargo.hpp b/src/ai/api/ai_cargo.hpp
index 4d7ad777b..4cc3a1a1c 100644
--- a/src/ai/api/ai_cargo.hpp
+++ b/src/ai/api/ai_cargo.hpp
@@ -51,6 +51,14 @@ public:
};
/**
+ * Special cargo types.
+ */
+ enum SpecialCargoID {
+ CT_AUTO_REFIT = 0xFD, ///< Automatically choose cargo type when doing auto-refitting.
+ CT_NO_REFIT = 0xFE, ///< Do not refit cargo of a vehicle.
+ };
+
+ /**
* Checks whether the given cargo type is valid.
* @param cargo_type The cargo to check.
* @return True if and only if the cargo type is valid.
diff --git a/src/ai/api/ai_cargo.hpp.sq b/src/ai/api/ai_cargo.hpp.sq
index 3757c02fd..19154dc3f 100644
--- a/src/ai/api/ai_cargo.hpp.sq
+++ b/src/ai/api/ai_cargo.hpp.sq
@@ -17,6 +17,8 @@ namespace SQConvert {
template <> int Return<AICargo::CargoClass>(HSQUIRRELVM vm, AICargo::CargoClass res) { sq_pushinteger(vm, (int32)res); return 1; }
template <> AICargo::TownEffect GetParam(ForceType<AICargo::TownEffect>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AICargo::TownEffect)tmp; }
template <> int Return<AICargo::TownEffect>(HSQUIRRELVM vm, AICargo::TownEffect res) { sq_pushinteger(vm, (int32)res); return 1; }
+ template <> AICargo::SpecialCargoID GetParam(ForceType<AICargo::SpecialCargoID>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AICargo::SpecialCargoID)tmp; }
+ template <> int Return<AICargo::SpecialCargoID>(HSQUIRRELVM vm, AICargo::SpecialCargoID res) { sq_pushinteger(vm, (int32)res); return 1; }
/* Allow AICargo to be used as Squirrel parameter */
template <> AICargo *GetParam(ForceType<AICargo *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AICargo *)instance; }
@@ -48,6 +50,8 @@ void SQAICargo_Register(Squirrel *engine)
SQAICargo.DefSQConst(engine, AICargo::TE_GOODS, "TE_GOODS");
SQAICargo.DefSQConst(engine, AICargo::TE_WATER, "TE_WATER");
SQAICargo.DefSQConst(engine, AICargo::TE_FOOD, "TE_FOOD");
+ SQAICargo.DefSQConst(engine, AICargo::CT_AUTO_REFIT, "CT_AUTO_REFIT");
+ SQAICargo.DefSQConst(engine, AICargo::CT_NO_REFIT, "CT_NO_REFIT");
SQAICargo.DefSQStaticMethod(engine, &AICargo::IsValidCargo, "IsValidCargo", 2, ".i");
SQAICargo.DefSQStaticMethod(engine, &AICargo::GetCargoLabel, "GetCargoLabel", 2, ".i");
diff --git a/src/ai/api/ai_changelog.hpp b/src/ai/api/ai_changelog.hpp
index 30c524ffa..68777440b 100644
--- a/src/ai/api/ai_changelog.hpp
+++ b/src/ai/api/ai_changelog.hpp
@@ -21,12 +21,17 @@
*
* API additions:
*
+ * \li AICargo::CT_AUTO_REFIT
+ * \li AICargo::CT_NO_REFIT
* \li AICompany::GetQuarterlyIncome
* \li AICompany::GetQuarterlyExpenses
* \li AICompany::GetQuarterlyCargoDelivered
* \li AICompany::GetQuarterlyPerformanceRating
* \li AICompany::GetQuarterlyCompanyValue
* \li AIController::GetOpsTillSuspend
+ * \li AIOrder::GetOrderRefit
+ * \li AIOrder::IsRefitOrder
+ * \li AIOrder::SetOrderRefit
* \li AITown::GetTownAuthority
* \li AIVehicle::ERR_VEHICLE_TOO_LONG in case vehicle length limit is reached
*
diff --git a/src/ai/api/ai_order.cpp b/src/ai/api/ai_order.cpp
index 9fa8eca6a..a3ab21e4c 100644
--- a/src/ai/api/ai_order.cpp
+++ b/src/ai/api/ai_order.cpp
@@ -12,6 +12,7 @@
#include "../../stdafx.h"
#include "ai_order.hpp"
#include "ai_vehicle.hpp"
+#include "ai_cargo.hpp"
#include "../ai_instance.hpp"
#include "../../debug.h"
#include "../../vehicle_base.h"
@@ -117,6 +118,14 @@ static const Order *ResolveOrder(VehicleID vehicle_id, AIOrder::OrderPosition or
return order->GetType() == OT_DUMMY;
}
+/* static */ bool AIOrder::IsRefitOrder(VehicleID vehicle_id, OrderPosition order_position)
+{
+ if (!IsValidVehicleOrder(vehicle_id, order_position)) return false;
+
+ const Order *order = ::ResolveOrder(vehicle_id, order_position);
+ return order != NULL && order->IsRefit();
+}
+
/* static */ bool AIOrder::IsCurrentOrderPartOfOrderList(VehicleID vehicle_id)
{
if (AIVehicle::IsValidVehicle(vehicle_id)) return false;
@@ -323,6 +332,15 @@ static const Order *ResolveOrder(VehicleID vehicle_id, AIOrder::OrderPosition or
return (AIOrder::StopLocation)order->GetStopLocation();
}
+/* static */ CargoID AIOrder::GetOrderRefit(VehicleID vehicle_id, OrderPosition order_position)
+{
+ if (!IsValidVehicleOrder(vehicle_id, order_position)) return CT_NO_REFIT;
+ if (order_position != ORDER_CURRENT && !IsGotoStationOrder(vehicle_id, order_position) && !IsGotoDepotOrder(vehicle_id, order_position)) return CT_NO_REFIT;
+
+ const Order *order = ::ResolveOrder(vehicle_id, order_position);
+ return order->IsRefit() ? order->GetRefitCargo() : CT_NO_REFIT;
+}
+
/* static */ bool AIOrder::SetOrderJumpTo(VehicleID vehicle_id, OrderPosition order_position, OrderPosition jump_to)
{
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
@@ -372,6 +390,17 @@ static const Order *ResolveOrder(VehicleID vehicle_id, AIOrder::OrderPosition or
return AIObject::DoCommand(0, p1, p2, CMD_MODIFY_ORDER);
}
+/* static */ bool AIOrder::SetOrderRefit(VehicleID vehicle_id, OrderPosition order_position, CargoID refit_cargo)
+{
+ EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
+ EnforcePrecondition(false, IsGotoStationOrder(vehicle_id, order_position) || (IsGotoDepotOrder(vehicle_id, order_position) && refit_cargo != CT_AUTO_REFIT));
+ EnforcePrecondition(false, AICargo::IsValidCargo(refit_cargo) || refit_cargo == CT_AUTO_REFIT || refit_cargo == CT_NO_REFIT);
+
+ uint32 p1 = vehicle_id;
+ uint32 p2 = refit_cargo | AIOrder::ResolveOrderPosition(vehicle_id, order_position) << 16;
+ return AIObject::DoCommand(0, p1, p2, CMD_ORDER_REFIT);
+}
+
/* static */ bool AIOrder::AppendOrder(VehicleID vehicle_id, TileIndex destination, AIOrderFlags order_flags)
{
EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
diff --git a/src/ai/api/ai_order.hpp b/src/ai/api/ai_order.hpp
index 0c4c91287..10e47993a 100644
--- a/src/ai/api/ai_order.hpp
+++ b/src/ai/api/ai_order.hpp
@@ -191,6 +191,15 @@ public:
static bool IsVoidOrder(VehicleID vehicle_id, OrderPosition order_position);
/**
+ * Checks whether the given order has a valid refit cargo.
+ * @param vehicle_id The vehicle to check.
+ * @param order_position The order index to check.
+ * @pre order_position != ORDER_CURRENT && IsValidVehicleOrder(vehicle_id, order_position).
+ * @return True if and only if the order is has a valid refit cargo.
+ */
+ static bool IsRefitOrder(VehicleID vehicle_id, OrderPosition order_position);
+
+ /**
* Checks whether the current order is part of the orderlist.
* @param vehicle_id The vehicle to check.
* @pre AIVehicle::IsValidVehicle(vehicle_id).
@@ -320,6 +329,21 @@ public:
static StopLocation GetStopLocation(VehicleID vehicle_id, OrderPosition order_position);
/**
+ * Gets the refit cargo type of the given order for the given vehicle.
+ * @param vehicle_id The vehicle to get the refit cargo for.
+ * @param order_position The order to get the refit cargo for.
+ * @pre IsValidVehicleOrder(vehicle_id, order_position).
+ * @pre order_position == ORDER_CURRENT || IsGotoStationOrder(vehicle_id, order_position) || IsGotoDepotOrder(vehicle_id, order_position).
+ * @note Giving ORDER_CURRENT as order_position will give the order that is
+ * currently being executed by the vehicle. This is not necessarily the
+ * current order as given by ResolveOrderPosition (the current index in the
+ * order list) as manual or autoservicing depot orders do not show up
+ * in the orderlist, but they can be the current order of a vehicle.
+ * @return The refit cargo of the order or CT_NO_REFIT if no refit is set.
+ */
+ static CargoID GetOrderRefit(VehicleID vehicle_id, OrderPosition order_position);
+
+ /**
* Sets the OrderPosition to jump to if the check succeeds of the given order for the given vehicle.
* @param vehicle_id The vehicle to set the OrderPosition for.
* @param order_position The order to set the OrderPosition for.
@@ -381,6 +405,18 @@ public:
static bool SetStopLocation(VehicleID vehicle_id, OrderPosition order_position, StopLocation stop_location);
/**
+ * Sets the refit cargo type of the given order for the given vehicle.
+ * @param vehicle_id The vehicle to set the refit cargo for.
+ * @param order_position The order to set the refit cargo for.
+ * @param refit_cargo The cargo to refit to. The refit can be cleared by passing CT_NO_REFIT.
+ * @pre IsValidVehicleOrder(vehicle_id, order_position).
+ * @pre IsGotoStationOrder(vehicle_id, order_position) || (IsGotoDepotOrder(vehicle_id, order_position) && refit_cargo != CT_AUTO_REFIT).
+ * @pre AICargo::IsValidCargo(refit_cargo) || refit_cargo == CT_AUTO_REFIT || refit_cargo == CT_NO_REFIT
+ * @return Whether the order has been/can be changed.
+ */
+ static bool SetOrderRefit(VehicleID vehicle_id, OrderPosition order_position, CargoID refit_cargo);
+
+ /**
* Appends an order to the end of the vehicle's order list.
* @param vehicle_id The vehicle to append the order to.
* @param destination The destination of the order.
diff --git a/src/ai/api/ai_order.hpp.sq b/src/ai/api/ai_order.hpp.sq
index 12655e1ca..9a289bdf0 100644
--- a/src/ai/api/ai_order.hpp.sq
+++ b/src/ai/api/ai_order.hpp.sq
@@ -96,6 +96,7 @@ void SQAIOrder_Register(Squirrel *engine)
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::IsGotoWaypointOrder, "IsGotoWaypointOrder", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::IsConditionalOrder, "IsConditionalOrder", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::IsVoidOrder, "IsVoidOrder", 3, ".ii");
+ SQAIOrder.DefSQStaticMethod(engine, &AIOrder::IsRefitOrder, "IsRefitOrder", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::IsCurrentOrderPartOfOrderList, "IsCurrentOrderPartOfOrderList", 2, ".i");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::ResolveOrderPosition, "ResolveOrderPosition", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::AreOrderFlagsValid, "AreOrderFlagsValid", 3, ".ii");
@@ -108,11 +109,13 @@ void SQAIOrder_Register(Squirrel *engine)
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::GetOrderCompareFunction, "GetOrderCompareFunction", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::GetOrderCompareValue, "GetOrderCompareValue", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::GetStopLocation, "GetStopLocation", 3, ".ii");
+ SQAIOrder.DefSQStaticMethod(engine, &AIOrder::GetOrderRefit, "GetOrderRefit", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::SetOrderJumpTo, "SetOrderJumpTo", 4, ".iii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::SetOrderCondition, "SetOrderCondition", 4, ".iii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::SetOrderCompareFunction, "SetOrderCompareFunction", 4, ".iii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::SetOrderCompareValue, "SetOrderCompareValue", 4, ".iii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::SetStopLocation, "SetStopLocation", 4, ".iii");
+ SQAIOrder.DefSQStaticMethod(engine, &AIOrder::SetOrderRefit, "SetOrderRefit", 4, ".iii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::AppendOrder, "AppendOrder", 4, ".iii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::AppendConditionalOrder, "AppendConditionalOrder", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::InsertOrder, "InsertOrder", 5, ".iiii");