Changeset - r28397:940ec1a88c17
[Not reviewed]
master
0 7 0
Peter Nelson - 11 months ago 2024-01-05 08:32:43
peter1138@openttd.org
Codechange: Use locals for company/deity mode during script loops. (#11686)
7 files changed with 32 insertions and 12 deletions:
0 comments (0 inline, 0 general)
src/script/api/script_depotlist.cpp
Show inline comments
 
@@ -24,22 +24,26 @@ ScriptDepotList::ScriptDepotList(ScriptT
 
		case ScriptTile::TRANSPORT_ROAD:  tile_type = ::MP_ROAD; break;
 
		case ScriptTile::TRANSPORT_RAIL:  tile_type = ::MP_RAILWAY; break;
 
		case ScriptTile::TRANSPORT_WATER: tile_type = ::MP_WATER; break;
 

	
 
		case ScriptTile::TRANSPORT_AIR: {
 
			/* Hangars are not seen as real depots by the depot code. */
 
			bool is_deity = ScriptCompanyMode::IsDeity();
 
			CompanyID owner = ScriptObject::GetCompany();
 
			for (const Station *st : Station::Iterate()) {
 
				if (st->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity()) {
 
				if (is_deity || st->owner == owner) {
 
					for (uint i = 0; i < st->airport.GetNumHangars(); i++) {
 
						this->AddItem(st->airport.GetHangarTile(i).base());
 
					}
 
				}
 
			}
 
			return;
 
		}
 
	}
 

	
 
	/* Handle 'standard' depots. */
 
	bool is_deity = ScriptCompanyMode::IsDeity();
 
	CompanyID owner = ScriptObject::GetCompany();
 
	for (const Depot *depot : Depot::Iterate()) {
 
		if ((::GetTileOwner(depot->xy) == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity()) && ::IsTileType(depot->xy, tile_type)) this->AddItem(depot->xy.base());
 
		if ((is_deity || ::GetTileOwner(depot->xy) == owner) && ::IsTileType(depot->xy, tile_type)) this->AddItem(depot->xy.base());
 
	}
 
}
src/script/api/script_enginelist.cpp
Show inline comments
 
@@ -13,10 +13,12 @@
 

	
 
#include "../../safeguards.h"
 

	
 
ScriptEngineList::ScriptEngineList(ScriptVehicle::VehicleType vehicle_type)
 
{
 
	EnforceDeityOrCompanyModeValid_Void();
 
	bool is_deity = ScriptCompanyMode::IsDeity();
 
	CompanyID owner = ScriptObject::GetCompany();
 
	for (const Engine *e : Engine::IterateType((::VehicleType)vehicle_type)) {
 
		if (ScriptCompanyMode::IsDeity() || HasBit(e->company_avail, ScriptObject::GetCompany())) this->AddItem(e->index);
 
		if (is_deity || HasBit(e->company_avail, owner)) this->AddItem(e->index);
 
	}
 
}
src/script/api/script_grouplist.cpp
Show inline comments
 
@@ -14,10 +14,11 @@
 

	
 
#include "../../safeguards.h"
 

	
 
ScriptGroupList::ScriptGroupList()
 
{
 
	EnforceCompanyModeValid_Void();
 
	CompanyID owner = ScriptObject::GetCompany();
 
	for (const Group *g : Group::Iterate()) {
 
		if (g->owner == ScriptObject::GetCompany()) this->AddItem(g->index);
 
		if (g->owner == owner) this->AddItem(g->index);
 
	}
 
}
src/script/api/script_railtypelist.cpp
Show inline comments
 
@@ -14,10 +14,12 @@
 

	
 
#include "../../safeguards.h"
 

	
 
ScriptRailTypeList::ScriptRailTypeList()
 
{
 
	EnforceDeityOrCompanyModeValid_Void();
 
	bool is_deity = ScriptCompanyMode::IsDeity();
 
	CompanyID owner = ScriptObject::GetCompany();
 
	for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
 
		if (ScriptCompanyMode::IsDeity() || ::HasRailTypeAvail(ScriptObject::GetCompany(), rt)) this->AddItem(rt);
 
		if (is_deity || ::HasRailTypeAvail(owner, rt)) this->AddItem(rt);
 
	}
 
}
src/script/api/script_roadtypelist.cpp
Show inline comments
 
@@ -13,11 +13,12 @@
 

	
 
#include "../../safeguards.h"
 

	
 
ScriptRoadTypeList::ScriptRoadTypeList(ScriptRoad::RoadTramTypes rtts)
 
{
 
	EnforceDeityOrCompanyModeValid_Void();
 
	CompanyID owner = ScriptObject::GetCompany();
 
	for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) {
 
		if (!HasBit(rtts, GetRoadTramType(rt))) continue;
 
		if (::HasRoadTypeAvail(ScriptObject::GetCompany(), rt)) this->AddItem(rt);
 
		if (::HasRoadTypeAvail(owner, rt)) this->AddItem(rt);
 
	}
 
}
src/script/api/script_stationlist.cpp
Show inline comments
 
@@ -16,14 +16,16 @@
 

	
 
#include "../../safeguards.h"
 

	
 
ScriptStationList::ScriptStationList(ScriptStation::StationType station_type)
 
{
 
	EnforceDeityOrCompanyModeValid_Void();
 
	bool is_deity = ScriptCompanyMode::IsDeity();
 
	CompanyID owner = ScriptObject::GetCompany();
 
	for (Station *st : Station::Iterate()) {
 
		if ((st->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity()) && (st->facilities & station_type) != 0) this->AddItem(st->index);
 
		if ((is_deity || st->owner == owner) && (st->facilities & station_type) != 0) this->AddItem(st->index);
 
	}
 
}
 

	
 
ScriptStationList_Vehicle::ScriptStationList_Vehicle(VehicleID vehicle_id)
 
{
 
	if (!ScriptVehicle::IsPrimaryVehicle(vehicle_id)) return;
src/script/api/script_vehiclelist.cpp
Show inline comments
 
@@ -48,14 +48,16 @@ ScriptVehicleList::ScriptVehicleList(HSQ
 
	if (nparam >= 1 && vm->_ops_till_suspend_error_threshold == INT64_MIN) {
 
		new_ops_error_threshold = vm->_ops_till_suspend - MAX_VALUATE_OPS;
 
		vm->_ops_till_suspend_error_label = "vehicle filter function";
 
	}
 
	AutoRestoreBackup ops_error_threshold_backup(vm->_ops_till_suspend_error_threshold, new_ops_error_threshold);
 

	
 
	bool is_deity = ScriptCompanyMode::IsDeity();
 
	CompanyID owner = ScriptObject::GetCompany();
 
	for (const Vehicle *v : Vehicle::Iterate()) {
 
		if (v->owner != ScriptObject::GetCompany() && !ScriptCompanyMode::IsDeity()) continue;
 
		if (v->owner != owner && !is_deity) continue;
 
		if (!v->IsPrimaryVehicle() && !(v->type == VEH_TRAIN && ::Train::From(v)->IsFreeWagon())) continue;
 

	
 
		if (nparam < 1) {
 
			/* No filter, just add the item. */
 
			this->AddItem(v->index);
 
			continue;
 
@@ -104,14 +106,16 @@ ScriptVehicleList::ScriptVehicleList(HSQ
 

	
 
ScriptVehicleList_Station::ScriptVehicleList_Station(StationID station_id)
 
{
 
	EnforceDeityOrCompanyModeValid_Void();
 
	if (!ScriptBaseStation::IsValidBaseStation(station_id)) return;
 

	
 
	bool is_deity = ScriptCompanyMode::IsDeity();
 
	CompanyID owner = ScriptObject::GetCompany();
 
	for (const Vehicle *v : Vehicle::Iterate()) {
 
		if ((v->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity()) && v->IsPrimaryVehicle()) {
 
		if ((v->owner == owner || is_deity) && v->IsPrimaryVehicle()) {
 
			for (const Order *order : v->Orders()) {
 
				if ((order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT)) && order->GetDestination() == station_id) {
 
					this->AddItem(v->index);
 
					break;
 
				}
 
			}
 
@@ -153,14 +157,16 @@ ScriptVehicleList_Depot::ScriptVehicleLi
 
			break;
 

	
 
		default: // No depot
 
			return;
 
	}
 

	
 
	bool is_deity = ScriptCompanyMode::IsDeity();
 
	CompanyID owner = ScriptObject::GetCompany();
 
	for (const Vehicle *v : Vehicle::Iterate()) {
 
		if ((v->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity()) && v->IsPrimaryVehicle() && v->type == type) {
 
		if ((v->owner == owner || is_deity) && v->IsPrimaryVehicle() && v->type == type) {
 
			for (const Order *order : v->Orders()) {
 
				if (order->IsType(OT_GOTO_DEPOT) && order->GetDestination() == dest) {
 
					this->AddItem(v->index);
 
					break;
 
				}
 
			}
 
@@ -179,24 +185,26 @@ ScriptVehicleList_SharedOrders::ScriptVe
 

	
 
ScriptVehicleList_Group::ScriptVehicleList_Group(GroupID group_id)
 
{
 
	EnforceCompanyModeValid_Void();
 
	if (!ScriptGroup::IsValidGroup((ScriptGroup::GroupID)group_id)) return;
 

	
 
	CompanyID owner = ScriptObject::GetCompany();
 
	for (const Vehicle *v : Vehicle::Iterate()) {
 
		if (v->owner == ScriptObject::GetCompany() && v->IsPrimaryVehicle()) {
 
		if (v->owner == owner && v->IsPrimaryVehicle()) {
 
			if (v->group_id == group_id) this->AddItem(v->index);
 
		}
 
	}
 
}
 

	
 
ScriptVehicleList_DefaultGroup::ScriptVehicleList_DefaultGroup(ScriptVehicle::VehicleType vehicle_type)
 
{
 
	EnforceCompanyModeValid_Void();
 
	if (vehicle_type < ScriptVehicle::VT_RAIL || vehicle_type > ScriptVehicle::VT_AIR) return;
 

	
 
	CompanyID owner = ScriptObject::GetCompany();
 
	for (const Vehicle *v : Vehicle::Iterate()) {
 
		if (v->owner == ScriptObject::GetCompany() && v->IsPrimaryVehicle()) {
 
		if (v->owner == owner && v->IsPrimaryVehicle()) {
 
			if (v->type == (::VehicleType)vehicle_type && v->group_id == ScriptGroup::GROUP_DEFAULT) this->AddItem(v->index);
 
		}
 
	}
 
}
0 comments (0 inline, 0 general)