Browse Source

Use index instead of pointer for board state

This changes the size of the occupied state array from 288 bytes to
36, which just seemed criminal not to do.

Also ensure memset is setting the whole thing, not just the number of
elements' worth in bytes.
Macoy Madson 3 years ago
  1. 39


@ -65,8 +65,10 @@
label char)
;; Pieces are stored separately, then their positions inform occupied state, which is used to check
;; movement constraints. null = empty cell. Pointers used to easily pick from tap/click
(var g-game-board-occupied-state ([] 6 ([] 6 (* board-piece))) (array 0))
;; movement constraints. -1 = empty cell. Values are indices into g-game-board-pieces. Used to
;; easily pick from tap/click
(def-type-alias BoardPieceIndex char)
(var g-game-board-occupied-state ([] 6 ([] 6 BoardPieceIndex)) (array 0))
;; Max pieces is determined as follows:
;; 6x6 grid = 36 squares
@ -74,27 +76,29 @@
;; 36 squares / 2 (min piece size) = 13; if two walls, need 14
(var g-game-board-pieces ([] 14 board-piece) (array 0))
(defmacro on-each-existing-board-piece (piece-pointer-name symbol &rest body any)
(defmacro on-each-existing-board-piece (piece-pointer-name symbol piece-index-name symbol &rest body any)
(var next-piece-index int 0)
(var (token-splice piece-index-name) int 0)
(while (< next-piece-index (array-size g-game-board-pieces))
(var (token-splice piece-pointer-name) (* board-piece)
(addr (at next-piece-index g-game-board-pieces)))
(set (token-splice piece-index-name) next-piece-index)
(incr next-piece-index) ;; In case expansions include (continue), we've already incremented
(when (path (token-splice piece-pointer-name) > num-cells)
(token-splice-rest body tokens))))
(return true))
(defun-local print-board-piece (piece (* (const board-piece)))
(printf "Piece %c ( %p )
(defun-local print-board-piece (piece (* (const board-piece)) index int)
(printf "Piece %c ([%d] %p )
\n\tgrid-position %d %d
\n\tmoving-position %f %f
\n\tnum-cells %d
\n\tis-vertical %d
\n\tis-wall %d
\n\tis-primary-piece %d\n\n"
(path piece > label) piece
(path piece > label) index piece
(field (path piece > grid-position) X) (field (path piece > grid-position) Y)
(field (path piece > moving-position) X) (field (path piece > moving-position) Y)
(path piece > num-cells)
@ -104,10 +108,11 @@
(defun-local game-board-sync-occupied-state (&return bool)
;; Zero out to make overlap validation easy
(memset g-game-board-occupied-state 0 (array-size g-game-board-occupied-state))
(memset g-game-board-occupied-state -1
(sizeof g-game-board-occupied-state))
(print-board-piece piece)
piece piece-index
(print-board-piece piece piece-index)
(var num-cells int (path piece > num-cells))
@ -139,23 +144,25 @@
(set (vec-x cell-to-set) (+ cell-offset (vec-x (path piece > grid-position))))
(set (vec-y cell-to-set) (vec-y (path piece > grid-position)))))
(var occupy-space-pointer (* (* board-piece))
(var occupy-space-pointer (* BoardPieceIndex)
(addr (at (vec-y cell-to-set) (vec-x cell-to-set) g-game-board-occupied-state)))
(when (deref occupy-space-pointer)
(printf "error: Piece %p overlapping %p at (%d, %d)! Aborting\n"
piece (deref occupy-space-pointer)
(when (!= -1 (deref occupy-space-pointer))
(printf "error: Piece %d overlapping %d at (%d, %d)! Aborting\n"
piece-index (deref occupy-space-pointer)
(vec-xy cell-to-set))
(return false))
(set (deref occupy-space-pointer) piece)
(set (deref occupy-space-pointer) piece-index)
(incr cell-offset)))
;; Print board
(var row int 0)
(while (< row g-game-board-grid-size)
(var column int 0)
(while (< column g-game-board-grid-size)
(var piece-in-cell (* board-piece) (at row column g-game-board-occupied-state))
(if (and piece-in-cell (path piece-in-cell > num-cells))
(var index BoardPieceIndex (at row column g-game-board-occupied-state))
(if (and (>= index 0) (field (at index g-game-board-pieces) num-cells))
(var piece-in-cell (* board-piece) (addr (at index g-game-board-pieces)))
(var label char (path piece-in-cell > label))
(printf "%c " (? label label '#')))
(printf ". "))