OpenTTD
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++) {
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) {
76  continue;
77  }
78 
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 };
Owner
Enum for all companies/owners.
Definition: company_type.h:18
static void StartNew(CompanyID company, bool rerandomise_ai=true)
Start a new AI company.
Definition: ai_core.cpp:36
bool _networking
are we in networking mode?
Definition: network.cpp:52
#define SLEG_STR(variable, type)
Storage of a global string in every savegame version.
Definition: saveload.h:737
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:354
108 15045
Definition: saveload.h:171
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.
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
const char * GetName() const
Get the name of the Script.
bool IsRandom() const
Is the current Script a randomly chosen Script?
static void Save(CompanyID company)
Save data from an AI to a savegame.
Definition: ai_core.cpp:278
Functions/types related to saving and loading games.
Highest possible saveload version.
Definition: saveload.h:305
static bool IsValidAiID(size_t index)
Is this company a valid company, controlled by the computer (a NoAI program)?
Definition: company_base.h:138
bool HasScript() const
Is this config attached to an Script? In other words, is there a Script that is assigned to this slot...
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: ai_config.cpp:45
static void Load(CompanyID company, int version)
Load data for an AI from a savegame.
Definition: ai_core.cpp:292
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:637
Handlers and description of chunk.
Definition: saveload.h:356
#define SLEG_CONDVAR(variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition: saveload.h:671
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:651
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:57
First company, same as owner.
Definition: company_type.h:22
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:66
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1546
Maximum number of companies.
Definition: company_type.h:23
bool _network_server
network-server is active
Definition: network.cpp:53
void SlAutolength(AutolengthProc *proc, void *arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:1574
SaveLoad type struct.
Definition: saveload.h:496
136 18764
Definition: saveload.h:205
static void LoadEmpty()
Load and discard data from a savegame.
void StringToSettings(const char *value)
Convert a string which is stored in the config file or savegames to custom settings of this Script...
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.
Get the Script config from the current game.
Last chunk in this array.
Definition: saveload.h:391
int GetVersion() const
Get the version of the Script.