mirror of https://github.com/OpenTTD/OpenTTD
Change: Allow a few more GS functions to work without needing to be in CompanyMode
ScriptGroup::IsValidGroup, ScriptGroupList, ScriptVehicleList_Group and ScriptVehicleList_DefaultGroup lets GS to get groups of any company without being in the company. ScriptGroup::GetNumEngines, ScriptGroup::GetNumVehicles and ScriptGroup::GetEngineReplacement have extra conditions to make it less restrictive to use when in deity mode. If the group exists and is not GROUP_DEFAULT and not GROUP_ALL, then the company of that group could be retrieved from the group->owner itself, without the need to be in company scope, but since those functions can also be supplied with GROUP_DEFAULT and GROUP_ALL as parameters, you need to be in a valid company scope mode for those.pull/10411/head
parent
68a9f6e774
commit
7c6561259c
|
@ -28,7 +28,7 @@
|
|||
{
|
||||
EnforceDeityOrCompanyModeValid(false);
|
||||
const Group *g = ::Group::GetIfValid(group_id);
|
||||
return g != nullptr && g->owner == ScriptObject::GetCompany();
|
||||
return g != nullptr && (g->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity());
|
||||
}
|
||||
|
||||
/* static */ ScriptCompany::CompanyID ScriptGroup::GetOwner(GroupID group_id)
|
||||
|
@ -117,23 +117,29 @@
|
|||
|
||||
/* static */ SQInteger ScriptGroup::GetNumEngines(GroupID group_id, EngineID engine_id)
|
||||
{
|
||||
EnforceCompanyModeValid(-1);
|
||||
if (group_id == GROUP_DEFAULT || group_id == GROUP_ALL) EnforceCompanyModeValid(-1);
|
||||
EnforceDeityOrCompanyModeValid(-1);
|
||||
if (!ScriptEngine::IsValidEngine(engine_id)) return -1;
|
||||
bool valid_group = IsValidGroup(group_id);
|
||||
if (!valid_group && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return -1;
|
||||
if (valid_group && ScriptEngine::GetVehicleType(engine_id) != GetVehicleType(group_id)) return -1;
|
||||
|
||||
return GetGroupNumEngines(ScriptObject::GetCompany(), group_id, engine_id);
|
||||
::CompanyID company = (valid_group && ScriptCompanyMode::IsDeity()) ? ::Group::Get(group_id)->owner : ScriptObject::GetCompany();
|
||||
|
||||
return GetGroupNumEngines(company, group_id, engine_id);
|
||||
}
|
||||
|
||||
/* static */ SQInteger ScriptGroup::GetNumVehicles(GroupID group_id, ScriptVehicle::VehicleType vehicle_type)
|
||||
{
|
||||
EnforceCompanyModeValid(-1);
|
||||
if (group_id == GROUP_DEFAULT || group_id == GROUP_ALL) EnforceCompanyModeValid(-1);
|
||||
EnforceDeityOrCompanyModeValid(-1);
|
||||
bool valid_group = IsValidGroup(group_id);
|
||||
if (!valid_group && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return -1;
|
||||
if (!valid_group && (vehicle_type < ScriptVehicle::VT_RAIL || vehicle_type > ScriptVehicle::VT_AIR)) return -1;
|
||||
|
||||
return GetGroupNumVehicle(ScriptObject::GetCompany(), group_id, valid_group ? ::Group::Get(group_id)->vehicle_type : (::VehicleType)vehicle_type);
|
||||
::CompanyID company = (valid_group && ScriptCompanyMode::IsDeity()) ? ::Group::Get(group_id)->owner : ScriptObject::GetCompany();
|
||||
|
||||
return GetGroupNumVehicle(company, group_id, valid_group ? ::Group::Get(group_id)->vehicle_type : (::VehicleType)vehicle_type);
|
||||
}
|
||||
|
||||
/* static */ bool ScriptGroup::MoveVehicle(GroupID group_id, VehicleID vehicle_id)
|
||||
|
@ -170,10 +176,14 @@
|
|||
|
||||
/* static */ EngineID ScriptGroup::GetEngineReplacement(GroupID group_id, EngineID engine_id)
|
||||
{
|
||||
EnforceCompanyModeValid(::EngineID::Invalid());
|
||||
if (!IsValidGroup(group_id) && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return ::EngineID::Invalid();
|
||||
if (group_id == GROUP_DEFAULT || group_id == GROUP_ALL) EnforceCompanyModeValid(::EngineID::Invalid());
|
||||
EnforceDeityOrCompanyModeValid(::EngineID::Invalid());
|
||||
bool valid_group = IsValidGroup(group_id);
|
||||
if (!valid_group && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return ::EngineID::Invalid();
|
||||
|
||||
return ::EngineReplacementForCompany(Company::Get(ScriptObject::GetCompany()), engine_id, group_id);
|
||||
::CompanyID company = (valid_group && ScriptCompanyMode::IsDeity()) ? ::Group::Get(group_id)->owner : ScriptObject::GetCompany();
|
||||
|
||||
return ::EngineReplacementForCompany(Company::Get(company), engine_id, group_id);
|
||||
}
|
||||
|
||||
/* static */ bool ScriptGroup::StopAutoReplace(GroupID group_id, EngineID engine_id)
|
||||
|
|
|
@ -134,7 +134,7 @@ public:
|
|||
* @pre ScriptEngine::IsValidEngine(engine_id).
|
||||
* @pre (IsValidGroup(group_id) && ScriptEngine::GetVehicleType(engine_id) == GetVehicleType(group_id)) ||
|
||||
group_id == GROUP_ALL || group_id == GROUP_DEFAULT.
|
||||
* @game @pre ScriptCompanyMode::IsValid().
|
||||
* @game @pre ScriptCompanyMode::IsValid() when group_id == GROUP_ALL || group_id == GROUP_DEFAULT.
|
||||
* @return The number of engines with id engine_id in the group with id group_id.
|
||||
*/
|
||||
static SQInteger GetNumEngines(GroupID group_id, EngineID engine_id);
|
||||
|
@ -146,7 +146,7 @@ public:
|
|||
* @pre IsValidGroup(group_id) || group_id == GROUP_ALL || group_id == GROUP_DEFAULT.
|
||||
* @pre IsValidGroup(group_id) || vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL ||
|
||||
* vehicle_type == ScriptVehicle::VT_WATER || vehicle_type == ScriptVehicle::VT_AIR
|
||||
* @game @pre ScriptCompanyMode::IsValid().
|
||||
* @game @pre ScriptCompanyMode::IsValid() when group_id == GROUP_ALL || group_id == GROUP_DEFAULT.
|
||||
* @return The total number of vehicles in the group with id group_id and it's sub-groups.
|
||||
* @note If the group is valid (neither GROUP_ALL nor GROUP_DEFAULT), the value of
|
||||
* vehicle_type is retrieved from the group itself and not from the input value.
|
||||
|
@ -194,7 +194,7 @@ public:
|
|||
* @param engine_id_new The engine id to replace with.
|
||||
* @pre IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL.
|
||||
* @pre ScriptEngine.IsBuildable(engine_id_new).
|
||||
* @game @pre ScriptCompanyMode::IsValid().
|
||||
* @game @pre ScriptCompanyMode::IsValid() when group_id == GROUP_ALL || group_id == GROUP_DEFAULT.
|
||||
* @return True if and if the replacing was successfully started.
|
||||
* @note To stop autoreplacing engine_id_old, call StopAutoReplace(group_id, engine_id_old).
|
||||
*/
|
||||
|
|
|
@ -16,9 +16,12 @@
|
|||
|
||||
ScriptGroupList::ScriptGroupList(HSQUIRRELVM vm)
|
||||
{
|
||||
EnforceCompanyModeValid_Void();
|
||||
EnforceDeityOrCompanyModeValid_Void();
|
||||
|
||||
bool is_deity = ScriptCompanyMode::IsDeity();
|
||||
::CompanyID owner = ScriptObject::GetCompany();
|
||||
|
||||
ScriptList::FillList<Group>(vm, this,
|
||||
[owner](const Group *g) { return g->owner == owner; }
|
||||
[owner, is_deity](const Group *g) { return g->owner == owner || is_deity; }
|
||||
);
|
||||
}
|
||||
|
|
|
@ -21,9 +21,6 @@
|
|||
class ScriptGroupList : public ScriptList {
|
||||
public:
|
||||
#ifdef DOXYGEN_API
|
||||
/**
|
||||
* @game @pre ScriptCompanyMode::IsValid().
|
||||
*/
|
||||
ScriptGroupList();
|
||||
|
||||
/**
|
||||
|
|
|
@ -106,26 +106,28 @@ ScriptVehicleList_SharedOrders::ScriptVehicleList_SharedOrders(VehicleID vehicle
|
|||
|
||||
ScriptVehicleList_Group::ScriptVehicleList_Group(GroupID group_id)
|
||||
{
|
||||
EnforceCompanyModeValid_Void();
|
||||
EnforceDeityOrCompanyModeValid_Void();
|
||||
if (!ScriptGroup::IsValidGroup(group_id)) return;
|
||||
|
||||
bool is_deity = ScriptCompanyMode::IsDeity();
|
||||
::CompanyID owner = ScriptObject::GetCompany();
|
||||
|
||||
ScriptList::FillList<Vehicle>(this,
|
||||
[owner](const Vehicle *v) { return v->owner == owner && v->IsPrimaryVehicle(); },
|
||||
[owner, is_deity](const Vehicle *v) { return (v->owner == owner || is_deity) && v->IsPrimaryVehicle(); },
|
||||
[group_id](const Vehicle *v) { return v->group_id == group_id; }
|
||||
);
|
||||
}
|
||||
|
||||
ScriptVehicleList_DefaultGroup::ScriptVehicleList_DefaultGroup(ScriptVehicle::VehicleType vehicle_type)
|
||||
{
|
||||
EnforceCompanyModeValid_Void();
|
||||
EnforceDeityOrCompanyModeValid_Void();
|
||||
if (vehicle_type < ScriptVehicle::VT_RAIL || vehicle_type > ScriptVehicle::VT_AIR) return;
|
||||
|
||||
bool is_deity = ScriptCompanyMode::IsDeity();
|
||||
::CompanyID owner = ScriptObject::GetCompany();
|
||||
|
||||
ScriptList::FillList<Vehicle>(this,
|
||||
[owner](const Vehicle *v) { return v->owner == owner && v->IsPrimaryVehicle(); },
|
||||
[owner, is_deity](const Vehicle *v) { return (v->owner == owner || is_deity) && v->IsPrimaryVehicle(); },
|
||||
[vehicle_type](const Vehicle *v) { return v->type == (::VehicleType)vehicle_type && v->group_id == ScriptGroup::GROUP_DEFAULT; }
|
||||
);
|
||||
}
|
||||
|
|
|
@ -104,7 +104,6 @@ class ScriptVehicleList_Group : public ScriptList {
|
|||
public:
|
||||
/**
|
||||
* @param group_id The ID of the group the vehicles are in.
|
||||
* @game @pre ScriptCompanyMode::IsValid().
|
||||
*/
|
||||
ScriptVehicleList_Group(GroupID group_id);
|
||||
};
|
||||
|
@ -118,7 +117,6 @@ class ScriptVehicleList_DefaultGroup : public ScriptList {
|
|||
public:
|
||||
/**
|
||||
* @param vehicle_type The VehicleType to get the list of vehicles for.
|
||||
* @game @pre ScriptCompanyMode::IsValid().
|
||||
*/
|
||||
ScriptVehicleList_DefaultGroup(ScriptVehicle::VehicleType vehicle_type);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue