From 5a419e4bddb0dae77b2a9049e75544bfa3f63ae0 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Thu, 24 Nov 2022 23:17:46 +0000 Subject: [PATCH] VarAction2: Detect callback switches which include other adjusts --- src/newgrf_optimiser.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/newgrf_optimiser.cpp b/src/newgrf_optimiser.cpp index d4b0194219..bcd72ee7bd 100644 --- a/src/newgrf_optimiser.cpp +++ b/src/newgrf_optimiser.cpp @@ -2453,10 +2453,11 @@ void OptimiseVarAction2DeterministicSpriteGroup(VarAction2OptimiseState &state, bool seen_req_var1C = false; if (!group->calculated_result) { bool is_cb_switch = false; - if (possible_callback_handler && group->adjusts.size() == 1 && !group->calculated_result && + if (possible_callback_handler && group->adjusts.size() > 0 && !group->calculated_result && IsFeatureUsableForCBQuickExit(group->feature) && !HasGrfOptimiserFlag(NGOF_NO_OPT_VARACT2_CB_QUICK_EXIT)) { - const auto &adjust = group->adjusts[0]; - if (adjust.variable == 0xC && (adjust.operation == DSGA_OP_ADD || adjust.operation == DSGA_OP_RST) && + size_t idx = group->adjusts.size() - 1; + const auto &adjust = group->adjusts[idx]; + if (adjust.variable == 0xC && ((adjust.operation == DSGA_OP_ADD && idx == 0) || adjust.operation == DSGA_OP_RST) && adjust.shift_num == 0 && (adjust.and_mask & 0xFF) == 0xFF && adjust.type == DSGA_TYPE_NONE) { is_cb_switch = true; }