Changeset - r26117:dec8ad7628ec
[Not reviewed]
master
0 9 0
Michael Lutz - 3 years ago 2021-11-20 21:30:56
michi@icosahedron.de
Codechange: Un-bitstuff goal and story page commands.
9 files changed with 119 insertions and 173 deletions:
0 comments (0 inline, 0 general)
src/goal.cpp
Show inline comments
 
@@ -36,49 +36,44 @@ INSTANTIATE_POOL_METHODS(Goal)
 
/**
 
 * Create a new goal.
 
 * @param flags type of operation
 
 * @param tile unused.
 
 * @param p1 various bitstuffed elements
 
 * - p1 = (bit  0 -  7) - GoalType of destination.
 
 * - p1 = (bit  8 - 15) - Company for which this goal is.
 
 * @param p2 GoalTypeID of destination.
 
 * @param company Company for which this goal is.
 
 * @param type GoalType of destination.
 
 * @param dest GoalTypeID of destination.
 
 * @param text Text of the goal.
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdCreateGoal(DoCommandFlag flags, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
 
CommandCost CmdCreateGoal(DoCommandFlag flags, CompanyID company, GoalType type, GoalTypeID dest, const std::string &text)
 
{
 
	if (!Goal::CanAllocateItem()) return CMD_ERROR;
 

	
 
	GoalType type = (GoalType)GB(p1, 0, 8);
 
	CompanyID company = (CompanyID)GB(p1, 8, 8);
 

	
 
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
 
	if (text.empty()) return CMD_ERROR;
 
	if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR;
 

	
 
	switch (type) {
 
		case GT_NONE:
 
			if (p2 != 0) return CMD_ERROR;
 
			if (dest != 0) return CMD_ERROR;
 
			break;
 

	
 
		case GT_TILE:
 
			if (!IsValidTile(p2)) return CMD_ERROR;
 
			if (!IsValidTile(dest)) return CMD_ERROR;
 
			break;
 

	
 
		case GT_INDUSTRY:
 
			if (!Industry::IsValidID(p2)) return CMD_ERROR;
 
			if (!Industry::IsValidID(dest)) return CMD_ERROR;
 
			break;
 

	
 
		case GT_TOWN:
 
			if (!Town::IsValidID(p2)) return CMD_ERROR;
 
			if (!Town::IsValidID(dest)) return CMD_ERROR;
 
			break;
 

	
 
		case GT_COMPANY:
 
			if (!Company::IsValidID(p2)) return CMD_ERROR;
 
			if (!Company::IsValidID(dest)) return CMD_ERROR;
 
			break;
 

	
 
		case GT_STORY_PAGE: {
 
			if (!StoryPage::IsValidID(p2)) return CMD_ERROR;
 
			CompanyID story_company = StoryPage::Get(p2)->company;
 
			if (!StoryPage::IsValidID(dest)) return CMD_ERROR;
 
			CompanyID story_company = StoryPage::Get(dest)->company;
 
			if (company == INVALID_COMPANY ? story_company != INVALID_COMPANY : story_company != INVALID_COMPANY && story_company != company) return CMD_ERROR;
 
			break;
 
		}
 
@@ -89,7 +84,7 @@ CommandCost CmdCreateGoal(DoCommandFlag 
 
	if (flags & DC_EXEC) {
 
		Goal *g = new Goal();
 
		g->type = type;
 
		g->dst = p2;
 
		g->dst = dest;
 
		g->company = company;
 
		g->text = stredup(text.c_str());
 
		g->progress = nullptr;
 
@@ -111,19 +106,16 @@ CommandCost CmdCreateGoal(DoCommandFlag 
 
/**
 
 * Remove a goal.
 
 * @param flags type of operation
 
 * @param tile unused.
 
 * @param p1 GoalID to remove.
 
 * @param p2 unused.
 
 * @param text unused.
 
 * @param goal GoalID to remove.
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdRemoveGoal(DoCommandFlag flags, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
 
CommandCost CmdRemoveGoal(DoCommandFlag flags, GoalID goal)
 
{
 
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
 
	if (!Goal::IsValidID(p1)) return CMD_ERROR;
 
	if (!Goal::IsValidID(goal)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		Goal *g = Goal::Get(p1);
 
		Goal *g = Goal::Get(goal);
 
		CompanyID c = g->company;
 
		delete g;
 

	
 
@@ -141,20 +133,18 @@ CommandCost CmdRemoveGoal(DoCommandFlag 
 
/**
 
 * Update goal text of a goal.
 
 * @param flags type of operation
 
 * @param tile unused.
 
 * @param p1 GoalID to update.
 
 * @param p2 unused
 
 * @param goal GoalID to update.
 
 * @param text Text of the goal.
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdSetGoalText(DoCommandFlag flags, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
 
CommandCost CmdSetGoalText(DoCommandFlag flags, GoalID goal, const std::string &text)
 
{
 
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
 
	if (!Goal::IsValidID(p1)) return CMD_ERROR;
 
	if (!Goal::IsValidID(goal)) return CMD_ERROR;
 
	if (text.empty()) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		Goal *g = Goal::Get(p1);
 
		Goal *g = Goal::Get(goal);
 
		free(g->text);
 
		g->text = stredup(text.c_str());
 

	
 
@@ -171,19 +161,17 @@ CommandCost CmdSetGoalText(DoCommandFlag
 
/**
 
 * Update progress text of a goal.
 
 * @param flags type of operation
 
 * @param tile unused.
 
 * @param p1 GoalID to update.
 
 * @param p2 unused
 
 * @param goal GoalID to update.
 
 * @param text Progress text of the goal.
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdSetGoalProgress(DoCommandFlag flags, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
 
CommandCost CmdSetGoalProgress(DoCommandFlag flags, GoalID goal, const std::string &text)
 
{
 
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
 
	if (!Goal::IsValidID(p1)) return CMD_ERROR;
 
	if (!Goal::IsValidID(goal)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		Goal *g = Goal::Get(p1);
 
		Goal *g = Goal::Get(goal);
 
		free(g->progress);
 
		if (text.empty()) {
 
			g->progress = nullptr;
 
@@ -204,20 +192,18 @@ CommandCost CmdSetGoalProgress(DoCommand
 
/**
 
 * Update completed state of a goal.
 
 * @param flags type of operation
 
 * @param tile unused.
 
 * @param p1 GoalID to update.
 
 * @param p2 completed state. If goal is completed, set to 1, otherwise 0.
 
 * @param text unused
 
 * @param goal GoalID to update.
 
 * @param completed completed state of goal.
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdSetGoalCompleted(DoCommandFlag flags, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
 
CommandCost CmdSetGoalCompleted(DoCommandFlag flags, GoalID goal, bool completed)
 
{
 
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
 
	if (!Goal::IsValidID(p1)) return CMD_ERROR;
 
	if (!Goal::IsValidID(goal)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		Goal *g = Goal::Get(p1);
 
		g->completed = p2 == 1;
 
		Goal *g = Goal::Get(goal);
 
		g->completed = completed;
 

	
 
		if (g->company == INVALID_COMPANY) {
 
			InvalidateWindowClassesData(WC_GOALS_LIST);
 
@@ -232,27 +218,21 @@ CommandCost CmdSetGoalCompleted(DoComman
 
/**
 
 * Ask a goal related question
 
 * @param flags type of operation
 
 * @param tile unused.
 
 * @param p1 various bitstuffed elements
 
 * - p1 = (bit  0 - 15) - Unique ID to use for this question.
 
 * - p1 = (bit 16 - 31) - Company or client for which this question is.
 
 * @param p2 various bitstuffed elements
 
 * - p2 = (bit 0 - 17) - Buttons of the question.
 
 * - p2 = (bit 29 - 30) - Question type.
 
 * - p2 = (bit 31) - Question target: 0 - company, 1 - client.
 
 * @param uniqueid Unique ID to use for this question.
 
 * @param target Company or client for which this question is.
 
 * @param is_client Question target: false - company, true - client.
 
 * @param button_mask Buttons of the question.
 
 * @param type Question type.
 
 * @param text Text of the question.
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdGoalQuestion(DoCommandFlag flags, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
 
CommandCost CmdGoalQuestion(DoCommandFlag flags, uint16 uniqueid, uint16 target, bool is_client, uint32 button_mask, GoalQuestionType type, const std::string &text)
 
{
 
	uint16 uniqueid = (uint16)GB(p1, 0, 16);
 
	CompanyID company = (CompanyID)GB(p1, 16, 8);
 
	ClientID client = (ClientID)GB(p1, 16, 16);
 
	CompanyID company = (CompanyID)target;
 
	ClientID client = (ClientID)target;
 

	
 
	static_assert(GOAL_QUESTION_BUTTON_COUNT < 29);
 
	uint32 button_mask = GB(p2, 0, GOAL_QUESTION_BUTTON_COUNT);
 
	byte type = GB(p2, 29, 2);
 
	bool is_client = HasBit(p2, 31);
 
	button_mask &= (1U << GOAL_QUESTION_BUTTON_COUNT) - 1;
 

	
 
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
 
	if (text.empty()) return CMD_ERROR;
 
@@ -284,31 +264,28 @@ CommandCost CmdGoalQuestion(DoCommandFla
 
/**
 
 * Reply to a goal question.
 
 * @param flags type of operation
 
 * @param tile unused.
 
 * @param p1 Unique ID to use for this question.
 
 * @param p2 Button the company pressed
 
 * @param text Text of the question.
 
 * @param uniqueid Unique ID to use for this question.
 
 * @param button Button the company pressed
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdGoalQuestionAnswer(DoCommandFlag flags, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
 
CommandCost CmdGoalQuestionAnswer(DoCommandFlag flags, uint16 uniqueid, uint8 button)
 
{
 
	if (p1 > UINT16_MAX) return CMD_ERROR;
 
	if (p2 >= GOAL_QUESTION_BUTTON_COUNT) return CMD_ERROR;
 
	if (button >= GOAL_QUESTION_BUTTON_COUNT) return CMD_ERROR;
 

	
 
	if (_current_company == OWNER_DEITY) {
 
		/* It has been requested to close this specific question on all clients */
 
		if (flags & DC_EXEC) CloseWindowById(WC_GOAL_QUESTION, p1);
 
		if (flags & DC_EXEC) CloseWindowById(WC_GOAL_QUESTION, uniqueid);
 
		return CommandCost();
 
	}
 

	
 
	if (_networking && _local_company == _current_company) {
 
		/* Somebody in the same company answered the question. Close the window */
 
		if (flags & DC_EXEC) CloseWindowById(WC_GOAL_QUESTION, p1);
 
		if (flags & DC_EXEC) CloseWindowById(WC_GOAL_QUESTION, uniqueid);
 
		if (!_network_server) return CommandCost();
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		Game::NewEvent(new ScriptEventGoalQuestionAnswer(p1, (ScriptCompany::CompanyID)(byte)_current_company, (ScriptGoal::QuestionButton)(1 << p2)));
 
		Game::NewEvent(new ScriptEventGoalQuestionAnswer(uniqueid, (ScriptCompany::CompanyID)(byte)_current_company, (ScriptGoal::QuestionButton)(1 << button)));
 
	}
 

	
 
	return CommandCost();
src/goal_cmd.h
Show inline comments
 
@@ -11,14 +11,16 @@
 
#define GOAL_CMD_H
 

	
 
#include "command_type.h"
 
#include "command_type.h"
 
#include "goal_type.h"
 

	
 
CommandProc CmdCreateGoal;
 
CommandProc CmdRemoveGoal;
 
CommandProc CmdSetGoalText;
 
CommandProc CmdSetGoalProgress;
 
CommandProc CmdSetGoalCompleted;
 
CommandProc CmdGoalQuestion;
 
CommandProc CmdGoalQuestionAnswer;
 
CommandCost CmdCreateGoal(DoCommandFlag flags, CompanyID company, GoalType type, GoalTypeID dest, const std::string &text);
 
CommandCost CmdRemoveGoal(DoCommandFlag flags, GoalID goal);
 
CommandCost CmdSetGoalText(DoCommandFlag flags, GoalID goal, const std::string &text);
 
CommandCost CmdSetGoalProgress(DoCommandFlag flags, GoalID goal, const std::string &text);
 
CommandCost CmdSetGoalCompleted(DoCommandFlag flags, GoalID goal, bool completed);
 
CommandCost CmdGoalQuestion(DoCommandFlag flags, uint16 uniqueid, uint16 target, bool is_client, uint32 button_mask, GoalQuestionType type, const std::string &text);
 
CommandCost CmdGoalQuestionAnswer(DoCommandFlag flags, uint16 uniqueid, uint8 button);
 

	
 
DEF_CMD_TRAIT(CMD_CREATE_GOAL,          CmdCreateGoal,         CMD_DEITY | CMD_STR_CTRL, CMDT_OTHER_MANAGEMENT)
 
DEF_CMD_TRAIT(CMD_REMOVE_GOAL,          CmdRemoveGoal,         CMD_DEITY,                CMDT_OTHER_MANAGEMENT)
src/goal_gui.cpp
Show inline comments
 
@@ -383,17 +383,17 @@ struct GoalQuestionWindow : public Windo
 
	{
 
		switch (widget) {
 
			case WID_GQ_BUTTON_1:
 
				Command<CMD_GOAL_QUESTION_ANSWER>::Post(0, this->window_number, this->button[0], {});
 
				Command<CMD_GOAL_QUESTION_ANSWER>::Post(this->window_number, this->button[0]);
 
				this->Close();
 
				break;
 

	
 
			case WID_GQ_BUTTON_2:
 
				Command<CMD_GOAL_QUESTION_ANSWER>::Post(0, this->window_number, this->button[1], {});
 
				Command<CMD_GOAL_QUESTION_ANSWER>::Post(this->window_number, this->button[1]);
 
				this->Close();
 
				break;
 

	
 
			case WID_GQ_BUTTON_3:
 
				Command<CMD_GOAL_QUESTION_ANSWER>::Post(0, this->window_number, this->button[2], {});
 
				Command<CMD_GOAL_QUESTION_ANSWER>::Post(this->window_number, this->button[2]);
 
				this->Close();
 
				break;
 
		}
src/script/api/script_goal.cpp
Show inline comments
 
@@ -38,7 +38,7 @@
 
	EnforcePreconditionEncodedText(GOAL_INVALID, text);
 
	EnforcePrecondition(GOAL_INVALID, company == ScriptCompany::COMPANY_INVALID || ScriptCompany::ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID);
 

	
 
	uint8 c = company;
 
	CompanyID c = (::CompanyID)company;
 
	if (company == ScriptCompany::COMPANY_INVALID) c = INVALID_COMPANY;
 
	StoryPage *story_page = nullptr;
 
	if (type == GT_STORY_PAGE && ScriptStoryPage::IsValidStoryPage((ScriptStoryPage::StoryPageID)destination)) story_page = ::StoryPage::Get((ScriptStoryPage::StoryPageID)destination);
 
@@ -50,7 +50,7 @@
 
			(type == GT_COMPANY && ScriptCompany::ResolveCompanyID((ScriptCompany::CompanyID)destination) != ScriptCompany::COMPANY_INVALID) ||
 
			(type == GT_STORY_PAGE && story_page != nullptr && (c == INVALID_COMPANY ? story_page->company == INVALID_COMPANY : story_page->company == INVALID_COMPANY || story_page->company == c)));
 

	
 
	if (!ScriptObject::Command<CMD_CREATE_GOAL>::Do(&ScriptInstance::DoCommandReturnGoalID, 0, type | (c << 8), destination, text)) return GOAL_INVALID;
 
	if (!ScriptObject::Command<CMD_CREATE_GOAL>::Do(&ScriptInstance::DoCommandReturnGoalID, c, (::GoalType)type, destination, text)) return GOAL_INVALID;
 

	
 
	/* In case of test-mode, we return GoalID 0 */
 
	return (ScriptGoal::GoalID)0;
 
