1 /* $Id: ai_sl.cpp 24033 2012-03-17 11:14:25Z rubidium $ */
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/>.
10 /** @file ai_sl.cpp Handles the saveload part of the AIs */
12 #include "../stdafx.h"
13 #include "../company_base.h"
16 #include "../string_func.h"
18 #include "../ai/ai.hpp"
19 #include "../ai/ai_config.hpp"
20 #include "../network/network.h"
21 #include "../ai/ai_instance.hpp"
23 #include "../safeguards.h"
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
;
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
),
38 static void SaveReal_AIPL(int *index_ptr
)
40 CompanyID index
= (CompanyID
)*index_ptr
;
41 AIConfig
*config
= AIConfig::GetConfig(index
);
43 if (config
->HasScript()) {
44 strecpy(_ai_saveload_name
, config
->GetName(), lastof(_ai_saveload_name
));
45 _ai_saveload_version
= config
->GetVersion();
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;
52 _ai_saveload_is_random
= config
->IsRandom();
53 _ai_saveload_settings
[0] = '\0';
54 config
->SettingsToString(_ai_saveload_settings
, lastof(_ai_saveload_settings
));
56 SlObject(nullptr, _ai_company
);
57 /* If the AI was active, store his data too */
58 if (Company::IsValidAiID(index
)) AI::Save(index
);
61 static void Load_AIPL()
63 /* Free all current data */
64 for (CompanyID c
= COMPANY_FIRST
; c
< MAX_COMPANIES
; c
++) {
65 AIConfig::GetConfig(c
, AIConfig::SSS_FORCE_GAME
)->Change(nullptr);
69 while ((index
= (CompanyID
)SlIterateArray()) != (CompanyID
)-1) {
70 if (index
>= MAX_COMPANIES
) SlErrorCorrupt("Too many AI configs");
72 _ai_saveload_is_random
= 0;
73 _ai_saveload_version
= -1;
74 SlObject(nullptr, _ai_company
);
76 if (_networking
&& !_network_server
) {
77 if (Company::IsValidAiID(index
)) AIInstance::LoadEmpty();
81 AIConfig
*config
= AIConfig::GetConfig(index
, AIConfig::SSS_FORCE_GAME
);
82 if (StrEmpty(_ai_saveload_name
)) {
84 config
->Change(nullptr, -1, false, true);
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.");
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.");
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.");
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;
109 config
->StringToSettings(_ai_saveload_settings
);
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
);
119 static void Save_AIPL()
121 for (int i
= COMPANY_FIRST
; i
< MAX_COMPANIES
; i
++) {
123 SlAutolength((AutolengthProc
*)SaveReal_AIPL
, &i
);
127 extern const ChunkHandler _ai_chunk_handlers
[] = {
128 { 'AIPL', Save_AIPL
, Load_AIPL
, nullptr, nullptr, CH_ARRAY
| CH_LAST
},