(svn r9361) [NoAI] -Add: some function to find, build and sell road vehicles.
--- a/projects/openttd.vcproj Tue Mar 20 00:14:29 2007 +0000
+++ b/projects/openttd.vcproj Tue Mar 20 00:19:26 2007 +0000
@@ -1062,6 +1062,9 @@
<File
RelativePath=".\..\src\ai\api\ai_transactionmode.hpp">
</File>
+ <File
+ RelativePath=".\..\src\ai\api\ai_vehicle.hpp">
+ </File>
</Filter>
<Filter
Name="AI API Implementation"
@@ -1108,6 +1111,9 @@
<File
RelativePath=".\..\src\ai\api\ai_transactionmode.cpp">
</File>
+ <File
+ RelativePath=".\..\src\ai\api\ai_vehicle.cpp">
+ </File>
</Filter>
<Filter
Name="NewGRF"
--- a/projects/openttd_vs80.vcproj Tue Mar 20 00:14:29 2007 +0000
+++ b/projects/openttd_vs80.vcproj Tue Mar 20 00:19:26 2007 +0000
@@ -1635,6 +1635,10 @@
RelativePath=".\..\src\ai\api\ai_transactionmode.hpp"
>
</File>
+ <File
+ RelativePath=".\..\src\ai\api\ai_vehicle.hpp"
+ >
+ </File>
</Filter>
<Filter
Name="AI API Implementation"
@@ -1695,6 +1699,10 @@
RelativePath=".\..\src\ai\api\ai_transactionmode.cpp"
>
</File>
+ <File
+ RelativePath=".\..\src\ai\api\ai_vehicle.cpp"
+ >
+ </File>
</Filter>
<Filter
Name="NewGRF"
--- a/source.list Tue Mar 20 00:14:29 2007 +0000
+++ b/source.list Tue Mar 20 00:19:26 2007 +0000
@@ -326,6 +326,7 @@
ai/api/ai_town.hpp
ai/api/ai_testmode.hpp
ai/api/ai_transactionmode.hpp
+ai/api/ai_vehicle.hpp
# AI API Implementation
ai/api/ai_accounting.cpp
@@ -342,6 +343,7 @@
ai/api/ai_town.cpp
ai/api/ai_testmode.cpp
ai/api/ai_transactionmode.cpp
+ai/api/ai_vehicle.cpp
# NewGRF
newgrf.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_vehicle.cpp Tue Mar 20 00:19:26 2007 +0000
@@ -0,0 +1,114 @@
+/* $Id$ */
+
+/** @file ai_vehicle.cpp handles the functions of the AIVehicle class */
+
+#include "ai_vehicle.hpp"
+#include "ai_cargo.hpp"
+#include "../../command.h"
+#include "../../depot.h"
+#include "../../engine.h"
+#include "../../player.h"
+#include "../../vehicle.h"
+
+bool AIVehicle::IsValidEngine(EngineID engine_id)
+{
+ return ::IsEngineIndex(engine_id); // TODO: implement 'can I build this engine check'
+}
+
+bool AIVehicle::IsValidVehicle(VehicleID vehicle_id)
+{
+ return ::IsValidVehicleID(vehicle_id) && ::GetVehicle(vehicle_id)->owner == _current_player;
+}
+
+EngineID AIVehicle::FindBestRoadVehicle(CargoID cargo, uint8 min_reliability)
+{
+ if (!AICargo::IsValidCargo(cargo) || min_reliability > 100) return INVALID_ENGINE;
+
+ EngineID best_engine = INVALID_ENGINE;
+ EngineID engine_id;
+
+ FOR_ALL_ENGINEIDS_OF_TYPE(engine_id, VEH_ROAD) {
+ /* Is the vehicle available for this player */
+ if (IsEngineBuildable(engine_id, VEH_ROAD, _current_player) &&
+ GetEngine(engine_id)->reliability * 100 >= min_reliability << 16 &&
+ (RoadVehInfo(engine_id)->cargo_type == cargo || CanRefitTo(engine_id, cargo))) {
+ best_engine = engine_id;
+ }
+ }
+
+ return INVALID_ENGINE;
+}
+
+VehicleID AIVehicle::BuildVehicle(TileIndex depot, EngineID engine_id)
+{
+ if (!this->IsValidEngine(engine_id)) return false;
+
+ bool ret;
+ switch (::GetEngine(engine_id)->type) {
+ case VEH_ROAD: ret = this->DoCommand(depot, engine_id, 0, CMD_BUILD_ROAD_VEH); break;
+ default: NOT_REACHED(); return INVALID_VEHICLE; // TODO: implement trains, ships and aircraft
+ }
+
+ return ret ? _new_vehicle_id : INVALID_VEHICLE;
+}
+
+VehicleID AIVehicle::CloneVehicle(TileIndex depot, VehicleID vehicle_id, bool share_orders)
+{
+ if (!this->IsValidVehicle(vehicle_id)) return false;
+
+ bool ret;
+ switch (::GetVehicle(vehicle_id)->type) {
+ case VEH_ROAD: ret = this->DoCommand(depot, vehicle_id, share_orders, CMD_CLONE_VEHICLE); break;
+ default: return INVALID_VEHICLE; // TODO: implement trains, ships and aircraft
+ }
+
+ return ret ? _new_vehicle_id : INVALID_VEHICLE;
+}
+
+bool AIVehicle::RefitVehicle(VehicleID vehicle_id, CargoID cargo)
+{
+ if (!this->IsValidVehicle(vehicle_id) || !AICargo::IsValidCargo(cargo)) return false;
+
+ switch (::GetVehicle(vehicle_id)->type) {
+ case VEH_ROAD: return this->DoCommand(0, vehicle_id, cargo, CMD_REFIT_ROAD_VEH);
+ default: return false; // TODO: implement trains, ships and aircraft
+ }
+}
+
+
+bool AIVehicle::SellVehicle(VehicleID vehicle_id)
+{
+ if (!this->IsValidVehicle(vehicle_id)) return false;
+
+ switch (::GetVehicle(vehicle_id)->type) {
+ case VEH_ROAD: return this->DoCommand(0, vehicle_id, 0, CMD_SELL_ROAD_VEH);
+ default: return false; // TODO: implement trains, ships and aircraft
+ }
+}
+
+bool AIVehicle::SendVehicleToDepot(VehicleID vehicle_id)
+{
+ if (!this->IsValidVehicle(vehicle_id)) return false;
+
+ switch (::GetVehicle(vehicle_id)->type) {
+ case VEH_ROAD: return this->DoCommand(0, vehicle_id, 0, CMD_SEND_ROADVEH_TO_DEPOT);
+ default: return false; // TODO: implement trains, ships and aircraft
+ }
+}
+
+bool AIVehicle::StartStopVehicle(VehicleID vehicle_id)
+{
+ if (!this->IsValidVehicle(vehicle_id)) return false;
+
+ switch (::GetVehicle(vehicle_id)->type) {
+ case VEH_ROAD: return this->DoCommand(0, vehicle_id, 0, CMD_START_STOP_ROADVEH);
+ default: return false; // TODO: implement trains, ships and aircraft
+ }
+}
+
+bool AIVehicle::SkipVehicleOrder(VehicleID vehicle_id)
+{
+ if (!this->IsValidVehicle(vehicle_id)) return false;
+
+ return this->DoCommand(0, vehicle_id, 0, CMD_SKIP_ORDER);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_vehicle.hpp Tue Mar 20 00:19:26 2007 +0000
@@ -0,0 +1,135 @@
+/* $Id$ */
+
+/** @file ai_vehicle.hpp Everything to query and build vehicles */
+
+#ifndef AI_VEHICLE_HPP
+#define AI_VEHICLE_HPP
+
+#include "ai_object.hpp"
+
+/**
+ * Class that handles all vehicle related functions.
+ */
+class AIVehicle : public AIObject {
+public:
+ /**
+ * Checks whether the given engine type is valid and buildable by you.
+ * @param engine_id the engine to check.
+ * @return true if and only if the engine type is valid.
+ */
+ bool IsValidEngine(EngineID engine_id);
+
+ /**
+ * Checks whether the given vehicle is valid and owned by you.
+ * @param vehicle_id the vehicle to check.
+ * @return true if and only if the vehicle is valid.
+ */
+ bool IsValidVehicle(VehicleID vehicle_id);
+
+ /**
+ * Find the best road vehicle for this job, given a minimum reliability.
+ * @param cargo the cargo the vehicle has to transport.
+ * @param min_reliability the minimum reliability of the vehicle,
+ * between 0 and 100.
+ * @pre the tile at depot has a depot that can build the engine and
+ * is owned by you.
+ * @pre AICargo::IsValidCargo(cargo).
+ * @pre min_reliability is between 0 and 100.
+ * @return the engine with the best characteristics, or INVALID_ENGINE
+ * when no engine was found given the cargo and reliability.
+ */
+ EngineID FindBestRoadVehicle(CargoID cargo, uint8 min_reliability);
+
+ /**
+ * Builds a vehicle with the given engine at the given depot.
+ * @param depot the depot where the vehicle will be build.
+ * @param engine_id the engine to use for this vehicle.
+ * @pre the tile at depot has a depot that can build the engine and
+ * is owned by you.
+ * @pre IsValidEngine(engine_id).
+ * @return true if and only if the vehicle has been build.
+ * @return the VehicleID of the new vehicle, or an invalid VehicleID when
+ * it failed. Check the return value using IsValidVehicle.
+ */
+ VehicleID BuildVehicle(TileIndex depot, EngineID vehicle_id);
+
+ /**
+ * Clones a vehicle at the given depot, copying or cloning it's orders.
+ * @param depot the depot where the vehicle will be build.
+ * @param vehicle_id the vehicle to use as example for the new vehicle.
+ * @param share_orders should the orders be copied or shared?
+ * @pre the tile at depot has a depot.
+ * @pre IsValidVehicle(vehicle_id).
+ * @return the VehicleID of the new vehicle, or an invalid VehicleID when
+ * it failed. Check the return value using IsValidVehicle.
+ */
+ VehicleID CloneVehicle(TileIndex depot, VehicleID vehicle_id, bool share_orders);
+
+ /**
+ * Refits a vehicle to the given cargo type
+ * @param vehicle_id the vehicle to refit
+ * @param cargo the cargo to refit to
+ * @pre IsValidVehicle(vehicle_id).
+ * @pre AICargo::IsValidCargo(cargo)
+ * @pre you must own the vehicle
+ * @pre the vehicle must be stopped in the depot
+ * @return true if and only if the refit succeeded.
+ */
+ bool RefitVehicle(VehicleID vehicle_id, CargoID cargo);
+
+ /**
+ * Sells the given vehicle.
+ * @param vehicle_id the vehicle to sell.
+ * @pre IsValidVehicle(vehicle_id).
+ * @pre you must own the vehicle
+ * @pre the vehicle must be stopped in the depot
+ * @return true if and only if the vehicle has been sold.
+ */
+ bool SellVehicle(VehicleID vehicle_id);
+
+
+ /**
+ * Sends the given vehicle to a depot.
+ * @param vehicle_id the vehicle to send to a depot.
+ * @pre IsValidVehicle(vehicle_id).
+ * @return true if and only if the vehicle has been sent to a depot.
+ */
+ bool SendVehicleToDepot(VehicleID vehicle_id);
+
+ /**
+ * Starts or stops the given vehicle depending on the current state.
+ * @param vehicle_id the vehicle to start/stop.
+ * @pre IsValidVehicle(vehicle_id).
+ * @return true if and only if the vehicle has been started or stopped.
+ */
+ bool StartStopVehicle(VehicleID vehicle_id);
+
+ /**
+ * Skips the current order of the given vehicle.
+ * @param vehicle_id the vehicle to skip the order for.
+ * @pre IsValidVehicle(vehicle_id).
+ * @return true if and only if the order has been skipped.
+ */
+ bool SkipVehicleOrder(VehicleID vehicle_id);
+};
+
+#ifdef DEFINE_SQUIRREL_CLASS
+void SQAIVehicleRegister(Squirrel *engine) {
+ DefSQClass <AIVehicle> SQAIVehicle("AIVehicle");
+ SQAIVehicle.PreRegister(engine);
+ SQAIVehicle.AddConstructor(engine);
+ SQAIVehicle.DefSQFunction(engine, &AIVehicle::IsValidEngine, "IsValidEngine");
+ SQAIVehicle.DefSQFunction(engine, &AIVehicle::IsValidVehicle, "IsValidVehicle");
+ SQAIVehicle.DefSQFunction(engine, &AIVehicle::FindBestRoadVehicle, "FindBestRoadVehicle");
+ SQAIVehicle.DefSQFunction(engine, &AIVehicle::BuildVehicle, "BuildVehicle");
+ SQAIVehicle.DefSQFunction(engine, &AIVehicle::CloneVehicle, "CloneVehicle");
+ SQAIVehicle.DefSQFunction(engine, &AIVehicle::RefitVehicle, "RefitVehicle");
+ SQAIVehicle.DefSQFunction(engine, &AIVehicle::SellVehicle, "SellVehicle");
+ SQAIVehicle.DefSQFunction(engine, &AIVehicle::SendVehicleToDepot, "SendVehicleToDepot");
+ SQAIVehicle.DefSQFunction(engine, &AIVehicle::StartStopVehicle, "StartStopVehicle");
+ SQAIVehicle.DefSQFunction(engine, &AIVehicle::SkipVehicleOrder, "SkipVehicleOrder");
+ SQAIVehicle.PostRegister(engine);
+}
+#endif /* SQUIRREL_CLASS */
+
+#endif /* AI_VEHICLE_HPP */