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 script_story_page.cpp Implementation of ScriptStoryPage. */
12 #include "../../stdafx.h"
13 #include "script_story_page.hpp"
14 #include "script_error.hpp"
15 #include "script_industry.hpp"
16 #include "script_map.hpp"
17 #include "script_town.hpp"
18 #include "script_goal.hpp"
19 #include "../script_instance.hpp"
20 #include "../../story_base.h"
21 #include "../../goal_base.h"
22 #include "../../string_func.h"
23 #include "../../tile_map.h"
25 #include "../../safeguards.h"
27 /* static */ bool ScriptStoryPage::IsValidStoryPage(StoryPageID story_page_id
)
29 return ::StoryPage::IsValidID(story_page_id
);
32 /* static */ bool ScriptStoryPage::IsValidStoryPageElement(StoryPageElementID story_page_element_id
)
34 return ::StoryPageElement::IsValidID(story_page_element_id
);
37 /* static */ ScriptStoryPage::StoryPageID
ScriptStoryPage::New(ScriptCompany::CompanyID company
, Text
*title
)
39 CCountedPtr
<Text
> counter(title
);
41 EnforcePrecondition(STORY_PAGE_INVALID
, ScriptObject::GetCompany() == OWNER_DEITY
);
42 EnforcePrecondition(STORY_PAGE_INVALID
, company
== ScriptCompany::COMPANY_INVALID
|| ScriptCompany::ResolveCompanyID(company
) != ScriptCompany::COMPANY_INVALID
);
45 if (company
== ScriptCompany::COMPANY_INVALID
) c
= INVALID_COMPANY
;
47 if (!ScriptObject::DoCommand(0,
50 CMD_CREATE_STORY_PAGE
,
51 title
!= NULL
? title
->GetEncodedText() : NULL
,
52 &ScriptInstance::DoCommandReturnStoryPageID
)) return STORY_PAGE_INVALID
;
54 /* In case of test-mode, we return StoryPageID 0 */
55 return (ScriptStoryPage::StoryPageID
)0;
58 /* static */ ScriptStoryPage::StoryPageElementID
ScriptStoryPage::NewElement(StoryPageID story_page_id
, StoryPageElementType type
, uint32 reference
, Text
*text
)
60 CCountedPtr
<Text
> counter(text
);
62 EnforcePrecondition(STORY_PAGE_ELEMENT_INVALID
, ScriptObject::GetCompany() == OWNER_DEITY
);
63 EnforcePrecondition(STORY_PAGE_ELEMENT_INVALID
, IsValidStoryPage(story_page_id
));
64 EnforcePrecondition(STORY_PAGE_ELEMENT_INVALID
, (type
!= SPET_TEXT
&& type
!= SPET_LOCATION
) || (text
!= NULL
&& !StrEmpty(text
->GetEncodedText())));
65 EnforcePrecondition(STORY_PAGE_ELEMENT_INVALID
, type
!= SPET_LOCATION
|| ::IsValidTile(reference
));
66 EnforcePrecondition(STORY_PAGE_ELEMENT_INVALID
, type
!= SPET_GOAL
|| ScriptGoal::IsValidGoal((ScriptGoal::GoalID
)reference
));
67 EnforcePrecondition(STORY_PAGE_ELEMENT_INVALID
, type
!= SPET_GOAL
|| !(StoryPage::Get(story_page_id
)->company
== INVALID_COMPANY
&& Goal::Get(reference
)->company
!= INVALID_COMPANY
));
69 if (!ScriptObject::DoCommand(type
== SPET_LOCATION
? reference
: 0,
70 story_page_id
+ (type
<< 16),
71 type
== SPET_GOAL
? reference
: 0,
72 CMD_CREATE_STORY_PAGE_ELEMENT
,
73 type
== SPET_TEXT
|| type
== SPET_LOCATION
? text
->GetEncodedText() : NULL
,
74 &ScriptInstance::DoCommandReturnStoryPageElementID
)) return STORY_PAGE_ELEMENT_INVALID
;
76 /* In case of test-mode, we return StoryPageElementID 0 */
77 return (ScriptStoryPage::StoryPageElementID
)0;
80 /* static */ bool ScriptStoryPage::UpdateElement(StoryPageElementID story_page_element_id
, uint32 reference
, Text
*text
)
82 CCountedPtr
<Text
> counter(text
);
84 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY
);
85 EnforcePrecondition(false, IsValidStoryPageElement(story_page_element_id
));
87 StoryPageElement
*pe
= StoryPageElement::Get(story_page_element_id
);
88 StoryPage
*p
= StoryPage::Get(pe
->page
);
89 ::StoryPageElementType type
= pe
->type
;
91 EnforcePrecondition(false, (type
!= ::SPET_TEXT
&& type
!= ::SPET_LOCATION
) || (text
!= NULL
&& !StrEmpty(text
->GetEncodedText())));
92 EnforcePrecondition(false, type
!= ::SPET_LOCATION
|| ::IsValidTile(reference
));
93 EnforcePrecondition(false, type
!= ::SPET_GOAL
|| ScriptGoal::IsValidGoal((ScriptGoal::GoalID
)reference
));
94 EnforcePrecondition(false, type
!= ::SPET_GOAL
|| !(p
->company
== INVALID_COMPANY
&& Goal::Get(reference
)->company
!= INVALID_COMPANY
));
96 return ScriptObject::DoCommand(type
== ::SPET_LOCATION
? reference
: 0,
97 story_page_element_id
,
98 type
== ::SPET_GOAL
? reference
: 0,
99 CMD_UPDATE_STORY_PAGE_ELEMENT
,
100 type
== ::SPET_TEXT
|| type
== ::SPET_LOCATION
? text
->GetEncodedText() : NULL
);
103 /* static */ uint32
ScriptStoryPage::GetPageSortValue(StoryPageID story_page_id
)
105 EnforcePrecondition(false, IsValidStoryPage(story_page_id
));
107 return StoryPage::Get(story_page_id
)->sort_value
;
110 /* static */ uint32
ScriptStoryPage::GetPageElementSortValue(StoryPageElementID story_page_element_id
)
112 EnforcePrecondition(false, IsValidStoryPageElement(story_page_element_id
));
114 return StoryPageElement::Get(story_page_element_id
)->sort_value
;
117 /* static */ bool ScriptStoryPage::SetTitle(StoryPageID story_page_id
, Text
*title
)
119 CCountedPtr
<Text
> counter(title
);
121 EnforcePrecondition(false, IsValidStoryPage(story_page_id
));
122 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY
);
124 return ScriptObject::DoCommand(0, story_page_id
, 0, CMD_SET_STORY_PAGE_TITLE
, title
!= NULL
? title
->GetEncodedText() : NULL
);
127 /* static */ ScriptCompany::CompanyID
ScriptStoryPage::GetCompany(StoryPageID story_page_id
)
129 EnforcePrecondition(ScriptCompany::COMPANY_INVALID
, IsValidStoryPage(story_page_id
));
131 CompanyID c
= StoryPage::Get(story_page_id
)->company
;
132 ScriptCompany::CompanyID company
= c
== INVALID_COMPANY
? ScriptCompany::COMPANY_INVALID
: (ScriptCompany::CompanyID
)c
;
137 /* static */ ScriptDate::Date
ScriptStoryPage::GetDate(StoryPageID story_page_id
)
139 EnforcePrecondition(ScriptDate::DATE_INVALID
, IsValidStoryPage(story_page_id
));
140 EnforcePrecondition(ScriptDate::DATE_INVALID
, ScriptObject::GetCompany() == OWNER_DEITY
);
142 return (ScriptDate::Date
)StoryPage::Get(story_page_id
)->date
;
145 /* static */ bool ScriptStoryPage::SetDate(StoryPageID story_page_id
, ScriptDate::Date date
)
147 EnforcePrecondition(false, IsValidStoryPage(story_page_id
));
148 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY
);
150 return ScriptObject::DoCommand(0, story_page_id
, date
, CMD_SET_STORY_PAGE_DATE
, NULL
);
154 /* static */ bool ScriptStoryPage::Show(StoryPageID story_page_id
)
156 EnforcePrecondition(false, IsValidStoryPage(story_page_id
));
157 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY
);
159 return ScriptObject::DoCommand(0, story_page_id
, 0, CMD_SHOW_STORY_PAGE
);
162 /* static */ bool ScriptStoryPage::Remove(StoryPageID story_page_id
)
164 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY
);
165 EnforcePrecondition(false, IsValidStoryPage(story_page_id
));
167 return ScriptObject::DoCommand(0, story_page_id
, 0, CMD_REMOVE_STORY_PAGE
);
170 /* static */ bool ScriptStoryPage::RemoveElement(StoryPageElementID story_page_element_id
)
172 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY
);
173 EnforcePrecondition(false, IsValidStoryPageElement(story_page_element_id
));
175 return ScriptObject::DoCommand(0, story_page_element_id
, 0, CMD_REMOVE_STORY_PAGE_ELEMENT
);