@@ -61,7 +61,7 @@
 
	EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
 
	EnforcePrecondition(false, IsValidGoal(goal_id));
 

	
 
	return ScriptObject::Command<CMD_REMOVE_GOAL>::Do(0, goal_id, 0, {});
 
	return ScriptObject::Command<CMD_REMOVE_GOAL>::Do(goal_id);
 
}
 

	
 
/* static */ bool ScriptGoal::SetText(GoalID goal_id, Text *goal)
 
@@ -73,7 +73,7 @@
 
	EnforcePrecondition(false, goal != nullptr);
 
	EnforcePrecondition(false, !StrEmpty(goal->GetEncodedText()));
 

	
 
	return ScriptObject::Command<CMD_SET_GOAL_TEXT>::Do(0, goal_id, 0, goal->GetEncodedText());
 
	return ScriptObject::Command<CMD_SET_GOAL_TEXT>::Do(goal_id, goal->GetEncodedText());
 
}
 

	
 
/* static */ bool ScriptGoal::SetProgress(GoalID goal_id, Text *progress)
 
@@ -88,7 +88,7 @@
 
		progress = nullptr;
 
	}
 

	
 
	return ScriptObject::Command<CMD_SET_GOAL_PROGRESS>::Do(0, goal_id, 0, progress != nullptr ? std::string{ progress->GetEncodedText() } : std::string{});
 
	return ScriptObject::Command<CMD_SET_GOAL_PROGRESS>::Do(goal_id, progress != nullptr ? std::string{ progress->GetEncodedText() } : std::string{});
 
}
 

	
 
/* static */ bool ScriptGoal::SetCompleted(GoalID goal_id, bool completed)
 
@@ -96,7 +96,7 @@
 
	EnforcePrecondition(false, IsValidGoal(goal_id));
 
	EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
 

	
 
	return ScriptObject::Command<CMD_SET_GOAL_COMPLETED>::Do(0, goal_id, completed ? 1 : 0, {});
 
	return ScriptObject::Command<CMD_SET_GOAL_COMPLETED>::Do(goal_id, completed);
 
}
 

	
 
/* static */ bool ScriptGoal::IsCompleted(GoalID goal_id)
 
@@ -121,7 +121,7 @@
 
	EnforcePrecondition(false, buttons < (1 << ::GOAL_QUESTION_BUTTON_COUNT));
 
	EnforcePrecondition(false, (int)type < ::GQT_END);
 

	
 
	return ScriptObject::Command<CMD_GOAL_QUESTION>::Do(0, uniqueid | (target << 16), buttons | (type << 29) | (is_client ? (1 << 31) : 0), text);
 
	return ScriptObject::Command<CMD_GOAL_QUESTION>::Do(uniqueid, target, is_client, buttons, (::GoalQuestionType)type, text);
 
}
 

	
 
/* static */ bool ScriptGoal::Question(uint16 uniqueid, ScriptCompany::CompanyID company, Text *question, QuestionType type, int buttons)
 
@@ -146,5 +146,5 @@
 
{
 
	EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
 

	
 
	return ScriptObject::Command<CMD_GOAL_QUESTION_ANSWER>::Do(0, uniqueid, 0, {});
 
	return ScriptObject::Command<CMD_GOAL_QUESTION_ANSWER>::Do(uniqueid, 0);
 
}
src/script/api/script_story_page.cpp
Show inline comments
 
