src/ai/api/ai_object.cpp
author rubidium
Sun, 03 Feb 2008 20:17:54 +0000
branchnoai
changeset 9724 b39bc69bb2f2
parent 9723 eee46cb39750
child 9748 4584f8cd00e8
permissions -rw-r--r--
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
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
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
    56
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
    57
{
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
    58
	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
    59
}
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
    60
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
    61
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
    62
{
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
    63
	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
    64
}
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
    65
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    66
void AIObject::SetNewSignID(SignID sign)
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    67
{
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    68
	AIObject::GetDoCommandStruct(_current_player)->new_sign_id = sign;
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    69
}
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    70
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    71
SignID AIObject::GetNewSignID()
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    72
{
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    73
	return AIObject::GetDoCommandStruct(_current_player)->new_sign_id;
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    74
}
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
    75
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
    76
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
    77
{
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
    78
	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
    79
}
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
    80
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    81
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
    82
{
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    83
	/* 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
    84
	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
    85
	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
    86
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
    87
	/* 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
    88
	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
    89
		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
    90
		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
    91
	}
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    92
9454
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
    93
	return &command_struct[player];
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
    94
}
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    95
9454
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
    96
void AIObject::ResetInternalPlayerData()
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
    97
{
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
    98
	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
    99
	command_struct->mode = NULL;
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
   100
	command_struct->delay = 1;
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
   101
	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
   102
	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
   103
	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
   104
}
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
   105
9695
708f1e3cc4c4 (svn r10936) [NoAI] -Fix: allow water_protection for non-water-building in DoCommand
truelight
parents: 9688
diff changeset
   106
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
   107
{
9486
a9b5f6b8667c (svn r9336) [NoAI] -Fix: remove 'flags' for AI DoCommand and detect its value automaticly
truelight
parents: 9473
diff changeset
   108
	uint32 flags = 0;
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   109
	PlayerID old_lp;
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
   110
	CommandCost res;
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   111
	const char* tmp_cmdtext;
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   112
9486
a9b5f6b8667c (svn r9336) [NoAI] -Fix: remove 'flags' for AI DoCommand and detect its value automaticly
truelight
parents: 9473
diff changeset
   113
	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
   114
	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
   115
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   116
	/* 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
   117
	tmp_cmdtext = _cmd_text;
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   118
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   119
	/* 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
   120
	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
   121
	/* 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
   122
	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
   123
9473
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
   124
	/* Restore _cmd_text */
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
   125
	_cmd_text = tmp_cmdtext;
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
   126
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
   127
	/* 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
   128
	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
   129
		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
   130
		return true;
4c5eedbc3ba9 (svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
truelight
parents: 9450
diff changeset
   131
	}
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   132
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   133
	/* 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
   134
	 * over the network */
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   135
	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
   136
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   137
#ifdef ENABLE_NETWORK
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   138
	/* Send the command */
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   139
	if (_networking) {
9406
3500f836dc21 (svn r9203) [NoAI] -Fix: in SP do not switch _local_player, only in MP
truelight
parents: 9370
diff changeset
   140
		/* 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
   141
		 * 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
   142
		old_lp = _local_player;
3500f836dc21 (svn r9203) [NoAI] -Fix: in SP do not switch _local_player, only in MP
truelight
parents: 9370
diff changeset
   143
		_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
   144
		::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
   145
		_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
   146
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
   147
		/* Suspend the AI till the command is really executed */
9508
e2ff0ef99c76 (svn r9404) [NoAI] -Fix: one more MSVC2005 (KUDr)
truelight
parents: 9496
diff changeset
   148
		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
   149
		/* Check if the callback still agrees with us, else return error */
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
   150
		if (!AI_GetCallbackResult(_current_player)) res = CMD_ERROR;
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   151
	} else {
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   152
#else
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   153
	{
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   154
#endif
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   155
		/* 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
   156
		::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
   157
		/* 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
   158
		AIObject::SetNewVehicleID(_new_vehicle_id);
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
   159
		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
   160
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
   161
		/* 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
   162
		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
   163
	}
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   164
9452
4c5eedbc3ba9 (svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
truelight
parents: 9450
diff changeset
   165
	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
   166
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
   167
	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
   168
	return true;
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   169
}