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

(import "goal_src/jak1/import/swamp-rat-ag.gc")

;; definition of type swamp-rat
(deftype swamp-rat (nav-enemy)
  ((up-vector          vector     :inline :offset-assert 400)
   (state-float        float              :offset-assert 416)
   (state-vector       vector     :inline :offset-assert 432)
   (_hack              uint64             :offset-assert 448)
   (wiggle-time        time-frame         :offset-assert 456)
   (wiggle-angle       float              :offset-assert 464)
   (delta-wiggle-angle float              :offset-assert 468)
   (wiggle-factor      float              :offset-assert 472)
   (min-height         float              :offset-assert 476)
   (chase-rest-time    time-frame         :offset-assert 480)
   (target-nav-time    time-frame         :offset-assert 488)
   )
  :heap-base #x180
  :method-count-assert 76
  :size-assert         #x1f0
  :flag-assert         #x4c018001f0
  (:states
    swamp-rat-spawn
    )
  )

;; definition for method 3 of type swamp-rat
(defmethod inspect swamp-rat ((obj swamp-rat))
  (let ((t9-0 (method-of-type nav-enemy inspect)))
    (t9-0 obj)
    )
  (format #t "~T~Tup-vector: #<vector @ #x~X>~%" (-> obj up-vector))
  (format #t "~T~Tstate-float: ~f~%" (-> obj state-float))
  (format #t "~T~Tstate-vector: ~`vector`P~%" (-> obj state-vector))
  (format #t "~T~Tfree-time: ~D~%" (-> obj free-time))
  (format #t "~T~Twiggle-time: ~D~%" (-> obj wiggle-time))
  (format #t "~T~Twiggle-angle: ~f~%" (-> obj wiggle-angle))
  (format #t "~T~Tdelta-wiggle-angle: ~f~%" (-> obj delta-wiggle-angle))
  (format #t "~T~Twiggle-factor: ~f~%" (-> obj wiggle-factor))
  (format #t "~T~Tmin-height: ~f~%" (-> obj min-height))
  (format #t "~T~Tchase-rest-time: ~D~%" (-> obj chase-rest-time))
  (format #t "~T~Ttarget-nav-time: ~D~%" (-> obj target-nav-time))
  obj
  )

;; failed to figure out what this is:
(defskelgroup *swamp-rat-sg* swamp-rat swamp-rat-lod0-jg swamp-rat-idle-ja
              ((swamp-rat-lod0-mg (meters 20)) (swamp-rat-lod1-mg (meters 40)) (swamp-rat-lod2-mg (meters 999999)))
              :bounds (static-spherem 0 1 0 4)
              :longest-edge (meters 1)
              )

;; definition for method 44 of type swamp-rat
(defmethod dummy-44 swamp-rat ((obj swamp-rat) (arg0 process) (arg1 event-message-block))
  (when ((method-of-type touching-shapes-entry prims-touching?)
         (the-as touching-shapes-entry (-> arg1 param 0))
         (-> obj collide-info)
         (the-as uint 1)
         )
    (when (nav-enemy-send-attack arg0 (the-as touching-shapes-entry (-> arg1 param 0)) 'generic)
      (let* ((s5-1 (-> obj parent))
             (a0-4 (if (and (nonzero? s5-1) (type-type? pointer process-drawable))
                       s5-1
                       )
                   )
             )
        (if a0-4
            (send-event (the-as process-tree a0-4) 'victory)
            (go (method-of-object obj nav-enemy-victory))
            )
        )
      )
    )
  )

;; definition for function swamp-rat-default-event-handler
;; INFO: Return type mismatch none vs object.
;; WARN: rewrite_to_get_var got a none typed variable. Is there unreachable code? [OP: 7]
(defbehavior swamp-rat-default-event-handler swamp-rat ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
  (let ((v1-0 arg2))
    (the-as object (if (= v1-0 'victory)
                       (go-virtual nav-enemy-victory)
                       (nav-enemy-default-event-handler arg0 arg1 arg2 arg3)
                       )
            )
    )
  )

;; failed to figure out what this is:
swamp-rat-default-event-handler

;; definition for method 39 of type swamp-rat
;; INFO: Used lq/sq
(defmethod common-post swamp-rat ((obj swamp-rat))
  (when (logtest? (-> obj collide-info status) (cshape-moving-flags onsurf))
    (vector-deg-seek (-> obj up-vector) (-> obj up-vector) (-> obj collide-info surface-normal) 910.2222)
    (vector-normalize! (-> obj up-vector) 1.0)
    )
  (forward-up-nopitch->quaternion
    (-> obj collide-info quat)
    (vector-z-quaternion! (new-stack-vector0) (-> obj collide-info quat))
    (-> obj up-vector)
    )
  ((the-as (function nav-enemy none) (find-parent-method swamp-rat 39)) obj)
  (none)
  )

;; definition for method 38 of type swamp-rat
;; INFO: Used lq/sq
;; INFO: Return type mismatch int vs none.
(defmethod TODO-RENAME-38 swamp-rat ((obj swamp-rat))
  (integrate-for-enemy-with-move-to-ground!
    (-> obj collide-info)
    (-> obj collide-info transv)
    (collide-kind background)
    8192.0
    #t
    #f
    #f
    )
  (when (< (-> obj collide-info trans y) (-> obj min-height))
    (let ((a1-1 (new 'stack-no-clear 'vector)))
      (set! (-> a1-1 quad) (-> obj collide-info trans quad))
      (set! (-> a1-1 y) (-> obj min-height))
      (move-to-ground-point! (-> obj collide-info) a1-1 (-> obj collide-info transv) *y-vector*)
      )
    )
  0
  (none)
  )

;; failed to figure out what this is:
(defstate nav-enemy-idle (swamp-rat)
  :virtual #t
  :event swamp-rat-default-event-handler
  :post (behavior ()
    (ja-post)
    (none)
    )
  )

;; failed to figure out what this is:
(defstate nav-enemy-patrol (swamp-rat)
  :virtual #t
  :event swamp-rat-default-event-handler
  :code (behavior ()
    (let ((f30-0 (nav-enemy-rnd-float-range 0.9 1.1)))
      (loop
        (ja-no-eval :group! (-> self draw art-group data (-> self nav-info walk-anim))
                    :num! (seek! max f30-0)
                    :frame-num 0.0
                    )
        (until (ja-done? 0)
          (suspend)
          (ja :num! (seek! max f30-0))
          )
        )
      )
    (none)
    )
  )

;; failed to figure out what this is:
(defstate nav-enemy-notice (swamp-rat)
  :virtual #t
  :event swamp-rat-default-event-handler
  :code (behavior ()
    (ja-no-eval :num! (loop!))
    (ja-channel-push! 1 (seconds 0.17))
    (ja-no-eval :group! swamp-rat-notice-ja :num! (seek! (ja-aframe 30.0 0)) :frame-num 0.0)
    (until (ja-done? 0)
      (ja-blend-eval)
      (suspend)
      (ja :num! (seek! (ja-aframe 30.0 0)))
      )
    (ja-no-eval :num! (seek!))
    (go-virtual nav-enemy-chase)
    (none)
    )
  )

;; definition for function swamp-rat-update-wiggle-params
(defbehavior swamp-rat-update-wiggle-params swamp-rat ()
  (let* ((f0-0 (rand-vu-float-range 0.0 1.0))
         (f1-1 (+ 1.0 (* 2.0 f0-0)))
         (f2-2 f1-1)
         (f2-4 (/ 1.0 f2-2))
         (f0-2 (+ 1.0 (* 0.2 f0-0)))
         )
    (set! (-> self delta-wiggle-angle) (* 910.2222 f1-1))
    (set! (-> self wiggle-factor) (* 1.5 f2-4))
    (let ((f0-3 (* 28672.0 f0-2)))
      (set! (-> self target-speed) f0-3)
      f0-3
      )
    )
  )

;; definition for function swamp-rat-update-wiggle-target
;; INFO: Used lq/sq
(defbehavior swamp-rat-update-wiggle-target swamp-rat ((arg0 vector))
  (+! (-> self wiggle-angle) (-> self delta-wiggle-angle))
  (if (< 65536.0 (-> self wiggle-angle))
      (set! (-> self wiggle-angle) (+ -65536.0 (-> self wiggle-angle)))
      )
  (let* ((v1-3 (-> self collide-info trans))
         (a1-1 (vector-! (new 'stack-no-clear 'vector) v1-3 arg0))
         (s3-0 (vector-rotate-around-y! (new 'stack-no-clear 'vector) a1-1 16384.0))
         (v1-4
           (vector+*! (new 'stack-no-clear 'vector) arg0 s3-0 (* (-> self wiggle-factor) (sin (-> self wiggle-angle))))
           )
         (v0-3 (-> self nav target-pos))
         )
    (set! (-> v0-3 quad) (-> v1-4 quad))
    v0-3
    )
  )

;; failed to figure out what this is:
(defstate nav-enemy-chase (swamp-rat)
  :virtual #t
  :event swamp-rat-default-event-handler
  :trans (behavior ()
    (if (>= (- (-> *display* base-frame-counter) (-> self state-time)) (-> self chase-rest-time))
        (go-virtual nav-enemy-victory)
        )
    ((-> (method-of-type nav-enemy nav-enemy-chase) trans))
    (none)
    )
  :code (behavior ()
    (set! (-> self target-nav-time) (-> *display* base-frame-counter))
    (set! (-> self wiggle-time) (+ (-> *display* base-frame-counter) (seconds -10)))
    (set! (-> self wiggle-angle) 0.0)
    (set! (-> self chase-rest-time) (rand-vu-int-range (seconds 1) (seconds 4)))
    (ja-channel-push! 1 (seconds 0.17))
    (ja :group! swamp-rat-run-ja :num! min)
    (loop
      (when (>= (- (-> *display* base-frame-counter) (-> self wiggle-time)) (seconds 1))
        (set! (-> self wiggle-time) (-> *display* base-frame-counter))
        (swamp-rat-update-wiggle-params)
        )
      (suspend)
      (ja :num! (loop!))
      )
    (none)
    )
  :post (behavior ()
    (swamp-rat-update-wiggle-target (target-pos 0))
    (nav-enemy-travel-post)
    (none)
    )
  )

;; failed to figure out what this is:
(defstate nav-enemy-stop-chase (swamp-rat)
  :virtual #t
  :event swamp-rat-default-event-handler
  :code (-> (method-of-type nav-enemy nav-enemy-stop-chase) code)
  )

;; failed to figure out what this is:
(defstate nav-enemy-stare (swamp-rat)
  :virtual #t
  :event swamp-rat-default-event-handler
  :code (behavior ()
    (set! (-> self rotate-speed) 1456355.5)
    (set! (-> self turn-time) (seconds 0.075))
    (let ((f30-0 (rand-vu-float-range 0.8 1.2)))
      (loop
        (logior! (-> self nav-enemy-flags) (nav-enemy-flags enable-travel))
        (ja-channel-push! 1 (seconds 0.1))
        (ja-no-eval :group! swamp-rat-celebrate-ja :num! (seek! max f30-0) :frame-num 0.0)
        (until (ja-done? 0)
          (suspend)
          (ja :num! (seek! max f30-0))
          )
        (ja-no-eval :num! (loop!))
        (logclear! (-> self nav-enemy-flags) (nav-enemy-flags enable-travel))
        (let ((gp-0 (rand-vu-int-range 300 600))
              (s5-0 (-> *display* base-frame-counter))
              )
          (until (>= (- (-> *display* base-frame-counter) s5-0) gp-0)
            (ja :num-func num-func-identity :frame-num 0.0)
            (ja-blend-eval)
            (suspend)
            (suspend)
            )
          )
        )
      )
    (none)
    )
  )

;; failed to figure out what this is:
(defstate nav-enemy-give-up (swamp-rat)
  :virtual #t
  :event swamp-rat-default-event-handler
  :code (behavior ()
    (ja-channel-push! 1 (seconds 0.075))
    (ja-no-eval :group! swamp-rat-idle-ja :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek!))
      )
    (logclear! (-> self nav flags) (nav-control-flags navcf17 navcf19))
    (nav-enemy-get-new-patrol-point)
    (ja-no-eval :group! swamp-rat-idle-ja :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (seek-to-point-toward-point!
        (-> self collide-info)
        (-> self nav destination-pos)
        (-> self rotate-speed)
        (-> self turn-time)
        )
      (suspend)
      (ja :num! (seek!))
      )
    (go-virtual nav-enemy-patrol)
    (none)
    )
  )

;; failed to figure out what this is:
(defstate nav-enemy-attack (swamp-rat)
  :virtual #t
  :event swamp-rat-default-event-handler
  :code (behavior ()
    (ja-channel-push! 1 (seconds 0.075))
    (ja-no-eval :group! swamp-rat-idle-ja :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek!))
      )
    (go-virtual nav-enemy-victory)
    (none)
    )
  )

;; failed to figure out what this is:
(defstate nav-enemy-victory (swamp-rat)
  :virtual #t
  :event swamp-rat-default-event-handler
  :code (-> (method-of-type nav-enemy nav-enemy-victory) code)
  )

;; failed to figure out what this is:
(defstate swamp-rat-spawn (swamp-rat)
  :event swamp-rat-default-event-handler
  :code (behavior ()
    (let ((gp-0 (new-stack-vector0)))
      (set! (-> gp-0 x) 0.0)
      (set! (-> gp-0 y) (- (-> *standard-dynamics* gravity-length)))
      (set! (-> gp-0 z) 0.0)
      (set! (-> gp-0 w) 1.0)
      (let ((f30-0 0.125))
        (set! (-> self state-time) (-> *display* base-frame-counter))
        (ja :group! swamp-rat-fall-ja :num! min)
        (loop
          (set! f30-0 (seek f30-0 1.5 0.1))
          (vector-v++! (-> self collide-info transv) gp-0)
          (integrate-for-enemy-with-move-to-ground!
            (-> self collide-info)
            (-> self collide-info transv)
            (collide-kind background)
            8192.0
            #t
            #f
            #f
            )
          (when (or (logtest? (-> self collide-info status) (cshape-moving-flags tsurf))
                    (or (< (-> self collide-info trans y) (-> self min-height))
                        (>= (- (-> *display* base-frame-counter) (-> self state-time)) (seconds 1))
                        )
                    )
            0
            (goto cfg-13)
            )
          (vector-float*! (-> self collide-info scale) *identity-vector* f30-0)
          (ja :num! (loop!))
          (suspend)
          )
        )
      )
    (label cfg-13)
    (if (< (-> self collide-info trans y) (-> self min-height))
        (set! (-> self collide-info trans y) (-> self min-height))
        )
    (set! (-> self collide-info transv quad) (-> *null-vector* quad))
    (vector-float*! (-> self collide-info scale) *identity-vector* 1.5)
    (ja-no-eval :group! swamp-rat-bounce-ja :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek!))
      )
    (if (TODO-RENAME-46 self (-> self nav-info stop-chase-distance))
        (go-virtual nav-enemy-chase)
        (go-virtual nav-enemy-idle)
        )
    (none)
    )
  :post (the-as (function none :behavior swamp-rat) nav-enemy-simple-post)
  )

;; definition for symbol *swamp-rat-nav-enemy-info*, type nav-enemy-info
(define *swamp-rat-nav-enemy-info* (new 'static 'nav-enemy-info
                                     :idle-anim 4
                                     :walk-anim 7
                                     :turn-anim 7
                                     :notice-anim 8
                                     :run-anim 9
                                     :jump-anim 9
                                     :jump-land-anim 6
                                     :victory-anim 10
                                     :taunt-anim 10
                                     :die-anim 11
                                     :neck-joint 7
                                     :run-travel-speed (meters 7)
                                     :run-rotate-speed (degrees 7999.9995)
                                     :run-acceleration (meters 1)
                                     :run-turn-time (seconds 0.075)
                                     :walk-travel-speed (meters 2)
                                     :walk-rotate-speed (degrees 7999.9995)
                                     :walk-acceleration (meters 1)
                                     :walk-turn-time (seconds 0.075)
                                     :attack-shove-back (meters 3)
                                     :attack-shove-up (meters 2)
                                     :shadow-size (meters 1)
                                     :notice-nav-radius (meters 1)
                                     :nav-nearest-y-threshold (meters 10)
                                     :notice-distance (meters 30)
                                     :stop-chase-distance (meters 40)
                                     :frustration-distance (meters 3)
                                     :frustration-time (seconds 1.5)
                                     :die-anim-hold-frame 20.0
                                     :jump-anim-start-frame 19.0
                                     :jump-land-anim-end-frame 10000000000.0
                                     :jump-height-min (meters 1)
                                     :jump-height-factor 0.5
                                     :jump-start-anim-speed 1.0
                                     :shadow-max-y (meters 1)
                                     :shadow-min-y (meters -1)
                                     :shadow-locus-dist (meters 150)
                                     :use-align #f
                                     :draw-shadow #f
                                     :move-to-ground #t
                                     :hover-if-no-ground #f
                                     :use-momentum #f
                                     :use-flee #f
                                     :use-proximity-notice #f
                                     :use-jump-blocked #f
                                     :use-jump-patrol #f
                                     :gnd-collide-with (collide-kind background)
                                     :debug-draw-neck #f
                                     :debug-draw-jump #f
                                     )
        )

;; definition for method 47 of type swamp-rat
;; INFO: Return type mismatch int vs none.
(defmethod initialize-collision swamp-rat ((obj swamp-rat))
  (let ((s5-0 (new 'process 'collide-shape-moving obj (collide-list-enum usually-hit-by-player))))
    (set! (-> s5-0 dynam) (copy *standard-dynamics* 'process))
    (set! (-> s5-0 reaction) default-collision-reaction)
    (set! (-> s5-0 no-reaction)
          (the-as (function collide-shape-moving collide-shape-intersect vector vector none) nothing)
          )
    (let ((s4-0 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 3))))
      (set! (-> s4-0 prim-core collide-as) (collide-kind enemy))
      (set! (-> s4-0 collide-with) (collide-kind target))
      (set! (-> s4-0 prim-core action) (collide-action solid))
      (set! (-> s4-0 prim-core offense) (collide-offense normal-attack))
      (set-vector! (-> s4-0 local-sphere) 0.0 3686.4001 0.0 3686.4001)
      (set-root-prim! s5-0 s4-0)
      )
    (set! (-> s5-0 nav-radius) (* 0.75 (-> s5-0 root-prim local-sphere w)))
    (backup-collide-with-as s5-0)
    (set! (-> obj collide-info) s5-0)
    )
  0
  (none)
  )

;; definition for method 48 of type swamp-rat
;; INFO: Used lq/sq
;; INFO: Return type mismatch int vs none.
(defmethod TODO-RENAME-48 swamp-rat ((obj swamp-rat))
  (initialize-skeleton obj *swamp-rat-sg* '())
  (TODO-RENAME-45 obj *swamp-rat-nav-enemy-info*)
  (vector-float*! (-> obj collide-info scale) *identity-vector* 1.5)
  (set! (-> obj neck up) (the-as uint 1))
  (set! (-> obj neck nose) (the-as uint 2))
  (set! (-> obj neck ear) (the-as uint 0))
  (set! (-> obj wiggle-angle) 0.0)
  (set! (-> obj delta-wiggle-angle) 910.2222)
  (set! (-> obj wiggle-factor) 1.5)
  (set! (-> obj reaction-time) (rand-vu-int-range (seconds 0.1) (seconds 0.8)))
  (set! (-> obj chase-rest-time) (seconds 1))
  (set! (-> obj water) (new 'process 'water-control obj 7 0.0 8192.0 2048.0))
  (set! (-> obj water flags) (water-flags wt01))
  (set! (-> obj water height) (res-lump-float (-> obj entity) 'water-height))
  (set! (-> obj water ripple-size) 12288.0)
  (set! (-> obj min-height) (+ -2048.0 (-> obj water height)))
  (set! (-> obj up-vector quad) (-> *y-vector* quad))
  0
  (none)
  )

;; definition for function swamp-rat-init-by-other
;; INFO: Used lq/sq
;; INFO: Return type mismatch object vs none.
(defbehavior swamp-rat-init-by-other swamp-rat ((arg0 billy) (arg1 vector) (arg2 vector) (arg3 pickup-type) (arg4 symbol))
  (initialize-collision self)
  (if arg4
      (logclear! (-> self mask) (process-mask actor-pause))
      (logior! (-> self mask) (process-mask actor-pause))
      )
  (set! (-> self collide-info trans quad) (-> arg1 quad))
  (forward-up->quaternion (-> self collide-info quat) arg2 *up-vector*)
  (vector-float*! (-> self collide-info scale) *identity-vector* 1.5)
  (vector-float*! (-> self collide-info transv) arg2 49152.0)
  (set! (-> self entity) (-> arg0 entity))
  (TODO-RENAME-48 self)
  (set! (-> self enemy-info pickup-type) arg3)
  (go swamp-rat-spawn)
  (none)
  )
