(svn r15519) -Add [NoAI]: support for waypoints by adding AIRail::Build/Remove RailWaypoint, AIWaypoint and AIWaypointList.
parent
c2d9c85c0f
commit
c0e7ddeb9e
@ -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;
|
||||
}
|
@ -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 */
|
@ -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);
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
@ -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 */
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue