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")})
(def edge-set
{0 [3 3],
70 [0 0],
7 [4 4],
59 [2 1],
{0 [3 3],
70 [0 0],
7 [4 4],
59 [2 1],
175 [4 5],
27 [3 6],
1 [2 3],
27 [3 6],
1 [2 3],
206 [0 1],
239 [2 5],
4 [0 3],
95 [4 6],
4 [0 3],
95 [4 6],
141 [2 7],
15 [4 8],
15 [4 8],
159 [1 9],
31 [3 9],
31 [3 9],
223 [2 6],
13 [4 7],
13 [4 7],
191 [1 5],
143 [2 10],
41 [2 2],
43 [3 5],
6 [0 4],
41 [2 2],
43 [3 5],
6 [0 4],
111 [0 9],
3 [3 4],
12 [0 7],
2 [3 0],
3 [3 4],
12 [0 7],
2 [3 0],
142 [0 5],
23 [1 4],
47 [3 10],
23 [1 4],
47 [3 10],
127 [1 6],
19 [2 0],
11 [3 8],
255 [6 5],
9 [3 7],
5 [1 3],
14 [0 8],
45 [1 7],
78 [0 6],
19 [2 0],
11 [3 8],
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],
45 [1 7],
78 [0 6],
140 [0 2],
79 [2 9],
79 [2 9],
173 [1 2],
87 [1 0],
87 [1 0],
207 [2 8],
10 [3 1],
71 [2 4],
63 [1 8],
8 [3 2]})
10 [3 1],
71 [2 4],
63 [1 8],
8 [3 2]})
(def directions
[[:N [-1 0]]
@ -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))
(bit-set acc i)
acc))
0
directions)))
(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)))
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)
@ -211,5 +240,5 @@
(future (-main))
(reset! @#'engine/game-state (init-game {}))
(swap! @#'engine/game-state :assoc {})
(set-tile! @@#'engine/game-state 1 1 [2 2])
(set-tile! @@#'engine/game-state 1 1 [2 2])
:-)

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