From 041a639203c71e2f82627b67695ac462cec01483 Mon Sep 17 00:00:00 2001 From: Richard Harrington Date: Mon, 5 Aug 2013 21:40:23 -0400 Subject: [PATCH] wrote special-purpose registers: X, Y, DAMAGE, INDEX, DATA, RANDOM. Brain tests now failing. --- src/robotwar/core.clj | 13 ++++--- src/robotwar/game_lexicon.clj | 9 +++++ src/robotwar/robot.clj | 70 ++++++++++++++++++++++++++++++----- src/robotwar/world.clj | 8 ++-- 4 files changed, 81 insertions(+), 19 deletions(-) diff --git a/src/robotwar/core.clj b/src/robotwar/core.clj index 1a9cf59..977fa03 100644 --- a/src/robotwar/core.clj +++ b/src/robotwar/core.clj @@ -1,6 +1,6 @@ (ns robotwar.core - (:use [clojure.pprint] - (robotwar foundry brain robot world game-lexicon brain-test))) + (:use [clojure.pprint]) + (:require (robotwar foundry brain robot world game-lexicon brain-test))) ; this is a hacky place for messing with stuff. currently imports ; all the test data from brain-test, and the function below uses @@ -14,7 +14,11 @@ ; their ugly full system-names of the read and write functions.) Very convenient. (def get-robot (fn [world-tick-idx robot-idx] - ((:robots (get-world world-tick-idx robot-idx worlds)) robot-idx))) + ((:robots (robotwar.world/get-world + world-tick-idx + robot-idx + robotwar.brain-test/worlds)) + robot-idx))) (def ppt (fn [world-tick-idx robot-idx & [reg-keys]] (let [{:keys [brain registers] :as robot} (get-robot world-tick-idx robot-idx)] @@ -24,6 +28,5 @@ brain [:obj-code :instrs] (sort (zipmap (range) (get-in brain [:obj-code :instrs])))) - :registers (sort (into {} (for [[reg-name reg-map] - (select-keys registers reg-keys)] + :registers (sort (into {} (for [[reg-name reg-map] (select-keys registers reg-keys)] {reg-name (:val reg-map)})))}))))) diff --git a/src/robotwar/game_lexicon.clj b/src/robotwar/game_lexicon.clj index 1d953dc..ea814f9 100644 --- a/src/robotwar/game_lexicon.clj +++ b/src/robotwar/game_lexicon.clj @@ -1,5 +1,14 @@ (ns robotwar.game-lexicon) +; The reason that the reg-names vector is not composed from concatting +; the other two is that the order in reg-names is important for indexing, +; and the X and Y registers have to go in the right place. + +(def storage-reg-names [ "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" + "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "Z"]) + +(def special-purpose-reg-names [ "DATA" "X" "Y" "AIM" "SHOT" "RADAR" "DAMAGE" "SPEEDX" "SPEEDY" "RANDOM" "INDEX" ]) + (def reg-names [ "DATA" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z" diff --git a/src/robotwar/robot.clj b/src/robotwar/robot.clj index 1818d9f..8f2eafb 100644 --- a/src/robotwar/robot.clj +++ b/src/robotwar/robot.clj @@ -17,31 +17,83 @@ (write-func world path-to-val data)) :val val}})) -(defn default-register +(defn get-robot [world path-to-val] + (get-in world (take 2 path-to-val))) + +(defn get-registers [world path-to-val] + (get-in world (take 3 path-to-val))) + +(defn init-default-register "takes a reg-name and robot-idx, and returns a register with initial :val 0, whose read function returns :val and whose write function returns a world with its data argument pushed to :val" [reg-name robot-idx] (init-register reg-name robot-idx get-in assoc-in 0)) +(defn init-read-only-register + "returns a register which has no effect (i.e. returns the world it was given) + when it is written to, but which returns a particular robot field when it is read." + [reg-name robot-idx field-name val] + (init-register reg-name robot-idx + (fn [world path-to-val] + (field-name (get-robot world path-to-val))) + (fn [world _ _] world) + val)) + +(defn init-registers + [robot-idx attributes] + (let [storage-registers (into {} (for [reg-name robotwar.game-lexicon/storage-reg-names] + (init-default-register reg-name robot-idx)))] + (into storage-registers + [ + ; INDEX + (init-default-register "INDEX" robot-idx) + + ; DATA + (letfn [(target-register [world path-to-val] + (let [registers (get-registers world path-to-val) + index-register (registers "INDEX")] + (registers (robotwar.game-lexicon/reg-names (:val index-register)))))] + (init-register "DATA" robot-idx + (fn [world path-to-val] + (robotwar.brain/read-register (target-register world path-to-val))) + (fn [world path-to-val data] + (robotwar.brain/write-register (target-register world) data)) + 0)) + + ; RANDOM + (init-register "RANDOM" robot-idx + (fn [world path-to-val] + (rand-int (get-in world path-to-val))) + assoc-in + 0) + + ;X and Y and DAMAGE + (init-read-only-register "X" robot-idx :pos-x (:pos-x attributes)) + (init-read-only-register "Y" robot-idx :pos-y (:pos-y attributes)) + (init-read-only-register "DAMAGE" robot-idx :damage (:damage attributes))]))) + +; REGISTERS DONE: "X" "Y" "DAMAGE" "RANDOM" "INDEX" "DATA" +; REGISTERS TODO: "AIM" "SHOT" "RADAR" "SPEEDX" "SPEEDY" + (defn init-robot - [idx pos-x pos-y src-code] + [idx src-code attributes] {:idx idx - :pos-x pos-x - :pos-y pos-y + :pos-x (:pos-x attributes) + :pos-y (:pos-y attributes) :veloc-x 0 :veloc-y 0 :accel-x 0 :accel-y 0 - :damage 100 - ;TODO: make some custom registers - :registers (into {} (for [reg-name robotwar.game-lexicon/reg-names] - (default-register reg-name idx))) + :damage (:damage attributes) + :registers (init-registers idx attributes) :brain (robotwar.brain/init-brain src-code robotwar.game-lexicon/reg-names)}) (defn step-robot "takes a robot and a world and returns the new state of the world - after the robot has taken its turn" + after the robot has taken its turn. + TODO: add a lot more stuff here that happens after the step-brain function, + like moving the robot. Actually, that's the main thing." [robot world] (if (<= (:damage robot) 0) world diff --git a/src/robotwar/world.clj b/src/robotwar/world.clj index 322498a..dedc93f 100644 --- a/src/robotwar/world.clj +++ b/src/robotwar/world.clj @@ -9,11 +9,9 @@ :height height :shells [] :robots (vec (map-indexed (fn [idx program] - (robotwar.robot/init-robot - idx - (rand-int width) - (rand-int height) - program)) + (robotwar.robot/init-robot idx program {:pos-x (rand-int width) + :pos-y (rand-int height) + :damage 100})) programs)) :robot-idx 0})