diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e2bc49..d1a77de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 0.2.0 (unreleased) +- **Hazards (#5):** wind, falling roof tiles with a shadow first, lifts on a rope, and drawbridges, each a function of the frame count. Rooftops, The Canal, Market Square, Old Wall, and Lantern Hill have them, with new routes. - **Level files and 5 new levels (#4):** each hand level is a file of `levels/`, which the core checks part by part, with the file and the line in an error. Rooftops, The Canal, Market Square, Old Wall, and Lantern Hill are new. `tools/solve.tcl` found a route for each, and the tests replay the 10 routes. - **Save and continue (#3):** the save goes through `ztk::game save` in the data folder of the app, and the play page keeps it in the local storage of the browser. Continue goes to the entry that the player started last, when it is open and not done. A save of 0.1.0 is read and moved, and a save of an unknown version or a bad save gives a new game. - **Title and pause (#2):** the title is a menu of Continue, All streets, Endless street, and Quit on a desktop. Escape, Enter, or the mark "II" pauses a level or a run, and the pause has Resume, Restart, and Title. A window that goes to the background pauses the game. diff --git a/core/README.md b/core/README.md index 37d319a..e6f29c6 100644 --- a/core/README.md +++ b/core/README.md @@ -8,6 +8,7 @@ The rules and the data of the game. The core has no Tk, reads no clock, opens no | `levels.tcl` | The check of a level file (`parseLevel`), the streets of the generator, and the facts of each entry. | | `color.tcl` | The palette, the color math, and the themes with their rules of contrast. | | `progress.tcl` | The best times, the stars, the open entries, and the save format. | +| `hazards.tcl` | Wind, falling tiles, lifts, and drawbridges: each one a function of the frame count. | | `play.tcl` | The state, one frame of a hand level (`step`), the collisions, the lamps, and the events. | | `run.tcl` | The physics of a run, the generator with its proof of each piece, and one frame of a run (`runStep`). | | `route.tcl` | The moves of a route and its replay. | diff --git a/core/hazards.tcl b/core/hazards.tcl new file mode 100644 index 0000000..381e973 --- /dev/null +++ b/core/hazards.tcl @@ -0,0 +1,78 @@ +# The hazards and the moving things of the hand levels. Each one is a +# function of the frame count, so a level plays the same each time. +# +# - Wind {x0 x1 force period}: a gust for the first half of each period +# pushes the hero by force pixels each frame while it is between x0 +# and x1. A negative force pushes to the left. +# - Tile {x y0 period}: a roof tile falls at x once in each period. For +# the first 40 frames of the period its shadow shows on the floor. Then +# it falls from y0 at 4 pixels a frame to the floor under x. A tile +# that touches the hero puts out the flame, as the water does. +# - Lift {x0 y0 x1 y1 period}: a floor from x0 to x1 that goes from y0 +# to y1 and back in each period, on a cosine. The hero on it moves +# with it. +# - Drawbridge {x0 y x1 period}: a floor that is down for the first half +# of each period and up for the second half. +# +# No Tk, no clock, no file, no random. +# +# Copyright (C) 2026 Jeremy Collins +# SPDX-License-Identifier: AGPL-3.0-or-later + +proc gustOn {wind frames} { + lassign $wind x0 x1 force per + expr {$frames % int($per) < $per / 2.0} +} + +# The push of the winds of the street on the hero at x y. +proc windPush {x y frames} { + set push 0 + foreach w [data winds] { + lassign $w x0 x1 force + if {$x >= $x0 && $x <= $x1 && [gustOn $w $frames]} { set push [expr {$push + $force}] } + } + return $push +} + +# The top of the floor under x: the top of a block, or the bottom of the +# street. +proc floorUnder {x} { + set top $::H + foreach b [data blocks] { lassign $b x0 t x1; if {$x >= $x0 && $x <= $x1} { set top [expr {min($top, $t)}] } } + return $top +} + +# The state of a tile at a frame: {shadow}, {fall Y}, or {} when it is +# on the floor. +proc tileState {tile frames} { + lassign $tile x y0 per + set t [expr {$frames % int($per)}] + if {$t < 40} { return shadow } + set y [expr {$y0 + ($t - 40) * 4}] + if {$y >= [floorUnder $x]} { return {} } + list fall $y +} + +# 1 when a falling tile touches the hero at x y (y is the middle of its +# body, its feet at y + 7 and its hat at y - 12). +proc tileHit {x y frames} { + foreach tl [data tiles] { + set st [tileState $tl $frames] + if {[lindex $st 0] ne "fall"} continue + set ty [lindex $st 1] + if {abs($x - [lindex $tl 0]) < 9 && $ty + 6 >= $y - 12 && $ty <= $y + 7} { return 1 } + } + return 0 +} + +# The top of a lift at a frame. +proc liftY {lift frames} { + lassign $lift x0 y0 x1 y1 per + expr {$y0 + ($y1 - $y0) * (1 - cos($frames * 6.283185307179586 / $per)) / 2.0} +} + +# 1 when a drawbridge is down at a frame. +proc bridgeDown {bridge frames} { + lassign $bridge x0 y x1 per + expr {$frames % int($per) < $per / 2.0} +} diff --git a/core/levels.tcl b/core/levels.tcl index 18b0268..4fe4344 100644 --- a/core/levels.tcl +++ b/core/levels.tcl @@ -13,12 +13,14 @@ set W 472; set H 232 # springs boats crumbles water}: blocks {x0 top x1} that go to the # floor, ledges {x0 y x1} that you can jump up through, lamps {x y}, # springs {x y}, boats {x0 y x1 dx frames} that swing, crumbling boards -# {x0 y x1}, and water {x0 x1}. The gate is at the right of the last +# {x0 y x1}, water {x0 x1}, and the hazards (core/hazards.tcl): winds +# {x0 x1 force period}, tiles {x y0 period}, lifts {x0 y0 x1 y1 period}, +# and drawbridges {x0 y x1 period}. The gate is at the right of the last # street. set levels {} # The parts of a street and the count of numbers of each part. -set partSize {blocks 3 ledges 3 lamps 2 springs 2 boats 5 crumbles 3 water 2} +set partSize {blocks 3 ledges 3 lamps 2 springs 2 boats 5 crumbles 3 water 2 winds 4 tiles 3 lifts 5 bridges 4} # Reads the text of one level: a list of `street NAME {PART LIST ...}`. # file and line give the place of the text for an error. Gives the level @@ -51,6 +53,10 @@ proc parseLevel {name body {file level} {line 1}} { lamps - springs { lassign $p x y; set ok [expr {$x >= 0 && $x <= $W && $y >= 0 && $y <= $H}] } water { lassign $p x0 x1; set ok [expr {$x0 >= 0 && $x1 <= $W && $x0 < $x1}] } boats { lassign $p x0 y x1; set ok [expr {$x0 < $x1 && $y >= 0 && $y <= $H}] } + winds { lassign $p x0 x1 force per; set ok [expr {$x0 >= 0 && $x1 <= $W && $x0 < $x1 && abs($force) <= 3 && $per >= 2}] } + tiles { lassign $p x y0 per; set ok [expr {$x >= 0 && $x <= $W && $y0 >= 0 && $y0 <= $H && $per >= 60}] } + lifts { lassign $p x0 y0 x1 y1 per; set ok [expr {$x0 >= 0 && $x1 <= $W && $x0 < $x1 && $y0 >= 0 && $y0 <= $H && $y1 >= 0 && $y1 <= $H && $per >= 2 * abs($y1 - $y0)}] } + bridges { lassign $p x0 y x1 per; set ok [expr {$x0 >= 0 && $x1 <= $W && $x0 < $x1 && $y >= 0 && $y <= $H && $per >= 2}] } default { lassign $p x0 y x1; set ok [expr {$x0 >= 0 && $x1 <= $W && $x0 < $x1 && $y >= 0 && $y <= $H}] } } if {!$ok} { apply $fail "a part of $k in $sname is outside the street: $p" $sname } diff --git a/core/load.tcl b/core/load.tcl index 9c882a1..98e493c 100644 --- a/core/load.tcl +++ b/core/load.tcl @@ -4,7 +4,7 @@ # Copyright (C) 2026 Jeremy Collins # SPDX-License-Identifier: AGPL-3.0-or-later -foreach f {levels color progress play run route menu} { +foreach f {levels color progress hazards play run route menu} { source [file join [file dirname [info script]] $f.tcl] } unset f diff --git a/core/play.tcl b/core/play.tcl index 7d76d34..789db0f 100644 --- a/core/play.tcl +++ b/core/play.tcl @@ -2,7 +2,7 @@ # physics (`step`), the collisions with blocks, ledges, boats, boards, # and springs, the lamps, the night, and the end of a level. # -# A level is a walk through 2 or 3 streets. Walk off the right edge to +# A level is a walk through 1 to 3 streets. Walk off the right edge to # go to the next street. Touch a lamp to light it: each lamp pushes the # night back. Light all the lamps of a level to open its gate. # @@ -20,6 +20,7 @@ # crumbshake SCENE N DX a board shakes # crumbhide SCENE N a board breaks # crumbshow SCENE N a board comes back +# tilehit SCENE a falling tile hit the hero # # app(sfx) gets the name of each sound, and app(music) the name of the # music. The core has no random: the same inputs give the same frames. @@ -37,7 +38,7 @@ if {![info exists app(touch)]} { set app(touch) 0 } # The state of the game. array set g {state title level 0 scene 0 dark 0 frames 0 shake 0 freeze 0 pick 0 clear 0 msgT 0 unlocked 1 auto 0 cam 0 paused 0 picked 0} -array set h {x 30 y 150 vx 0 vy 0 ground 1 coyote 0 buffer 0 face 1 sx 1 sy 1 step 0 spawnS 0 spawnX 30 spawnY 150 ride -1 crumble -1} +array set h {x 30 y 150 vx 0 vy 0 ground 1 coyote 0 buffer 0 face 1 sx 1 sy 1 step 0 spawnS 0 spawnX 30 spawnY 150 ride -1 crumble -1 lift -1 liftY 0} array set hold {left 0 right 0 jump 0} set events {} @@ -53,7 +54,7 @@ proc say {a b} { proc lvl {} { lindex $::levels $::g(level) 1 } # The parts of the current street, read once when you enter it. proc loadStreet {} { - foreach what {name blocks ledges lamps springs boats crumbles water} v [lindex [lvl] $::g(scene)] { set ::cur($what) $v } + foreach what {name blocks ledges lamps springs boats crumbles water winds tiles lifts bridges} v [lindex [lvl] $::g(scene)] { set ::cur($what) $v } } proc data {what} { return $::cur($what) } @@ -63,7 +64,7 @@ proc startLevel {n} { array set g [list state play level $n scene 0 dark 0 frames 0 shake 0 freeze 0 clear 0 msgT 120 auto $app(touch) cam 0 paused 0] array unset lit; array unset crumb set app(music) play - array set h {x 24 y 150 vx 0 vy 0 ground 0 coyote 0 buffer 0 face 1 sx 1 sy 1 ride -1 crumble -1} + array set h {x 24 y 150 vx 0 vy 0 ground 0 coyote 0 buffer 0 face 1 sx 1 sy 1 ride -1 crumble -1 lift -1 liftY 0} array set h {spawnS 0 spawnX 24 spawnY 150} enter 0 24 } @@ -106,6 +107,18 @@ proc floors {} { if {![info exists crumb($g(scene),$n)] || [lindex $crumb($g(scene),$n) 0] < 40} { lappend out [concat $cr crumble $n] } incr n } + set n 0 + foreach lf [data lifts] { + lassign $lf x0 y0 x1 + lappend out [list $x0 [liftY $lf $g(frames)] $x1 lift $n] + incr n + } + set n 0 + foreach br [data bridges] { + lassign $br x0 y x1 per + if {[bridgeDown $br $g(frames)]} { lappend out [list $x0 $y $x1 bridge $n] } + incr n + } return $out } @@ -138,7 +151,7 @@ proc step {} { if {$h(buffer) > 0} { incr h(buffer) -1 } if {$h(ground)} { set h(coyote) 6 } elseif {$h(coyote) > 0} { incr h(coyote) -1 } if {$h(buffer) > 0 && $h(coyote) > 0} { - set h(vy) -6.3; set h(buffer) 0; set h(coyote) 0; set h(ground) 0; set h(ride) -1 + set h(vy) -6.3; set h(buffer) 0; set h(coyote) 0; set h(ground) 0; set h(ride) -1; set h(lift) -1 set h(sx) 0.7; set h(sy) 1.35; sfx jump burst $h(x) [expr {$h(y) + 7}] 4 #c8b8e0 } @@ -147,7 +160,13 @@ proc step {} { if {$h(ride) >= 0} { foreach f [floors] { lassign $f x0 y x1 kind n; if {$kind eq "boat" && $n == $h(ride)} { set h(x) [expr {$h(x) + ($x0 - $h(rideX))}]; set h(rideX) $x0 } } } - set h(x) [expr {$h(x) + $h(vx)}] + # Ride a lift: move up and down with it. + if {$h(lift) >= 0} { + set y [liftY [lindex [data lifts] $h(lift)] $g(frames)] + set h(y) [expr {$h(y) + ($y - $h(liftY))}] + set h(liftY) $y + } + set h(x) [expr {$h(x) + $h(vx) + [windPush $h(x) $h(y) $g(frames)]}] # The sides of the blocks. foreach b [data blocks] { lassign $b x0 top x1 @@ -172,12 +191,14 @@ proc step {} { set h(vy) 0; set h(ground) 1 set h(ride) [expr {$kind eq "boat" ? $n : -1}] if {$kind eq "boat"} { set h(rideX) $x0 } + set h(lift) [expr {$kind eq "lift" ? $n : -1}] + if {$kind eq "lift"} { set h(liftY) $y } if {$kind eq "crumble" && ![info exists crumb($g(scene),$n)]} { set crumb($g(scene),$n) {0} } break } } } - if {!$h(ground)} { set h(ride) -1 } + if {!$h(ground)} { set h(ride) -1; set h(lift) -1 } # The boards break 40 frames after a step, and come back after 3 s. foreach k [array names crumb $g(scene),*] { set n [lindex [split $k ,] 1] @@ -194,6 +215,8 @@ proc step {} { set h(vy) -9.6; set h(ground) 0; set h(sx) 0.6; set h(sy) 1.5; sfx pop; burst $sx $sy 6 #a6e6b2 1 } } + # A falling tile puts out the flame. + if {[tileHit $h(x) $h(y) $g(frames)]} { emit tilehit $g(scene); hurt; return } # The lamps. set k 0 foreach l [data lamps] { diff --git a/docs/how-to-play.md b/docs/how-to-play.md index 7c2fd0c..1bed56a 100644 --- a/docs/how-to-play.md +++ b/docs/how-to-play.md @@ -42,6 +42,10 @@ Escape, Enter, or the mark at the top right pauses a level or a run. The pause h | Boat | A floor that swings over the water. It moves you with it. | | Spring | Sends you high into the air. | | Water | Puts out the flame. | +| Wind | A gust pushes you for half of each period. Streaks show it. | +| Falling tile | A shadow shows on the floor, then a roof tile falls there. It puts out the flame. | +| Lift | A floor on a rope that goes up and down. It moves you with it. | +| Drawbridge | A floor that is down for half of each period and up for the other half. | ## Streets and stars diff --git a/docs/levels.md b/docs/levels.md index eda93bc..d4c01f3 100644 --- a/docs/levels.md +++ b/docs/levels.md @@ -17,7 +17,7 @@ | 9 | Old Wall | Gate Arch, Rampart, Watchtower | A board under an arch, a moat, and a spring to a ledge. | | 10 | Lantern Hill | Path, Terraces, Crown | Steps, a boat and a board over the water, and the crown of the hill. | -Each level is a file of `levels/`. A file is a list of `street NAME {PART LIST ...}`, with the parts blocks, ledges, lamps, springs, boats, crumbles (boards), and water. The core checks each part when the game reads the file, and a fault gives the file and the line. +Each level is a file of `levels/`. A file is a list of `street NAME {PART LIST ...}`, with the parts blocks, ledges, lamps, springs, boats, crumbles (boards), water, winds, tiles, lifts, and bridges (drawbridges). The core checks each part when the game reads the file, and a fault gives the file and the line. The tests replay a route through each hand level and check that it finishes the level. `tools/solve.tcl` finds the routes. diff --git a/levels/06-rooftops.tcl b/levels/06-rooftops.tcl index 410b9c3..6c3c05b 100644 --- a/levels/06-rooftops.tcl +++ b/levels/06-rooftops.tcl @@ -1,4 +1,5 @@ -# Rooftops: chimneys, gutters over the water, and a weathervane. +# Rooftops: chimneys, gutters over the water with a falling tile, and a +# weathervane in the wind. # # Copyright (C) 2026 Jeremy Collins # SPDX-License-Identifier: AGPL-3.0-or-later @@ -13,10 +14,12 @@ level Rooftops { blocks {{0 196 90} {150 180 250} {310 160 472}} lamps {{200 150} {430 130}} water {{90 150} {250 310}} + tiles {{380 0 150}} } street Weathervane { blocks {{0 196 472}} ledges {{80 166 150} {170 136 240} {260 106 330}} lamps {{295 76} {60 166}} + winds {{140 340 -0.9 200}} } } diff --git a/levels/07-the-canal.tcl b/levels/07-the-canal.tcl index 4702dcb..a530c74 100644 --- a/levels/07-the-canal.tcl +++ b/levels/07-the-canal.tcl @@ -1,4 +1,5 @@ -# The canal: a lock with a boat, a towpath with a spring, and a footbridge of boards. +# The canal: a lock with a boat, a towpath with a spring, and a +# footbridge of boards with a drawbridge in the middle. # # Copyright (C) 2026 Jeremy Collins # SPDX-License-Identifier: AGPL-3.0-or-later @@ -19,7 +20,8 @@ level {The Canal} { street Footbridge { blocks {{0 196 100} {380 196 472}} lamps {{240 146} {430 166}} - crumbles {{100 186 180} {200 176 280} {300 186 380}} + crumbles {{100 186 180} {300 186 380}} + bridges {{200 176 280 180}} water {{100 380}} } } diff --git a/levels/08-market-square.tcl b/levels/08-market-square.tcl index 48656d5..5a20cdf 100644 --- a/levels/08-market-square.tcl +++ b/levels/08-market-square.tcl @@ -1,4 +1,5 @@ -# Market square: the awnings of the stalls, a fountain with a spring, and the clock. +# Market square: the awnings of the stalls, a fountain with a spring, +# and the clock with a lift. # # Copyright (C) 2026 Jeremy Collins # SPDX-License-Identifier: AGPL-3.0-or-later @@ -18,6 +19,7 @@ level {Market Square} { street Clock { blocks {{0 196 472}} ledges {{100 150 170} {200 110 270} {300 70 370}} - lamps {{335 40} {135 120}} + lamps {{335 40} {135 120} {405 60}} + lifts {{380 190 430 100 240}} } } diff --git a/levels/09-old-wall.tcl b/levels/09-old-wall.tcl index 138fbb4..b7fece4 100644 --- a/levels/09-old-wall.tcl +++ b/levels/09-old-wall.tcl @@ -1,4 +1,5 @@ -# Old wall: an arch with a board, the rampart over a moat, and the watchtower. +# Old wall: an arch with a board, the rampart over a moat in the wind, +# and the watchtower. # # Copyright (C) 2026 Jeremy Collins # SPDX-License-Identifier: AGPL-3.0-or-later @@ -13,6 +14,7 @@ level {Old Wall} { blocks {{0 196 60} {60 146 200} {260 146 400} {400 196 472}} lamps {{130 116} {330 116}} water {{200 260}} + winds {{150 300 1.0 160}} } street Watchtower { blocks {{0 196 472}} diff --git a/levels/10-lantern-hill.tcl b/levels/10-lantern-hill.tcl index 39369f8..9c4cfdb 100644 --- a/levels/10-lantern-hill.tcl +++ b/levels/10-lantern-hill.tcl @@ -1,4 +1,5 @@ -# Lantern hill: a path of steps, terraces over the water, and the crown of the hill. +# Lantern hill: a path of steps, terraces over the water under a +# falling tile, and the crown of the hill. # # Copyright (C) 2026 Jeremy Collins # SPDX-License-Identifier: AGPL-3.0-or-later @@ -15,6 +16,7 @@ level {Lantern Hill} { boats {{90 186 150 60 100}} crumbles {{200 166 260}} water {{80 380}} + tiles {{320 0 160}} } street Crown { blocks {{0 196 472}} diff --git a/levels/README.md b/levels/README.md index 0460cea..a97c63e 100644 --- a/levels/README.md +++ b/levels/README.md @@ -11,5 +11,9 @@ One file for each hand level, in the order of `files` in `load.tcl`. A file call | `boats` | `{x0 y x1 dx frames}`: a floor that swings by dx in the given frames. | | `crumbles` | `{x0 y x1}`: a board that breaks 40 frames after a step. | | `water` | `{x0 x1}` | +| `winds` | `{x0 x1 force period}`: a gust for the first half of each period pushes the hero by force pixels a frame. | +| `tiles` | `{x y0 period}`: a shadow for 40 frames, then a tile falls from y0 at x. | +| `lifts` | `{x0 y0 x1 y1 period}`: a floor that goes from y0 to y1 and back. The period is at least 2 times the distance. | +| `bridges` | `{x0 y x1 period}`: a floor that is down for the first half of each period. | A street is 472 by 232. A level has 1 to 3 streets, and the gate is at the right of the last one. `parseLevel` of `core/levels.tcl` checks each part. A new level gets a route from `tools/solve.tcl` in `tests/data/routes.txt`. diff --git a/tests/data/routes.txt b/tests/data/routes.txt index e16ad13..29ba454 100644 --- a/tests/data/routes.txt +++ b/tests/data/routes.txt @@ -3,8 +3,8 @@ 2 552 {R R RJ R R R RJ - R - R RJ LJ LJ L - L R - R L R RJ RJ RJ RJ R R R RJ R R R RJ RJ R R R RJ RJ RJ RJ R R RJ J RJ J L - RJ J RJ - - L R R - - R RJ RJ RJ RJ R R RJ R R RJ RJ RJ R R RJ RJ RJ RJ R R R RJ R RJ R R RJ R R R R} 3 511 {R R R RJ RJ R R RJ RJ RJ R R RJ R R RJ R R R R RJ R R RJ R R RJ R R RJ R R RJ R RJ R R RJ RJ R R RJ R R R R R R RJ R R R R R R RJ RJ RJ RJ R RJ R R RJ RJ RJ R R RJ RJ RJ RJ R R RJ RJ RJ RJ R RJ R R R R R R} 4 558 {R R RJ RJ R R R RJ RJ RJ RJ R R R RJ R R - R RJ R R R RJ R R RJ RJ RJ RJ R R RJ RJ J LJ RJ - - - R R RJ J L L J J L R RJ RJ RJ LJ - - R R R - - R RJ R R R R R R RJ R R R RJ RJ RJ RJ RJ R R RJ RJ RJ RJ R R R R R R R R R} -5 528 {R R R R R RJ RJ R R RJ RJ RJ L L - R RJ RJ R RJ RJ R RJ R R RJ RJ R R R RJ RJ RJ RJ RJ R RJ R R RJ RJ J RJ J L - RJ J R - L L LJ LJ LJ J R R RJ RJ RJ R R R RJ R R RJ RJ RJ RJ RJ R RJ RJ R R R R R R R R R R R R R} -6 961 {R R RJ R R R RJ RJ R R R RJ RJ R R R R L R R RJ RJ R R - RJ RJ R R R RJ RJ RJ RJ R R R R R R RJ R R R RJ RJ RJ R R R R RJ RJ RJ RJ R R R R R R RJ RJ R R - RJ RJ RJ R R R RJ R R R R R R RJ R R R R R L R J R R R RJ RJ RJ R R R R RJ R R R RJ RJ RJ R R R RJ RJ RJ R R R R R RJ R R R R RJ R R R R R R R RJ RJ RJ RJ RJ R RJ RJ RJ R RJ R RJ RJ RJ R R R RJ R R R RJ R R RJ R RJ R R R R} -7 521 {R R R R RJ RJ RJ RJ R RJ RJ R RJ R RJ R R RJ RJ RJ RJ RJ R R R R RJ R R R RJ RJ R R R R RJ RJ RJ RJ R R L J RJ RJ R R R R R R R RJ RJ RJ R R R RJ RJ RJ RJ R RJ R R RJ RJ RJ RJ R R RJ RJ RJ R R R R R R R R R R R} -8 511 {R R R R R RJ RJ R RJ R R R RJ RJ R R RJ RJ R R R R RJ RJ RJ R R R RJ RJ RJ RJ RJ R R RJ RJ R RJ R RJ RJ RJ R R R RJ RJ RJ R R R RJ RJ RJ RJ RJ R R R R R RJ RJ RJ R R R R RJ RJ RJ R R RJ R R R R R R R R R R R} +5 559 {R R R R R RJ RJ R R RJ RJ RJ L L - R RJ RJ R RJ RJ R RJ R R RJ RJ R R R RJ RJ RJ RJ RJ R RJ R R RJ RJ J RJ J L - RJ J R - L L LJ LJ LJ J R R R RJ RJ RJ RJ R R R RJ RJ RJ RJ R R R RJ RJ RJ RJ R RJ R R R R R R R R R R R R R R R} +6 1204 {R R RJ R R R RJ RJ R R R RJ RJ R R R R L R R RJ RJ R R - RJ RJ R R R RJ RJ RJ RJ R R R R R R RJ R R R RJ RJ RJ R R R R RJ RJ RJ RJ R R R R R R RJ RJ R R - RJ RJ RJ R R R RJ R R R R R R RJ R R R R R L R J R R R RJ RJ RJ R R R R RJ R R R RJ RJ RJ R R R RJ RJ RJ R R R R R RJ R R R R RJ R R R R R R R RJ RJ RJ RJ RJ R RJ RJ R R RJ R R - RJ RJ RJ LJ L L R R R R RJ R R J L - J J L RJ R RJ RJ RJ RJ R R R R R R RJ R R RJ RJ R R RJ R R R R R RJ R RJ R RJ R R RJ R RJ R R R R} +7 521 {R R R R RJ RJ RJ RJ R RJ RJ R RJ R RJ R R RJ RJ RJ RJ RJ R R R R RJ R R R RJ RJ R R R R RJ RJ RJ RJ R R L J RJ RJ R R R R R R RJ R R R RJ RJ RJ R R R RJ RJ RJ R R RJ RJ RJ RJ R RJ RJ RJ RJ R RJ RJ R RJ R R R R R R} +8 505 {R R R R R RJ RJ R RJ R R R RJ RJ R R RJ RJ R R R R RJ RJ RJ R R R RJ RJ RJ RJ RJ R R RJ R R R RJ RJ RJ RJ R R R RJ RJ R R R RJ RJ RJ RJ R R R R R R RJ RJ RJ R R RJ R R RJ RJ RJ RJ R R R R R R R R R R R R} 9 490 {R R R RJ RJ R R R RJ RJ RJ R RJ R R R R RJ R R R RJ R R RJ RJ RJ R RJ R R R R L R RJ R R RJ RJ R R R RJ RJ RJ R R R R RJ RJ RJ RJ RJ R R R RJ RJ R R LJ RJ J RJ R RJ R R R RJ R R R R R R R R R R} diff --git a/tests/hazards.test b/tests/hazards.test new file mode 100644 index 0000000..5babc9e --- /dev/null +++ b/tests/hazards.test @@ -0,0 +1,118 @@ +# Tests of the hazards (core/hazards.tcl) in the physics of a hand level: +# wind, falling tiles, lifts, and drawbridges. +# +# Copyright (C) 2026 Jeremy Collins +# SPDX-License-Identifier: AGPL-3.0-or-later +source [file join [file dirname [info script]] .. levels load.tcl] + +# Hand level 0 with one street of the given parts, for one test. +proc testStreet {parts} { + set ::saved $::levels + lset ::levels 0 [parseLevel Test [list street Test $parts]] + startLevel 0 + array set ::hold {left 0 right 0 jump 0} + set ::events {} +} +proc restoreLevels {} { set ::levels $::saved } +proc frames {n} { for {set i 0} {$i < $n && $::g(state) eq "play"} {incr i} { step }; set ::events {} } + +test hazard-1.1 {a gust of wind pushes the hero to the left} -setup { + testStreet {blocks {{0 196 472}} winds {{0 472 -1 400}}} +} -body { + set h(x) 200 + frames 40 + expr {$h(x) < 170} +} -cleanup restoreLevels -result 1 + +test hazard-1.2 {the wind is calm for the second half of its period} -setup { + testStreet {blocks {{0 196 472}} winds {{0 472 -1 400}}} +} -body { + frames 200 + set h(x) 200 + frames 100 + set h(x) +} -cleanup restoreLevels -result 200 + +test hazard-1.3 {the wind pushes only between its two x} -setup { + testStreet {blocks {{0 196 472}} winds {{300 472 -1 400}}} +} -body { + set h(x) 200 + frames 40 + set h(x) +} -cleanup restoreLevels -result 200 + +test hazard-2.1 {a tile shows its shadow, then falls, then lies on the block} -body { + set cur(blocks) {{0 196 472}} + list [tileState {100 0 200} 10] [tileState {100 0 200} 50] [tileState {100 0 200} 199] +} -result {shadow {fall 40} {}} + +test hazard-2.2 {a falling tile puts out the flame of the hero under it} -setup { + testStreet {blocks {{0 196 472}} tiles {{100 0 400}}} +} -body { + frames 30 + set h(x) 100; set h(spawnX) 300 + set hit 0 + for {set i 0} {$i < 200 && !$hit} {incr i} { step; if {"tilehit 0" in $events} { set hit 1 }; set events {} } + list $hit $h(x) [expr {$g(dark) > 0.05}] +} -cleanup restoreLevels -result {1 300 1} + +test hazard-2.3 {a tile does not touch a hero that stands to the side} -setup { + testStreet {blocks {{0 196 472}} tiles {{100 0 400}}} +} -body { + frames 30 + set h(x) 120 + set hit 0 + for {set i 0} {$i < 200} {incr i} { step; if {"tilehit 0" in $events} { set hit 1 }; set events {} } + set hit +} -cleanup restoreLevels -result 0 + +test hazard-3.1 {a lift goes from y0 to y1 and back in its period} -body { + lmap f {0 50 100} { format %.1f [liftY {0 190 60 90 100} $f] } +} -result {190.0 90.0 190.0} + +test hazard-3.2 {the hero on a lift goes up with it} -setup { + testStreet {lifts {{80 190 160 100 240}} water {{0 472}}} +} -body { + set h(x) 120; set h(y) 170 + frames 10 + set on [expr {$h(ground) && $h(lift) == 0}] + frames 110 + list $on $h(ground) [expr {abs($h(y) + 7 - [liftY {80 190 160 100 240} $g(frames)]) < 0.5}] [expr {$h(y) < 110}] +} -cleanup restoreLevels -result {1 1 1 1} + +test hazard-4.1 {a drawbridge is a floor while it is down} -setup { + testStreet {bridges {{0 196 472 400}}} +} -body { + frames 60 + list $h(ground) $h(y) +} -cleanup restoreLevels -result {1 189} + +test hazard-4.2 {the hero falls when the drawbridge goes up} -setup { + testStreet {bridges {{0 196 472 400}}} +} -body { + frames 199 + set before $h(ground) + frames 3 + list $before $h(ground) +} -cleanup restoreLevels -result {1 0} + +test hazard-5.1 {a hazard with a bad value gives an error with its street} -body { + list [catch {parseLevel T {street A {tiles {{100 0 10}}}} t.tcl 1} e] $e +} -result {1 {t.tcl:1: a part of tiles in A is outside the street: 100 0 10}} + +test hazard-5.2 {a lift that moves faster than half a pixel a frame gives an error} -body { + catch {parseLevel T {street A {lifts {{0 190 60 90 100}}}} t.tcl 1} +} -result 1 + +test hazard-6.1 {the same inputs give the same frames with each hazard} -body { + set out {} + foreach k {1 2} { + testStreet {blocks {{0 196 200} {300 196 472}} winds {{0 472 -0.5 90}} tiles {{150 0 120}} lifts {{200 190 300 150 300}} bridges {{200 196 300 160}}} + playInput right 1 + for {set i 0} {$i < 500} {incr i} { if {$i % 50 == 0} { playInput a 1 } elseif {$i % 50 == 15} { playInput a 0 }; step } + set events {} + lappend out [list $h(x) $h(y) $g(dark)] + restoreLevels + } + expr {[lindex $out 0] eq [lindex $out 1]} +} -result 1 diff --git a/tests/levels.test b/tests/levels.test index 81e9df2..4c24238 100644 --- a/tests/levels.test +++ b/tests/levels.test @@ -15,9 +15,9 @@ test levels-1.1 {there are 10 hand levels of 2 or 3 streets} -body { lmap l $levels { llength [lindex $l 1] } } -result {2 3 3 3 3 3 3 3 3 3} -test levels-1.2 {each street has a name and 7 lists of parts} -body { +test levels-1.2 {each street has a name and 11 lists of parts} -body { set bad {} - foreach l $levels { foreach s [lindex $l 1] { if {[llength $s] != 8} { lappend bad [lindex $s 0] } } } + foreach l $levels { foreach s [lindex $l 1] { if {[llength $s] != 12} { lappend bad [lindex $s 0] } } } set bad } -result {} @@ -70,7 +70,7 @@ test levels-4.2 {each hand level has a name that no other level has} -body { test levels-5.1 {a level file reads into the street lists of the core} -body { parseLevel Test {street One {blocks {{0 196 472}} lamps {{236 166}}}} -} -result {Test {{One {{0 196 472}} {} {{236 166}} {} {} {} {}}}} +} -result {Test {{One {{0 196 472}} {} {{236 166}} {} {} {} {} {} {} {} {}}}} test levels-5.2 {a part outside the street gives an error with the line of its street} -body { parseLevel Test "street One {\n blocks {{0 196 472}}\n}\nstreet Two {\n blocks {{0 196 900}}\n}" t.tcl 10 diff --git a/tools/solve.tcl b/tools/solve.tcl index 1ec381e..896a24e 100644 --- a/tools/solve.tcl +++ b/tools/solve.tcl @@ -1,7 +1,7 @@ # Finds a route for each hand level: a beam search over the moves of # core/route.tcl. Each node is the state after a move. Two states with # the same street, place (in cells of 6 pixels), speed, lit lamps, and -# boards are the same node (with boats: in the same 24 frames), and the first one wins. The beam keeps the +# boards are the same node (with a moving part: in the same 24 frames), and the first one wins. The beam keeps the # WIDTH nodes with the best score: the count of lit lamps first, then the # distance to the next dark lamp, or to the gate when all are lit. The # first node that finishes the level gives the route. @@ -22,7 +22,8 @@ proc unsnap {s} { } proc key {} { global g h hold lit crumb - list [expr {[llength [data boats]] ? $g(frames) / 24 : 0}] $g(scene) [expr {int($h(x) / 6)}] [expr {int($h(y) / 6)}] [expr {round($h(vx))}] [expr {$h(vy) < 0}] $h(ground) $hold(jump) \ + set moving [expr {[llength [data boats]] + [llength [data tiles]] + [llength [data lifts]] + [llength [data bridges]] + [llength [data winds]]}] + list [expr {$moving ? $g(frames) / 24 : 0}] $g(scene) [expr {int($h(x) / 6)}] [expr {int($h(y) / 6)}] [expr {round($h(vx))}] [expr {$h(vy) < 0}] $h(ground) $hold(jump) \ [lsort [array names lit]] [lsort [array names crumb]] } diff --git a/ui/view.tcl b/ui/view.tcl index 8aa25e7..42f028e 100644 --- a/ui/view.tcl +++ b/ui/view.tcl @@ -80,7 +80,7 @@ proc street {i} { global levels g W H dusk ink set c .nb.s$i $c delete all - lassign [lindex $levels $g(level) 1 $i] name blocks ledges lamps springs boats crumbles water + lassign [lindex $levels $g(level) 1 $i] name blocks ledges lamps springs boats crumbles water winds tiles lifts bridges backdrop $c $i foreach wt $water { lassign $wt x0 x1; $c create rectangle $x0 206 $x1 $H -width 0 -fill $ink(water) -tags water for {set x [expr {$x0 + 6}]} {$x < $x1 - 6} {incr x 18} { $c create line $x 210 [expr {$x + 8}] 210 -fill #6a86c8 -width 2 -tags ripple } } @@ -100,6 +100,7 @@ proc street {i} { foreach bt $boats { lassign $bt x0 y x1 $c create polygon $x0 $y $x1 $y [expr {$x1 - 6}] [expr {$y + 8}] [expr {$x0 + 6}] [expr {$y + 8}] -fill #a0703c -outline #d09a5a -tags "boat bt$n" incr n } + drawHazards $c $blocks $winds $tiles $lifts $bridges foreach s $springs { lassign $s x y $c create rectangle [expr {$x - 9}] [expr {$y - 5}] [expr {$x + 9}] $y -width 0 -fill #5fb27a -tags spring $c create rectangle [expr {$x - 9}] [expr {$y - 7}] [expr {$x + 9}] [expr {$y - 5}] -width 0 -fill #a6e6b2 -tags spring } @@ -298,6 +299,7 @@ proc draw {} { $c coords bt$n [expr {$x0 + $off}] $y [expr {$x1 + $off}] $y [expr {$x1 + $off - 6}] [expr {$y + 8}] [expr {$x0 + $off + 6}] [expr {$y + 8}] incr n } + if {$g(state) eq "play"} { moveHazards $c } if {$g(state) ni {run runover} && [allLit]} { foreach id [$c find withtag bars] { lassign [$c coords $id] a b cc d; if {$b > 60} { $c coords $id $a [expr {$b - 2}] $cc [expr {$d - 2}] } } } if {$g(state) in {run runover streetclear}} { weatherTick $c } if {$g(state) eq "title"} return @@ -626,3 +628,96 @@ proc weatherTick {c} { } $c raise wx } + +# The items of the hazards of a street (core/hazards.tcl): streaks of +# wind, the shadow and the tile of a falling tile, a lift on its rope, +# and a drawbridge. `moveHazards` moves them at each frame. +proc drawHazards {c blocks winds tiles lifts bridges} { + global H + set n 0 + foreach w $winds { + lassign $w x0 x1 force + for {set k 0} {$k < 6} {incr k} { + set y [expr {60 + ($k * 37) % 120}] + $c create line $x0 $y [expr {$x0 + 18}] $y -fill #c8d4f0 -width 1 -state hidden -tags "wind w$n wk$n.$k" + } + incr n + } + set n 0 + foreach tl $tiles { + lassign $tl x + set top $H + foreach b $blocks { lassign $b bx0 bt bx1; if {$x >= $bx0 && $x <= $bx1} { set top [expr {min($top, $bt)}] } } + $c create oval [expr {$x - 8}] [expr {$top - 2}] [expr {$x + 8}] [expr {$top + 2}] -width 0 -fill #0c0b1a -state hidden -tags "tile tsh$n" + $c create polygon 0 0 1 1 2 0 -fill #b4553c -outline #6e2e22 -state hidden -tags "tile tl$n" + incr n + } + set n 0 + foreach lf $lifts { + lassign $lf x0 y0 x1 + set mid [expr {($x0 + $x1) / 2}] + $c create line $mid 0 $mid $y0 -fill #8f86b0 -width 1 -tags "lift rope$n" + $c create rectangle $x0 $y0 $x1 [expr {$y0 + 6}] -fill #7a5a3a -outline #c09a6a -tags "lift lf$n" + incr n + } + set n 0 + foreach br $bridges { + lassign $br x0 y x1 + $c create rectangle $x0 $y $x1 [expr {$y + 6}] -fill #8a6a4a -outline #5a4430 -tags "bridge br$n" + $c create line $x0 [expr {$y - 40}] $x1 $y -fill #8f86b0 -width 1 -tags "bridge chain$n" + incr n + } +} +proc moveHazards {c} { + global g + set f $g(frames) + set n 0 + foreach w [data winds] { + lassign $w x0 x1 force + set on [gustOn $w $f] + for {set k 0} {$k < 6} {incr k} { + set len [expr {$x1 - $x0 - 18}] + set off [expr {fmod(($force < 0 ? -$f : $f) * 3 + $k * 53, max(1, $len))}] + if {$off < 0} { set off [expr {$off + $len}] } + set x [expr {$x0 + $off}]; set y [expr {60 + ($k * 37) % 120}] + $c coords wk$n.$k $x $y [expr {$x + 18}] $y + $c itemconfigure wk$n.$k -state [expr {$on && !$::app(calm) ? "normal" : "hidden"}] + } + incr n + } + set n 0 + foreach tl [data tiles] { + lassign $tl x + set st [tileState $tl $f] + $c itemconfigure tsh$n -state [expr {[llength $st] ? "normal" : "hidden"}] + if {[lindex $st 0] eq "fall"} { + set y [lindex $st 1] + $c coords tl$n [expr {$x - 7}] $y [expr {$x + 7}] $y [expr {$x + 5}] [expr {$y + 6}] [expr {$x - 5}] [expr {$y + 6}] + $c itemconfigure tl$n -state normal + } else { + $c itemconfigure tl$n -state hidden + } + incr n + } + set n 0 + foreach lf [data lifts] { + lassign $lf x0 y0 x1 + set y [liftY $lf $f] + set mid [expr {($x0 + $x1) / 2}] + $c coords lf$n $x0 $y $x1 [expr {$y + 6}] + $c coords rope$n $mid 0 $mid $y + incr n + } + set n 0 + foreach br [data bridges] { + lassign $br x0 y x1 + if {[bridgeDown $br $f]} { + $c coords br$n $x0 $y $x1 [expr {$y + 6}] + $c coords chain$n $x0 [expr {$y - 40}] $x1 $y + } else { + $c coords br$n $x0 [expr {$y - ($x1 - $x0)}] [expr {$x0 + 6}] $y + $c coords chain$n $x0 [expr {$y - 40}] [expr {$x0 + 6}] [expr {$y - ($x1 - $x0)}] + } + incr n + } +}