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

@ -45,53 +45,65 @@
:texture (load-texture-once "source-assets/Sprout Lands - Sprites - premium pack/Sprout Lands - Sprites - premium pack/Tilesets/ground tiles/New tiles/Grass_Hill_Tiles_v2.png")}) :texture (load-texture-once "source-assets/Sprout Lands - Sprites - premium pack/Sprout Lands - Sprites - premium pack/Tilesets/ground tiles/New tiles/Grass_Hill_Tiles_v2.png")})
(def edge-set (def edge-set
{0 [3 3], {0 [3 3],
70 [0 0], 70 [0 0],
7 [4 4], 7 [4 4],
59 [2 1], 59 [2 1],
175 [4 5], 175 [4 5],
27 [3 6], 27 [3 6],
1 [2 3], 1 [2 3],
206 [0 1], 206 [0 1],
239 [2 5], 239 [2 5],
4 [0 3], 4 [0 3],
95 [4 6], 95 [4 6],
141 [2 7], 141 [2 7],
15 [4 8], 15 [4 8],
159 [1 9], 159 [1 9],
31 [3 9], 31 [3 9],
223 [2 6], 223 [2 6],
13 [4 7], 13 [4 7],
191 [1 5], 191 [1 5],
143 [2 10], 143 [2 10],
41 [2 2], 41 [2 2],
43 [3 5], 43 [3 5],
6 [0 4], 6 [0 4],
111 [0 9], 111 [0 9],
3 [3 4], 3 [3 4],
12 [0 7], 12 [0 7],
2 [3 0], 2 [3 0],
142 [0 5], 142 [0 5],
23 [1 4], 23 [1 4],
47 [3 10], 47 [3 10],
127 [1 6], 127 [1 6],
19 [2 0], 19 [2 0],
11 [3 8], 11 [3 8],
255 [6 5], 255 {[1 1] 10.0
9 [3 7], [5 0] 0.5
5 [1 3], [5 1] 0.5
14 [0 8], [5 2] 0.5
45 [1 7], [5 3] 0.05
78 [0 6], [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],
45 [1 7],
78 [0 6],
140 [0 2], 140 [0 2],
79 [2 9], 79 [2 9],
173 [1 2], 173 [1 2],
87 [1 0], 87 [1 0],
207 [2 8], 207 [2 8],
10 [3 1], 10 [3 1],
71 [2 4], 71 [2 4],
63 [1 8], 63 [1 8],
8 [3 2]}) 8 [3 2]})
(def directions (def directions
[[:N [-1 0]] [[:N [-1 0]]
@ -105,37 +117,52 @@
(defn normalize-mask [m] (defn normalize-mask [m]
(cond-> 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 1))) (bit-clear 4)
(not (and (bit-test m 0) (bit-test m 3))) (bit-clear 5) ; NW needs N+W (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) ; SE needs S+E (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))) ; SW needs S+W (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] (defn surrounding-tiles [game at-row at-col]
(normalize-mask (normalize-mask
(reduce-kv (fn [acc i [_ [dy dx]]] (reduce-kv (fn [acc i [_ [dy dx]]]
(if (tile-at game (+ at-row dy) (+ at-col dx)) (let [r (+ at-row dy)
(bit-set acc i) c (+ at-col dx)]
acc)) (if (and (>= r 0) (>= c 0)
0 (tile-at game r c))
directions))) (bit-set acc i)
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] (defn auto-tile! [game at-row at-col]
(let [me (surrounding-tiles game at-row at-col) (set-random-tile! game at-row at-col)
north (tile-at game (dec at-row) at-col) (do-neighbors! (:grid game) at-row at-col
south (tile-at game (inc at-row) at-col) (fn [r c]
west (tile-at game at-row (dec at-col)) (when (tile-at game r c)
east (tile-at game at-row (inc at-col)) (set-random-tile! game r c)))))
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)))))))
(defn handle-game-input [config game] (defn handle-game-input [config game]
(let [game (assoc game :event-queue [])] (let [game (assoc game :event-queue [])]
@ -146,7 +173,9 @@
at-col (quot mx (* scale cell-size))] at-col (quot mx (* scale cell-size))]
(auto-tile! game at-row at-col) (auto-tile! game at-row at-col)
(set-tile! game at-row at-col nil))) (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 (cond-> game
(rl/key-pressed? rl/key-space) (rl/key-pressed? rl/key-space)
(update :editor-mode not) (update :editor-mode not)

View File

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