changeset 11265:d56f68646d3e draft

(svn r15614) -Add [NoAI]: AIVehicleList_Group(group_id) and AIVehicleList_DefaultGroup(vehicle_type).
author yexo <yexo@openttd.org>
date Wed, 04 Mar 2009 22:37:25 +0000
parents aa863e852da8
children 47b87f7aa945
files src/ai/ai_instance.cpp src/ai/api/ai_vehiclelist.cpp src/ai/api/ai_vehiclelist.hpp src/ai/api/ai_vehiclelist.hpp.sq
diffstat 4 files changed, 92 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/ai_instance.cpp
+++ b/src/ai/ai_instance.cpp
@@ -225,6 +225,8 @@
 	SQAITunnel_Register(this->engine);
 	SQAIVehicle_Register(this->engine);
 	SQAIVehicleList_Register(this->engine);
+	SQAIVehicleList_DefaultGroup_Register(this->engine);
+	SQAIVehicleList_Group_Register(this->engine);
 	SQAIVehicleList_SharedOrders_Register(this->engine);
 	SQAIVehicleList_Station_Register(this->engine);
 	SQAIWaypoint_Register(this->engine);
--- a/src/ai/api/ai_vehiclelist.cpp
+++ b/src/ai/api/ai_vehiclelist.cpp
@@ -3,6 +3,7 @@
 /** @file ai_vehiclelist.cpp Implementation of AIVehicleList and friends. */
 
 #include "ai_vehiclelist.hpp"
+#include "ai_group.hpp"
 #include "ai_station.hpp"
 #include "ai_vehicle.hpp"
 #include "../../company_func.h"
