OpenTTD Source  1.11.0-beta2
story_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 "../story_base.h"
12 
13 #include "saveload.h"
14 
15 #include "../safeguards.h"
16 
19 {
21  /* Trash all story pages and page elements because
22  * they were saved with wrong data types.
23  */
24  _story_page_element_pool.CleanPool();
25  _story_page_pool.CleanPool();
26  }
27 }
28 
29 static const SaveLoad _story_page_elements_desc[] = {
30  SLE_CONDVAR(StoryPageElement, sort_value, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_185),
31  SLE_CONDVAR(StoryPageElement, sort_value, SLE_UINT32, SLV_185, SL_MAX_VERSION),
32  SLE_VAR(StoryPageElement, page, SLE_UINT16),
33  SLE_CONDVAR(StoryPageElement, type, SLE_FILE_U16 | SLE_VAR_U8, SL_MIN_VERSION, SLV_185),
35  SLE_VAR(StoryPageElement, referenced_id, SLE_UINT32),
37  SLE_END()
38 };
39 
40 static void Save_STORY_PAGE_ELEMENT()
41 {
43  SlSetArrayIndex(s->index);
44  SlObject(s, _story_page_elements_desc);
45  }
46 }
47 
48 static void Load_STORY_PAGE_ELEMENT()
49 {
50  int index;
51  uint32 max_sort_value = 0;
52  while ((index = SlIterateArray()) != -1) {
53  StoryPageElement *s = new (index) StoryPageElement();
54  SlObject(s, _story_page_elements_desc);
55  if (s->sort_value > max_sort_value) {
56  max_sort_value = s->sort_value;
57  }
58  }
59  /* Update the next sort value, so that the next
60  * created page is shown after all existing pages.
61  */
62  _story_page_element_next_sort_value = max_sort_value + 1;
63 }
64 
65 static const SaveLoad _story_pages_desc[] = {
66  SLE_CONDVAR(StoryPage, sort_value, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_185),
67  SLE_CONDVAR(StoryPage, sort_value, SLE_UINT32, SLV_185, SL_MAX_VERSION),
68  SLE_VAR(StoryPage, date, SLE_UINT32),
69  SLE_CONDVAR(StoryPage, company, SLE_FILE_U16 | SLE_VAR_U8, SL_MIN_VERSION, SLV_185),
70  SLE_CONDVAR(StoryPage, company, SLE_UINT8, SLV_185, SL_MAX_VERSION),
72  SLE_END()
73 };
74 
75 static void Save_STORY_PAGE()
76 {
77  for (StoryPage *s : StoryPage::Iterate()) {
78  SlSetArrayIndex(s->index);
79  SlObject(s, _story_pages_desc);
80  }
81 }
82 
83 static void Load_STORY_PAGE()
84 {
85  int index;
86  uint32 max_sort_value = 0;
87  while ((index = SlIterateArray()) != -1) {
88  StoryPage *s = new (index) StoryPage();
89  SlObject(s, _story_pages_desc);
90  if (s->sort_value > max_sort_value) {
91  max_sort_value = s->sort_value;
92  }
93  }
94  /* Update the next sort value, so that the next
95  * created page is shown after all existing pages.
96  */
97  _story_page_next_sort_value = max_sort_value + 1;
98 }
99 
100 extern const ChunkHandler _story_page_chunk_handlers[] = {
101  { 'STPE', Save_STORY_PAGE_ELEMENT, Load_STORY_PAGE_ELEMENT, nullptr, nullptr, CH_ARRAY},
102  { 'STPA', Save_STORY_PAGE, Load_STORY_PAGE, nullptr, nullptr, CH_ARRAY | CH_LAST},
103 };
StoryPageElement::sort_value
uint32 sort_value
A number that increases for every created story page element. Used for sorting. The id of a story pag...
Definition: story_base.h:140
AfterLoadStoryBook
void AfterLoadStoryBook()
Called after load to trash broken pages.
Definition: story_sl.cpp:18
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
Pool::CleanPool
virtual void CleanPool()
Virtual method that deletes all items in the pool.
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:227
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:551
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:647
CH_LAST
@ CH_LAST
Last chunk in this array.
Definition: saveload.h:410
saveload.h
StoryPage::sort_value
uint32 sort_value
A number that increases for every created story page. Used for sorting. The id of a story page is the...
Definition: story_base.h:160
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:379
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
allow control codes in the strings
Definition: saveload.h:489
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:686
StoryPage
Struct about stories, current and completed.
Definition: story_base.h:159
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:815
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:328
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:621
Pool::PoolItem<&_story_page_element_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:378
StoryPageElement
Struct about story page elements.
Definition: story_base.h:139
SLV_185
@ SLV_185
185 25620 Storybooks
Definition: saveload.h:265
SaveLoad
SaveLoad type struct.
Definition: saveload.h:516
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:631