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

;; name: nav-control-h.gc
;; name in dgo: nav-control-h
;; dgos: ENGINE, GAME

(defenum nav-state-flag
  :type uint32
  :bitfield #t

  (display-marks      0) ;; 1
  (recovery-mode      1) ;; 2
  (initialized        2) ;; 4
  (debug              3) ;; 8
  (directional-mode   4) ;; 16
  (trapped-by-sphere  5) ;; 32
  (target-poly-dirty  6) ;; 64
  (blocked            7) ;; 128
  (in-target-poly     8) ;; 256
  (at-target          9) ;; 512
  (target-inside     10) ;; 1024
  (in-mesh           11) ;; 2048
  (avoiding-sphere   12) ;; 4096
  (touching-sphere   13) ;; 8192
  (at-gap            14) ;; 16384
  )

(defenum nav-control-flag
  :type uint32
  :bitfield #t
  (display-marks        0) ;; 1
  (debug                1) ;; 2
  (no-redirect-in-clamp 2) ;; 4
  (limit-rotation-rate  3) ;; 8
  (update-heading-from-facing 4) ;; 16
  (use-momentum        5) ;; 32
  (momentum-ignore-heading 6) ;; 64
  (output-sphere-hash  7) ;; 128
  (kernel-run          8) ;; 256
  )

;; DECOMP BEGINS

(deftype check-vector-collision-with-nav-spheres-info (structure)
  ((u          float)
   (intersect  vector  :inline)
   (normal     vector  :inline)
   )
  )


(deftype nav-gap-info (structure)
  ((dest  vector  :inline)
   (poly  nav-poly)
   )
  )


(deftype nav-avoid-spheres-params (structure)
  ((current-pos           vector  :inline)
   (travel                vector  :inline)
   (pref-dir              vector  :inline)
   (out-travel            vector  2 :inline)
   (closest-sphere-dist2  float)
   (avoiding-sphere?      symbol)
   )
  )


(deftype nav-callback-info (structure)
  ((callback-count  int32)
   (callback-array  (function object nav-control none)  10)
   )
  )


(deftype nav-state (structure)
  ((flags                      nav-state-flag)
   (nav                        nav-control)
   (user-poly                  nav-poly)
   (mesh                       nav-mesh)
   (current-poly               nav-poly)
   (virtual-current-poly       nav-poly)
   (next-poly                  nav-poly)
   (target-poly                nav-poly)
   (rotation-rate              float)
   (speed                      meters)
   (prev-speed                 meters)
   (pad0                       uint32  1)
   (travel                     vector  :inline)
   (target-post                vector  :inline)
   (current-pos                vector  :inline)
   (current-pos-local          vector  :inline)
   (virtual-current-pos-local  vector  :inline)
   (velocity                   vector  :inline)
   (heading                    vector  :inline)
   (target-dir                 vector  :inline)
   (accel                      vector  :inline :overlay-at target-dir)
   )
  (:methods
    (debug-draw (_type_) none)
    (nav-state-method-10 (_type_) none)
    (plan-over-pat1-polys-using-route (_type_ nav-gap-info) symbol)
    (get-velocity (_type_ vector) vector)
    (get-travel (_type_ vector) vector)
    (get-heading (_type_ vector) vector)
    (get-target-post (_type_ vector) vector)
    (get-speed (_type_) meters)
    (get-rotation-rate (_type_) float)
    (try-projecting-to-current-poly (_type_ vector object vector) symbol)
    (get-current-poly (_type_) nav-poly)
    (copy-nav-state! (_type_ (pointer nav-state)) none)
    (nav-state-method-21 () none)
    (nav-state-method-22 () none)
    (nav-state-method-23 () none)
    (turn-and-navigate-to-destination (_type_) none)
    (navigate-using-route-portals-wrapper (_type_) none)
    (navigate-using-best-dir-recompute-avoid-spheres-1-wrapper (_type_) none)
    (navigate-within-poly-wrapper (_type_) none)
    (compute-travel-speed (_type_) none)
    (nav-state-method-29 (_type_) none)
    (nav-state-method-30 (_type_) none)
    (navigate-using-best-dir-recompute-avoid-spheres-2 (_type_) none)
    (update-travel-dir-from-spheres (_type_) none)
    (compute-speed-simple (_type_) none)
    (navigate-v1! (_type_) none)
    (reset-target! (_type_) none)
    (add-offset-to-target! (_type_ vector) none)
    (navigate-v2! (_type_) none)
    (set-current-poly! (_type_ nav-poly) none)
    (nav-state-method-39 (_type_) symbol)
    (do-navigation-to-destination (_type_ vector) none)
    (clamp-vector-to-mesh-cross-gaps (_type_ vector) symbol)
    (set-target-post! (_type_ vector) none)
    (set-travel! (_type_ vector) none)
    (set-velocity! (_type_ vector) none)
    (set-heading! (_type_ vector) none)
    (set-speed! (_type_ meters) none)
    (reset! (_type_ nav-control) none)
    (nav-state-method-48 () none)
    (navigate-using-best-dir-use-existing-avoid-spheres (_type_ nav-avoid-spheres-params) none)
    (nav-state-method-50 (_type_) none)
    (navigate-using-route-portals (_type_) none)
    (navigate-using-best-dir-recompute-avoid-spheres-1 (_type_) none)
    (navigate-within-poly (_type_) none)
    (clamp-travel-vector (_type_) none)
    )
  )


