wrote special-purpose registers: X, Y, DAMAGE, INDEX, DATA, RANDOM. Brain tests now failing.

This commit is contained in:
Richard Harrington 2013-08-05 21:40:23 -04:00
parent 8091e1f216
commit 041a639203
4 changed files with 81 additions and 19 deletions

View File

@ -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)})))})))))

View File

@ -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"

View File

@ -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

View File

@ -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})