peer-kit / src / ardegazu / peer / gym / neon_grid.cljs
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
;; ported-from: src/gym/neon-grid.ts @ v1.3.0
;;
;; Offline neon-grid gym: one round per episode, one sim tick per step(), with
;; per-seat views folded from the host frames exactly the way the live
;; NeonGridClient folds them — a policy trained here plugs into
;; NeonGridOptions.onTick unchanged. The host sim is deterministic; episodes
;; differ only through the policies' actions.
;;
;; Default rewards: +0.05 per tick alive, −1 on death, +1 for winning the round
;; (seat still standing when the round ends multi-player).
;;
;; Pinned by test/vectors/gym-neon-grid.json plus the gym↔client view-parity
;; assertion (field names AND order).
(ns ardegazu.peer.gym.neon-grid
  (:require [ardegazu.peer.obj :as obj]
            [ardegazu.peer.games.host.neon-grid-sim :as ng]
            [shadow.cljs.modern :refer (defclass)]))

(declare gym-apply gym-views gym-roster)

(defclass NeonGridGym
  (constructor [this opts]
    (let [players (unchecked-get opts "players")]
      (when (< players 2) (throw (js/Error. "neon-grid gym needs at least 2 players")))
      (unchecked-set this "players" players)
      (let [ids (js/Array. players)]
        (dotimes [i players] (aset ids i (str "p" i)))
        (unchecked-set this "_ids" ids)))
    (unchecked-set this "_scores" #js [])
    (unchecked-set this "_sim" nil)
    ;; client-visible state, folded from frames like NeonGridClient
    (unchecked-set this "_trail" (js/Uint8Array. (* ng/GW ng/GH)))
    (unchecked-set this "_pos" #js [])
    (unchecked-set this "_dir" #js [])
    (unchecked-set this "_alive" #js [])
    (unchecked-set this "_tickN" 0)))

(defn- gym-roster [self]
  (.map ^js (unchecked-get self "_ids")
        (fn [id i] #js [id id i (aget (unchecked-get self "_scores") i)])))

(defn- gym-apply
  "Fold host frames into client-visible state (mirrors NeonGridClient)."
  [self frames]
  (.forEach ^js frames
            (fn [f]
              (let [t (unchecked-get f "t")
                    trail (unchecked-get self "_trail")]
                (cond
                  (identical? t "st")
                  (do
                    (.fill trail 0)
                    (unchecked-set self "_tickN" 0)
                    (unchecked-set self "_pos" #js [])
                    (unchecked-set self "_dir" #js [])
                    (unchecked-set self "_alive" #js [])
                    (.forEach ^js (unchecked-get f "order")
                              (fn [_ s]
                                (let [st (aget (unchecked-get f "starts") s)
                                      x (aget st 0) y (aget st 1) d (aget st 2)]
                                  (aset (unchecked-get self "_pos") s #js [x y])
                                  (aset (unchecked-get self "_dir") s d)
                                  (aset (unchecked-get self "_alive") s true)
                                  (aset trail (+ (* y ng/GW) x) (inc s))))))

                  (identical? t "tk")
                  (do
                    (unchecked-set self "_tickN" (unchecked-get f "n"))
                    (.forEach ^js (unchecked-get f "h")
                              (fn [e]
                                (let [s (aget e 0) x (aget e 1) y (aget e 2) alive (aget e 3)]
                                  (if ^boolean (js* "!!(~{})" alive)
                                    (let [p (aget (unchecked-get self "_pos") s)
                                          px (aget p 0) py (aget p 1)
                                          dir (unchecked-get self "_dir")]
                                      (cond (> x px) (aset dir s 1)
                                            (< x px) (aset dir s 3)
                                            (> y py) (aset dir s 2)
                                            (< y py) (aset dir s 0))
                                      (aset (unchecked-get self "_pos") s #js [x y])
                                      (aset trail (+ (* y ng/GW) x) (inc s)))
                                    (do
                                      (aset (unchecked-get self "_alive") s false)
                                      (let [want (inc s) n (.-length trail)]
                                        (dotimes [i n]
                                          (when (identical? (aget trail i) want)
                                            (aset trail i 0))))))))))

                  :else nil))))
  js/undefined)

(defn- gym-views [self]
  ;; NeonGridGymView — field-identical to games/neon-grid.cljs NeonGridView, IN
  ;; ORDER; nine keys, so sequential sets (the eight-pair js-obj rule)
  (.map ^js (unchecked-get self "_ids")
        (fn [_ seat]
          (obj/ordered "seat" seat
                       "pos" (.slice ^js (aget (unchecked-get self "_pos") seat))
                       "dir" (aget (unchecked-get self "_dir") seat)
                       "alive" (aget (unchecked-get self "_alive") seat)
                       "tickN" (unchecked-get self "_tickN")
                       "gw" ng/GW
                       "gh" ng/GH
                       ;; a copy, safe to mutate
                       "grid" (js/Uint8Array. (unchecked-get self "_trail"))
                       "heads" (.map ^js (unchecked-get self "_pos")
                                     (fn [p s] (if (aget (unchecked-get self "_alive") s)
                                                 (.slice ^js p)
                                                 nil)))))))

(defn reset
  "Start a fresh round; returns the first decision views (countdown skipped)."
  [self]
  (let [ids (unchecked-get self "_ids")]
    (unchecked-set self "_scores" (.map ^js ids (fn [_] 0)))
    (let [sim (ng/NeonGridSim.
               (js-obj "roster" (fn [] (gym-roster self))
                       "addScore" (fn [id]
                                    (let [sc (unchecked-get self "_scores")
                                          i (.indexOf ^js ids id)]
                                      (aset sc i (inc (aget sc i))))
                                    js/undefined)
                       "resetScores" (fn []
                                       (.fill ^js (unchecked-get self "_scores") 0)
                                       js/undefined)))]
      (unchecked-set self "_sim" sim)
      (let [emit (ng/start-round sim (.map ^js ids (fn [id i] (js-obj "id" id "color" i))))]
        (gym-apply self (.map ^js (unchecked-get emit "frames") (fn [x] (unchecked-get x "f")))))
      (gym-views self))))

(defn step
  "One tick: actions[seat] = absolute direction 0..3, or null to hold."
  [self actions]
  (let [sim (unchecked-get self "_sim")]
    (when-not (and ^boolean (js* "!!(~{})" sim) (unchecked-get sim "_running"))
      (throw (js/Error. "call reset() first")))
    (let [players (unchecked-get self "players")
          ids (unchecked-get self "_ids")]
      (dotimes [s players]
        (let [a (aget actions s)]
          (when (and (not ^boolean (js* "~{} == null" a))
                     (aget (unchecked-get self "_alive") s))
            (ng/input sim (aget ids s) a))))
      (let [alive-before (.slice ^js (unchecked-get self "_alive"))
            emit (ng/step sim "tick")]
        (gym-apply self (.map ^js (unchecked-get emit "frames") (fn [x] (unchecked-get x "f"))))
        (let [over (let [e (.find ^js (unchecked-get emit "frames")
                                 (fn [x] (identical? "ov" (unchecked-get (unchecked-get x "f") "t"))))]
                     (when ^boolean (js* "!!(~{})" e) (unchecked-get e "f")))
              rewards (.map ^js (unchecked-get self "_alive")
                            (fn [alive s]
                              (if-not (aget alive-before s)
                                0
                                (if alive 0.05 -1))))]
          (if ^boolean (js* "!!(~{})" over)
            (let [w (unchecked-get over "w")
                  winner (if (nil? w) nil (.indexOf ^js ids w))]
              (when (and (not (nil? winner)) (>= winner 0))
                (aset rewards winner (+ (aget rewards winner) 1)))
              (js-obj "views" (gym-views self) "rewards" rewards "done" true "winner" winner))
            (js-obj "views" (gym-views self) "rewards" rewards
                    "done" false "winner" nil)))))))

(let [proto (.-prototype NeonGridGym)]
  (unchecked-set proto "reset" (fn [] (this-as self (reset self))))
  (unchecked-set proto "step" (fn [actions] (this-as self (step self actions)))))

static mirror of HEAD · about · clone: git clone https://git.ardegazu.ro/peer-kit.git