From 3d7e5d5369c5e8de8339871aef707f6e88df47e6 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Thu, 24 Nov 2022 02:38:10 +0000 Subject: [PATCH] Avoid unnecessary viewport redraws for unused tile loop house triggers --- src/genworld.cpp | 2 ++ src/house.h | 8 ++++++++ src/newgrf_analysis.cpp | 4 +++- src/newgrf_analysis.h | 1 + src/newgrf_house.cpp | 29 ++++++++++++++++++++++++++--- src/newgrf_house.h | 2 ++ src/saveload/afterload.cpp | 4 ++++ src/table/newgrf_debug_data.h | 2 +- src/table/town_land.h | 2 +- 9 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/genworld.cpp b/src/genworld.cpp index 79b5972bbe..45a4fd46c1 100644 --- a/src/genworld.cpp +++ b/src/genworld.cpp @@ -320,6 +320,8 @@ void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_setti LoadStringWidthTable(); AnalyseEngineCallbacks(); AnalyseIndustryTileSpriteGroups(); + extern void AnalyseHouseSpriteGroups(); + AnalyseHouseSpriteGroups(); /* Re-init the windowing system */ ResetWindowSystem(); diff --git a/src/house.h b/src/house.h index e63a957807..84a1241d44 100644 --- a/src/house.h +++ b/src/house.h @@ -95,6 +95,13 @@ enum HouseExtraFlags { DECLARE_ENUM_AS_BIT_SET(HouseExtraFlags) +enum HouseCtrlFlags { + HCF_NONE = 0, + HCF_NO_TRIGGERS = 1U << 0, ///< this house does not use random triggers +}; + +DECLARE_ENUM_AS_BIT_SET(HouseCtrlFlags) + struct HouseSpec { /* Standard properties */ Year min_year; ///< introduction year of the house @@ -116,6 +123,7 @@ struct HouseSpec { byte random_colour[4]; ///< 4 "random" colours byte probability; ///< Relative probability of appearing (16 is the standard value) HouseExtraFlags extra_flags; ///< some more flags + HouseCtrlFlags ctrl_flags; ///< control flags HouseClassID class_id; ///< defines the class this house has (not grf file based) AnimationInfo animation; ///< information about the animation. byte processing_time; ///< Periodic refresh multiplier diff --git a/src/newgrf_analysis.cpp b/src/newgrf_analysis.cpp index 4847bed774..595c1a3ea4 100644 --- a/src/newgrf_analysis.cpp +++ b/src/newgrf_analysis.cpp @@ -299,7 +299,9 @@ void RandomizedSpriteGroup::AnalyseCallbacks(AnalyseCallbackOperation &op) const { op.result_flags |= ACORF_CB_REFIT_CAP_NON_WHITELIST_FOUND; - if (op.mode == ACOM_CB_VAR) op.callbacks_used |= SGCU_RANDOM_TRIGGER; + if ((op.mode == ACOM_CB_VAR || op.mode == ACOM_FIND_RANDOM_TRIGGER) && (this->triggers != 0 || this->cmp_mode == RSG_CMP_ALL)) { + op.callbacks_used |= SGCU_RANDOM_TRIGGER; + } for (const SpriteGroup *group: this->groups) { if (group != nullptr) group->AnalyseCallbacks(op); diff --git a/src/newgrf_analysis.h b/src/newgrf_analysis.h index 4cb17df4f6..e5ee458190 100644 --- a/src/newgrf_analysis.h +++ b/src/newgrf_analysis.h @@ -23,6 +23,7 @@ enum AnalyseCallbackOperationMode : uint8 { ACOM_CB36_SPEED, ACOM_INDUSTRY_TILE, ACOM_CB_REFIT_CAPACITY, + ACOM_FIND_RANDOM_TRIGGER, }; struct AnalyseCallbackOperationIndustryTileData; diff --git a/src/newgrf_house.cpp b/src/newgrf_house.cpp index 89af0470b1..456256bea4 100644 --- a/src/newgrf_house.cpp +++ b/src/newgrf_house.cpp @@ -22,6 +22,7 @@ #include "newgrf_animation_base.h" #include "newgrf_cargo.h" #include "station_base.h" +#include "newgrf_analysis.h" #include "safeguards.h" @@ -727,8 +728,12 @@ bool NewHouseTileLoop(TileIndex tile) return true; } - TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP); - if (hs->building_flags & BUILDING_HAS_1_TILE) TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP_TOP); + bool do_triggers = !(hs->ctrl_flags & HCF_NO_TRIGGERS); + + if (do_triggers) { + TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP); + if (hs->building_flags & BUILDING_HAS_1_TILE) TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP_TOP); + } if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) { /* If this house is marked as having a synchronised callback, all the @@ -758,7 +763,7 @@ bool NewHouseTileLoop(TileIndex tile) } SetHouseProcessingTime(tile, hs->processing_time); - MarkTileDirtyByTile(tile, VMDF_NOT_MAP_MODE); + if (do_triggers) MarkTileDirtyByTile(tile, VMDF_NOT_MAP_MODE); return true; } @@ -857,3 +862,21 @@ void WatchedCargoCallback(TileIndex tile, CargoTypes trigger_cargoes) if (hs->building_flags & BUILDING_HAS_4_TILES) DoWatchedCargoCallback(TILE_ADDXY(north, 1, 1), tile, trigger_cargoes, r); } +void AnalyseHouseSpriteGroups() +{ + for (uint i = 0; i < NUM_HOUSES; i++) { + HouseSpec *spec = HouseSpec::Get(i); + spec->ctrl_flags = HCF_NONE; + + if (spec->grf_prop.spritegroup[0] == nullptr) { + spec->ctrl_flags |= HCF_NO_TRIGGERS; + continue; + } + + AnalyseCallbackOperation find_triggers_op(ACOM_FIND_RANDOM_TRIGGER); + spec->grf_prop.spritegroup[0]->AnalyseCallbacks(find_triggers_op); + if ((find_triggers_op.callbacks_used & SGCU_RANDOM_TRIGGER) == 0) { + spec->ctrl_flags |= HCF_NO_TRIGGERS; + } + } +} diff --git a/src/newgrf_house.h b/src/newgrf_house.h index c377ff9086..6f01a8d373 100644 --- a/src/newgrf_house.h +++ b/src/newgrf_house.h @@ -166,4 +166,6 @@ enum HouseTrigger { }; void TriggerHouse(TileIndex t, HouseTrigger trigger); +void AnalyseHouseSpriteGroups(); + #endif /* NEWGRF_HOUSE_H */ diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 11538b844a..ca6bafd416 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -952,6 +952,8 @@ bool AfterLoadGame() AfterLoadEngines(); AnalyseIndustryTileSpriteGroups(); + extern void AnalyseHouseSpriteGroups(); + AnalyseHouseSpriteGroups(); /* Update all vehicles */ AfterLoadVehicles(true); @@ -4211,6 +4213,8 @@ void ReloadNewGRFData() ResetVehicleHash(); AfterLoadEngines(); AnalyseIndustryTileSpriteGroups(); + extern void AnalyseHouseSpriteGroups(); + AnalyseHouseSpriteGroups(); AfterLoadVehicles(false); StartupEngines(); GroupStatistics::UpdateAfterLoad(); diff --git a/src/table/newgrf_debug_data.h b/src/table/newgrf_debug_data.h index fdb3f16b06..9328742e90 100644 --- a/src/table/newgrf_debug_data.h +++ b/src/table/newgrf_debug_data.h @@ -592,7 +592,7 @@ class NIHHouse : public NIHelper { const HouseSpec *hs = HouseSpec::Get(GetHouseType(index)); seprintf(buffer, lastof(buffer), " building_flags: 0x%X", hs->building_flags); output.print(buffer); - seprintf(buffer, lastof(buffer), " extra_flags: 0x%X", hs->extra_flags); + seprintf(buffer, lastof(buffer), " extra_flags: 0x%X, ctrl_flags: 0x%X", hs->extra_flags, hs->ctrl_flags); output.print(buffer); seprintf(buffer, lastof(buffer), " remove_rating_decrease: %u, minimum_life: %u", hs->remove_rating_decrease, hs->minimum_life); output.print(buffer); diff --git a/src/table/town_land.h b/src/table/town_land.h index 92292f6203..d73940877d 100644 --- a/src/table/town_land.h +++ b/src/table/town_land.h @@ -1814,7 +1814,7 @@ static_assert(lengthof(_town_draw_tile_data) == (NEW_HOUSE_OFFSET) * 4 * 4); {ca1, ca2, ca3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, \ {cg1, cg2, cg3, CT_INVALID, CT_INVALID, CT_INVALID, CT_INVALID, CT_INVALID, CT_INVALID, CT_INVALID, CT_INVALID, CT_INVALID, CT_INVALID, CT_INVALID, CT_INVALID, CT_INVALID}, \ bf, ba, true, GRFFileProps(INVALID_HOUSE_ID), 0, {0, 0, 0, 0}, \ - 16, NO_EXTRA_FLAG, HOUSE_NO_CLASS, {0, 2, 0, 0}, 0, 0, 0} + 16, NO_EXTRA_FLAG, HCF_NONE, HOUSE_NO_CLASS, {0, 2, 0, 0}, 0, 0, 0} /** House specifications from original data */ static const HouseSpec _original_house_specs[] = { /**