unblockjam: add some puzzle utilities
- Commit
1b28e8bc1fcf427bc3aeb729c70102fd147e53e7- Author
- Anonymous <[email protected]>
- When
- 2026-08-13 04:05 UTC (3 months ago)
- Parents
3d79ef6
Diff
scripts/unblockjam/level_tools/README.md
diff --git a/scripts/unblockjam/level_tools/README.md b/scripts/unblockjam/level_tools/README.mdnew file mode 100644index 0000000000..bc1bf9bd8d--- /dev/null+++ b/scripts/unblockjam/level_tools/README.md@@ -0,0 +1,20 @@+## Puzzle converter+Converts puzzles from [Michael Fogleman's format](https://www.michaelfogleman.com/rush/#DatabaseFormat) to unblockjam's JSON format.++> [!IMPORTANT]+> The output level pack will have a default pack name and all colors set to `0`. You will need to manually set these in the resulting file.++## Usage+`Stdin`: Puzzles in [this format](https://www.michaelfogleman.com/rush/#DatabaseFormat).+`Stdout`: Converted level pack.++## Example+You can find a large set of puzzles [here](https://www.michaelfogleman.com/rush/#DatabaseDownload).++```sh+cd src+cat example_levels.txt | go run cmd/convert_set > example_pack.json+```++## Level IDs+Level IDs are automatically generated. They are a 96-bit slice of the board's sha256 representation encoded in URL-safe base64. I'm pretty sure this should suffice for avoiding collisions. There's also a version number (`n-`) prefixed onto each ID for future-proofing.\ No newline at end of file
scripts/unblockjam/level_tools/src/cmd/convert_set/main.go
diff --git a/scripts/unblockjam/level_tools/src/cmd/convert_set/main.go b/scripts/unblockjam/level_tools/src/cmd/convert_set/main.gonew file mode 100644index 0000000000..81533782f6--- /dev/null+++ b/scripts/unblockjam/level_tools/src/cmd/convert_set/main.go@@ -0,0 +1,72 @@+package main++import (+ "bufio"+ "encoding/json"+ "fmt"+ "os"+ "strconv"+ "strings"++ "rush_conv"+)++func main() {+ if err := run(); err != nil {+ fmt.Fprintln(os.Stderr, err)+ os.Exit(1)+ }+}++func run() error {+ var levels []rush_conv.Level++ scanner := bufio.NewScanner(os.Stdin)+ for scanner.Scan() {+ line := scanner.Text()+ parts := strings.Fields(line)+ if len(parts) != 3 {+ return fmt.Errorf("expected exactly 3 parts in line %q, got %d", line, len(parts))+ }++ optimal, err := strconv.Atoi(parts[2])+ if err != nil {+ return fmt.Errorf("converting optimal value to integer in line %q: %w", line, err)+ }++ puzzle := parts[1]+ board := rush_conv.Convert(puzzle)++ level := rush_conv.Level{+ Board: board,+ ID: rush_conv.BoardID(board),+ Moves: optimal,+ }++ levels = append(levels, level)+ }+ if err := scanner.Err(); err != nil {+ return fmt.Errorf("reading stdin: %w", err)+ }++ result := map[string]interface{}{+ "name": "Example Pack",+ "colors": map[string]string{+ "primary": "0x00000000",+ "secondary": "0x00000000",+ "tertiary": "0x00000000",+ "background": "0x00000000",+ "surface": "0x00000000",+ "primaryContainer": "0x00000000",+ "secondaryContainer": "0x00000000",+ },+ "levels": levels,+ }++ encoder := json.NewEncoder(os.Stdout)+ encoder.SetIndent("", " ")+ if err := encoder.Encode(result); err != nil {+ return fmt.Errorf("encoding JSON: %w", err)+ }+ return nil+}
scripts/unblockjam/level_tools/src/cmd/get_id/main.go
diff --git a/scripts/unblockjam/level_tools/src/cmd/get_id/main.go b/scripts/unblockjam/level_tools/src/cmd/get_id/main.gonew file mode 100644index 0000000000..c897fb363d--- /dev/null+++ b/scripts/unblockjam/level_tools/src/cmd/get_id/main.go@@ -0,0 +1,33 @@+package main++import (+ "encoding/json"+ "fmt"+ "io"+ "os"++ "rush_conv"+)++func main() {+ if err := run(); err != nil {+ fmt.Fprintln(os.Stderr, err)+ os.Exit(1)+ }+}++func run() error {+ data, err := io.ReadAll(os.Stdin)+ if err != nil {+ return fmt.Errorf("reading stdin: %w", err)+ }++ var board rush_conv.Board+ if err := json.Unmarshal(data, &board); err != nil {+ return fmt.Errorf("parsing board JSON: %w\ninput should be a single board in JSON format", err)+ }++ fmt.Println(rush_conv.BoardID(board))++ return nil+}\ No newline at end of file
scripts/unblockjam/level_tools/src/convert.go
diff --git a/scripts/unblockjam/level_tools/src/convert.go b/scripts/unblockjam/level_tools/src/convert.gonew file mode 100644index 0000000000..0f54388326--- /dev/null+++ b/scripts/unblockjam/level_tools/src/convert.go@@ -0,0 +1,58 @@+package rush_conv++import (+ "github.com/fogleman/rush"+)++func tocoord(position int, board_size int) (int, int) {+ x := position % board_size+ y := board_size - 1 - (position / board_size)+ return x, y+}++func tosize(size int, orientation int) (int, int) {+ if orientation == 0 {+ return size, 1+ }+ return 1, size+}++func Convert(board_desc string) Board {+ board, _ := rush.NewBoardFromString(board_desc)++ blocks := make([]Block, 0, len(board.Pieces)+len(board.Walls))++ for _, piece := range board.Pieces {+ x, y := tocoord(piece.Position, board.Width)+ if piece.Orientation == 1 {+ y -= piece.Size - 1+ }+ w, h := tosize(piece.Size, int(piece.Orientation))+ blocks = append(blocks, Block{+ X: x,+ Y: y,+ Width: w,+ Height: h,+ })+ }+ for _, wall := range board.Walls {+ x, y := tocoord(wall, board.Width)+ blocks = append(blocks, Block{+ X: x,+ Y: y,+ Width: 1,+ Height: 1,+ Fixed: true,+ })+ }++ return Board{+ Blocks: blocks,+ Exit: Exit{+ X: board.Width,+ Y: board.Height / 2,+ },+ Width: board.Width,+ Height: board.Height,+ }+}\ No newline at end of file
scripts/unblockjam/level_tools/src/go.mod
diff --git a/scripts/unblockjam/level_tools/src/go.mod b/scripts/unblockjam/level_tools/src/go.modnew file mode 100644index 0000000000..8c1ba350c2--- /dev/null+++ b/scripts/unblockjam/level_tools/src/go.mod@@ -0,0 +1,11 @@+module rush_conv++go 1.26.4++require github.com/fogleman/rush v0.0.0-20240610155544-3e3b8396891d++require (+ github.com/fogleman/gg v1.3.0 // indirect+ github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect+ golang.org/x/image v0.44.0 // indirect+)
scripts/unblockjam/level_tools/src/go.sum
diff --git a/scripts/unblockjam/level_tools/src/go.sum b/scripts/unblockjam/level_tools/src/go.sumnew file mode 100644index 0000000000..8bc3185002--- /dev/null+++ b/scripts/unblockjam/level_tools/src/go.sum@@ -0,0 +1,8 @@+github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=+github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=+github.com/fogleman/rush v0.0.0-20240610155544-3e3b8396891d h1:aGLEr2e8hkf/G5Hn63Xk8ZZ+fsxvw5easqMWWMXUbc4=+github.com/fogleman/rush v0.0.0-20240610155544-3e3b8396891d/go.mod h1:kksjrhrbCEDMSF1oVARUVGqHQnM3GBdWmjzDVuspkQ0=+github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=+github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=+golang.org/x/image v0.44.0 h1:+tDekMZED9+LrtB3G5xzRggpVh9CARjZqROla3R3R+I=+golang.org/x/image v0.44.0/go.mod h1:V8K3KE9KKKE+pLpQDOeN18w9oacNSvy1tDOirTu4xtY=
scripts/unblockjam/level_tools/src/hash.go
diff --git a/scripts/unblockjam/level_tools/src/hash.go b/scripts/unblockjam/level_tools/src/hash.gonew file mode 100644index 0000000000..44387259f5--- /dev/null+++ b/scripts/unblockjam/level_tools/src/hash.go@@ -0,0 +1,52 @@+package rush_conv++import (+ "crypto/sha256"+ "encoding/base64"+ "fmt"+ "sort"+)++const ID_VERSION = 1++func BoardID(board Board) string {+ sort.Slice(board.Blocks, func(i, j int) bool {+ a, b := board.Blocks[i], board.Blocks[j]++ if a.X != b.X {+ return a.X < b.X+ }+ if a.Y != b.Y {+ return a.Y < b.Y+ }+ if a.Width != b.Width {+ return a.Width < b.Width+ }+ if a.Height != b.Height {+ return a.Height < b.Height+ }+ return !a.Fixed && b.Fixed+ })++ h := sha256.New()++ h.Write([]byte{byte(board.Width), byte(board.Height), byte(board.Exit.X), byte(board.Exit.Y)})++ for _, b := range board.Blocks {+ h.Write([]byte{+ byte(b.X),+ byte(b.Y),+ byte(b.Width),+ byte(b.Height),+ })++ if b.Fixed {+ h.Write([]byte{1})+ } else {+ h.Write([]byte{0})+ }+ }++ sum := h.Sum(nil)+ return fmt.Sprintf("%d-%s", ID_VERSION, base64.RawURLEncoding.EncodeToString(sum[:12]))+}\ No newline at end of file
scripts/unblockjam/level_tools/src/types.go
diff --git a/scripts/unblockjam/level_tools/src/types.go b/scripts/unblockjam/level_tools/src/types.gonew file mode 100644index 0000000000..22a5362519--- /dev/null+++ b/scripts/unblockjam/level_tools/src/types.go@@ -0,0 +1,27 @@+package rush_conv++type Block struct {+ X int `json:"x"`+ Y int `json:"y"`+ Width int `json:"w"`+ Height int `json:"h"`+ Fixed bool `json:"fixed,omitempty"`+}++type Exit struct {+ X int `json:"x"`+ Y int `json:"y"`+}++type Board struct {+ Blocks []Block `json:"b"`+ Exit Exit `json:"e"`+ Width int `json:"w"`+ Height int `json:"h"`+}++type Level struct {+ Board+ ID string `json:"id"`+ Moves int `json:"c"`+}\ No newline at end of file