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);
|
|
|
|
}
|
|
|
|
|
2023-06-08 17:00:31 +00:00
|
|
|
/* static */ bool ScriptGoal::IsValidGoalDestination(ScriptCompany::CompanyID company, GoalType type, SQInteger destination)
|
2011-12-19 21:03:17 +00:00
|
|
|
{
|
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);
|
2023-06-08 17:00:31 +00:00
|
|
|
return (type == GT_NONE && destination == 0) ||
|
2013-11-16 17:41:57 +00:00
|
|
|
(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) ||
|
2023-06-08 17:00:31 +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
|
|
|
|
2023-06-08 17:00:31 +00:00
|
|
|
/* static */ ScriptGoal::GoalID ScriptGoal::New(ScriptCompany::CompanyID company, Text *goal, GoalType type, SQInteger destination)
|
|
|
|
{
|
|
|
|
CCountedPtr<Text> counter(goal);
|
|
|
|
|
|
|
|
EnforceDeityMode(GOAL_INVALID);
|
|
|
|
EnforcePrecondition(GOAL_INVALID, goal != nullptr);
|
2023-07-03 14:22:15 +00:00
|
|
|
std::string text = goal->GetEncodedText();
|
2023-06-08 17:00:31 +00:00
|
|
|
EnforcePreconditionEncodedText(GOAL_INVALID, text);
|
|
|
|
EnforcePrecondition(GOAL_INVALID, company == ScriptCompany::COMPANY_INVALID || ScriptCompany::ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID);
|
|
|
|
EnforcePrecondition(GOAL_INVALID, IsValidGoalDestination(company, type, destination));
|
|
|
|
|
|
|
|
if (!ScriptObject::Command<CMD_CREATE_GOAL>::Do(&ScriptInstance::DoCommandReturnGoalID, (::CompanyID)company, (::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)
|
|
|
|
{
|
2023-03-02 18:44:13 +00:00
|
|
|
EnforceDeityMode(false);
|
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
|
|
|
|
2023-06-08 17:00:31 +00:00
|
|
|
/* static */ bool ScriptGoal::SetDestination(GoalID goal_id, GoalType type, SQInteger destination)
|
|
|
|
{
|
|
|
|
EnforceDeityMode(false);
|
|
|
|
EnforcePrecondition(false, IsValidGoal(goal_id));
|
|
|
|
Goal *g = Goal::Get(goal_id);
|
|
|
|
EnforcePrecondition(false, IsValidGoalDestination((ScriptCompany::CompanyID)g->company, type, destination));
|
|
|
|
|
|
|
|
return ScriptObject::Command<CMD_SET_GOAL_DESTINATION>::Do(goal_id, (::GoalType)type, destination);
|
|
|
|
}
|
|
|
|
|
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));
|
2023-03-02 18:44:13 +00:00
|
|
|
EnforceDeityMode(false);
|
2019-04-10 21:07:06 +00:00
|
|
|
EnforcePrecondition(false, goal != nullptr);
|
2023-07-03 14:22:15 +00:00
|
|
|
std::string text = goal->GetEncodedText();
|
2023-02-16 01:05:54 +00:00
|
|
|
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));
|
2023-03-02 18:44:13 +00:00
|
|
|
EnforceDeityMode(false);
|
2013-05-26 19:54:43 +00:00
|
|
|
|
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));
|
2023-03-02 18:44:13 +00:00
|
|
|
EnforceDeityMode(false);
|
2013-05-26 19:54:43 +00:00
|
|
|
|
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));
|
2023-03-02 18:44:13 +00:00
|
|
|
EnforceDeityMode(false);
|
2013-05-26 19:54:43 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-05-08 17:01:06 +00:00
|
|
|
/* static */ bool ScriptGoal::DoQuestion(SQInteger uniqueid, uint32_t target, bool is_client, Text *question, QuestionType type, SQInteger buttons)
|
2012-01-03 16:36:24 +00:00
|
|
|
{
|
|
|
|
CCountedPtr<Text> counter(question);
|
|
|
|
|
2023-03-02 18:44:13 +00:00
|
|
|
EnforceDeityMode(false);
|
2019-04-10 21:07:06 +00:00
|
|
|
EnforcePrecondition(false, question != nullptr);
|
2023-07-03 14:22:15 +00:00
|
|
|
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);
|
2023-02-11 18:02:23 +00:00
|
|
|
EnforcePrecondition(false, buttons >= 0 && buttons < (1 << ::GOAL_QUESTION_BUTTON_COUNT));
|
2021-02-02 16:14:26 +00:00
|
|
|
EnforcePrecondition(false, (int)type < ::GQT_END);
|
2023-02-11 18:02:23 +00:00
|
|
|
EnforcePrecondition(false, uniqueid >= 0 && uniqueid <= UINT16_MAX);
|
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
|
|
|
}
|
|
|
|
|
2023-02-11 18:02:23 +00:00
|
|
|
/* static */ bool ScriptGoal::Question(SQInteger uniqueid, ScriptCompany::CompanyID company, Text *question, QuestionType type, SQInteger buttons)
|
2018-04-28 15:37:59 +00:00
|
|
|
{
|
|
|
|
EnforcePrecondition(false, company == ScriptCompany::COMPANY_INVALID || ScriptCompany::ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID);
|
2023-05-08 17:01:06 +00:00
|
|
|
uint8_t c = company;
|
2012-01-03 16:36:24 +00:00
|
|
|
if (company == ScriptCompany::COMPANY_INVALID) c = INVALID_COMPANY;
|
|
|
|
|
2018-04-28 15:37:59 +00:00
|
|
|
return DoQuestion(uniqueid, c, false, question, type, buttons);
|
|
|
|
}
|
|
|
|
|
2023-02-11 18:02:23 +00:00
|
|
|
/* static */ bool ScriptGoal::QuestionClient(SQInteger uniqueid, ScriptClient::ClientID client, Text *question, QuestionType type, SQInteger buttons)
|
2018-04-28 15:37:59 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-02-11 18:02:23 +00:00
|
|
|
/* static */ bool ScriptGoal::CloseQuestion(SQInteger uniqueid)
|
2012-01-03 16:36:24 +00:00
|
|
|
{
|
2023-03-02 18:44:13 +00:00
|
|
|
EnforceDeityMode(false);
|
2023-02-11 18:02:23 +00:00
|
|
|
EnforcePrecondition(false, uniqueid >= 0 && uniqueid <= UINT16_MAX);
|
2012-01-03 16:36:24 +00:00
|
|
|
|
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
|
|
|
}
|