(svn r9368) [NoAI] -Fix: store _new_vehicle_id directly after successful handling the command in a per-AI-player-safe piece of memory, so we can restore the value when ever we want later in the process noai
authortruelight
Tue, 20 Mar 2007 11:42:07 +0000
branchnoai
changeset 9496 05ebee9884b3
parent 9495 29284ad4ae39
child 9497 f6678533ccba
(svn r9368) [NoAI] -Fix: store _new_vehicle_id directly after successful handling the command in a per-AI-player-safe piece of memory, so we can restore the value when ever we want later in the process
src/ai/ai_threads.cpp
src/ai/api/ai_object.cpp
src/ai/api/ai_object.hpp
src/ai/api/ai_vehicle.cpp
--- a/src/ai/ai_threads.cpp	Tue Mar 20 01:04:21 2007 +0000
+++ b/src/ai/ai_threads.cpp	Tue Mar 20 11:42:07 2007 +0000
@@ -11,6 +11,7 @@
 #include "ai.h"
 #include "ai_threads.h"
 #include "api/ai_controller.hpp"
+#include "api/ai_object.hpp"
 
 #include <pthread.h>
 
@@ -264,6 +265,10 @@
 {
 	/* Store if we were a success or not and resume */
 	_ai_state[_current_player].last_command_res = success;
+
+	/* Store some values inside the AIObject static memory */
+	AIObject::SetNewVehicleID(_new_vehicle_id);
+
 	_ai_state[_current_player].Resume();
 }
 
--- a/src/ai/api/ai_object.cpp	Tue Mar 20 01:04:21 2007 +0000
+++ b/src/ai/api/ai_object.cpp	Tue Mar 20 11:42:07 2007 +0000
@@ -50,6 +50,16 @@
 	return AIObject::GetDoCommandStruct(_current_player)->costs;
 }
 
+void AIObject::SetNewVehicleID(VehicleID vehicle)
+{
+	AIObject::GetDoCommandStruct(_current_player)->new_vehicle_id = vehicle;
+}
+
+VehicleID AIObject::GetNewVehicleID()
+{
+	return AIObject::GetDoCommandStruct(_current_player)->new_vehicle_id;
+}
+
 AIObject::AIDoCommandStruct *AIObject::GetDoCommandStruct(PlayerID player)
 {
 	/* Storage for data on per-AI level */
@@ -118,6 +128,9 @@
 #endif
 		/* For SinglePlayer we execute the command immediatly */
 		::DoCommandP(tile, p1, p2, NULL, procc);
+		/* Store some values inside the AIObject static memory */
+		AIObject::SetNewVehicleID(_new_vehicle_id);
+
 		/* Suspend the AI player for 1 tick, so it simulates MultiPlayer */
 		AI_SuspendPlayer(_current_player, AIObject::GetDoCommandDelay());
 	}
--- a/src/ai/api/ai_object.hpp	Tue Mar 20 01:04:21 2007 +0000
+++ b/src/ai/api/ai_object.hpp	Tue Mar 20 11:42:07 2007 +0000
@@ -26,6 +26,7 @@
 		AIObject *mode_instance;
 		uint delay;
 		int32 costs;
+		VehicleID new_vehicle_id;
 	};
 
 	/**
@@ -79,10 +80,22 @@
 	 */
 	static uint GetDoCommandDelay();
 
+	/**
+	 * Get the latest stored new_vehicle_id.
+	 */
+	static VehicleID GetNewVehicleID();
+
 public:
 	/**
+	 * Store a new_vehicle_id per player.
+	 * @note NEVER use this yourself in your AI!
+	 */
+	static void SetNewVehicleID(VehicleID vehicle);
+
+	/**
 	 * If an AI starts, some internals needs to be resetted. This function
 	 *   takes care of that.
+	 * @note NEVER use this yourself in your AI!
 	 */
 	static void ResetInternalPlayerData();
 };
--- a/src/ai/api/ai_vehicle.cpp	Tue Mar 20 01:04:21 2007 +0000
+++ b/src/ai/api/ai_vehicle.cpp	Tue Mar 20 11:42:07 2007 +0000
@@ -49,7 +49,7 @@
 		default: NOT_REACHED(); return INVALID_VEHICLE; // TODO: implement trains, ships and aircraft
 	}
 
-	return ret ? _new_vehicle_id : INVALID_VEHICLE;
+	return ret ? AIObject::GetNewVehicleID() : INVALID_VEHICLE;
 }
 
 VehicleID AIVehicle::CloneVehicle(TileIndex depot, VehicleID vehicle_id, bool share_orders)
@@ -62,7 +62,7 @@
 		default: return INVALID_VEHICLE; // TODO: implement trains, ships and aircraft
 	}
 
-	return ret ? _new_vehicle_id : INVALID_VEHICLE;
+	return ret ? AIObject::GetNewVehicleID() : INVALID_VEHICLE;
 }
 
 bool AIVehicle::RefitVehicle(VehicleID vehicle_id, CargoID cargo)