useless solution search
This commit is contained in:
parent
064a29b8d6
commit
70b8e60f4e
1 changed files with 356 additions and 104 deletions
438
src/game.odin
438
src/game.odin
|
|
@ -11,9 +11,11 @@ import "core:fmt"
|
||||||
import "core:log"
|
import "core:log"
|
||||||
import "core:math/rand"
|
import "core:math/rand"
|
||||||
import "core:mem"
|
import "core:mem"
|
||||||
|
import "core:reflect"
|
||||||
import "core:slice"
|
import "core:slice"
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
import ray "vendor:raylib"
|
import ray "vendor:raylib"
|
||||||
|
import "vendor:raylib/rlgl"
|
||||||
|
|
||||||
MAJOR_COUNT :: 22
|
MAJOR_COUNT :: 22
|
||||||
MINOR_COUNT :: 14
|
MINOR_COUNT :: 14
|
||||||
|
|
@ -27,7 +29,7 @@ ROUNDED_RECT_SEGMENTS :: 6
|
||||||
GAP :: 16
|
GAP :: 16
|
||||||
CARD_SEPARATION :: 24
|
CARD_SEPARATION :: 24
|
||||||
CARD_DAMPING :: 70
|
CARD_DAMPING :: 70
|
||||||
AUTO_MOVE_INTERVAL :: 0.2
|
AUTO_MOVE_INTERVAL :: 0.1
|
||||||
|
|
||||||
game: Game
|
game: Game
|
||||||
Game :: struct {
|
Game :: struct {
|
||||||
|
|
@ -45,8 +47,10 @@ Game :: struct {
|
||||||
suit_textures: [Suit]ray.Texture,
|
suit_textures: [Suit]ray.Texture,
|
||||||
dragged_card: Maybe(Card_Handle),
|
dragged_card: Maybe(Card_Handle),
|
||||||
dragged_card_position: Vec2,
|
dragged_card_position: Vec2,
|
||||||
|
dragged_card_rotation: f32,
|
||||||
auto_moves: queue.Queue(Move),
|
auto_moves: queue.Queue(Move),
|
||||||
auto_move_cooldown: f32,
|
auto_move_cooldown: f32,
|
||||||
|
suits_overlay_opacity: f32,
|
||||||
}
|
}
|
||||||
Vec2 :: [2]f32
|
Vec2 :: [2]f32
|
||||||
Card :: struct {
|
Card :: struct {
|
||||||
|
|
@ -65,6 +69,9 @@ Card_Entity :: struct {
|
||||||
position: Vec2,
|
position: Vec2,
|
||||||
target_position: Vec2,
|
target_position: Vec2,
|
||||||
velocity: Vec2,
|
velocity: Vec2,
|
||||||
|
rotation: f32,
|
||||||
|
target_rotation: f32,
|
||||||
|
angular_velocity: f32,
|
||||||
is_draggable: bool,
|
is_draggable: bool,
|
||||||
state: Card_State_Kind,
|
state: Card_State_Kind,
|
||||||
}
|
}
|
||||||
|
|
@ -74,9 +81,8 @@ Card_State_Kind :: enum {
|
||||||
Locked,
|
Locked,
|
||||||
}
|
}
|
||||||
State :: struct {
|
State :: struct {
|
||||||
columns: [COLUMN_COUNT][dynamic; 32]Card_Handle,
|
columns: [COLUMN_COUNT][dynamic; 30]Card_Handle,
|
||||||
major_low: [dynamic; MAJOR_COUNT]Card_Handle,
|
major: [Major_Pile_Kind][dynamic; MAJOR_COUNT]Card_Handle,
|
||||||
major_high: [dynamic; MAJOR_COUNT]Card_Handle,
|
|
||||||
suits: [4][dynamic; MINOR_COUNT]Card_Handle,
|
suits: [4][dynamic; MINOR_COUNT]Card_Handle,
|
||||||
aside: [dynamic; 1]Card_Handle,
|
aside: [dynamic; 1]Card_Handle,
|
||||||
}
|
}
|
||||||
|
|
@ -89,10 +95,13 @@ Place :: struct {
|
||||||
kind: Place_Kind,
|
kind: Place_Kind,
|
||||||
index: int,
|
index: int,
|
||||||
}
|
}
|
||||||
|
Major_Pile_Kind :: enum {
|
||||||
|
Low,
|
||||||
|
High,
|
||||||
|
}
|
||||||
Place_Kind :: enum {
|
Place_Kind :: enum {
|
||||||
Column,
|
Column,
|
||||||
Major_Low,
|
Major,
|
||||||
Major_High,
|
|
||||||
Suit,
|
Suit,
|
||||||
Aside,
|
Aside,
|
||||||
}
|
}
|
||||||
|
|
@ -149,15 +158,32 @@ init :: proc() {
|
||||||
codepointCount = 0,
|
codepointCount = 0,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
queue.init(&magic_moves)
|
||||||
|
auto_moves := make([dynamic]Move, context.temp_allocator)
|
||||||
|
for {
|
||||||
|
log.info("searching solution…")
|
||||||
state_init(&game.state)
|
state_init(&game.state)
|
||||||
for &obj in game_cards_zipped() {
|
state_make_stacks(&game.state, &auto_moves)
|
||||||
obj.entity.position = card_position({.Column, 5})
|
queue.append(&game.auto_moves, ..auto_moves[:])
|
||||||
obj.entity.is_draggable = true
|
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[:])
|
||||||
game.displayed_state = game.state
|
game.displayed_state = game.state
|
||||||
log.infof("%#v", game.state)
|
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() {
|
update :: proc() {
|
||||||
free_all(context.temp_allocator)
|
free_all(context.temp_allocator)
|
||||||
delta := ray.GetFrameTime()
|
delta := ray.GetFrameTime()
|
||||||
|
|
@ -175,11 +201,14 @@ update :: proc() {
|
||||||
}
|
}
|
||||||
|
|
||||||
mouse_position := ray.GetMousePosition()
|
mouse_position := ray.GetMousePosition()
|
||||||
game.dragged_card_position = mouse_position - CARD_SIZE * 0.5
|
game.dragged_card_position = mouse_position
|
||||||
if ray.IsMouseButtonPressed(.LEFT) {
|
if ray.IsMouseButtonPressed(.LEFT) {
|
||||||
for &obj, index in game_cards_zipped() {
|
for &obj, index in game_cards_zipped() {
|
||||||
handle := Card_Handle(index)
|
handle := Card_Handle(index)
|
||||||
if rect_contains(rrect_from_pos_sz(obj.entity.position, CARD_SIZE), mouse_position) &&
|
if rect_contains(
|
||||||
|
rrect_from_pos_sz(obj.entity.position - CARD_SIZE * 0.5, CARD_SIZE),
|
||||||
|
mouse_position,
|
||||||
|
) &&
|
||||||
game.entities[int(handle)].is_draggable {
|
game.entities[int(handle)].is_draggable {
|
||||||
game.dragged_card = handle
|
game.dragged_card = handle
|
||||||
obj.entity.state = .Dragged
|
obj.entity.state = .Dragged
|
||||||
|
|
@ -187,6 +216,18 @@ update :: proc() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
game.dragged_card_rotation = 0
|
||||||
|
hovered_place := place_from_position(mouse_position)
|
||||||
|
draggin_card_over_aside :=
|
||||||
|
game.dragged_card != nil &&
|
||||||
|
hovered_place.kind == .Suit &&
|
||||||
|
!state_can_stack_suit(&game.displayed_state, game.dragged_card.?) &&
|
||||||
|
len(game.state.aside) == 0
|
||||||
|
if draggin_card_over_aside {
|
||||||
|
game.dragged_card_rotation = -math.PI / 2
|
||||||
|
}
|
||||||
|
|
||||||
if dragged_card, ok := game.dragged_card.?; ok {
|
if dragged_card, ok := game.dragged_card.?; ok {
|
||||||
obj := &game_cards_zipped()[int(dragged_card)]
|
obj := &game_cards_zipped()[int(dragged_card)]
|
||||||
if ray.IsMouseButtonReleased(.LEFT) {
|
if ray.IsMouseButtonReleased(.LEFT) {
|
||||||
|
|
@ -197,7 +238,11 @@ update :: proc() {
|
||||||
to = new_place,
|
to = new_place,
|
||||||
card = dragged_card,
|
card = dragged_card,
|
||||||
}
|
}
|
||||||
effects, ok := state_perform_move(&game.state, move)
|
effects, ok := state_perform_move(
|
||||||
|
&game.state,
|
||||||
|
move,
|
||||||
|
allocator = context.temp_allocator,
|
||||||
|
)
|
||||||
obj.entity.state = .Idle
|
obj.entity.state = .Idle
|
||||||
game.dragged_card = nil
|
game.dragged_card = nil
|
||||||
for auto_move in effects {
|
for auto_move in effects {
|
||||||
|
|
@ -208,7 +253,24 @@ update :: proc() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
game.auto_move_cooldown -= ray.GetFrameTime()
|
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 game.auto_move_cooldown <= 0 {
|
||||||
if auto_move, ok := queue.pop_front_safe(&game.auto_moves); ok {
|
if auto_move, ok := queue.pop_front_safe(&game.auto_moves); ok {
|
||||||
game.auto_move_cooldown = AUTO_MOVE_INTERVAL
|
game.auto_move_cooldown = AUTO_MOVE_INTERVAL
|
||||||
|
|
@ -224,13 +286,20 @@ update :: proc() {
|
||||||
|
|
||||||
|
|
||||||
// NOTE(lilith): Draw slots and set card target positions
|
// NOTE(lilith): Draw slots and set card target positions
|
||||||
slot_draw(card_position({.Major_Low, 0}))
|
for &entity in game.entities {
|
||||||
for handle in game.displayed_state.major_low {
|
entity.target_rotation = 0
|
||||||
game.entities[int(handle)].target_position = card_position({.Major_Low, 0})
|
|
||||||
}
|
}
|
||||||
slot_draw(card_position({.Major_High, 0}))
|
for major_pile, major_pile_kind in game.displayed_state.major {
|
||||||
for handle in game.displayed_state.major_high {
|
place := Place{.Major, int(major_pile_kind)}
|
||||||
game.entities[int(handle)].target_position = card_position({.Major_High, 0})
|
position := card_position(place)
|
||||||
|
slot_draw(position)
|
||||||
|
for handle in major_pile {
|
||||||
|
game.entities[int(handle)].target_position = position
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for handle in game.displayed_state.aside {
|
||||||
|
game.entities[int(handle)].target_position = card_position({.Aside, 0})
|
||||||
|
game.entities[int(handle)].target_rotation = -math.PI / 2
|
||||||
}
|
}
|
||||||
|
|
||||||
for suit in Suit.Chalice ..= Suit.Wand {
|
for suit in Suit.Chalice ..= Suit.Wand {
|
||||||
|
|
@ -242,7 +311,7 @@ update :: proc() {
|
||||||
ray.DrawTexturePro(
|
ray.DrawTexturePro(
|
||||||
game.suit_textures[suit],
|
game.suit_textures[suit],
|
||||||
source = rrect_from_pos_sz({0, 0}, SUIT_TEX_SIZE),
|
source = rrect_from_pos_sz({0, 0}, SUIT_TEX_SIZE),
|
||||||
dest = rrect_from_pos_sz(suit_position + CARD_SIZE * 0.5 - icon_size * 0.5, icon_size),
|
dest = rrect_from_pos_sz(suit_position - icon_size * 0.5, icon_size),
|
||||||
origin = {0, 0},
|
origin = {0, 0},
|
||||||
rotation = 0,
|
rotation = 0,
|
||||||
tint = {255, 255, 255, 100},
|
tint = {255, 255, 255, 100},
|
||||||
|
|
@ -271,14 +340,21 @@ update :: proc() {
|
||||||
target_position,
|
target_position,
|
||||||
dt = delta,
|
dt = delta,
|
||||||
)
|
)
|
||||||
|
target_rotation :=
|
||||||
|
obj.entity.state != .Dragged ? entity.target_rotation : game.dragged_card_rotation
|
||||||
|
entity.rotation, entity.angular_velocity = critical_spring_scalar(
|
||||||
|
entity.rotation,
|
||||||
|
entity.angular_velocity,
|
||||||
|
target_rotation,
|
||||||
|
dt = delta,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE(lilith): Draw cards
|
// NOTE(lilith): Draw cards
|
||||||
for handle in game.displayed_state.major_low {
|
for major_pile in game.displayed_state.major {
|
||||||
|
for handle in major_pile {
|
||||||
card_draw(handle)
|
card_draw(handle)
|
||||||
}
|
}
|
||||||
for handle in game.displayed_state.major_high {
|
|
||||||
card_draw(handle)
|
|
||||||
}
|
}
|
||||||
for suit in Suit.Chalice ..= Suit.Wand {
|
for suit in Suit.Chalice ..= Suit.Wand {
|
||||||
suit_index := int(suit) - 1
|
suit_index := int(suit) - 1
|
||||||
|
|
@ -287,6 +363,21 @@ update :: proc() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if draggin_card_over_aside || len(game.displayed_state.aside) != 0 {
|
||||||
|
game.suits_overlay_opacity = min(160, game.suits_overlay_opacity + 500 * delta)
|
||||||
|
} else {
|
||||||
|
game.suits_overlay_opacity = max(0, game.suits_overlay_opacity - 500 * delta)
|
||||||
|
}
|
||||||
|
ray.DrawRectangleRounded(
|
||||||
|
rrect_from_pos_sz(
|
||||||
|
card_position({.Suit, 0}) - CARD_SIZE / 2 - GAP / 2,
|
||||||
|
{CARD_SIZE.x * 4 + GAP * 3, CARD_SIZE.y} + Vec2{GAP, GAP},
|
||||||
|
),
|
||||||
|
CARD_ROUNDNESS,
|
||||||
|
ROUNDED_RECT_SEGMENTS,
|
||||||
|
ray.Color{0, 0, 0, u8(game.suits_overlay_opacity)},
|
||||||
|
)
|
||||||
|
|
||||||
if len(game.displayed_state.aside) > 0 {
|
if len(game.displayed_state.aside) > 0 {
|
||||||
card_draw(game.displayed_state.aside[0])
|
card_draw(game.displayed_state.aside[0])
|
||||||
}
|
}
|
||||||
|
|
@ -304,7 +395,7 @@ update :: proc() {
|
||||||
|
|
||||||
slot_draw :: proc(position: Vec2) {
|
slot_draw :: proc(position: Vec2) {
|
||||||
ray.DrawRectangleRounded(
|
ray.DrawRectangleRounded(
|
||||||
rrect_from_pos_sz(position, CARD_SIZE),
|
rrect_from_pos_sz(position - CARD_SIZE * 0.5, CARD_SIZE),
|
||||||
CARD_ROUNDNESS,
|
CARD_ROUNDNESS,
|
||||||
ROUNDED_RECT_SEGMENTS,
|
ROUNDED_RECT_SEGMENTS,
|
||||||
color = ray.Color{0, 0, 0, 90},
|
color = ray.Color{0, 0, 0, 90},
|
||||||
|
|
@ -316,14 +407,19 @@ card_draw :: proc(handle: Card_Handle) {
|
||||||
card := obj.card
|
card := obj.card
|
||||||
entity := obj.entity
|
entity := obj.entity
|
||||||
|
|
||||||
|
rlgl.PushMatrix(); defer rlgl.PopMatrix()
|
||||||
|
rlgl.Translatef(entity.position.x, entity.position.y, 0)
|
||||||
|
rlgl.Rotatef(entity.rotation * math.DEG_PER_RAD, 0, 0, 1)
|
||||||
|
rlgl.Translatef(-entity.position.x, -entity.position.y, 0)
|
||||||
|
|
||||||
ray.DrawRectangleRounded(
|
ray.DrawRectangleRounded(
|
||||||
rrect_from_pos_sz(entity.position, CARD_SIZE),
|
rrect_from_pos_sz(entity.position - CARD_SIZE * 0.5, CARD_SIZE),
|
||||||
CARD_ROUNDNESS,
|
CARD_ROUNDNESS,
|
||||||
ROUNDED_RECT_SEGMENTS,
|
ROUNDED_RECT_SEGMENTS,
|
||||||
color = background_color(card),
|
color = background_color(card),
|
||||||
)
|
)
|
||||||
ray.DrawRectangleRoundedLinesEx(
|
ray.DrawRectangleRoundedLinesEx(
|
||||||
rrect_from_pos_sz(entity.position, CARD_SIZE),
|
rrect_from_pos_sz(entity.position - CARD_SIZE * 0.5, CARD_SIZE),
|
||||||
CARD_ROUNDNESS,
|
CARD_ROUNDNESS,
|
||||||
ROUNDED_RECT_SEGMENTS,
|
ROUNDED_RECT_SEGMENTS,
|
||||||
lineThick = 2,
|
lineThick = 2,
|
||||||
|
|
@ -339,6 +435,7 @@ card_draw :: proc(handle: Card_Handle) {
|
||||||
case:
|
case:
|
||||||
offset = {4, 0}
|
offset = {4, 0}
|
||||||
}
|
}
|
||||||
|
offset -= CARD_SIZE * 0.5
|
||||||
ray.DrawTextEx(
|
ray.DrawTextEx(
|
||||||
game.card_font,
|
game.card_font,
|
||||||
text,
|
text,
|
||||||
|
|
@ -367,6 +464,7 @@ state_init :: proc(state: ^State) {
|
||||||
for &handle, index in deck {handle = Card_Handle(index)}
|
for &handle, index in deck {handle = Card_Handle(index)}
|
||||||
rand.shuffle(deck[:])
|
rand.shuffle(deck[:])
|
||||||
target_column: int
|
target_column: int
|
||||||
|
state^ = {}
|
||||||
for handle, index in deck {
|
for handle, index in deck {
|
||||||
append(&state.columns[target_column], handle)
|
append(&state.columns[target_column], handle)
|
||||||
target_column += 1
|
target_column += 1
|
||||||
|
|
@ -382,9 +480,118 @@ state_init :: proc(state: ^State) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
state_find_solution :: proc(
|
||||||
|
state: State,
|
||||||
|
allocator := context.allocator,
|
||||||
|
) -> (
|
||||||
|
solution: [dynamic]Move,
|
||||||
|
is_solution: bool,
|
||||||
|
) {
|
||||||
|
DEPTH_LIMIT :: 5000
|
||||||
|
TRIAL_LIMIT :: 100000000
|
||||||
|
solution = make([dynamic]Move, allocator)
|
||||||
|
|
||||||
|
searcher := Searcher {
|
||||||
|
states = make([dynamic]State, allocator),
|
||||||
|
moves = make([dynamic]Move, allocator),
|
||||||
|
rejectables = make(map[[616]byte]struct{}, allocator),
|
||||||
|
}
|
||||||
|
append(&searcher.states, state)
|
||||||
|
return solution, inner(&searcher)
|
||||||
|
|
||||||
|
Searcher :: struct {
|
||||||
|
states: [dynamic]State,
|
||||||
|
moves: [dynamic]Move,
|
||||||
|
rejectables: map[[616]byte]struct{},
|
||||||
|
trials: int,
|
||||||
|
}
|
||||||
|
|
||||||
|
inner :: proc(searcher: ^Searcher) -> (is_solution: bool) {
|
||||||
|
if len(searcher.moves) > DEPTH_LIMIT {return false}
|
||||||
|
searcher.trials += 1
|
||||||
|
if searcher.trials > TRIAL_LIMIT {return false}
|
||||||
|
state := slice.last(searcher.states[:])
|
||||||
|
if state_is_solved(&state) {return true}
|
||||||
|
// log.debugf("depth: %d, state: %v", len(searcher.moves), state)
|
||||||
|
|
||||||
|
recurse :: proc(searcher: ^Searcher, move: Move) -> (is_solution: bool) {
|
||||||
|
state := slice.last(searcher.states[:])
|
||||||
|
if _, ok := state_perform_move(
|
||||||
|
&state,
|
||||||
|
move,
|
||||||
|
auto_make_stacks = true,
|
||||||
|
allocator = context.temp_allocator,
|
||||||
|
); ok {
|
||||||
|
for previous_state in searcher.states {
|
||||||
|
if reflect.eq(
|
||||||
|
previous_state,
|
||||||
|
state,
|
||||||
|
including_indirect_array_recursion = true,
|
||||||
|
recursion_level = 4,
|
||||||
|
) {return false}
|
||||||
|
}
|
||||||
|
if transmute([616]byte)state in searcher.rejectables {return false}
|
||||||
|
append(&searcher.moves, move)
|
||||||
|
append(&searcher.states, state)
|
||||||
|
if inner(searcher) {return true}
|
||||||
|
searcher.rejectables[transmute([616]byte)state] = {}
|
||||||
|
pop(&searcher.states)
|
||||||
|
pop(&searcher.moves)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if len(state.aside) > 0 {
|
||||||
|
for &to_column, to_column_index in state.columns {
|
||||||
|
move := Move {
|
||||||
|
card = state.aside[0],
|
||||||
|
from = {.Aside, 0},
|
||||||
|
to = {.Column, to_column_index},
|
||||||
|
}
|
||||||
|
if recurse(searcher, move) {return true}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
has_empty_columns := false
|
||||||
|
for column in state.columns {if len(column) == 0 {has_empty_columns = true; break}}
|
||||||
|
|
||||||
|
for &from_column, from_column_index in state.columns {
|
||||||
|
if len(from_column) == 0 {continue}
|
||||||
|
for &to_column, to_column_index in state.columns {
|
||||||
|
if len(from_column) == 1 && len(to_column) == 0 {continue}
|
||||||
|
move := Move {
|
||||||
|
card = slice.last(from_column[:]),
|
||||||
|
from = {.Column, from_column_index},
|
||||||
|
to = {.Column, to_column_index},
|
||||||
|
}
|
||||||
|
if recurse(searcher, move) {return true}
|
||||||
|
}
|
||||||
|
if !has_empty_columns {
|
||||||
|
move := Move {
|
||||||
|
card = slice.last(from_column[:]),
|
||||||
|
from = {.Column, from_column_index},
|
||||||
|
to = {.Suit, 0},
|
||||||
|
}
|
||||||
|
if recurse(searcher, move) {return true}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
state_is_solved :: proc(state: ^State) -> bool {
|
||||||
|
for &column in state.columns {
|
||||||
|
if len(column) > 0 {return false}
|
||||||
|
}
|
||||||
|
if len(state.aside) > 0 {return false}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
state_perform_move :: proc(
|
state_perform_move :: proc(
|
||||||
state: ^State,
|
state: ^State,
|
||||||
move: Move,
|
move: Move,
|
||||||
|
auto_make_stacks := true,
|
||||||
allocator := context.allocator,
|
allocator := context.allocator,
|
||||||
) -> (
|
) -> (
|
||||||
auto_moves: [dynamic]Move,
|
auto_moves: [dynamic]Move,
|
||||||
|
|
@ -407,14 +614,56 @@ state_perform_move :: proc(
|
||||||
next_move.card = next_handle
|
next_move.card = next_handle
|
||||||
state_apply_single_move(state, next_move)
|
state_apply_single_move(state, next_move)
|
||||||
append(&auto_moves, next_move)
|
append(&auto_moves, next_move)
|
||||||
|
last_moved_handle = next_handle
|
||||||
|
last_moved_card = next_card
|
||||||
} else {break}
|
} else {break}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
state_get_auto_moves(state, &auto_moves)
|
if auto_make_stacks {
|
||||||
|
state_make_stacks(state, &auto_moves)
|
||||||
|
}
|
||||||
return auto_moves, true
|
return auto_moves, true
|
||||||
}
|
}
|
||||||
|
|
||||||
state_get_auto_moves :: proc(state: ^State, auto_moves: ^[dynamic]Move) {
|
state_make_stacks :: proc(state: ^State, auto_moves: ^[dynamic]Move) {
|
||||||
|
big_loop: for {
|
||||||
|
for &column, column_index in state.columns {
|
||||||
|
if len(column) == 0 {continue}
|
||||||
|
handle := slice.last(column[:])
|
||||||
|
card := card_from_handle(handle)
|
||||||
|
if state_can_stack_suit(state, handle) {
|
||||||
|
move := Move {
|
||||||
|
card = handle,
|
||||||
|
from = {.Column, column_index},
|
||||||
|
to = {.Suit, int(card.suit) - 1},
|
||||||
|
}
|
||||||
|
state_apply_single_move(state, move)
|
||||||
|
append(auto_moves, move)
|
||||||
|
continue big_loop
|
||||||
|
}
|
||||||
|
if state_can_stack_major_low(state, handle) {
|
||||||
|
move := Move {
|
||||||
|
card = handle,
|
||||||
|
from = {.Column, column_index},
|
||||||
|
to = {.Major, 0},
|
||||||
|
}
|
||||||
|
state_apply_single_move(state, move)
|
||||||
|
append(auto_moves, move)
|
||||||
|
continue big_loop
|
||||||
|
}
|
||||||
|
if state_can_stack_major_high(state, handle) {
|
||||||
|
move := Move {
|
||||||
|
card = handle,
|
||||||
|
from = {.Column, column_index},
|
||||||
|
to = {.Major, 1},
|
||||||
|
}
|
||||||
|
state_apply_single_move(state, move)
|
||||||
|
append(auto_moves, move)
|
||||||
|
continue big_loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
state_apply_single_move :: proc(state: ^State, move: Move) -> (ok: bool) {
|
state_apply_single_move :: proc(state: ^State, move: Move) -> (ok: bool) {
|
||||||
|
|
@ -427,15 +676,9 @@ state_apply_single_move :: proc(state: ^State, move: Move) -> (ok: bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
state_pop :: proc(state: ^State, place: Place) -> (handle: Card_Handle, ok: bool) {
|
state_pop :: proc(state: ^State, place: Place) -> (handle: Card_Handle, ok: bool) {
|
||||||
switch place.kind {
|
#partial switch place.kind {
|
||||||
case .Column:
|
case .Column:
|
||||||
return pop_safe(&state.columns[place.index])
|
return pop_safe(&state.columns[place.index])
|
||||||
case .Major_Low:
|
|
||||||
return pop_safe(&state.major_low)
|
|
||||||
case .Major_High:
|
|
||||||
return pop_safe(&state.major_high)
|
|
||||||
case .Suit:
|
|
||||||
return pop_safe(&state.suits[place.index])
|
|
||||||
case .Aside:
|
case .Aside:
|
||||||
return pop_safe(&state.aside)
|
return pop_safe(&state.aside)
|
||||||
}
|
}
|
||||||
|
|
@ -443,18 +686,49 @@ state_pop :: proc(state: ^State, place: Place) -> (handle: Card_Handle, ok: bool
|
||||||
}
|
}
|
||||||
|
|
||||||
state_push :: proc(state: ^State, place: Place, handle: Card_Handle) {
|
state_push :: proc(state: ^State, place: Place, handle: Card_Handle) {
|
||||||
switch place.kind {
|
#partial switch place.kind {
|
||||||
case .Column:
|
case .Column:
|
||||||
append(&state.columns[place.index], handle)
|
append(&state.columns[place.index], handle)
|
||||||
case .Major_Low:
|
case .Major:
|
||||||
append(&state.major_low, handle)
|
card := card_from_handle(handle)
|
||||||
case .Major_High:
|
if state_can_stack_major_low(state, handle) {
|
||||||
append(&state.major_high, handle)
|
append(&state.major[.Low], handle)
|
||||||
|
} else {
|
||||||
|
append(&state.major[.High], handle)
|
||||||
|
}
|
||||||
case .Suit:
|
case .Suit:
|
||||||
append(&state.suits[place.index], handle)
|
if state_can_stack_suit(state, handle) {
|
||||||
case .Aside:
|
suit_index := int(card_from_handle(handle).suit) - 1
|
||||||
|
suit_pile := &state.suits[suit_index]
|
||||||
|
append(&state.suits[suit_index], handle)
|
||||||
|
} else {
|
||||||
append(&state.aside, handle)
|
append(&state.aside, handle)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
state_can_stack_suit :: proc(state: ^State, handle: Card_Handle) -> bool {
|
||||||
|
if len(state.aside) > 0 {return false}
|
||||||
|
card := card_from_handle(handle)
|
||||||
|
if card.suit == .Major {return false}
|
||||||
|
suit_index := int(card.suit) - 1
|
||||||
|
suit_pile := &state.suits[suit_index]
|
||||||
|
if len(suit_pile) == 0 {return card.number == 1}
|
||||||
|
return card.number - 1 == card_from_handle(slice.last(suit_pile[:])).number
|
||||||
|
}
|
||||||
|
|
||||||
|
state_can_stack_major_low :: proc(state: ^State, handle: Card_Handle) -> bool {
|
||||||
|
card := card_from_handle(handle)
|
||||||
|
if card.suit != .Major {return false}
|
||||||
|
if len(state.major[.Low]) == 0 {return card.number == 0}
|
||||||
|
return card.number - 1 == card_from_handle(slice.last(state.major[.Low][:])).number
|
||||||
|
}
|
||||||
|
|
||||||
|
state_can_stack_major_high :: proc(state: ^State, handle: Card_Handle) -> bool {
|
||||||
|
card := card_from_handle(handle)
|
||||||
|
if card.suit != .Major {return false}
|
||||||
|
if len(state.major[.High]) == 0 {return card.number == MAJOR_COUNT - 1}
|
||||||
|
return card.number + 1 == card_from_handle(slice.last(state.major[.High][:])).number
|
||||||
}
|
}
|
||||||
|
|
||||||
state_is_move_valid :: proc(state: ^State, move: Move) -> (ok: bool) {
|
state_is_move_valid :: proc(state: ^State, move: Move) -> (ok: bool) {
|
||||||
|
|
@ -465,28 +739,20 @@ state_is_move_valid :: proc(state: ^State, move: Move) -> (ok: bool) {
|
||||||
if slice.last(from_pile) != move.card {return false}
|
if slice.last(from_pile) != move.card {return false}
|
||||||
|
|
||||||
to_pile := state_get_pile(state, move.to)
|
to_pile := state_get_pile(state, move.to)
|
||||||
switch move.to.kind {
|
#partial switch move.to.kind {
|
||||||
case .Column:
|
case .Column:
|
||||||
if len(to_pile) == 0 {return true}
|
if len(to_pile) == 0 {return true}
|
||||||
top_card := card_from_handle(slice.last(to_pile))
|
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 top_card.suit == moved_card.suit && abs(top_card.number - moved_card.number) == 1
|
||||||
case .Major_Low:
|
case .Major:
|
||||||
if moved_card.suit != .Major {return false}
|
return(
|
||||||
if len(to_pile) == 0 {return moved_card.number == 0}
|
state_can_stack_major_low(state, move.card) ||
|
||||||
return card_from_handle(slice.last(to_pile)).number == moved_card.number - 1
|
state_can_stack_major_high(state, move.card) \
|
||||||
case .Major_High:
|
)
|
||||||
if moved_card.suit != .Major {return false}
|
|
||||||
if len(to_pile) == 0 {return moved_card.number == MAJOR_COUNT - 1}
|
|
||||||
return card_from_handle(slice.last(to_pile)).number == moved_card.number + 1
|
|
||||||
case .Suit:
|
case .Suit:
|
||||||
if len(state.aside) > 0 {return false}
|
return len(state.aside) == 0
|
||||||
if int(moved_card.suit) - 1 != move.to.index {return false}
|
|
||||||
if len(to_pile) == 0 {return moved_card.number == 1}
|
|
||||||
return card_from_handle(slice.last(to_pile)).number == moved_card.number - 1
|
|
||||||
case .Aside:
|
|
||||||
return len(to_pile) == 0
|
|
||||||
}
|
}
|
||||||
unreachable()
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
state_get_pile :: proc(state: ^State, place: Place) -> (pile: []Card_Handle) {
|
state_get_pile :: proc(state: ^State, place: Place) -> (pile: []Card_Handle) {
|
||||||
|
|
@ -494,10 +760,8 @@ state_get_pile :: proc(state: ^State, place: Place) -> (pile: []Card_Handle) {
|
||||||
case .Column:
|
case .Column:
|
||||||
assert(0 <= place.index && place.index < COLUMN_COUNT)
|
assert(0 <= place.index && place.index < COLUMN_COUNT)
|
||||||
return state.columns[place.index][:]
|
return state.columns[place.index][:]
|
||||||
case .Major_Low:
|
case .Major:
|
||||||
return state.major_low[:]
|
return state.major[Major_Pile_Kind(place.index)][:]
|
||||||
case .Major_High:
|
|
||||||
return state.major_high[:]
|
|
||||||
case .Suit:
|
case .Suit:
|
||||||
assert(0 <= place.index && place.index < 4)
|
assert(0 <= place.index && place.index < 4)
|
||||||
return state.suits[place.index][:]
|
return state.suits[place.index][:]
|
||||||
|
|
@ -511,8 +775,9 @@ state_card_place :: proc(state: ^State, handle: Card_Handle) -> Place {
|
||||||
for column_index in 0 ..< COLUMN_COUNT {
|
for column_index in 0 ..< COLUMN_COUNT {
|
||||||
if slice.contains(state.columns[column_index][:], handle) {return {.Column, column_index}}
|
if slice.contains(state.columns[column_index][:], handle) {return {.Column, column_index}}
|
||||||
}
|
}
|
||||||
if slice.contains(state.major_low[:], handle) {return {.Major_Low, 0}}
|
for &major_pile, major_pile_kind in state.major {
|
||||||
if slice.contains(state.major_high[:], handle) {return {.Major_High, 0}}
|
if slice.contains(major_pile[:], handle) {return {.Major, int(major_pile_kind)}}
|
||||||
|
}
|
||||||
for suit in 0 ..< 4 {
|
for suit in 0 ..< 4 {
|
||||||
if slice.contains(state.suits[suit][:], handle) {return {.Suit, suit}}
|
if slice.contains(state.suits[suit][:], handle) {return {.Suit, suit}}
|
||||||
}
|
}
|
||||||
|
|
@ -530,29 +795,29 @@ card_position :: proc(place: Place, pile_index: int = 0) -> (position: Vec2) {
|
||||||
switch place.kind {
|
switch place.kind {
|
||||||
case .Column:
|
case .Column:
|
||||||
column_offset := Vec2{f32(place.index) * (CARD_SIZE.x + GAP), 0} + column_section_offset
|
column_offset := Vec2{f32(place.index) * (CARD_SIZE.x + GAP), 0} + column_section_offset
|
||||||
return Vec2{0, f32(pile_index) * CARD_SEPARATION} + column_offset
|
position = Vec2{0, f32(pile_index) * CARD_SEPARATION} + column_offset
|
||||||
case .Major_Low:
|
case .Major:
|
||||||
return top_section_offset
|
position = top_section_offset + Vec2{f32(place.index) * (CARD_SIZE.x + GAP), 0}
|
||||||
case .Major_High:
|
|
||||||
return top_section_offset + Vec2{CARD_SIZE.x + GAP, 0}
|
|
||||||
case .Suit:
|
case .Suit:
|
||||||
return suits_section_offset + Vec2{f32(place.index) * (CARD_SIZE.x + GAP), 0}
|
position = suits_section_offset + Vec2{f32(place.index) * (CARD_SIZE.x + GAP), 0}
|
||||||
case .Aside:
|
case .Aside:
|
||||||
return suits_section_offset + Vec2{1.5 * (CARD_SIZE.x + GAP), 0}
|
suits_section_center :=
|
||||||
|
suits_section_offset + 0.5 * Vec2{4 * CARD_SIZE.x + 3 * GAP, CARD_SIZE.y}
|
||||||
|
return suits_section_center
|
||||||
}
|
}
|
||||||
unreachable()
|
return position + CARD_SIZE * 0.5
|
||||||
}
|
}
|
||||||
|
|
||||||
place_from_position :: proc(p: Vec2) -> (place: Place) {
|
place_from_position :: proc(p: Vec2) -> (place: Place) {
|
||||||
for column_index in 0 ..< COLUMN_COUNT {
|
for column_index in 0 ..< COLUMN_COUNT {
|
||||||
place := Place{.Column, column_index}
|
place := Place{.Column, column_index}
|
||||||
pos := card_position(place)
|
pos := card_position(place) - CARD_SIZE * 0.5 - GAP * 0.5
|
||||||
rect := rrect_from_pos_sz(pos, {CARD_SIZE.x, 10000})
|
rect := rrect_from_pos_sz(pos, {CARD_SIZE.x + GAP, 10000})
|
||||||
if rect_contains(rect, p) {return place}
|
if rect_contains(rect, p) {return place}
|
||||||
}
|
}
|
||||||
for place in ([]Place{{.Major_Low, 0}, {.Major_High, 0}, {.Suit, 0}, {.Suit, 1}, {.Suit, 2}, {.Suit, 3}}) {
|
for place in ([]Place{{.Major, 0}, {.Major, 1}, {.Suit, 0}, {.Suit, 1}, {.Suit, 2}, {.Suit, 3}}) {
|
||||||
pos := card_position(place)
|
pos := card_position(place) - CARD_SIZE * 0.5 - GAP * 0.5
|
||||||
rect := rrect_from_pos_sz(pos, CARD_SIZE)
|
rect := rrect_from_pos_sz(pos, CARD_SIZE + GAP)
|
||||||
if rect_contains(rect, p) {return place}
|
if rect_contains(rect, p) {return place}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -633,19 +898,6 @@ outline_color :: proc(card: Card) -> ray.Color {
|
||||||
}
|
}
|
||||||
|
|
||||||
short_title :: proc(card: Card) -> string {
|
short_title :: proc(card: Card) -> string {
|
||||||
suit_name: string
|
|
||||||
switch card.suit {
|
|
||||||
case .Major:
|
|
||||||
return fmt.tprintf("%d", card.number)
|
|
||||||
case .Chalice:
|
|
||||||
suit_name = "Chalices"
|
|
||||||
case .Coin:
|
|
||||||
suit_name = "Coins"
|
|
||||||
case .Sword:
|
|
||||||
suit_name = "Swords"
|
|
||||||
case .Wand:
|
|
||||||
suit_name = "Wands"
|
|
||||||
}
|
|
||||||
number_name: string
|
number_name: string
|
||||||
switch card.number {
|
switch card.number {
|
||||||
case 1:
|
case 1:
|
||||||
|
|
@ -699,8 +951,8 @@ critical_spring_scalar :: proc(
|
||||||
position: f32,
|
position: f32,
|
||||||
velocity: f32,
|
velocity: f32,
|
||||||
goal_position: f32,
|
goal_position: f32,
|
||||||
goal_velocity: f32,
|
goal_velocity: f32 = 0,
|
||||||
damping: f32,
|
damping: f32 = CARD_DAMPING,
|
||||||
dt: f32,
|
dt: f32,
|
||||||
) -> (
|
) -> (
|
||||||
new_position: f32,
|
new_position: f32,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue