Changeset - r26124:7d51c03a3f5e
[Not reviewed]
master
0 23 0
Michael Lutz - 3 years ago 2021-11-30 21:59:23
michi@icosahedron.de
Codechange: Don't use globals for story/goal/sign/group command proc return values.
23 files changed with 105 insertions and 236 deletions:
0 comments (0 inline, 0 general)
src/autoreplace_cmd.cpp
Show inline comments
 
@@ -402,13 +402,13 @@ static CommandCost CopyHeadSpecificThing
 
	CommandCost cost = CommandCost();
 

	
 
	/* Share orders */
 
	if (cost.Succeeded() && old_head != new_head) cost.AddCost(Command<CMD_CLONE_ORDER>::Do(DC_EXEC, CO_SHARE, new_head->index, old_head->index));
 

	
 
	/* Copy group membership */
 
	if (cost.Succeeded() && old_head != new_head) cost.AddCost(Command<CMD_ADD_VEHICLE_GROUP>::Do(DC_EXEC, old_head->group_id, new_head->index, false));
 
	if (cost.Succeeded() && old_head != new_head) cost.AddCost(std::get<0>(Command<CMD_ADD_VEHICLE_GROUP>::Do(DC_EXEC, old_head->group_id, new_head->index, false)));
 

	
 
	/* Perform start/stop check whether the new vehicle suits newgrf restrictions etc. */
 
	if (cost.Succeeded()) {
 
		/* Start the vehicle, might be denied by certain things */
 
		assert((new_head->vehstatus & VS_STOPPED) != 0);
 
		cost.AddCost(DoCmdStartStopVehicle(new_head, true));
src/goal.cpp
Show inline comments
 
@@ -25,63 +25,61 @@
 
#include "network/network_func.h"
 
#include "goal_cmd.h"
 

	
 
#include "safeguards.h"
 

	
 

	
 
GoalID _new_goal_id;
 

	
 
GoalPool _goal_pool("Goal");
 
INSTANTIATE_POOL_METHODS(Goal)
 

	
 
/**
 
 * Create a new goal.
 
 * @param flags type of operation
 
 * @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, CompanyID company, GoalType type, GoalTypeID dest, const std::string &text)
 
std::tuple<CommandCost, GoalID> CmdCreateGoal(DoCommandFlag flags, CompanyID company, GoalType type, GoalTypeID dest, const std::string &text)
 
{
 
	if (!Goal::CanAllocateItem()) return CMD_ERROR;
 
	if (!Goal::CanAllocateItem()) return { CMD_ERROR, INVALID_GOAL };
 

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

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

	
 
		case GT_TILE:
 
			if (!IsValidTile(dest)) return CMD_ERROR;
 
			if (!IsValidTile(dest)) return { CMD_ERROR, INVALID_GOAL };
 
			break;
 

	
 
		case GT_INDUSTRY:
 
			if (!Industry::IsValidID(dest)) return CMD_ERROR;
 
			if (!Industry::IsValidID(dest)) return { CMD_ERROR, INVALID_GOAL };
 
			break;
 

	
 
		case GT_TOWN:
 
			if (!Town::IsValidID(dest)) return CMD_ERROR;
 
			if (!Town::IsValidID(dest)) return { CMD_ERROR, INVALID_GOAL };
 
			break;
 

	
 
		case GT_COMPANY:
 
			if (!Company::IsValidID(dest)) return CMD_ERROR;
 
			if (!Company::IsValidID(dest)) return { CMD_ERROR, INVALID_GOAL };
 
			break;
 

	
 
		case GT_STORY_PAGE: {
 
			if (!StoryPage::IsValidID(dest)) return CMD_ERROR;
 
			if (!StoryPage::IsValidID(dest)) return { CMD_ERROR, INVALID_GOAL };
 
			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;
 
			if (company == INVALID_COMPANY ? story_company != INVALID_COMPANY : story_company != INVALID_COMPANY && story_company != company) return { CMD_ERROR, INVALID_GOAL };
 
			break;
 
		}
 

	
 
		default: return CMD_ERROR;
 
		default: return { CMD_ERROR, INVALID_GOAL };
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		Goal *g = new Goal();
 
		g->type = type;
 
		g->dst = dest;
 
@@ -94,16 +92,16 @@ CommandCost CmdCreateGoal(DoCommandFlag 
 
			InvalidateWindowClassesData(WC_GOALS_LIST);
 
		} else {
 
			InvalidateWindowData(WC_GOALS_LIST, g->company);
 
		}
 
		if (Goal::GetNumItems() == 1) InvalidateWindowData(WC_MAIN_TOOLBAR, 0);
 

	
 
		_new_goal_id = g->index;
 
		return { CommandCost(), g->index };
 
	}
 

	
 
	return CommandCost();
 
	return { CommandCost(), INVALID_GOAL };
 
}
 

	
 
/**
 
 * Remove a goal.
 
 * @param flags type of operation
 
 * @param goal GoalID to remove.
src/goal_cmd.h
Show inline comments
 
@@ -11,13 +11,13 @@
 
#define GOAL_CMD_H
 

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

	
 
CommandCost CmdCreateGoal(DoCommandFlag flags, CompanyID company, GoalType type, GoalTypeID dest, const std::string &text);
 
std::tuple<CommandCost, GoalID> 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);
src/goal_type.h
Show inline comments
 
@@ -34,10 +34,9 @@ enum GoalType : byte {
 

	
 
typedef uint32 GoalTypeID; ///< Contains either tile, industry ID, town ID or company ID (or INVALID_GOALTYPE)
 
static const GoalTypeID INVALID_GOALTYPE = 0xFFFFFFFF; ///< Invalid/unknown index of GoalType
 

	
 
typedef uint16 GoalID; ///< ID of a goal
 
struct Goal;
 

	
 
extern GoalID _new_goal_id;
 
static const GoalID INVALID_GOAL = 0xFFFF; ///< Constant representing a non-existing goal.
 

	
 
#endif /* GOAL_TYPE_H */
src/group.h
Show inline comments
 
@@ -110,9 +110,7 @@ Money GetGroupProfitLastYear(CompanyID c
 
void SetTrainGroupID(Train *v, GroupID grp);
 
void UpdateTrainGroupID(Train *v);
 
void RemoveVehicleFromGroup(const Vehicle *v);
 
void RemoveAllGroupsForCompany(const CompanyID company);
 
bool GroupIsInGroup(GroupID search, GroupID group);
 

	
 
extern GroupID _new_group_id;
 

	
 
#endif /* GROUP_H */
src/group_cmd.cpp
Show inline comments
 
@@ -22,14 +22,12 @@
 
#include "group_cmd.h"
 

	
 
#include "table/strings.h"
 

	
 
#include "safeguards.h"
 

	
 
GroupID _new_group_id;
 

	
 
GroupPool _group_pool("Group");
 
INSTANTIATE_POOL_METHODS(Group)
 

	
 
GroupStatistics::GroupStatistics()
 
{
 
	this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
 
@@ -296,22 +294,22 @@ Group::Group(Owner owner)
 
 * Create a new vehicle group.
 
 * @param flags type of operation
 
 * @param vt vehicle type
 
 * @param parent_group parent groupid
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdCreateGroup(DoCommandFlag flags, VehicleType vt, GroupID parent_group)
 
std::tuple<CommandCost, GroupID> CmdCreateGroup(DoCommandFlag flags, VehicleType vt, GroupID parent_group)
 
{
 
	if (!IsCompanyBuildableVehicleType(vt)) return CMD_ERROR;
 
	if (!IsCompanyBuildableVehicleType(vt)) return { CMD_ERROR, INVALID_GROUP };
 

	
 
	if (!Group::CanAllocateItem()) return CMD_ERROR;
 
	if (!Group::CanAllocateItem()) return { CMD_ERROR, INVALID_GROUP };
 

	
 
	const Group *pg = Group::GetIfValid(parent_group);
 
	if (pg != nullptr) {
 
		if (pg->owner != _current_company) return CMD_ERROR;
 
		if (pg->vehicle_type != vt) return CMD_ERROR;
 
		if (pg->owner != _current_company) return { CMD_ERROR, INVALID_GROUP };
 
		if (pg->vehicle_type != vt) return { CMD_ERROR, INVALID_GROUP };
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		Group *g = new Group(_current_company);
 
		g->vehicle_type = vt;
 
		g->parent = INVALID_GROUP;
 
@@ -325,19 +323,19 @@ CommandCost CmdCreateGroup(DoCommandFlag
 
			g->parent = pg->index;
 
			g->livery.colour1 = pg->livery.colour1;
 
			g->livery.colour2 = pg->livery.colour2;
 
			g->flags = pg->flags;
 
		}
 

	
 
		_new_group_id = g->index;
 

	
 
		InvalidateWindowData(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, _current_company).Pack());
 
		InvalidateWindowData(WC_COMPANY_COLOUR, g->owner, g->vehicle_type);
 

	
 
		return { CommandCost(), g->index };
 
	}
 

	
 
	return CommandCost();
 
	return { CommandCost(), INVALID_GROUP};
 
}
 

	
 

	
 
/**
 
 * Add all vehicles in the given group to the default group and then deletes the group.
 
 * @param flags type of operation
 
@@ -494,32 +492,32 @@ static void AddVehicleToGroup(Vehicle *v
 
 * @param flags type of operation
 
 * @param group_id index of group
 
 * @param veh_id vehicle to add to a group
 
 * @param add_shared Add shared vehicles as well.
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdAddVehicleGroup(DoCommandFlag flags, GroupID group_id, VehicleID veh_id, bool add_shared)
 
std::tuple<CommandCost, GroupID> CmdAddVehicleGroup(DoCommandFlag flags, GroupID group_id, VehicleID veh_id, bool add_shared)
 
{
 
	Vehicle *v = Vehicle::GetIfValid(veh_id);
 
	GroupID new_g = group_id;
 

	
 
	if (v == nullptr || (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP)) return CMD_ERROR;
 
	if (v == nullptr || (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP)) return { CMD_ERROR, INVALID_GROUP };
 

	
 
	if (Group::IsValidID(new_g)) {
 
		Group *g = Group::Get(new_g);
 
		if (g->owner != _current_company || g->vehicle_type != v->type) return CMD_ERROR;
 
		if (g->owner != _current_company || g->vehicle_type != v->type) return { CMD_ERROR, INVALID_GROUP };
 
	}
 

	
 
	if (v->owner != _current_company || !v->IsPrimaryVehicle()) return CMD_ERROR;
 
	if (v->owner != _current_company || !v->IsPrimaryVehicle()) return { CMD_ERROR, INVALID_GROUP };
 

	
 
	if (new_g == NEW_GROUP) {
 
		/* Create new group. */
 
		CommandCost ret = CmdCreateGroup(flags, v->type, INVALID_GROUP);
 
		if (ret.Failed()) return ret;
 
		auto [ret, group_id] = CmdCreateGroup(flags, v->type, INVALID_GROUP);
 
		if (ret.Failed()) return { ret, group_id };
 

	
 
		new_g = _new_group_id;
 
		new_g = group_id;
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		AddVehicleToGroup(v, new_g);
 

	
 
		if (add_shared) {
 
@@ -538,13 +536,13 @@ CommandCost CmdAddVehicleGroup(DoCommand
 
		SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
 
		InvalidateWindowData(GetWindowClassForVehicleType(v->type), VehicleListIdentifier(VL_GROUP_LIST, v->type, _current_company).Pack());
 
		InvalidateWindowData(WC_VEHICLE_VIEW, v->index);
 
		InvalidateWindowData(WC_VEHICLE_DETAILS, v->index);
 
	}
 

	
 
	return CommandCost();
 
	return { CommandCost(), new_g };
 
}
 

	
 
/**
 
 * Add all shared vehicles of all vehicles from a group
 
 * @param flags type of operation
 
 * @param id_g index of group
src/group_cmd.h
Show inline comments
 
@@ -20,16 +20,16 @@ enum GroupFlags : uint8;
 
/** Action for \c CmdAlterGroup. */
 
enum class AlterGroupMode : byte {
 
	Rename,    ///< Change group name.
 
	SetParent, ///< Change group parent.
 
};
 

	
 
CommandCost CmdCreateGroup(DoCommandFlag flags, VehicleType vt, GroupID parent_group);
 
std::tuple<CommandCost, GroupID> CmdCreateGroup(DoCommandFlag flags, VehicleType vt, GroupID parent_group);
 
CommandCost CmdAlterGroup(DoCommandFlag flags, AlterGroupMode mode, GroupID group_id, GroupID parent_id, const std::string &text);
 
CommandCost CmdDeleteGroup(DoCommandFlag flags, GroupID group_id);
 
CommandCost CmdAddVehicleGroup(DoCommandFlag flags, GroupID group_id, VehicleID veh_id, bool add_shared);
 
std::tuple<CommandCost, GroupID> CmdAddVehicleGroup(DoCommandFlag flags, GroupID group_id, VehicleID veh_id, bool add_shared);
 
CommandCost CmdAddSharedVehicleGroup(DoCommandFlag flags, GroupID id_g, VehicleType type);
 
CommandCost CmdRemoveAllVehiclesGroup(DoCommandFlag flags, GroupID group_id);
 
CommandCost CmdSetGroupFlag(DoCommandFlag flags, GroupID group_id, GroupFlags flag, bool value, bool recursive);
 
CommandCost CmdSetGroupLivery(DoCommandFlag flags, GroupID group_id, bool primary, Colours colour);
 

	
 
DEF_CMD_TRAIT(CMD_CREATE_GROUP,              CmdCreateGroup,            0, CMDT_ROUTE_MANAGEMENT)
 
@@ -38,10 +38,10 @@ DEF_CMD_TRAIT(CMD_ALTER_GROUP,          
 
DEF_CMD_TRAIT(CMD_ADD_VEHICLE_GROUP,         CmdAddVehicleGroup,        0, CMDT_ROUTE_MANAGEMENT)
 
DEF_CMD_TRAIT(CMD_ADD_SHARED_VEHICLE_GROUP,  CmdAddSharedVehicleGroup,  0, CMDT_ROUTE_MANAGEMENT)
 
DEF_CMD_TRAIT(CMD_REMOVE_ALL_VEHICLES_GROUP, CmdRemoveAllVehiclesGroup, 0, CMDT_ROUTE_MANAGEMENT)
 
DEF_CMD_TRAIT(CMD_SET_GROUP_FLAG,            CmdSetGroupFlag,           0, CMDT_ROUTE_MANAGEMENT)
 
DEF_CMD_TRAIT(CMD_SET_GROUP_LIVERY,          CmdSetGroupLivery,         0, CMDT_ROUTE_MANAGEMENT)
 

	
 
void CcCreateGroup(Commands cmd, const CommandCost &result, VehicleType vt, GroupID parent_group);
 
void CcAddVehicleNewGroup(Commands cmd, const CommandCost &result, GroupID, VehicleID veh_id, bool);
 
void CcCreateGroup(Commands cmd, const CommandCost &result, GroupID new_group, VehicleType vt, GroupID parent_group);
 
void CcAddVehicleNewGroup(Commands cmd, const CommandCost &result, GroupID new_group, GroupID, VehicleID veh_id, bool);
 

	
 
#endif /* GROUP_CMD_H */
src/group_gui.cpp
Show inline comments
 
@@ -1140,46 +1140,48 @@ static inline VehicleGroupWindow *FindVe
 
}
 

	
 
/**
 
 * Opens a 'Rename group' window for newly created group.
 
 * @param veh_type Vehicle type.
 
 */
 
static void CcCreateGroup(VehicleType veh_type)
 
static void CcCreateGroup(GroupID gid, VehicleType veh_type)
 
{
 
	VehicleGroupWindow *w = FindVehicleGroupWindow(veh_type, _current_company);
 
	if (w != nullptr) w->ShowRenameGroupWindow(_new_group_id, true);
 
	if (w != nullptr) w->ShowRenameGroupWindow(gid, true);
 
}
 

	
 
/**
 
 * Opens a 'Rename group' window for newly created group.
 
 * @param cmd Unused.
 
 * @param result Did command succeed?
 
 * @param new_group ID of the created group.
 
 * @param vt Vehicle type.
 
 * @param parent_group Parent group of the enw group.
 
 * @param parent_group Parent group of the new group.
 
 * @see CmdCreateGroup
 
 */
 
void CcCreateGroup(Commands cmd, const CommandCost &result, VehicleType vt, GroupID parent_group)
 
void CcCreateGroup(Commands cmd, const CommandCost &result, GroupID new_group, VehicleType vt, GroupID parent_group)
 
{
 
	if (result.Failed()) return;
 

	
 
	assert(vt <= VEH_AIRCRAFT);
 
	CcCreateGroup(vt);
 
	CcCreateGroup(new_group, vt);
 
}
 

	
 
/**
 
 * Open rename window after adding a vehicle to a new group via drag and drop.
 
 * @param cmd Unused.
 
 * @param result Did command succeed?
 
 * @param new_group ID of the created group.
 
 * @param veh_id vehicle to add to a group
 
 */
 
void CcAddVehicleNewGroup(Commands cmd, const CommandCost &result, GroupID, VehicleID veh_id, bool)
 
void CcAddVehicleNewGroup(Commands cmd, const CommandCost &result, GroupID new_group, GroupID, VehicleID veh_id, bool)
 
{
 
	if (result.Failed()) return;
 

	
 
	assert(Vehicle::IsValidID(veh_id));
 
	CcCreateGroup(Vehicle::Get(veh_id)->type);
 
	CcCreateGroup(new_group, Vehicle::Get(veh_id)->type);
 
}
 

	
 
/**
 
 * Removes the highlight of a vehicle in a group window
 
 * @param *v Vehicle to remove all highlights from
 
 */
src/script/api/script_object.cpp
Show inline comments
 
@@ -158,17 +158,12 @@ ScriptObject::ActiveInstance::~ActiveIns
 

	
 
/* static */ void ScriptObject::SetLastCommandRes(bool res)
 
{
 
	GetStorage()->last_command_res = res;
 
	/* Also store the results of various global variables */
 
	SetNewVehicleID(_new_vehicle_id);
 
	SetNewSignID(_new_sign_id);
 
	SetNewGroupID(_new_group_id);
 
	SetNewGoalID(_new_goal_id);
 
	SetNewStoryPageID(_new_story_page_id);
 
	SetNewStoryPageElementID(_new_story_page_element_id);
 
}
 

	
 
/* static */ bool ScriptObject::GetLastCommandRes()
 
{
 
	return GetStorage()->last_command_res;
 
}
 
@@ -190,62 +185,12 @@ ScriptObject::ActiveInstance::~ActiveIns
 

	
 
/* static */ VehicleID ScriptObject::GetNewVehicleID()
 
{
 
	return GetStorage()->new_vehicle_id;
 
}
 

	
 
/* static */ void ScriptObject::SetNewSignID(SignID sign_id)
 
{
 
	GetStorage()->new_sign_id = sign_id;
 
}
 

	
 
/* static */ SignID ScriptObject::GetNewSignID()
 
{
 
	return GetStorage()->new_sign_id;
 
}
 

	
 
/* static */ void ScriptObject::SetNewGroupID(GroupID group_id)
 
{
 
	GetStorage()->new_group_id = group_id;
 
}
 

	
 
/* static */ GroupID ScriptObject::GetNewGroupID()
 
{
 
	return GetStorage()->new_group_id;
 
}
 

	
 
/* static */ void ScriptObject::SetNewGoalID(GoalID goal_id)
 
{
 
	GetStorage()->new_goal_id = goal_id;
 
}
 

	
 
/* static */ GroupID ScriptObject::GetNewGoalID()
 
{
 
	return GetStorage()->new_goal_id;
 
}
 

	
 
/* static */ void ScriptObject::SetNewStoryPageID(StoryPageID story_page_id)
 
{
 
	GetStorage()->new_story_page_id = story_page_id;
 
}
 

	
 
/* static */ GroupID ScriptObject::GetNewStoryPageID()
 
{
 
	return GetStorage()->new_story_page_id;
 
}
 

	
 
/* static */ void ScriptObject::SetNewStoryPageElementID(StoryPageElementID story_page_element_id)
 
{
 
	GetStorage()->new_story_page_element_id = story_page_element_id;
 
}
 

	
 
/* static */ GroupID ScriptObject::GetNewStoryPageElementID()
 
{
 
	return GetStorage()->new_story_page_element_id;
 
}
 

	
 
/* static */ void ScriptObject::SetAllowDoCommand(bool allow)
 
{
 
	GetStorage()->allow_do_command = allow;
 
}
 

	
 
/* static */ bool ScriptObject::GetAllowDoCommand()
src/script/api/script_object.hpp
Show inline comments
 
@@ -195,37 +195,12 @@ protected:
 
	/**
 
	 * Get the latest stored new_vehicle_id.
 
	 */
 
	static VehicleID GetNewVehicleID();
 

	
 
	/**
 
	 * Get the latest stored new_sign_id.
 
	 */
 
	static SignID GetNewSignID();
 

	
 
	/**
 
	 * Get the latest stored new_group_id.
 
	 */
 
	static GroupID GetNewGroupID();
 

	
 
	/**
 
	 * Get the latest stored new_goal_id.
 
	 */
 
	static GoalID GetNewGoalID();
 

	
 
	/**
 
	 * Get the latest stored new_story_page_id.
 
	 */
 
	static StoryPageID GetNewStoryPageID();
 

	
 
	/**
 
	 * Get the latest stored new_story_page_id.
 
	 */
 
	static StoryPageID GetNewStoryPageElementID();
 

	
 
	/**
 
	 * Store a allow_do_command per company.
 
	 * @param allow The new allow.
 
	 */
 
	static void SetAllowDoCommand(bool allow);
 

	
 
	/**
 
@@ -302,42 +277,12 @@ private:
 
	/**
 
	 * Store a new_vehicle_id per company.
 
	 * @param vehicle_id The new VehicleID.
 
	 */
 
	static void SetNewVehicleID(VehicleID vehicle_id);
 

	
 
	/**
 
	 * Store a new_sign_id per company.
 
	 * @param sign_id The new SignID.
 
	 */
 
	static void SetNewSignID(SignID sign_id);
 

	
 
	/**
 
	 * Store a new_group_id per company.
 
	 * @param group_id The new GroupID.
 
	 */
 
	static void SetNewGroupID(GroupID group_id);
 

	
 
	/**
 
	 * Store a new_goal_id per company.
 
	 * @param goal_id The new GoalID.
 
	 */
 
	static void SetNewGoalID(GoalID goal_id);
 

	
 
	/**
 
	 * Store a new_story_page_id per company.
 
	 * @param story_page_id The new StoryPageID.
 
	 */
 
	static void SetNewStoryPageID(StoryPageID story_page_id);
 

	
 
	/**
 
	 * Store a new_story_page_id per company.
 
	 * @param story_page_id The new StoryPageID.
 
	 */
 
	static void SetNewStoryPageElementID(StoryPageElementID story_page_element_id);
 

	
 
	/* Helper functions for DoCommand. */
 
	static std::tuple<bool, bool, bool> DoCommandPrep();
 
	static bool DoCommandProcessResult(const CommandCost &res, Script_SuspendCallbackProc *callback, bool estimate_only);
 
	static CommandCallbackData *GetDoCommandCallback();
 
};
 

	
src/script/script_instance.cpp
Show inline comments
 
@@ -23,12 +23,13 @@
 
#include "api/script_event.hpp"
 
#include "api/script_log.hpp"
 

	
 
#include "../company_base.h"
 
#include "../company_func.h"
 
#include "../fileio_func.h"
 
#include "../misc/endian_buffer.hpp"
 

	
 
#include "../safeguards.h"
 

	
 
ScriptStorage::~ScriptStorage()
 
{
 
	/* Free our pointers */
 
@@ -271,33 +272,33 @@ void ScriptInstance::CollectGarbage()
 
{
 
	instance->engine->InsertResult(ScriptObject::GetNewVehicleID());
 
}
 

	
 
/* static */ void ScriptInstance::DoCommandReturnSignID(ScriptInstance *instance)
 
{
 
	instance->engine->InsertResult(ScriptObject::GetNewSignID());
 
	instance->engine->InsertResult(EndianBufferReader::ToValue<SignID>(ScriptObject::GetLastCommandResData()));
 
}
 

	
 
/* static */ void ScriptInstance::DoCommandReturnGroupID(ScriptInstance *instance)
 
{
 
	instance->engine->InsertResult(ScriptObject::GetNewGroupID());
 
	instance->engine->InsertResult(EndianBufferReader::ToValue<GroupID>(ScriptObject::GetLastCommandResData()));
 
}
 

	
 
/* static */ void ScriptInstance::DoCommandReturnGoalID(ScriptInstance *instance)
 
{
 
	instance->engine->InsertResult(ScriptObject::GetNewGoalID());
 
	instance->engine->InsertResult(EndianBufferReader::ToValue<GoalID>(ScriptObject::GetLastCommandResData()));
 
}
 

	
 
/* static */ void ScriptInstance::DoCommandReturnStoryPageID(ScriptInstance *instance)
 
{
 
	instance->engine->InsertResult(ScriptObject::GetNewStoryPageID());
 
	instance->engine->InsertResult(EndianBufferReader::ToValue<StoryPageID>(ScriptObject::GetLastCommandResData()));
 
}
 

	
 
/* static */ void ScriptInstance::DoCommandReturnStoryPageElementID(ScriptInstance *instance)
 
{
 
	instance->engine->InsertResult(ScriptObject::GetNewStoryPageElementID());
 
	instance->engine->InsertResult(EndianBufferReader::ToValue<StoryPageElementID>(ScriptObject::GetLastCommandResData()));
 
}
 

	
 
ScriptStorage *ScriptInstance::GetStorage()
 
{
 
	return this->storage;
 
}
src/script/script_storage.hpp
Show inline comments
 
@@ -47,17 +47,12 @@ private:
 
	TileIndex last_tile;             ///< The last tile passed to a command.
 
	CommandDataBuffer last_data;     ///< The last data passed to a command.
 
	Commands last_cmd;               ///< The last cmd passed to a command.
 
	CommandDataBuffer last_cmd_ret;  ///< The extra data returned by the last command.
 

	
 
	VehicleID new_vehicle_id;        ///< The ID of the new Vehicle.
 
	SignID new_sign_id;              ///< The ID of the new Sign.
 
	GroupID new_group_id;            ///< The ID of the new Group.
 
	GoalID new_goal_id;              ///< The ID of the new Goal.
 
	StoryPageID new_story_page_id;   ///< The ID of the new StoryPage.
 
	StoryPageID new_story_page_element_id; ///< The ID of the new StoryPageElement.
 

	
 
	std::vector<int> callback_value; ///< The values which need to survive a callback.
 

	
 
	RoadType road_type;              ///< The current roadtype we build.
 
	RailType rail_type;              ///< The current railtype we build.
 

	
 
@@ -76,17 +71,12 @@ public:
 
		last_cost         (0),
 
		last_error        (STR_NULL),
 
		last_command_res  (true),
 
		last_tile         (INVALID_TILE),
 
		last_cmd          (CMD_END),
 
		new_vehicle_id    (0),
 
		new_sign_id       (0),
 
		new_group_id      (0),
 
		new_goal_id       (0),
 
		new_story_page_id (0),
 
		new_story_page_element_id(0),
 
		/* calback_value (can't be set) */
 
		road_type         (INVALID_ROADTYPE),
 
		rail_type         (INVALID_RAILTYPE),
 
		event_data        (nullptr),
 
		log_data          (nullptr)
 
	{ }
src/signs_cmd.cpp
Show inline comments
 
@@ -20,31 +20,28 @@
 
#include "signs_cmd.h"
 

	
 
#include "table/strings.h"
 

	
 
#include "safeguards.h"
 

	
 
/** The last built sign. */
 
SignID _new_sign_id;
 

	
 
/**
 
 * Place a sign at the given coordinates. Ownership of sign has
 
 * no effect whatsoever except for the colour the sign gets for easy recognition,
 
 * but everybody is able to rename/remove it.
 
 * @param tile tile to place sign at
 
 * @param flags type of operation
 
 * @param text contents of the sign
 
 * @return the cost of this operation or an error
 
 * @return the cost of this operation + the ID of the new sign or an error
 
 */
 
CommandCost CmdPlaceSign(DoCommandFlag flags, TileIndex tile, const std::string &text)
 
std::tuple<CommandCost, SignID> CmdPlaceSign(DoCommandFlag flags, TileIndex tile, const std::string &text)
 
{
 
	/* Try to locate a new sign */
 
	if (!Sign::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_SIGNS);
 
	if (!Sign::CanAllocateItem()) return { CommandCost(STR_ERROR_TOO_MANY_SIGNS), INVALID_SIGN };
 

	
 
	/* Check sign text length if any */
 
	if (Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return CMD_ERROR;
 
	if (Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return { CMD_ERROR, INVALID_SIGN };
 

	
 
	/* When we execute, really make the sign */
 
	if (flags & DC_EXEC) {
 
		Sign *si = new Sign(_game_mode == GM_EDITOR ? OWNER_DEITY : _current_company);
 
		int x = TileX(tile) * TILE_SIZE;
 
		int y = TileY(tile) * TILE_SIZE;
 
@@ -54,16 +51,16 @@ CommandCost CmdPlaceSign(DoCommandFlag f
 
		si->z = GetSlopePixelZ(x, y);
 
		if (!text.empty()) {
 
			si->name = text;
 
		}
 
		si->UpdateVirtCoord();
 
		InvalidateWindowData(WC_SIGN_LIST, 0, 0);
 
		_new_sign_id = si->index;
 
		return { CommandCost(), si->index };
 
	}
 

	
 
	return CommandCost();
 
	return { CommandCost(), INVALID_SIGN };
 
}
 

	
 
/**
 
 * Rename a sign. If the new name of the sign is empty, we assume
 
 * the user wanted to delete it. So delete it. Ownership of signs
 
 * has no meaning/effect whatsoever except for eyecandy
 
@@ -104,19 +101,19 @@ CommandCost CmdRenameSign(DoCommandFlag 
 
}
 

	
 
/**
 
 * Callback function that is called after a sign is placed
 
 * @param cmd unused
 
 * @param result of the operation
 
 * @param tile unused
 
 * @param new_sign ID of the placed sign.
 
 */
 
void CcPlaceSign(Commands cmd, const CommandCost &result, TileIndex tile)
 
void CcPlaceSign(Commands cmd, const CommandCost &result, SignID new_sign)
 
{
 
	if (result.Failed()) return;
 

	
 
	ShowRenameSignWindow(Sign::Get(_new_sign_id));
 
	ShowRenameSignWindow(Sign::Get(new_sign));
 
	ResetObjectToPlace();
 
}
 

	
 
/**
 
 *
 
 * PlaceProc function, called when someone pressed the button if the
src/signs_cmd.h
Show inline comments
 
@@ -10,13 +10,15 @@
 
#ifndef SIGNS_CMD_H
 
#define SIGNS_CMD_H
 

	
 
#include "command_type.h"
 
#include "signs_type.h"
 

	
 
CommandCost CmdPlaceSign(DoCommandFlag flags, TileIndex tile, const std::string &text);
 
std::tuple<CommandCost, SignID> CmdPlaceSign(DoCommandFlag flags, TileIndex tile, const std::string &text);
 
CommandCost CmdRenameSign(DoCommandFlag flags, SignID sign_id, const std::string &text);
 

	
 
DEF_CMD_TRAIT(CMD_PLACE_SIGN,  CmdPlaceSign,  CMD_DEITY, CMDT_OTHER_MANAGEMENT)
 
DEF_CMD_TRAIT(CMD_RENAME_SIGN, CmdRenameSign, CMD_DEITY, CMDT_OTHER_MANAGEMENT)
 

	
 
void CcPlaceSign(Commands cmd, const CommandCost &result, SignID new_sign);
 

	
 
#endif /* SIGNS_CMD_H */
src/signs_func.h
Show inline comments
 
@@ -11,13 +11,12 @@
 
#define SIGNS_FUNC_H
 

	
 
#include "signs_type.h"
 
#include "tile_type.h"
 

	
 
struct Window;
 
extern SignID _new_sign_id;
 

	
 
void UpdateAllSignVirtCoords();
 
void PlaceProc_Sign(TileIndex tile);
 
bool CompanyCanRenameSign(const Sign *si);
 

	
 
/* signs_gui.cpp */
src/story.cpp
Show inline comments
 
@@ -27,14 +27,12 @@
 
#include "script/api/script_event_types.hpp"
 
#include "story_cmd.h"
 

	
 
#include "safeguards.h"
 

	
 

	
 
StoryPageElementID _new_story_page_element_id;
 
StoryPageID _new_story_page_id;
 
uint32 _story_page_element_next_sort_value;
 
uint32 _story_page_next_sort_value;
 

	
 
StoryPageElementPool _story_page_element_pool("StoryPageElement");
 
StoryPagePool _story_page_pool("StoryPage");
 
INSTANTIATE_POOL_METHODS(StoryPageElement)
 
@@ -199,18 +197,18 @@ bool StoryPageButtonData::ValidateVehicl
 
 * Create a new story page.
 
 * @param flags type of operation
 
 * @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, CompanyID company, const std::string &text)
 
std::tuple<CommandCost, StoryPageID> CmdCreateStoryPage(DoCommandFlag flags, CompanyID company, const std::string &text)
 
{
 
	if (!StoryPage::CanAllocateItem()) return CMD_ERROR;
 
	if (!StoryPage::CanAllocateItem()) return { CMD_ERROR, INVALID_STORY_PAGE };
 

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

	
 
	if (flags & DC_EXEC) {
 
		if (_story_page_pool.items == 0) {
 
			/* Initialize the next sort value variable. */
 
			_story_page_next_sort_value = 0;
 
		}
 
@@ -225,43 +223,44 @@ CommandCost CmdCreateStoryPage(DoCommand
 
			s->title = stredup(text.c_str());
 
		}
 

	
 
		InvalidateWindowClassesData(WC_STORY_BOOK, -1);
 
		if (StoryPage::GetNumItems() == 1) InvalidateWindowData(WC_MAIN_TOOLBAR, 0);
 

	
 
		_new_story_page_id = s->index;
 
		_story_page_next_sort_value++;
 
		return { CommandCost(), s->index };
 
	}
 

	
 
	return CommandCost();
 
	return { CommandCost(), INVALID_STORY_PAGE };
 
}
 

	
 
/**
 
 * 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 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, StoryPageID page_id, StoryPageElementType type, uint32 reference, const std::string &text)
 
std::tuple<CommandCost, StoryPageElementID> CmdCreateStoryPageElement(DoCommandFlag flags, TileIndex tile, StoryPageID page_id, StoryPageElementType type, uint32 reference, const std::string &text)
 
{
 
	if (!StoryPageElement::CanAllocateItem()) return CMD_ERROR;
 
	if (!StoryPageElement::CanAllocateItem()) return { CMD_ERROR, INVALID_STORY_PAGE_ELEMENT };
 

	
 
	/* Allow at most 128 elements per page. */
 
	uint16 element_count = 0;
 
	for (StoryPageElement *iter : StoryPageElement::Iterate()) {
 
		if (iter->page == page_id) element_count++;
 
	}
 
	if (element_count >= 128) return CMD_ERROR;
 
	if (element_count >= 128) return { CMD_ERROR, INVALID_STORY_PAGE_ELEMENT };
 

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

	
 

	
 
	if (flags & DC_EXEC) {
 
		if (_story_page_element_pool.items == 0) {
 
			/* Initialize the next sort value variable. */
 
			_story_page_element_next_sort_value = 0;
 
		}
 
@@ -271,17 +270,17 @@ CommandCost CmdCreateStoryPageElement(Do
 
		pe->type = type;
 
		pe->page = page_id;
 
		UpdateElement(*pe, tile, reference, text.c_str());
 

	
 
		InvalidateWindowClassesData(WC_STORY_BOOK, page_id);
 

	
 
		_new_story_page_element_id = pe->index;
 
		_story_page_element_next_sort_value++;
 
		return { CommandCost(), pe->index };
 
	}
 

	
 
	return CommandCost();
 
	return { CommandCost(), INVALID_STORY_PAGE_ELEMENT };
 
}
 

	
 
/**
 
 * Update a new story page element.
 
 * @param flags type of operation
 
 * @param tile Tile location if it is a location page element, otherwise unused.
src/story_cmd.h
Show inline comments
 
@@ -13,14 +13,14 @@
 
#include "command_type.h"
 
#include "company_type.h"
 
#include "date_type.h"
 
#include "story_type.h"
 
#include "vehicle_type.h"
 

	
 
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);
 
std::tuple<CommandCost, StoryPageID> CmdCreateStoryPage(DoCommandFlag flags, CompanyID company, const std::string &text);
 
std::tuple<CommandCost, StoryPageElementID> 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);
src/story_type.h
Show inline comments
 
@@ -15,13 +15,11 @@
 
typedef uint16 StoryPageElementID; ///< ID of a story page element
 
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;
 
static const StoryPageElementID INVALID_STORY_PAGE_ELEMENT = 0xFFFF; ///< Constant representing a non-existing story page element.
 
static const StoryPageID INVALID_STORY_PAGE = 0xFFFF; ///< Constant representing a non-existing story page.
 

	
 
#endif /* STORY_TYPE_H */
 

	
src/terraform_cmd.h
Show inline comments
 
@@ -18,10 +18,9 @@ CommandCost CmdTerraformLand(DoCommandFl
 
CommandCost CmdLevelLand(DoCommandFlag flags, TileIndex tile, TileIndex start_tile, bool diagonal, LevelMode lm);
 

	
 
DEF_CMD_TRAIT(CMD_TERRAFORM_LAND, CmdTerraformLand, CMD_ALL_TILES | CMD_AUTO,               CMDT_LANDSCAPE_CONSTRUCTION)
 
DEF_CMD_TRAIT(CMD_LEVEL_LAND,     CmdLevelLand,     CMD_ALL_TILES | CMD_AUTO | CMD_NO_TEST, CMDT_LANDSCAPE_CONSTRUCTION) // test run might clear tiles multiple times, in execution that only happens once
 

	
 
CommandCallback CcPlaySound_EXPLOSION;
 
CommandCallback CcPlaceSign;
 
CommandCallback CcTerraform;
 

	
 
#endif /* TERRAFORM_CMD_H */
src/town.h
Show inline comments
 
@@ -225,13 +225,12 @@ enum TownActions {
 
	TACT_FUNDS            = TACT_BUY_RIGHTS | TACT_BRIBE,                                        ///< All possible funding actions.
 
	TACT_ALL              = TACT_ADVERTISE | TACT_CONSTRUCTION | TACT_FUNDS,                     ///< All possible actions.
 
};
 
DECLARE_ENUM_AS_BIT_SET(TownActions)
 

	
 
extern const byte _town_action_costs[TACT_COUNT];
 
extern TownID _new_town_id;
 

	
 
/**
 
 * Set the default name for a depot/waypoint
 
 * @tparam T The type/class to make a default name for
 
 * @param obj The object/instance we want to find the name for
 
 */
src/town_cmd.cpp
Show inline comments
 
@@ -56,14 +56,12 @@
 

	
 
#include "table/strings.h"
 
#include "table/town_land.h"
 

	
 
#include "safeguards.h"
 

	
 
TownID _new_town_id;
 

	
 
/* Initialize the town-pool */
 
TownPool _town_pool("Town");
 
INSTANTIATE_POOL_METHODS(Town)
 

	
 

	
 
TownKdtree _town_kdtree(&Kdtree_TownXYFunc);
 
@@ -1934,74 +1932,75 @@ static bool IsUniqueTownName(const std::
 
 * @param layout town road layout (@see TownLayout)
 
 * @param random_location use random location (randomize \c tile )
 
 * @param townnameparts town name parts
 
 * @param text Custom name for the town. If empty, the town name parts will be used.
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdFoundTown(DoCommandFlag flags, TileIndex tile, TownSize size, bool city, TownLayout layout, bool random_location, uint32 townnameparts, const std::string &text)
 
std::tuple<CommandCost, TownID> CmdFoundTown(DoCommandFlag flags, TileIndex tile, TownSize size, bool city, TownLayout layout, bool random_location, uint32 townnameparts, const std::string &text)
 
{
 
	TownNameParams par(_settings_game.game_creation.town_name);
 

	
 
	if (size >= TSZ_END) return CMD_ERROR;
 
	if (layout >= NUM_TLS) return CMD_ERROR;
 
	if (size >= TSZ_END) return { CMD_ERROR, INVALID_TOWN };
 
	if (layout >= NUM_TLS) return { CMD_ERROR, INVALID_TOWN };
 

	
 
	/* Some things are allowed only in the scenario editor and for game scripts. */
 
	if (_game_mode != GM_EDITOR && _current_company != OWNER_DEITY) {
 
		if (_settings_game.economy.found_town == TF_FORBIDDEN) return CMD_ERROR;
 
		if (size == TSZ_LARGE) return CMD_ERROR;
 
		if (random_location) return CMD_ERROR;
 
		if (_settings_game.economy.found_town == TF_FORBIDDEN) return { CMD_ERROR, INVALID_TOWN };
 
		if (size == TSZ_LARGE) return { CMD_ERROR, INVALID_TOWN };
 
		if (random_location) return { CMD_ERROR, INVALID_TOWN };
 
		if (_settings_game.economy.found_town != TF_CUSTOM_LAYOUT && layout != _settings_game.economy.town_layout) {
 
			return CMD_ERROR;
 
			return { CMD_ERROR, INVALID_TOWN };
 
		}
 
	} else if (_current_company == OWNER_DEITY && random_location) {
 
		/* Random parameter is not allowed for Game Scripts. */
 
		return CMD_ERROR;
 
		return { CMD_ERROR, INVALID_TOWN };
 
	}
 

	
 
	if (text.empty()) {
 
		/* If supplied name is empty, townnameparts has to generate unique automatic name */
 
		if (!VerifyTownName(townnameparts, &par)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
 
		if (!VerifyTownName(townnameparts, &par)) return { CommandCost(STR_ERROR_NAME_MUST_BE_UNIQUE), INVALID_TOWN };
 
	} else {
 
		/* If name is not empty, it has to be unique custom name */
 
		if (Utf8StringLength(text) >= MAX_LENGTH_TOWN_NAME_CHARS) return CMD_ERROR;
 
		if (!IsUniqueTownName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
 
		if (Utf8StringLength(text) >= MAX_LENGTH_TOWN_NAME_CHARS) return { CMD_ERROR, INVALID_TOWN };
 
		if (!IsUniqueTownName(text)) return { CommandCost(STR_ERROR_NAME_MUST_BE_UNIQUE), INVALID_TOWN };
 
	}
 

	
 
	/* Allocate town struct */
 
	if (!Town::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_TOWNS);
 
	if (!Town::CanAllocateItem()) return { CommandCost(STR_ERROR_TOO_MANY_TOWNS), INVALID_TOWN };
 

	
 
	if (!random_location) {
 
		CommandCost ret = TownCanBePlacedHere(tile);
 
		if (ret.Failed()) return ret;
 
		if (ret.Failed()) return { ret, INVALID_TOWN };
 
	}
 

	
 
	static const byte price_mult[][TSZ_RANDOM + 1] = {{ 15, 25, 40, 25 }, { 20, 35, 55, 35 }};
 
	/* multidimensional arrays have to have defined length of non-first dimension */
 
	static_assert(lengthof(price_mult[0]) == 4);
 

	
 
	CommandCost cost(EXPENSES_OTHER, _price[PR_BUILD_TOWN]);
 
	byte mult = price_mult[city][size];
 

	
 
	cost.MultiplyCost(mult);
 

	
 
	/* Create the town */
 
	TownID new_town = INVALID_TOWN;
 
	if (flags & DC_EXEC) {
 
		if (cost.GetCost() > GetAvailableMoneyForCommand()) {
 
			_additional_cash_required = cost.GetCost();
 
			return CommandCost(EXPENSES_OTHER);
 
			return { CommandCost(EXPENSES_OTHER), INVALID_TOWN };
 
		}
 

	
 
		Backup<bool> old_generating_world(_generating_world, true, FILE_LINE);
 
		UpdateNearestTownForRoadTiles(true);
 
		Town *t;
 
		if (random_location) {
 
			t = CreateRandomTown(20, townnameparts, size, city, layout);
 
			if (t == nullptr) {
 
				cost = CommandCost(STR_ERROR_NO_SPACE_FOR_TOWN);
 
			} else {
 
				_new_town_id = t->index;
 
				new_town = t->index;
 
			}
 
		} else {
 
			t = new Town(tile);
 
			DoCreateTown(t, tile, townnameparts, size, city, layout, true);
 
		}
 
		UpdateNearestTownForRoadTiles(false);
 
@@ -2029,13 +2028,13 @@ CommandCost CmdFoundTown(DoCommandFlag f
 
				AddTileNewsItem(STR_NEWS_NEW_TOWN, NT_INDUSTRY_OPEN, tile, company_name);
 
			}
 
			AI::BroadcastNewEvent(new ScriptEventTownFounded(t->index));
 
			Game::NewEvent(new ScriptEventTownFounded(t->index));
 
		}
 
	}
 
	return cost;
 
	return { cost, new_town };
 
}
 

	
 
/**
 
 * Towns must all be placed on the same grid or when they eventually
 
 * interpenetrate their road networks will not mesh nicely; this
 
 * function adjusts a tile so that it aligns properly.
src/town_cmd.h
Show inline comments
 
@@ -13,13 +13,13 @@
 
#include "command_type.h"
 
#include "company_type.h"
 
#include "town_type.h"
 

	
 
enum TownEffect : byte;
 

	
 
CommandCost CmdFoundTown(DoCommandFlag flags, TileIndex tile, TownSize size, bool city, TownLayout layout, bool random_location, uint32 townnameparts, const std::string &text);
 
std::tuple<CommandCost, TownID> CmdFoundTown(DoCommandFlag flags, TileIndex tile, TownSize size, bool city, TownLayout layout, bool random_location, uint32 townnameparts, const std::string &text);
 
CommandCost CmdRenameTown(DoCommandFlag flags, TownID town_id, const std::string &text);
 
CommandCost CmdDoTownAction(DoCommandFlag flags, TownID town_id, uint8 action);
 
CommandCost CmdTownGrowthRate(DoCommandFlag flags, TownID town_id, uint16 growth_rate);
 
CommandCost CmdTownRating(DoCommandFlag flags, TownID town_id, CompanyID company_id, int16 rating);
 
CommandCost CmdTownCargoGoal(DoCommandFlag flags, TownID town_id, TownEffect te, uint32 goal);
 
CommandCost CmdTownSetText(DoCommandFlag flags, TownID town_id, const std::string &text);
 
@@ -34,9 +34,9 @@ DEF_CMD_TRAIT(CMD_TOWN_GROWTH_RATE, CmdT
 
DEF_CMD_TRAIT(CMD_TOWN_RATING,      CmdTownRating,     CMD_DEITY,                CMDT_OTHER_MANAGEMENT)
 
DEF_CMD_TRAIT(CMD_TOWN_SET_TEXT,    CmdTownSetText,    CMD_DEITY | CMD_STR_CTRL, CMDT_OTHER_MANAGEMENT)
 
DEF_CMD_TRAIT(CMD_EXPAND_TOWN,      CmdExpandTown,     CMD_DEITY,                CMDT_LANDSCAPE_CONSTRUCTION)
 
DEF_CMD_TRAIT(CMD_DELETE_TOWN,      CmdDeleteTown,     CMD_OFFLINE,              CMDT_LANDSCAPE_CONSTRUCTION)
 

	
 
CommandCallback CcFoundTown;
 
CommandCallback CcFoundRandomTown;
 
void CcFoundRandomTown(Commands cmd, const CommandCost &result, TownID town_id);
 

	
 
#endif /* TOWN_CMD_H */
src/town_gui.cpp
Show inline comments
 
@@ -1014,15 +1014,15 @@ void CcFoundTown(Commands cmd, const Com
 
	if (result.Failed()) return;
 

	
 
	if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_CONSTRUCTION_OTHER, tile);
 
	if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
 
}
 

	
 
void CcFoundRandomTown(Commands cmd, const CommandCost &result, TileIndex tile)
 
void CcFoundRandomTown(Commands cmd, const CommandCost &result, TownID town_id)
 
{
 
	if (result.Succeeded()) ScrollMainWindowToTile(Town::Get(_new_town_id)->xy);
 
	if (result.Succeeded()) ScrollMainWindowToTile(Town::Get(town_id)->xy);
 
}
 

	
 
static const NWidgetPart _nested_found_town_widgets[] = {
 
	NWidget(NWID_HORIZONTAL),
 
		NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
 
		NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FOUND_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
 
@@ -1147,13 +1147,14 @@ public:
 
			this->SetWidgetLoweredState(i, i == WID_TF_LAYOUT_ORIGINAL + this->town_layout);
 
		}
 

	
 
		this->SetDirty();
 
	}
 

	
 
	void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, CommandCallback cc)
 
	template <typename Tcallback>
 
	void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, Tcallback cc)
 
	{
 
		std::string name;
 

	
 
		if (!this->townnamevalid) {
 
			name = this->townname_editbox.text.buf;
 
		} else {
0 comments (0 inline, 0 general)