2011-12-19 21:03:17 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file script_goal.cpp Implementation of ScriptGoal. */
|
|
|
|
|
|
|
|
#include "../../stdafx.h"
|
2018-04-28 15:37:59 +00:00
|
|
|
#include "script_game.hpp"
|
2011-12-19 21:03:17 +00:00
|
|
|
#include "script_goal.hpp"
|
|
|
|
#include "script_error.hpp"
|
|
|
|
#include "script_industry.hpp"
|
|
|
|
#include "script_map.hpp"
|
|
|
|
#include "script_town.hpp"
|
2013-11-16 17:41:57 +00:00
|
|
|
#include "script_story_page.hpp"
|
2011-12-19 21:03:17 +00:00
|
|
|
#include "../script_instance.hpp"
|
|
|
|
#include "../../goal_base.h"
|
|
|
|
#include "../../string_func.h"
|
2018-04-28 15:37:59 +00:00
|
|
|
#include "../../network/network_base.h"
|
2021-11-01 20:30:34 +00:00
|
|
|
#include "../../goal_cmd.h"
|
2011-12-19 21:03:17 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../../safeguards.h"
|
|
|
|
|
2011-12-19 21:03:17 +00:00
|
|
|
/* static */ bool ScriptGoal::IsValidGoal(GoalID goal_id)
|
|
|
|
{
|
|
|
|
return ::Goal::IsValidID(goal_id);
|
|
|
|
}
|
|
|
|
|
2011-12-19 21:06:06 +00:00
|
|
|
/* static */ ScriptGoal::GoalID ScriptGoal::New(ScriptCompany::CompanyID company, Text *goal, GoalType type, uint32 destination)
|
2011-12-19 21:03:17 +00:00
|
|
|
{
|
2011-12-19 21:06:06 +00:00
|
|
|
CCountedPtr<Text> counter(goal);
|
|
|
|
|
2012-01-03 16:36:24 +00:00
|
|
|
EnforcePrecondition(GOAL_INVALID, ScriptObject::GetCompany() == OWNER_DEITY);
|
2019-04-10 21:07:06 +00:00
|
|
|
EnforcePrecondition(GOAL_INVALID, goal != nullptr);
|
2023-02-16 01:05:54 +00:00
|
|
|
const std::string &text = goal->GetEncodedText();
|
2013-02-08 20:34:27 +00:00
|
|
|
EnforcePreconditionEncodedText(GOAL_INVALID, text);
|
2011-12-19 21:03:17 +00:00
|
|
|
EnforcePrecondition(GOAL_INVALID, company == ScriptCompany::COMPANY_INVALID || ScriptCompany::ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID);
|
|
|
|
|
2021-11-20 21:30:56 +00:00
|
|
|
CompanyID c = (::CompanyID)company;
|
2011-12-19 21:03:17 +00:00
|
|
|
if (company == ScriptCompany::COMPANY_INVALID) c = INVALID_COMPANY;
|
2019-04-10 21:07:06 +00:00
|
|
|
StoryPage *story_page = nullptr;
|
2013-11-16 17:41:57 +00:00
|
|
|
if (type == GT_STORY_PAGE && ScriptStoryPage::IsValidStoryPage((ScriptStoryPage::StoryPageID)destination)) story_page = ::StoryPage::Get((ScriptStoryPage::StoryPageID)destination);
|
|
|
|
|
|
|
|
EnforcePrecondition(GOAL_INVALID, (type == GT_NONE && destination == 0) ||
|
|
|
|
(type == GT_TILE && ScriptMap::IsValidTile(destination)) ||
|
|
|
|
(type == GT_INDUSTRY && ScriptIndustry::IsValidIndustry(destination)) ||
|
|
|
|
(type == GT_TOWN && ScriptTown::IsValidTown(destination)) ||
|
|
|
|
(type == GT_COMPANY && ScriptCompany::ResolveCompanyID((ScriptCompany::CompanyID)destination) != ScriptCompany::COMPANY_INVALID) ||
|
2019-04-10 21:07:06 +00:00
|
|
|
(type == GT_STORY_PAGE && story_page != nullptr && (c == INVALID_COMPANY ? story_page->company == INVALID_COMPANY : story_page->company == INVALID_COMPANY || story_page->company == c)));
|
2011-12-19 21:03:17 +00:00
|
|
|
|
2021-11-20 21:30:56 +00:00
|
|
|
if (!ScriptObject::Command<CMD_CREATE_GOAL>::Do(&ScriptInstance::DoCommandReturnGoalID, c, (::GoalType)type, destination, text)) return GOAL_INVALID;
|
2011-12-19 21:03:17 +00:00
|
|
|
|
|
|
|
/* In case of test-mode, we return GoalID 0 */
|
|
|
|
return (ScriptGoal::GoalID)0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool ScriptGoal::Remove(GoalID goal_id)
|
|
|
|
{
|
2012-01-03 16:36:24 +00:00
|
|
|
EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
|
2011-12-19 21:03:17 +00:00
|
|
|
EnforcePrecondition(false, IsValidGoal(goal_id));
|
|
|
|
|
2021-11-20 21:30:56 +00:00
|
|
|
return ScriptObject::Command<CMD_REMOVE_GOAL>::Do(goal_id);
|
2011-12-19 21:03:17 +00:00
|
|
|
}
|
2012-01-03 16:36:24 +00:00
|
|
|
|
2013-05-26 19:54:43 +00:00
|
|
|
/* static */ bool ScriptGoal::SetText(GoalID goal_id, Text *goal)
|
|
|
|
{
|
|
|
|
CCountedPtr<Text> counter(goal);
|
|
|
|
|
|
|
|
EnforcePrecondition(false, IsValidGoal(goal_id));
|
|
|
|
EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
|
2019-04-10 21:07:06 +00:00
|
|
|
EnforcePrecondition(false, goal != nullptr);
|
2023-02-16 01:05:54 +00:00
|
|
|
const std::string &text = goal->GetEncodedText();
|
|
|
|
EnforcePreconditionEncodedText(false, text);
|
2013-05-26 19:54:43 +00:00
|
|
|
|
2023-02-16 01:05:54 +00:00
|
|
|
return ScriptObject::Command<CMD_SET_GOAL_TEXT>::Do(goal_id, text);
|
2013-05-26 19:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool ScriptGoal::SetProgress(GoalID goal_id, Text *progress)
|
|
|
|
{
|
|
|
|
CCountedPtr<Text> counter(progress);
|
|
|
|
|
|
|
|
EnforcePrecondition(false, IsValidGoal(goal_id));
|
|
|
|
EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
|
|
|
|
|
2023-02-16 01:05:54 +00:00
|
|
|
return ScriptObject::Command<CMD_SET_GOAL_PROGRESS>::Do(goal_id, progress != nullptr ? progress->GetEncodedText() : std::string{});
|
2013-05-26 19:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool ScriptGoal::SetCompleted(GoalID goal_id, bool completed)
|
|
|
|
{
|
|
|
|
EnforcePrecondition(false, IsValidGoal(goal_id));
|
|
|
|
EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
|
|
|
|
|
2021-11-20 21:30:56 +00:00
|
|
|
return ScriptObject::Command<CMD_SET_GOAL_COMPLETED>::Do(goal_id, completed);
|
2013-05-26 19:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool ScriptGoal::IsCompleted(GoalID goal_id)
|
|
|
|
{
|
|
|
|
EnforcePrecondition(false, IsValidGoal(goal_id));
|
|
|
|
EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
|
|
|
|
|
|
|
|
Goal *g = Goal::Get(goal_id);
|
2019-04-10 21:07:06 +00:00
|
|
|
return g != nullptr && g->completed;
|
2013-05-26 19:54:43 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 23:50:24 +00:00
|
|
|
/* static */ bool ScriptGoal::DoQuestion(uint16 uniqueid, uint32 target, bool is_client, Text *question, QuestionType type, uint32 buttons)
|
2012-01-03 16:36:24 +00:00
|
|
|
{
|
|
|
|
CCountedPtr<Text> counter(question);
|
|
|
|
|
|
|
|
EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
|
2019-04-10 21:07:06 +00:00
|
|
|
EnforcePrecondition(false, question != nullptr);
|
2023-02-16 01:05:54 +00:00
|
|
|
const std::string &text = question->GetEncodedText();
|
2013-02-08 20:34:27 +00:00
|
|
|
EnforcePreconditionEncodedText(false, text);
|
2021-02-02 16:14:26 +00:00
|
|
|
uint min_buttons = (type == QT_QUESTION ? 1 : 0);
|
|
|
|
EnforcePrecondition(false, CountBits(buttons) >= min_buttons && CountBits(buttons) <= 3);
|
2012-01-21 12:03:55 +00:00
|
|
|
EnforcePrecondition(false, buttons < (1 << ::GOAL_QUESTION_BUTTON_COUNT));
|
2021-02-02 16:14:26 +00:00
|
|
|
EnforcePrecondition(false, (int)type < ::GQT_END);
|
2012-01-03 16:36:24 +00:00
|
|
|
|
2021-11-20 21:30:56 +00:00
|
|
|
return ScriptObject::Command<CMD_GOAL_QUESTION>::Do(uniqueid, target, is_client, buttons, (::GoalQuestionType)type, text);
|
2018-04-28 15:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool ScriptGoal::Question(uint16 uniqueid, ScriptCompany::CompanyID company, Text *question, QuestionType type, int buttons)
|
|
|
|
{
|
|
|
|
EnforcePrecondition(false, company == ScriptCompany::COMPANY_INVALID || ScriptCompany::ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID);
|
2012-01-03 16:36:24 +00:00
|
|
|
uint8 c = company;
|
|
|
|
if (company == ScriptCompany::COMPANY_INVALID) c = INVALID_COMPANY;
|
|
|
|
|
2018-04-28 15:37:59 +00:00
|
|
|
return DoQuestion(uniqueid, c, false, question, type, buttons);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool ScriptGoal::QuestionClient(uint16 uniqueid, ScriptClient::ClientID client, Text *question, QuestionType type, int buttons)
|
|
|
|
{
|
|
|
|
EnforcePrecondition(false, ScriptGame::IsMultiplayer());
|
|
|
|
EnforcePrecondition(false, ScriptClient::ResolveClientID(client) != ScriptClient::CLIENT_INVALID);
|
2019-05-02 23:50:24 +00:00
|
|
|
return DoQuestion(uniqueid, client, true, question, type, buttons);
|
2012-01-03 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool ScriptGoal::CloseQuestion(uint16 uniqueid)
|
|
|
|
{
|
|
|
|
EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
|
|
|
|
|
2021-11-20 21:30:56 +00:00
|
|
|
return ScriptObject::Command<CMD_GOAL_QUESTION_ANSWER>::Do(uniqueid, 0);
|
2012-01-03 16:36:24 +00:00
|
|
|
}
|