mirror of
https://github.com/richardharrington/robotwar.git
synced 2024-07-15 01:29:06 +00:00
wrote special-purpose registers: X, Y, DAMAGE, INDEX, DATA, RANDOM. Brain tests now failing.
This commit is contained in:
parent
8091e1f216
commit
041a639203
@ -1,6 +1,6 @@
|
|||||||
(ns robotwar.core
|
(ns robotwar.core
|
||||||
(:use [clojure.pprint]
|
(:use [clojure.pprint])
|
||||||
(robotwar foundry brain robot world game-lexicon brain-test)))
|
(:require (robotwar foundry brain robot world game-lexicon brain-test)))
|
||||||
|
|
||||||
; this is a hacky place for messing with stuff. currently imports
|
; this is a hacky place for messing with stuff. currently imports
|
||||||
; all the test data from brain-test, and the function below uses
|
; 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.
|
; their ugly full system-names of the read and write functions.) Very convenient.
|
||||||
|
|
||||||
(def get-robot (fn [world-tick-idx robot-idx]
|
(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]]
|
(def ppt (fn [world-tick-idx robot-idx & [reg-keys]]
|
||||||
(let [{:keys [brain registers] :as robot} (get-robot world-tick-idx robot-idx)]
|
(let [{:keys [brain registers] :as robot} (get-robot world-tick-idx robot-idx)]
|
||||||
@ -24,6 +28,5 @@
|
|||||||
brain
|
brain
|
||||||
[:obj-code :instrs]
|
[:obj-code :instrs]
|
||||||
(sort (zipmap (range) (get-in brain [:obj-code :instrs]))))
|
(sort (zipmap (range) (get-in brain [:obj-code :instrs]))))
|
||||||
:registers (sort (into {} (for [[reg-name reg-map]
|
:registers (sort (into {} (for [[reg-name reg-map] (select-keys registers reg-keys)]
|
||||||
(select-keys registers reg-keys)]
|
|
||||||
{reg-name (:val reg-map)})))})))))
|
{reg-name (:val reg-map)})))})))))
|
||||||
|
@ -1,5 +1,14 @@
|
|||||||
(ns robotwar.game-lexicon)
|
(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"
|
(def reg-names [ "DATA"
|
||||||
"A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M"
|
"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"
|
"N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
|
||||||
|
@ -17,31 +17,83 @@
|
|||||||
(write-func world path-to-val data))
|
(write-func world path-to-val data))
|
||||||
:val val}}))
|
: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,
|
"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
|
whose read function returns :val and whose write function returns a world
|
||||||
with its data argument pushed to :val"
|
with its data argument pushed to :val"
|
||||||
[reg-name robot-idx]
|
[reg-name robot-idx]
|
||||||
(init-register reg-name robot-idx get-in assoc-in 0))
|
(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
|
(defn init-robot
|
||||||
[idx pos-x pos-y src-code]
|
[idx src-code attributes]
|
||||||
{:idx idx
|
{:idx idx
|
||||||
:pos-x pos-x
|
:pos-x (:pos-x attributes)
|
||||||
:pos-y pos-y
|
:pos-y (:pos-y attributes)
|
||||||
:veloc-x 0
|
:veloc-x 0
|
||||||
:veloc-y 0
|
:veloc-y 0
|
||||||
:accel-x 0
|
:accel-x 0
|
||||||
:accel-y 0
|
:accel-y 0
|
||||||
:damage 100
|
:damage (:damage attributes)
|
||||||
;TODO: make some custom registers
|
:registers (init-registers idx attributes)
|
||||||
:registers (into {} (for [reg-name robotwar.game-lexicon/reg-names]
|
|
||||||
(default-register reg-name idx)))
|
|
||||||
:brain (robotwar.brain/init-brain src-code robotwar.game-lexicon/reg-names)})
|
:brain (robotwar.brain/init-brain src-code robotwar.game-lexicon/reg-names)})
|
||||||
|
|
||||||
(defn step-robot
|
(defn step-robot
|
||||||
"takes a robot and a world and returns the new state of the world
|
"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]
|
[robot world]
|
||||||
(if (<= (:damage robot) 0)
|
(if (<= (:damage robot) 0)
|
||||||
world
|
world
|
||||||
|
@ -9,11 +9,9 @@
|
|||||||
:height height
|
:height height
|
||||||
:shells []
|
:shells []
|
||||||
:robots (vec (map-indexed (fn [idx program]
|
:robots (vec (map-indexed (fn [idx program]
|
||||||
(robotwar.robot/init-robot
|
(robotwar.robot/init-robot idx program {:pos-x (rand-int width)
|
||||||
idx
|
:pos-y (rand-int height)
|
||||||
(rand-int width)
|
:damage 100}))
|
||||||
(rand-int height)
|
|
||||||
program))
|
|
||||||
programs))
|
programs))
|
||||||
:robot-idx 0})
|
:robot-idx 0})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user