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_goal.cpp Implementation of ScriptGoal. */
12 #include "../../stdafx.h"
13 #include "script_goal.hpp"
14 #include "script_error.hpp"
15 #include "script_industry.hpp"
16 #include "script_map.hpp"
17 #include "script_town.hpp"
18 #include "script_story_page.hpp"
19 #include "../script_instance.hpp"
20 #include "../../goal_base.h"
21 #include "../../string_func.h"
23 #include "../../safeguards.h"
25 /* static */ bool ScriptGoal::IsValidGoal(GoalID goal_id
)
27 return ::Goal::IsValidID(goal_id
);
30 /* static */ ScriptGoal::GoalID
ScriptGoal::New(ScriptCompany::CompanyID company
, Text
*goal
, GoalType type
, uint32 destination
)
32 CCountedPtr
<Text
> counter(goal
);
34 EnforcePrecondition(GOAL_INVALID
, ScriptObject::GetCompany() == OWNER_DEITY
);
35 EnforcePrecondition(GOAL_INVALID
, goal
!= NULL
);
36 const char *text
= goal
->GetEncodedText();
37 EnforcePreconditionEncodedText(GOAL_INVALID
, text
);
38 EnforcePrecondition(GOAL_INVALID
, company
== ScriptCompany::COMPANY_INVALID
|| ScriptCompany::ResolveCompanyID(company
) != ScriptCompany::COMPANY_INVALID
);
41 if (company
== ScriptCompany::COMPANY_INVALID
) c
= INVALID_COMPANY
;
42 StoryPage
*story_page
= NULL
;
43 if (type
== GT_STORY_PAGE
&& ScriptStoryPage::IsValidStoryPage((ScriptStoryPage::StoryPageID
)destination
)) story_page
= ::StoryPage::Get((ScriptStoryPage::StoryPageID
)destination
);
45 EnforcePrecondition(GOAL_INVALID
, (type
== GT_NONE
&& destination
== 0) ||
46 (type
== GT_TILE
&& ScriptMap::IsValidTile(destination
)) ||
47 (type
== GT_INDUSTRY
&& ScriptIndustry::IsValidIndustry(destination
)) ||
48 (type
== GT_TOWN
&& ScriptTown::IsValidTown(destination
)) ||
49 (type
== GT_COMPANY
&& ScriptCompany::ResolveCompanyID((ScriptCompany::CompanyID
)destination
) != ScriptCompany::COMPANY_INVALID
) ||
50 (type
== GT_STORY_PAGE
&& story_page
!= NULL
&& (c
== INVALID_COMPANY
? story_page
->company
== INVALID_COMPANY
: story_page
->company
== INVALID_COMPANY
|| story_page
->company
== c
)));
52 if (!ScriptObject::DoCommand(0, type
| (c
<< 8), destination
, CMD_CREATE_GOAL
, text
, &ScriptInstance::DoCommandReturnGoalID
)) return GOAL_INVALID
;
54 /* In case of test-mode, we return GoalID 0 */
55 return (ScriptGoal::GoalID
)0;
58 /* static */ bool ScriptGoal::Remove(GoalID goal_id
)
60 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY
);
61 EnforcePrecondition(false, IsValidGoal(goal_id
));
63 return ScriptObject::DoCommand(0, goal_id
, 0, CMD_REMOVE_GOAL
);
66 /* static */ bool ScriptGoal::SetText(GoalID goal_id
, Text
*goal
)
68 CCountedPtr
<Text
> counter(goal
);
70 EnforcePrecondition(false, IsValidGoal(goal_id
));
71 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY
);
72 EnforcePrecondition(false, goal
!= NULL
);
73 EnforcePrecondition(false, !StrEmpty(goal
->GetEncodedText()));
75 return ScriptObject::DoCommand(0, goal_id
, 0, CMD_SET_GOAL_TEXT
, goal
->GetEncodedText());
78 /* static */ bool ScriptGoal::SetProgress(GoalID goal_id
, Text
*progress
)
80 CCountedPtr
<Text
> counter(progress
);
82 EnforcePrecondition(false, IsValidGoal(goal_id
));
83 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY
);
85 /* Ensure null as used for emtpy string. */
86 if (progress
!= NULL
&& StrEmpty(progress
->GetEncodedText())) {
90 return ScriptObject::DoCommand(0, goal_id
, 0, CMD_SET_GOAL_PROGRESS
, progress
!= NULL
? progress
->GetEncodedText() : NULL
);
93 /* static */ bool ScriptGoal::SetCompleted(GoalID goal_id
, bool completed
)
95 EnforcePrecondition(false, IsValidGoal(goal_id
));
96 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY
);
98 return ScriptObject::DoCommand(0, goal_id
, completed
? 1 : 0, CMD_SET_GOAL_COMPLETED
);
101 /* static */ bool ScriptGoal::IsCompleted(GoalID goal_id
)
103 EnforcePrecondition(false, IsValidGoal(goal_id
));
104 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY
);
106 Goal
*g
= Goal::Get(goal_id
);
107 return g
!= NULL
&& g
->completed
;
110 /* static */ bool ScriptGoal::Question(uint16 uniqueid
, ScriptCompany::CompanyID company
, Text
*question
, QuestionType type
, int buttons
)
112 CCountedPtr
<Text
> counter(question
);
114 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY
);
115 EnforcePrecondition(false, question
!= NULL
);
116 const char *text
= question
->GetEncodedText();
117 EnforcePreconditionEncodedText(false, text
);
118 EnforcePrecondition(false, company
== ScriptCompany::COMPANY_INVALID
|| ScriptCompany::ResolveCompanyID(company
) != ScriptCompany::COMPANY_INVALID
);
119 EnforcePrecondition(false, CountBits(buttons
) >= 1 && CountBits(buttons
) <= 3);
120 EnforcePrecondition(false, buttons
< (1 << ::GOAL_QUESTION_BUTTON_COUNT
));
121 EnforcePrecondition(false, (int)type
< ::GOAL_QUESTION_TYPE_COUNT
);
124 if (company
== ScriptCompany::COMPANY_INVALID
) c
= INVALID_COMPANY
;
126 return ScriptObject::DoCommand(0, uniqueid
| (c
<< 16) | (type
<< 24), buttons
, CMD_GOAL_QUESTION
, text
);
129 /* static */ bool ScriptGoal::CloseQuestion(uint16 uniqueid
)
131 EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY
);
133 return ScriptObject::DoCommand(0, uniqueid
, 0, CMD_GOAL_QUESTION_ANSWER
);