From a7c0b389e8dcefaaf8f5116d1d6dd34429be7caa Mon Sep 17 00:00:00 2001 From: Macoy Madson Date: Thu, 18 Mar 2021 20:49:07 -0700 Subject: [PATCH] Fix progression realloc bug I wasn't actually telling the right size, so garbage data was being read instead. It's good to know the game doesn't crash on this anyways, I guess. I also reduced the number of attempts made at picking the next puzzle if the data set is small. There's now another condition to ensure the next puzzle is different from the current one. --- src/Main.cake | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Main.cake b/src/Main.cake index f7be8e0..e91c06d 100644 --- a/src/Main.cake +++ b/src/Main.cake @@ -518,13 +518,21 @@ (defun-local game-board-load-next-puzzle () (when g-num-progression-puzzles + (var g-previous-progression-puzzle (* progression-puzzle) g-current-progression-puzzle) (pick-random-progression-puzzle) + (var num-attempts int 1) - (while (path g-current-progression-puzzle > is-solved) + ;; Reasonable given how many puzzles someone might do in one sitting vs. total num puzzles + (var max-num-attempts int 200) + ;; Don't try too hard when the data set is small + (when (< g-num-progression-puzzles max-num-attempts) + (set max-num-attempts (* 2 g-num-progression-puzzles))) + + (while (or (= g-previous-progression-puzzle g-current-progression-puzzle) ;; Try to ensure change + (path g-current-progression-puzzle > is-solved)) ;; Only pick non-solved (pick-random-progression-puzzle) (incr num-attempts) - ;; Reasonable given how many puzzles someone might do in one sitting vs. total num puzzles - (when (= num-attempts 200) + (when (= num-attempts max-num-attempts) (SDL_Log "Max attempts reached for random puzzle! Has the app not been restarted in a long time?\n") (break))) (unless (< (path g-current-progression-puzzle > index) g-num-puzzles) @@ -618,7 +626,8 @@ (when (and g-num-progression-puzzles (!= g-num-progression-puzzles num-unsolved-puzzles)) (set g-progression-puzzles - (type-cast (realloc g-progression-puzzles num-unsolved-puzzles) + (type-cast (realloc g-progression-puzzles + (* (sizeof (type progression-puzzle)) num-unsolved-puzzles)) (* progression-puzzle))) (set g-num-progression-puzzles num-unsolved-puzzles))