(deftype nav-control (structure)
  ((flags                 nav-control-flag)
   (callback-info         nav-callback-info)
   (process               process)
   (pad0                  uint32)
   (shape                 collide-shape)
   (nearest-y-threshold   meters)
   (nav-cull-radius       meters)
   (sec-per-frame         float)
   (target-speed          meters)
   (acceleration          meters)
   (turning-acceleration  meters)
   (max-rotation-rate     float)
   (speed-scale           float)
   (sphere-count          int32)
   (sphere-array          (inline-array sphere))
   (root-sphere-id        uint8)
   (sphere-mask           uint8)
   (pad1                  uint8      2)
   (sphere-id-array       uint8      16)
   (extra-nav-sphere      vector     :inline)
   (root-nav-sphere       vector     :inline)
   (state                 nav-state  :inline)
   )
  (:methods
    (debug-draw (_type_) none)
    (point-in-bsphere? (_type_ vector) symbol)
    (find-poly-containing-point-1 (_type_ vector) nav-poly)
    (cloest-point-on-mesh (_type_ vector vector nav-poly) nav-poly)
    (find-nearest-poly-to-point (_type_ vector) nav-poly)
    (project-point-onto-plane-of-poly (_type_ nav-poly vector vector vector) none)
    (find-poly-containing-point-2 (_type_ vector) nav-poly)
    (is-above-poly-max-height? (_type_ vector float) symbol)
    (is-in-mesh? (_type_ vector float) symbol)
    (avoid-spheres-1! (_type_ nav-avoid-spheres-params) symbol)
    (avoid-spheres-2! (_type_ nav-avoid-spheres-params) symbol)
    (clamp-vector-to-mesh-cross-gaps (_type_ vector nav-poly vector float symbol clamp-travel-vector-to-mesh-return-info) none)
    (clamp-vector-to-mesh-no-gaps (_type_ vector nav-poly vector clamp-travel-vector-to-mesh-return-info) none)
    (find-first-sphere-and-update-avoid-params (_type_ vector nav-avoid-spheres-params) float)
    (set-spheres-from-nav-ids (_type_) none)
    (add-root-sphere-to-hash! (_type_ vector int) symbol)
    (get-max-rotation-rate (_type_) float)
    (get-sphere-mask (_type_) uint)
    (get-target-speed (_type_) meters)
    (enable-extra-sphere! (_type_) none)
    (disable-extra-sphere! (_type_) none)
    (copy-extra-nav-sphere! (_type_ sphere) none)
    (set-extra-nav-sphere-xyz! (_type_ sphere) none)
    (set-extra-nav-sphere-radius! (_type_ float) none)
    (set-nearest-y-thres! (_type_ float) none)
    (set-nav-cull-radius! (_type_ meters) none)
    (set-speed-scale! (_type_ float) none)
    (set-target-speed! (_type_ meters) none)
    (set-acceleration! (_type_ meters) none)
    (set-turning-acceleration! (_type_ meters) none)
    (set-max-rotation-rate! (_type_ float) none)
    (set-sphere-mask! (_type_ uint) none)
    (remove! (_type_) none)
    (init! (_type_ collide-shape) none)
    (display-marks? (_type_) symbol)
    (nav-control-method-44 () none)
    (find-first-sphere-intersecting-ray (_type_ vector vector vector) sphere)
    (find-sphere-ids-from-sphere-hash (_type_ symbol) none)
    )
  )
