From 52aeccb831b294d4225835b38a3a551d761ba10d Mon Sep 17 00:00:00 2001 From: cirdan Date: Wed, 5 Mar 2014 23:30:53 +0100 Subject: [PATCH] Keep a tileset of industries Keep a tileset of all industries on the map, and use it to search for nearby conflicting industries when trying to build another one. --- src/industry.h | 21 ++++++++++++++++++--- src/industry_cmd.cpp | 8 ++++++-- src/saveload/industry_sl.cpp | 1 + src/saveload/oldloader_sl.cpp | 1 + 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/industry.h b/src/industry.h index c7300c4c2..071c6e826 100644 --- a/src/industry.h +++ b/src/industry.h @@ -16,7 +16,7 @@ #include "subsidy_type.h" #include "industrytype.h" #include "map/tilearea.h" - +#include "map/tileset.h" /** * Production level maximum, minimum and default values. @@ -30,10 +30,13 @@ enum ProductionLevels { PRODLEVEL_MAXIMUM = 0x80, ///< the industry is running at full speed }; +/* Forward declaration. */ +inline TileIndex get_industry_tile (const Industry*); + /** * Defines the internal data of a functional industry. */ -struct Industry : PooledItem { +struct Industry : PooledItem , TileSetObject { TileArea location; ///< Location of the industry Town *town; ///< Nearest town CargoID produced_cargo[2]; ///< 2 production cargo slots @@ -68,7 +71,13 @@ struct Industry : PooledItem { PersistentStorage *psa; ///< Persistent storage for NewGRF industries. - Industry(TileIndex tile = INVALID_TILE) : location(tile, 0, 0) {} + Industry() : location() {} + + Industry(TileIndex tile) : location(tile, 0, 0) + { + add_to_tileset(); + } + ~Industry(); void RecomputeProductionMultipliers(); @@ -140,6 +149,12 @@ protected: static uint16 counts[NUM_INDUSTRYTYPES]; ///< Number of industries per type ingame }; +/** Get the tile where an industry is located. */ +inline TileIndex get_industry_tile (const Industry *i) +{ + return i->location.tile; +} + void PlantRandomFarmField(const Industry *i); void ReleaseDisastersTargetingIndustry(IndustryID); diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index 948ed8f66..0e7b70f22 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -139,6 +139,8 @@ Industry::~Industry() * Also we must not decrement industry counts in that case. */ if (this->location.w == 0) return; + remove_from_tileset(); + TILE_AREA_LOOP(tile_cur, this->location) { if (IsIndustryTile(tile_cur)) { if (GetIndustryIndex(tile_cur) == this->index) { @@ -1543,8 +1545,10 @@ static bool CheckIfCanLevelIndustryPlatform(TileIndex tile, DoCommandFlag flags, static CommandCost CheckIfFarEnoughFromConflictingIndustry(TileIndex tile, int type) { const IndustrySpec *indspec = GetIndustrySpec(type); - const Industry *i; - FOR_ALL_INDUSTRIES(i) { + + for (Industry::TileSet::Iterator iter (&Industry::set, tile, 14); iter.get_item() != NULL; iter.next()) { + const Industry *i = iter.get_item(); + /* Within 14 tiles from another industry is considered close */ if (DistanceMax(tile, i->location.tile) > 14) continue; diff --git a/src/saveload/industry_sl.cpp b/src/saveload/industry_sl.cpp index d8f230468..d0b73d775 100644 --- a/src/saveload/industry_sl.cpp +++ b/src/saveload/industry_sl.cpp @@ -93,6 +93,7 @@ static void Load_INDY(LoadBuffer *reader) while ((index = reader->IterateChunk()) != -1) { Industry *i = new (index) Industry(); reader->ReadObject(i, _industry_desc); + i->add_to_tileset(); /* Before legacy savegame version 161, persistent storages were not stored in a pool. */ if (reader->IsOTTDVersionBefore(161) && !reader->IsOTTDVersionBefore(76)) { diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index f4a49793f..a550c980a 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -897,6 +897,7 @@ static bool LoadOldIndustry(LoadgameState *ls, int num) i->random_colour = RemapTTOColour(i->random_colour); } + i->add_to_tileset(); Industry::IncIndustryTypeCount(i->type); } else { delete i; -- 2.11.4.GIT