rubidium@9500: /* $Id$ */ rubidium@9500: rubidium@9500: /** @file ai_order.hpp Everything to query and build Orders */ rubidium@9500: rubidium@9500: #ifndef AI_ORDER_HPP rubidium@9500: #define AI_ORDER_HPP rubidium@9500: rubidium@9500: #include "ai_object.hpp" rubidium@9500: rubidium@9500: /** rubidium@9500: * Class that handles all order related functions. rubidium@9500: */ rubidium@9500: class AIOrder : public AIObject { rubidium@9500: public: rubidium@9500: /** truelight@9529: * The name of the class, needed by several sub-processes. truelight@9529: */ truelight@9529: static const char *GetClassName() { return "AIOrder"; } truelight@9529: truelight@9529: /** rubidium@9500: * Flags that can be used to modify the behaviour of orders. rubidium@9500: */ rubidium@9500: enum AIOrderFlags { rubidium@9500: /** Just go to the station/depot, stop unload if possible and load if needed. */ rubidium@9500: AIOF_NONE = 0, rubidium@9500: rubidium@9500: /** Transfer instead of deliver the goods; only for stations. */ rubidium@9500: AIOF_TRANSFER = 1 << 0, rubidium@9548: /** Always unload the vehicle; only for stations. Cannot be set when AIOF_FULL_LOAD is set. */ rubidium@9500: AIOF_UNLOAD = 1 << 1, rubidium@9548: /** Wait till the the vehicle is fully loaded; only for stations. Cannot be set when AIOF_UNLOAD is set. */ rubidium@9500: AIOF_FULL_LOAD = 1 << 2, rubidium@9500: rubidium@9500: /** Service the vehicle when needed, otherwise skip this order; only for depots. */ rubidium@9500: AIOF_SERVICE_IF_NEEDED = 1 << 2, rubidium@9500: rubidium@9500: /** Do not stop at the stations that are passed when going to the destination. */ rubidium@9500: AIOF_NON_STOP = 1 << 3, rubidium@9500: rubidium@9500: /** For marking invalid order flags */ rubidium@9500: AIOF_INVALID = 0xFFFF, rubidium@9500: }; rubidium@9500: rubidium@9500: /** rubidium@9500: * Checks whether the given order id is valid for the given vehicle. rubidium@9500: * @param vehicle_id the vehicle to check the order index for. rubidium@9500: * @param order_id the order index to check. rubidium@9500: * @pre AIVehicle::IsValidVehicle(vehicle_id). rubidium@9500: * @return true if and only if the order_id is valid for the given vehicle. rubidium@9500: */ rubidium@9500: static bool IsValidVehicleOrder(VehicleID vehicle_id, uint32 order_id); rubidium@9500: rubidium@9500: /** rubidium@9500: * Checks whether the given order flags are valid for the given destination. rubidium@9500: * @param destination the destination of the order. rubidium@9500: * @param order_flags the flags given to the order. rubidium@9500: * @return true if and only if the order_flags are valid for the given location. rubidium@9500: */ rubidium@9500: static bool AreOrderFlagsValid(TileIndex destination, AIOrderFlags order_flags); rubidium@9500: rubidium@9500: /** rubidium@9500: * Returns the number of orders for the given vehicle. rubidium@9500: * @param vehicle_id the vehicle to get the order count of. rubidium@9500: * @pre AIVehicle::IsValidVehicle(vehicle_id). rubidium@9500: * @return the number of orders for the given vehicle or a negative rubidium@9500: * value when the vehicle does not exist. rubidium@9500: */ rubidium@9500: int32 GetNumberOfOrders(VehicleID vehicle_id); rubidium@9500: rubidium@9500: /** rubidium@9500: * Gets the destination of the given order for the given vehicle. rubidium@9500: * @param vehicle_id the vehicle to get the destination for. rubidium@9500: * @param order_id the order to get the destination for. rubidium@9500: * @pre IsValidVehicleOrder(vehicle_id, order_id). rubidium@9500: * @return the destination tile of the order. rubidium@9500: */ rubidium@9500: TileIndex GetOrderDestination(VehicleID vehicle_id, uint32 order_id); rubidium@9500: rubidium@9500: /** rubidium@9500: * Gets the AIOrderFlags of the given order for the given vehicle. rubidium@9500: * @param vehicle_id the vehicle to get the destination for. rubidium@9500: * @param order_id the order to get the destination for. rubidium@9500: * @pre IsValidVehicleOrder(vehicle_id, order_id). rubidium@9500: * @return the AIOrderFlags of the order. rubidium@9500: */ rubidium@9500: AIOrderFlags GetOrderFlags(VehicleID vehicle_id, uint32 order_id); rubidium@9500: rubidium@9500: /** rubidium@9500: * Appends an order to the end of the vehicle's order list. rubidium@9500: * @param vehicle_id the vehicle to append the order to. rubidium@9500: * @param destination the destination of the order. rubidium@9500: * @param order_flags the flags given to the order. rubidium@9500: * @pre AIVehicle::IsValidVehicle(vehicle_id). rubidium@9500: * @pre AreOrderFlagsValid(destination, order_flags). rubidium@9500: * @return true if and only if the order was appended. rubidium@9500: */ rubidium@9500: bool AppendOrder(VehicleID vehicle_id, TileIndex destination, AIOrderFlags order_flags); rubidium@9500: rubidium@9500: /** rubidium@9500: * Inserts an order before the given order_id into the vehicle's order list. rubidium@9500: * @param vehicle_id the vehicle to add the order to. rubidium@9500: * @param order_id the order to place the new order before. rubidium@9500: * @param destination the destination of the order. rubidium@9500: * @param order_flags the flags given to the order. rubidium@9500: * @pre IsValidVehicleOrder(vehicle_id, order_id). rubidium@9500: * @pre AreOrderFlagsValid(destination, order_flags). rubidium@9500: * @return true if and only if the order was inserted. rubidium@9500: */ rubidium@9500: bool InsertOrder(VehicleID vehicle_id, uint32 order_id, TileIndex destination, AIOrderFlags order_flags); rubidium@9500: rubidium@9500: /** rubidium@9500: * Removes an order from the vehicle's order list. rubidium@9500: * @param vehicle_id the vehicle to remove the order from. rubidium@9500: * @param order_id the order to remove from the order list. rubidium@9500: * @pre AIVehicle::IsValidVehicleOrder(vehicle_id, order_id). rubidium@9500: * @return true if and only if the order was removed. rubidium@9500: */ rubidium@9500: bool RemoveOrder(VehicleID vehicle_id, uint32 order_id); rubidium@9500: rubidium@9500: /** rubidium@9500: * Changes the order flags of the given order. rubidium@9500: * @param vehicle_id the vehicle to change the order of. rubidium@9500: * @param order_id the order to change. rubidium@9500: * @param order_flags the new flags given to the order. rubidium@9500: * @pre IsValidVehicleOrder(vehicle_id, order_id). rubidium@9500: * @pre AreOrderFlagsValid(GetOrderDestination(vehicle_id, order_id), order_flags). rubidium@9500: * @return true if and only if the order was changed. rubidium@9500: */ rubidium@9500: bool ChangeOrder(VehicleID vehicle_id, uint32 order_id, AIOrderFlags order_flags); rubidium@9500: rubidium@9500: /** rubidium@9500: * Copies the orders from another vehicle. The orders of the main rubidium@9500: * vehicle are going to be the orders of the changed vehicle. rubidium@9500: * @param vehicle_id the vehicle to copy the orders to. rubidium@9500: * @param main_vehicle_id the vehicle to copy the orders from. rubidium@9500: * @pre AIVehicle::IsValidVehicle(vehicle_id). rubidium@9500: * @pre AIVehicle::IsValidVehicle(main_vehicle_id). rubidium@9500: * @return true if and only if the copying succeeded. rubidium@9500: */ rubidium@9500: bool CopyOrders(VehicleID vehicle_id, VehicleID main_vehicle_id); rubidium@9500: rubidium@9500: /** rubidium@9500: * Shares the orders between two vehicles. The orders of the main rubidium@9500: * vehicle are going to be the orders of the changed vehicle. rubidium@9500: * @param vehicle_id the vehicle to add to the shared order list. rubidium@9500: * @param main_vehicle_id the vehicle to share the orders with. rubidium@9500: * @pre AIVehicle::IsValidVehicle(vehicle_id). rubidium@9500: * @pre AIVehicle::IsValidVehicle(main_vehicle_id). rubidium@9500: * @return true if and only if the sharing succeeded. rubidium@9500: */ rubidium@9500: bool ShareOrders(VehicleID vehicle_id, VehicleID main_vehicle_id); rubidium@9500: rubidium@9500: /** rubidium@9500: * Removes the given vehicle from a shared orders list. rubidium@9500: * @param vehicle_id the vehicle to remove from the shared order list. rubidium@9500: * @pre AIVehicle::IsValidVehicle(vehicle_id). rubidium@9500: * @return true if and only if the unsharing succeeded. rubidium@9500: */ rubidium@9500: bool UnshareOrders(VehicleID vehicle_id); rubidium@9500: }; rubidium@9500: DECLARE_ENUM_AS_BIT_SET(AIOrder::AIOrderFlags); rubidium@9500: rubidium@9500: #ifdef DEFINE_SQUIRREL_CLASS truelight@9510: namespace SQConvert { rubidium@9524: /* Allow enums to be used as Squirrel parameters */ truelight@9510: template <> AIOrder::AIOrderFlags GetParam(ForceType, HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AIOrder::AIOrderFlags)tmp; } truelight@9510: template <> int Return(HSQUIRRELVM vm, AIOrder::AIOrderFlags res) { sq_pushinteger(vm, (int32)res); return 1; } rubidium@9524: rubidium@9524: /* Allow AIOrder to be used as Squirrel parameter */ rubidium@9594: template <> AIOrder *GetParam(ForceType, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIOrder *)instance; } rubidium@9594: template <> AIOrder &GetParam(ForceType, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIOrder *)instance; } rubidium@9594: template <> const AIOrder *GetParam(ForceType, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIOrder *)instance; } rubidium@9594: template <> const AIOrder &GetParam(ForceType, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIOrder *)instance; } truelight@9510: }; // namespace SQConvert truelight@9501: rubidium@9500: void SQAIOrderRegister(Squirrel *engine) { rubidium@9500: DefSQClass SQAIOrder("AIOrder"); rubidium@9500: SQAIOrder.PreRegister(engine); rubidium@9500: SQAIOrder.AddConstructor(engine); truelight@9525: rubidium@9547: SQAIOrder.DefSQConst(engine, AIOrder::AIOF_NONE, "AIOF_NONE"); rubidium@9547: SQAIOrder.DefSQConst(engine, AIOrder::AIOF_TRANSFER, "AIOF_TRANSFER"); rubidium@9547: SQAIOrder.DefSQConst(engine, AIOrder::AIOF_UNLOAD, "AIOF_UNLOAD"); rubidium@9547: SQAIOrder.DefSQConst(engine, AIOrder::AIOF_FULL_LOAD, "AIOF_FULL_LOAD"); rubidium@9547: SQAIOrder.DefSQConst(engine, AIOrder::AIOF_SERVICE_IF_NEEDED, "AIOF_SERVICE_IF_NEEDED"); rubidium@9547: SQAIOrder.DefSQConst(engine, AIOrder::AIOF_NON_STOP, "AIOF_NON_STOP"); rubidium@9547: SQAIOrder.DefSQConst(engine, AIOrder::AIOF_INVALID, "AIOF_INVALID"); truelight@9525: rubidium@9541: SQAIOrder.DefSQStaticMethod(engine, &AIOrder::GetClassName, "GetClassName", 1, "x"); rubidium@9541: SQAIOrder.DefSQStaticMethod(engine, &AIOrder::IsValidVehicleOrder, "IsValidVehicleOrder", 3, "xii"); rubidium@9541: SQAIOrder.DefSQStaticMethod(engine, &AIOrder::AreOrderFlagsValid, "AreOrderFlagsValid", 3, "xii"); rubidium@9532: rubidium@9541: SQAIOrder.DefSQMethod(engine, &AIOrder::GetNumberOfOrders, "GetNumberOfOrders", 2, "xi"); rubidium@9541: SQAIOrder.DefSQMethod(engine, &AIOrder::GetOrderDestination, "GetOrderDestination", 3, "xii"); rubidium@9541: SQAIOrder.DefSQMethod(engine, &AIOrder::GetOrderFlags, "GetOrderFlags", 3, "xii"); rubidium@9541: SQAIOrder.DefSQMethod(engine, &AIOrder::AppendOrder, "AppendOrder", 4, "xiii"); rubidium@9541: SQAIOrder.DefSQMethod(engine, &AIOrder::InsertOrder, "InsertOrder", 5, "xiiii"); rubidium@9541: SQAIOrder.DefSQMethod(engine, &AIOrder::RemoveOrder, "RemoveOrder", 3, "xii"); rubidium@9541: SQAIOrder.DefSQMethod(engine, &AIOrder::ChangeOrder, "ChangeOrder", 4, "xiii"); rubidium@9541: SQAIOrder.DefSQMethod(engine, &AIOrder::CopyOrders, "CopyOrders", 3, "xii"); rubidium@9541: SQAIOrder.DefSQMethod(engine, &AIOrder::ShareOrders, "ShareOrders", 3, "xii"); rubidium@9541: SQAIOrder.DefSQMethod(engine, &AIOrder::UnshareOrders, "UnshareOrders", 2, "xi"); rubidium@9526: rubidium@9500: SQAIOrder.PostRegister(engine); rubidium@9500: } rubidium@9520: #endif /* DEFINE_SQUIRREL_CLASS */ rubidium@9500: rubidium@9500: #endif /* AI_ORDER_HPP */