@@ -49,10 +49,7 @@ static inline bool StoryPageElementTypeR
 
	if (company == ScriptCompany::COMPANY_INVALID) c = INVALID_COMPANY;
 

	
 
	if (!ScriptObject::Command<CMD_CREATE_STORY_PAGE>::Do(&ScriptInstance::DoCommandReturnStoryPageID,
 
		0,
 
		c,
 
		0,
 
		title != nullptr ? std::string{ title->GetEncodedText() } : std::string{})) return STORY_PAGE_INVALID;
 
		(::CompanyID)c, title != nullptr ? std::string{ title->GetEncodedText() } : std::string{})) return STORY_PAGE_INVALID;
 

	
 
	/* In case of test-mode, we return StoryPageID 0 */
 
	return (ScriptStoryPage::StoryPageID)0;
 
@@ -91,7 +88,7 @@ static inline bool StoryPageElementTypeR
 

	
 
	if (!ScriptObject::Command<CMD_CREATE_STORY_PAGE_ELEMENT>::Do(&ScriptInstance::DoCommandReturnStoryPageElementID,
 
			reftile,
 
			story_page_id + (type << 16),
 
			(::StoryPageID)story_page_id, (::StoryPageElementType)type,
 
			refid,
 
			StoryPageElementTypeRequiresText(btype) ? std::string{ text->GetEncodedText() } : std::string{})) return STORY_PAGE_ELEMENT_INVALID;
 

	
 
@@ -160,7 +157,7 @@ static inline bool StoryPageElementTypeR
 
	EnforcePrecondition(false, IsValidStoryPage(story_page_id));
 
	EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
 

	
 
	return ScriptObject::Command<CMD_SET_STORY_PAGE_TITLE>::Do(0, story_page_id, 0, title != nullptr ? std::string{ title->GetEncodedText() } : std::string{});
 
	return ScriptObject::Command<CMD_SET_STORY_PAGE_TITLE>::Do(story_page_id, title != nullptr ? std::string{ title->GetEncodedText() } : std::string{});
 
}
 

	
 
/* static */ ScriptCompany::CompanyID ScriptStoryPage::GetCompany(StoryPageID story_page_id)
 
@@ -186,7 +183,7 @@ static inline bool StoryPageElementTypeR
 
	EnforcePrecondition(false, IsValidStoryPage(story_page_id));
 
	EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
 

	
 
	return ScriptObject::Command<CMD_SET_STORY_PAGE_DATE>::Do(0, story_page_id, date, {});
 
	return ScriptObject::Command<CMD_SET_STORY_PAGE_DATE>::Do(story_page_id, date);
 
}
 

	
 

	
 
@@ -195,7 +192,7 @@ static inline bool StoryPageElementTypeR
 
	EnforcePrecondition(false, IsValidStoryPage(story_page_id));
 
	EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
 

	
 
	return ScriptObject::Command<CMD_SHOW_STORY_PAGE>::Do(0, story_page_id, 0, {});
 
	return ScriptObject::Command<CMD_SHOW_STORY_PAGE>::Do(story_page_id);
 
}
 

	
 
/* static */ bool ScriptStoryPage::Remove(StoryPageID story_page_id)
 
@@ -203,7 +200,7 @@ static inline bool StoryPageElementTypeR
 
	EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
 
	EnforcePrecondition(false, IsValidStoryPage(story_page_id));
 

	
 
	return ScriptObject::Command<CMD_REMOVE_STORY_PAGE>::Do(0, story_page_id, 0, {});
 
	return ScriptObject::Command<CMD_REMOVE_STORY_PAGE>::Do(story_page_id);
 
}
 

	
 
/* static */ bool ScriptStoryPage::RemoveElement(StoryPageElementID story_page_element_id)
 
@@ -211,7 +208,7 @@ static inline bool StoryPageElementTypeR
 
	EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
 
	EnforcePrecondition(false, IsValidStoryPageElement(story_page_element_id));
 

	
 
	return ScriptObject::Command<CMD_REMOVE_STORY_PAGE_ELEMENT>::Do(0, story_page_element_id, 0, {});
 
	return ScriptObject::Command<CMD_REMOVE_STORY_PAGE_ELEMENT>::Do(story_page_element_id);
 
}
 

	
 
/* static */ ScriptStoryPage::StoryPageButtonFormatting ScriptStoryPage::MakePushButtonReference(StoryPageButtonColour colour, StoryPageButtonFlags flags)
src/story.cpp
Show inline comments
 
@@ -198,19 +198,14 @@ bool StoryPageButtonData::ValidateVehicl
 
