Mercurial > hg > openttd
changeset 11005:3da26d1d540d draft
(svn r15345) -Add [NoAI]: Add AIVehicle::HasSharedOrders() and AIVehicleList_SharedOrders.
author | Yexo <Yexo@openttd.org> |
---|---|
date | Thu, 05 Feb 2009 01:12:49 +0000 |
parents | 48aced5a73d8 |
children | 57ca96988687 |
files | src/ai/api/ai_vehicle.cpp src/ai/api/ai_vehicle.hpp src/ai/api/ai_vehicle.hpp.sq src/ai/api/ai_vehiclelist.cpp src/ai/api/ai_vehiclelist.hpp src/ai/api/ai_vehiclelist.hpp.sq |
diffstat | 6 files changed, 65 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- 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; +}
--- a/src/ai/api/ai_vehicle.hpp +++ b/src/ai/api/ai_vehicle.hpp @@ -478,6 +478,14 @@ * @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 */
--- a/src/ai/api/ai_vehicle.hpp.sq +++ b/src/ai/api/ai_vehicle.hpp.sq @@ -137,6 +137,7 @@ 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); }
--- 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_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); + } +}
--- a/src/ai/api/ai_vehiclelist.hpp +++ b/src/ai/api/ai_vehiclelist.hpp @@ -31,4 +31,18 @@ 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 */
--- a/src/ai/api/ai_vehiclelist.hpp.sq +++ b/src/ai/api/ai_vehiclelist.hpp.sq @@ -40,3 +40,22 @@ 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); +}