2020-08-18 09:07:06 +03:00
|
|
|
import gg
|
|
|
|
import gx
|
2020-09-26 09:54:04 +03:00
|
|
|
import math
|
2020-08-18 09:07:06 +03:00
|
|
|
import os
|
|
|
|
import rand
|
2020-09-11 13:19:38 +03:00
|
|
|
import time
|
2020-08-18 09:07:06 +03:00
|
|
|
|
2020-09-11 13:19:38 +03:00
|
|
|
struct App {
|
|
|
|
mut:
|
2022-09-15 07:59:31 +03:00
|
|
|
gg &gg.Context = unsafe { nil }
|
2020-09-27 22:47:26 +03:00
|
|
|
touch TouchInfo
|
|
|
|
ui Ui
|
|
|
|
theme &Theme = themes[0]
|
|
|
|
theme_idx int
|
|
|
|
board Board
|
|
|
|
undo []Undo
|
|
|
|
atickers [4][4]int
|
2021-01-23 01:24:48 +03:00
|
|
|
state GameState = .play
|
2020-09-27 22:47:26 +03:00
|
|
|
tile_format TileFormat = .normal
|
|
|
|
moves int
|
2022-09-15 07:59:31 +03:00
|
|
|
perf &Perf = unsafe { nil }
|
2020-09-27 22:47:26 +03:00
|
|
|
is_ai_mode bool
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
|
|
|
|
2020-09-11 13:19:38 +03:00
|
|
|
struct Ui {
|
|
|
|
mut:
|
2020-09-26 09:54:04 +03:00
|
|
|
dpi_scale f32
|
2020-09-11 13:19:38 +03:00
|
|
|
tile_size int
|
|
|
|
border_size int
|
|
|
|
padding_size int
|
|
|
|
header_size int
|
|
|
|
font_size int
|
|
|
|
window_width int
|
|
|
|
window_height int
|
|
|
|
x_padding int
|
|
|
|
y_padding int
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Theme {
|
|
|
|
bg_color gx.Color
|
|
|
|
padding_color gx.Color
|
|
|
|
text_color gx.Color
|
|
|
|
game_over_color gx.Color
|
|
|
|
victory_color gx.Color
|
|
|
|
tile_colors []gx.Color
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2021-01-23 01:24:48 +03:00
|
|
|
themes = [
|
2020-09-27 22:47:26 +03:00
|
|
|
&Theme{
|
2020-09-11 13:19:38 +03:00
|
|
|
bg_color: gx.rgb(250, 248, 239)
|
|
|
|
padding_color: gx.rgb(143, 130, 119)
|
|
|
|
victory_color: gx.rgb(100, 160, 100)
|
|
|
|
game_over_color: gx.rgb(190, 50, 50)
|
|
|
|
text_color: gx.black
|
|
|
|
tile_colors: [
|
2021-01-23 01:24:48 +03:00
|
|
|
gx.rgb(205, 193, 180), /* Empty / 0 tile */
|
|
|
|
gx.rgb(238, 228, 218), /* 2 */
|
|
|
|
gx.rgb(237, 224, 200), /* 4 */
|
|
|
|
gx.rgb(242, 177, 121), /* 8 */
|
|
|
|
gx.rgb(245, 149, 99), /* 16 */
|
|
|
|
gx.rgb(246, 124, 95), /* 32 */
|
|
|
|
gx.rgb(246, 94, 59), /* 64 */
|
|
|
|
gx.rgb(237, 207, 114), /* 128 */
|
|
|
|
gx.rgb(237, 204, 97), /* 256 */
|
|
|
|
gx.rgb(237, 200, 80), /* 512 */
|
|
|
|
gx.rgb(237, 197, 63), /* 1024 */
|
2021-02-04 18:34:49 +03:00
|
|
|
gx.rgb(237, 194, 46),
|
2020-09-11 13:19:38 +03:00
|
|
|
]
|
|
|
|
},
|
2020-09-27 22:47:26 +03:00
|
|
|
&Theme{
|
2020-09-11 13:19:38 +03:00
|
|
|
bg_color: gx.rgb(55, 55, 55)
|
|
|
|
padding_color: gx.rgb(68, 60, 59)
|
|
|
|
victory_color: gx.rgb(100, 160, 100)
|
|
|
|
game_over_color: gx.rgb(190, 50, 50)
|
|
|
|
text_color: gx.white
|
|
|
|
tile_colors: [
|
2020-09-27 22:47:26 +03:00
|
|
|
gx.rgb(123, 115, 108),
|
|
|
|
gx.rgb(142, 136, 130),
|
|
|
|
gx.rgb(142, 134, 120),
|
|
|
|
gx.rgb(145, 106, 72),
|
|
|
|
gx.rgb(147, 89, 59),
|
|
|
|
gx.rgb(147, 74, 57),
|
|
|
|
gx.rgb(147, 56, 35),
|
|
|
|
gx.rgb(142, 124, 68),
|
|
|
|
gx.rgb(142, 122, 58),
|
|
|
|
gx.rgb(142, 120, 48),
|
|
|
|
gx.rgb(142, 118, 37),
|
|
|
|
gx.rgb(142, 116, 27),
|
2020-09-11 13:19:38 +03:00
|
|
|
]
|
|
|
|
},
|
2020-09-27 22:47:26 +03:00
|
|
|
&Theme{
|
2020-09-11 13:19:38 +03:00
|
|
|
bg_color: gx.rgb(38, 38, 66)
|
|
|
|
padding_color: gx.rgb(58, 50, 74)
|
|
|
|
victory_color: gx.rgb(100, 160, 100)
|
|
|
|
game_over_color: gx.rgb(190, 50, 50)
|
|
|
|
text_color: gx.white
|
|
|
|
tile_colors: [
|
2020-09-27 22:47:26 +03:00
|
|
|
gx.rgb(92, 86, 140),
|
|
|
|
gx.rgb(106, 99, 169),
|
|
|
|
gx.rgb(106, 97, 156),
|
|
|
|
gx.rgb(108, 79, 93),
|
|
|
|
gx.rgb(110, 66, 76),
|
|
|
|
gx.rgb(110, 55, 74),
|
|
|
|
gx.rgb(110, 42, 45),
|
|
|
|
gx.rgb(106, 93, 88),
|
|
|
|
gx.rgb(106, 91, 75),
|
|
|
|
gx.rgb(106, 90, 62),
|
|
|
|
gx.rgb(106, 88, 48),
|
|
|
|
gx.rgb(106, 87, 35),
|
2020-09-11 13:19:38 +03:00
|
|
|
]
|
|
|
|
},
|
2020-08-18 09:07:06 +03:00
|
|
|
]
|
2020-09-11 13:19:38 +03:00
|
|
|
window_title = 'V 2048'
|
|
|
|
default_window_width = 544
|
|
|
|
default_window_height = 560
|
|
|
|
animation_length = 10 // frames
|
2020-09-23 09:26:09 +03:00
|
|
|
frames_per_ai_move = 8
|
2020-09-27 22:47:26 +03:00
|
|
|
possible_moves = [Direction.up, .right, .down, .left]
|
|
|
|
predictions_per_move = 200
|
|
|
|
prediction_depth = 8
|
2020-08-18 09:07:06 +03:00
|
|
|
)
|
|
|
|
|
2020-09-11 13:19:38 +03:00
|
|
|
// Used for performance monitoring when `-d showfps` is passed, unused / optimized out otherwise
|
|
|
|
struct Perf {
|
2020-08-18 09:07:06 +03:00
|
|
|
mut:
|
2020-09-11 13:19:38 +03:00
|
|
|
frame int
|
|
|
|
frame_old int
|
2021-07-20 11:17:08 +03:00
|
|
|
frame_sw time.StopWatch = time.new_stopwatch()
|
|
|
|
second_sw time.StopWatch = time.new_stopwatch()
|
2020-09-11 13:19:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Pos {
|
|
|
|
x int = -1
|
|
|
|
y int = -1
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Board {
|
|
|
|
mut:
|
|
|
|
field [4][4]int
|
|
|
|
points int
|
|
|
|
shifts int
|
|
|
|
}
|
|
|
|
|
2020-09-26 09:54:04 +03:00
|
|
|
struct Undo {
|
|
|
|
board Board
|
|
|
|
state GameState
|
|
|
|
}
|
|
|
|
|
2020-09-11 13:19:38 +03:00
|
|
|
struct TileLine {
|
2021-01-23 01:24:48 +03:00
|
|
|
ypos int
|
2020-09-11 13:19:38 +03:00
|
|
|
mut:
|
|
|
|
field [5]int
|
|
|
|
points int
|
|
|
|
shifts int
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TouchInfo {
|
|
|
|
mut:
|
2020-09-26 09:54:04 +03:00
|
|
|
start Touch
|
|
|
|
end Touch
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Touch {
|
|
|
|
mut:
|
|
|
|
pos Pos
|
|
|
|
time time.Time
|
2020-09-11 13:19:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
enum TileFormat {
|
|
|
|
normal
|
|
|
|
log
|
|
|
|
exponent
|
|
|
|
shifts
|
|
|
|
none_
|
|
|
|
end_ // To know when to wrap around
|
|
|
|
}
|
|
|
|
|
|
|
|
enum GameState {
|
|
|
|
play
|
|
|
|
over
|
|
|
|
victory
|
2020-09-26 09:54:04 +03:00
|
|
|
freeplay
|
2020-09-11 13:19:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
enum LabelKind {
|
|
|
|
points
|
|
|
|
moves
|
|
|
|
tile
|
|
|
|
victory
|
|
|
|
game_over
|
2020-09-12 09:25:44 +03:00
|
|
|
score_end
|
2020-09-11 13:19:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Direction {
|
|
|
|
up
|
|
|
|
down
|
|
|
|
left
|
|
|
|
right
|
|
|
|
}
|
|
|
|
|
2020-09-26 09:54:04 +03:00
|
|
|
// Utility functions
|
|
|
|
[inline]
|
2020-10-26 14:14:21 +03:00
|
|
|
fn avg(a int, b int) int {
|
2020-09-26 09:54:04 +03:00
|
|
|
return (a + b) / 2
|
|
|
|
}
|
|
|
|
|
2020-08-18 09:07:06 +03:00
|
|
|
fn (b Board) transpose() Board {
|
|
|
|
mut res := b
|
2020-09-27 22:47:26 +03:00
|
|
|
for y in 0 .. 4 {
|
|
|
|
for x in 0 .. 4 {
|
2020-08-18 09:07:06 +03:00
|
|
|
res.field[y][x] = b.field[x][y]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (b Board) hmirror() Board {
|
|
|
|
mut res := b
|
2020-09-27 22:47:26 +03:00
|
|
|
for y in 0 .. 4 {
|
|
|
|
for x in 0 .. 4 {
|
2020-09-11 13:19:38 +03:00
|
|
|
res.field[y][x] = b.field[y][3 - x]
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (t TileLine) to_left() TileLine {
|
2021-02-22 22:24:18 +03:00
|
|
|
right_border_idx := 4
|
2020-08-18 09:07:06 +03:00
|
|
|
mut res := t
|
|
|
|
mut zeros := 0
|
|
|
|
mut nonzeros := 0
|
|
|
|
// gather meta info about the line:
|
2020-09-11 13:19:38 +03:00
|
|
|
for x in res.field {
|
2020-09-27 22:47:26 +03:00
|
|
|
if x == 0 {
|
|
|
|
zeros++
|
|
|
|
} else {
|
|
|
|
nonzeros++
|
|
|
|
}
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
|
|
|
if nonzeros == 0 {
|
|
|
|
// when all the tiles are empty, there is nothing left to do
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
if zeros > 0 {
|
|
|
|
// we have some 0s, do shifts to compact them:
|
|
|
|
mut remaining_zeros := zeros
|
|
|
|
for x := 0; x < right_border_idx - 1; x++ {
|
|
|
|
for res.field[x] == 0 && remaining_zeros > 0 {
|
|
|
|
res.shifts++
|
|
|
|
for k := x; k < right_border_idx; k++ {
|
|
|
|
res.field[k] = res.field[k + 1]
|
|
|
|
}
|
|
|
|
remaining_zeros--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// At this point, the non 0 tiles are all on the left, with no empty spaces
|
|
|
|
// between them. we can safely merge them, when they have the same value:
|
|
|
|
for x := 0; x < right_border_idx - 1; x++ {
|
|
|
|
if res.field[x] == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if res.field[x] == res.field[x + 1] {
|
|
|
|
for k := x; k < right_border_idx; k++ {
|
|
|
|
res.field[k] = res.field[k + 1]
|
|
|
|
}
|
|
|
|
res.shifts++
|
|
|
|
res.field[x]++
|
2020-09-11 13:19:38 +03:00
|
|
|
res.points += 1 << res.field[x]
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (b Board) to_left() Board {
|
|
|
|
mut res := b
|
2020-09-27 22:47:26 +03:00
|
|
|
for y in 0 .. 4 {
|
|
|
|
mut hline := TileLine{
|
|
|
|
ypos: y
|
|
|
|
}
|
|
|
|
for x in 0 .. 4 {
|
2020-08-18 09:07:06 +03:00
|
|
|
hline.field[x] = b.field[y][x]
|
|
|
|
}
|
|
|
|
reshline := hline.to_left()
|
|
|
|
res.shifts += reshline.shifts
|
|
|
|
res.points += reshline.points
|
2020-09-27 22:47:26 +03:00
|
|
|
for x in 0 .. 4 {
|
2020-08-18 09:07:06 +03:00
|
|
|
res.field[y][x] = reshline.field[x]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-09-22 11:21:20 +03:00
|
|
|
fn (b Board) move(d Direction) (Board, bool) {
|
|
|
|
new := match d {
|
2020-09-27 22:47:26 +03:00
|
|
|
.left { b.to_left() }
|
2020-09-22 11:21:20 +03:00
|
|
|
.right { b.hmirror().to_left().hmirror() }
|
2020-09-27 22:47:26 +03:00
|
|
|
.up { b.transpose().to_left().transpose() }
|
|
|
|
.down { b.transpose().hmirror().to_left().hmirror().transpose() }
|
2020-09-22 11:21:20 +03:00
|
|
|
}
|
|
|
|
// If the board hasn't changed, it's an illegal move, don't allow it.
|
2020-09-27 22:47:26 +03:00
|
|
|
for x in 0 .. 4 {
|
|
|
|
for y in 0 .. 4 {
|
2020-09-22 11:21:20 +03:00
|
|
|
if b.field[x][y] != new.field[x][y] {
|
|
|
|
return new, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new, false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut b Board) is_game_over() bool {
|
2020-09-27 22:47:26 +03:00
|
|
|
for y in 0 .. 4 {
|
|
|
|
for x in 0 .. 4 {
|
2020-09-22 11:21:20 +03:00
|
|
|
fidx := b.field[y][x]
|
|
|
|
if fidx == 0 {
|
|
|
|
// there are remaining zeros
|
|
|
|
return false
|
|
|
|
}
|
2021-02-17 08:44:01 +03:00
|
|
|
if (x > 0 && fidx == b.field[y][x - 1])
|
|
|
|
|| (x < 4 - 1 && fidx == b.field[y][x + 1])
|
|
|
|
|| (y > 0 && fidx == b.field[y - 1][x])
|
|
|
|
|| (y < 4 - 1 && fidx == b.field[y + 1][x]) {
|
2020-09-22 11:21:20 +03:00
|
|
|
// there are remaining merges
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-08-18 09:07:06 +03:00
|
|
|
fn (mut app App) update_tickers() {
|
2020-09-27 22:47:26 +03:00
|
|
|
for y in 0 .. 4 {
|
|
|
|
for x in 0 .. 4 {
|
2020-08-18 09:07:06 +03:00
|
|
|
mut old := app.atickers[y][x]
|
|
|
|
if old > 0 {
|
|
|
|
old--
|
|
|
|
app.atickers[y][x] = old
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut app App) new_game() {
|
|
|
|
app.board = Board{}
|
2020-09-27 22:47:26 +03:00
|
|
|
for y in 0 .. 4 {
|
|
|
|
for x in 0 .. 4 {
|
2020-08-18 09:07:06 +03:00
|
|
|
app.board.field[y][x] = 0
|
|
|
|
app.atickers[y][x] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
app.state = .play
|
2020-09-27 22:47:26 +03:00
|
|
|
app.undo = []Undo{cap: 4096}
|
2020-08-18 09:07:06 +03:00
|
|
|
app.moves = 0
|
|
|
|
app.new_random_tile()
|
|
|
|
app.new_random_tile()
|
|
|
|
}
|
|
|
|
|
2020-09-26 09:54:04 +03:00
|
|
|
[inline]
|
2020-08-18 09:07:06 +03:00
|
|
|
fn (mut app App) check_for_victory() {
|
2020-09-27 22:47:26 +03:00
|
|
|
for y in 0 .. 4 {
|
|
|
|
for x in 0 .. 4 {
|
2020-08-18 09:07:06 +03:00
|
|
|
fidx := app.board.field[y][x]
|
|
|
|
if fidx == 11 {
|
2020-09-26 09:54:04 +03:00
|
|
|
app.state = .victory
|
2020-08-18 09:07:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-26 09:54:04 +03:00
|
|
|
[inline]
|
2020-08-18 09:07:06 +03:00
|
|
|
fn (mut app App) check_for_game_over() {
|
2020-09-22 11:21:20 +03:00
|
|
|
if app.board.is_game_over() {
|
2020-09-26 09:54:04 +03:00
|
|
|
app.state = .over
|
2020-09-22 11:21:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut b Board) place_random_tile() (Pos, int) {
|
|
|
|
mut etiles := [16]Pos{}
|
|
|
|
mut empty_tiles_max := 0
|
2020-09-27 22:47:26 +03:00
|
|
|
for y in 0 .. 4 {
|
|
|
|
for x in 0 .. 4 {
|
2020-09-22 11:21:20 +03:00
|
|
|
fidx := b.field[y][x]
|
2020-08-18 09:07:06 +03:00
|
|
|
if fidx == 0 {
|
2020-09-22 11:21:20 +03:00
|
|
|
etiles[empty_tiles_max] = Pos{x, y}
|
|
|
|
empty_tiles_max++
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-22 11:21:20 +03:00
|
|
|
if empty_tiles_max > 0 {
|
2022-02-23 13:36:14 +03:00
|
|
|
new_random_tile_index := rand.intn(empty_tiles_max) or { 0 }
|
2020-09-22 11:21:20 +03:00
|
|
|
empty_pos := etiles[new_random_tile_index]
|
|
|
|
// 10% chance of getting a `4` tile
|
2022-02-23 13:36:14 +03:00
|
|
|
value := rand.f64n(1.0) or { 0.0 }
|
|
|
|
random_value := if value < 0.9 { 1 } else { 2 }
|
2020-09-22 11:21:20 +03:00
|
|
|
b.field[empty_pos.y][empty_pos.x] = random_value
|
|
|
|
return empty_pos, random_value
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
2020-09-22 11:21:20 +03:00
|
|
|
return Pos{}, 0
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut app App) new_random_tile() {
|
2020-09-27 22:47:26 +03:00
|
|
|
for y in 0 .. 4 {
|
|
|
|
for x in 0 .. 4 {
|
2020-08-18 09:07:06 +03:00
|
|
|
fidx := app.board.field[y][x]
|
|
|
|
if fidx == 0 {
|
2020-09-11 13:19:38 +03:00
|
|
|
app.atickers[y][x] = 0
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-22 11:21:20 +03:00
|
|
|
empty_pos, random_value := app.board.place_random_tile()
|
|
|
|
if random_value > 0 {
|
2020-09-11 13:19:38 +03:00
|
|
|
app.atickers[empty_pos.y][empty_pos.x] = animation_length
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
2020-09-27 22:47:26 +03:00
|
|
|
if app.state != .freeplay {
|
|
|
|
app.check_for_victory()
|
|
|
|
}
|
2020-08-18 09:07:06 +03:00
|
|
|
app.check_for_game_over()
|
|
|
|
}
|
|
|
|
|
2020-09-22 11:21:20 +03:00
|
|
|
fn (mut app App) apply_new_board(new Board) {
|
2020-08-18 09:07:06 +03:00
|
|
|
old := app.board
|
2020-09-22 11:21:20 +03:00
|
|
|
app.moves++
|
|
|
|
app.board = new
|
2020-09-27 22:47:26 +03:00
|
|
|
app.undo << Undo{old, app.state}
|
2020-09-22 11:21:20 +03:00
|
|
|
app.new_random_tile()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut app App) move(d Direction) {
|
|
|
|
new, is_valid := app.board.move(d)
|
|
|
|
if !is_valid {
|
|
|
|
return
|
2020-09-11 13:19:38 +03:00
|
|
|
}
|
2020-09-22 11:21:20 +03:00
|
|
|
app.apply_new_board(new)
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Prediction {
|
|
|
|
mut:
|
2020-09-27 22:47:26 +03:00
|
|
|
move Direction
|
2020-09-22 11:21:20 +03:00
|
|
|
mpoints f64
|
2020-09-23 09:26:09 +03:00
|
|
|
mcmoves f64
|
2020-09-22 11:21:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (p Prediction) str() string {
|
2020-09-26 09:54:04 +03:00
|
|
|
return '{ move: ${p.move:5}, mpoints: ${p.mpoints:6.2f}, mcmoves: ${p.mcmoves:6.2f} }'
|
2020-09-22 11:21:20 +03:00
|
|
|
}
|
2020-09-26 09:54:04 +03:00
|
|
|
|
2020-09-22 11:21:20 +03:00
|
|
|
fn (mut app App) ai_move() {
|
|
|
|
mut predictions := [4]Prediction{}
|
|
|
|
mut is_valid := false
|
2021-07-20 11:17:08 +03:00
|
|
|
think_watch := time.new_stopwatch()
|
2020-09-22 11:21:20 +03:00
|
|
|
for move in possible_moves {
|
|
|
|
move_idx := int(move)
|
|
|
|
predictions[move_idx].move = move
|
|
|
|
mut mpoints := 0
|
2020-09-23 09:26:09 +03:00
|
|
|
mut mcmoves := 0
|
2020-09-27 22:47:26 +03:00
|
|
|
for _ in 0 .. predictions_per_move {
|
2020-09-22 11:21:20 +03:00
|
|
|
mut cboard := app.board
|
|
|
|
cboard, is_valid = cboard.move(move)
|
2020-09-27 22:47:26 +03:00
|
|
|
if !is_valid || cboard.is_game_over() {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-22 11:21:20 +03:00
|
|
|
mpoints += cboard.points
|
|
|
|
cboard.place_random_tile()
|
|
|
|
mut cmoves := 0
|
|
|
|
for !cboard.is_game_over() {
|
2022-02-23 13:36:14 +03:00
|
|
|
nmove := possible_moves[rand.intn(possible_moves.len) or { 0 }]
|
2020-09-26 09:54:04 +03:00
|
|
|
cboard, is_valid = cboard.move(nmove)
|
2020-09-27 22:47:26 +03:00
|
|
|
if !is_valid {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-22 11:21:20 +03:00
|
|
|
cboard.place_random_tile()
|
|
|
|
cmoves++
|
2020-09-27 22:47:26 +03:00
|
|
|
if cmoves > prediction_depth {
|
|
|
|
break
|
|
|
|
}
|
2020-09-12 09:25:44 +03:00
|
|
|
}
|
2020-09-22 11:21:20 +03:00
|
|
|
mpoints += cboard.points
|
2020-09-23 09:26:09 +03:00
|
|
|
mcmoves += cmoves
|
2020-09-12 09:25:44 +03:00
|
|
|
}
|
2020-09-26 09:54:04 +03:00
|
|
|
predictions[move_idx].mpoints = f64(mpoints) / predictions_per_move
|
|
|
|
predictions[move_idx].mcmoves = f64(mcmoves) / predictions_per_move
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
2020-09-26 09:54:04 +03:00
|
|
|
think_time := think_watch.elapsed().milliseconds()
|
2020-09-27 22:47:26 +03:00
|
|
|
mut bestprediction := Prediction{
|
|
|
|
mpoints: -1
|
|
|
|
}
|
|
|
|
for move_idx in 0 .. possible_moves.len {
|
2020-09-22 11:21:20 +03:00
|
|
|
if bestprediction.mpoints < predictions[move_idx].mpoints {
|
|
|
|
bestprediction = predictions[move_idx]
|
|
|
|
}
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('Simulation time: ${think_time:4}ms | best ${bestprediction}')
|
2020-09-26 09:54:04 +03:00
|
|
|
app.move(bestprediction.move)
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
|
|
|
|
2020-09-11 13:19:38 +03:00
|
|
|
fn (app &App) label_format(kind LabelKind) gx.TextCfg {
|
|
|
|
match kind {
|
2021-02-17 08:44:01 +03:00
|
|
|
.points {
|
2021-07-20 11:17:08 +03:00
|
|
|
return gx.TextCfg{
|
2021-02-17 08:44:01 +03:00
|
|
|
color: if app.state in [.over, .victory] { gx.white } else { app.theme.text_color }
|
2020-09-11 13:19:38 +03:00
|
|
|
align: .left
|
|
|
|
size: app.ui.font_size / 2
|
2021-02-17 08:44:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.moves {
|
2021-07-20 11:17:08 +03:00
|
|
|
return gx.TextCfg{
|
2021-02-17 08:44:01 +03:00
|
|
|
color: if app.state in [.over, .victory] { gx.white } else { app.theme.text_color }
|
2020-09-11 13:19:38 +03:00
|
|
|
align: .right
|
|
|
|
size: app.ui.font_size / 2
|
2021-02-17 08:44:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.tile {
|
2021-07-20 11:17:08 +03:00
|
|
|
return gx.TextCfg{
|
2020-09-11 13:19:38 +03:00
|
|
|
color: app.theme.text_color
|
|
|
|
align: .center
|
|
|
|
vertical_align: .middle
|
|
|
|
size: app.ui.font_size
|
2021-02-17 08:44:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.victory {
|
2021-07-20 11:17:08 +03:00
|
|
|
return gx.TextCfg{
|
2020-09-11 13:19:38 +03:00
|
|
|
color: app.theme.victory_color
|
|
|
|
align: .center
|
|
|
|
vertical_align: .middle
|
|
|
|
size: app.ui.font_size * 2
|
2021-02-17 08:44:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.game_over {
|
2021-07-20 11:17:08 +03:00
|
|
|
return gx.TextCfg{
|
2020-09-11 13:19:38 +03:00
|
|
|
color: app.theme.game_over_color
|
|
|
|
align: .center
|
|
|
|
vertical_align: .middle
|
|
|
|
size: app.ui.font_size * 2
|
2021-02-17 08:44:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.score_end {
|
2021-07-20 11:17:08 +03:00
|
|
|
return gx.TextCfg{
|
2020-09-26 09:54:04 +03:00
|
|
|
color: gx.white
|
2020-09-12 09:25:44 +03:00
|
|
|
align: .center
|
|
|
|
vertical_align: .middle
|
2020-09-26 09:54:04 +03:00
|
|
|
size: app.ui.font_size * 3 / 4
|
2021-02-17 08:44:01 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-11 13:19:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-26 09:54:04 +03:00
|
|
|
[inline]
|
2020-09-11 13:19:38 +03:00
|
|
|
fn (mut app App) set_theme(idx int) {
|
|
|
|
theme := themes[idx]
|
|
|
|
app.theme_idx = idx
|
|
|
|
app.theme = theme
|
|
|
|
app.gg.set_bg_color(theme.bg_color)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut app App) resize() {
|
2022-05-15 09:14:18 +03:00
|
|
|
mut s := app.gg.scale
|
2020-09-27 22:47:26 +03:00
|
|
|
if s == 0.0 {
|
|
|
|
s = 1.0
|
|
|
|
}
|
2022-05-15 09:14:18 +03:00
|
|
|
window_size := app.gg.window_size()
|
2021-02-17 18:18:29 +03:00
|
|
|
w := window_size.width
|
|
|
|
h := window_size.height
|
2022-01-05 19:02:20 +03:00
|
|
|
m := f32(math.min(w, h))
|
2020-09-26 09:54:04 +03:00
|
|
|
app.ui.dpi_scale = s
|
2020-09-11 13:19:38 +03:00
|
|
|
app.ui.window_width = w
|
|
|
|
app.ui.window_height = h
|
|
|
|
app.ui.padding_size = int(m / 38)
|
|
|
|
app.ui.header_size = app.ui.padding_size
|
|
|
|
app.ui.border_size = app.ui.padding_size * 2
|
|
|
|
app.ui.tile_size = int((m - app.ui.padding_size * 5 - app.ui.border_size * 2) / 4)
|
|
|
|
app.ui.font_size = int(m / 10)
|
|
|
|
// If the window's height is greater than its width, center the board vertically.
|
|
|
|
// If not, center it horizontally
|
|
|
|
if w > h {
|
|
|
|
app.ui.y_padding = 0
|
|
|
|
app.ui.x_padding = (app.ui.window_width - app.ui.window_height) / 2
|
|
|
|
} else {
|
|
|
|
app.ui.y_padding = (app.ui.window_height - app.ui.window_width - app.ui.header_size) / 2
|
|
|
|
app.ui.x_padding = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (app &App) draw() {
|
2020-09-26 09:54:04 +03:00
|
|
|
xpad, ypad := app.ui.x_padding, app.ui.y_padding
|
2020-09-11 13:19:38 +03:00
|
|
|
ww := app.ui.window_width
|
|
|
|
wh := app.ui.window_height
|
2022-01-05 19:02:20 +03:00
|
|
|
m := math.min(ww, wh)
|
2020-09-26 09:54:04 +03:00
|
|
|
labelx := xpad + app.ui.border_size
|
|
|
|
labely := ypad + app.ui.border_size / 2
|
2020-09-11 13:19:38 +03:00
|
|
|
app.draw_tiles()
|
|
|
|
// TODO: Make transparency work in `gg`
|
|
|
|
if app.state == .over {
|
2021-12-23 14:31:25 +03:00
|
|
|
app.gg.draw_rect_filled(0, 0, ww, wh, gx.rgba(10, 0, 0, 180))
|
2020-09-26 09:54:04 +03:00
|
|
|
app.gg.draw_text(ww / 2, (m * 4 / 10) + ypad, 'Game Over', app.label_format(.game_over))
|
|
|
|
f := app.label_format(.tile)
|
|
|
|
msg := $if android { 'Tap to restart' } $else { 'Press `r` to restart' }
|
2021-01-23 01:24:48 +03:00
|
|
|
app.gg.draw_text(ww / 2, (m * 6 / 10) + ypad, msg, gx.TextCfg{
|
|
|
|
...f
|
2020-09-27 22:47:26 +03:00
|
|
|
color: gx.white
|
|
|
|
size: f.size * 3 / 4
|
|
|
|
})
|
2020-09-11 13:19:38 +03:00
|
|
|
}
|
|
|
|
if app.state == .victory {
|
2021-12-23 14:31:25 +03:00
|
|
|
app.gg.draw_rect_filled(0, 0, ww, wh, gx.rgba(0, 10, 0, 180))
|
2020-09-26 09:54:04 +03:00
|
|
|
app.gg.draw_text(ww / 2, (m * 4 / 10) + ypad, 'Victory!', app.label_format(.victory))
|
|
|
|
// f := app.label_format(.tile)
|
|
|
|
msg1 := $if android { 'Tap to continue' } $else { 'Press `space` to continue' }
|
|
|
|
msg2 := $if android { 'Tap to restart' } $else { 'Press `r` to restart' }
|
|
|
|
app.gg.draw_text(ww / 2, (m * 6 / 10) + ypad, msg1, app.label_format(.score_end))
|
|
|
|
app.gg.draw_text(ww / 2, (m * 8 / 10) + ypad, msg2, app.label_format(.score_end))
|
2020-09-11 13:19:38 +03:00
|
|
|
}
|
2020-09-26 09:54:04 +03:00
|
|
|
// Draw at the end, so that it's on top of the victory / game over overlays
|
2022-11-15 16:53:13 +03:00
|
|
|
app.gg.draw_text(labelx, labely, 'Points: ${app.board.points}', app.label_format(.points))
|
|
|
|
app.gg.draw_text(ww - labelx, labely, 'Moves: ${app.moves}', app.label_format(.moves))
|
2020-09-11 13:19:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (app &App) draw_tiles() {
|
|
|
|
xstart := app.ui.x_padding + app.ui.border_size
|
|
|
|
ystart := app.ui.y_padding + app.ui.border_size + app.ui.header_size
|
|
|
|
toffset := app.ui.tile_size + app.ui.padding_size
|
2022-01-05 19:02:20 +03:00
|
|
|
tiles_size := math.min(app.ui.window_width, app.ui.window_height) - app.ui.border_size * 2
|
2020-09-11 13:19:38 +03:00
|
|
|
// Draw the padding around the tiles
|
2021-12-23 14:31:25 +03:00
|
|
|
app.gg.draw_rounded_rect_filled(xstart, ystart, tiles_size, tiles_size, tiles_size / 24,
|
2021-01-23 01:24:48 +03:00
|
|
|
app.theme.padding_color)
|
2020-09-11 13:19:38 +03:00
|
|
|
// Draw the actual tiles
|
2020-09-27 22:47:26 +03:00
|
|
|
for y in 0 .. 4 {
|
|
|
|
for x in 0 .. 4 {
|
2020-09-11 13:19:38 +03:00
|
|
|
tidx := app.board.field[y][x]
|
|
|
|
tile_color := if tidx < app.theme.tile_colors.len {
|
|
|
|
app.theme.tile_colors[tidx]
|
|
|
|
} else {
|
|
|
|
// If there isn't a specific color for this tile, reuse the last color available
|
|
|
|
app.theme.tile_colors.last()
|
|
|
|
}
|
|
|
|
anim_size := animation_length - app.atickers[y][x]
|
|
|
|
tw := int(f64(app.ui.tile_size) / animation_length * anim_size)
|
|
|
|
th := tw // square tiles, w == h
|
|
|
|
xoffset := xstart + app.ui.padding_size + x * toffset + (app.ui.tile_size - tw) / 2
|
|
|
|
yoffset := ystart + app.ui.padding_size + y * toffset + (app.ui.tile_size - th) / 2
|
2021-12-23 14:31:25 +03:00
|
|
|
app.gg.draw_rounded_rect_filled(xoffset, yoffset, tw, th, tw / 8, tile_color)
|
2020-09-11 13:19:38 +03:00
|
|
|
if tidx != 0 { // 0 == blank spot
|
|
|
|
xpos := xoffset + tw / 2
|
|
|
|
ypos := yoffset + th / 2
|
|
|
|
mut fmt := app.label_format(.tile)
|
2021-01-23 01:24:48 +03:00
|
|
|
fmt = gx.TextCfg{
|
|
|
|
...fmt
|
2020-09-27 22:47:26 +03:00
|
|
|
size: int(f32(fmt.size - 1) / animation_length * anim_size)
|
|
|
|
}
|
2020-09-11 13:19:38 +03:00
|
|
|
match app.tile_format {
|
|
|
|
.normal {
|
2020-10-26 14:14:21 +03:00
|
|
|
app.gg.draw_text(xpos, ypos, '${1 << tidx}', fmt)
|
2020-09-27 22:47:26 +03:00
|
|
|
}
|
|
|
|
.log {
|
2022-11-15 16:53:13 +03:00
|
|
|
app.gg.draw_text(xpos, ypos, '${tidx}', fmt)
|
2020-09-27 22:47:26 +03:00
|
|
|
}
|
|
|
|
.exponent {
|
2020-09-11 13:19:38 +03:00
|
|
|
app.gg.draw_text(xpos, ypos, '2', fmt)
|
|
|
|
fs2 := int(f32(fmt.size) * 0.67)
|
|
|
|
app.gg.draw_text(xpos + app.ui.tile_size / 10, ypos - app.ui.tile_size / 8,
|
2022-11-15 16:53:13 +03:00
|
|
|
'${tidx}', gx.TextCfg{
|
2021-01-23 01:24:48 +03:00
|
|
|
...fmt
|
2020-09-27 22:47:26 +03:00
|
|
|
size: fs2
|
|
|
|
align: gx.HorizontalAlign.left
|
|
|
|
})
|
|
|
|
}
|
|
|
|
.shifts {
|
2020-09-11 13:19:38 +03:00
|
|
|
fs2 := int(f32(fmt.size) * 0.6)
|
2021-01-23 01:24:48 +03:00
|
|
|
app.gg.draw_text(xpos, ypos, '2<<${tidx - 1}', gx.TextCfg{
|
|
|
|
...fmt
|
2020-09-27 22:47:26 +03:00
|
|
|
size: fs2
|
|
|
|
})
|
|
|
|
}
|
|
|
|
.none_ {} // Don't draw any text here, colors only
|
2020-09-11 13:19:38 +03:00
|
|
|
.end_ {} // Should never get here
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-26 09:54:04 +03:00
|
|
|
fn (mut app App) handle_touches() {
|
|
|
|
s, e := app.touch.start, app.touch.end
|
2022-01-05 19:02:20 +03:00
|
|
|
adx, ady := math.abs(e.pos.x - s.pos.x), math.abs(e.pos.y - s.pos.y)
|
|
|
|
if math.max(adx, ady) < 10 {
|
2020-09-26 09:54:04 +03:00
|
|
|
app.handle_tap()
|
|
|
|
} else {
|
|
|
|
app.handle_swipe()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut app App) handle_tap() {
|
|
|
|
_, ypad := app.ui.x_padding, app.ui.y_padding
|
|
|
|
w, h := app.ui.window_width, app.ui.window_height
|
2022-01-05 19:02:20 +03:00
|
|
|
m := math.min(w, h)
|
2020-09-26 09:54:04 +03:00
|
|
|
s, e := app.touch.start, app.touch.end
|
|
|
|
avgx, avgy := avg(s.pos.x, e.pos.x), avg(s.pos.y, e.pos.y)
|
|
|
|
// TODO: Replace "touch spots" with actual buttons
|
|
|
|
// bottom left -> change theme
|
2021-02-22 22:24:18 +03:00
|
|
|
if avgx < 50 && h - avgy < 50 {
|
2020-09-27 22:47:26 +03:00
|
|
|
app.next_theme()
|
|
|
|
}
|
2020-09-26 09:54:04 +03:00
|
|
|
// bottom right -> change tile format
|
2021-02-22 22:24:18 +03:00
|
|
|
if w - avgx < 50 && h - avgy < 50 {
|
2020-09-27 22:47:26 +03:00
|
|
|
app.next_tile_format()
|
|
|
|
}
|
2020-09-26 09:54:04 +03:00
|
|
|
if app.state == .victory {
|
|
|
|
if avgy > (m / 2) + ypad {
|
|
|
|
if avgy < (m * 7 / 10) + ypad {
|
|
|
|
app.state = .freeplay
|
|
|
|
} else if avgy < (m * 9 / 10) + ypad {
|
|
|
|
app.new_game()
|
|
|
|
} else {
|
|
|
|
// TODO remove and implement an actual way to toggle themes on mobile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if app.state == .over {
|
|
|
|
if avgy > (m / 2) + ypad && avgy < (m * 7 / 10) + ypad {
|
|
|
|
app.new_game()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut app App) handle_swipe() {
|
2020-09-27 22:47:26 +03:00
|
|
|
// Currently, swipes are only used to move the tiles.
|
2020-09-26 09:54:04 +03:00
|
|
|
// If the user's not playing, exit early to avoid all the unnecessary calculations
|
2020-09-27 22:47:26 +03:00
|
|
|
if app.state !in [.play, .freeplay] {
|
|
|
|
return
|
|
|
|
}
|
2020-09-26 09:54:04 +03:00
|
|
|
s, e := app.touch.start, app.touch.end
|
|
|
|
w, h := app.ui.window_width, app.ui.window_height
|
|
|
|
dx, dy := e.pos.x - s.pos.x, e.pos.y - s.pos.y
|
2022-01-05 19:02:20 +03:00
|
|
|
adx, ady := math.abs(dx), math.abs(dy)
|
|
|
|
dmin := if math.min(adx, ady) > 0 { math.min(adx, ady) } else { 1 }
|
|
|
|
dmax := if math.max(adx, ady) > 0 { math.max(adx, ady) } else { 1 }
|
2020-09-26 09:54:04 +03:00
|
|
|
tdiff := int(e.time.unix_time_milli() - s.time.unix_time_milli())
|
|
|
|
// TODO: make this calculation more accurate (don't use arbitrary numbers)
|
2022-01-05 19:02:20 +03:00
|
|
|
min_swipe_distance := int(math.sqrt(math.min(w, h) * tdiff / 100)) + 20
|
2020-09-27 22:47:26 +03:00
|
|
|
if dmax < min_swipe_distance {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Swipe was too short
|
|
|
|
if dmax / dmin < 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Swiped diagonally
|
2020-09-11 13:19:38 +03:00
|
|
|
if adx > ady {
|
2020-09-27 22:47:26 +03:00
|
|
|
if dx < 0 {
|
|
|
|
app.move(.left)
|
|
|
|
} else {
|
|
|
|
app.move(.right)
|
|
|
|
}
|
2020-09-11 13:19:38 +03:00
|
|
|
} else {
|
2020-09-27 22:47:26 +03:00
|
|
|
if dy < 0 {
|
|
|
|
app.move(.up)
|
|
|
|
} else {
|
|
|
|
app.move(.down)
|
|
|
|
}
|
2020-09-11 13:19:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-26 09:54:04 +03:00
|
|
|
[inline]
|
|
|
|
fn (mut app App) next_theme() {
|
2021-02-17 08:44:01 +03:00
|
|
|
app.set_theme(if app.theme_idx == themes.len - 1 { 0 } else { app.theme_idx + 1 })
|
2020-09-26 09:54:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
fn (mut app App) next_tile_format() {
|
2022-10-02 22:39:11 +03:00
|
|
|
app.tile_format = unsafe { TileFormat(int(app.tile_format) + 1) }
|
2020-09-26 09:54:04 +03:00
|
|
|
if app.tile_format == .end_ {
|
|
|
|
app.tile_format = .normal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
fn (mut app App) undo() {
|
|
|
|
if app.undo.len > 0 {
|
|
|
|
undo := app.undo.pop()
|
|
|
|
app.board = undo.board
|
|
|
|
app.state = undo.state
|
|
|
|
app.moves--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 08:44:01 +03:00
|
|
|
fn (mut app App) on_key_down(key gg.KeyCode) {
|
2020-08-18 09:07:06 +03:00
|
|
|
// these keys are independent from the game state:
|
|
|
|
match key {
|
2022-06-23 21:07:30 +03:00
|
|
|
.c { app.is_ai_mode = !app.is_ai_mode }
|
2021-11-11 18:45:22 +03:00
|
|
|
.escape { app.gg.quit() }
|
2020-09-27 22:47:26 +03:00
|
|
|
.n, .r { app.new_game() }
|
|
|
|
.backspace { app.undo() }
|
|
|
|
.enter { app.next_tile_format() }
|
|
|
|
.j { app.state = .over }
|
|
|
|
.t { app.next_theme() }
|
|
|
|
else {}
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
2020-09-26 09:54:04 +03:00
|
|
|
if app.state in [.play, .freeplay] {
|
2022-06-23 21:07:30 +03:00
|
|
|
if !app.is_ai_mode {
|
|
|
|
match key {
|
|
|
|
.w, .up { app.move(.up) }
|
|
|
|
.a, .left { app.move(.left) }
|
|
|
|
.s, .down { app.move(.down) }
|
|
|
|
.d, .right { app.move(.right) }
|
|
|
|
else {}
|
|
|
|
}
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-26 09:54:04 +03:00
|
|
|
if app.state == .victory {
|
2020-09-27 22:47:26 +03:00
|
|
|
if key == .space {
|
|
|
|
app.state = .freeplay
|
|
|
|
}
|
2020-09-26 09:54:04 +03:00
|
|
|
}
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
|
|
|
|
2021-02-17 08:44:01 +03:00
|
|
|
fn on_event(e &gg.Event, mut app App) {
|
2020-09-11 13:19:38 +03:00
|
|
|
match e.typ {
|
|
|
|
.key_down {
|
|
|
|
app.on_key_down(e.key_code)
|
2020-09-27 22:47:26 +03:00
|
|
|
}
|
|
|
|
.resized, .restored, .resumed {
|
2020-09-11 13:19:38 +03:00
|
|
|
app.resize()
|
2020-09-27 22:47:26 +03:00
|
|
|
}
|
|
|
|
.touches_began {
|
2020-09-11 13:19:38 +03:00
|
|
|
if e.num_touches > 0 {
|
|
|
|
t := e.touches[0]
|
2021-07-20 11:17:08 +03:00
|
|
|
app.touch.start = Touch{
|
|
|
|
pos: Pos{
|
2020-09-27 22:47:26 +03:00
|
|
|
x: int(t.pos_x / app.ui.dpi_scale)
|
2020-09-26 09:54:04 +03:00
|
|
|
y: int(t.pos_y / app.ui.dpi_scale)
|
2020-09-27 22:47:26 +03:00
|
|
|
}
|
2020-09-26 09:54:04 +03:00
|
|
|
time: time.now()
|
|
|
|
}
|
2020-09-11 13:19:38 +03:00
|
|
|
}
|
2020-09-27 22:47:26 +03:00
|
|
|
}
|
|
|
|
.touches_ended {
|
2020-09-11 13:19:38 +03:00
|
|
|
if e.num_touches > 0 {
|
|
|
|
t := e.touches[0]
|
2021-07-20 11:17:08 +03:00
|
|
|
app.touch.end = Touch{
|
|
|
|
pos: Pos{
|
2020-09-27 22:47:26 +03:00
|
|
|
x: int(t.pos_x / app.ui.dpi_scale)
|
2020-09-26 09:54:04 +03:00
|
|
|
y: int(t.pos_y / app.ui.dpi_scale)
|
2020-09-27 22:47:26 +03:00
|
|
|
}
|
2020-09-26 09:54:04 +03:00
|
|
|
time: time.now()
|
|
|
|
}
|
|
|
|
app.handle_touches()
|
|
|
|
}
|
2020-09-27 22:47:26 +03:00
|
|
|
}
|
|
|
|
.mouse_down {
|
2021-07-20 11:17:08 +03:00
|
|
|
app.touch.start = Touch{
|
|
|
|
pos: Pos{
|
2020-09-27 22:47:26 +03:00
|
|
|
x: int(e.mouse_x / app.ui.dpi_scale)
|
2020-09-26 09:54:04 +03:00
|
|
|
y: int(e.mouse_y / app.ui.dpi_scale)
|
2020-09-27 22:47:26 +03:00
|
|
|
}
|
2020-09-26 09:54:04 +03:00
|
|
|
time: time.now()
|
2020-09-11 13:19:38 +03:00
|
|
|
}
|
2020-09-27 22:47:26 +03:00
|
|
|
}
|
|
|
|
.mouse_up {
|
2021-07-20 11:17:08 +03:00
|
|
|
app.touch.end = Touch{
|
|
|
|
pos: Pos{
|
2020-09-27 22:47:26 +03:00
|
|
|
x: int(e.mouse_x / app.ui.dpi_scale)
|
2020-09-26 09:54:04 +03:00
|
|
|
y: int(e.mouse_y / app.ui.dpi_scale)
|
2020-09-27 22:47:26 +03:00
|
|
|
}
|
2020-09-26 09:54:04 +03:00
|
|
|
time: time.now()
|
|
|
|
}
|
|
|
|
app.handle_touches()
|
2020-09-27 22:47:26 +03:00
|
|
|
}
|
|
|
|
else {}
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn frame(mut app App) {
|
2020-09-27 22:47:26 +03:00
|
|
|
$if showfps ? {
|
|
|
|
app.perf.frame_sw.restart()
|
|
|
|
}
|
2020-08-18 09:07:06 +03:00
|
|
|
app.gg.begin()
|
2020-09-11 13:19:38 +03:00
|
|
|
app.update_tickers()
|
2020-08-18 09:07:06 +03:00
|
|
|
app.draw()
|
2020-09-22 11:21:20 +03:00
|
|
|
app.perf.frame++
|
2020-09-26 09:54:04 +03:00
|
|
|
if app.is_ai_mode && app.state in [.play, .freeplay] && app.perf.frame % frames_per_ai_move == 0 {
|
2020-09-22 11:21:20 +03:00
|
|
|
app.ai_move()
|
|
|
|
}
|
2020-09-27 22:47:26 +03:00
|
|
|
$if showfps ? {
|
|
|
|
app.showfps()
|
|
|
|
}
|
2020-09-22 11:21:20 +03:00
|
|
|
app.gg.end()
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
|
|
|
|
2020-09-11 13:19:38 +03:00
|
|
|
fn init(mut app App) {
|
|
|
|
app.resize()
|
2020-09-27 22:47:26 +03:00
|
|
|
$if showfps ? {
|
2020-09-11 13:19:38 +03:00
|
|
|
app.perf.frame_sw.restart()
|
|
|
|
app.perf.second_sw.restart()
|
2020-08-18 09:07:06 +03:00
|
|
|
}
|
2020-09-11 13:19:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut app App) showfps() {
|
|
|
|
println(app.perf.frame_sw.elapsed().microseconds())
|
|
|
|
f := app.perf.frame
|
|
|
|
if (f & 127) == 0 {
|
|
|
|
last_frame_us := app.perf.frame_sw.elapsed().microseconds()
|
|
|
|
ticks := f64(app.perf.second_sw.elapsed().milliseconds())
|
|
|
|
fps := f64(app.perf.frame - app.perf.frame_old) * ticks / 1000 / 4.5
|
2020-09-27 22:47:26 +03:00
|
|
|
last_fps := 128000.0 / ticks
|
2020-09-11 13:19:38 +03:00
|
|
|
eprintln('frame ${f:-5} | avg. fps: ${fps:-5.1f} | avg. last 128 fps: ${last_fps:-5.1f} | last frame time: ${last_frame_us:-4}µs')
|
|
|
|
app.perf.second_sw.restart()
|
|
|
|
app.perf.frame_old = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 00:17:13 +03:00
|
|
|
$if emscripten ? {
|
|
|
|
#flag --embed-file ./examples/assets/fonts/RobotoMono-Regular.ttf@/assets/fonts/RobotoMono-Regular.ttf
|
|
|
|
}
|
|
|
|
|
2020-09-11 13:19:38 +03:00
|
|
|
fn main() {
|
|
|
|
mut app := &App{}
|
2020-08-18 09:07:06 +03:00
|
|
|
app.new_game()
|
2021-12-06 10:53:46 +03:00
|
|
|
mut font_path := os.resource_abs_path(os.join_path('..', 'assets', 'fonts', 'RobotoMono-Regular.ttf'))
|
2020-09-11 13:19:38 +03:00
|
|
|
$if android {
|
2020-09-26 09:54:04 +03:00
|
|
|
font_path = 'fonts/RobotoMono-Regular.ttf'
|
2020-09-11 13:19:38 +03:00
|
|
|
}
|
2021-01-10 23:41:29 +03:00
|
|
|
mut window_title_ := 'V 2048'
|
2020-09-11 13:19:38 +03:00
|
|
|
// TODO: Make emcc a real platform ifdef
|
2020-09-27 22:47:26 +03:00
|
|
|
$if emscripten ? {
|
2020-09-11 13:19:38 +03:00
|
|
|
// in emscripten, sokol uses `window_title` as the selector to the canvas it'll render to,
|
|
|
|
// and since `document.querySelector('V 2048')` isn't valid JS, we use `canvas` instead
|
2021-01-10 23:41:29 +03:00
|
|
|
window_title_ = 'canvas'
|
2020-09-11 13:19:38 +03:00
|
|
|
}
|
2020-09-22 11:21:20 +03:00
|
|
|
app.perf = &Perf{}
|
2021-01-23 01:24:48 +03:00
|
|
|
app.gg = gg.new_context(
|
2020-09-11 13:19:38 +03:00
|
|
|
bg_color: app.theme.bg_color
|
|
|
|
width: default_window_width
|
|
|
|
height: default_window_height
|
2021-02-05 20:35:51 +03:00
|
|
|
sample_count: 4 // higher quality curves
|
2020-08-18 09:07:06 +03:00
|
|
|
create_window: true
|
2021-01-10 23:41:29 +03:00
|
|
|
window_title: window_title_
|
2020-08-18 09:07:06 +03:00
|
|
|
frame_fn: frame
|
|
|
|
event_fn: on_event
|
2020-09-11 13:19:38 +03:00
|
|
|
init_fn: init
|
2020-08-18 09:07:06 +03:00
|
|
|
user_data: app
|
2020-09-11 13:19:38 +03:00
|
|
|
font_path: font_path
|
2021-01-23 01:24:48 +03:00
|
|
|
)
|
2020-08-18 09:07:06 +03:00
|
|
|
app.gg.run()
|
2020-09-12 09:25:44 +03:00
|
|
|
}
|