/**
 
 * Create a new story page.
 
 * @param flags type of operation
 
 * @param tile unused.
 
 * @param p1 various bitstuffed elements
 
 * - p1 = (bit  0 -  7) - Company for which this story page belongs to.
 
 * @param p2 unused.
 
 * @param company Company for which this story page belongs to.
 
 * @param text Title of the story page. Null is allowed in which case a generic page title is provided by OpenTTD.
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdCreateStoryPage(DoCommandFlag flags, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
 
CommandCost CmdCreateStoryPage(DoCommandFlag flags, CompanyID company, const std::string &text)
 
{
 
	if (!StoryPage::CanAllocateItem()) return CMD_ERROR;
 

	
 
	CompanyID company = (CompanyID)GB(p1, 0, 8);
 

	
 
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
 
	if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR;
 

	
 
@@ -244,20 +239,16 @@ CommandCost CmdCreateStoryPage(DoCommand
 
 * Create a new story page element.
 
 * @param flags type of operation
 
 * @param tile Tile location if it is a location page element, otherwise unused.
 
 * @param p1 various bitstuffed elements
 
 * - p1 = (bit  0 -  15) - The page which the element belongs to.
 
 *        (bit  16 -  23) - Page element type
 
 * @param p2 Id of referenced object
 
 * @param page_id The page which the element belongs to.
 
 * @param type Page element type
 
 * @param reference Id of referenced object
 
 * @param text Text content in case it is a text or location page element
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdCreateStoryPageElement(DoCommandFlag flags, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
 
CommandCost CmdCreateStoryPageElement(DoCommandFlag flags, TileIndex tile, StoryPageID page_id, StoryPageElementType type, uint32 reference, const std::string &text)
 
{
 
	if (!StoryPageElement::CanAllocateItem()) return CMD_ERROR;
 

	
 
	StoryPageID page_id = (StoryPageID)GB(p1, 0, 16);
 
	StoryPageElementType type = Extract<StoryPageElementType, 16, 8>(p1);
 

	
 
	/* Allow at most 128 elements per page. */
 
	uint16 element_count = 0;
 
	for (StoryPageElement *iter : StoryPageElement::Iterate()) {
 
@@ -267,7 +258,7 @@ CommandCost CmdCreateStoryPageElement(Do
 

	
 
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
 
	if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
 
	if (!VerifyElementContentParameters(page_id, type, tile, p2, text.c_str())) return CMD_ERROR;
 
	if (!VerifyElementContentParameters(page_id, type, tile, reference, text.c_str())) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		if (_story_page_element_pool.items == 0) {
 
@@ -279,7 +270,7 @@ CommandCost CmdCreateStoryPageElement(Do
 
		pe->sort_value = _story_page_element_next_sort_value;
 
		pe->type = type;
 
		pe->page = page_id;
 
		UpdateElement(*pe, tile, p2, text.c_str());
 
		UpdateElement(*pe, tile, reference, text.c_str());
 

	
 
		InvalidateWindowClassesData(WC_STORY_BOOK, page_id);
 

	
 
@@ -294,17 +285,13 @@ CommandCost CmdCreateStoryPageElement(Do
 
 * Update a new story page element.
 
 * @param flags type of operation
 
 * @param tile Tile location if it is a location page element, otherwise unused.
 
 * @param p1 various bitstuffed elements
 
 * - p1 = (bit  0 -  15) - The page element to update.
 
 *        (bit  16 -  31) - unused
 
 * @param p2 Id of referenced object
 
 * @param page_element_id The page element to update.
 
 * @param reference Id of referenced object
 
 * @param text Text content in case it is a text or location page element
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdUpdateStoryPageElement(DoCommandFlag flags, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
 
CommandCost CmdUpdateStoryPageElement(DoCommandFlag flags, TileIndex tile, StoryPageElementID page_element_id, uint32 reference, const std::string &text)
 
{
 
	StoryPageElementID page_element_id = (StoryPageElementID)GB(p1, 0, 16);
 

	
 
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
 
	if (!StoryPageElement::IsValidID(page_element_id)) return CMD_ERROR;
 

	
 
@@ -312,10 +299,10 @@ CommandCost CmdUpdateStoryPageElement(Do
 
	StoryPageID page_id = pe->page;
 
	StoryPageElementType type = pe->type;
 

	
 
	if (!VerifyElementContentParameters(page_id, type, tile, p2, text.c_str())) return CMD_ERROR;
 
	if (!VerifyElementContentParameters(page_id, type, tile, reference, text.c_str())) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		UpdateElement(*pe, tile, p2, text.c_str());
 
		UpdateElement(*pe, tile, reference, text.c_str());
 
		InvalidateWindowClassesData(WC_STORY_BOOK, pe->page);
 
	}
 

	
 
@@ -325,16 +312,13 @@ CommandCost CmdUpdateStoryPageElement(Do
 
/**
 
 * Update title of a story page.
 
 * @param flags type of operation
 
 * @param tile unused.
 
 * @param p1 = (bit 0 - 15) - StoryPageID to update.
 
 * @param p2 unused
 
 * @param page_id StoryPageID to update.
 
 * @param text title text of the story page.
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdSetStoryPageTitle(DoCommandFlag flags, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
 
CommandCost CmdSetStoryPageTitle(DoCommandFlag flags, StoryPageID page_id, const std::string &text)
 
{
 
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
 
	StoryPageID page_id = (StoryPageID)GB(p1, 0, 16);
 
	if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
@@ -355,18 +339,14 @@ CommandCost CmdSetStoryPageTitle(DoComma
 
/**
 
 * Update date of a story page.
 
 * @param flags type of operation
 
 * @param tile unused.
 
 * @param p1 = (bit 0 - 15) - StoryPageID to update.
 
 * @param p2 = (bit 0 - 31) - date
 
 * @param text unused
 
 * @param page_id StoryPageID to update.
 
 * @param date date
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdSetStoryPageDate(DoCommandFlag flags, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
 
CommandCost CmdSetStoryPageDate(DoCommandFlag flags, StoryPageID page_id, Date date)
 
{
 
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
 
	StoryPageID page_id = (StoryPageID)GB(p1, 0, 16);
 
	if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
 
	Date date = (Date)p2;
 

	
 
	if (flags & DC_EXEC) {
 
		StoryPage *p = StoryPage::Get(page_id);
 
@@ -382,16 +362,12 @@ CommandCost CmdSetStoryPageDate(DoComman
 
 * Display a story page for all clients that are allowed to
 
 * view the story page.
 
 * @param flags type of operation
 
 * @param tile unused.
 
 * @param p1 = (bit 0 - 15) - StoryPageID to show.
 
 * @param p2 unused
 
 * @param text unused
 
 * @param page_id StoryPageID to show.
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdShowStoryPage(DoCommandFlag flags, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
 
CommandCost CmdShowStoryPage(DoCommandFlag flags, StoryPageID page_id)
 
{
 
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
 
	StoryPageID page_id = (StoryPageID)GB(p1, 0, 16);
 
	if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
@@ -404,16 +380,12 @@ CommandCost CmdShowStoryPage(DoCommandFl
 
/**
 
 * Remove a story page and associated story page elements.
 
 * @param flags type of operation
 
 * @param tile unused.
 
 * @param p1 = (bit 0 - 15) - StoryPageID to remove.
 
 * @param p2 unused.
 
 * @param text unused.
 
 * @param page_id StoryPageID to remove.
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdRemoveStoryPage(DoCommandFlag flags, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
 
CommandCost CmdRemoveStoryPage(DoCommandFlag flags, StoryPageID page_id)
 
{
 
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
 
	StoryPageID page_id = (StoryPageID)p1;
 
	if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
@@ -437,16 +409,12 @@ CommandCost CmdRemoveStoryPage(DoCommand
 
/**
 
 * Remove a story page element
 
 * @param flags type of operation
 
 * @param tile unused.
 
 * @param p1 = (bit 0 - 15) - StoryPageElementID to remove.
 
 * @param p2 unused.
 
 * @param text unused.
 
 * @param page_element_id StoryPageElementID to remove.
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdRemoveStoryPageElement(DoCommandFlag flags, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
 
CommandCost CmdRemoveStoryPageElement(DoCommandFlag flags, StoryPageElementID page_element_id)
 
{
 
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
 
	StoryPageElementID page_element_id = (StoryPageElementID)p1;
 
	if (!StoryPageElement::IsValidID(page_element_id)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
@@ -465,15 +433,12 @@ CommandCost CmdRemoveStoryPageElement(Do
 
 * Clicked/used a button on a story page.
 
 * @param flags  Type of operation.
 
 * @param tile   Tile selected, for tile selection buttons, otherwise unused.
 
 * @param p1     Bit 0..15 = story page element id of button.
 
 * @param p2     ID of selected item for buttons that select an item (e.g. vehicle), otherwise unused.
 
 * @param text   Unused.
 
 * @param page_element_id story page element id of button.
 
 * @param reference ID of selected item for buttons that select an item (e.g. vehicle), otherwise unused.
 
 * @return The cost of the operation, or an error.
 
 */
 
CommandCost CmdStoryPageButton(DoCommandFlag flags, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
 
CommandCost CmdStoryPageButton(DoCommandFlag flags, TileIndex tile, StoryPageElementID page_element_id, VehicleID reference)
 
{
 
	StoryPageElementID page_element_id = (StoryPageElementID)GB(p1, 0, 16);
 

	
 
	if (!StoryPageElement::IsValidID(page_element_id)) return CMD_ERROR;
 
	const StoryPageElement *const pe = StoryPageElement::Get(page_element_id);
 

	
 
@@ -491,8 +456,8 @@ CommandCost CmdStoryPageButton(DoCommand
 
			if (flags & DC_EXEC) Game::NewEvent(new ScriptEventStoryPageTileSelect(_current_company, pe->page, page_element_id, tile));
 
			break;
 
		case SPET_BUTTON_VEHICLE:
 
			if (!Vehicle::IsValidID(p2)) return CMD_ERROR;
 
			if (flags & DC_EXEC) Game::NewEvent(new ScriptEventStoryPageVehicleSelect(_current_company, pe->page, page_element_id, (VehicleID)p2));
 
			if (!Vehicle::IsValidID(reference)) return CMD_ERROR;
 
			if (flags & DC_EXEC) Game::NewEvent(new ScriptEventStoryPageVehicleSelect(_current_company, pe->page, page_element_id, reference));
 
			break;
 
		default:
 
			/* Invalid page element type, not a button. */
src/story_cmd.h
Show inline comments
 
@@ -11,16 +11,20 @@
 
#define STORY_CMD_H
 

	
 
#include "command_type.h"
 
#include "company_type.h"
 
#include "date_type.h"
 
#include "story_type.h"
 
#include "vehicle_type.h"
 

	
 
CommandProc CmdCreateStoryPage;
 
CommandProc CmdCreateStoryPageElement;
 
CommandProc CmdUpdateStoryPageElement;
 
CommandProc CmdSetStoryPageTitle;
 
CommandProc CmdSetStoryPageDate;
 
CommandProc CmdShowStoryPage;
 
CommandProc CmdRemoveStoryPage;
 
CommandProc CmdRemoveStoryPageElement;
 
CommandProc CmdStoryPageButton;
 
CommandCost CmdCreateStoryPage(DoCommandFlag flags, CompanyID company, const std::string &text);
 
CommandCost CmdCreateStoryPageElement(DoCommandFlag flags, TileIndex tile, StoryPageID page_id, StoryPageElementType type, uint32 reference, const std::string &text);
 
CommandCost CmdUpdateStoryPageElement(DoCommandFlag flags, TileIndex tile, StoryPageElementID page_element_id, uint32 reference, const std::string &text);
 
CommandCost CmdSetStoryPageTitle(DoCommandFlag flags, StoryPageID page_id, const std::string &text);
 
CommandCost CmdSetStoryPageDate(DoCommandFlag flags, StoryPageID page_id, Date date);
 
CommandCost CmdShowStoryPage(DoCommandFlag flags, StoryPageID page_id);
 
CommandCost CmdRemoveStoryPage(DoCommandFlag flags, StoryPageID page_id);
 
CommandCost CmdRemoveStoryPageElement(DoCommandFlag flags, StoryPageElementID page_element_id);
 
CommandCost CmdStoryPageButton(DoCommandFlag flags, TileIndex tile, StoryPageElementID page_element_id, VehicleID reference);
 

	
 
DEF_CMD_TRAIT(CMD_CREATE_STORY_PAGE,         CmdCreateStoryPage,        CMD_DEITY | CMD_STR_CTRL, CMDT_OTHER_MANAGEMENT)
 
DEF_CMD_TRAIT(CMD_CREATE_STORY_PAGE_ELEMENT, CmdCreateStoryPageElement, CMD_DEITY | CMD_STR_CTRL, CMDT_OTHER_MANAGEMENT)
src/story_gui.cpp
Show inline comments
 
@@ -567,7 +567,7 @@ protected:
 
				this->SetTimeout();
 
				this->SetWidgetDirty(WID_SB_PAGE_PANEL);
 

	
 
				Command<CMD_STORY_PAGE_BUTTON>::Post(0, pe.index, 0, {});
 
				Command<CMD_STORY_PAGE_BUTTON>::Post(0, pe.index, 0);
 
				break;
 

	
 
			case SPET_BUTTON_TILE:
 
@@ -922,7 +922,7 @@ public:
 
			return;
 
		}
 

	
 
		Command<CMD_STORY_PAGE_BUTTON>::Post(tile, pe->index, 0, {});
 
		Command<CMD_STORY_PAGE_BUTTON>::Post(tile, pe->index, 0);
 
		ResetObjectToPlace();
 
	}
 

	
 
@@ -941,7 +941,7 @@ public:
 
		VehicleType wanted_vehtype = data.GetVehicleType();
 
		if (wanted_vehtype != VEH_INVALID && wanted_vehtype != v->type) return false;
 

	
 
		Command<CMD_STORY_PAGE_BUTTON>::Post(0, pe->index, v->index, {});
 
		Command<CMD_STORY_PAGE_BUTTON>::Post(0, pe->index, v->index);
 
		ResetObjectToPlace();
 
		return true;
 
	}
src/story_type.h
Show inline comments
 
@@ -16,6 +16,7 @@ typedef uint16 StoryPageElementID; ///< 
 
typedef uint16 StoryPageID; ///< ID of a story page
 
struct StoryPageElement;
 
struct StoryPage;
 
enum StoryPageElementType : byte;
 

	
 
extern StoryPageElementID _new_story_page_element_id;
 
extern StoryPageID _new_story_page_id;
0 comments (0 inline, 0 general)