trip logs / gnuvola


Trip Log 2022-01-29 h12 -- Hooray! Part 6

I felt so good after writing part 5 of the Hooray! series that I watched an episode of Arcane on Netflix and decided to jump right back and tackle part 6.  We will use the momentum from all these speedy proceedings obliquely, this time; patch 9 does not directly address the animation, but rather makes it interesting to observe repetitions.  Patch 10 is in the same vein, and patch 11 takes full advantage of these changes to increase the repetitions.  All in all, a breezy 10 minutes, I predict... 

patch 9

     1  commit 81eb56d522a75e95b23d0017a675b287cf136f42
     2  Author: Thien-Thi Nguyen <ttn@gnuvola.org>
     3  Date:   2022-01-29 06:47:50 -0500
     4
     5      Add change of scenery
     6
     7      * hooray.scm: Import SRFI 1 ‘car+cdr’.
     8      (MID): New data structure.
     9      (refresh-scenery! place-tile!): Gate blit w/ ‘name’.
    10      (refresh-scenery!): Process ‘MID’ for mid ground.
    11      (change-scenery-slightly!): New proc.
    12      <top-level>: Call ‘change-scenery-slightly!’
    13      immediately after calling ‘hooray!’.
    14  ---
    15   hooray.scm | 41 ++++++++++++++++++++++++++++++++++++++---
    16   1 file changed, 38 insertions(+), 3 deletions(-)
    17
    18  diff --git a/hooray.scm b/hooray.scm
    19  index cd3da51..55dc817 100644
    20  --- a/hooray.scm
    21  +++ b/hooray.scm
    22  @@ -27,6 +27,7 @@
    23   ;;; Code:
    24
    25   (use-modules
    26  + ((srfi srfi-1) #:select (car+cdr))
    27    ((srfi srfi-11) #:select (let-values))
    28    ((sdl misc-utils) #:select (copy-surface
    29                                exact-truncate))
    30  @@ -92,11 +93,15 @@ (define FAR #2((window window window window window)
    31                  (window cell   cell   cell   window)
    32                  (window window window window window)))
    33
    34  +(define MID (let ((a (apply make-array #f (array-shape FAR))))
    35  +              (array-set! a 'rec-1 2 2)
    36  +              a))
    37  +
    38   (define refresh-scenery!
    39     (let ((rect (apply make-array #f (array-shape FAR))))
    40
    41       (define (place-tile! name rect)
    42  -      (SDL:blit-surface (surf name) #f #f rect)
    43  +      (and name (SDL:blit-surface (surf name) #f #f rect))
    44         rect)
    45
    46       ;; initialize ‘rect’
    47  @@ -107,7 +112,7 @@ (define refresh-scenery!
    48         ;; far ground
    49         (array-map! rect place-tile! FAR rect)
    50         ;; mid ground
    51  -      (place-tile! 'rec-1 (array-ref rect 2 2)))))
    52  +      (array-map! rect place-tile! MID rect))))
    53
    54   (define smokin
    55     (let ((keys (list->vector (hash-fold (lambda (k v acc)
    56  @@ -245,12 +250,42 @@ (define hooray!
    57              (SDL:delay 42))
    58            fg w/2 h/2)))))
    59
    60  +(define (change-scenery-slightly!)
    61  +
    62  +  (define random-button
    63  +    (let ((v (list->vector '(left right check-0 x mix tools blank check-1))))
    64  +      ;; random-button
    65  +      (lambda ()
    66  +        (vector-ref v (random (vector-length v))))))
    67  +
    68  +  (define (new-place)
    69  +    (let loop ((count 9))
    70  +      (let ((try (cons (random 5) (random 5))))
    71  +        (cond ((zero? count)
    72  +               (cons 2 2))              ; default
    73  +              ((array-ref MID (car try) (cdr try))
    74  +               (loop (1- count)))
    75  +              (else
    76  +               try)))))
    77  +
    78  +  (let-values (((r c) (car+cdr (new-place))))
    79  +
    80  +    (define (a! a name)
    81  +      (array-set! a name r c))
    82  +
    83  +    (a! FAR (if (and (< 0 r 4)
    84  +                     (< 0 c 4))
    85  +                'window
    86  +                'cell))
    87  +    (a! MID (random-button))))
    88  +
    89   (set! *random-state* (seed->random-state (let ((now (gettimeofday)))
    90                                              (* (car now) (cdr now)))))
    91   (smokin 9)
    92   (do ((i 0 (1+ i)))
    93       ((= 5 i))
    94  -  (hooray!))
    95  +  (hooray!)
    96  +  (change-scenery-slightly!))
    97
    98   (SDL:delay 420)
    99   (exit (SDL:quit))

Since we're talking about scenery here, it makes sense to use patterns from the existing scenery processing, and in fact, that's what patch 9 does.  The new data structure ‘MID’ (lines 34-36) is an array, just like ‘FAR’, and is processed in exactly the same manner as ‘FAR’ in the existing procedure ‘refresh-scenery!’ (lines 51-52).  Since ‘MID’ is not fully populated like ‘FAR’, internal procedure ‘place-tile!’ is modified (lines 42-43), as well, to blit only if there is something to blit. 

As for ‘change-scenery-slightly!’, a good chunk of it (internal procedure ‘new-place’, lines 68-76) is concerned with not repeating a change (by, somewhat ironically, repeatedly trying if such a repeat is detected! — lines 73-74).  The rest of ‘change-scenery-slightly!’ is the usual application of ‘random’, this time w/ tiles from row 2 of the theme.  Pretty straightforward. 

patch 10

     1  commit f61d23feb0c1d669209d40c21494fb6ed32094cf
     2  Author: Thien-Thi Nguyen <ttn@gnuvola.org>
     3  Date:   2022-01-29 07:27:15 -0500
     4
     5      Bump ‘smokin’ delay
     6
     7      * hooray.scm <top-level>: ...to 3 milliseconds.
     8  ---
     9   hooray.scm | 2 +-
    10   1 file changed, 1 insertion(+), 1 deletion(-)
    11
    12  diff --git a/hooray.scm b/hooray.scm
    13  index 55dc817..09723cb 100644
    14  --- a/hooray.scm
    15  +++ b/hooray.scm
    16  @@ -281,7 +281,7 @@ (define (change-scenery-slightly!)
    17
    18   (set! *random-state* (seed->random-state (let ((now (gettimeofday)))
    19                                              (* (car now) (cdr now)))))
    20  -(smokin 9)
    21  +(smokin 3)
    22   (do ((i 0 (1+ i)))
    23       ((= 5 i))
    24     (hooray!)

As mentioned in the intro paragraph, patch 10 tries to make things more interesting by doing the sanity check approximately three times more quickly. 

patch 11

     1  commit 997c20c105ef5ad5b6b2020379fb61b840fc712e
     2  Author: Thien-Thi Nguyen <ttn@gnuvola.org>
     3  Date:   2022-01-29 07:32:14 -0500
     4
     5      Bump ‘hooray!’ iteration
     6
     7      * hooray.scm <top-level>: ...to 9.
     8  ---
     9   hooray.scm | 2 +-
    10   1 file changed, 1 insertion(+), 1 deletion(-)
    11
    12  diff --git a/hooray.scm b/hooray.scm
    13  index 09723cb..37011a2 100644
    14  --- a/hooray.scm
    15  +++ b/hooray.scm
    16  @@ -283,7 +283,7 @@ (set! *random-state* (seed->random-state (let ((now (gettimeofday)))
    17                                              (* (car now) (cdr now)))))
    18   (smokin 3)
    19   (do ((i 0 (1+ i)))
    20  -    ((= 5 i))
    21  +    ((= 9 i))
    22     (hooray!)
    23     (change-scenery-slightly!))

Lastly, on the theory that we will want to see more of the animation going forward (and that doing so is more interesting now, with patch 9), patch 11 almost doubles the iteration count for ‘hooray!’. 

Well, that's it.  Weekend is still young, so maybe we'll get another part done Real Soon Now.  Fingers crossed! 


Copyright (C) 2022 Thien-Thi Nguyen