(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include "../../map.h"
|
||||
#include "../../road_map.h"
|
||||
#include "../../tile.h"
|
||||
#include "../../vehicle.h"
|
||||
#include "../../command.h"
|
||||
#include "trolly.h"
|
||||
#include "../../engine.h"
|
||||
@@ -249,6 +250,20 @@ EngineID AiNew_PickVehicle(Player *p)
|
||||
}
|
||||
|
||||
|
||||
void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2)
|
||||
{
|
||||
Player* p = GetPlayer(_current_player);
|
||||
|
||||
if (success) {
|
||||
p->ainew.state = AI_STATE_GIVE_ORDERS;
|
||||
p->ainew.veh_id = _new_vehicle_id;
|
||||
} else {
|
||||
/* XXX this should be handled more gracefully */
|
||||
p->ainew.state = AI_STATE_NOTHING;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Builds the best vehicle possible
|
||||
int AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag)
|
||||
{
|
||||
@@ -257,7 +272,11 @@ int AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag)
|
||||
if (i == INVALID_ENGINE) return CMD_ERROR;
|
||||
if (p->ainew.tbt == AI_TRAIN) return CMD_ERROR;
|
||||
|
||||
return AI_DoCommand(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
|
||||
if (flag & DC_EXEC) {
|
||||
return AI_DoCommandCc(tile, i, 0, flag, CMD_BUILD_ROAD_VEH, CcAI);
|
||||
} else {
|
||||
return AI_DoCommand(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
|
||||
}
|
||||
}
|
||||
|
||||
int AiNew_Build_Depot(Player* p, TileIndex tile, DiagDirection direction, byte flag)
|
||||
|
||||
@@ -1155,7 +1155,7 @@ static void AiNew_State_BuildVehicle(Player *p)
|
||||
// Decrease the total counter
|
||||
p->ainew.amount_veh--;
|
||||
// Go give some orders!
|
||||
p->ainew.state = AI_STATE_GIVE_ORDERS;
|
||||
p->ainew.state = AI_STATE_WAIT_FOR_BUILD;
|
||||
}
|
||||
|
||||
|
||||
@@ -1167,18 +1167,6 @@ static void AiNew_State_GiveOrders(Player *p)
|
||||
|
||||
assert(p->ainew.state == AI_STATE_GIVE_ORDERS);
|
||||
|
||||
// Get the new ID
|
||||
/* XXX -- Because this AI isn't using any event-system, this is VERY dangerous!
|
||||
* There is no way telling if the vehicle is already bought (or delayed by the
|
||||
* network), and if bought, if not an other vehicle is bought in between.. in
|
||||
* other words, there is absolutely no way knowing if this id is the true
|
||||
* id.. soon this will all change, but for now, we needed something to test
|
||||
* on ;) -- TrueLight -- 21-11-2005 */
|
||||
if (p->ainew.tbt == AI_TRAIN) {
|
||||
} else {
|
||||
p->ainew.veh_id = _new_roadveh_id;
|
||||
}
|
||||
|
||||
if (p->ainew.veh_main_id != INVALID_VEHICLE) {
|
||||
AI_DoCommand(0, p->ainew.veh_id + (p->ainew.veh_main_id << 16), 0, DC_EXEC, CMD_CLONE_ORDER);
|
||||
|
||||
@@ -1323,6 +1311,7 @@ static AiNew_StateFunction* const _ainew_state[] = {
|
||||
AiNew_State_BuildPath,
|
||||
AiNew_State_BuildDepot,
|
||||
AiNew_State_BuildVehicle,
|
||||
NULL,
|
||||
AiNew_State_GiveOrders,
|
||||
AiNew_State_StartVehicle,
|
||||
AiNew_State_RepayMoney,
|
||||
|
||||
@@ -187,6 +187,7 @@ enum {
|
||||
AI_STATE_BUILD_PATH,
|
||||
AI_STATE_BUILD_DEPOT,
|
||||
AI_STATE_BUILD_VEHICLE,
|
||||
AI_STATE_WAIT_FOR_BUILD,
|
||||
AI_STATE_GIVE_ORDERS,
|
||||
AI_STATE_START_VEHICLE,
|
||||
AI_STATE_REPAY_MONEY,
|
||||
|
||||
Reference in New Issue
Block a user