diff options
author | Yexo <Yexo@openttd.org> | 2009-02-05 01:12:49 +0000 |
---|---|---|
committer | Yexo <Yexo@openttd.org> | 2009-02-05 01:12:49 +0000 |
commit | 8f51ee50c1d810a7472ed8a0194f2a6c36b36b99 (patch) | |
tree | 6b29d7d897ad9b7e9938b372fd066771967fc2db /src | |
parent | 85594561e0cff8553f14b14d7cc37556b0243a92 (diff) | |
download | openttd-8f51ee50c1d810a7472ed8a0194f2a6c36b36b99.tar.xz |
(svn r15345) -Add [NoAI]: Add AIVehicle::HasSharedOrders() and AIVehicleList_SharedOrders.
Diffstat (limited to 'src')
-rw-r--r-- | src/ai/api/ai_vehicle.cpp | 8 | ||||
-rw-r--r-- | src/ai/api/ai_vehicle.hpp | 8 | ||||
-rw-r--r-- | src/ai/api/ai_vehicle.hpp.sq | 1 | ||||
-rw-r--r-- | src/ai/api/ai_vehiclelist.cpp | 15 | ||||
-rw-r--r-- | src/ai/api/ai_vehiclelist.hpp | 14 | ||||
-rw-r--r-- | src/ai/api/ai_vehiclelist.hpp.sq | 19 |
6 files changed, 65 insertions, 0 deletions
diff --git a/src/ai/api/ai_vehicle.cpp b/src/ai/api/ai_vehicle.cpp index 18e17221d..8951cb5f2 100644 --- a/src/ai/api/ai_vehicle.cpp +++ b/src/ai/api/ai_vehicle.cpp @@ -390,3 +390,11 @@ default: NOT_REACHED(); } } + +/* static */ bool AIVehicle::HasSharedOrders(VehicleID vehicle_id) +{ + if (!IsValidVehicle(vehicle_id)) return false; + + Vehicle *v = ::GetVehicle(vehicle_id); + return v->orders.list != NULL && v->orders.list->GetNumVehicles() > 1; +} diff --git a/src/ai/api/ai_vehicle.hpp b/src/ai/api/ai_vehicle.hpp index 961a451d3..5b9a276c6 100644 --- a/src/ai/api/ai_vehicle.hpp +++ b/src/ai/api/ai_vehicle.hpp @@ -478,6 +478,14 @@ public: * @return True if the vehicle is articulated. */ static bool IsArticulated(VehicleID vehicle_id); + + /** + * Check if the vehicle has shared orders. + * @param vehicle_id The vehicle to check. + * @pre IsValidVehicle(vehicle_id). + * @return True if the vehicle has shared orders. + */ + static bool HasSharedOrders(VehicleID vehicle_id); }; #endif /* AI_VEHICLE_HPP */ diff --git a/src/ai/api/ai_vehicle.hpp.sq b/src/ai/api/ai_vehicle.hpp.sq index babb85306..f81124fda 100644 --- a/src/ai/api/ai_vehicle.hpp.sq +++ b/src/ai/api/ai_vehicle.hpp.sq @@ -137,6 +137,7 @@ void SQAIVehicle_Register(Squirrel *engine) { SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetCargoLoad, "GetCargoLoad", 3, "xii"); SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetGroupID, "GetGroupID", 2, "xi"); SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::IsArticulated, "IsArticulated", 2, "xi"); + SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::HasSharedOrders, "HasSharedOrders", 2, "xi"); SQAIVehicle.PostRegister(engine); } diff --git a/src/ai/api/ai_vehiclelist.cpp b/src/ai/api/ai_vehiclelist.cpp index c7e75c795..1c099910b 100644 --- a/src/ai/api/ai_vehiclelist.cpp +++ b/src/ai/api/ai_vehiclelist.cpp @@ -4,6 +4,7 @@ #include "ai_vehiclelist.hpp" #include "ai_station.hpp" +#include "ai_vehicle.hpp" #include "../../company_func.h" #include "../../vehicle_base.h" @@ -34,3 +35,17 @@ AIVehicleList_Station::AIVehicleList_Station(StationID station_id) } } } + +AIVehicleList_SharedOrders::AIVehicleList_SharedOrders(VehicleID vehicle_id) +{ + if (!AIVehicle::IsValidVehicle(vehicle_id)) return; + + Vehicle *v = GetVehicle(vehicle_id)->FirstShared(); + if (v == NULL) { + this->AddItem(vehicle_id); + return; + } + for (; v != NULL; v->NextShared()) { + this->AddItem(v->index); + } +} diff --git a/src/ai/api/ai_vehiclelist.hpp b/src/ai/api/ai_vehiclelist.hpp index 0e91d75bc..a18d938b1 100644 --- a/src/ai/api/ai_vehiclelist.hpp +++ b/src/ai/api/ai_vehiclelist.hpp @@ -31,4 +31,18 @@ public: AIVehicleList_Station(StationID station_id); }; +/** + * Creates a list of vehicles that share orders. + * @ingroup AIList + */ +class AIVehicleList_SharedOrders : public AIAbstractList { +public: + static const char *GetClassName() { return "AIVehicleList_SharedOrders"; } + + /** + * @param station_id The vehicle that the rest shared orders with. + */ + AIVehicleList_SharedOrders(VehicleID vehicle_id); +}; + #endif /* AI_VEHICLELIST_HPP */ diff --git a/src/ai/api/ai_vehiclelist.hpp.sq b/src/ai/api/ai_vehiclelist.hpp.sq index 5a5f930cb..603e36565 100644 --- a/src/ai/api/ai_vehiclelist.hpp.sq +++ b/src/ai/api/ai_vehiclelist.hpp.sq @@ -40,3 +40,22 @@ void SQAIVehicleList_Station_Register(Squirrel *engine) { SQAIVehicleList_Station.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; } + template <> const AIVehicleList_SharedOrders *GetParam(ForceType<const AIVehicleList_SharedOrders *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIVehicleList_SharedOrders *)instance; } + template <> const AIVehicleList_SharedOrders &GetParam(ForceType<const AIVehicleList_SharedOrders &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIVehicleList_SharedOrders *)instance; } + template <> int Return<AIVehicleList_SharedOrders *>(HSQUIRRELVM vm, AIVehicleList_SharedOrders *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AIVehicleList_SharedOrders", res, NULL, DefSQDestructorCallback<AIVehicleList_SharedOrders>); return 1; } +}; // namespace SQConvert + +void SQAIVehicleList_SharedOrders_Register(Squirrel *engine) { + DefSQClass <AIVehicleList_SharedOrders> SQAIVehicleList_SharedOrders("AIVehicleList_SharedOrders"); + SQAIVehicleList_SharedOrders.PreRegister(engine, "AIAbstractList"); + SQAIVehicleList_SharedOrders.AddConstructor<void (AIVehicleList_SharedOrders::*)(VehicleID vehicle_id), 2>(engine, "xi"); + + SQAIVehicleList_SharedOrders.DefSQStaticMethod(engine, &AIVehicleList_SharedOrders::GetClassName, "GetClassName", 1, "x"); + + SQAIVehicleList_SharedOrders.PostRegister(engine); +} |