OpenTTD Source: src/saveload/ai_sl.cpp Source File

OpenTTD Source 1.11.0-RC1

ai_sl.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #include "../stdafx.h"
11 #include "../company_base.h"
12 #include "../debug.h"
13 #include "saveload.h"
14 #include "../string_func.h"
15
16 #include "../ai/ai.hpp"
17 #include "../ai/ai_config.hpp"
18 #include "../network/network.h"
19 #include "../ai/ai_instance.hpp"
20
21 #include "../safeguards.h"
22
23 static char _ai_saveload_name[64];
24 static int _ai_saveload_version;
25 static char _ai_saveload_settings[1024];
26 static bool _ai_saveload_is_random;
27
28 static const SaveLoad _ai_company[] = {
29 SLEG_STR(_ai_saveload_name, SLE_STRB),
30 SLEG_STR(_ai_saveload_settings, SLE_STRB),
31 SLEG_CONDVAR(_ai_saveload_version, SLE_UINT32, SLV_108, SL_MAX_VERSION),
32 SLEG_CONDVAR(_ai_saveload_is_random, SLE_BOOL, SLV_136, SL_MAX_VERSION),
33 SLE_END()
34 };
35
36 static void SaveReal_AIPL(int *index_ptr)
37 {
38 CompanyID index = (CompanyID)*index_ptr;
39 AIConfig *config = AIConfig::GetConfig(index);
40
41 if (config->HasScript()) {
42 strecpy(_ai_saveload_name, config->GetName(), lastof(_ai_saveload_name));
43 _ai_saveload_version = config->GetVersion();
44 } else {
45 /* No AI is configured for this so store an empty string as name. */
46 _ai_saveload_name[0] = '\0';
47 _ai_saveload_version = -1;
48 }
49
50 _ai_saveload_is_random = config->IsRandom();
51 _ai_saveload_settings[0] = '\0';
52 config->SettingsToString(_ai_saveload_settings, lastof(_ai_saveload_settings));
53
54 SlObject(nullptr, _ai_company);
55 /* If the AI was active, store his data too */
56 if (Company::IsValidAiID(index)) AI::Save(index);
57 }
58
59 static void Load_AIPL()
60 {
61 /* Free all current data */
62 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
63 AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(nullptr);
64 }
65
66 CompanyID index;
67 while ((index = (CompanyID)SlIterateArray()) != (CompanyID)-1) {
68 if (index >= MAX_COMPANIES) SlErrorCorrupt("Too many AI configs");
69
70 _ai_saveload_is_random = 0;
71 _ai_saveload_version = -1;
72 SlObject(nullptr, _ai_company);
73
74 if (_networking && !_network_server) {
75 if (Company::IsValidAiID(index)) AIInstance::LoadEmpty();
76 continue;
77 }
78
79 AIConfig *config = AIConfig::GetConfig(index, AIConfig::SSS_FORCE_GAME);
80 if (StrEmpty(_ai_saveload_name)) {
81 /* A random AI. */
82 config->Change(nullptr, -1, false, true);
83 } else {
84 config->Change(_ai_saveload_name, _ai_saveload_version, false, _ai_saveload_is_random);
85 if (!config->HasScript()) {
86 /* No version of the AI available that can load the data. Try to load the
87 * latest version of the AI instead. */
88 config->Change(_ai_saveload_name, -1, false, _ai_saveload_is_random);
89 if (!config->HasScript()) {
90 if (strcmp(_ai_saveload_name, "%_dummy") != 0) {
91 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);
92 DEBUG(script, 0, "A random other AI will be loaded in its place.");
93 } else {
94 DEBUG(script, 0, "The savegame had no AIs available at the time of saving.");
95 DEBUG(script, 0, "A random available AI will be loaded now.");
96 }
97 } else {
98 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);
99 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.");
100 }
101 /* Make sure the AI doesn't get the saveload data, as he was not the
102 * writer of the saveload data in the first place */
103 _ai_saveload_version = -1;
104 }
105 }
106
107 config->StringToSettings(_ai_saveload_settings);
108
109 /* Start the AI directly if it was active in the savegame */
110 if (Company::IsValidAiID(index)) {
111 AI::StartNew(index, false);
112 AI::Load(index, _ai_saveload_version);
113 }
114 }
115 }
116
117 static void Save_AIPL()
118 {
119 for (int i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
120 SlSetArrayIndex(i);
121 SlAutolength((AutolengthProc *)SaveReal_AIPL, &i);
122 }
123 }
124
125 extern const ChunkHandler _ai_chunk_handlers[] = {
126 { 'AIPL', Save_AIPL, Load_AIPL, nullptr, nullptr, CH_ARRAY | CH_LAST},
127 };
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:179
AIConfig
Definition: ai_config.hpp:16
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:205
_network_server
bool _network_server
network-server is active
Definition: network.cpp:53
AI::Load
static void Load(CompanyID company, int version)
Load data for an AI from a savegame.
Definition: ai_core.cpp:292
CH_LAST
@ CH_LAST
Last chunk in this array.
Definition: saveload.h:410
saveload.h
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:379
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
SLEG_CONDVAR
#define SLEG_CONDVAR(variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition: saveload.h:707
COMPANY_FIRST
@ COMPANY_FIRST
First company, same as owner.
Definition: company_type.h:22
SLV_108
@ SLV_108
108 15045
Definition: saveload.h:172
AI::Save
static void Save(CompanyID company)
Save data from an AI to a savegame.
Definition: ai_core.cpp:278
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
ScriptConfig::GetName
const char * GetName() const
Get the name of the Script.
Definition: script_config.cpp:169
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:686
AI::StartNew
static void StartNew(CompanyID company, bool rerandomise_ai=true)
Start a new AI company.
Definition: ai_core.cpp:36
AIConfig::GetConfig
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: ai_config.cpp:45
ScriptConfig::IsRandom
bool IsRandom() const
Is the current Script a randomly chosen Script?
Definition: script_config.cpp:164
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
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:133
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:60
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:52
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
ScriptInstance::LoadEmpty
static void LoadEmpty()
Load and discard data from a savegame.
Definition: script_instance.cpp:618
SlAutolength
void SlAutolength(AutolengthProc *proc, void *arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:1640
ScriptConfig::GetVersion
int GetVersion() const
Get the version of the Script.
Definition: script_config.cpp:174
SLV_136
@ SLV_136
136 18764
Definition: saveload.h:206
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:328
SLEG_STR
#define SLEG_STR(variable, type)
Storage of a global string in every savegame version.
Definition: saveload.h:782
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:358
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:19
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
SaveLoad
SaveLoad type struct.
Definition: saveload.h:516
ScriptConfig::SSS_FORCE_GAME
@ SSS_FORCE_GAME
Get the Script config from the current game.
Definition: script_config.hpp:105
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
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:159
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:631