Randomize floor tiles with ratios

This commit is contained in:
Joseph Ferano 2026-08-25 22:25:10 +07:00
parent 8543729118
commit f658f2974c
2 changed files with 97 additions and 63 deletions

View File

@ -77,7 +77,19 @@
127 [1 6],
19 [2 0],
11 [3 8],
255 [6 5],
255 {[1 1] 10.0
[5 0] 0.5
[5 1] 0.5
[5 2] 0.5
[5 3] 0.05
[5 4] 0.05
[5 5] 0.1
[6 0] 0.2
[6 1] 0.2
[6 2] 0.2
[6 3] 0.05
[6 4] 0.05
[6 5] 0.1},
9 [3 7],
5 [1 3],
14 [0 8],
@ -105,37 +117,52 @@
(defn normalize-mask [m]
(cond-> m
(not (and (bit-test m 0) (bit-test m 1))) (bit-clear 4) ; NE needs N+E
(not (and (bit-test m 0) (bit-test m 3))) (bit-clear 5) ; NW needs N+W
(not (and (bit-test m 2) (bit-test m 1))) (bit-clear 6) ; SE needs S+E
(not (and (bit-test m 2) (bit-test m 3))) (bit-clear 7))) ; SW needs S+W
(not (and (bit-test m 0) (bit-test m 1))) (bit-clear 4)
(not (and (bit-test m 0) (bit-test m 3))) (bit-clear 5)
(not (and (bit-test m 2) (bit-test m 1))) (bit-clear 6)
(not (and (bit-test m 2) (bit-test m 3))) (bit-clear 7)))
;; TODO: Is this working? Diagonals don't seem to be working
(defn surrounding-tiles [game at-row at-col]
(normalize-mask
(reduce-kv (fn [acc i [_ [dy dx]]]
(if (tile-at game (+ at-row dy) (+ at-col dx))
(let [r (+ at-row dy)
c (+ at-col dx)]
(if (and (>= r 0) (>= c 0)
(tile-at game r c))
(bit-set acc i)
acc))
acc)))
0
directions)))
(defn do-neighbors! [grid row col do-fn]
(doseq [[_ [dy dx]] directions
:let [r (+ row dy)
c (+ col dx)]]
(when (and (< -1 r rows) (< -1 c cols))
(do-fn r c))))
;; rows ;; => 80
;; cols ;; => 39
;; grid-len ;; => 3120
(defn set-random-tile! [game row col]
(let [tile (edge-set (surrounding-tiles game row col))]
(if (vector? tile)
(set-tile! game row col tile)
;; It's a map, should have the ratios
(set-tile! game row col
(reduce (fn [acc [tile weight]]
(if (< acc weight) (reduced tile) (- acc weight)))
(rand (reduce + (vals tile)))
tile)))))
(defn auto-tile! [game at-row at-col]
(let [me (surrounding-tiles game at-row at-col)
north (tile-at game (dec at-row) at-col)
south (tile-at game (inc at-row) at-col)
west (tile-at game at-row (dec at-col))
east (tile-at game at-row (inc at-col))
tile (edge-set me)]
(println me tile)
(set-tile! game at-row at-col tile)
(when north
(set-tile! game (dec at-row) at-col (edge-set (surrounding-tiles game (dec at-row) at-col))))
(when south
(set-tile! game (inc at-row) at-col (edge-set (surrounding-tiles game (inc at-row) at-col))))
(when west
(set-tile! game at-row (dec at-col) (edge-set (surrounding-tiles game at-row (dec at-col)))))
(when east
(set-tile! game at-row (inc at-col) (edge-set (surrounding-tiles game at-row (inc at-col)))))))
(set-random-tile! game at-row at-col)
(do-neighbors! (:grid game) at-row at-col
(fn [r c]
(when (tile-at game r c)
(set-random-tile! game r c)))))
(defn handle-game-input [config game]
(let [game (assoc game :event-queue [])]
@ -146,7 +173,9 @@
at-col (quot mx (* scale cell-size))]
(auto-tile! game at-row at-col)
(set-tile! game at-row at-col nil)))
(when (rl/mouse-button-pressed? rl/mouse-button-left))
(when (rl/mouse-button-pressed? rl/mouse-button-middle)
(doseq [r (range 10) c (range 20)]
(auto-tile! @@#'engine/game-state r c)))
(cond-> game
(rl/key-pressed? rl/key-space)
(update :editor-mode not)

View File

@ -68,7 +68,6 @@
(partition 11 (mapv tile-directions tiles))
(defn tile-bitmask [tile]
(reduce-kv (fn [acc i [_ [x y]]]
(if (contains? ref-colors (.getRGB tile x y))
@ -77,13 +76,19 @@
0
edges))
(mapv tile-bitmask tiles)
(into {} (map-indexed (fn [i t]
[(tile-bitmask t)
((juxt quot rem) i (quot (.getWidth img) tile-size))]))
tiles)
(reduce-kv (fn [acc i t]
(let [bm (tile-bitmask t)
row (quot i (quot (.getWidth img) tile-size))
col (rem i (quot (.getWidth img) tile-size))]
(update acc bm (fnil conj []) [row col])))
{}
tiles)
(comment
;; #C0D470
;; #A4C263