 
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Actions fn, actual env ;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
-; Simplest way to express mechanics (in order of preference):
-
-;  Schematics:
-;  a* => A   goes to a* -> a   (greedy fire)
-;  a => A    goes to  A => A   (bottom-up action)
-;  a -> A    goes to  a => A   (bottom-up activate)
-;  a => a => goes to => A =>   (top-down action)          
-;  a => a -> goes to a => a => (top-down activate)          
-
-; Ideally, should prefer top-most top-down, bottom-most bottom-up, or something...
 
 (defrecord ASPlanEnv [base-sas-env init actions actions-fn g-map 
                     ; Following stuff is used by hierarchy.

;; Assumes this will always be called on a "source", and asserts correspondingly.
(defn make-greedy-fire-action [s effect-var]
  (let [{:keys [name precond-map effect-map reward] :as factored-primitive}
          (->> effect-var action-var (state/get-var s))
        precond-vars (keys (dissoc precond-map effect-var))
        [free-pv unfree-pv] (util/separate #(state/get-var s (free-var %)) precond-vars)]
    (assert (every? #(contains? #{nil :frozen} (state/get-var s (action-var %))) precond-vars))
    (assert (every? #(contains? #{nil :frozen} (state/get-var s (action-var %))) unfree-pv))
    (doto (env-util/make-factored-primitive
           [::GreedyFire name]
           (into precond-map 
                 (concat [[(action-var effect-var) factored-primitive]]
                         (for [v free-pv]   [(free-var v)              true])
                         (for [v unfree-pv] [(parent-var v effect-var) true])
                         (for [v precond-vars] [(action-var v) (state/get-var s (action-var v))])))           
           (into effect-map 
                 (concat [[(action-var effect-var) nil]]
                         (for [v unfree-pv] [(free-var v)              true])
                         (for [v unfree-pv] [(parent-var v effect-var) false])
                         (for [v precond-vars] [(action-var v) nil])))
           0)
      (-> (env/applicable? s) assert))))


;; TODO: sloppy (var can be reserved for someone else, no action),
;;       extra-sloppy (if it has the right value now, who cares about anything else?)

;; TODO: should fail if I own parent, it has right value, but an action. (right now, we assert)...

(comment ;; Version of above that allows "stealing"
 (defn make-greedy-fire-action [s effect-var]
   (let [{:keys [name precond-map effect-map reward] :as factored-primitive}
         (->> effect-var action-var (state/get-var s))
         precond-vars (keys (dissoc precond-map effect-var))
         [free-pv unfree-pv] (util/separate #(state/get-var s (free-var %)) precond-vars)
         [reserved-pv external-pv] (util/separate #(state/get-var s (parent-var % effect-var))
                                                  unfree-pv)]
     (when (state/state-matches-map? s precond-map)
       (env-util/make-factored-primitive
        [::GreedyFire name]
        (into precond-map 
              (concat [[(action-var effect-var) factored-primitive]]
                      (for [v free-pv]   [(free-var v)              true])
                      (for [v reserved-pv] [(parent-var v effect-var) true])
                      (for [v external-pv] [(free-var v) false])
                      (for [v external-pv] [(parent-var v effect-var) false])))
        (into effect-map 
              (concat [[(action-var effect-var) nil]]
                      (for [v reserved-pv] [(free-var v)              true])
                      (for [v reserved-pv] [(parent-var v effect-var) false])))
        0)))))




                   (for [v precond-vars] [(action-var v) nil])))
     0)))

;; TODO: should fail if I own parent, it has right value, but an action. (right now, we assert)...

;; Try to make an action that greedily fires the action scheduled on effect-var,
;; effectively representing a composition of set-parent and fire-action actions.
