src/ai/api/ai_order.hpp
author truelight
Thu, 22 Mar 2007 09:10:00 +0000
branchnoai
changeset 9510 261d33fbabb8
parent 9501 15689f09d1f5
child 9520 f7cf8bea10db
permissions -rw-r--r--
(svn r9406) [NoAI] -Fix: made the detection of 'void' against 'non-void' functions for the class->SQ convertor via templates (as MSVC failed to understand the other method) (KUDr)
-Fix: merged squirrel_helper.cpp to squirrel_helper.hpp, as that was a silly attempt of TrueLight (KUDr)
-Fix: put all Squirrel conversion code in a namespace instead of a class (to avoid G++ bitching) (KUDr)
/* $Id$ */

/** @file ai_order.hpp Everything to query and build Orders */

#ifndef AI_ORDER_HPP
#define AI_ORDER_HPP

#include "ai_object.hpp"

/**
 * Class that handles all order related functions.
 */
class AIOrder : public AIObject {
public:
	/**
	 * Flags that can be used to modify the behaviour of orders.
	 */
	enum AIOrderFlags {
		/** Just go to the station/depot, stop unload if possible and load if needed. */
		AIOF_NONE              = 0,

		/** Transfer instead of deliver the goods; only for stations. */
		AIOF_TRANSFER          = 1 << 0,
		/** Always unload the vehicle; only for stations. */
		AIOF_UNLOAD            = 1 << 1,
		/** Wait till the the vehicle is fully loaded; only for stations. */
		AIOF_FULL_LOAD         = 1 << 2,

		/** Service the vehicle when needed, otherwise skip this order; only for depots. */
		AIOF_SERVICE_IF_NEEDED = 1 << 2,

		/** Do not stop at the stations that are passed when going to the destination. */
		AIOF_NON_STOP          = 1 << 3,

		/** For marking invalid order flags */
		AIOF_INVALID           = 0xFFFF,
	};

	/**
	 * Checks whether the given order id is valid for the given vehicle.
	 * @param vehicle_id the vehicle to check the order index for.
	 * @param order_id   the order index to check.
	 * @pre AIVehicle::IsValidVehicle(vehicle_id).
	 * @return true if and only if the order_id is valid for the given vehicle.
	 */
	static bool IsValidVehicleOrder(VehicleID vehicle_id, uint32 order_id);

	/**
	 * Checks whether the given order flags are valid for the given destination.
	 * @param destination the destination of the order.
	 * @param order_flags the flags given to the order.
	 * @return true if and only if the order_flags are valid for the given location.
	 */
	static bool AreOrderFlagsValid(TileIndex destination, AIOrderFlags order_flags);

	/**
	 * Returns the number of orders for the given vehicle.
	 * @param vehicle_id the vehicle to get the order count of.
	 * @pre AIVehicle::IsValidVehicle(vehicle_id).
	 * @return the number of orders for the given vehicle or a negative
	 *   value when the vehicle does not exist.
	 */
	int32 GetNumberOfOrders(VehicleID vehicle_id);

	/**
	 * Gets the destination of the given order for the given vehicle.
	 * @param vehicle_id the vehicle to get the destination for.
	 * @param order_id   the order to get the destination for.
	 * @pre IsValidVehicleOrder(vehicle_id, order_id).
	 * @return the destination tile of the order.
	 */
	TileIndex GetOrderDestination(VehicleID vehicle_id, uint32 order_id);

	/**
	 * Gets the AIOrderFlags of the given order for the given vehicle.
	 * @param vehicle_id the vehicle to get the destination for.
	 * @param order_id   the order to get the destination for.
	 * @pre IsValidVehicleOrder(vehicle_id, order_id).
	 * @return the AIOrderFlags of the order.
	 */
	AIOrderFlags GetOrderFlags(VehicleID vehicle_id, uint32 order_id);

	/**
	 * 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.
	 * @param order_flags the flags given to the order.
	 * @pre AIVehicle::IsValidVehicle(vehicle_id).
	 * @pre AreOrderFlagsValid(destination, order_flags).
	 * @return true if and only if the order was appended.
	 */
	bool AppendOrder(VehicleID vehicle_id, TileIndex destination, AIOrderFlags order_flags);

	/**
	 * Inserts an order before the given order_id into the vehicle's order list.
	 * @param vehicle_id  the vehicle to add the order to.
	 * @param order_id    the order to place the new order before.
	 * @param destination the destination of the order.
	 * @param order_flags the flags given to the order.
	 * @pre IsValidVehicleOrder(vehicle_id, order_id).
	 * @pre AreOrderFlagsValid(destination, order_flags).
	 * @return true if and only if the order was inserted.
	 */
	bool InsertOrder(VehicleID vehicle_id, uint32 order_id, TileIndex destination, AIOrderFlags order_flags);

