;; Fennel Behavior Trees
;; LGPL3+ AlexJGriffith

(local outcomes {:success 1 :failure 0 :running 2})

(local root-mt
       {:__call
        (fn [self actor blackboard dt]
          (local timestamp (+ self.timestamp dt))
          (set self.timestamp timestamp)
          (each [_ child (ipairs self.children)]
            (match (child actor blackboard dt timestamp)
              outcomes.success :continue
              outcomes.failure (lua "return outcomes.failure" )
              outcomes.running (lua "return outcomes.running") ))
          outcomes.success)})

(local sequence-mt
       {:__call
        (fn [self actor blackboard dt timestamp]
          (each [_ child (ipairs self.children)]
            (match (child actor blackboard dt timestamp)
              outcomes.success :continue
              outcomes.failure (lua "return outcomes.failure" )
              outcomes.running (lua "return outcomes.running") ))
          outcomes.success)})

(local selector-mt
       {:__call
        (fn [self actor blackboard dt timestamp]
          (each [_ child (ipairs self.children)]
            (match (child actor blackboard dt timestamp)
              outcomes.success (lua  "return outcomes.success")
              outcomes.failure :continue
              outcomes.running (lua "return outcomes.running")))
          outcomes.failure)})

(local invert-mt
       {:__call
        (fn [self actor blackboard dt timestamp]
          (local child (. self.children 1))          
          (match (child actor blackboard dt timestamp)
            outcomes.success outcomes.failure
            outcomes.failure outcomes.success
            outcomes.running outcomes.running))})

(local limit-mt
       {:__call
        (fn [self actor blackboard dt timestamp]
          (local child (. self.children 1))
          (local events (icollect [_ time (ipairs self.events)]
                          (when (> time timestamp) time)))
          (set self.events events)
          (if (< (# self.events) self.limit)
            (match (child actor blackboard dt timestamp)
              outcomes.success (do (table.insert self.events (+ timestamp self.window))
                                   outcomes.success)
              outcomes.failure (do
                                 (table.insert self.events (+ timestamp self.window))
                                 outcomes.failure)
              outcomes.running outcomes.running)
            outcomes.failure))})

(local succeed-mt
       {:__call
        (fn [self actor blackboard dt timestamp]
          (local child (. self.children 1))          
          (match (child actor blackboard dt timestamp)
            outcomes.success outcomes.success
            outcomes.failure outcomes.success
            outcomes.running outcomes.running))})

(fn root [...]  
  (setmetatable {:type :root :timestamp 0 :children [...]} root-mt))

;; Control Flow
(fn seq [...]  
  (setmetatable {:type :sequence :children [...]} sequence-mt))

(fn sel [...]
  (setmetatable {:type :selection :children [...]} selector-mt))

;; Decorators
(fn invert [child]
  (setmetatable {:type :invert :children [child]} invert-mt))

(fn limit [limit seconds child]
  (setmetatable
   {:type :limit :children [child] :events [] :limit limit :window seconds}
   limit-mt))

(fn succeed [child]
  (setmetatable {:type :succeed :children [child]} succeed-mt))

;; leaf
(fn leaf [name call]
  (setmetatable {:type :leaf :name name} {:__call call}))

{: root : leaf : seq : sel : invert : limit : succeed : outcomes}
