1 /* $Id: story_sl.cpp 25620 2013-07-21 13:18:45Z zuu $ */
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 story_sl.cpp Code handling saving and loading of story pages */
12 #include "../stdafx.h"
13 #include "../story_base.h"
17 #include "../safeguards.h"
19 /** Called after load to trash broken pages. */
20 void AfterLoadStoryBook()
22 if (IsSavegameVersionBefore(185)) {
23 /* Trash all story pages and page elements because
24 * they were saved with wrong data types.
26 _story_page_element_pool
.CleanPool();
27 _story_page_pool
.CleanPool();
31 static const SaveLoad _story_page_elements_desc
[] = {
32 SLE_CONDVAR(StoryPageElement
, sort_value
, SLE_FILE_U16
| SLE_VAR_U32
, 0, 184),
33 SLE_CONDVAR(StoryPageElement
, sort_value
, SLE_UINT32
, 185, SL_MAX_VERSION
),
34 SLE_VAR(StoryPageElement
, page
, SLE_UINT16
),
35 SLE_CONDVAR(StoryPageElement
, type
, SLE_FILE_U16
| SLE_VAR_U8
, 0, 184),
36 SLE_CONDVAR(StoryPageElement
, type
, SLE_UINT8
, 185, SL_MAX_VERSION
),
37 SLE_VAR(StoryPageElement
, referenced_id
, SLE_UINT32
),
38 SLE_STR(StoryPageElement
, text
, SLE_STR
| SLF_ALLOW_CONTROL
, 0),
42 static void Save_STORY_PAGE_ELEMENT()
45 FOR_ALL_STORY_PAGE_ELEMENTS(s
) {
46 SlSetArrayIndex(s
->index
);
47 SlObject(s
, _story_page_elements_desc
);
51 static void Load_STORY_PAGE_ELEMENT()
54 uint32 max_sort_value
= 0;
55 while ((index
= SlIterateArray()) != -1) {
56 StoryPageElement
*s
= new (index
) StoryPageElement();
57 SlObject(s
, _story_page_elements_desc
);
58 if (s
->sort_value
> max_sort_value
) {
59 max_sort_value
= s
->sort_value
;
62 /* Update the next sort value, so that the next
63 * created page is shown after all existing pages.
65 _story_page_element_next_sort_value
= max_sort_value
+ 1;
68 static const SaveLoad _story_pages_desc
[] = {
69 SLE_CONDVAR(StoryPage
, sort_value
, SLE_FILE_U16
| SLE_VAR_U32
, 0, 184),
70 SLE_CONDVAR(StoryPage
, sort_value
, SLE_UINT32
, 185, SL_MAX_VERSION
),
71 SLE_VAR(StoryPage
, date
, SLE_UINT32
),
72 SLE_CONDVAR(StoryPage
, company
, SLE_FILE_U16
| SLE_VAR_U8
, 0, 184),
73 SLE_CONDVAR(StoryPage
, company
, SLE_UINT8
, 185, SL_MAX_VERSION
),
74 SLE_STR(StoryPage
, title
, SLE_STR
| SLF_ALLOW_CONTROL
, 0),
78 static void Save_STORY_PAGE()
81 FOR_ALL_STORY_PAGES(s
) {
82 SlSetArrayIndex(s
->index
);
83 SlObject(s
, _story_pages_desc
);
87 static void Load_STORY_PAGE()
90 uint32 max_sort_value
= 0;
91 while ((index
= SlIterateArray()) != -1) {
92 StoryPage
*s
= new (index
) StoryPage();
93 SlObject(s
, _story_pages_desc
);
94 if (s
->sort_value
> max_sort_value
) {
95 max_sort_value
= s
->sort_value
;
98 /* Update the next sort value, so that the next
99 * created page is shown after all existing pages.
101 _story_page_next_sort_value
= max_sort_value
+ 1;
104 extern const ChunkHandler _story_page_chunk_handlers
[] = {
105 { 'STPE', Save_STORY_PAGE_ELEMENT
, Load_STORY_PAGE_ELEMENT
, nullptr, nullptr, CH_ARRAY
},
106 { 'STPA', Save_STORY_PAGE
, Load_STORY_PAGE
, nullptr, nullptr, CH_ARRAY
| CH_LAST
},