<commit_before>(ns examples.followbot
  (:use [clojure.contrib.math :only (abs)])
  (:require [clojurecraft.core :as core])
  (:require [clojurecraft.events :as events])
  (:require [clojurecraft.loops :as loops])
  (:require [clojurecraft.actions :as actions]))

; Helper functions -----------------------------------------------------------------
(defn- locs [bot entity]
  [(:loc @(:player bot))
   (:loc @entity)])

(defn- distance-between [bot entity]
  (let [[from to] (locs bot entity)]
    (+ (abs (- (:x from) (:x to)))
       (abs (- (:y from) (:y to)))
       (abs (- (:z from) (:z to))))))

(defn- toward-single [from to]
  (if (<= (abs (- from to)) 1)
    0
    (if (< from to) 1 -1)))

(defn- toward [bot entity]
  (let [[from to] (locs bot entity)]
    {:x (toward-single (:x from) (:x to))
     :z (toward-single (:z from) (:z to))}))

(defn- find-other-players [bot]
  (remove #(= (:name @%)
              (:name @(:player bot)))
          (filter #(:name @%)
                  (vals @(:entities (:world bot))))))

(defn- closest-entity [bot entities]
  (when (seq entities)
    (first (sort #(< (distance-between bot %1)
                     (distance-between bot %2))
                 entities))))

; Loops ----------------------------------------------------------------------------
(defn follow [bot]
  (when (:loc @(:player bot))
    (let [players (find-other-players bot)
          closest (closest-entity bot players)]
      (when closest
        (println (:name @(:player bot)) "following" (:name @closest))
        (println (:loc @(:player bot)))
        (println (:loc @closest))
        (let [{x :x z :z} (toward bot closest)]
          [(actions/move bot x 0 z)])))))


; Event handlers -------------------------------------------------------------------
(defn handle-dead [bot]
  [(actions/chat bot "WTF mate?")
   (actions/respawn bot)])


; Creation function ----------------------------------------------------------------
(defn make-followbot [server username]
  (let [bot (core/connect server username)]
    (events/add-handler bot :dead #'handle-dead)
    (loops/add-loop bot #'follow 200 :follow-loop)
    bot))
<commit_msg>Leftover.<commit_after>(ns examples.followbot
  (:use [clojure.contrib.math :only (abs)])
  (:require [clojurecraft.core :as core])
  (:require [clojurecraft.events :as events])
  (:require [clojurecraft.loops :as loops])
  (:require [clojurecraft.actions :as actions]))

; Helper functions -----------------------------------------------------------------
(defn- locs [bot entity]
  [(:loc @(:player bot))
   (:loc @entity)])

(defn- distance-between [bot entity]
  (let [[from to] (locs bot entity)]
    (+ (abs (- (:x from) (:x to)))
       (abs (- (:y from) (:y to)))
       (abs (- (:z from) (:z to))))))

(defn- toward-single [from to]
  (if (<= (abs (- from to)) 2)
    0
    (if (< from to) 1 -1)))

(defn- toward [bot entity]
  (let [[from to] (locs bot entity)]
    {:x (toward-single (:x from) (:x to))
     :z (toward-single (:z from) (:z to))}))

(defn- find-other-players [bot]
  (remove #(= (:name @%)
              (:name @(:player bot)))
          (filter #(:name @%)
                  (vals @(:entities (:world bot))))))

(defn- closest-entity [bot entities]
  (when (seq entities)
    (first (sort #(< (distance-between bot %1)
                     (distance-between bot %2))
                 entities))))


; Loops ----------------------------------------------------------------------------
(defn follow [bot]
  (when (:loc @(:player bot))
    (let [players (find-other-players bot)
          closest (closest-entity bot players)]
      (when closest
        (let [{x :x z :z} (toward bot closest)]
          [(actions/move bot x 0 z)])))))


; Event handlers -------------------------------------------------------------------
(defn handle-dead [bot]
  [(actions/chat bot "WTF mate?")
   (actions/respawn bot)])


; Creation function ----------------------------------------------------------------
(defn make-followbot [server username]
  (let [bot (core/connect server username)]
    (events/add-handler bot :dead #'handle-dead)
    (loops/add-loop bot #'follow 200 :follow-loop)
    bot))

{'fraction_non_alphanumeric': 0.29437125748502996, 'fraction_numerical': 0.004790419161676647, 'mean_word_length': 1.7637326273990734, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2943824', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1258, 'n_words': 390}