Skip to content

Commit

Permalink
Add: AI/GS IsNoiseLevelIncreaseAllowed.
Browse files Browse the repository at this point in the history
API addition that may be used to check whether the noise that will be added to the nearest town if an airport would be built at a tile is allowed.
  • Loading branch information
SamuXarick committed Mar 31, 2019
1 parent 6d1cc14 commit 9804f2d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/script/api/ai/ai_airport.hpp.sq
Expand Up @@ -50,6 +50,7 @@ void SQAIAirport_Register(Squirrel *engine)
SQAIAirport.DefSQStaticMethod(engine, &ScriptAirport::RemoveAirport, "RemoveAirport", 2, ".i");
SQAIAirport.DefSQStaticMethod(engine, &ScriptAirport::GetAirportType, "GetAirportType", 2, ".i");
SQAIAirport.DefSQStaticMethod(engine, &ScriptAirport::GetNoiseLevelIncrease, "GetNoiseLevelIncrease", 3, ".ii");
SQAIAirport.DefSQStaticMethod(engine, &ScriptAirport::IsNoiseLevelIncreaseAllowed, "IsNoiseLevelIncreaseAllowed", 3, ".ii");
SQAIAirport.DefSQStaticMethod(engine, &ScriptAirport::GetNearestTown, "GetNearestTown", 3, ".ii");
SQAIAirport.DefSQStaticMethod(engine, &ScriptAirport::GetMaintenanceCostFactor, "GetMaintenanceCostFactor", 2, ".i");
SQAIAirport.DefSQStaticMethod(engine, &ScriptAirport::GetMonthlyMaintenanceCost, "GetMonthlyMaintenanceCost", 2, ".i");
Expand Down
1 change: 1 addition & 0 deletions src/script/api/ai_changelog.hpp
Expand Up @@ -24,6 +24,7 @@
* \li AIGroup::SetSecondaryColour
* \li AIGroup::GetPrimaryColour
* \li AIGroup::GetSecondaryColour
* \li AIAirport::IsNoiseLevelIncreaseAllowed
*
* \b 1.9.0
*
Expand Down
1 change: 1 addition & 0 deletions src/script/api/game/game_airport.hpp.sq
Expand Up @@ -50,6 +50,7 @@ void SQGSAirport_Register(Squirrel *engine)
SQGSAirport.DefSQStaticMethod(engine, &ScriptAirport::RemoveAirport, "RemoveAirport", 2, ".i");
SQGSAirport.DefSQStaticMethod(engine, &ScriptAirport::GetAirportType, "GetAirportType", 2, ".i");
SQGSAirport.DefSQStaticMethod(engine, &ScriptAirport::GetNoiseLevelIncrease, "GetNoiseLevelIncrease", 3, ".ii");
SQGSAirport.DefSQStaticMethod(engine, &ScriptAirport::IsNoiseLevelIncreaseAllowed, "IsNoiseLevelIncreaseAllowed", 3, ".ii");
SQGSAirport.DefSQStaticMethod(engine, &ScriptAirport::GetNearestTown, "GetNearestTown", 3, ".ii");
SQGSAirport.DefSQStaticMethod(engine, &ScriptAirport::GetMaintenanceCostFactor, "GetMaintenanceCostFactor", 2, ".i");
SQGSAirport.DefSQStaticMethod(engine, &ScriptAirport::GetMonthlyMaintenanceCost, "GetMonthlyMaintenanceCost", 2, ".i");
Expand Down
3 changes: 3 additions & 0 deletions src/script/api/game_changelog.hpp
Expand Up @@ -18,6 +18,9 @@
* \b 1.10.0
*
* This version is not yet released. The following changes are not set in stone yet.
* API additions:
* \li GSAirport::IsNoiseLevelIncreaseAllowed
*
* \b 1.9.0
*
Expand Down
28 changes: 28 additions & 0 deletions src/script/api/script_airport.cpp
Expand Up @@ -149,6 +149,34 @@
return 1;
}

/* static */ bool ScriptAirport::IsNoiseLevelIncreaseAllowed(TileIndex tile, AirportType type)
{
extern Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it, uint &mindist);
extern uint8 GetAirportNoiseLevelForDistance(const AirportSpec *as, uint distance);

if (!::IsValidTile(tile)) return false;
if (!IsAirportInformationAvailable(type)) return false;

const AirportSpec *as = ::AirportSpec::Get(type);
if (!as->IsWithinMapBounds(0, tile)) return false;

AirportTileTableIterator it(as->table[0], tile);
uint dist;
Town *t = AirportGetNearestTown(as, it, dist);
if (!::Town::IsValidID(t->index)) return false;

if (_settings_game.economy.station_noise_level) {
return GetAirportNoiseLevelForDistance(as, dist) <= t->MaxTownNoise() - t->noise_reached;
}

int num = 0;
const Station *st;
FOR_ALL_STATIONS(st) {
if (st->town == t && (st->facilities & FACIL_AIRPORT) && st->airport.type != AT_OILRIG) num++;
}
return 1 <= max(0, 2 - num);
}

/* static */ TownID ScriptAirport::GetNearestTown(TileIndex tile, AirportType type)
{
extern Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it, uint &mindist);
Expand Down
11 changes: 11 additions & 0 deletions src/script/api/script_airport.hpp
Expand Up @@ -184,6 +184,17 @@ class ScriptAirport : public ScriptObject {
*/
static int GetNoiseLevelIncrease(TileIndex tile, AirportType type);

/**
* Checks whether the noise that will be added to the nearest town if an airport would be
* built at this tile is allowed.
* @param tile The tile to check.
* @param type The AirportType to check.
* @pre IsAirportInformationAvailable(type).
* @return true if and only if the noise level increase added by the airport is allowed.
* @note The noise will be added to the town with TownID GetNearestTown(tile, type).
*/
static bool IsNoiseLevelIncreaseAllowed(TileIndex tile, AirportType type);

/**
* Get the TownID of the town whose local authority will influence
* an airport at some tile.
Expand Down

0 comments on commit 9804f2d

Please sign in to comment.