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
8 /** @file script_story_page.cpp Implementation of ScriptStoryPage. */
9
10 #include "../../stdafx.h"
11 #include "script_story_page.hpp"
12 #include "script_error.hpp"
13 #include "script_industry.hpp"
14 #include "script_map.hpp"
15 #include "script_town.hpp"
16 #include "script_goal.hpp"
17 #include "../script_instance.hpp"
18 #include "../../story_base.h"
19 #include "../../goal_base.h"
20 #include "../../string_func.h"
21 #include "../../tile_map.h"
22
23 #include "../../safeguards.h"
24
StoryPageElementTypeRequiresText(StoryPageElementType type)25 static inline bool StoryPageElementTypeRequiresText(StoryPageElementType type)
26 {
27 return type == SPET_TEXT || type == SPET_LOCATION || type == SPET_BUTTON_PUSH || type == SPET_BUTTON_TILE || type == SPET_BUTTON_VEHICLE;
28 }
29
IsValidStoryPage(StoryPageID story_page_id)30 /* static */ bool ScriptStoryPage::IsValidStoryPage(StoryPageID story_page_id)
31 {
32 return ::StoryPage::IsValidID(story_page_id);
33 }
34
IsValidStoryPageElement(StoryPageElementID story_page_element_id)35 /* static */ bool ScriptStoryPage::IsValidStoryPageElement(StoryPageElementID story_page_element_id)
36 {
37 return ::StoryPageElement::IsValidID(story_page_element_id);
38 }
39
New(ScriptCompany::CompanyID company,Text * title)40 /* static */ ScriptStoryPage::StoryPageID ScriptStoryPage::New(ScriptCompany::CompanyID company, Text *title)
41 {
42 CCountedPtr<Text> counter(title);
43
44 EnforcePrecondition(STORY_PAGE_INVALID, ScriptObject::GetCompany() == OWNER_DEITY);
45 EnforcePrecondition(STORY_PAGE_INVALID, company == ScriptCompany::COMPANY_INVALID || ScriptCompany::ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID);
46
47 uint8 c = company;
48 if (company == ScriptCompany::COMPANY_INVALID) c = INVALID_COMPANY;
49
50 if (!ScriptObject::DoCommand(0,
51 c,
52 0,
53 CMD_CREATE_STORY_PAGE,
54 title != nullptr? title->GetEncodedText() : nullptr,
55 &ScriptInstance::DoCommandReturnStoryPageID)) return STORY_PAGE_INVALID;
56
57 /* In case of test-mode, we return StoryPageID 0 */
58 return (ScriptStoryPage::StoryPageID)0;
59 }
60
NewElement(StoryPageID story_page_id,StoryPageElementType type,uint32 reference,Text * text)61 /* static */ ScriptStoryPage::StoryPageElementID ScriptStoryPage::NewElement(StoryPageID story_page_id, StoryPageElementType type, uint32 reference, Text *text)
62 {
63 CCountedPtr<Text> counter(text);
64
65 ::StoryPageElementType btype = static_cast<::StoryPageElementType>(type);
66
67 EnforcePrecondition(STORY_PAGE_ELEMENT_INVALID, ScriptObject::GetCompany() == OWNER_DEITY);
68 EnforcePrecondition(STORY_PAGE_ELEMENT_INVALID, IsValidStoryPage(story_page_id));
69 EnforcePrecondition(STORY_PAGE_ELEMENT_INVALID, !StoryPageElementTypeRequiresText(btype) || (text != nullptr && !StrEmpty(text->GetEncodedText())));
70 EnforcePrecondition(STORY_PAGE_ELEMENT_INVALID, type != SPET_LOCATION || ::IsValidTile(reference));
71 EnforcePrecondition(STORY_PAGE_ELEMENT_INVALID, type != SPET_GOAL || ScriptGoal::IsValidGoal((ScriptGoal::GoalID)reference));
72 EnforcePrecondition(STORY_PAGE_ELEMENT_INVALID, type != SPET_GOAL || !(StoryPage::Get(story_page_id)->company == INVALID_COMPANY && Goal::Get(reference)->company != INVALID_COMPANY));
73
74 uint32 refid = 0;
75 TileIndex reftile = 0;
76 switch (type) {
77 case SPET_LOCATION:
78 reftile = reference;
79 break;
80 case SPET_GOAL:
81 case SPET_BUTTON_PUSH:
82 case SPET_BUTTON_TILE:
83 case SPET_BUTTON_VEHICLE:
84 refid = reference;
85 break;
86 case SPET_TEXT:
87 break;
88 default:
89 NOT_REACHED();
90 }
91
92 if (!ScriptObject::DoCommand(reftile,
93 story_page_id + (type << 16),
94 refid,
95 CMD_CREATE_STORY_PAGE_ELEMENT,
96 StoryPageElementTypeRequiresText(btype) ? text->GetEncodedText() : nullptr,
97 &ScriptInstance::DoCommandReturnStoryPageElementID)) return STORY_PAGE_ELEMENT_INVALID;
98
99 /* In case of test-mode, we return StoryPageElementID 0 */
100 return (ScriptStoryPage::StoryPageElementID)0;
101 }
102
UpdateElement(StoryPageElementID story_page_element_id,uint32 reference,Text * text)103 /* static */ bool ScriptStoryPage::UpdateElement(StoryPageElementID story_page_element_id, uint32 reference, Text *text)
104 {
105 CCountedPtr<Text> counter(text);
106
107 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
108 EnforcePrecondition(false, IsValidStoryPageElement(story_page_element_id));
109
110 StoryPageElement *pe = StoryPageElement::Get(story_page_element_id);
111 StoryPage *p = StoryPage::Get(pe->page);
112 ::StoryPageElementType type = pe->type;
113
114 EnforcePrecondition(false, !StoryPageElementTypeRequiresText(type) || (text != nullptr && !StrEmpty(text->GetEncodedText())));
115 EnforcePrecondition(false, type != ::SPET_LOCATION || ::IsValidTile(reference));
116 EnforcePrecondition(false, type != ::SPET_GOAL || ScriptGoal::IsValidGoal((ScriptGoal::GoalID)reference));
117 EnforcePrecondition(false, type != ::SPET_GOAL || !(p->company == INVALID_COMPANY && Goal::Get(reference)->company != INVALID_COMPANY));
118
119 uint32 refid = 0;
120 TileIndex reftile = 0;
121 switch (type) {
122 case ::SPET_LOCATION:
123 reftile = reference;
124 break;
125 case ::SPET_GOAL:
126 case ::SPET_BUTTON_PUSH:
127 case ::SPET_BUTTON_TILE:
128 case ::SPET_BUTTON_VEHICLE:
129 refid = reference;
130 break;
131 case ::SPET_TEXT:
132 break;
133 default:
134 NOT_REACHED();
135 }
136
137 return ScriptObject::DoCommand(reftile,
138 story_page_element_id,
139 refid,
140 CMD_UPDATE_STORY_PAGE_ELEMENT,
141 StoryPageElementTypeRequiresText(type) ? text->GetEncodedText() : nullptr);
142 }
143
GetPageSortValue(StoryPageID story_page_id)144 /* static */ uint32 ScriptStoryPage::GetPageSortValue(StoryPageID story_page_id)
145 {
146 EnforcePrecondition(false, IsValidStoryPage(story_page_id));
147
148 return StoryPage::Get(story_page_id)->sort_value;
149 }
150
GetPageElementSortValue(StoryPageElementID story_page_element_id)151 /* static */ uint32 ScriptStoryPage::GetPageElementSortValue(StoryPageElementID story_page_element_id)
152 {
153 EnforcePrecondition(false, IsValidStoryPageElement(story_page_element_id));
154
155 return StoryPageElement::Get(story_page_element_id)->sort_value;
156 }
157
SetTitle(StoryPageID story_page_id,Text * title)158 /* static */ bool ScriptStoryPage::SetTitle(StoryPageID story_page_id, Text *title)
159 {
160 CCountedPtr<Text> counter(title);
161
162 EnforcePrecondition(false, IsValidStoryPage(story_page_id));
163 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
164
165 return ScriptObject::DoCommand(0, story_page_id, 0, CMD_SET_STORY_PAGE_TITLE, title != nullptr? title->GetEncodedText() : nullptr);
166 }
167
GetCompany(StoryPageID story_page_id)168 /* static */ ScriptCompany::CompanyID ScriptStoryPage::GetCompany(StoryPageID story_page_id)
169 {
170 EnforcePrecondition(ScriptCompany::COMPANY_INVALID, IsValidStoryPage(story_page_id));
171
172 CompanyID c = StoryPage::Get(story_page_id)->company;
173 ScriptCompany::CompanyID company = c == INVALID_COMPANY ? ScriptCompany::COMPANY_INVALID : (ScriptCompany::CompanyID)c;
174
175 return company;
176 }
177
GetDate(StoryPageID story_page_id)178 /* static */ ScriptDate::Date ScriptStoryPage::GetDate(StoryPageID story_page_id)
179 {
180 EnforcePrecondition(ScriptDate::DATE_INVALID, IsValidStoryPage(story_page_id));
181 EnforcePrecondition(ScriptDate::DATE_INVALID, ScriptObject::GetCompany() == OWNER_DEITY);
182
183 return (ScriptDate::Date)StoryPage::Get(story_page_id)->date;
184 }
185
SetDate(StoryPageID story_page_id,ScriptDate::Date date)186 /* static */ bool ScriptStoryPage::SetDate(StoryPageID story_page_id, ScriptDate::Date date)
187 {
188 EnforcePrecondition(false, IsValidStoryPage(story_page_id));
189 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
190
191 return ScriptObject::DoCommand(0, story_page_id, date, CMD_SET_STORY_PAGE_DATE, nullptr);
192 }
193
194
Show(StoryPageID story_page_id)195 /* static */ bool ScriptStoryPage::Show(StoryPageID story_page_id)
196 {
197 EnforcePrecondition(false, IsValidStoryPage(story_page_id));
198 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
199
200 return ScriptObject::DoCommand(0, story_page_id, 0, CMD_SHOW_STORY_PAGE);
201 }
202
Remove(StoryPageID story_page_id)203 /* static */ bool ScriptStoryPage::Remove(StoryPageID story_page_id)
204 {
205 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
206 EnforcePrecondition(false, IsValidStoryPage(story_page_id));
207
208 return ScriptObject::DoCommand(0, story_page_id, 0, CMD_REMOVE_STORY_PAGE);
209 }
210
RemoveElement(StoryPageElementID story_page_element_id)211 /* static */ bool ScriptStoryPage::RemoveElement(StoryPageElementID story_page_element_id)
212 {
213 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
214 EnforcePrecondition(false, IsValidStoryPageElement(story_page_element_id));
215
216 return ScriptObject::DoCommand(0, story_page_element_id, 0, CMD_REMOVE_STORY_PAGE_ELEMENT);
217 }
218
MakePushButtonReference(StoryPageButtonColour colour,StoryPageButtonFlags flags)219 /* static */ ScriptStoryPage::StoryPageButtonFormatting ScriptStoryPage::MakePushButtonReference(StoryPageButtonColour colour, StoryPageButtonFlags flags)
220 {
221 StoryPageButtonData data;
222 data.SetColour((Colours)colour);
223 data.SetFlags((::StoryPageButtonFlags)flags);
224 if (!data.ValidateColour()) return UINT32_MAX;
225 if (!data.ValidateFlags()) return UINT32_MAX;
226 return data.referenced_id;
227 }
228
MakeTileButtonReference(StoryPageButtonColour colour,StoryPageButtonFlags flags,StoryPageButtonCursor cursor)229 /* static */ ScriptStoryPage::StoryPageButtonFormatting ScriptStoryPage::MakeTileButtonReference(StoryPageButtonColour colour, StoryPageButtonFlags flags, StoryPageButtonCursor cursor)
230 {
231 StoryPageButtonData data;
232 data.SetColour((Colours)colour);
233 data.SetFlags((::StoryPageButtonFlags)flags);
234 data.SetCursor((::StoryPageButtonCursor)cursor);
235 if (!data.ValidateColour()) return UINT32_MAX;
236 if (!data.ValidateFlags()) return UINT32_MAX;
237 if (!data.ValidateCursor()) return UINT32_MAX;
238 return data.referenced_id;
239 }
240
MakeVehicleButtonReference(StoryPageButtonColour colour,StoryPageButtonFlags flags,StoryPageButtonCursor cursor,ScriptVehicle::VehicleType vehtype)241 /* static */ ScriptStoryPage::StoryPageButtonFormatting ScriptStoryPage::MakeVehicleButtonReference(StoryPageButtonColour colour, StoryPageButtonFlags flags, StoryPageButtonCursor cursor, ScriptVehicle::VehicleType vehtype)
242 {
243 StoryPageButtonData data;
244 data.SetColour((Colours)colour);
245 data.SetFlags((::StoryPageButtonFlags)flags);
246 data.SetCursor((::StoryPageButtonCursor)cursor);
247 data.SetVehicleType((::VehicleType)vehtype);
248 if (!data.ValidateColour()) return UINT32_MAX;
249 if (!data.ValidateFlags()) return UINT32_MAX;
250 if (!data.ValidateCursor()) return UINT32_MAX;
251 if (!data.ValidateVehicleType()) return UINT32_MAX;
252 return data.referenced_id;
253 }
254
255