From e3aa768034ce953afb0b50b49807d7a49350915c Mon Sep 17 00:00:00 2001 From: Misa Date: Wed, 23 Dec 2020 20:20:18 -0800 Subject: [PATCH] De-duplicate script.running checks While I was working on #535, I noticed that all the call sites of script.run() have the exact same code, namely: if (script.running) { script.run(); } I figured, why not move the script.running check into the function itself? That way, we won't have to duplicate the check every single time, and we don't risk forgetting to add the check and causing a bug because of that. The check was already duplicated once since 2.0 (it's used in both GAMEMODE and TELEPORTERMODE), and with the fix of the two-frame delay in 2.3, it's now duplicated twice, leading to THREE instances of this check in the code, when there should be only one. --- desktop_version/src/Map.cpp | 7 ++----- desktop_version/src/Script.cpp | 5 +++++ desktop_version/src/main.cpp | 7 ++----- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/desktop_version/src/Map.cpp b/desktop_version/src/Map.cpp index 99b110bd..af2d4deb 100644 --- a/desktop_version/src/Map.cpp +++ b/desktop_version/src/Map.cpp @@ -2101,9 +2101,6 @@ void mapclass::twoframedelayfix() game.state = 0; game.statedelay = 0; script.load(game.newscript); - if (script.running) - { - script.run(); - script.dontrunnextframe = true; - } + script.run(); + script.dontrunnextframe = true; } diff --git a/desktop_version/src/Script.cpp b/desktop_version/src/Script.cpp index 17c59726..a7f41fdc 100644 --- a/desktop_version/src/Script.cpp +++ b/desktop_version/src/Script.cpp @@ -79,6 +79,11 @@ void scriptclass::tokenize( const std::string& t ) void scriptclass::run() { + if (!running) + { + return; + } + // This counter here will stop the function when it gets too high short execution_counter = 0; while(running && scriptdelay<=0 && !game.pausescript) diff --git a/desktop_version/src/main.cpp b/desktop_version/src/main.cpp index a707d684..c3b922c3 100644 --- a/desktop_version/src/main.cpp +++ b/desktop_version/src/main.cpp @@ -556,7 +556,7 @@ void inline fixedloop() { script.dontrunnextframe = false; } - else if (script.running) + else { script.run(); } @@ -586,10 +586,7 @@ void inline fixedloop() } else { - if (script.running) - { - script.run(); - } + script.run(); gameinput(); } maplogic();