OpenTTD

ai_sl.cpp
Go to the documentation of this file.
1 /* $Id: ai_sl.cpp 26493 2014-04-24 04:41:54Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
12 #include "../stdafx.h"
13 #include "../company_base.h"
14 #include "../debug.h"
15 #include "saveload.h"
16 #include "../string_func.h"
17
18 #include "../ai/ai.hpp"
19 #include "../ai/ai_config.hpp"
20 #include "../network/network.h"
21 #include "../ai/ai_instance.hpp"
22
23 #include "../safeguards.h"
24
25 static char _ai_saveload_name[64];
26 static int _ai_saveload_version;
27 static char _ai_saveload_settings[1024];
28 static bool _ai_saveload_is_random;
29
30 static const SaveLoad _ai_company[] = {
31 SLEG_STR(_ai_saveload_name, SLE_STRB),
32 SLEG_STR(_ai_saveload_settings, SLE_STRB),
33 SLEG_CONDVAR(_ai_saveload_version, SLE_UINT32, 108, SL_MAX_VERSION),
34 SLEG_CONDVAR(_ai_saveload_is_random, SLE_BOOL, 136, SL_MAX_VERSION),
35 SLE_END()
36 };
37
38 static void SaveReal_AIPL(int *index_ptr)
39 {
40 CompanyID index = (CompanyID)*index_ptr;
41 AIConfig *config = AIConfig::GetConfig(index);
42
43 if (config->HasScript()) {
44 strecpy(_ai_saveload_name, config->GetName(), lastof(_ai_saveload_name));
45 _ai_saveload_version = config->GetVersion();
46 } else {
47 /* No AI is configured for this so store an empty string as name. */
48 _ai_saveload_name[0] = '\0';
49 _ai_saveload_version = -1;
50 }
51
52 _ai_saveload_is_random = config->IsRandom();
53 _ai_saveload_settings[0] = '\0';
54 config->SettingsToString(_ai_saveload_settings, lastof(_ai_saveload_settings));
55
56 SlObject(NULL, _ai_company);
57 /* If the AI was active, store his data too */
58 if (Company::IsValidAiID(index)) AI::Save(index);
59 }
60
61 static void Load_AIPL()
62 {
63 /* Free all current data */
64 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
65 AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(NULL);
66 }
67
68 CompanyID index;
69 while ((index = (CompanyID)SlIterateArray()) != (CompanyID)-1) {
70 if (index >= MAX_COMPANIES) SlErrorCorrupt("Too many AI configs");
71
72 _ai_saveload_is_random = 0;
73 _ai_saveload_version = -1;
74 SlObject(NULL, _ai_company);
75
76 if (_networking && !_network_server) {
77 if (Company::IsValidAiID(index)) AIInstance::LoadEmpty();
78 continue;
79 }
80
81 AIConfig *config = AIConfig::GetConfig(index, AIConfig::SSS_FORCE_GAME);
82 if (StrEmpty(_ai_saveload_name)) {
83 /* A random AI. */
84 config->Change(NULL, -1, false, true);
85 } else {
86 config->Change(_ai_saveload_name, _ai_saveload_version, false, _ai_saveload_is_random);
87 if (!config->HasScript()) {
88 /* No version of the AI available that can load the data. Try to load the
89 * latest version of the AI instead. */
90 config->Change(_ai_saveload_name, -1, false, _ai_saveload_is_random);
91 if (!config->HasScript()) {
92 if (strcmp(_ai_saveload_name, "%_dummy") != 0) {
93 DEBUG(script, 0, "The savegame has an AI by the name '%s', version %d which is no longer available.", _ai_saveload_name, _ai_saveload_version);
94 DEBUG(script, 0, "A random other AI will be loaded in its place.");
95 } else {
96 DEBUG(script, 0, "The savegame had no AIs available at the time of saving.");
97 DEBUG(script, 0, "A random available AI will be loaded now.");
98 }
99 } else {
100 DEBUG(script, 0, "The savegame has an AI by the name '%s', version %d which is no longer available.", _ai_saveload_name, _ai_saveload_version);
101 DEBUG(script, 0, "The latest version of that AI has been loaded instead, but it'll not get the savegame data as it's incompatible.");
102 }
103 /* Make sure the AI doesn't get the saveload data, as he was not the
104 * writer of the saveload data in the first place */
105 _ai_saveload_version = -1;
106 }
107 }
108
109 config->StringToSettings(_ai_saveload_settings);
110
111 /* Start the AI directly if it was active in the savegame */
112 if (Company::IsValidAiID(index)) {
113 AI::StartNew(index, false);
114 AI::Load(index, _ai_saveload_version);
115 }
116 }
117 }
118
119 static void Save_AIPL()
120 {
121 for (int i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
122 SlSetArrayIndex(i);
123 SlAutolength((AutolengthProc *)SaveReal_AIPL, &i);
124 }
125 }
126
127 extern const ChunkHandler _ai_chunk_handlers[] = {
128 { 'AIPL', Save_AIPL, Load_AIPL, NULL, NULL, CH_ARRAY | CH_LAST},
129 };
AI::StartNew
static void StartNew(CompanyID company, bool rerandomise_ai=true)
Start a new AI company.
Definition: ai_core.cpp:37
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:56
SLEG_STR
#define SLEG_STR(variable, type)
Storage of a global string in every savegame version.
Definition: saveload.h:439
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:561
ScriptConfig::Change
void Change(const char *name, int version=-1, bool force_exact_match=false, bool is_random=false)
Set another Script to be loaded in this slot.
Definition: script_config.cpp:21
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
ScriptConfig::GetName
const char * GetName() const
Get the name of the Script.
Definition: script_config.cpp:151
ScriptConfig::IsRandom
bool IsRandom() const
Is the current Script a randomly chosen Script?
Definition: script_config.cpp:146
AI::Save
static void Save(CompanyID company)
Save data from an AI to a savegame.
Definition: ai_core.cpp:277
saveload.h
Functions/types related to saving and loading games.
Company::IsValidAiID
static bool IsValidAiID(size_t index)
Is this company a valid company, controlled by the computer (a NoAI program)?
Definition: company_base.h:132
ScriptConfig::HasScript
bool HasScript() const
Is this config attached to an Script? In other words, is there a Script that is assigned to this slot...
Definition: script_config.cpp:141
COMPANY_FIRST
First company, same as owner.
Definition: company_type.h:24
AIConfig
Definition: ai_config.hpp:18
AIConfig::GetConfig
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: ai_config.cpp:38
SL_MAX_VERSION
#define SL_MAX_VERSION
Highest possible savegame version.
Definition: saveload.h:96
AI::Load
static void Load(CompanyID company, int version)
Load data for an AI from a savegame.
Definition: ai_core.cpp:291
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:828
MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:25
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:66
SLEG_CONDVAR
#define SLEG_CONDVAR(variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition: saveload.h:373
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:353
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
_network_server
bool _network_server
network-server is active
Definition: network.cpp:57
SlAutolength
void SlAutolength(AutolengthProc *proc, void *arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:1640
SaveLoad
SaveLoad type struct.
Definition: saveload.h:208
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:20
ScriptInstance::LoadEmpty
static void LoadEmpty()
Load and discard data from a savegame.
Definition: script_instance.cpp:611
ScriptConfig::StringToSettings
void StringToSettings(const char *value)
Convert a string which is stored in the config file or savegames to custom settings of this Script...
Definition: script_config.cpp:161
ScriptConfig::SettingsToString
void SettingsToString(char *string, const char *last) const
Convert the custom settings to a string that can be stored in the config file or savegames.
Definition: script_config.cpp:187
ScriptConfig::SSS_FORCE_GAME
Get the Script config from the current game.
Definition: script_config.hpp:107
CH_LAST
Last chunk in this array.
Definition: saveload.h:104
ScriptConfig::GetVersion
int GetVersion() const
Get the version of the Script.
Definition: script_config.cpp:156