src/ai/api/ai_object.cpp
author truebrain
Tue, 29 Apr 2008 21:34:17 +0000
branchnoai
changeset 10383 bda80b3e9c25
parent 10339 ce6cd68d9eb8
child 10668 495789401303
permissions -rw-r--r--
(svn r12925) [NoAI] -Add: added AIIndustry::GetAmountOfStationsAround() (Yexo)
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
9833
89a64246458f (svn r12496) [NoAI] -Documentation: give the .cpp files a nice uniform format too
truebrain
parents: 9760
diff changeset
     3
/** @file ai_object.cpp Implementation of AIObject. */
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"
9851
a5f5a7cf2b61 (svn r12519) [NoAI] -Add: added AILog with Info(), Warning(), and Error()
truebrain
parents: 9847
diff changeset
     6
#include "ai_log.hpp"
9863
73647fe2e301 (svn r12552) [NoAI] -Change: do not force the use of StringIDs when setting errors.
rubidium
parents: 9862
diff changeset
     7
#include "ai_error.hpp"
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9841
diff changeset
     8
#include "table/strings.h"
10339
ce6cd68d9eb8 (svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
truebrain
parents: 9864
diff changeset
     9
#include "../../openttd.h"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9703
diff changeset
    10
#include "../../command_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9703
diff changeset
    11
#include "../../network/network.h"
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    12
#include "../../player_func.h"
9837
c9ec4f82e0d0 (svn r12503) [NoAI] -Sync: with trunk r12461:12501.
rubidium
parents: 9833
diff changeset
    13
#include "../../signs_func.h"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9703
diff changeset
    14
#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
    15
#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
    16
#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
    17
10339
ce6cd68d9eb8 (svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
truebrain
parents: 9864
diff changeset
    18
struct AIObject::AIDoCommandStruct {
ce6cd68d9eb8 (svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
truebrain
parents: 9864
diff changeset
    19
	AIModeProc *mode;
ce6cd68d9eb8 (svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
truebrain
parents: 9864
diff changeset
    20
	AIObject *mode_instance;
ce6cd68d9eb8 (svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
truebrain
parents: 9864
diff changeset
    21
	uint delay;
ce6cd68d9eb8 (svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
truebrain
parents: 9864
diff changeset
    22
	CommandCost costs;
ce6cd68d9eb8 (svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
truebrain
parents: 9864
diff changeset
    23
	uint last_error;
ce6cd68d9eb8 (svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
truebrain
parents: 9864
diff changeset
    24
	bool last_command_res;
ce6cd68d9eb8 (svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
truebrain
parents: 9864
diff changeset
    25
	VehicleID new_vehicle_id;
ce6cd68d9eb8 (svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
truebrain
parents: 9864
diff changeset
    26
	SignID new_sign_id;
ce6cd68d9eb8 (svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
truebrain
parents: 9864
diff changeset
    27
	void *event_data;
ce6cd68d9eb8 (svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
truebrain
parents: 9864
diff changeset
    28
	void *log_data;
ce6cd68d9eb8 (svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
truebrain
parents: 9864
diff changeset
    29
};
ce6cd68d9eb8 (svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
truebrain
parents: 9864
diff changeset
    30
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    31
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
    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
	assert(ticks > 0);
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
    34
	GetDoCommandStruct(_current_player)->delay = ticks;
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    35
}
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    36
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    37
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
    38
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
    39
	return GetDoCommandStruct(_current_player)->delay;
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    40
}
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    41
9473
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
    42
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
    43
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
    44
	GetDoCommandStruct(_current_player)->mode = proc;
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
    45
	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
    46
}
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    47
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    48
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
    49
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
    50
	return GetDoCommandStruct(_current_player)->mode;
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    51
}
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
    52
9473
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
    53
AIObject *AIObject::GetDoCommandModeInstance()
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
    54
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
    55
	return GetDoCommandStruct(_current_player)->mode_instance;
9473
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
    56
}
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
    57
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
    58
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
    59
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
    60
	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
    61
}
a7f7bbec08da (svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents: 9406
diff changeset
    62
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
    63
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
    64
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
    65
	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
    66
}
4c5eedbc3ba9 (svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
truelight
parents: 9450
diff changeset
    67
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
    68
Money AIObject::GetDoCommandCosts()
9452
4c5eedbc3ba9 (svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
truelight
parents: 9450
diff changeset
    69
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
    70
	return 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
    71
}
a7f7bbec08da (svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents: 9406
diff changeset
    72
9864
4a9b7b610b13 (svn r12554) [NoAI] -Codechange: add more typing information.
rubidium
parents: 9863
diff changeset
    73
void AIObject::SetLastError(AIErrorType last_error)
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9841
diff changeset
    74
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
    75
	GetDoCommandStruct(_current_player)->last_error = last_error;
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9841
diff changeset
    76
}
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9841
diff changeset
    77
9864
4a9b7b610b13 (svn r12554) [NoAI] -Codechange: add more typing information.
rubidium
parents: 9863
diff changeset
    78
AIErrorType AIObject::GetLastError()
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9841
diff changeset
    79
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
    80
	return GetDoCommandStruct(_current_player)->last_error;
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9841
diff changeset
    81
}
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9841
diff changeset
    82
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
    83
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
    84
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
    85
	GetDoCommandStruct(_current_player)->last_command_res = res;
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
    86
}
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
    87
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
    88
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
    89
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
    90
	return GetDoCommandStruct(_current_player)->last_command_res;
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
    91
}
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
    92
9841
f931a10242c9 (svn r12507) [NoAI] -Fix: enable Doxyfile warning about missing params and fix 3 missing params in AIObject
truebrain
parents: 9837
diff changeset
    93
void AIObject::SetNewVehicleID(VehicleID vehicle_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
    94
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
    95
	GetDoCommandStruct(_current_player)->new_vehicle_id = vehicle_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
    96
}
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
    97
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
    98
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
    99
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
   100
	return GetDoCommandStruct(_current_player)->new_vehicle_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
   101
}
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
   102
9841
f931a10242c9 (svn r12507) [NoAI] -Fix: enable Doxyfile warning about missing params and fix 3 missing params in AIObject
truebrain
parents: 9837
diff changeset
   103
void AIObject::SetNewSignID(SignID sign_id)
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
   104
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
   105
	GetDoCommandStruct(_current_player)->new_sign_id = sign_id;
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
   106
}
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
   107
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
   108
SignID AIObject::GetNewSignID()
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
   109
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
   110
	return GetDoCommandStruct(_current_player)->new_sign_id;
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
   111
}
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents: 9508
diff changeset
   112
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
   113
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
   114
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
   115
	return 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
   116
}
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
   117
9851
a5f5a7cf2b61 (svn r12519) [NoAI] -Add: added AILog with Info(), Warning(), and Error()
truebrain
parents: 9847
diff changeset
   118
void *&AIObject::GetLogPointer()
a5f5a7cf2b61 (svn r12519) [NoAI] -Add: added AILog with Info(), Warning(), and Error()
truebrain
parents: 9847
diff changeset
   119
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
   120
	return GetDoCommandStruct(_current_player)->log_data;
9851
a5f5a7cf2b61 (svn r12519) [NoAI] -Add: added AILog with Info(), Warning(), and Error()
truebrain
parents: 9847
diff changeset
   121
}
a5f5a7cf2b61 (svn r12519) [NoAI] -Add: added AILog with Info(), Warning(), and Error()
truebrain
parents: 9847
diff changeset
   122
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
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
   124
{
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
   125
	/* Storage for data on per-AI level */
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
   126
	static 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
   127
	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
   128
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
   129
	/* 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
   130
	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
   131
		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
   132
		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
   133
	}
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
   134
9454
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
   135
	return &command_struct[player];
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
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
9454
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
   138
void AIObject::ResetInternalPlayerData()
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
   139
{
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
   140
	AIDoCommandStruct *command_struct = GetDoCommandStruct(_current_player);
9454
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
   141
	command_struct->mode = NULL;
ee6a65b37b82 (svn r9284) [NoAI] -Fix: reset internal static AIObject data per AI on AI startup
truelight
parents: 9452
diff changeset
   142
	command_struct->delay = 1;
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
   143
	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
   144
	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
   145
	command_struct->event_data = NULL;
9851
a5f5a7cf2b61 (svn r12519) [NoAI] -Add: added AILog with Info(), Warning(), and Error()
truebrain
parents: 9847
diff changeset
   146
	if (command_struct->log_data != NULL) AILog::FreeLogPointer();
a5f5a7cf2b61 (svn r12519) [NoAI] -Add: added AILog with Info(), Warning(), and Error()
truebrain
parents: 9847
diff changeset
   147
	command_struct->log_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
   148
}
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
   149
9695
708f1e3cc4c4 (svn r10936) [NoAI] -Fix: allow water_protection for non-water-building in DoCommand
truelight
parents: 9688
diff changeset
   150
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
   151
{
9486
a9b5f6b8667c (svn r9336) [NoAI] -Fix: remove 'flags' for AI DoCommand and detect its value automaticly
truelight
parents: 9473
diff changeset
   152
	uint32 flags = 0;
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   153
	PlayerID old_lp;
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9511
diff changeset
   154
	CommandCost res;
9748
4584f8cd00e8 (svn r12228) [NoAI] -Fix: very minor coding style error (VERY minor)
truebrain
parents: 9724
diff changeset
   155
	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
   156
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9841
diff changeset
   157
	/* Make sure the last error is reset, so we don't give faulty warnings */
9863
73647fe2e301 (svn r12552) [NoAI] -Change: do not force the use of StringIDs when setting errors.
rubidium
parents: 9862
diff changeset
   158
	SetLastError(AIError::ERR_NONE);
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9841
diff changeset
   159
9486
a9b5f6b8667c (svn r9336) [NoAI] -Fix: remove 'flags' for AI DoCommand and detect its value automaticly
truelight
parents: 9473
diff changeset
   160
	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
   161
	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
   162
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   163
	/* 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
   164
	tmp_cmdtext = _cmd_text;
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   165
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   166
	/* 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
   167
	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
   168
	/* The command failed, so return */
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9841
diff changeset
   169
	if (::CmdFailed(res)) {
9864
4a9b7b610b13 (svn r12554) [NoAI] -Codechange: add more typing information.
rubidium
parents: 9863
diff changeset
   170
		SetLastError(AIError::StringToError(_error_message));
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9841
diff changeset
   171
		return false;
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9841
diff changeset
   172
	}
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
   173
9473
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
   174
	/* Restore _cmd_text */
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
   175
	_cmd_text = tmp_cmdtext;
dcbcd1c4496d (svn r9318) [NoAI] -Add: added new param in ModeCallback: costs
truelight
parents: 9454
diff changeset
   176
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9444
diff changeset
   177
	/* Check what the callback wants us to do */
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
   178
	if (GetDoCommandMode() != NULL && !GetDoCommandMode()(tile, p1, p2, procc, res)) {
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
   179
		IncreaseDoCommandCosts(res.GetCost());
9452
4c5eedbc3ba9 (svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
truelight
parents: 9450
diff changeset
   180
		return true;
4c5eedbc3ba9 (svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
truelight
parents: 9450
diff changeset
   181
	}
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   182
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   183
	/* 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
   184
	 * over the network */
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   185
	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
   186
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   187
#ifdef ENABLE_NETWORK
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   188
	/* Send the command */
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   189
	if (_networking) {
9406
3500f836dc21 (svn r9203) [NoAI] -Fix: in SP do not switch _local_player, only in MP
truelight
parents: 9370
diff changeset
   190
		/* 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
   191
		 * 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
   192
		old_lp = _local_player;
3500f836dc21 (svn r9203) [NoAI] -Fix: in SP do not switch _local_player, only in MP
truelight
parents: 9370
diff changeset
   193
		_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
   194
		::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
   195
		_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
   196
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
   197
		/* Suspend the AI till the command is really executed */
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
   198
		::AI_SuspendPlayer(_current_player, -(int)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
   199
		/* Check if the callback still agrees with us, else return error */
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
   200
		if (!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
   201
	} else {
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   202
#else
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   203
	{
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   204
#endif
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   205
		/* 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
   206
		::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
   207
		/* Store some values inside the AIObject static memory */
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
   208
		SetNewVehicleID(_new_vehicle_id);
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
   209
		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
   210
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
   211
		/* Suspend the AI player for 1 tick, so it simulates MultiPlayer */
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
   212
		::AI_SuspendPlayer(_current_player, GetDoCommandDelay());
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   213
	}
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   214
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9841
diff changeset
   215
	if (::CmdFailed(res)) {
9864
4a9b7b610b13 (svn r12554) [NoAI] -Codechange: add more typing information.
rubidium
parents: 9863
diff changeset
   216
		SetLastError(AIError::StringToError(_error_message));
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9841
diff changeset
   217
		return false;
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9841
diff changeset
   218
	}
9452
4c5eedbc3ba9 (svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
truelight
parents: 9450
diff changeset
   219
9862
26f6a061eae6 (svn r12551) [NoAI] -Fix: remove unneeded AIObject:: (don't use AIObject:: inside the AIObject class ;))
truebrain
parents: 9851
diff changeset
   220
	IncreaseDoCommandCosts(res.GetCost());
9452
4c5eedbc3ba9 (svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
truelight
parents: 9450
diff changeset
   221
	return true;
9361
7bb2bd22b16e (svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff changeset
   222
}