@@ -10,7 +11,7 @@
 
 AIVehicleList::AIVehicleList()
 {
-	Vehicle *v;
+	const Vehicle *v;
 	FOR_ALL_VEHICLES(v) {
 		if (v->owner == _current_company && v->IsPrimaryVehicle()) this->AddItem(v->index);
 	}
@@ -20,8 +21,7 @@
 {
 	if (!AIStation::IsValidStation(station_id)) return;
 
-	Vehicle *v;
-
+	const Vehicle *v;
 	FOR_ALL_VEHICLES(v) {
 		if (v->owner == _current_company && v->IsPrimaryVehicle()) {
 			const Order *order;
@@ -44,3 +44,27 @@
 		this->AddItem(v->index);
 	}
 }
+
+AIVehicleList_Group::AIVehicleList_Group(GroupID group_id)
+{
+	if (!AIGroup::IsValidGroup((AIGroup::GroupID)group_id)) return;
+
+	const Vehicle *v;
+	FOR_ALL_VEHICLES(v) {
+		if (v->owner == _current_company && v->IsPrimaryVehicle()) {
+			if (v->group_id == group_id) this->AddItem(v->index);
+		}
+	}
+}
+
+AIVehicleList_DefaultGroup::AIVehicleList_DefaultGroup(AIVehicle::VehicleType vehicle_type)
+{
+	if (vehicle_type < AIVehicle::VT_RAIL || vehicle_type > AIVehicle::VT_AIR) return;
+
+	const Vehicle *v;
+	FOR_ALL_VEHICLES(v) {
+		if (v->owner == _current_company && v->IsPrimaryVehicle()) {
+			if (v->type == vehicle_type && v->group_id == AIGroup::GROUP_DEFAULT) this->AddItem(v->index);
+		}
+	}
+}
--- a/src/ai/api/ai_vehiclelist.hpp
+++ b/src/ai/api/ai_vehiclelist.hpp
@@ -6,6 +6,7 @@
 #define AI_VEHICLELIST_HPP
 
 #include "ai_abstractlist.hpp"
+#include "ai_vehicle.hpp"
 
 /**
  * Creates a list of vehicles of which you are the owner.
@@ -45,4 +46,32 @@
 	AIVehicleList_SharedOrders(VehicleID vehicle_id);
 };
 
+/**
+ * Creates a list of vehicles that are in a group.
+ * @ingroup AIList
+ */
+class AIVehicleList_Group : public AIAbstractList {
+public:
+	static const char *GetClassName() { return "AIVehicleList_Group"; }
+
+	/**
+	 * @param group_id The ID of the group the vehicles are in.
+	 */
+	AIVehicleList_Group(GroupID group_id);
+};
+
+/**
+ * Creates a list of vehicles that are in the default group.
+ * @ingroup AIList
+ */
+class AIVehicleList_DefaultGroup : public AIAbstractList {
+public:
+	static const char *GetClassName() { return "AIVehicleList_DefaultGroup"; }
+
+	/**
+	 * @param vehicle_type The VehicleType to get the list of vehicles for.
+	 */
+	AIVehicleList_DefaultGroup(AIVehicle::VehicleType vehicle_type);
+};
+
 #endif /* AI_VEHICLELIST_HPP */
--- a/src/ai/api/ai_vehiclelist.hpp.sq
+++ b/src/ai/api/ai_vehiclelist.hpp.sq
@@ -53,3 +53,37 @@
 
 	SQAIVehicleList_SharedOrders.PostRegister(engine);
 }
+
+namespace SQConvert {
+	/* Allow AIVehicleList_Group to be used as Squirrel parameter */
+	template <> AIVehicleList_Group *GetParam(ForceType<AIVehicleList_Group *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIVehicleList_Group *)instance; }
+	template <> AIVehicleList_Group &GetParam(ForceType<AIVehicleList_Group &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIVehicleList_Group *)instance; }
+	template <> const AIVehicleList_Group *GetParam(ForceType<const AIVehicleList_Group *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIVehicleList_Group *)instance; }
+	template <> const AIVehicleList_Group &GetParam(ForceType<const AIVehicleList_Group &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIVehicleList_Group *)instance; }
+	template <> int Return<AIVehicleList_Group *>(HSQUIRRELVM vm, AIVehicleList_Group *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AIVehicleList_Group", res, NULL, DefSQDestructorCallback<AIVehicleList_Group>); return 1; }
+}; // namespace SQConvert
+
+void SQAIVehicleList_Group_Register(Squirrel *engine) {
+	DefSQClass <AIVehicleList_Group> SQAIVehicleList_Group("AIVehicleList_Group");
+	SQAIVehicleList_Group.PreRegister(engine, "AIAbstractList");
+	SQAIVehicleList_Group.AddConstructor<void (AIVehicleList_Group::*)(GroupID group_id), 2>(engine, "xi");
+
+	SQAIVehicleList_Group.PostRegister(engine);
+}
+
+namespace SQConvert {
+	/* Allow AIVehicleList_DefaultGroup to be used as Squirrel parameter */
+	template <> AIVehicleList_DefaultGroup *GetParam(ForceType<AIVehicleList_DefaultGroup *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIVehicleList_DefaultGroup *)instance; }
+	template <> AIVehicleList_DefaultGroup &GetParam(ForceType<AIVehicleList_DefaultGroup &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIVehicleList_DefaultGroup *)instance; }
+	template <> const AIVehicleList_DefaultGroup *GetParam(ForceType<const AIVehicleList_DefaultGroup *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIVehicleList_DefaultGroup *)instance; }
+	template <> const AIVehicleList_DefaultGroup &GetParam(ForceType<const AIVehicleList_DefaultGroup &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIVehicleList_DefaultGroup *)instance; }
+	template <> int Return<AIVehicleList_DefaultGroup *>(HSQUIRRELVM vm, AIVehicleList_DefaultGroup *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AIVehicleList_DefaultGroup", res, NULL, DefSQDestructorCallback<AIVehicleList_DefaultGroup>); return 1; }
+}; // namespace SQConvert
+
+void SQAIVehicleList_DefaultGroup_Register(Squirrel *engine) {
+	DefSQClass <AIVehicleList_DefaultGroup> SQAIVehicleList_DefaultGroup("AIVehicleList_DefaultGroup");
+	SQAIVehicleList_DefaultGroup.PreRegister(engine, "AIAbstractList");
+	SQAIVehicleList_DefaultGroup.AddConstructor<void (AIVehicleList_DefaultGroup::*)(AIVehicle::VehicleType vehicle_type), 2>(engine, "xi");
+
+	SQAIVehicleList_DefaultGroup.PostRegister(engine);
+}