2017-12-24 20:36:34 +03:00
|
|
|
(use Int)
|
|
|
|
(use Double)
|
|
|
|
(use Array)
|
|
|
|
|
2018-03-20 18:05:52 +03:00
|
|
|
(load "SDL.carp")
|
|
|
|
(load "SDL_image.carp")
|
2017-12-25 18:35:00 +03:00
|
|
|
|
|
|
|
(Debug.sanitize-addresses)
|
2017-12-24 20:36:34 +03:00
|
|
|
|
2018-03-14 22:48:23 +03:00
|
|
|
(Project.config "title" "Ant")
|
|
|
|
|
2017-12-24 20:36:34 +03:00
|
|
|
(deftype State
|
|
|
|
[width Int
|
|
|
|
height Int
|
|
|
|
grid (Array Bool)
|
|
|
|
x Int
|
|
|
|
y Int
|
2018-03-26 12:22:18 +03:00
|
|
|
dir Int
|
|
|
|
stopped Bool])
|
2017-12-24 20:36:34 +03:00
|
|
|
|
|
|
|
(defn coord-to-index [state x y]
|
2018-02-05 13:14:13 +03:00
|
|
|
(+ (* y @(State.width state)) x))
|
2017-12-24 20:36:34 +03:00
|
|
|
|
|
|
|
(defn get-square [state x y]
|
|
|
|
@(nth (State.grid state) (coord-to-index state x y)))
|
|
|
|
|
|
|
|
(defn draw [rend state]
|
|
|
|
(do
|
2018-03-20 16:34:49 +03:00
|
|
|
(SDL.set-render-draw-blend-mode rend SDL.blend-mode-add)
|
|
|
|
(SDL.set-render-draw-color rend 0 0 0 0)
|
|
|
|
(SDL.render-clear rend)
|
2018-02-05 13:14:13 +03:00
|
|
|
(for [y 0 @(State.height state)]
|
|
|
|
(for [x 0 @(State.width state)]
|
|
|
|
(let [w (/ 800 @(State.width state))
|
|
|
|
h (/ 600 @(State.height state))]
|
2017-12-24 20:36:34 +03:00
|
|
|
(do
|
|
|
|
(cond
|
2018-03-20 16:34:49 +03:00
|
|
|
(and (= x @(State.x state)) (= y @(State.y state))) (SDL.set-render-draw-color rend 255 0 0 255)
|
|
|
|
(get-square state x y) (SDL.set-render-draw-color rend 0 0 0 255)
|
|
|
|
(SDL.set-render-draw-color rend 255 255 255 255))
|
|
|
|
(SDL.render-fill-rect rend (address (SDL.rect (* x w) (* y h) (dec w) (dec h))))))))
|
|
|
|
(SDL.render-present rend)
|
2017-12-24 20:36:34 +03:00
|
|
|
))
|
|
|
|
|
|
|
|
(defn handle-events [app rend]
|
2018-03-20 18:05:52 +03:00
|
|
|
(let [event (SDL.Event.init)]
|
|
|
|
(while (SDL.Event.poll (address event))
|
|
|
|
(let [et (SDL.Event.type &event)]
|
|
|
|
(cond (= et SDL.Event.quit) (SDLApp.stop app)
|
2017-12-24 20:36:34 +03:00
|
|
|
())))))
|
|
|
|
|
|
|
|
(defn flip-at-ant [state]
|
2018-02-05 13:14:13 +03:00
|
|
|
(let [x @(State.x &state)
|
|
|
|
y @(State.y &state)
|
2017-12-24 20:36:34 +03:00
|
|
|
n (coord-to-index &state x y)
|
|
|
|
b (nth (State.grid &state) n)]
|
|
|
|
(do (aset! (State.grid &state) n (not @b))
|
|
|
|
state)))
|
|
|
|
|
|
|
|
(defn turn-left [dir]
|
|
|
|
(if (= dir 3) 0 (inc dir)))
|
|
|
|
|
|
|
|
(defn turn-right [dir]
|
|
|
|
(if (= dir 0) 3 (dec dir)))
|
|
|
|
|
|
|
|
(defn move-ant [state]
|
2018-02-05 13:14:13 +03:00
|
|
|
(let [x @(State.x &state)
|
|
|
|
y @(State.y &state)
|
2017-12-24 20:36:34 +03:00
|
|
|
n (coord-to-index &state x y)
|
|
|
|
b (nth (State.grid &state) n)
|
2018-02-05 13:14:13 +03:00
|
|
|
dir @(State.dir &state)
|
2017-12-24 20:36:34 +03:00
|
|
|
new-dir (if @b (turn-left dir) (turn-right dir))
|
|
|
|
new-x (case new-dir
|
|
|
|
0 (inc x)
|
|
|
|
2 (dec x)
|
|
|
|
x)
|
|
|
|
new-y (case new-dir
|
|
|
|
1 (inc y)
|
|
|
|
3 (dec y)
|
|
|
|
y)]
|
2018-03-26 12:22:18 +03:00
|
|
|
(if (or* (< new-x 0)
|
|
|
|
(< new-y 0)
|
|
|
|
(>= new-x @(State.width &state))
|
|
|
|
(>= new-y @(State.height &state)))
|
|
|
|
(State.set-stopped state true)
|
|
|
|
(=> state
|
|
|
|
(State.set-x new-x)
|
|
|
|
(State.set-y new-y)
|
|
|
|
(State.set-dir new-dir)))))
|
2017-12-24 20:36:34 +03:00
|
|
|
|
2017-12-25 20:02:03 +03:00
|
|
|
(defn tick [state]
|
|
|
|
(=> state
|
2017-12-24 20:36:34 +03:00
|
|
|
(move-ant)
|
|
|
|
(flip-at-ant)))
|
|
|
|
|
|
|
|
(defn create-state []
|
|
|
|
(let [w 80
|
|
|
|
h 60
|
|
|
|
b false
|
|
|
|
arr (Array.replicate (* w h) &b)]
|
2018-03-26 12:22:18 +03:00
|
|
|
(State.init w h arr (/ w 2) (/ h 2) 0 false)))
|
2017-12-24 20:36:34 +03:00
|
|
|
|
2018-01-23 16:20:35 +03:00
|
|
|
(defn-do main []
|
|
|
|
(Debug.log-memory-balance! true)
|
2018-03-26 12:22:18 +03:00
|
|
|
(let-do [app (SDLApp.create "Langton's Ant" 800 600)
|
|
|
|
rend @(SDLApp.renderer &app)
|
|
|
|
state (create-state)]
|
|
|
|
(while (not @(State.stopped &state))
|
|
|
|
(do
|
|
|
|
(handle-events &app rend)
|
|
|
|
(draw rend &state)
|
|
|
|
(set! state (tick state))
|
|
|
|
(SDL.delay 1))))
|
|
|
|
0)
|
2017-12-25 18:51:23 +03:00
|
|
|
|
2017-12-25 20:02:03 +03:00
|
|
|
;; Just the model
|
2017-12-25 18:52:00 +03:00
|
|
|
;; (defn main []
|
|
|
|
;; (do
|
|
|
|
;; (Debug.reset-memory-balance!)
|
|
|
|
;; (let [state (create-state)]
|
2017-12-25 20:02:03 +03:00
|
|
|
;; (for [i 0 10000]
|
2017-12-25 18:52:00 +03:00
|
|
|
;; (do
|
2018-02-02 09:19:10 +03:00
|
|
|
;; (set! state (tick state))
|
2017-12-25 18:52:00 +03:00
|
|
|
;; (IO.println &(str (State.dir &state)))
|
|
|
|
;; (IO.println &(str (Debug.memory-balance)))
|
2017-12-25 20:02:03 +03:00
|
|
|
;; (System.sleep-micros 1000)
|
2017-12-25 18:52:00 +03:00
|
|
|
;; )))
|
|
|
|
;; (IO.println &(str (Debug.memory-balance)))))
|