diff options
author | yexo <yexo@openttd.org> | 2009-02-19 07:40:08 +0000 |
---|---|---|
committer | yexo <yexo@openttd.org> | 2009-02-19 07:40:08 +0000 |
commit | 69df9f278732429ea2a1c66897c15bb53a5e4c15 (patch) | |
tree | fa0d25f1d5a2c7fdfd64122bd10dc5ee7291d2fe /src | |
parent | 29173de379eaa1f8c7a1a409ad0232a748172906 (diff) | |
download | openttd-69df9f278732429ea2a1c66897c15bb53a5e4c15.tar.xz |
(svn r15519) -Add [NoAI]: support for waypoints by adding AIRail::Build/Remove RailWaypoint, AIWaypoint and AIWaypointList.
Diffstat (limited to 'src')
-rw-r--r-- | src/ai/ai_instance.cpp | 2 | ||||
-rw-r--r-- | src/ai/api/ai_rail.cpp | 18 | ||||
-rw-r--r-- | src/ai/api/ai_rail.hpp | 21 | ||||
-rw-r--r-- | src/ai/api/ai_rail.hpp.sq | 2 | ||||
-rw-r--r-- | src/ai/api/ai_types.hpp | 1 | ||||
-rw-r--r-- | src/ai/api/ai_waypoint.cpp | 53 | ||||
-rw-r--r-- | src/ai/api/ai_waypoint.hpp | 66 | ||||
-rw-r--r-- | src/ai/api/ai_waypoint.hpp.sq | 33 | ||||
-rw-r--r-- | src/ai/api/ai_waypointlist.cpp | 29 | ||||
-rw-r--r-- | src/ai/api/ai_waypointlist.hpp | 35 | ||||
-rw-r--r-- | src/ai/api/ai_waypointlist.hpp.sq | 38 |
11 files changed, 298 insertions, 0 deletions
diff --git a/src/ai/ai_instance.cpp b/src/ai/ai_instance.cpp index 12165e9dd..2030f73e1 100644 --- a/src/ai/ai_instance.cpp +++ b/src/ai/ai_instance.cpp @@ -69,6 +69,8 @@ #include "api/ai_tunnel.hpp.sq" #include "api/ai_vehicle.hpp.sq" #include "api/ai_vehiclelist.hpp.sq" +#include "api/ai_waypoint.hpp.sq" +#include "api/ai_waypointlist.hpp.sq" #undef DEFINE_SCRIPT_FILES diff --git a/src/ai/api/ai_rail.cpp b/src/ai/api/ai_rail.cpp index f520fdb5c..bce4719ca 100644 --- a/src/ai/api/ai_rail.cpp +++ b/src/ai/api/ai_rail.cpp @@ -170,6 +170,24 @@ return AIObject::DoCommand(tile, p1, p2, CMD_BUILD_RAILROAD_STATION); } +/* static */ bool AIRail::BuildRailWaypoint(TileIndex tile) +{ + EnforcePrecondition(false, ::IsValidTile(tile)); + EnforcePrecondition(false, IsRailTile(tile)); + EnforcePrecondition(false, GetRailTracks(tile) == RAILTRACK_NE_SW || GetRailTracks(tile) == RAILTRACK_NW_SE); + EnforcePrecondition(false, IsRailTypeAvailable(GetCurrentRailType())); + + return AIObject::DoCommand(tile, 0, 0, CMD_BUILD_TRAIN_WAYPOINT); +} + +/* static */ bool AIRail::RemoveRailWaypoint(TileIndex tile) +{ + EnforcePrecondition(false, ::IsValidTile(tile)); + EnforcePrecondition(false, IsRailWaypointTile(tile)); + + return AIObject::DoCommand(tile, 0, 0, CMD_REMOVE_TRAIN_WAYPOINT); +} + /* static */ bool AIRail::RemoveRailStationTileRect(TileIndex tile, TileIndex tile2) { EnforcePrecondition(false, ::IsValidTile(tile)); diff --git a/src/ai/api/ai_rail.hpp b/src/ai/api/ai_rail.hpp index 7444907f2..0044d56a9 100644 --- a/src/ai/api/ai_rail.hpp +++ b/src/ai/api/ai_rail.hpp @@ -261,6 +261,27 @@ public: static bool BuildNewGRFRailStation(TileIndex tile, RailTrack direction, uint num_platforms, uint platform_length, StationID station_id, CargoID cargo_id, IndustryType source_industry, IndustryType goal_industry, int distance, bool source_station); /** + * Build a rail waypoint. + * @param tile Place to build the waypoint. + * @pre AIMap::IsValidTile(tile). + * @pre IsRailTile(tile). + * @pre GetRailTracks(tile) == RAILTRACK_NE_SW || GetRailTracks(tile) == RAILTRACK_NW_SE. + * @pre IsRailTypeAvailable(GetCurrentRailType()). + * @exception AIError::ERR_FLAT_LAND_REQUIRED + * @return Whether the rail waypoint has been/can be build or not. + */ + static bool BuildRailWaypoint(TileIndex tile); + + /** + * Remove a rail waypoint. + * @param tile Place to remove the waypoint from. + * @pre AIMap::IsValidTile(tile). + * @pre IsRailWaypointTile(tile). + * @return Whether the rail waypoint has been/can be removed or not. + */ + static bool RemoveRailWaypoint(TileIndex tile); + + /** * Remove a rectangle of platform pieces from a rail station. * @param tile One corner of the rectangle to clear. * @param tile2 The oppposite corner. diff --git a/src/ai/api/ai_rail.hpp.sq b/src/ai/api/ai_rail.hpp.sq index 612551aa4..ef7f6c9de 100644 --- a/src/ai/api/ai_rail.hpp.sq +++ b/src/ai/api/ai_rail.hpp.sq @@ -77,6 +77,8 @@ void SQAIRail_Register(Squirrel *engine) { SQAIRail.DefSQStaticMethod(engine, &AIRail::BuildRailDepot, "BuildRailDepot", 3, "?ii"); SQAIRail.DefSQStaticMethod(engine, &AIRail::BuildRailStation, "BuildRailStation", 6, "?iiiii"); SQAIRail.DefSQStaticMethod(engine, &AIRail::BuildNewGRFRailStation, "BuildNewGRFRailStation", 11, "?iiiiiiiiib"); + SQAIRail.DefSQStaticMethod(engine, &AIRail::BuildRailWaypoint, "BuildRailWaypoint", 2, "?i"); + SQAIRail.DefSQStaticMethod(engine, &AIRail::RemoveRailWaypoint, "RemoveRailWaypoint", 2, "?i"); SQAIRail.DefSQStaticMethod(engine, &AIRail::RemoveRailStationTileRect, "RemoveRailStationTileRect", 3, "?ii"); SQAIRail.DefSQStaticMethod(engine, &AIRail::GetRailTracks, "GetRailTracks", 2, "?i"); SQAIRail.DefSQStaticMethod(engine, &AIRail::BuildRailTrack, "BuildRailTrack", 3, "?ii"); diff --git a/src/ai/api/ai_types.hpp b/src/ai/api/ai_types.hpp index 086cf75e0..2ccd1d27d 100644 --- a/src/ai/api/ai_types.hpp +++ b/src/ai/api/ai_types.hpp @@ -23,6 +23,7 @@ typedef uint16 StringID; //!< The ID of a string. typedef uint32 TileIndex; //!< The ID of a tile (just named differently). typedef uint16 TownID; //!< The ID of a town. typedef uint16 VehicleID; //!< The ID of a vehicle. +typedef uint16 WaypointID; //!< The ID of a waypoint. /* Types we defined ourself, as the OpenTTD core doesn't have them (yet) */ typedef uint AIErrorType; //!< The types of errors inside the NoAI framework. diff --git a/src/ai/api/ai_waypoint.cpp b/src/ai/api/ai_waypoint.cpp new file mode 100644 index 000000000..3836978df --- /dev/null +++ b/src/ai/api/ai_waypoint.cpp @@ -0,0 +1,53 @@ +/* $Id$ */ + +/** @file ai_waypoint.cpp Implementation of AIWaypoint. */ + +#include "ai_waypoint.hpp" +#include "ai_rail.hpp" +#include "../../command_func.h" +#include "../../string_func.h" +#include "../../strings_func.h" +#include "../../company_func.h" +#include "../../waypoint.h" +#include "../../core/alloc_func.hpp" +#include "table/strings.h" + +/* static */ bool AIWaypoint::IsValidWaypoint(WaypointID waypoint_id) +{ + return ::IsValidWaypointID(waypoint_id) && ::GetWaypoint(waypoint_id)->owner == _current_company; +} + +/* static */ WaypointID AIWaypoint::GetWaypointID(TileIndex tile) +{ + if (!AIRail::IsRailWaypointTile(tile)) return WAYPOINT_INVALID; + + return ::GetWaypointIndex(tile); +} + +/* static */ char *AIWaypoint::GetName(WaypointID waypoint_id) +{ + if (!IsValidWaypoint(waypoint_id)) return NULL; + + static const int len = 64; + char *waypoint_name = MallocT<char>(len); + + ::SetDParam(0, waypoint_id); + ::GetString(waypoint_name, STR_WAYPOINT_RAW, &waypoint_name[len - 1]); + return waypoint_name; +} + +/* static */ bool AIWaypoint::SetName(WaypointID waypoint_id, const char *name) +{ + EnforcePrecondition(false, IsValidWaypoint(waypoint_id)); + EnforcePrecondition(false, !::StrEmpty(name)); + EnforcePreconditionCustomError(false, ::strlen(name) < MAX_LENGTH_WAYPOINT_NAME_BYTES, AIError::ERR_PRECONDITION_STRING_TOO_LONG); + + return AIObject::DoCommand(0, waypoint_id, 0, CMD_RENAME_WAYPOINT, name); +} + +/* static */ TileIndex AIWaypoint::GetLocation(WaypointID waypoint_id) +{ + if (!IsValidWaypoint(waypoint_id)) return INVALID_TILE; + + return ::GetWaypoint(waypoint_id)->xy; +} diff --git a/src/ai/api/ai_waypoint.hpp b/src/ai/api/ai_waypoint.hpp new file mode 100644 index 000000000..69eacb10e --- /dev/null +++ b/src/ai/api/ai_waypoint.hpp @@ -0,0 +1,66 @@ +/* $Id$ */ + +/** @file ai_waypoint.hpp Everything to query and build waypoints. */ + +#ifndef AI_WAYPOINT_HPP +#define AI_WAYPOINT_HPP + +#include "ai_object.hpp" +#include "ai_error.hpp" + +/** + * Class that handles all waypoint related functions. + */ +class AIWaypoint : public AIObject { +public: + static const char *GetClassName() { return "AIWaypoint"; } + + enum SpecialWaypointIDs { + WAYPOINT_INVALID = -1, //!< An invalid WaypointID. + }; + + /** + * Checks whether the given waypoint is valid and owned by you. + * @param waypoint_id The waypoint to check. + * @return True if and only if the waypoint is valid. + */ + static bool IsValidWaypoint(WaypointID waypoint_id); + + /** + * Get the WaypointID of a tile. + * @param tile The tile to find the WaypointID of. + * @pre AIRail::IsRailWaypointTile(tile). + * @return WaypointID of the waypoint. + */ + static WaypointID GetWaypointID(TileIndex tile); + + /** + * Get the name of a waypoint. + * @param waypoint_id The waypoint to get the name of. + * @pre IsValidWaypoint(waypoint_id). + * @return The name of the waypoint. + */ + static char *GetName(WaypointID waypoint_id); + + /** + * Set the name this waypoint. + * @param waypoint_id The waypoint to set the name of. + * @param name The new name of the waypoint. + * @pre IsValidWaypointwaypoint_id). + * @pre 'name' must have at least one character. + * @pre 'name' must have at most 30 characters. + * @exception AIError::ERR_NAME_IS_NOT_UNIQUE + * @return True if the name was changed. + */ + static bool SetName(WaypointID waypoint_id, const char *name); + + /** + * Get the current location of a waypoint. + * @param waypoint_id The waypoint to get the location of. + * @pre IsValidWaypoint(waypoint_id). + * @return The tile the waypoint is currently on. + */ + static TileIndex GetLocation(WaypointID waypoint_id); +}; + +#endif /* AI_WAYPOINT_HPP */ diff --git a/src/ai/api/ai_waypoint.hpp.sq b/src/ai/api/ai_waypoint.hpp.sq new file mode 100644 index 000000000..4985e745f --- /dev/null +++ b/src/ai/api/ai_waypoint.hpp.sq @@ -0,0 +1,33 @@ +/* $Id$ */ +/* THIS FILE IS AUTO-GENERATED; PLEASE DO NOT ALTER MANUALLY */ + +#include "ai_waypoint.hpp" + +namespace SQConvert { + /* Allow enums to be used as Squirrel parameters */ + template <> AIWaypoint::SpecialWaypointIDs GetParam(ForceType<AIWaypoint::SpecialWaypointIDs>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AIWaypoint::SpecialWaypointIDs)tmp; } + template <> int Return<AIWaypoint::SpecialWaypointIDs>(HSQUIRRELVM vm, AIWaypoint::SpecialWaypointIDs res) { sq_pushinteger(vm, (int32)res); return 1; } + + /* Allow AIWaypoint to be used as Squirrel parameter */ + template <> AIWaypoint *GetParam(ForceType<AIWaypoint *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIWaypoint *)instance; } + template <> AIWaypoint &GetParam(ForceType<AIWaypoint &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIWaypoint *)instance; } + template <> const AIWaypoint *GetParam(ForceType<const AIWaypoint *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIWaypoint *)instance; } + template <> const AIWaypoint &GetParam(ForceType<const AIWaypoint &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIWaypoint *)instance; } + template <> int Return<AIWaypoint *>(HSQUIRRELVM vm, AIWaypoint *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AIWaypoint", res, NULL, DefSQDestructorCallback<AIWaypoint>); return 1; } +}; // namespace SQConvert + +void SQAIWaypoint_Register(Squirrel *engine) { + DefSQClass <AIWaypoint> SQAIWaypoint("AIWaypoint"); + SQAIWaypoint.PreRegister(engine); + SQAIWaypoint.AddConstructor<void (AIWaypoint::*)(), 1>(engine, "x"); + + SQAIWaypoint.DefSQConst(engine, AIWaypoint::WAYPOINT_INVALID, "WAYPOINT_INVALID"); + + SQAIWaypoint.DefSQStaticMethod(engine, &AIWaypoint::IsValidWaypoint, "IsValidWaypoint", 2, "?i"); + SQAIWaypoint.DefSQStaticMethod(engine, &AIWaypoint::GetWaypointID, "GetWaypointID", 2, "?i"); + SQAIWaypoint.DefSQStaticMethod(engine, &AIWaypoint::GetName, "GetName", 2, "?i"); + SQAIWaypoint.DefSQStaticMethod(engine, &AIWaypoint::SetName, "SetName", 3, "?is"); + SQAIWaypoint.DefSQStaticMethod(engine, &AIWaypoint::GetLocation, "GetLocation", 2, "?i"); + + SQAIWaypoint.PostRegister(engine); +} diff --git a/src/ai/api/ai_waypointlist.cpp b/src/ai/api/ai_waypointlist.cpp new file mode 100644 index 000000000..2b25157a4 --- /dev/null +++ b/src/ai/api/ai_waypointlist.cpp @@ -0,0 +1,29 @@ +/* $Id$ */ + +/** @file ai_waypointlist.cpp Implementation of AIWaypointList and friends. */ + +#include "ai_waypointlist.hpp" +#include "ai_vehicle.hpp" +#include "ai_waypoint.hpp" +#include "../../company_func.h" +#include "../../vehicle_base.h" +#include "../../waypoint.h" + +AIWaypointList::AIWaypointList() +{ + const Waypoint *wp; + FOR_ALL_WAYPOINTS(wp) { + if (wp->owner == _current_company) this->AddItem(wp->index); + } +} + +AIWaypointList_Vehicle::AIWaypointList_Vehicle(VehicleID vehicle_id) +{ + if (!AIVehicle::IsValidVehicle(vehicle_id)) return; + + const Vehicle *v = ::GetVehicle(vehicle_id); + + for (const Order *o = v->GetFirstOrder(); o != NULL; o = o->next) { + if (o->IsType(OT_GOTO_WAYPOINT)) this->AddItem(o->GetDestination()); + } +} diff --git a/src/ai/api/ai_waypointlist.hpp b/src/ai/api/ai_waypointlist.hpp new file mode 100644 index 000000000..7b67f5382 --- /dev/null +++ b/src/ai/api/ai_waypointlist.hpp @@ -0,0 +1,35 @@ +/* $Id$ */ + +/** @file ai_waypointlist.hpp List all the waypoints (you own). */ + +#ifndef AI_WAYPOINTLIST_HPP +#define AI_WAYPOINTLIST_HPP + +#include "ai_abstractlist.hpp" + +/** + * Creates a list of waypoints of which you are the owner. + * @ingroup AIList + */ +class AIWaypointList : public AIAbstractList { +public: + static const char *GetClassName() { return "AIWaypointList"; } + + AIWaypointList(); +}; + +/** + * Creates a list of waypoints which the vehicle has in its orders. + * @ingroup AIList + */ +class AIWaypointList_Vehicle : public AIAbstractList { +public: + static const char *GetClassName() { return "AIWaypointList_Vehicle"; } + + /** + * @param vehicle_id The vehicle to get the list of waypoints he has in its orders from. + */ + AIWaypointList_Vehicle(VehicleID vehicle_id); +}; + +#endif /* AI_WAYPOINTLIST_HPP */ diff --git a/src/ai/api/ai_waypointlist.hpp.sq b/src/ai/api/ai_waypointlist.hpp.sq new file mode 100644 index 000000000..f9ea80c65 --- /dev/null +++ b/src/ai/api/ai_waypointlist.hpp.sq @@ -0,0 +1,38 @@ +/* $Id$ */ +/* THIS FILE IS AUTO-GENERATED; PLEASE DO NOT ALTER MANUALLY */ + +#include "ai_waypointlist.hpp" + +namespace SQConvert { + /* Allow AIWaypointList to be used as Squirrel parameter */ + template <> AIWaypointList *GetParam(ForceType<AIWaypointList *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIWaypointList *)instance; } + template <> AIWaypointList &GetParam(ForceType<AIWaypointList &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIWaypointList *)instance; } + template <> const AIWaypointList *GetParam(ForceType<const AIWaypointList *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIWaypointList *)instance; } + template <> const AIWaypointList &GetParam(ForceType<const AIWaypointList &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIWaypointList *)instance; } + template <> int Return<AIWaypointList *>(HSQUIRRELVM vm, AIWaypointList *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AIWaypointList", res, NULL, DefSQDestructorCallback<AIWaypointList>); return 1; } +}; // namespace SQConvert + +void SQAIWaypointList_Register(Squirrel *engine) { + DefSQClass <AIWaypointList> SQAIWaypointList("AIWaypointList"); + SQAIWaypointList.PreRegister(engine, "AIAbstractList"); + SQAIWaypointList.AddConstructor<void (AIWaypointList::*)(), 1>(engine, "x"); + + SQAIWaypointList.PostRegister(engine); +} + +namespace SQConvert { + /* Allow AIWaypointList_Vehicle to be used as Squirrel parameter */ + template <> AIWaypointList_Vehicle *GetParam(ForceType<AIWaypointList_Vehicle *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIWaypointList_Vehicle *)instance; } + template <> AIWaypointList_Vehicle &GetParam(ForceType<AIWaypointList_Vehicle &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIWaypointList_Vehicle *)instance; } + template <> const AIWaypointList_Vehicle *GetParam(ForceType<const AIWaypointList_Vehicle *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIWaypointList_Vehicle *)instance; } + template <> const AIWaypointList_Vehicle &GetParam(ForceType<const AIWaypointList_Vehicle &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIWaypointList_Vehicle *)instance; } + template <> int Return<AIWaypointList_Vehicle *>(HSQUIRRELVM vm, AIWaypointList_Vehicle *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AIWaypointList_Vehicle", res, NULL, DefSQDestructorCallback<AIWaypointList_Vehicle>); return 1; } +}; // namespace SQConvert + +void SQAIWaypointList_Vehicle_Register(Squirrel *engine) { + DefSQClass <AIWaypointList_Vehicle> SQAIWaypointList_Vehicle("AIWaypointList_Vehicle"); + SQAIWaypointList_Vehicle.PreRegister(engine, "AIAbstractList"); + SQAIWaypointList_Vehicle.AddConstructor<void (AIWaypointList_Vehicle::*)(VehicleID vehicle_id), 2>(engine, "xi"); + + SQAIWaypointList_Vehicle.PostRegister(engine); +} |