	/**
	 * Removes an order from the vehicle's order list.
	 * @param vehicle_id  the vehicle to remove the order from.
	 * @param order_id    the order to remove from the order list.
	 * @pre AIVehicle::IsValidVehicleOrder(vehicle_id, order_id).
	 * @return true if and only if the order was removed.
	 */
	bool RemoveOrder(VehicleID vehicle_id, uint32 order_id);

	/**
	 * Changes the order flags of the given order.
	 * @param vehicle_id  the vehicle to change the order of.
	 * @param order_id    the order to change.
	 * @param order_flags the new flags given to the order.
	 * @pre IsValidVehicleOrder(vehicle_id, order_id).
	 * @pre AreOrderFlagsValid(GetOrderDestination(vehicle_id, order_id), order_flags).
	 * @return true if and only if the order was changed.
	 */
	bool ChangeOrder(VehicleID vehicle_id, uint32 order_id, AIOrderFlags order_flags);

	/**
	 * Copies the orders from another vehicle. The orders of the main
	 * vehicle are going to be the orders of the changed vehicle.
	 * @param vehicle_id      the vehicle to copy the orders to.
	 * @param main_vehicle_id the vehicle to copy the orders from.
	 * @pre AIVehicle::IsValidVehicle(vehicle_id).
	 * @pre AIVehicle::IsValidVehicle(main_vehicle_id).
	 * @return true if and only if the copying succeeded.
	 */
	bool CopyOrders(VehicleID vehicle_id, VehicleID main_vehicle_id);

	/**
	 * Shares the orders between two vehicles. The orders of the main
	 * vehicle are going to be the orders of the changed vehicle.
	 * @param vehicle_id      the vehicle to add to the shared order list.
	 * @param main_vehicle_id the vehicle to share the orders with.
	 * @pre AIVehicle::IsValidVehicle(vehicle_id).
	 * @pre AIVehicle::IsValidVehicle(main_vehicle_id).
	 * @return true if and only if the sharing succeeded.
	 */
	bool ShareOrders(VehicleID vehicle_id, VehicleID main_vehicle_id);

	/**
	 * Removes the given vehicle from a shared orders list.
	 * @param vehicle_id the vehicle to remove from the shared order list.
	 * @pre AIVehicle::IsValidVehicle(vehicle_id).
	 * @return true if and only if the unsharing succeeded.
	 */
	bool UnshareOrders(VehicleID vehicle_id);
};
DECLARE_ENUM_AS_BIT_SET(AIOrder::AIOrderFlags);

#ifdef DEFINE_SQUIRREL_CLASS

/* Custom template to allow AIOrderFlags as param */
namespace SQConvert {
	template <> AIOrder::AIOrderFlags GetParam(ForceType<AIOrder::AIOrderFlags>, HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AIOrder::AIOrderFlags)tmp; }
	template <> int Return<AIOrder::AIOrderFlags>(HSQUIRRELVM vm, AIOrder::AIOrderFlags res) { sq_pushinteger(vm, (int32)res); return 1; }
}; // namespace SQConvert

void SQAIOrderRegister(Squirrel *engine) {
	DefSQClass <AIOrder> SQAIOrder("AIOrder");
	SQAIOrder.PreRegister(engine);
	SQAIOrder.AddConstructor(engine);
	SQAIOrder.DefSQFunction(engine, &AIOrder::IsValidVehicleOrder, "IsValidVehicleOrder");
	SQAIOrder.DefSQFunction(engine, &AIOrder::ShareOrders,         "ShareOrders");
	SQAIOrder.DefSQFunction(engine, &AIOrder::UnshareOrders,       "UnshareOrders");
	SQAIOrder.DefSQFunction(engine, &AIOrder::AreOrderFlagsValid,  "AreOrderFlagsValid");
	SQAIOrder.DefSQFunction(engine, &AIOrder::GetNumberOfOrders,   "GetNumberOfOrders");
	SQAIOrder.DefSQFunction(engine, &AIOrder::GetOrderDestination, "GetOrderDestination");
	SQAIOrder.DefSQFunction(engine, &AIOrder::GetOrderFlags,       "GetOrderFlags");
	SQAIOrder.DefSQFunction(engine, &AIOrder::AppendOrder,         "AppendOrder");
	SQAIOrder.DefSQFunction(engine, &AIOrder::InsertOrder,         "InsertOrder");
	SQAIOrder.DefSQFunction(engine, &AIOrder::RemoveOrder,         "RemoveOrder");
	SQAIOrder.DefSQFunction(engine, &AIOrder::ChangeOrder,         "ChangeOrder");
	SQAIOrder.DefSQFunction(engine, &AIOrder::CopyOrders,          "CopyOrders");
	SQAIOrder.PostRegister(engine);
}
#endif /* SQUIRREL_CLASS */

#endif /* AI_ORDER_HPP */