diff --git a/src/assets/images.kra b/src/assets/images.kra index 5a2afe8..5336673 100644 Binary files a/src/assets/images.kra and b/src/assets/images.kra differ diff --git a/src/game.odin b/src/game.odin index 4bcd274..c85445c 100644 --- a/src/game.odin +++ b/src/game.odin @@ -158,32 +158,23 @@ init :: proc() { codepointCount = 0, ) - queue.init(&magic_moves) auto_moves := make([dynamic]Move, context.temp_allocator) - for { - log.info("searching solution…") - state_init(&game.state) - state_make_stacks(&game.state, &auto_moves) - queue.append(&game.auto_moves, ..auto_moves[:]) - if solution, ok := state_find_solution(game.state, context.temp_allocator); ok { - queue.append(&magic_moves, ..solution[:]) - break - } - clear(&auto_moves) - free_all(context.temp_allocator) - } - queue.append(&game.auto_moves, ..auto_moves[:]) + reverse_moves := state_init_reverse(&game.state) game.displayed_state = game.state + log.debugf("Generated game: %#v", game.state) + // #reverse for move in reverse_moves { + // queue.append(&game.auto_moves, move) + // } + state_make_stacks(&game.state, &auto_moves) + queue.append(&game.auto_moves, ..auto_moves[:]) + // solution, _ := state_find_solution(game.state, context.temp_allocator) + // log.infof("Solution: %v", solution) for &entity in game.entities { entity.position = card_position({.Column, 5}) } } -magic_moves: queue.Queue(Move) -magic_wait: f32 -MAGIC_INTERVAL :: 0.4 - update :: proc() { free_all(context.temp_allocator) delta := ray.GetFrameTime() @@ -253,28 +244,13 @@ update :: proc() { } } - magic_wait -= delta - if magic_wait <= 0 { - if magic_move, ok := queue.pop_front_safe(&magic_moves); ok { - effects, ok := state_perform_move( - &game.state, - magic_move, - allocator = context.temp_allocator, - ) - for auto_move in effects { - queue.push(&game.auto_moves, auto_move) - auto_obj := &game_cards_zipped()[int(auto_move.card)] - auto_obj.entity.state = .Locked - } - - } - } - game.auto_move_cooldown -= delta if game.auto_move_cooldown <= 0 { if auto_move, ok := queue.pop_front_safe(&game.auto_moves); ok { + log.debugf("running auto move: %v", auto_move) game.auto_move_cooldown = AUTO_MOVE_INTERVAL - state_apply_single_move(&game.displayed_state, auto_move) + ok := state_apply_single_move(&game.displayed_state, auto_move) + assert(ok, "auto moves should always be valid") } } game.auto_move_cooldown = max(0, game.auto_move_cooldown) @@ -351,6 +327,11 @@ update :: proc() { } // NOTE(lilith): Draw cards + for column, column_index in game.displayed_state.columns { + for handle, cc_index in column { + card_draw(handle) + } + } for major_pile in game.displayed_state.major { for handle in major_pile { card_draw(handle) @@ -382,12 +363,6 @@ update :: proc() { card_draw(game.displayed_state.aside[0]) } - for column, column_index in game.displayed_state.columns { - for handle, cc_index in column { - card_draw(handle) - } - } - if handle, ok := game.dragged_card.?; ok { card_draw(handle) } @@ -459,7 +434,7 @@ card_draw :: proc(handle: Card_Handle) { } } -state_init :: proc(state: ^State) { +state_init_fully_random :: proc(state: ^State) { deck := [TAROT_COUNT]Card_Handle{} for &handle, index in deck {handle = Card_Handle(index)} rand.shuffle(deck[:]) @@ -480,6 +455,371 @@ state_init :: proc(state: ^State) { } } +state_init_solved :: proc(state: ^State) { + state^ = {} + for &suit_pile, suit_index in state.suits { + suit := Suit(suit_index + 1) + for number in 1 ..= MINOR_COUNT { + append(&suit_pile, Card_Handle(MAJOR_COUNT + suit_index * MINOR_COUNT + number - 1)) + } + } + major_split := rand.int_range(0, MAJOR_COUNT + 1) + for number in 0 ..< major_split { + append(&state.major[.Low], Card_Handle(number)) + } + for number := MAJOR_COUNT - 1; number >= major_split; number -= 1 { + append(&state.major[.High], Card_Handle(number)) + } +} + +state_init_weird :: proc(state: ^State) { + state^ = {} + outer_column_idcs: [COLUMN_COUNT - 1]int + for index in 0 ..< COLUMN_COUNT - 1 {outer_column_idcs[index] = index >= 5 ? index + 1 : index} + rand.shuffle(outer_column_idcs[:]) + for suit_index in 0 ..< 4 { + suit := Suit(suit_index + 1) + for number in 1 ..= MINOR_COUNT { + append( + &state.columns[outer_column_idcs[suit_index]], + Card_Handle(MAJOR_COUNT + suit_index * MINOR_COUNT + number - 1), + ) + } + } + for number in 0 ..< 11 { + append(&state.columns[outer_column_idcs[4]], Card_Handle(number)) + } + for number := MAJOR_COUNT - 1; number >= 11; number -= 1 { + append(&state.columns[outer_column_idcs[5]], Card_Handle(number)) + } +} + +state_init_reverse :: proc(state: ^State) -> (moves: [dynamic]Move) { + state_init_solved(state) + Initializer :: struct { + moves: [dynamic]Move, + states_seen: map[[616]byte]struct{}, + state: ^State, + } + initializer := Initializer { + moves = make([dynamic]Move, context.temp_allocator), + states_seen = make(map[[616]byte]struct{}, context.temp_allocator), + state = state, + } + column_indices: [COLUMN_COUNT]int + for index in 0 ..< COLUMN_COUNT {column_indices[index] = index} + outer_column_idcs: [COLUMN_COUNT - 1]int + for index in 0 ..< COLUMN_COUNT - 1 {outer_column_idcs[index] = index >= 5 ? index + 1 : index} + avoid_column := rand.int_range(0, COLUMN_COUNT - 1) + avoid_column = avoid_column >= 5 ? avoid_column + 1 : avoid_column + + // NOTE(lilith): Create some buried top cards + rand.shuffle(outer_column_idcs[:]) + bury_majors := rand.float32() < 1 + if bury_majors { + clear(&state.major[.Low]) + clear(&state.major[.High]) + for number in 0 ..< 11 { + append(&state.columns[outer_column_idcs[COLUMN_COUNT - 2]], Card_Handle(number)) + } + for number := MAJOR_COUNT - 1; number >= 11; number -= 1 { + append(&state.columns[outer_column_idcs[COLUMN_COUNT - 3]], Card_Handle(number)) + } + } + suits := []Suit{.Chalice, .Coin, .Sword, .Wand} + rand.shuffle(suits) + for suit in suits[:rand.int_range(0, 2) + (bury_majors ? 2 : 3)] { + suit_index := int(suit) - 1 + clear(&state.suits[suit_index]) + append( + &state.columns[outer_column_idcs[suit_index + 4]], + Card_Handle(MAJOR_COUNT + suit_index * MINOR_COUNT), + ) + for number in 2 ..= MINOR_COUNT { + append( + &state.columns[outer_column_idcs[suit_index]], + Card_Handle(MAJOR_COUNT + suit_index * MINOR_COUNT + number - 1), + ) + } + } + + initial_shuffle: for _ in 0 ..< 50 { + shuffle(&initializer, random_chance = 0) + } + + reverse_moves: for _ in 0 ..< 1000 { + stowed_majors: int + for major_pile in state.major {stowed_majors += len(major_pile)} + stowed_minors: int + for minor_pile in state.suits {stowed_minors += len(minor_pile)} + if stowed_majors + stowed_minors == 0 {break} + if rand.float32() < f32(stowed_majors) / f32(stowed_majors + stowed_minors) { + major_clear: for &major_pile, major_pile_kind in state.major { + if len(major_pile) > 0 { + rand.shuffle(outer_column_idcs[:]) + for from_column_index in outer_column_idcs { + if from_column_index == avoid_column {continue} + from_column := &state.columns[from_column_index] + if len(from_column) >= 8 {continue} + to_handle := slice.last(major_pile[:]) + to_card := card_from_handle(to_handle) + if len(from_column) > 1 { + if !cards_are_stackable( + to_card, + card_from_handle(slice.last(from_column[:])), + ) {continue} + } + move := Move { + from = {.Column, from_column_index}, + to = {.Major, int(major_pile_kind)}, + card = slice.last(major_pile[:]), + } + state_reverse_single_move(state, move) + append(&initializer.moves, move) + continue major_clear + } + if rand.float32() > 0.05 {continue} + rand.shuffle(outer_column_idcs[:]) + for from_column_index in outer_column_idcs { + from_column := &state.columns[from_column_index] + if len(from_column) >= 8 {continue} + move := Move { + from = {.Column, from_column_index}, + to = {.Major, int(major_pile_kind)}, + card = slice.last(major_pile[:]), + } + state_reverse_single_move(state, move) + append(&initializer.moves, move) + continue major_clear + } + } + } + } else { + suit_clear: for &suit_pile, suit_index in state.suits { + if len(state.aside) > 0 {break} + if len(suit_pile) > 0 { + rand.shuffle(outer_column_idcs[:]) + for from_column_index in outer_column_idcs { + if from_column_index == avoid_column {continue} + from_column := &state.columns[from_column_index] + if len(from_column) >= 8 {continue} + to_handle := slice.last(suit_pile[:]) + to_card := card_from_handle(to_handle) + if len(from_column) > 1 { + if !cards_are_stackable( + to_card, + card_from_handle(slice.last(from_column[:])), + ) {continue} + } + move := Move { + from = {.Column, from_column_index}, + to = {.Suit, suit_index}, + card = to_handle, + } + state_reverse_single_move(state, move) + append(&initializer.moves, move) + continue suit_clear + } + if rand.float32() > 0.05 {continue} + rand.shuffle(outer_column_idcs[:]) + for from_column_index in outer_column_idcs { + from_column := &state.columns[from_column_index] + if len(from_column) >= 8 {continue} + move := Move { + from = {.Column, from_column_index}, + to = {.Suit, suit_index}, + card = slice.last(suit_pile[:]), + } + state_reverse_single_move(state, move) + append(&initializer.moves, move) + continue suit_clear + } + } + } + } + small_shuffle: for _ in 0 ..< (rand.float32() < 0.15 ? 0 : 20) { + shuffle(&initializer) + } + } + + if len(state.aside) > 0 { + for &column, column_index in state.columns { + if len(column) >= 8 {continue} + if len(column) >= 7 && column_index >= 10 {continue} + if len(column) >= 0 && column_index == 5 {continue} + move := Move { + from = {.Column, column_index}, + to = {.Aside, 0}, + card = state.aside[0], + } + state_reverse_single_move(state, move) + break + } + } + // for len(state.columns[5]) > 0 { + // for &column, column_index in state.columns { + // if len(column) >= 8 {continue} + // if len(column) >= 7 && column_index >= 10 {continue} + // if column_index == 5 {continue} + // move := Move { + // from = {.Column, column_index}, + // to = {.Column, 5}, + // card = slice.last(state.columns[5][:]), + // } + // state_reverse_single_move(state, move) + // break + // } + // } + + return initializer.moves + + SHUFFLE_BRANCHING :: 2 + SHUFFLE_BRANCHING_DEPTH :: 3 + SHUFFLE_ROLLOUT_DEPTH :: 50 + + state_random_shuffle_move :: proc(state: ^State) -> (move: Move, ok: bool) { + column_indices: [COLUMN_COUNT]int + for index in 0 ..< COLUMN_COUNT {column_indices[index] = index} + for _ in 0 ..< 10 { + switch rand.float32() { + case 0 ..< 0.4: + rand.shuffle(column_indices[:]) + for from_column_index in column_indices { + from_column := &state.columns[from_column_index] + for &to_column, to_column_index in state.columns { + if len(to_column) == 0 {continue} + if len(to_column) == 1 && len(from_column) == 0 {continue} + move := Move { + from = {.Column, from_column_index}, + to = {.Column, to_column_index}, + card = slice.last(to_column[:]), + } + if len(from_column) > 0 { + if !cards_are_stackable( + card_from_handle(slice.last(to_column[:])), + card_from_handle(slice.last(from_column[:])), + ) {continue} + } + if state_is_move_valid_reverse(state, move) { + return move, true + } + } + } + case 0.4 ..< 0.7: + rand.shuffle(column_indices[:]) + for from_column_index in column_indices { + from_column := &state.columns[from_column_index] + for &to_column, to_column_index in state.columns { + if len(to_column) == 0 {continue} + if len(to_column) == 1 && len(from_column) == 0 {continue} + move := Move { + from = {.Column, from_column_index}, + to = {.Column, to_column_index}, + card = slice.last(to_column[:]), + } + if state_is_move_valid_reverse(state, move) { + return move, true + } + } + } + case 0.7 ..= 1: + rand.shuffle(column_indices[:]) + if len(state.aside) == 0 { + for to_column_index in column_indices { + to_column := &state.columns[to_column_index] + if len(to_column) == 0 {continue} + move := Move { + from = {.Aside, 0}, + to = {.Column, to_column_index}, + card = slice.last(to_column[:]), + } + if state_is_move_valid_reverse(state, move) { + return move, true + } + } + } else { + if rand.float32() < 0.7 {return} + for from_column_index in column_indices { + from_column := &state.columns[from_column_index] + move := Move { + from = {.Column, from_column_index}, + to = {.Aside, 0}, + card = state.aside[0], + } + if state_is_move_valid_reverse(state, move) { + return move, true + } + } + } + } + } + return {}, false + } + + recurse_max_difficulty :: proc(state: State, depth: int) -> (difficulty: int = -200000000) { + state := state + difficulty = state_difficulty_score(&state) + if depth >= SHUFFLE_ROLLOUT_DEPTH { + return + } + for _ in 0 ..< (depth < SHUFFLE_BRANCHING_DEPTH ? SHUFFLE_BRANCHING : 1) { + move, ok := state_random_shuffle_move(&state) + if !ok {continue} + clone := state + state_reverse_single_move(&clone, move) + difficulty = max(difficulty, recurse_max_difficulty(clone, depth + 1)) + } + return + } + + shuffle :: proc(init: ^Initializer, random_chance := f32(0.05)) { + state := init.state + move: Move + if rand.float32() < random_chance { + move, _ = state_random_shuffle_move(state) + } else { + moves: [8]Move + difficulties: [8]int + for index in 0 ..< 8 { + ok: bool + moves[index], ok = state_random_shuffle_move(state) + if !ok {continue} + clone := state^ + state_reverse_single_move(&clone, moves[index]) + difficulties[index] = recurse_max_difficulty(clone, 0) + } + max_index := slice.max_index(difficulties[:]) + move = moves[max_index] + } + if state_is_move_valid_reverse(state, move) { + try_move(init, move) + } + } + + try_move :: proc(init: ^Initializer, move: Move) -> (ok: bool) { + state := init.state^ + state_reverse_single_move(&state, move) + if state_to_bytes(state) in init.states_seen {return false} + init.state^ = state + init.states_seen[state_to_bytes(state)] = {} + append(&init.moves, move) + return true + } + +} + +State_Bytes :: [616]byte +state_to_bytes :: proc(state: State) -> State_Bytes { + return transmute(State_Bytes)state +} + +state_reverse_single_move :: proc(state: ^State, move: Move) { + moved_card, pop_ok := state_pop(state, move.to) + assert(pop_ok) + assert(moved_card == move.card) + state_push(state, move.from, moved_card) +} + state_find_solution :: proc( state: State, allocator := context.allocator, @@ -580,6 +920,99 @@ state_find_solution :: proc( } } +state_difficulty_score :: proc(state: ^State) -> int { + return( + 10000 * len(state.aside) + + 10 * state_nonsequential_count(state) - + 0 * state_trivially_removable_count(state) + + 0 * state_stacking_score(state) \ + ) +} + +state_stacking_score :: proc(state: ^State) -> (count: int) { + for &column in state.columns { + len := len(column) + count += len * len + } + return count +} + +state_nonsequential_count :: proc(state: ^State) -> (count: int) { + for &column in state.columns { + if len(column) < 2 {continue} + + for top_handle, top_idx in column[1:] { + top_card := card_from_handle(top_handle) + bottom_handle := column[top_idx] + bottom_card := card_from_handle(bottom_handle) + if top_card.suit != bottom_card.suit { + count += 1 + } else if abs(top_card.number - bottom_card.number) > 1 { + count += 1 + } + } + } + return count + +} + +state_trivially_removable_count :: proc(state: ^State) -> (count: int) { + moves := make([dynamic]Move, context.temp_allocator) + if len(state.aside) > 1 { + for &column, column_index in state.columns { + copied := state^ + moves := make([dynamic]Move, context.temp_allocator) + state_make_stacks(&copied, &moves) + count = max(count, len(moves)) + clear(&moves) + } + } + copied := state^ + state_make_stacks(&copied, &moves) + return len(moves) +} + +state_minor_inversion_count :: proc(state: ^State) -> (count: int) { + for &column in state.columns { + if len(column) < 2 {continue} + for top_handle, top_idx in column[1:] { + top_card := card_from_handle(top_handle) + if top_card.suit == .Major {continue} + for bottom_handle, bottom_idx in column[:top_idx] { + bottom_card := card_from_handle(bottom_handle) + if bottom_card.suit == .Major {continue} + if top_card.number >= bottom_card.number {count += 1} + } + } + } + return count +} + +state_major_inversion_count :: proc(state: ^State) -> (count: int) { + for &column in state.columns { + if len(column) < 2 {continue} + for top_handle, top_idx in column[1:] { + top_card := card_from_handle(top_handle) + if top_card.suit != .Major {continue} + for bottom_handle, bottom_idx in column[:top_idx] { + bottom_card := card_from_handle(bottom_handle) + if bottom_card.suit != .Major {continue} + if top_card.number >= 11 && + bottom_card.number >= 11 && + top_card.number < bottom_card.number { + count += 1 + } + if top_card.number <= 10 && + bottom_card.number <= 10 && + top_card.number > bottom_card.number { + count += 1 + } + } + } + } + return count +} + state_is_solved :: proc(state: ^State) -> bool { for &column in state.columns { if len(column) > 0 {return false} @@ -679,6 +1112,10 @@ state_pop :: proc(state: ^State, place: Place) -> (handle: Card_Handle, ok: bool #partial switch place.kind { case .Column: return pop_safe(&state.columns[place.index]) + case .Major: + return pop_safe(&state.major[Major_Pile_Kind(place.index)]) + case .Suit: + return pop_safe(&state.suits[place.index]) case .Aside: return pop_safe(&state.aside) } @@ -704,6 +1141,8 @@ state_push :: proc(state: ^State, place: Place, handle: Card_Handle) { } else { append(&state.aside, handle) } + case .Aside: + append(&state.aside, handle) } } @@ -739,22 +1178,61 @@ state_is_move_valid :: proc(state: ^State, move: Move) -> (ok: bool) { if slice.last(from_pile) != move.card {return false} to_pile := state_get_pile(state, move.to) + #partial switch move.from.kind { + case .Major, .Suit: + return false + } #partial switch move.to.kind { case .Column: if len(to_pile) == 0 {return true} top_card := card_from_handle(slice.last(to_pile)) - return top_card.suit == moved_card.suit && abs(top_card.number - moved_card.number) == 1 + return cards_are_stackable(moved_card, top_card) case .Major: return( state_can_stack_major_low(state, move.card) || state_can_stack_major_high(state, move.card) \ ) - case .Suit: + case .Suit, .Aside: return len(state.aside) == 0 } return false } +state_is_move_valid_reverse :: proc(state: ^State, move: Move) -> (ok: bool) { + if move.from == move.to {return false} + moved_card := card_from_handle(move.card) + to_pile := state_get_pile(state, move.to) + from_pile := state_get_pile(state, move.from) + if len(to_pile) == 0 {return false} + if slice.last(to_pile) != move.card {return false} + #partial switch move.from.kind { + case .Major, .Suit: + return false + case .Column: + if len(from_pile) >= 8 || + move.from.index >= 10 && len(from_pile) >= 7 || + move.from.index == 5 && len(from_pile) > 0 { + top_card := card_from_handle(slice.last(from_pile)) + if !cards_are_stackable(moved_card, top_card) {return false} + } + case .Aside: + if len(from_pile) > 0 {return false} + if state_can_stack_suit(state, move.card) {return false} + } + #partial switch move.to.kind { + case .Column: + if len(to_pile) > 1 { + above_card := card_from_handle(to_pile[len(to_pile) - 2]) + if !cards_are_stackable(moved_card, above_card) {return false} + } + } + return true +} + +cards_are_stackable :: proc(a, b: Card) -> bool { + return a.suit == b.suit && abs(a.number - b.number) == 1 +} + state_get_pile :: proc(state: ^State, place: Place) -> (pile: []Card_Handle) { switch place.kind { case .Column: @@ -898,6 +1376,9 @@ outline_color :: proc(card: Card) -> ray.Color { } short_title :: proc(card: Card) -> string { + if card.suit == .Major { + return fmt.tprintf("%d", card.number) + } number_name: string switch card.number { case 1: