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 
8 /** @file ai_sl.cpp Handles the saveload part of the AIs */
9 
10 #include "../stdafx.h"
11 #include "../debug.h"
12 
13 #include "saveload.h"
14 #include "compat/ai_sl_compat.h"
15 
16 #include "../company_base.h"
17 #include "../string_func.h"
18 
19 #include "../ai/ai.hpp"
20 #include "../ai/ai_config.hpp"
21 #include "../network/network.h"
22 #include "../ai/ai_instance.hpp"
23 
24 #include "../safeguards.h"
25 
26 static std::string _ai_saveload_name;
27 static int         _ai_saveload_version;
28 static std::string _ai_saveload_settings;
29 static bool        _ai_saveload_is_random;
30 
31 static const SaveLoad _ai_company_desc[] = {
32 	   SLEG_SSTR("name",      _ai_saveload_name,         SLE_STR),
33 	   SLEG_SSTR("settings",  _ai_saveload_settings,     SLE_STR),
34 	SLEG_CONDVAR("version",   _ai_saveload_version,   SLE_UINT32, SLV_108, SL_MAX_VERSION),
35 	SLEG_CONDVAR("is_random", _ai_saveload_is_random,   SLE_BOOL, SLV_136, SL_MAX_VERSION),
36 };
37 
SaveReal_AIPL(int * index_ptr)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 		_ai_saveload_name = config->GetName();
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.clear();
49 		_ai_saveload_version = -1;
50 	}
51 
52 	_ai_saveload_is_random = config->IsRandom();
53 	_ai_saveload_settings = config->SettingsToString();
54 
55 	SlObject(nullptr, _ai_company_desc);
56 	/* If the AI was active, store its data too */
57 	if (Company::IsValidAiID(index)) AI::Save(index);
58 }
59 
60 struct AIPLChunkHandler : ChunkHandler {
AIPLChunkHandlerAIPLChunkHandler61 	AIPLChunkHandler() : ChunkHandler('AIPL', CH_TABLE) {}
62 
LoadAIPLChunkHandler63 	void Load() const override
64 	{
65 		const std::vector<SaveLoad> slt = SlCompatTableHeader(_ai_company_desc, _ai_company_sl_compat);
66 
67 		/* Free all current data */
68 		for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
69 			AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(nullptr);
70 		}
71 
72 		CompanyID index;
73 		while ((index = (CompanyID)SlIterateArray()) != (CompanyID)-1) {
74 			if (index >= MAX_COMPANIES) SlErrorCorrupt("Too many AI configs");
75 
76 			_ai_saveload_is_random = false;
77 			_ai_saveload_version = -1;
78 			SlObject(nullptr, slt);
79 
80 			if (_networking && !_network_server) {
81 				if (Company::IsValidAiID(index)) AIInstance::LoadEmpty();
82 				continue;
83 			}
84 
85 			AIConfig *config = AIConfig::GetConfig(index, AIConfig::SSS_FORCE_GAME);
86 			if (_ai_saveload_name.empty()) {
87 				/* A random AI. */
88 				config->Change(nullptr, -1, false, true);
89 			} else {
90 				config->Change(_ai_saveload_name.c_str(), _ai_saveload_version, false, _ai_saveload_is_random);
91 				if (!config->HasScript()) {
92 					/* No version of the AI available that can load the data. Try to load the
93 					 * latest version of the AI instead. */
94 					config->Change(_ai_saveload_name.c_str(), -1, false, _ai_saveload_is_random);
95 					if (!config->HasScript()) {
96 						if (_ai_saveload_name.compare("%_dummy") != 0) {
97 							Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version);
98 							Debug(script, 0, "A random other AI will be loaded in its place.");
99 						} else {
100 							Debug(script, 0, "The savegame had no AIs available at the time of saving.");
101 							Debug(script, 0, "A random available AI will be loaded now.");
102 						}
103 					} else {
104 						Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version);
105 						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.");
106 					}
107 					/* Make sure the AI doesn't get the saveload data, as it was not the
108 					 *  writer of the saveload data in the first place */
109 					_ai_saveload_version = -1;
110 				}
111 			}
112 
113 			config->StringToSettings(_ai_saveload_settings);
114 
115 			/* Start the AI directly if it was active in the savegame */
116 			if (Company::IsValidAiID(index)) {
117 				AI::StartNew(index, false);
118 				AI::Load(index, _ai_saveload_version);
119 			}
120 		}
121 	}
122 
SaveAIPLChunkHandler123 	void Save() const override
124 	{
125 		SlTableHeader(_ai_company_desc);
126 
127 		for (int i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
128 			SlSetArrayIndex(i);
129 			SlAutolength((AutolengthProc *)SaveReal_AIPL, &i);
130 		}
131 	}
132 };
133 
134 static const AIPLChunkHandler AIPL;
135 static const ChunkHandlerRef ai_chunk_handlers[] = {
136 	AIPL,
137 };
138 
139 extern const ChunkHandlerTable _ai_chunk_handlers(ai_chunk_handlers);
140