peer-kit / src / ardegazu / peer / games / host / valley_blocks_core.cljs
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
;; ported-from: src/games/host/valley-blocks-core.ts @ v1.3.0
;;   (itself: game2/client/src/game/core/{board,pieces,bag,scoring}.ts @ 25e6d79)
;; divergences: four browser files folded into one; Bag's rng parameter is
;;   REQUIRED (the browser default was Math.random — the engine always passes a
;;   seeded mulberry32, and a gym must never fall through to ambient
;;   randomness).
;;
;; Every table below is byte-compared against test/vectors/valley-blocks-core.json
;; (SRS shapes for all 7×4 states plus out-of-range rotation normalization, all
;; wall-kick rows, the scoring curves, and five 7-bag sequences of 35 draws).
(ns ardegazu.peer.games.host.valley-blocks-core
  (:require [shadow.cljs.modern :refer (defclass)]))

(def PIECE-TYPES #js ["I" "O" "T" "S" "Z" "J" "L"])

;; Rotation states 0..3 (spawn, CW, 180, CCW). Each state is a list of [x, y]
;; cell offsets, y growing downward, within the SRS bounding box.
(def ^:private SHAPES
  (js-obj
   "I" #js [#js [#js [0 1] #js [1 1] #js [2 1] #js [3 1]]
            #js [#js [2 0] #js [2 1] #js [2 2] #js [2 3]]
            #js [#js [0 2] #js [1 2] #js [2 2] #js [3 2]]
            #js [#js [1 0] #js [1 1] #js [1 2] #js [1 3]]]
   "O" #js [#js [#js [1 0] #js [2 0] #js [1 1] #js [2 1]]
            #js [#js [1 0] #js [2 0] #js [1 1] #js [2 1]]
            #js [#js [1 0] #js [2 0] #js [1 1] #js [2 1]]
            #js [#js [1 0] #js [2 0] #js [1 1] #js [2 1]]]
   "T" #js [#js [#js [1 0] #js [0 1] #js [1 1] #js [2 1]]
            #js [#js [1 0] #js [1 1] #js [2 1] #js [1 2]]
            #js [#js [0 1] #js [1 1] #js [2 1] #js [1 2]]
            #js [#js [1 0] #js [0 1] #js [1 1] #js [1 2]]]
   "S" #js [#js [#js [1 0] #js [2 0] #js [0 1] #js [1 1]]
            #js [#js [1 0] #js [1 1] #js [2 1] #js [2 2]]
            #js [#js [1 1] #js [2 1] #js [0 2] #js [1 2]]
            #js [#js [0 0] #js [0 1] #js [1 1] #js [1 2]]]
   "Z" #js [#js [#js [0 0] #js [1 0] #js [1 1] #js [2 1]]
            #js [#js [2 0] #js [1 1] #js [2 1] #js [1 2]]
            #js [#js [0 1] #js [1 1] #js [1 2] #js [2 2]]
            #js [#js [1 0] #js [0 1] #js [1 1] #js [0 2]]]
   "J" #js [#js [#js [0 0] #js [0 1] #js [1 1] #js [2 1]]
            #js [#js [1 0] #js [2 0] #js [1 1] #js [1 2]]
            #js [#js [0 1] #js [1 1] #js [2 1] #js [2 2]]
            #js [#js [1 0] #js [1 1] #js [0 2] #js [1 2]]]
   "L" #js [#js [#js [2 0] #js [0 1] #js [1 1] #js [2 1]]
            #js [#js [1 0] #js [1 1] #js [1 2] #js [2 2]]
            #js [#js [0 1] #js [1 1] #js [2 1] #js [0 2]]
            #js [#js [0 0] #js [1 0] #js [1 1] #js [1 2]]]))

(defn cells-for [type rotation]
  (aget (unchecked-get SHAPES type) (mod (+ (mod rotation 4) 4) 4)))

;; SRS wall kick data: kicks[from][direction] where direction 0 = CW, 1 = CCW.
;; Values are [dx, dy] with y growing downward (SRS +y up is negated here).
(def ^:private KICKS-JLSTZ
  #js [;; from state 0
       #js [#js [#js [0 0] #js [-1 0] #js [-1 -1] #js [0 2] #js [-1 2]]    ; 0 -> 1 (CW)
            #js [#js [0 0] #js [1 0] #js [1 -1] #js [0 2] #js [1 2]]]      ; 0 -> 3 (CCW)
       #js [#js [#js [0 0] #js [1 0] #js [1 1] #js [0 -2] #js [1 -2]]      ; 1 -> 2
            #js [#js [0 0] #js [1 0] #js [1 1] #js [0 -2] #js [1 -2]]]     ; 1 -> 0
       #js [#js [#js [0 0] #js [1 0] #js [1 -1] #js [0 2] #js [1 2]]       ; 2 -> 3
            #js [#js [0 0] #js [-1 0] #js [-1 -1] #js [0 2] #js [-1 2]]]   ; 2 -> 1
       #js [#js [#js [0 0] #js [-1 0] #js [-1 1] #js [0 -2] #js [-1 -2]]   ; 3 -> 0
            #js [#js [0 0] #js [-1 0] #js [-1 1] #js [0 -2] #js [-1 -2]]]]) ; 3 -> 2

(def ^:private KICKS-I
  #js [#js [#js [#js [0 0] #js [-2 0] #js [1 0] #js [-2 1] #js [1 -2]]     ; 0 -> 1
            #js [#js [0 0] #js [-1 0] #js [2 0] #js [-1 -2] #js [2 1]]]    ; 0 -> 3
       #js [#js [#js [0 0] #js [-1 0] #js [2 0] #js [-1 -2] #js [2 1]]     ; 1 -> 2
            #js [#js [0 0] #js [2 0] #js [-1 0] #js [2 -1] #js [-1 2]]]    ; 1 -> 0
       #js [#js [#js [0 0] #js [2 0] #js [-1 0] #js [2 -1] #js [-1 2]]     ; 2 -> 3
            #js [#js [0 0] #js [1 0] #js [-2 0] #js [1 2] #js [-2 -1]]]    ; 2 -> 1
       #js [#js [#js [0 0] #js [1 0] #js [-2 0] #js [1 2] #js [-2 -1]]     ; 3 -> 0
            #js [#js [0 0] #js [-2 0] #js [1 0] #js [-2 1] #js [1 -2]]]])  ; 3 -> 2

(defn kicks-for [type from cw]
  (if (identical? type "O")
    #js [#js [0 0]]
    (let [table (if (identical? type "I") KICKS-I KICKS-JLSTZ)]
      (aget (aget table (mod (+ (mod from 4) 4) 4)) (if cw 0 1)))))

;; An ActivePiece is #js {:type :rotation :x :y}.

;; ---------------- board ----------------

(def COLS 10)
(def ROWS 20)
(def HIDDEN-ROWS 2)
(def TOTAL-ROWS (+ ROWS HIDDEN-ROWS))

;; A cell is 0 (empty), a PieceType string, or "N" for frozen snake segments.

(defclass Board
  (constructor [this]
    (let [grid (js/Array. TOTAL-ROWS)]
      (dotimes [y TOTAL-ROWS] (aset grid y (.fill (js/Array. COLS) 0)))
      (unchecked-set this "grid" grid))))

(defn collides? [board piece]
  (let [grid (unchecked-get board "grid")
        offs (cells-for (unchecked-get piece "type") (unchecked-get piece "rotation"))
        px (unchecked-get piece "x")
        py (unchecked-get piece "y")
        n (.-length offs)]
    (loop [i 0]
      (if (>= i n)
        false
        (let [o (aget offs i)
              x (+ px (aget o 0))
              y (+ py (aget o 1))]
          (cond
            (or (< x 0) (>= x COLS) (>= y TOTAL-ROWS)) true
            (and (>= y 0) (not (identical? 0 (aget (aget grid y) x)))) true
            :else (recur (inc i))))))))

(defn board-lock [board piece]
  (let [grid (unchecked-get board "grid")
        offs (cells-for (unchecked-get piece "type") (unchecked-get piece "rotation"))
        px (unchecked-get piece "x")
        py (unchecked-get piece "y")
        t (unchecked-get piece "type")]
    (.forEach offs
              (fn [o]
                (let [y (+ py (aget o 1))
                      x (+ px (aget o 0))]
                  (when (and (>= y 0) (< y TOTAL-ROWS) (>= x 0) (< x COLS))
                    (aset (aget grid y) x t))))))
  js/undefined)

(defn clear-lines
  "Removes full rows and returns the indices (in grid space) that were cleared."
  [board]
  (let [grid (unchecked-get board "grid")
        cleared #js []]
    (dotimes [y TOTAL-ROWS]
      (when (.every (aget grid y) (fn [c] (not (identical? c 0))))
        (.push cleared y)))
    (.forEach cleared
              (fn [y]
                (.splice grid y 1)
                (.unshift grid (.fill (js/Array. COLS) 0))))
    cleared))

(defn topped-out?
  "True when locked cells reach into the hidden spawn rows."
  [board]
  (let [grid (unchecked-get board "grid")]
    (loop [y 0]
      (cond
        (>= y HIDDEN-ROWS) false
        (.some (aget grid y) (fn [c] (not (identical? c 0)))) true
        :else (recur (inc y))))))

(let [proto (.-prototype Board)]
  (unchecked-set proto "collides" (fn [piece] (this-as self (collides? self piece))))
  (unchecked-set proto "lock" (fn [piece] (this-as self (board-lock self piece))))
  (unchecked-set proto "clearLines" (fn [] (this-as self (clear-lines self))))
  (unchecked-set proto "isToppedOut" (fn [] (this-as self (topped-out? self)))))

;; ---------------- bag ----------------

;; 7-bag randomizer: each set of 7 pieces contains every tetromino once.
;; Takes an rng so duel players sharing a seed get identical sequences.
(defclass Bag
  (constructor [this rng]
    (unchecked-set this "_queue" #js [])
    (unchecked-set this "_rng" rng)))

(defn- refill-once [self]
  (let [bag (.slice PIECE-TYPES)
        rng (unchecked-get self "_rng")]
    ;; Fisher-Yates from the top, one rng draw per swap — the draw ORDER and
    ;; COUNT are part of the seeded contract
    (loop [i (dec (.-length bag))]
      (when (> i 0)
        (let [j (js/Math.floor (* (rng) (inc i)))
              tmp (aget bag i)]
          (aset bag i (aget bag j))
          (aset bag j tmp)
          (recur (dec i)))))
    (.apply (.-push (unchecked-get self "_queue")) (unchecked-get self "_queue") bag))
  js/undefined)

(defn bag-next [self]
  (let [q (unchecked-get self "_queue")]
    (when (zero? (.-length q)) (refill-once self))
    (.shift q)))

(defn bag-peek [self count]
  (let [q (unchecked-get self "_queue")]
    (loop []
      (when (< (.-length q) count)
        (refill-once self)
        (recur)))
    (.slice q 0 count)))

(let [proto (.-prototype Bag)]
  (unchecked-set proto "next" (fn [] (this-as self (bag-next self))))
  (unchecked-set proto "peek" (fn [c] (this-as self (bag-peek self c)))))

;; ---------------- scoring ----------------

(def LINES-PER-LEVEL 10)

(def ^:private LINE-POINTS #js [0 100 300 500 800])

(defn points-for-lines [lines level]
  (let [p (aget LINE-POINTS lines)]
    (* (if (identical? p js/undefined) 0 p) level)))

(def SOFT-DROP-POINTS 1)
(def HARD-DROP-POINTS 2)

(defn gravity-seconds
  "Seconds a piece takes to fall one row at a given level (guideline curve)."
  [level]
  (let [l (js/Math.min level 20)]
    (js/Math.pow (- 0.8 (* (- l 1) 0.007)) (- l 1))))

(defn level-for-lines [lines]
  (inc (js/Math.floor (/ lines LINES-PER-LEVEL))))

(defn snake-interval
  "Seconds between snake auto-steps — the snake slithers faster at higher levels."
  [level]
  (js/Math.max 0.11 (- 0.28 (* (- level 1) 0.012))))

(defn snake-duration
  "Seconds of free snake movement before it freezes — shorter at higher levels."
  [level]
  (js/Math.max 6 (- 12 (* (- level 1) 0.4))))

static mirror of HEAD · about · clone: git clone https://git.ardegazu.ro/peer-kit.git