diff options
author | Joop Kiefte <ikojba@gmail.com> | 2020-09-12 16:03:18 +0200 |
---|---|---|
committer | Joop Kiefte <ikojba@gmail.com> | 2020-09-12 16:03:18 +0200 |
commit | bd80439d9620f02b124b274cd79b8c8d30b69016 (patch) | |
tree | 4e77dbb39ce9d046f5dcc9c323c96ed834773c18 | |
parent | 8324a82224026574bbfe866cc16a1adae5c1aec2 (diff) |
Implement kick system (badly, because I think it actually doesn't work yet...)
-rw-r--r-- | tris/move.go | 54 | ||||
-rw-r--r-- | tris/srs.go | 32 |
2 files changed, 54 insertions, 32 deletions
diff --git a/tris/move.go b/tris/move.go index 26dd568..127ce06 100644 --- a/tris/move.go +++ b/tris/move.go @@ -1,11 +1,57 @@ package tris +// As at the bottom of https://tetris.fandom.com/wiki/SRS but Y inverse because we start Y at the top +type Kicks map[Rotation][]Point + +var K3CW = Kicks{ + 0: {{X:0, Y:0}, {X:-1, Y:0}, {X:-1, Y: -1}, {X:0, Y:2}, {X:-1, Y:2}}, + 1: {{X:0, Y:0}, {X:1, Y:0}, {X:1, Y: 1}, {X:0, Y:-2}, {X:1, Y:-2}}, + 2: {{X:0, Y:0}, {X:1, Y:0}, {X:1, Y: -1}, {X:0, Y:2}, {X:1, Y:2}}, + 3: {{X:0, Y:0}, {X:-1, Y:0}, {X:-1, Y: 1}, {X:0, Y:-2}, {X:-1, Y:-2}}, +} + +var LCW = Kicks{ + 0: {{X:0, Y:0}, {X:-2, Y:0}, {X:1, Y: 0}, {X:-2, Y:1}, {X:1, Y:-2}}, + 1: {{X:0, Y:0}, {X:-1, Y:0}, {X:2, Y: 0}, {X:-1, Y:-2}, {X:2, Y:1}}, + 2: {{X:0, Y:0}, {X:2, Y:0}, {X:-1, Y: 0}, {X:2, Y:-1}, {X:-1, Y:2}}, + 3: {{X:0, Y:0}, {X:1, Y:0}, {X:-2, Y: 0}, {X:1, Y:2}, {X:-2, Y:-1}}, +} + +func (ks Kicks) Try(rot Rotation, reverse bool, p Placement, f Field) (np Placement) { + orot := (rot + 3)%4 + if reverse { // for going backwards, you start from one more + rot-- + orot = (rot + 1)%4 + } + for _, k := range ks[rot] { + np = p + x, y := k.X, k.Y + if reverse { + x, y = -x, -y + } + np.X, np.Y = p.X+x, p.Y+y + if !np.Collide(f) { + return np + } + } + p.Rot = orot + return p +} + func (p Placement) Floor(f Field) bool { fp := p fp.Y++ return fp.Collide(f) } +// CW checks if the rotation from r to nr is a Clockwise rotation +func (r Rotation) CW(nr Rotation) bool { + r += 4 + return (r - nr)%4 == 1 // if nr is 1 modulo 4 higher than r, it is a CW rotation +} + +// Move tries the move indicated by the parameters, applies kicks to fix when it doesn't work, +// and returns if the piece is on the floor or topping out func (p Placement) Move(f Field, rot Rotation, x, y int) (np Placement, floor, topout bool) { np = p np.Rot = rot @@ -14,6 +60,14 @@ func (p Placement) Move(f Field, rot Rotation, x, y int) (np Placement, floor, t if !np.Collide(f) { // free air return np, np.Floor(f), false } + // start handling kicks + switch p.piece { + case IPiece: + np = LCW.Try(rot, np.Rot.CW(rot), np, f) + case JPiece, SPiece, ZPiece, TPiece, LPiece: + np = K3CW.Try(rot, np.Rot.CW(rot), np, f) + } + np = p // last resort reset p if np.Floor(f) && np.Y < 0 { return np, true, true diff --git a/tris/srs.go b/tris/srs.go deleted file mode 100644 index d6c8220..0000000 --- a/tris/srs.go +++ /dev/null @@ -1,32 +0,0 @@ -package tris - -// As at the bottom of https://tetris.fandom.com/wiki/SRS but Y inverse because we start Y at the top -type Kicks map[Rotation][]Point - -var K3CW = Kicks{ - 0: {{X:0, Y:0}, {X:-1, Y:0}, {X:-1, Y: -1}, {X:0, Y:2}, {X:-1, Y:2}}, - 1: {{X:0, Y:0}, {X:1, Y:0}, {X:1, Y: 1}, {X:0, Y:-2}, {X:1, Y:-2}}, - 2: {{X:0, Y:0}, {X:1, Y:0}, {X:1, Y: -1}, {X:0, Y:2}, {X:1, Y:2}}, - 3: {{X:0, Y:0}, {X:-1, Y:0}, {X:-1, Y: 1}, {X:0, Y:-2}, {X:-1, Y:-2}}, -} - -var K3CCW = Kicks{ - 1: {{X:0, Y:0}, {X:1, Y:0}, {X:1, Y: 1}, {X:0, Y:-2}, {X:1, Y:-2}}, - 2: {{X:0, Y:0}, {X:-1, Y:0}, {X:-1, Y:-1}, {X:0, Y:2}, {X:-1, Y:2}}, - 3: {{X:0, Y:0}, {X:-1, Y:0}, {X:-1, Y: 1}, {X:0, Y:-2}, {X:-1, Y:-2}}, - 0: {{X:0, Y:0}, {X:1, Y:0}, {X:1, Y: -1}, {X:0, Y:2}, {X:1, Y:2}}, -} - -var LCW = Kicks{ - 0: {{X:0, Y:0}, {X:-2, Y:0}, {X:1, Y: 0}, {X:-2, Y:1}, {X:1, Y:-2}}, - 1: {{X:0, Y:0}, {X:-1, Y:0}, {X:2, Y: 0}, {X:-1, Y:-2}, {X:2, Y:1}}, - 2: {{X:0, Y:0}, {X:2, Y:0}, {X:-1, Y: 0}, {X:2, Y:-1}, {X:-1, Y:2}}, - 3: {{X:0, Y:0}, {X:1, Y:0}, {X:-2, Y: 0}, {X:1, Y:2}, {X:-2, Y:-1}}, -} - -var LCCW = Kicks{ - 1: {{X:0, Y:0}, {X:2, Y:0}, {X:-1, Y: 0}, {X:2, Y:-1}, {X:-1, Y:2}}, - 2: {{X:0, Y:0}, {X:1, Y:0}, {X:-2, Y: 0}, {X:1, Y:2}, {X:-2, Y:-1}}, - 3: {{X:0, Y:0}, {X:-2, Y:0}, {X:1, Y: 0}, {X:-2, Y:1}, {X:1, Y:-2}}, - 0: {{X:0, Y:0}, {X:-1, Y:0}, {X:2, Y: 0}, {X:-1, Y:-2}, {X:2, Y:1}}, -} |