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_goal.cpp Implementation of ScriptGoal. */
9 
10 #include "../../stdafx.h"
11 #include "script_game.hpp"
12 #include "script_goal.hpp"
13 #include "script_error.hpp"
14 #include "script_industry.hpp"
15 #include "script_map.hpp"
16 #include "script_town.hpp"
17 #include "script_story_page.hpp"
18 #include "../script_instance.hpp"
19 #include "../../goal_base.h"
20 #include "../../string_func.h"
21 #include "../../network/network_base.h"
22 
23 #include "../../safeguards.h"
24 
IsValidGoal(GoalID goal_id)25 /* static */ bool ScriptGoal::IsValidGoal(GoalID goal_id)
26 {
27 	return ::Goal::IsValidID(goal_id);
28 }
29 
New(ScriptCompany::CompanyID company,Text * goal,GoalType type,uint32 destination)30 /* static */ ScriptGoal::GoalID ScriptGoal::New(ScriptCompany::CompanyID company, Text *goal, GoalType type, uint32 destination)
31 {
32 	CCountedPtr<Text> counter(goal);
33 
34 	EnforcePrecondition(GOAL_INVALID, ScriptObject::GetCompany() == OWNER_DEITY);
35 	EnforcePrecondition(GOAL_INVALID, goal != nullptr);
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);
39 
40 	uint8 c = company;
41 	if (company == ScriptCompany::COMPANY_INVALID) c = INVALID_COMPANY;
42 	StoryPage *story_page = nullptr;
43 	if (type == GT_STORY_PAGE && ScriptStoryPage::IsValidStoryPage((ScriptStoryPage::StoryPageID)destination)) story_page = ::StoryPage::Get((ScriptStoryPage::StoryPageID)destination);
44 
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 != nullptr && (c == INVALID_COMPANY ? story_page->company == INVALID_COMPANY : story_page->company == INVALID_COMPANY || story_page->company == c)));
51 
52 	if (!ScriptObject::DoCommand(0, type | (c << 8), destination, CMD_CREATE_GOAL, text, &ScriptInstance::DoCommandReturnGoalID)) return GOAL_INVALID;
53 
54 	/* In case of test-mode, we return GoalID 0 */
55 	return (ScriptGoal::GoalID)0;
56 }
57 
Remove(GoalID goal_id)58 /* static */ bool ScriptGoal::Remove(GoalID goal_id)
59 {
60 	EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
61 	EnforcePrecondition(false, IsValidGoal(goal_id));
62 
63 	return ScriptObject::DoCommand(0, goal_id, 0, CMD_REMOVE_GOAL);
64 }
65 
SetText(GoalID goal_id,Text * goal)66 /* static */ bool ScriptGoal::SetText(GoalID goal_id, Text *goal)
67 {
68 	CCountedPtr<Text> counter(goal);
69 
70 	EnforcePrecondition(false, IsValidGoal(goal_id));
71 	EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
72 	EnforcePrecondition(false, goal != nullptr);
73 	EnforcePrecondition(false, !StrEmpty(goal->GetEncodedText()));
74 
75 	return ScriptObject::DoCommand(0, goal_id, 0, CMD_SET_GOAL_TEXT, goal->GetEncodedText());
76 }
77 
SetProgress(GoalID goal_id,Text * progress)78 /* static */ bool ScriptGoal::SetProgress(GoalID goal_id, Text *progress)
79 {
80 	CCountedPtr<Text> counter(progress);
81 
82 	EnforcePrecondition(false, IsValidGoal(goal_id));
83 	EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
84 
85 	/* Ensure null as used for empty string. */
86 	if (progress != nullptr && StrEmpty(progress->GetEncodedText())) {
87 		progress = nullptr;
88 	}
89 
90 	return ScriptObject::DoCommand(0, goal_id, 0, CMD_SET_GOAL_PROGRESS, progress != nullptr ? progress->GetEncodedText() : nullptr);
91 }
92 
SetCompleted(GoalID goal_id,bool completed)93 /* static */ bool ScriptGoal::SetCompleted(GoalID goal_id, bool completed)
94 {
95 	EnforcePrecondition(false, IsValidGoal(goal_id));
96 	EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
97 
98 	return ScriptObject::DoCommand(0, goal_id, completed ? 1 : 0, CMD_SET_GOAL_COMPLETED);
99 }
100 
IsCompleted(GoalID goal_id)101 /* static */ bool ScriptGoal::IsCompleted(GoalID goal_id)
102 {
103 	EnforcePrecondition(false, IsValidGoal(goal_id));
104 	EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
105 
106 	Goal *g = Goal::Get(goal_id);
107 	return g != nullptr && g->completed;
108 }
109 
DoQuestion(uint16 uniqueid,uint32 target,bool is_client,Text * question,QuestionType type,uint32 buttons)110 /* static */ bool ScriptGoal::DoQuestion(uint16 uniqueid, uint32 target, bool is_client, Text *question, QuestionType type, uint32 buttons)
111 {
112 	CCountedPtr<Text> counter(question);
113 
114 	EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
115 	EnforcePrecondition(false, question != nullptr);
116 	const char *text = question->GetEncodedText();
117 	EnforcePreconditionEncodedText(false, text);
118 	uint min_buttons = (type == QT_QUESTION ? 1 : 0);
119 	EnforcePrecondition(false, CountBits(buttons) >= min_buttons && CountBits(buttons) <= 3);
120 	EnforcePrecondition(false, buttons < (1 << ::GOAL_QUESTION_BUTTON_COUNT));
121 	EnforcePrecondition(false, (int)type < ::GQT_END);
122 
123 	return ScriptObject::DoCommand(0, uniqueid | (target << 16), buttons | (type << 29) | (is_client ? (1 << 31) : 0), CMD_GOAL_QUESTION, text);
124 }
125 
Question(uint16 uniqueid,ScriptCompany::CompanyID company,Text * question,QuestionType type,int buttons)126 /* static */ bool ScriptGoal::Question(uint16 uniqueid, ScriptCompany::CompanyID company, Text *question, QuestionType type, int buttons)
127 {
128 	EnforcePrecondition(false, company == ScriptCompany::COMPANY_INVALID || ScriptCompany::ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID);
129 	uint8 c = company;
130 	if (company == ScriptCompany::COMPANY_INVALID) c = INVALID_COMPANY;
131 
132 	return DoQuestion(uniqueid, c, false, question, type, buttons);
133 }
134 
QuestionClient(uint16 uniqueid,ScriptClient::ClientID client,Text * question,QuestionType type,int buttons)135 /* static */ bool ScriptGoal::QuestionClient(uint16 uniqueid, ScriptClient::ClientID client, Text *question, QuestionType type, int buttons)
136 {
137 	EnforcePrecondition(false, ScriptGame::IsMultiplayer());
138 	EnforcePrecondition(false, ScriptClient::ResolveClientID(client) != ScriptClient::CLIENT_INVALID);
139 	/* Can only send 16 bits of client_id before proper fix is implemented */
140 	EnforcePrecondition(false, client < (1 << 16));
141 	return DoQuestion(uniqueid, client, true, question, type, buttons);
142 }
143 
CloseQuestion(uint16 uniqueid)144 /* static */ bool ScriptGoal::CloseQuestion(uint16 uniqueid)
145 {
146 	EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
147 
148 	return ScriptObject::DoCommand(0, uniqueid, 0, CMD_GOAL_QUESTION_ANSWER);
149 }
150