OpenTTD
story_sl.cpp
Go to the documentation of this file.
1 /* $Id: story_sl.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2 
3 /*
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/>.
8  */
9 
12 #include "../stdafx.h"
13 #include "../story_base.h"
14 
15 #include "saveload.h"
16 
17 #include "../safeguards.h"
18 
21 {
22  if (IsSavegameVersionBefore(185)) {
23  /* Trash all story pages and page elements because
24  * they were saved with wrong data types.
25  */
26  _story_page_element_pool.CleanPool();
27  _story_page_pool.CleanPool();
28  }
29 }
30 
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),
39  SLE_END()
40 };
41 
42 static void Save_STORY_PAGE_ELEMENT()
43 {
45  FOR_ALL_STORY_PAGE_ELEMENTS(s) {
46  SlSetArrayIndex(s->index);
47  SlObject(s, _story_page_elements_desc);
48  }
49 }
50 
51 static void Load_STORY_PAGE_ELEMENT()
52 {
53  int index;
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;
60  }
61  }
62  /* Update the next sort value, so that the next
63  * created page is shown after all existing pages.
64  */
65  _story_page_element_next_sort_value = max_sort_value + 1;
66 }
67 
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),
75  SLE_END()
76 };
77 
78 static void Save_STORY_PAGE()
79 {
80  StoryPage *s;
81  FOR_ALL_STORY_PAGES(s) {
82  SlSetArrayIndex(s->index);
83  SlObject(s, _story_pages_desc);
84  }
85 }
86 
87 static void Load_STORY_PAGE()
88 {
89  int index;
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;
96  }
97  }
98  /* Update the next sort value, so that the next
99  * created page is shown after all existing pages.
100  */
101  _story_page_next_sort_value = max_sort_value + 1;
102 }
103 
104 extern const ChunkHandler _story_page_chunk_handlers[] = {
105  { 'STPE', Save_STORY_PAGE_ELEMENT, Load_STORY_PAGE_ELEMENT, NULL, NULL, CH_ARRAY},
106  { 'STPA', Save_STORY_PAGE, Load_STORY_PAGE, NULL, NULL, CH_ARRAY | CH_LAST},
107 };
void AfterLoadStoryBook()
Called after load to trash broken pages.
Definition: story_sl.cpp:20
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:48
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
Struct about story page elements.
Definition: story_base.h:47
Struct about stories, current and completed.
Definition: story_base.h:70
Functions/types related to saving and loading games.
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:246
allow control codes in the strings
Definition: saveload.h:184
#define SL_MAX_VERSION
Highest possible savegame version.
Definition: saveload.h:96
static bool IsSavegameVersionBefore(uint16 major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:465
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:71
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:828
Handlers and description of chunk.
Definition: saveload.h:66
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:353
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
SaveLoad type struct.
Definition: saveload.h:208
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:296
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:322
Last chunk in this array.
Definition: saveload.h:104
virtual void CleanPool()
Virtual method that deletes all items in the pool.