Files
breeze/tests/sun_salutation_behavior.zi
waozi 4ea83c536a Guide Sun Salutation with form cues, sides, next pose, intro and rest
Each pose shows a one-line form cue, the lunges name the leg that is back,
and a small held figure previews the next pose. A fresh session settles in
for three breaths before the first round and rests for two after the last,
with the narration's intro and closing lines. The first round never breathes
faster than 6 s, so every spoken line plays in full.
2026-10-30 00:55:35 -03:00

427 lines
16 KiB
Ziran

#import, file "../src/practices/sun_salutation/sun_salutation_rules.zi";
#program_export
Answer :: () -> s32 {
state: SunSalutationPracticeState
state.repetitions = 99
state.start_seconds = 1
state.end_seconds = 99
state = SalutationInit(state)
if state.repetitions != 3 || state.start_seconds != 8 ||
state.end_seconds != 6 {
return 1
}
state.repetitions = 1
state.start_seconds = 2
state.end_seconds = 13
state = SalutationNormalizeSettings(state)
if state.repetitions != 2 || state.start_seconds != 3 ||
state.end_seconds != 3 {
return 2
}
state.repetitions = 3
state.start_seconds = 8
state.end_seconds = 5
state.step = 5
state.repetition = 2
state.step_ticks = 15
state = SalutationStart(state)
if state.step != 0 || state.repetition != 0 ||
state.step_ticks != 0 ||
SalutationStepSeconds(state) != 8 {
return 3
}
state.repetition = 1
if SalutationStepSeconds(state) != 7 {
return 4
}
state.repetition = 2
if SalutationStepSeconds(state) != 5 {
return 5
}
// A round ends in Mountain pose, so the next one starts with the
// upward salute rather than standing there again.
state.step = SUN_SALUTATION_STEP_COUNT - 1
state.repetition = 0
next: SalutationStepResult = SalutationForward(state)
if next.completed != 0 || next.state.step != SUN_SALUTATION_REPEAT_STEP ||
next.state.repetition != 1 || next.state.step_ticks != 0 {
return 6
}
state = SalutationBack(next.state)
if state.step != SUN_SALUTATION_STEP_COUNT - 1 ||
state.repetition != 0 {
return 7
}
state.repetition = 2
next = SalutationForward(state)
if next.completed != 1 {
return 8
}
state = SalutationReset(state)
if state.step != 0 || state.repetition != 0 ||
state.step_ticks != 0 || state.start_seconds != 8 {
return 9
}
tick: s32 = 0
while tick < 480 {
next = SalutationAdvance(state, 1.0 / 60.0)
state = next.state
tick += 1
}
if state.step != 1 || state.step_ticks != 0 ||
next.completed != 0 {
return 10
}
// Equal wall time must produce the same pose at slow and fast frame rates.
rates: [5]s32 = .[15, 30, 60, 90, 120]
for rate: rates {
state = SalutationStart(state)
for frame: 0..rate * 3 - 1 {
unused frame
state = SalutationAdvance(state, 1.0 / cast(float64)rate).state
}
if state.step_ticks != 180 {
return 15
}
}
// A slow render can cross step boundaries without discarding time.
state = SalutationStart(state)
next = SalutationAdvance(state, 17.25)
if next.completed != 0 || next.state.step != 2 || next.state.step_ticks != 75 {
return 16
}
state = SalutationBack(next.state)
state.tick_fraction = 0.5
state = SalutationForward(state).state
if state.tick_fraction != 0.0 || SalutationReset(state).tick_fraction != 0.0 ||
SalutationStart(state).tick_fraction != 0.0 {
return 17
}
next = SalutationAdvance(state, 2000.0)
if next.completed != 1 {
return 18
}
// A breath of 8 s with 24 move frames and 4 hold frames at 8 fps: the
// move takes at most 6 s, from the previous pose (-1) to the last move
// frame, then the hold frames follow and the last one stays.
if SalutationFrameAt(0, 480, 28, 24, 8) != -1 ||
SalutationFrameAt(90, 480, 28, 24, 8) != 5 ||
SalutationFrameAt(180, 480, 28, 24, 8) != 11 ||
SalutationFrameAt(359, 480, 28, 24, 8) != 22 ||
SalutationFrameAt(360, 480, 28, 24, 8) != 23 ||
SalutationFrameAt(375, 480, 28, 24, 8) != 25 ||
SalutationFrameAt(1000, 480, 28, 24, 8) != 27 {
return 11
}
// A 4 s breath moves through its first three quarters; the first pose
// is one frame.
if SalutationFrameAt(90, 240, 28, 24, 8) != 11 ||
SalutationFrameAt(180, 240, 28, 24, 8) != 23 ||
SalutationFrameAt(30, 480, 1, 24, 8) != 0 {
return 13
}
// At 30 fps the full move has 90 distinct frames, followed by 15
// settling frames. Short breaths still keep a quarter for settling.
if SalutationFrameAt(0, 480, 105, 90, 30) != -1 ||
SalutationFrameAt(4, 480, 105, 90, 30) != 0 ||
SalutationFrameAt(180, 480, 105, 90, 30) != 44 ||
SalutationFrameAt(360, 480, 105, 90, 30) != 89 ||
SalutationFrameAt(362, 480, 105, 90, 30) != 90 ||
SalutationFrameAt(1000, 480, 105, 90, 30) != 104 ||
SalutationFrameAt(45, 180, 105, 90, 30) != 29 {
return 14
}
state.step = 99
state.repetition = 99
state = SalutationNormalizeDraw(state)
if state.step != SUN_SALUTATION_STEP_COUNT - 1 ||
state.repetition != state.repetitions - 1 {
return 12
}
// Changing session position or restarting must retain the selected person.
for character: 0..SUN_SALUTATION_CHARACTER_COUNT - 1 {
state.character = cast(s32)character
state = SalutationStart(state)
state = SalutationAdvance(state, 17.25).state
state = SalutationForward(state).state
state = SalutationBack(state)
state = SalutationReset(state)
if state.character != cast(s32)character {
return 19
}
}
// Old settings default to the first woman; invalid imported choices
// cannot index outside the two shipped characters.
initial: SunSalutationPracticeState
if SalutationInit(initial).character != 0 {
return 20
}
state.character = -1
if SalutationNormalizeSettings(state).character != 0 {
return 21
}
state.character = 99
if SalutationInit(state).character != SUN_SALUTATION_CHARACTER_COUNT - 1 ||
SalutationStart(state).character != SUN_SALUTATION_CHARACTER_COUNT - 1 ||
SalutationNormalizeDraw(state).character != SUN_SALUTATION_CHARACTER_COUNT - 1 {
return 22
}
// Bound hairstyles settle at the end of the 90-frame move.
if SalutationFrameAt(360, 480, 90, 90, 30) != 89 ||
SalutationFrameAt(1000, 480, 90, 90, 30) != 89 {
return 23
}
// Plank takes two breaths: out to step back into it, in to hold it.
breathing: SunSalutationPracticeState
breathing.repetitions = 3
breathing.start_seconds = 6
breathing.end_seconds = 4
breathing = SalutationStart(breathing)
breathing.step = SUN_SALUTATION_PLANK_STEP
if SalutationStepSeconds(breathing) != 12 || SalutationBreathSeconds(breathing) != 6 {
return 24
}
breathing.step_ticks = 90
breath := SalutationBreathAt(breathing)
if breath.direction != SUN_SALUTATION_BREATH_OUT || breath.progress != 250 {
return 25
}
breathing.step_ticks = 540
breath = SalutationBreathAt(breathing)
if breath.direction != SUN_SALUTATION_BREATH_IN || breath.progress != 500 {
return 26
}
// The move into plank happens on its first breath.
if SalutationFrameAt(270, 360, 105, 90, 30) != 89 {
return 27
}
// Through a whole session the breath turns from out to in and back on
// every breath, starting with a breath out in Mountain pose, and only
// the first round stands in Mountain pose before it rises.
breathing = SalutationStart(breathing)
expected: s32 = SUN_SALUTATION_BREATH_OUT
breaths: s32 = 0
mountains: s32 = 0
finished := false
while !finished && breaths < 100 {
seconds := SalutationBreathSeconds(breathing)
for breath_index: 0..SalutationStepBreaths(breathing.step) - 1 {
breathing.step_ticks = cast(s32)breath_index * seconds * 60 + seconds * 30
if SalutationBreathAt(breathing).direction != expected {
return 28
}
expected = 1 - expected
breaths += 1
}
if breathing.step == 0 {
mountains += 1
}
next = SalutationForward(breathing)
breathing = next.state
finished = next.completed != 0
}
// 13 breaths in the first round, 12 in each of the other two.
if !finished || breaths != 37 || mountains != 1 {
return 29
}
// Going back from the first step of a later round returns to the
// previous round's Mountain pose; drawing never shows a skipped step.
breathing = SalutationStart(breathing)
breathing.repetition = 1
breathing.step = SUN_SALUTATION_REPEAT_STEP
breathing = SalutationBack(breathing)
if breathing.repetition != 0 || breathing.step != SUN_SALUTATION_STEP_COUNT - 1 {
return 30
}
breathing.repetition = 1
breathing.step = 0
if SalutationNormalizeDraw(breathing).step != SUN_SALUTATION_REPEAT_STEP {
return 31
}
// Narration follows the character until a voice is chosen.
if SalutationMaleVoice(-1, 0) || !SalutationMaleVoice(-1, 1) ||
SalutationMaleVoice(0, 1) || !SalutationMaleVoice(1, 0) {
return 32
}
// The classic sequence switches legs each round.
breathing.repetition = 0
if SalutationOtherSide(breathing) {
return 33
}
breathing.repetition = 1
if !SalutationOtherSide(breathing) {
return 34
}
breathing.repetition = 2
if SalutationOtherSide(breathing) {
return 35
}
// The displayed duration is the same time consumed by real playback.
gentle := SalutationStart(SunSalutationPracticeState.{})
if SalutationDurationSeconds(gentle) != 260 {
return 36
}
almost := SalutationAdvance(gentle, 259.0)
if almost.completed != 0 || SalutationAdvance(almost.state, 1.0).completed != 1 {
return 37
}
gentle.start_seconds = 6
gentle.end_seconds = 4
migrated := SalutationMigrateTempo(gentle, 1)
if migrated.start_seconds != 8 || migrated.end_seconds != 6 ||
SalutationDurationSeconds(gentle) != 186 {
return 38
}
if SalutationMigrateTempo(gentle, SUN_SALUTATION_TEMPO_VERSION).start_seconds != 6 {
return 39
}
gentle.start_seconds = 9
gentle.end_seconds = 5
migrated = SalutationMigrateTempo(gentle, 1)
if migrated.start_seconds != 9 || migrated.end_seconds != 5 {
return 40
}
gentle.start_seconds = 8
if SalutationMigrateTempo(gentle, 0).end_seconds != 6 ||
SalutationMigrateTempo(gentle, 1).end_seconds != 5 {
return 41
}
// A fresh session settles in for three breaths, in, out and in, so
// Mountain pose's out-breath follows; Mountain pose comes next.
guided := SalutationStart(SunSalutationPracticeState.{})
guide: SalutationGuide
seconds := SalutationGuideBreathSeconds(guided)
if SalutationGuideFor(guide, guided).phase != SUN_SALUTATION_PHASE_INTRO ||
SalutationGuideNextStep(guide, guided) != 0 || seconds != 8 {
return 43
}
for breath_index: cast(s32)0..SUN_SALUTATION_INTRO_BREATHS - 1 {
guide.ticks = breath_index * seconds * 60 + seconds * 30
expected_breath := ifx breath_index % 2 == 0 then SUN_SALUTATION_BREATH_IN else SUN_SALUTATION_BREATH_OUT
if SalutationGuideBreathAt(guide, guided).direction != expected_breath ||
SalutationGuideBreathAt(guide, guided).progress != 500 ||
SalutationGuideBreathIndex(guide, guided) != breath_index {
return 44
}
}
guide.ticks = 0
settling := SalutationGuideAdvance(guide, guided, cast(float64)(seconds * 3) - 0.5)
if settling.guide.phase != SUN_SALUTATION_PHASE_INTRO || settling.remaining != 0.0 {
return 45
}
// Time past the intro's end goes on to the first round.
settling = SalutationGuideAdvance(settling.guide, guided, 1.5)
if settling.guide.phase != SUN_SALUTATION_PHASE_ROUNDS ||
settling.remaining < 0.99 || settling.remaining > 1.01 {
return 46
}
settling = SalutationGuideAdvance(settling.guide, guided, 2.0)
if settling.guide.phase != SUN_SALUTATION_PHASE_ROUNDS || settling.remaining != 2.0 {
return 47
}
// A session already under way never settles in again, and the rest
// follows only the last round's Mountain pose.
under_way := guided
under_way.step = 2
closing := SalutationGuide.{.phase = SUN_SALUTATION_PHASE_CLOSING}
if SalutationGuideFor(SalutationGuide.{}, under_way).phase != SUN_SALUTATION_PHASE_ROUNDS ||
SalutationGuideFor(closing, under_way).phase != SUN_SALUTATION_PHASE_ROUNDS {
return 48
}
under_way.step = SUN_SALUTATION_STEP_COUNT - 1
under_way.repetition = under_way.repetitions - 1
if SalutationGuideFor(closing, under_way).phase != SUN_SALUTATION_PHASE_CLOSING ||
SalutationGuideNextStep(closing, under_way) != -1 {
return 49
}
// The rest breathes in, then out, then the results follow.
closing.ticks = seconds * 60 + 1
if SalutationGuideBreathAt(closing, under_way).direction != SUN_SALUTATION_BREATH_OUT {
return 50
}
closing.ticks = 0
rest := SalutationGuideAdvance(closing, under_way, cast(float64)(seconds * 2) + 1.0)
if rest.guide.phase != SUN_SALUTATION_PHASE_DONE || rest.remaining != 0.0 {
return 51
}
// The next step runs through a round, then into the next round's
// upward salute, and there is none after the last round.
rounds_guide := SalutationGuide.{.phase = SUN_SALUTATION_PHASE_ROUNDS}
under_way.repetition = 0
under_way.step = SUN_SALUTATION_PLANK_STEP
if SalutationGuideNextStep(rounds_guide, under_way) != SUN_SALUTATION_PLANK_STEP + 1 {
return 52
}
under_way.step = SUN_SALUTATION_STEP_COUNT - 1
if SalutationGuideNextStep(rounds_guide, under_way) != SUN_SALUTATION_REPEAT_STEP {
return 53
}
under_way.repetition = under_way.repetitions - 1
if SalutationGuideNextStep(rounds_guide, under_way) != -1 {
return 54
}
// The first round steps the right leg back and brings the right foot
// forward, leaving the left back; the next round mirrors it.
under_way.repetition = 0
sides: [12]s32
for step: cast(s32)0..SUN_SALUTATION_STEP_COUNT - 1 {
under_way.step = step
sides[step] = SalutationBackLeg(under_way)
}
for step: cast(s32)0..SUN_SALUTATION_STEP_COUNT - 1 {
expected_side := SUN_SALUTATION_SIDE_NONE
if step == SUN_SALUTATION_LUNGE_STEP {
expected_side = SUN_SALUTATION_SIDE_RIGHT
} else if step == SUN_SALUTATION_RETURN_LUNGE_STEP {
expected_side = SUN_SALUTATION_SIDE_LEFT
}
if sides[step] != expected_side {
return 55
}
}
under_way.repetition = 1
under_way.step = SUN_SALUTATION_LUNGE_STEP
if SalutationBackLeg(under_way) != SUN_SALUTATION_SIDE_LEFT {
return 56
}
under_way.step = SUN_SALUTATION_RETURN_LUNGE_STEP
if SalutationBackLeg(under_way) != SUN_SALUTATION_SIDE_RIGHT ||
SalutationSideKey(SUN_SALUTATION_SIDE_RIGHT) != "sun_salutation_side_right" ||
SalutationSideKey(SUN_SALUTATION_SIDE_NONE) != "" {
return 57
}
// The first round is never quicker than the guided pace, so every line
// plays in full; later rounds keep the chosen tempo.
quick := SalutationStart(SunSalutationPracticeState.{.repetitions = 3,
.start_seconds = 4, .end_seconds = 3})
if SalutationBreathSeconds(quick) != SUN_SALUTATION_FIRST_ROUND_SECONDS ||
SalutationGuideBreathSeconds(quick) != SUN_SALUTATION_FIRST_ROUND_SECONDS {
return 58
}
quick.repetition = 1
if SalutationBreathSeconds(quick) != 4 {
return 59
}
quick.repetition = 2
if SalutationBreathSeconds(quick) != 3 {
return 60
}
if SalutationCueKey(6) != "sun_salutation_cue_7" ||
SalutationStepKey(-1) != "sun_salutation_step_1" ||
SalutationStepKey(99) != "sun_salutation_step_12" {
return 61
}
return 42
}