Last active
May 21, 2016 22:47
-
-
Save zehnpaard/37c2884aa492644cf8dfe0cd4f74758b to your computer and use it in GitHub Desktop.
Ant Sim 1 - Static World (Re-write of Rich Hickey's Ant Simulation as a Single-Threaded Application for my own studies)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns ant-sim | |
(:require [quil.core :as q] | |
[quil.middleware :as m])) | |
;Core data records for simulation | |
(defrecord Cell [food pheromone ant home]) | |
(defrecord Ant [position direction food]) | |
(defrecord World [board ants time]) | |
;Initial parameters | |
(def dim 10) ;Size of the world | |
(def ant-positions | |
[[0 0] [1 0] [2 0] [3 0] [4 0]]) | |
(def home-positions | |
(into [] | |
(for [i (range 5) | |
j (range 2)] | |
[i j]))) | |
(def foods | |
[[[5 5] 25] | |
[[5 6] 22] | |
[[6 5] 23]]) | |
;Function for creation of initialized world | |
(defn start-world | |
"Create World containing a board (map of Cell) and ants (vector of Ant) | |
as specified in the initial parameters and time set to 0" | |
[] | |
(let [coords (for [i (range dim) j (range dim)] [i j]) | |
board (zipmap coords | |
(repeat (->Cell 0 0 false false))) | |
board (reduce #(assoc-in %1 [%2 :ant] true) board ant-positions) | |
board (reduce #(assoc-in %1 [%2 :home] true) board home-positions) | |
board (reduce (fn [board [pos quantity]] | |
(assoc-in board [pos :food] quantity)) | |
board foods) | |
board (assoc-in board [[3 3] :pheromone] 0.5) | |
ants (into [] | |
(map #(->Ant % 4 0) ant-positions))] | |
(->World board ants 0))) | |
;Parameter for Quil - Size of square representing cell | |
(def ssize 20) | |
;Project-specific Quil functions | |
(declare draw-ant draw-cell) | |
(defn draw-world | |
"Color cells with particular properties, then draw ants" | |
[{:keys [board ants time]}] | |
(doseq [pos-cell board] | |
(apply draw-cell pos-cell)) | |
(doseq [ant ants] | |
(draw-ant ant))) | |
(defn draw-ant | |
"Draw a triangle based on the parameters of inputted Ant | |
Position determines the placement of the triangle | |
Direction determines the direction pointed by the apex | |
Food determines the color of the triangle" | |
[{:keys [position direction food]}] | |
(q/push-matrix) | |
(q/translate (/ ssize 2) (/ ssize 2)) | |
(apply q/translate | |
(map #(* % ssize) position)) | |
(q/rotate (* Math/PI (/ direction 4))) | |
(if (pos? food) | |
(q/fill 255 0 0) | |
(q/fill 0)) | |
(q/scale 0.8) | |
(q/triangle 0 (* -1 (/ ssize 2)) | |
(* -1 (/ ssize 4)) (/ ssize 2) | |
(/ ssize 4) (/ ssize 2)) | |
(q/pop-matrix)) | |
(defn draw-cell | |
"Color cell positions based on properties of the cell | |
Home: Grey | |
Food: Red with alpha value based on food value | |
Pheromone: Green with alpha value based on pheromone value" | |
[position {:keys [food pheromone home]}] | |
(q/push-matrix) | |
(q/no-stroke) | |
(apply q/translate (map #(* ssize %) position)) | |
(when home | |
(q/fill 200 200 200) | |
(q/rect 0 0 ssize ssize)) | |
(when (pos? food) | |
(q/fill 200 0 0 (* 10 food)) | |
(q/rect 0 0 ssize ssize)) | |
(when (pos? pheromone) | |
(q/fill 0 200 0 (* 20 pheromone)) | |
(q/rect 0 0 ssize ssize)) | |
(q/pop-matrix)) | |
;Core Quil rendering functions | |
(defn setup [] | |
(q/frame-rate 10) | |
(start-world)) | |
(defn update [state] | |
state) ;This does nothing at the moment, to be updated | |
(defn draw [state] | |
(q/background 255) | |
(draw-world state)) | |
(q/defsketch ant-sim | |
:size [(* ssize dim) (* ssize dim)] | |
:setup setup | |
:update update | |
:draw draw | |
:middleware [m/fun-mode]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment