changeset 12992:98a12053c614 draft

(svn r17486) -Add [NoAI]: a vehicle list for all vehicle that are ordered to a specific depot
author rubidium <rubidium@openttd.org>
date Wed, 09 Sep 2009 09:46:08 +0000
parents d3df6e84bf71
children 90133be1c216
files src/ai/ai_instance.cpp src/ai/api/ai_changelog.hpp src/ai/api/ai_vehiclelist.cpp src/ai/api/ai_vehiclelist.hpp src/ai/api/ai_vehiclelist.hpp.sq
diffstat 5 files changed, 92 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/ai_instance.cpp
+++ b/src/ai/ai_instance.cpp
@@ -244,6 +244,7 @@
 	SQAIVehicle_Register(this->engine);
 	SQAIVehicleList_Register(this->engine);
 	SQAIVehicleList_DefaultGroup_Register(this->engine);
+	SQAIVehicleList_Depot_Register(this->engine);
 	SQAIVehicleList_Group_Register(this->engine);
 	SQAIVehicleList_SharedOrders_Register(this->engine);
 	SQAIVehicleList_Station_Register(this->engine);
--- a/src/ai/api/ai_changelog.hpp
+++ b/src/ai/api/ai_changelog.hpp
@@ -22,6 +22,7 @@
  * \li AIBaseStation
  * \li AIBuoyList
  * \li AIEventCompanyAskMerger
+ * \li AIIndustry::GetLastMonthTransportedPercentage
  * \li AIRail::RemoveRailStationTileRectangle
  * \li AIRail::RemoveRailWaypointTileRectangle
  * \li AISubsidy::SubsidyParticipantType
@@ -30,7 +31,7 @@
  * \li AISubsidy::GetDestinationType
  * \li AISubsidy::GetDestinationIndex
  * \li AITown::GetLastMonthTransportedPercentage
- * \li AIIndustry::GetLastMonthTransportedPercentage
+ * \li AIVehicleList_Depot
  *
  * API removals:
  * \li AIOrder::ChangeOrder, use AIOrder::SetOrderFlags instead
--- a/src/ai/api/ai_vehiclelist.cpp
+++ b/src/ai/api/ai_vehiclelist.cpp
@@ -11,9 +11,12 @@
 
 #include "ai_vehiclelist.hpp"
 #include "ai_group.hpp"
+#include "ai_map.hpp"
 #include "ai_station.hpp"
 #include "ai_vehicle.hpp"
 #include "../../company_func.h"
+#include "../../depot_base.h"
+#include "../../depot_map.h"
 #include "../../vehicle_base.h"
 
 AIVehicleList::AIVehicleList()
@@ -43,6 +46,57 @@
 	}
 }
 
+AIVehicleList_Depot::AIVehicleList_Depot(TileIndex tile)
+{
+	if (!AIMap::IsValidTile(tile)) return;
+
+	DestinationID dest;
+	VehicleType type;
+
+	switch (GetTileType(tile)) {
+		case MP_STATION: // Aircraft
+			if (!IsAirport(tile)) return;
+			type = VEH_AIRCRAFT;
+			dest = GetStationIndex(tile);
+			break;
+
+		case MP_RAILWAY:
+			if (!IsRailDepot(tile)) return;
+			type = VEH_TRAIN;
+			dest = Depot::GetByTile(tile)->index;
+			break;
+
+		case MP_ROAD:
+			if (!IsRoadDepot(tile)) return;
+			type = VEH_ROAD;
+			dest = Depot::GetByTile(tile)->index;
+			break;
+
+		case MP_WATER:
+			if (!IsShipDepot(tile)) return;
+			type = VEH_SHIP;
+			dest = Depot::GetByTile(min(tile, GetOtherShipDepotTile(tile)))->index;
+			break;
+
+		default: // No depot
+			return;
+	}
+
+	const Vehicle *v;
+	FOR_ALL_VEHICLES(v) {
+		if (v->owner == _current_company && v->IsPrimaryVehicle() && v->type == type) {
+			const Order *order;
+
+			FOR_VEHICLE_ORDERS(v, order) {
+				if (order->IsType(OT_GOTO_DEPOT) && order->GetDestination() == dest) {
+					this->AddItem(v->index);
+					break;
+				}
+			}
+		}
+	}
+}
+
 AIVehicleList_SharedOrders::AIVehicleList_SharedOrders(VehicleID vehicle_id)
 {
 	if (!AIVehicle::IsValidVehicle(vehicle_id)) return;
--- a/src/ai/api/ai_vehiclelist.hpp
+++ b/src/ai/api/ai_vehiclelist.hpp
@@ -41,6 +41,24 @@
 };
 
 /**
+ * Creates a list of vehicles that have orders to a given depot.
+ * The list is created with a tile. If the tile is part of an airport all
+ * aircraft having a depot order on a hangar of that airport will be
+ * returned. For all other vehicle types the tile has to be a depot or
+ * an empty list will be returned.
+ * @ingroup AIList
+ */
+class AIVehicleList_Depot : public AIAbstractList {
+public:
+	static const char *GetClassName() { return "AIVehicleList_Depot"; }
+
+	/**
+	 * @param tile The tile of the depot to get the list of vehicles from, which have orders to it.
+	 */
+	AIVehicleList_Depot(TileIndex tile);
+};
+
+/**
  * Creates a list of vehicles that share orders.
  * @ingroup AIList
  */
--- a/src/ai/api/ai_vehiclelist.hpp.sq
+++ b/src/ai/api/ai_vehiclelist.hpp.sq
@@ -46,6 +46,23 @@
 }
 
 namespace SQConvert {
+	/* Allow AIVehicleList_Depot to be used as Squirrel parameter */
+	template <> AIVehicleList_Depot *GetParam(ForceType<AIVehicleList_Depot *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIVehicleList_Depot *)instance; }
+	template <> AIVehicleList_Depot &GetParam(ForceType<AIVehicleList_Depot &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIVehicleList_Depot *)instance; }
+	template <> const AIVehicleList_Depot *GetParam(ForceType<const AIVehicleList_Depot *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIVehicleList_Depot *)instance; }
+	template <> const AIVehicleList_Depot &GetParam(ForceType<const AIVehicleList_Depot &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIVehicleList_Depot *)instance; }
+	template <> int Return<AIVehicleList_Depot *>(HSQUIRRELVM vm, AIVehicleList_Depot *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AIVehicleList_Depot", res, NULL, DefSQDestructorCallback<AIVehicleList_Depot>); return 1; }
+}; // namespace SQConvert
+
+void SQAIVehicleList_Depot_Register(Squirrel *engine) {
+	DefSQClass <AIVehicleList_Depot> SQAIVehicleList_Depot("AIVehicleList_Depot");
+	SQAIVehicleList_Depot.PreRegister(engine, "AIAbstractList");
+	SQAIVehicleList_Depot.AddConstructor<void (AIVehicleList_Depot::*)(TileIndex tile), 2>(engine, "xi");
+
+	SQAIVehicleList_Depot.PostRegister(engine);
+}
+
+namespace SQConvert {
 	/* Allow AIVehicleList_SharedOrders to be used as Squirrel parameter */
 	template <> AIVehicleList_SharedOrders *GetParam(ForceType<AIVehicleList_SharedOrders *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIVehicleList_SharedOrders *)instance; }
 	template <> AIVehicleList_SharedOrders &GetParam(ForceType<AIVehicleList_SharedOrders &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIVehicleList_SharedOrders *)instance; }