;;-*-Lisp-*-
(in-package goal)

;; definition for method 9 of type trajectory
;; INFO: this function exists in multiple non-identical object files
;; Used lq/sq
(defmethod eval-position! trajectory ((obj trajectory) (time float) (result vector))
  (set! (-> result quad) (-> obj initial-position quad))
  (+! (-> result x) (* time (-> obj initial-velocity x)))
  (+! (-> result y) (* time (-> obj initial-velocity y)))
  (+! (-> result z) (* time (-> obj initial-velocity z)))
  (+! (-> result y) (* 0.5 time time (-> obj gravity)))
  result
  )

;; definition for method 10 of type trajectory
;; Used lq/sq
(defmethod eval-velocity! trajectory ((obj trajectory) (time float) (result vector))
  (set! (-> result quad) (-> obj initial-velocity quad))
  (+! (-> result y) (* time (-> obj gravity)))
  result
  )

;; definition for method 11 of type trajectory
;; INFO: Return type mismatch int vs none.
;; Used lq/sq
(defmethod setup-from-to-duration! trajectory ((obj trajectory) (from vector) (to vector) (duration float) (grav float))
  (set! (-> obj initial-position quad) (-> from quad))
  (set! (-> obj gravity) grav)
  (set! (-> obj time) duration)
  (let ((xz-vel (/ (vector-vector-xz-distance to from) duration)))
    (vector-! (-> obj initial-velocity) to from)
    (vector-xz-normalize! (-> obj initial-velocity) xz-vel)
    )
  (set! (-> obj initial-velocity y)
        (- (/ (- (-> to y) (-> from y)) duration) (* 0.5 duration (-> obj gravity)))
        )
  0
  (none)
  )

;; definition for method 12 of type trajectory
;; INFO: Return type mismatch int vs none.
(defmethod setup-from-to-xz-vel! trajectory ((obj trajectory) (from vector) (to vector) (xz-vel float) (grav float))
  (let ((duration (/ (vector-vector-xz-distance to from) xz-vel)))
    (setup-from-to-duration! obj from to duration grav)
    )
  0
  (none)
  )

;; definition for method 13 of type trajectory
;; INFO: Return type mismatch int vs none.
(defmethod setup-from-to-y-vel! trajectory ((obj trajectory) (from vector) (to vector) (y-vel float) (grav float))
  (let* ((f0-0 y-vel)
         (f1-3 (- (* f0-0 f0-0) (* 2.0 (- (-> from y) (-> to y)) grav)))
         (f0-3 900.0)
         )
    (when (>= f1-3 0.0)
      (let ((f0-4 (sqrtf f1-3)))
        (set! f0-3 (fmax (/ (- (- y-vel) f0-4) grav) (/ (+ (- y-vel) f0-4) grav)))
        )
      )
    (setup-from-to-duration! obj from to f0-3 grav)
    )
  0
  (none)
  )

;; definition for method 14 of type trajectory
;; INFO: Return type mismatch int vs none.
(defmethod setup-from-to-height! trajectory ((obj trajectory) (arg0 vector) (arg1 vector) (arg2 float) (arg3 float))
  (let* ((f1-2 (+ arg2 (fmax (-> arg0 y) (-> arg1 y))))
         (f1-5 (* 2.0 (- (-> arg0 y) f1-2) arg3))
         (f0-3 4096.0)
         )
    (if (< 0.0 f1-5)
        (set! f0-3 (sqrtf f1-5))
        )
    (setup-from-to-y-vel! obj arg0 arg1 f0-3 arg3)
    )
  0
  (none)
  )

;; definition for method 15 of type trajectory
;; INFO: Return type mismatch int vs none.
;; Used lq/sq
(defmethod debug-draw! trajectory ((obj trajectory))
  (let ((prev-pos (new 'stack-no-clear 'vector))
        (pos (new 'stack-no-clear 'vector))
        (num-segments 10)
        )
    (set! (-> pos quad) (-> obj initial-position quad))
    (dotimes (s2-0 num-segments)
      (set! (-> prev-pos quad) (-> pos quad))
      (let ((t-eval (* (-> obj time) (/ (+ 1.0 (the float s2-0)) (the float num-segments)))))
        (eval-position! obj t-eval pos)
        )
      (add-debug-line
        #t
        (bucket-id debug-no-zbuf)
        prev-pos
        pos
        (new 'static 'rgba :r #xff :a #x80)
        #f
        (the-as rgba -1)
        )
      )
    )
  0
  (none)
  )
