src/ai/api/ai_object.cpp
author truebrain
Wed, 26 Mar 2008 15:17:40 +0000
branchnoai
changeset 9823 0b7f816cf46f
parent 9760 265fdd2130c3
child 9833 89a64246458f
permissions -rw-r--r--
(svn r12431) [NoAI] -Add: added AIEventSubsidiaryOffer, which keeps you informed about new Subsidiaries
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
     1
/* $Id$ */
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
     2
9430
9e0a193b2bec (svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents: 9427
diff changeset
     3
/** @file ai_object.cpp handles the commands-related functions of the AIObject class */
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
     4
9430
9e0a193b2bec (svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents: 9427
diff changeset
     5
#include "ai_object.hpp"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9703
diff changeset
     6
#include "../../command_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9703
diff changeset
     7
#include "../../network/network.h"
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
     8
#include "../../player_func.h"
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
     9
#include "../../signs.h" // for _new_sign_id
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9703
diff changeset
    10
#include "../../vehicle_func.h"
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents: 9430
diff changeset
    11
#include "../ai.h"
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    12
#include "../ai_threads.h"
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
    13
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    14
void AIObject::SetDoCommandDelay(uint ticks)
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    15
{
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    16
	assert(ticks > 0);
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    17
	AIObject::GetDoCommandStruct(_current_player)->delay = ticks;
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    18
}
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    19
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    20
uint AIObject::GetDoCommandDelay()
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    21
{
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    22
	return AIObject::GetDoCommandStruct(_current_player)->delay;
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    23
}
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    24
9473
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
    25
void AIObject::SetDoCommandMode(AIModeProc *proc, AIObject *instance)
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    26
{
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    27
	AIObject::GetDoCommandStruct(_current_player)->mode = proc;
9473
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
    28
	AIObject::GetDoCommandStruct(_current_player)->mode_instance = instance;
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    29
}
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    30
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    31
AIModeProc *AIObject::GetDoCommandMode()
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    32
{
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    33
	return AIObject::GetDoCommandStruct(_current_player)->mode;
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    34
}
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    35
9473
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
    36
AIObject *AIObject::GetDoCommandModeInstance()
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
    37
{
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
    38
	return AIObject::GetDoCommandStruct(_current_player)->mode_instance;
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
    39
}
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
    40
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
    41
void AIObject::SetDoCommandCosts(Money value)
9414
a7f7bbec08da (svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents: 9406
diff changeset
    42
{
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
    43
	AIObject::GetDoCommandStruct(_current_player)->costs = CommandCost(value);
9414
a7f7bbec08da (svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents: 9406
diff changeset
    44
}
a7f7bbec08da (svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents: 9406
diff changeset
    45
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
    46
void AIObject::IncreaseDoCommandCosts(Money value)
9414
a7f7bbec08da (svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents: 9406
diff changeset
    47
{
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
    48
	AIObject::GetDoCommandStruct(_current_player)->costs.AddCost(value);
9452
4c5eedbc3ba9 (svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
truelight
parents: 9450
diff changeset
    49
}
4c5eedbc3ba9 (svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
truelight
parents: 9450
diff changeset
    50
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
    51
Money AIObject::GetDoCommandCosts()
9452
4c5eedbc3ba9 (svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
truelight
parents: 9450
diff changeset
    52
{
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
    53
	return AIObject::GetDoCommandStruct(_current_player)->costs.GetCost();
9414
a7f7bbec08da (svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents: 9406
diff changeset
    54
}
a7f7bbec08da (svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents: 9406
diff changeset
    55
9760
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9748
diff changeset
    56
void AIObject::SetLastCommandRes(bool res)
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9748
diff changeset
    57
{
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9748
diff changeset
    58
	AIObject::GetDoCommandStruct(_current_player)->last_command_res = res;
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9748
diff changeset
    59
}
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9748
diff changeset
    60
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9748
diff changeset
    61
bool AIObject::GetLastCommandRes()
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9748
diff changeset
    62
{
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9748
diff changeset
    63
	return AIObject::GetDoCommandStruct(_current_player)->last_command_res;
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9748
diff changeset
    64
}
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9748
diff changeset
    65
9496
05ebee9884b3 (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
truelight
parents: 9486
diff changeset
    66
void AIObject::SetNewVehicleID(VehicleID vehicle)
05ebee9884b3 (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
truelight
parents: 9486
diff changeset
    67
{
05ebee9884b3 (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
truelight
parents: 9486
diff changeset
    68
	AIObject::GetDoCommandStruct(_current_player)->new_vehicle_id = vehicle;
05ebee9884b3 (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
truelight
parents: 9486
diff changeset
    69
}
05ebee9884b3 (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
truelight
parents: 9486
diff changeset
    70
05ebee9884b3 (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
truelight
parents: 9486
diff changeset
    71
VehicleID AIObject::GetNewVehicleID()
05ebee9884b3 (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
truelight
parents: 9486
diff changeset
    72
{
05ebee9884b3 (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
truelight
parents: 9486
diff changeset
    73
	return AIObject::GetDoCommandStruct(_current_player)->new_vehicle_id;
05ebee9884b3 (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
truelight
parents: 9486
diff changeset
    74
}
05ebee9884b3 (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
truelight
parents: 9486
diff changeset
    75
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    76
void AIObject::SetNewSignID(SignID sign)
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    77
{
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    78
	AIObject::GetDoCommandStruct(_current_player)->new_sign_id = sign;
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    79
}
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    80
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    81
SignID AIObject::GetNewSignID()
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    82
{
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    83
	return AIObject::GetDoCommandStruct(_current_player)->new_sign_id;
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    84
}
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    85
9682
d031eb183733 (svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents: 9680
diff changeset
    86
void *&AIObject::GetEventPointer()
9680
5ed7bbfd51c7 (svn r10629) [NoAI] -Fix: on returning a class instance which is NULL, do not make a wrapper SQ, but return a NULL pointer too
truelight
parents: 9629
diff changeset
    87
{
9682
d031eb183733 (svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents: 9680
diff changeset
    88
	return AIObject::GetDoCommandStruct(_current_player)->event_data;
9680
5ed7bbfd51c7 (svn r10629) [NoAI] -Fix: on returning a class instance which is NULL, do not make a wrapper SQ, but return a NULL pointer too
truelight
parents: 9629
diff changeset
    89
}
5ed7bbfd51c7 (svn r10629) [NoAI] -Fix: on returning a class instance which is NULL, do not make a wrapper SQ, but return a NULL pointer too
truelight
parents: 9629
diff changeset
    90
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    91
AIObject::AIDoCommandStruct *AIObject::GetDoCommandStruct(PlayerID player)
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    92
{
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    93
	/* Storage for data on per-AI level */
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    94
	static AIObject::AIDoCommandStruct command_struct[MAX_PLAYERS];
9682
d031eb183733 (svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents: 9680
diff changeset
    95
	static bool initialized = false;
d031eb183733 (svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents: 9680
diff changeset
    96
d031eb183733 (svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents: 9680
diff changeset
    97
	/* Make sure all memory is NULL when we start */
d031eb183733 (svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents: 9680
diff changeset
    98
	if (!initialized) {
d031eb183733 (svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents: 9680
diff changeset
    99
		initialized = true;
d031eb183733 (svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents: 9680
diff changeset
   100
		memset(&command_struct, 0, sizeof(command_struct));
d031eb183733 (svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents: 9680
diff changeset
   101
	}
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
   102
9454
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
   103
	return &command_struct[player];
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
   104
}
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
   105
9454
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
   106
void AIObject::ResetInternalPlayerData()
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
   107
{
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
   108
	AIObject::AIDoCommandStruct *command_struct = GetDoCommandStruct(_current_player);
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
   109
	command_struct->mode = NULL;
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
   110
	command_struct->delay = 1;
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
   111
	command_struct->costs = CommandCost();
9682
d031eb183733 (svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents: 9680
diff changeset
   112
	if (command_struct->event_data != NULL) AIEventController::FreeEventPointer();
d031eb183733 (svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents: 9680
diff changeset
   113
	command_struct->event_data = NULL;
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
   114
}
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
   115
9695
708f1e3cc4c4 (svn r10936) [NoAI] -Fix: allow water_protection for non-water-building in DoCommand
truelight
parents: 9688
diff changeset
   116
bool AIObject::DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint procc, bool water_protection)
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   117
{
9486
a9b5f6b8667c (svn r9336) [NoAI] -Fix: remove 'flags' for AI DoCommand and detect its value automaticly
truelight
parents: 9473
diff changeset
   118
	uint32 flags = 0;
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   119
	PlayerID old_lp;
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
   120
	CommandCost res;
9748
4584f8cd00e8 (svn r12228) [NoAI] -Fix: very minor coding style error (VERY minor)
truebrain
parents: 9724
diff changeset
   121
	const char *tmp_cmdtext;
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   122
9486
a9b5f6b8667c (svn r9336) [NoAI] -Fix: remove 'flags' for AI DoCommand and detect its value automaticly
truelight
parents: 9473
diff changeset
   123
	if (procc != CMD_LANDSCAPE_CLEAR) flags |= DC_AUTO;
9695
708f1e3cc4c4 (svn r10936) [NoAI] -Fix: allow water_protection for non-water-building in DoCommand
truelight
parents: 9688
diff changeset
   124
	if (water_protection) flags |= DC_NO_WATER;
9486
a9b5f6b8667c (svn r9336) [NoAI] -Fix: remove 'flags' for AI DoCommand and detect its value automaticly
truelight
parents: 9473
diff changeset
   125
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   126
	/* The test already resets _cmd_text, so backup the pointer */
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   127
	tmp_cmdtext = _cmd_text;
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   128
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   129
	/* First, do a test-run to see if we can do this */
9486
a9b5f6b8667c (svn r9336) [NoAI] -Fix: remove 'flags' for AI DoCommand and detect its value automaticly
truelight
parents: 9473
diff changeset
   130
	res = ::DoCommand(tile, p1, p2, flags, procc);
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
   131
	/* The command failed, so return */
9452
4c5eedbc3ba9 (svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
truelight
parents: 9450
diff changeset
   132
	if (::CmdFailed(res)) return false;
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
   133
9473
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
   134
	/* Restore _cmd_text */
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
   135
	_cmd_text = tmp_cmdtext;
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
   136
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
   137
	/* Check what the callback wants us to do */
9486
a9b5f6b8667c (svn r9336) [NoAI] -Fix: remove 'flags' for AI DoCommand and detect its value automaticly
truelight
parents: 9473
diff changeset
   138
	if (AIObject::GetDoCommandMode() != NULL && !AIObject::GetDoCommandMode()(tile, p1, p2, procc, res)) {
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
   139
		AIObject::IncreaseDoCommandCosts(res.GetCost());
9452
4c5eedbc3ba9 (svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
truelight
parents: 9450
diff changeset
   140
		return true;
4c5eedbc3ba9 (svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
truelight
parents: 9450
diff changeset
   141
	}
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   142
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   143
	/* If we did a DC_EXEC, and the command did not return an error, execute it
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   144
	 * over the network */
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   145
	if (flags & DC_NO_WATER) procc |= CMD_NO_WATER;
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   146
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   147
#ifdef ENABLE_NETWORK
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   148
	/* Send the command */
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   149
	if (_networking) {
9406
3500f836dc21 (svn r9203) [NoAI] -Fix: in SP do not switch _local_player, only in MP
truelight
parents: 9370
diff changeset
   150
		/* NetworkSend_Command needs _local_player to be set correctly, so
3500f836dc21 (svn r9203) [NoAI] -Fix: in SP do not switch _local_player, only in MP
truelight
parents: 9370
diff changeset
   151
		 * adjust it, and put it back right after the function */
3500f836dc21 (svn r9203) [NoAI] -Fix: in SP do not switch _local_player, only in MP
truelight
parents: 9370
diff changeset
   152
		old_lp = _local_player;
3500f836dc21 (svn r9203) [NoAI] -Fix: in SP do not switch _local_player, only in MP
truelight
parents: 9370
diff changeset
   153
		_local_player = _current_player;
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents: 9430
diff changeset
   154
		::NetworkSend_Command(tile, p1, p2, procc, CcAI);
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents: 9430
diff changeset
   155
		_local_player = old_lp;
9406
3500f836dc21 (svn r9203) [NoAI] -Fix: in SP do not switch _local_player, only in MP
truelight
parents: 9370
diff changeset
   156
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents: 9430
diff changeset
   157
		/* Suspend the AI till the command is really executed */
9508
e2ff0ef99c76 (svn r9404) [NoAI] -Fix: one more MSVC2005 (KUDr)
truelight
parents: 9496
diff changeset
   158
		AI_SuspendPlayer(_current_player, -(int)AIObject::GetDoCommandDelay());
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents: 9430
diff changeset
   159
		/* Check if the callback still agrees with us, else return error */
9760
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9748
diff changeset
   160
		if (!AIObject::GetLastCommandRes()) res = CMD_ERROR;
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   161
	} else {
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   162
#else
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   163
	{
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   164
#endif
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   165
		/* For SinglePlayer we execute the command immediatly */
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents: 9430
diff changeset
   166
		::DoCommandP(tile, p1, p2, NULL, procc);
9496
05ebee9884b3 (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
truelight
parents: 9486
diff changeset
   167
		/* Store some values inside the AIObject static memory */
05ebee9884b3 (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
truelight
parents: 9486
diff changeset
   168
		AIObject::SetNewVehicleID(_new_vehicle_id);
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
   169
		AIObject::SetNewSignID(_new_sign_id);
9496
05ebee9884b3 (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
truelight
parents: 9486
diff changeset
   170
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents: 9430
diff changeset
   171
		/* Suspend the AI player for 1 tick, so it simulates MultiPlayer */
9473
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
   172
		AI_SuspendPlayer(_current_player, AIObject::GetDoCommandDelay());
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   173
	}
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   174
9452
4c5eedbc3ba9 (svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
truelight
parents: 9450
diff changeset
   175
	if (::CmdFailed(res)) return false;
4c5eedbc3ba9 (svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
truelight
parents: 9450
diff changeset
   176
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
   177
	AIObject::IncreaseDoCommandCosts(res.GetCost());
9452
4c5eedbc3ba9 (svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
truelight
parents: 9450
diff changeset
   178
	return true;
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   179
}