Skip to content

Instantly share code, notes, and snippets.

@thegeez
Created November 7, 2010 16:28
Show Gist options
  • Save thegeez/666228 to your computer and use it in GitHub Desktop.
Save thegeez/666228 to your computer and use it in GitHub Desktop.
(ns scratch.clomian
(:require clojure.java.io)
(:use [incanter core charts]))
;(set! *warn-on-reflection* true)
(def files (filter (fn [^java.io.File f]
(let [^String n (.getName f)]
(and
(.endsWith n ".dat")
(.startsWith n "c."))))
(file-seq (clojure.java.io/file "PATH_TO_DIR/World2"))))
(defn ^bytes blocks [^java.io.File file]
(let [stream (-> file
java.io.FileInputStream.
java.io.BufferedInputStream.
org.jnbt.NBTInputStream.)
res (-> stream
.readTag
^java.util.Map (.getValue)
^org.jnbt.CompoundTag (.get "Level")
.getValue
^org.jnbt.ByteArrayTag (.get "Blocks")
.getValue)]
(.close stream)
res))
(defn ^bytes blocks-array []
(let [baos (java.io.ByteArrayOutputStream. 99844096)]
(doseq [^bytes block-arr (doall (map blocks files))]
(let [size (alength block-arr)]
(.write baos block-arr 0 size)))
(.toByteArray baos)))
(def blocks-arr (time (blocks-array)))
;; updated to not bash the transients in place
(defn freqs-loop [^bytes blocks]
(let [types #{(byte 56) (byte 49) (byte 16) (byte 15) (byte 14) (byte 73)}
size (alength blocks)]
(doall (map persistent!
(loop [idx (int 0)
freq-layer (transient (vec (repeatedly 128 #(transient {}))))]
(if (< idx size)
(let [block (aget blocks idx)]
(recur (unchecked-inc idx)
(if (types block)
(let [layer (unchecked-remainder idx 128)
fl (freq-layer layer)]
(assoc! freq-layer layer (assoc! fl block (inc (get fl block (int 0))))))
freq-layer)))
(persistent! freq-layer)))))))
(def fr (time (freqs-loop blocks-arr)))
(def types {16 "Coal ore"
56 "Diamond ore"
15 "Iron ore"
14 "Gold ore"
49 "Obsidian"
73 "Redstone ore"})
(defn plotfn [freqs btype layer]
(get (nth freqs layer) (byte btype) 0))
(defn graph [freqs]
(let [canvas (reduce #(add-function %1 (partial plotfn fr (key %2)) 0 128
:series-label (val %2))
(xy-plot [] []
:x-label "Layer"
:y-label "Blocks"
:legend true)
types)
]
(slider #(set-y-range canvas 0 %) (range 0 500))
(view canvas)
(save canvas "graph.png")))
(def pic (time (graph fr)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment