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 };