@ -14,7 +14,7 @@
"Decompression.cake" ;; kitty-gridlock
&comptime-only "Macros.cake" ) ;; cakelisp/runtime
( c-import "<stdio.h>"
( c-import "<stdio.h>" "<stdlib.h>"
"SDL.h" "SDL_syswm.h" "SDL_timer.h" "SDL_render.h"
;; For round()
"<math.h>" )
@ -262,51 +262,89 @@
;; I used lowercase ~o~ instead of periods ~.~ for the empty cells in the database so that the entire board description can be selected with a double-click.
;; Example format:
;; 60 IBBxooIooLDDJAALooJoKEEMFFKooMGGHHHM 2332
( defun-local game-board-load ( fogleman- board-string ( * ( const char ) ) &return bool )
( defun-local game-board-load ( board-string ( * ( const char ) ) &return bool )
( game-board-reset-pieces )
( var num-moves int 0 )
( var num-states int 0 )
( var pieces-read bool false )
( var buffer ( [] 37 char ) ( array 0 ) )
( var write-char ( * char ) buffer )
( var current-char ( * ( const char ) ) fogleman-board-string )
( while ( deref current-char )
( var board-char ( * ( const char ) ) board-string )
( while ( < ( - board-char board-string ) 36 )
( cond
;; Space field delimiter
( ( = ( deref current-char ) ( space-hack ) )
( cond
( ( not num-moves )
( set num-moves ( atoi buffer ) ) )
( true ;; Reading pieces
( var board-char ( * ( const char ) ) buffer )
( while ( deref board-char )
( cond
( ( = ( deref board-char ) 'o ' )
( debug-log "empty\n" ) ) ;; Empty space
( true ;; Wall or piece
( var char-index int ( - board-char buffer ) )
( var piece-cell-position grid-vec2 ( array ( % char-index g-game-board-grid-size )
( / char-index g-game-board-grid-size ) ) )
( debug-log "[%d] '%c' = (%d %d)\n" char-index ( deref board-char ) ( vec-xy piece-cell-position ) )
( game-board-piece-from-char ( deref board-char ) piece-cell-position ) ) )
( incr board-char ) ) ) )
;; Reset buffer
( memset buffer 0 ( sizeof buffer ) )
( set write-char buffer ) )
( true
( set ( deref write-char ) ( deref current-char ) )
( incr write-char ) ) )
( incr current-char ) )
( set num-states ( atoi buffer ) )
( ( = ( deref board-char ) 'o ' )
( debug-log "empty\n" ) ) ;; Empty space
( true ;; Wall or piece
( var char-index int ( - board-char board-string ) )
( var piece-cell-position grid-vec2 ( array ( % char-index g-game-board-grid-size )
( / char-index g-game-board-grid-size ) ) )
( debug-log "[%d] '%c' = (%d %d)\n" char-index ( deref board-char ) ( vec-xy piece-cell-position ) )
( game-board-piece-from-char ( deref board-char ) piece-cell-position ) ) )
( incr board-char ) )
( game-board-sync-occupied-state )
( printf "Board loaded. %d moves, %d states.\n" num-moves num-states )
( printf "%s\n" fogleman-board-string )
( return true ) )
( defstruct-local puzzle-data
num-moves char
num-states int
board ( [] 36 char ) )
( var g-puzzle-list ( * puzzle-data ) null )
( var g-num-puzzles int 0 )
( defun-local read-puzzles ( &return bool )
( var puzzles-file ( * FILE ) ( fopen ( in-data-dir "puzzles.txt" ) "r" ) )
( unless puzzles-file
( printf "Failed to load puzzles. You may need to run Decompression.cake (see Build.sh)\n" )
( return false ) )
;; 48 = max length of a single line in puzzles .txt
( var line-buffer ( [] 48 char ) ( array 0 ) )
( unless ( fgets line-buffer ( array-size line-buffer ) puzzles-file )
( printf "Puzzle file malformed\n" ) ( return false ) )
( var num-puzzles int ( atoi line-buffer ) )
( printf "Reading %d puzzles\n" num-puzzles )
( when g-puzzle-list ( free ( type-cast g-puzzle-list ( * void ) ) ) )
( set g-num-puzzles num-puzzles )
( set g-puzzle-list ( type-cast ( calloc g-num-puzzles ( sizeof ( type puzzle-data ) ) ) ( * puzzle-data ) ) )
;; Read puzzles line by line
( var current-puzzle ( * puzzle-data ) g-puzzle-list )
( while ( and ( fgets line-buffer ( array-size line-buffer ) puzzles-file )
( < ( - current-puzzle g-puzzle-list ) g-num-puzzles ) )
( var num-moves int 0 )
( var num-states int 0 )
( var pieces-read bool false )
( var buffer ( [] 37 char ) ( array 0 ) )
( var write-char ( * char ) buffer )
( var current-char ( * ( const char ) ) line-buffer )
( while ( deref current-char )
( cond
;; Space field delimiter
( ( = ( deref current-char ) ( space-hack ) )
( cond
( ( not num-moves )
( set num-moves ( atoi buffer ) ) )
( true ;; Copy pieces
;; TODO Magic number
( memcpy ( path current-puzzle > board ) buffer 36 )
( set pieces-read true ) ) )
;; Reset buffer after space
( memset buffer 0 ( sizeof buffer ) )
( set write-char buffer ) )
( ( = ( deref current-char ) '\n ' )
( break ) )
( true
( set ( deref write-char ) ( deref current-char ) )
( incr write-char ) ) )
( incr current-char ) )
( set ( path current-puzzle > num-moves ) num-moves )
( set num-states ( atoi buffer ) )
( set ( path current-puzzle > num-states ) num-states )
( incr current-puzzle ) )
( fclose puzzles-file )
( return ( > g-num-puzzles 0 ) ) )
( defun-local game-piece-grid-position-to-screen-position ( piece ( * ( const board-piece ) ) &return vec2 )
( var piece-position vec2 ( array ( type-cast ( vec-x ( path piece > grid-position ) ) float )
( type-cast ( vec-y ( path piece > grid-position ) ) float ) ) )
@ -646,6 +684,7 @@ Rush Hour database from Michael Fogleman.\n\n")
;; (unless (bunzip-decompress puzzle-database-filename)
;; (return 1))
;; (sdl-print-time-delta start-load-ticks "Database loaded")
( unless ( read-puzzles ) ( return 1 ) )
( var background-texture ( * SDL_Texture ) ( sdl-texture-from-bmp ( in-data-dir "Board.bmp" )
renderer ) )
@ -694,8 +733,9 @@ Rush Hour database from Michael Fogleman.\n\n")
( sdl-print-time-delta start-load-ticks "Textures loaded" )
;; (game-board-load "60 IBBxooIooLDDJAALooJoKEEMFFKooMGGHHHM 2332")
( game-board-load "07 BBBoooooFxooAAFGooEooGooEDDDoooooooo 478" )
;; (game-board-load "IBBxooIooLDDJAALooJoKEEMFFKooMGGHHHM")
( srand ( type-cast ( SDL_GetPerformanceCounter ) int ) )
( game-board-load ( field ( at ( mod ( rand ) g-num-puzzles ) g-puzzle-list ) board ) )
;;
;; Game loop
;;
@ -821,7 +861,7 @@ Rush Hour database from Michael Fogleman.\n\n")
( when ( do-button ( addr in-state ) ( array 0.f 0.f ) ( array 166.f 166.f ) renderer theme-button-texture )
( set is-day-mode ( not is-day-mode ) ) )
( when ( do-button ( addr in-state ) ( array 300.f 0.f ) ( array 166.f 166.f ) renderer undo-button-texture )
( set is-day-mode ( not is-day-mode ) ) ) )
( game-board-load ( field ( at ( mod ( rand ) g-num-puzzles ) g-puzzle-list ) board ) ) ) )
( when ( is-in-win-state )
( var dest-rect SDL_Rect ( array 60 140 960 500 ) )
@ -936,6 +976,8 @@ Rush Hour database from Michael Fogleman.\n\n")
( when exit-reason
( printf "Exiting. Reason: %s\n" exit-reason ) )
( when g-puzzle-list ( free ( type-cast g-puzzle-list ( * void ) ) ) )
( SDL_DestroyTexture background-texture )
( set background-texture null )
( SDL_DestroyTexture grid-texture )
@ -952,6 +994,7 @@ Rush Hour database from Michael Fogleman.\n\n")
( SDL_DestroyRenderer renderer )
( sdl-shutdown window )
( return 0 ) )
;;