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

(import "goal_src/jak1/import/village1cam-ag.gc")
(import "goal_src/jak1/import/yakow-ag.gc")

;; failed to figure out what this is:
(defskelgroup *village1cam-sg* village1cam village1cam-lod0-jg village1cam-anim-ja
              ((village1cam-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 4)
              )

;; definition for function yakow-cam
;; INFO: Return type mismatch (pointer process) vs none.
(defun yakow-cam ()
  (with-pp
    (let ((gp-0 (entity-actor-lookup (-> pp entity) 'alt-actor 0)))
      (if gp-0
          (process-spawn pov-camera (-> gp-0 extra trans) *village1cam-sg* "anim" 0 #f '() :to pp)
          )
      )
    (none)
    )
  )

;; definition of type yakow-bank
(deftype yakow-bank (basic)
  ((walk-cycle-frame-count float       :offset-assert   4)
   (run-cycle-frame-count  float       :offset-assert   8)
   (walk-speed             meters      :offset-assert  12)
   (run-speed              meters      :offset-assert  16)
   (walk-anim-speed        float       :offset-assert  20)
   (run-anim-speed         float       :offset-assert  24)
   (walk-away-dist         meters      :offset-assert  28)
   (run-away-dist          meters      :offset-assert  32)
   (walk-rotate-speed      float       :offset-assert  36)
   (run-rotate-speed       float       :offset-assert  40)
   (walk-turn-time         time-frame  :offset-assert  48)
   (run-turn-time          time-frame  :offset-assert  56)
   (max-walk-speed         float       :offset-assert  64)
   (min-run-speed          float       :offset-assert  68)
   (walk-run-blend-rate    float       :offset-assert  72)
   (walk-turn-blend-rate   float       :offset-assert  76)
   (max-run-speed          float       :offset-assert  80)
   (acceleration           meters      :offset-assert  84)
   (default-patrol-time    time-frame  :offset-assert  88)
   (default-idle-distance  meters      :offset-assert  96)
   (safe-distance          meters      :offset-assert 100)
   (min-run-anim-speed     float       :offset-assert 104)
   (max-run-anim-speed     float       :offset-assert 108)
   (min-walk-anim-speed    float       :offset-assert 112)
   (speed-boost-impulse    meters      :offset-assert 116)
   )
  :method-count-assert 9
  :size-assert         #x78
  :flag-assert         #x900000078
  )

;; definition for method 3 of type yakow-bank
(defmethod inspect yakow-bank ((obj yakow-bank))
  (format #t "[~8x] ~A~%" obj (-> obj type))
  (format #t "~Twalk-cycle-frame-count: ~f~%" (-> obj walk-cycle-frame-count))
  (format #t "~Trun-cycle-frame-count: ~f~%" (-> obj run-cycle-frame-count))
  (format #t "~Twalk-speed: (meters ~m)~%" (-> obj walk-speed))
  (format #t "~Trun-speed: (meters ~m)~%" (-> obj run-speed))
  (format #t "~Twalk-anim-speed: ~f~%" (-> obj walk-anim-speed))
  (format #t "~Trun-anim-speed: ~f~%" (-> obj run-anim-speed))
  (format #t "~Twalk-away-dist: (meters ~m)~%" (-> obj walk-away-dist))
  (format #t "~Trun-away-dist: (meters ~m)~%" (-> obj run-away-dist))
  (format #t "~Twalk-rotate-speed: ~f~%" (-> obj walk-rotate-speed))
  (format #t "~Trun-rotate-speed: ~f~%" (-> obj run-rotate-speed))
  (format #t "~Twalk-turn-time: ~D~%" (-> obj walk-turn-time))
  (format #t "~Trun-turn-time: ~D~%" (-> obj run-turn-time))
  (format #t "~Tmax-walk-speed: ~f~%" (-> obj max-walk-speed))
  (format #t "~Tmin-run-speed: ~f~%" (-> obj min-run-speed))
  (format #t "~Twalk-run-blend-rate: ~f~%" (-> obj walk-run-blend-rate))
  (format #t "~Twalk-turn-blend-rate: ~f~%" (-> obj walk-turn-blend-rate))
  (format #t "~Tmax-run-speed: ~f~%" (-> obj max-run-speed))
  (format #t "~Tacceleration: (meters ~m)~%" (-> obj acceleration))
  (format #t "~Tdefault-patrol-time: ~D~%" (-> obj default-patrol-time))
  (format #t "~Tdefault-idle-distance: (meters ~m)~%" (-> obj default-idle-distance))
  (format #t "~Tsafe-distance: (meters ~m)~%" (-> obj safe-distance))
  (format #t "~Tmin-run-anim-speed: ~f~%" (-> obj min-run-anim-speed))
  (format #t "~Tmax-run-anim-speed: ~f~%" (-> obj max-run-anim-speed))
  (format #t "~Tmin-walk-anim-speed: ~f~%" (-> obj min-walk-anim-speed))
  (format #t "~Tspeed-boost-impulse: (meters ~m)~%" (-> obj speed-boost-impulse))
  obj
  )

;; definition for symbol *YAKOW-bank*, type yakow-bank
(define *YAKOW-bank* (new 'static 'yakow-bank
                       :walk-cycle-frame-count 16.0
                       :run-cycle-frame-count 16.0
                       :walk-speed (meters 2.13)
                       :run-speed (meters 6)
                       :walk-anim-speed 1.0
                       :run-anim-speed 2.0
                       :walk-away-dist (meters 10)
                       :run-away-dist (meters 6)
                       :walk-rotate-speed 131072.0
                       :run-rotate-speed 131072.0
                       :walk-turn-time (seconds 0.5)
                       :run-turn-time (seconds 0.2)
                       :max-walk-speed 16384.0
                       :min-run-speed 15155.2
                       :walk-run-blend-rate 6.0
                       :walk-turn-blend-rate 4.0
                       :max-run-speed 61440.0
                       :acceleration (meters 3)
                       :default-patrol-time (seconds 1)
                       :default-idle-distance (meters 10)
                       :safe-distance (meters 20)
                       :min-run-anim-speed 0.5
                       :max-run-anim-speed 1.5
                       :min-walk-anim-speed 0.5
                       :speed-boost-impulse (meters 4)
                       )
        )

;; definition of type yakow
(deftype yakow (process-drawable)
  ((root-override      collide-shape-moving         :offset        112)
   (fact-override      fact-info-enemy              :offset        144)
   (player-attack-id   int32                        :offset-assert 176)
   (walk-run-blend     float                        :offset-assert 180)
   (walk-turn-blend    float                        :offset-assert 184)
   (run-mode           basic                        :offset-assert 188)
   (travel-speed       meters                       :offset-assert 192)
   (final-speed        meters                       :offset-assert 196)
   (rotate-speed       float                        :offset-assert 200)
   (turn-time          time-frame                   :offset-assert 208)
   (vulnerable         basic                        :offset-assert 216)
   (grazing            basic                        :offset-assert 220)
   (push-velocity      vector               :inline :offset-assert 224)
   (home-base          vector               :inline :offset-assert 240)
   (dest-base          vector               :inline :offset-assert 256)
   (dest-rot           degrees                      :offset-assert 272)
   (enable-turn-around basic                        :offset-assert 276)
   (rotating           basic                        :offset-assert 280)
   (in-pen             basic                        :offset-assert 284)
   )
  :heap-base #xb0
  :method-count-assert 20
  :size-assert         #x120
  :flag-assert         #x1400b00120
  (:states
    yakow-die
    yakow-graze
    yakow-graze-kicked
    yakow-idle
    yakow-kicked
    yakow-notice
    yakow-run-away
    (yakow-walk-to vector)
    )
  )

;; definition for method 3 of type yakow
(defmethod inspect yakow ((obj yakow))
  (let ((t9-0 (method-of-type process-drawable inspect)))
    (t9-0 obj)
    )
  (format #t "~T~Tplayer-attack-id: ~D~%" (-> obj player-attack-id))
  (format #t "~T~Twalk-run-blend: ~f~%" (-> obj walk-run-blend))
  (format #t "~T~Twalk-turn-blend: ~f~%" (-> obj walk-turn-blend))
  (format #t "~T~Trun-mode: ~A~%" (-> obj run-mode))
  (format #t "~T~Ttravel-speed: (meters ~m)~%" (-> obj travel-speed))
  (format #t "~T~Tfinal-speed: (meters ~m)~%" (-> obj final-speed))
  (format #t "~T~Trotate-speed: ~f~%" (-> obj rotate-speed))
  (format #t "~T~Tturn-time: ~D~%" (-> obj turn-time))
  (format #t "~T~Tvulnerable: ~A~%" (-> obj vulnerable))
  (format #t "~T~Tgrazing: ~A~%" (-> obj grazing))
  (format #t "~T~Tpush-velocity: #<vector @ #x~X>~%" (-> obj push-velocity))
  (format #t "~T~Thome-base: ~`vector`P~%" (-> obj home-base))
  (format #t "~T~Tdest-base: ~`vector`P~%" (-> obj dest-base))
  (format #t "~T~Tdest-rot: (deg ~r)~%" (-> obj dest-rot))
  (format #t "~T~Tenable-turn-around: ~A~%" (-> obj enable-turn-around))
  (format #t "~T~Trotating: ~A~%" (-> obj rotating))
  (format #t "~T~Tin-pen: ~A~%" (-> obj in-pen))
  obj
  )

;; failed to figure out what this is:
(defskelgroup *yakow-sg* yakow yakow-lod0-jg yakow-idle-ja
              ((yakow-lod0-mg (meters 20)) (yakow-lod1-mg (meters 999999)))
              :bounds (static-spherem 0 2.5 0 4.4)
              :longest-edge (meters 1.3)
              :shadow yakow-shadow-mg
              )

;; definition for function yakow-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: 52]
;; WARN: rewrite_to_get_var got a none typed variable. Is there unreachable code? [OP: 54]
(defbehavior yakow-default-event-handler yakow ((arg0 process-drawable) (arg1 int) (arg2 symbol) (arg3 event-message-block))
  (let ((v1-0 arg2))
    (the-as object (cond
                     ((= v1-0 'touch)
                      (let* ((s4-0 arg0)
                             (v1-2 (if (and (nonzero? s4-0) (type-type? (-> s4-0 type) process-drawable))
                                       s4-0
                                       )
                                   )
                             )
                        (when (and v1-2 (< 8192.0 (- (-> v1-2 root trans y) (-> self root-override trans y))))
                          (do-push-aways! (-> self root-override))
                          (send-shove-back
                            (-> self root-override)
                            arg0
                            (the-as touching-shapes-entry (-> arg3 param 0))
                            0.7
                            6144.0
                            16384.0
                            )
                          )
                        )
                      )
                     ((= v1-0 'attack)
                      (let ((v1-9 (-> arg3 param 2)))
                        (cond
                          ((!= v1-9 (-> self player-attack-id))
                           (set! (-> self player-attack-id) (the-as int v1-9))
                           (if (-> self vulnerable)
                               (go yakow-kicked)
                               )
                           )
                          (else
                            'push
                            )
                          )
                        )
                      )
                     )
            )
    )
  )

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

;; definition for function yakow-task-complete?
(defbehavior yakow-task-complete? yakow ((arg0 symbol))
  (local-vars (sv-16 symbol) (sv-20 (pointer process-tree)))
  (let ((gp-0
          (not (and (-> self entity) (logtest? (-> self entity extra perm status) (entity-perm-status complete))))
          )
        )
    (if arg0
        (process-entity-status! self (entity-perm-status complete) #t)
        (process-entity-status! self (entity-perm-status complete) #f)
        )
    (when (or (-> self link prev) (-> self link next))
      (set! sv-16 (the-as symbol #f))
      (set! sv-20 (-> self child))
      (apply-all (-> self link) actor-link-subtask-complete-hook (& sv-16))
      (cond
        (sv-16
          (close-specific-task! (-> self entity extra perm task) (task-status need-hint))
          (close-specific-task! (-> self entity extra perm task) (task-status need-introduction))
          (close-specific-task! (-> self entity extra perm task) (task-status need-reminder))
          (if gp-0
              (yakow-cam)
              )
          (level-hint-spawn
            (game-text-id village1-yakow-resolution)
            "sksp018a"
            (the-as entity #f)
            *entity-pool*
            (game-task none)
            )
          )
        (else
          (open-specific-task! (-> self entity extra perm task) (task-status need-reminder))
          )
        )
      sv-16
      )
    )
  )

;; definition for function yakow-common-post
(defbehavior yakow-common-post yakow ()
  (update-direction-from-time-of-day (-> self draw shadow-ctrl))
  (if *target*
      (look-at-enemy! (-> *target* neck) (the-as vector (-> self root-override root-prim prim-core)) #f self)
      )
  (ja-post)
  (none)
  )

;; definition for function yakow-simple-post
;; INFO: Return type mismatch symbol vs none.
(defbehavior yakow-simple-post yakow ()
  (yakow-common-post)
  (update-transforms! (-> self root-override))
  (none)
  )

;; definition for function yakow-post
;; Used lq/sq
(defbehavior yakow-post yakow ()
  (dummy-11 (-> self nav) (-> self nav target-pos))
  (set! (-> self rotating) #f)
  (let ((f30-0 0.0))
    (when (< 40.96 (vector-length (-> self nav travel)))
      (let ((s5-0 (new 'stack-no-clear 'vector))
            (gp-0 (new 'stack-no-clear 'vector))
            )
        (vector-z-quaternion! s5-0 (-> self root-override quat))
        (seek-toward-heading-vec!
          (-> self root-override)
          (-> self nav travel)
          (-> self rotate-speed)
          (-> self turn-time)
          )
        (vector-z-quaternion! gp-0 (-> self root-override quat))
        (set! (-> self rotating) (< (vector-dot gp-0 s5-0) (cos (* 8192.0 (-> *display* seconds-per-frame)))))
        (when (-> self rotating)
          (let ((v1-16 (new 'stack-no-clear 'vector)))
            (vector-cross! v1-16 gp-0 s5-0)
            (set! f30-0 (sign (-> v1-16 y)))
            )
          )
        )
      )
    (seek!
      (-> self walk-turn-blend)
      f30-0
      (* (-> *YAKOW-bank* walk-turn-blend-rate) (-> *display* seconds-per-frame))
      )
    )
  (set! (-> self final-speed)
        (fmin (-> self travel-speed) (* (vector-length (-> self nav travel)) (-> *display* frames-per-second)))
        )
  (let ((v1-24 (vector-normalize-copy! (new-stack-vector0) (-> self nav travel) (-> self final-speed))))
    (set! (-> self root-override transv x) (-> v1-24 x))
    (set! (-> self root-override transv z) (-> v1-24 z))
    )
  (vector-v++!
    (-> self root-override transv)
    (compute-acc-due-to-gravity (-> self root-override) (new-stack-vector0) 0.0)
    )
  (let ((gp-2 (new 'stack-no-clear 'vector)))
    (set! (-> gp-2 quad) (-> self root-override trans quad))
    (integrate-for-enemy-with-move-to-ground!
      (-> self root-override)
      (-> self root-override transv)
      (collide-kind background)
      8192.0
      #t
      #f
      #f
      )
    (set! (-> self final-speed)
          (* (vector-vector-xz-distance gp-2 (-> self root-override trans)) (-> *display* frames-per-second))
          )
    )
  (set! (-> self travel-speed) (fmin (-> self travel-speed) (-> self final-speed)))
  (dummy-10 (-> self water))
  (when (and (logtest? (-> self nav current-poly pat) 2)
             (not (and (-> self entity) (logtest? (-> self entity extra perm status) (entity-perm-status complete))))
             )
    (set! (-> self in-pen) #t)
    (set! (-> self nav destination-pos quad) (-> self dest-base quad))
    (yakow-task-complete? #t)
    )
  (yakow-common-post)
  (none)
  )

;; definition for function yakow-generate-travel-vector
;; Used lq/sq
(defbehavior yakow-generate-travel-vector yakow ()
  (let ((gp-1 (vector-! (new 'stack-no-clear 'vector) (-> self root-override trans) (target-pos 0)))
        (s5-1 (vector-z-quaternion! (new 'stack-no-clear 'vector) (-> self root-override quat)))
        )
    0.0
    0.0
    (set! (-> gp-1 y) 0.0)
    (set! (-> self nav travel quad) (-> gp-1 quad))
    (vector-normalize! (-> self nav travel) 409600.0)
    (dummy-24 (-> self nav) 204.8 (the-as clip-travel-vector-to-mesh-return-info #f))
    (when (not (-> self enable-turn-around))
      (set! (-> gp-1 quad) (-> self nav travel quad))
      (let ((f30-0 (vector-length gp-1)))
        (vector-normalize! gp-1 1.0)
        (if (and (< 409.6 f30-0) (>= (vector-dot s5-1 gp-1) (cos 10922.667)))
            (set! (-> s5-1 quad) (-> gp-1 quad))
            )
        )
      (set! (-> self nav travel quad) (-> s5-1 quad))
      (vector-normalize! (-> self nav travel) 409600.0)
      (dummy-24 (-> self nav) 204.8 (the-as clip-travel-vector-to-mesh-return-info #f))
      (vector-length (-> self nav travel))
      )
    )
  (vector+! (-> self nav target-pos) (-> self root-override trans) (-> self nav travel))
  )

;; definition for function yakow-run-post
(defbehavior yakow-run-post yakow ()
  (yakow-generate-travel-vector)
  (yakow-post)
  (none)
  )

;; definition for function yakow-blend-walk-run
(defbehavior yakow-blend-walk-run yakow ()
  (let ((gp-0 (-> *display* base-frame-counter)))
    (loop
      (let ((f30-0 (-> self final-speed)))
        (cond
          ((and (>= 409.6 (-> self final-speed)) (not (-> self rotating)))
           (if (not (ja-group? yakow-idle-ja))
               (ja-channel-push! 1 (seconds 0.15))
               )
           (ja :group! yakow-idle-ja :num! min)
           (while (or (< (- (-> *display* base-frame-counter) gp-0) (seconds 0.2)) (< (-> self final-speed) 409.6))
             (suspend)
             (ja :num! (loop!))
             )
           )
          (else
            (set! gp-0 (-> *display* base-frame-counter))
            )
          )
        (when (not (ja-group? yakow-walk-ja))
          (ja-channel-push! 3 (seconds 0.075))
          (ja :group! yakow-walk-ja :num! min :frame-interp 0.0)
          (ja :chan 1 :group! yakow-walk-left-ja :num! min :frame-interp 0.0)
          (ja :chan 2 :group! yakow-run-ja :num! min :frame-interp 0.0)
          )
        (if (< (-> self walk-turn-blend) 0.0)
            (ja :chan 1 :group! yakow-walk-left-ja)
            (ja :chan 1 :group! yakow-walk-right-ja)
            )
        (cond
          ((< f30-0 (-> *YAKOW-bank* min-run-speed))
           (set! (-> self run-mode) #f)
           )
          ((< (-> *YAKOW-bank* max-walk-speed) f30-0)
           (set! (-> self run-mode) #t)
           )
          )
        (if (-> self run-mode)
            (seek!
              (-> self walk-run-blend)
              1.0
              (* (-> *YAKOW-bank* walk-run-blend-rate) (-> *display* seconds-per-frame))
              )
            (seek!
              (-> self walk-run-blend)
              0.0
              (* (-> *YAKOW-bank* walk-run-blend-rate) (-> *display* seconds-per-frame))
              )
            )
        (ja :chan 1 :frame-interp (fabs (-> self walk-turn-blend)))
        (ja :chan 2 :frame-interp (-> self walk-run-blend))
        (let* ((f0-25 (* (-> *YAKOW-bank* walk-anim-speed) f30-0))
               (f1-5 (-> *YAKOW-bank* walk-speed))
               (f0-26 (* f0-25 (/ 1.0 f1-5)))
               )
          (let* ((f1-9 (* (-> *YAKOW-bank* run-anim-speed) f30-0))
                 (f2-1 (-> *YAKOW-bank* run-speed))
                 (f1-12
                   (fmax (fmin (* f1-9 (/ 1.0 f2-1)) (-> *YAKOW-bank* max-run-anim-speed)) (-> *YAKOW-bank* min-run-anim-speed))
                   )
                 )
            (if (-> self run-mode)
                (set! f0-26 f1-12)
                )
            )
          (if (-> self rotating)
              (set! f0-26 (fmax (-> *YAKOW-bank* min-walk-anim-speed) f0-26))
              )
          (ja :num! (loop! f0-26))
          )
        )
      (ja :chan 1 :num-func num-func-identity :frame-num (ja-frame-num 0))
      (ja :chan 2 :num-func num-func-identity :frame-num (ja-frame-num 0))
      (suspend)
      0
      )
    )
  (none)
  )

;; definition for function yakow-facing-direction?
;; Used lq/sq
(defbehavior yakow-facing-direction? yakow ((arg0 vector) (arg1 float))
  (let ((s4-0 (vector-z-quaternion! (new 'stack-no-clear 'vector) (-> self root-override quat)))
        (s5-0 (new 'stack-no-clear 'vector))
        )
    (set! (-> s5-0 quad) (-> arg0 quad))
    (set! (-> s5-0 y) 0.0)
    (vector-normalize! s5-0 1.0)
    (>= (vector-dot s4-0 s5-0) (cos arg1))
    )
  )

;; definition for function yakow-facing-point?
(defbehavior yakow-facing-point? yakow ((arg0 vector) (arg1 float))
  (let ((v1-1 (vector-! (new 'stack-no-clear 'vector) arg0 (-> self root-override trans))))
    (yakow-facing-direction? v1-1 arg1)
    )
  )

;; definition for function yakow-facing-player?
(defbehavior yakow-facing-player? yakow ((arg0 float))
  (yakow-facing-point? (target-pos 0) arg0)
  )

;; failed to figure out what this is:
(defstate yakow-idle (yakow)
  :event yakow-default-event-handler
  :enter (behavior ()
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (set! (-> self travel-speed) 0.0)
    (none)
    )
  :trans (behavior ()
    (if (and (>= (- (-> *display* base-frame-counter) (-> self state-time)) (-> *YAKOW-bank* default-patrol-time))
             (and *target* (>= (-> self fact-override idle-distance)
                               (vector-vector-distance (-> self root-override trans) (-> *target* control trans))
                               )
                  )
             (not (yakow-facing-player? 21845.334))
             )
        (go yakow-notice)
        )
    (none)
    )
  :code (behavior ()
    (ja-channel-push! 1 (seconds 0.15))
    (loop
      (cond
        ((rand-vu-percent? 0.2)
         (dummy-10 (-> self skel effect) 'yakow-1 0.0 -1)
         )
        ((rand-vu-percent? 0.2)
         (dummy-10 (-> self skel effect) 'yakow-2 0.0 -1)
         )
        )
      (let ((f30-0 (rand-vu-float-range 0.9 1.1)))
        (ja-no-eval :group! yakow-idle-ja :num! (seek! max f30-0) :frame-num 0.0)
        (until (ja-done? 0)
          (suspend)
          (ja :num! (seek! max f30-0))
          )
        )
      (when (rand-vu-percent? 0.25)
        (ja-channel-push! 1 (seconds 0.1))
        (ja-no-eval :group! yakow-graze-ja :num! (seek!) :frame-num 0.0)
        (until (ja-done? 0)
          (suspend)
          (ja :num! (seek!))
          )
        (ja-channel-push! 1 (seconds 0.1))
        )
      )
    (none)
    )
  :post (the-as (function none :behavior yakow) ja-post)
  )

;; failed to figure out what this is:
(defstate yakow-notice (yakow)
  :event yakow-default-event-handler
  :enter (behavior ()
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (set! (-> self travel-speed) 0.0)
    (none)
    )
  :code (behavior ()
    (go yakow-run-away)
    (none)
    )
  :post yakow-simple-post
  )

;; failed to figure out what this is:
(defstate yakow-walk-to (yakow)
  :event yakow-default-event-handler
  :enter (behavior ((arg0 vector))
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (logior! (-> self nav flags) (nav-control-flags navcf10))
    (set! (-> self nav destination-pos quad) (-> arg0 quad))
    (set! (-> self rotate-speed) (-> *YAKOW-bank* walk-rotate-speed))
    (set! (-> self turn-time) (-> *YAKOW-bank* walk-turn-time))
    (none)
    )
  :trans (behavior ()
    (if (and (>= (- (-> *display* base-frame-counter) (-> self state-time)) (-> *YAKOW-bank* default-patrol-time))
             (not (-> self in-pen))
             (and *target*
                  (>= (-> self fact-override idle-distance)
                      (vector-vector-distance (-> self root-override trans) (-> *target* control trans))
                      )
                  )
             (not (yakow-facing-player? 21845.334))
             )
        (go yakow-notice)
        )
    (when (>= (- (-> *display* base-frame-counter) (-> self state-time)) (seconds 0.05))
      (when (or (logtest? (nav-control-flags navcf19) (-> self nav flags))
                (< (vector-vector-xz-distance (-> self root-override trans) (-> self nav destination-pos)) 4096.0)
                )
        (if (-> self in-pen)
            (go yakow-graze)
            (go yakow-idle)
            )
        )
      )
    (seek!
      (-> self travel-speed)
      (-> *YAKOW-bank* walk-speed)
      (* (-> *YAKOW-bank* acceleration) (-> *display* seconds-per-frame))
      )
    (none)
    )
  :code (the-as (function vector none :behavior yakow) yakow-blend-walk-run)
  :post (behavior ()
    (dummy-19
      (-> self nav)
      (-> self nav target-pos)
      (-> self root-override)
      (-> self nav destination-pos)
      131072.0
      )
    (if (logtest? (nav-control-flags navcf21) (-> self nav flags))
        (logclear! (-> self nav flags) (nav-control-flags navcf10))
        )
    (yakow-post)
    (none)
    )
  )

;; failed to figure out what this is:
(defstate yakow-graze (yakow)
  :event yakow-default-event-handler
  :enter (behavior ()
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (set! (-> self travel-speed) 0.0)
    (set! (-> self grazing) #t)
    (let ((v1-3 (entity-actor-lookup (-> self entity) 'alt-actor 0)))
      (send-event
        (if v1-3
            (-> v1-3 extra process)
            )
        'update
        )
      )
    (none)
    )
  :code (behavior ()
    (while (< 546.13336 (fabs (deg-diff (-> self dest-rot) (y-angle (-> self root-override)))))
      (if (not (ja-group? yakow-walk-ja))
          (ja-channel-push! 1 (seconds 0.075))
          )
      (seek-toward-yaw-angle! (-> self root-override) (-> self dest-rot) 16384.0 (seconds 0.5))
      (ja :group! yakow-walk-ja :num! (loop!))
      (suspend)
      )
    (ja-channel-push! 1 (seconds 0.15))
    (loop
      (ja-no-eval :group! yakow-idle-ja :num! (seek!) :frame-num 0.0)
      (until (ja-done? 0)
        (suspend)
        (ja :num! (seek!))
        )
      (when (rand-vu-percent? 0.5)
        (ja-channel-push! 1 (seconds 0.1))
        (ja-no-eval :group! yakow-graze-ja :num! (seek!) :frame-num 0.0)
        (until (ja-done? 0)
          (suspend)
          (ja :num! (seek!))
          )
        (ja-channel-push! 1 (seconds 0.1))
        )
      )
    (none)
    )
  :post yakow-simple-post
  )

;; failed to figure out what this is:
(defstate yakow-graze-kicked (yakow)
  :event (the-as (function process int symbol event-message-block object :behavior yakow) #f)
  :code (behavior ()
    (ja-no-eval :group! yakow-kicked-in-place-ja :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek!))
      )
    (go yakow-graze)
    (none)
    )
  :post yakow-simple-post
  )

;; failed to figure out what this is:
(defstate yakow-run-away (yakow)
  :event yakow-default-event-handler
  :enter (behavior ()
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (logior! (-> self nav flags) (nav-control-flags navcf10))
    (set! (-> self rotate-speed) (-> *YAKOW-bank* run-rotate-speed))
    (set! (-> self turn-time) (-> *YAKOW-bank* run-turn-time))
    (none)
    )
  :trans (behavior ()
    (when (or (not *target*) (< (-> *YAKOW-bank* safe-distance)
                                (vector-vector-distance (-> self root-override trans) (-> *target* control trans))
                                )
              )
      (if (-> self in-pen)
          (go yakow-walk-to (-> self dest-base))
          (go yakow-walk-to (-> self home-base))
          )
      )
    (let* ((f0-2 (* (if (-> self run-mode)
                        0.5
                        1.0
                        )
                    (vector-vector-distance (-> self root-override trans) (target-pos 0))
                    )
                 )
           (f30-2 (lerp-scale
                    (-> *YAKOW-bank* run-speed)
                    (-> *YAKOW-bank* walk-speed)
                    f0-2
                    (-> *YAKOW-bank* run-away-dist)
                    (-> *YAKOW-bank* walk-away-dist)
                    )
                  )
           )
      (if (yakow-facing-player? 21845.334)
          (set! f30-2 0.0)
          )
      (set! (-> self enable-turn-around) (< (-> *YAKOW-bank* run-speed) f30-2))
      (seek! (-> self travel-speed) f30-2 (* (-> *YAKOW-bank* acceleration) (-> *display* seconds-per-frame)))
      )
    (none)
    )
  :code yakow-blend-walk-run
  :post yakow-run-post
  )

;; failed to figure out what this is:
(defstate yakow-kicked (yakow)
  :event (the-as (function process int symbol event-message-block object :behavior yakow) #f)
  :enter (behavior ()
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (if (-> self grazing)
        (go yakow-graze-kicked)
        )
    (set! (-> self travel-speed)
          (fmax
            (fmin (+ (-> self travel-speed) (-> *YAKOW-bank* speed-boost-impulse)) (-> *YAKOW-bank* max-run-speed))
            (-> *YAKOW-bank* run-speed)
            )
          )
    (none)
    )
  :exit (behavior ()
    '()
    (none)
    )
  :trans (behavior ()
    (if (>= (- (-> *display* base-frame-counter) (-> self state-time)) (seconds 0.2))
        (seek!
          (-> self travel-speed)
          (-> *YAKOW-bank* run-speed)
          (* (-> *YAKOW-bank* acceleration) (-> *display* seconds-per-frame))
          )
        )
    0
    (none)
    )
  :code (behavior ()
    (set! (-> self enable-turn-around) #t)
    1.0
    (suspend)
    (ja-channel-push! 1 (seconds 0.05))
    (cond
      ((< 40.96 (vector-length (-> self nav travel)))
       (ja :group! yakow-kicked-ja)
       (ja :num-func num-func-identity :frame-num 0.0)
       (until (ja-done? 0)
         (suspend)
         (let* ((f0-3 0.75)
                (f1-0 0.25)
                (f2-0 (-> *YAKOW-bank* run-speed))
                (f0-4 (fmax f0-3 (fmin
                                   (* f1-0 (/ 1.0 f2-0) (-> self final-speed) (-> *YAKOW-bank* run-anim-speed))
                                   (-> *YAKOW-bank* max-run-anim-speed)
                                   )
                            )
                      )
                )
           (ja :num! (seek! max f0-4))
           )
         )
       )
      (else
        (ja-no-eval :group! yakow-kicked-in-place-ja :num! (seek!) :frame-num 0.0)
        (until (ja-done? 0)
          (if (< 40.96 (-> self final-speed))
              (go yakow-run-away)
              )
          (suspend)
          (ja :num! (seek!))
          )
        )
      )
    (go yakow-run-away)
    (none)
    )
  :post yakow-run-post
  )

;; failed to figure out what this is:
(defstate yakow-die (yakow)
  :event (the-as (function process int symbol event-message-block object :behavior yakow) #f)
  :code (behavior ()
    (let ((v1-1 (-> self root-override root-prim)))
      (set! (-> v1-1 collide-with) (collide-kind))
      (set! (-> v1-1 prim-core collide-as) (collide-kind))
      )
    0
    (drop-pickup (-> self fact-override) #t *entity-pool* (-> self fact-override) 0)
    (process-entity-status! self (entity-perm-status dead) #t)
    (none)
    )
  )

;; definition for method 11 of type yakow
;; INFO: Return type mismatch object vs none.
;; Used lq/sq
(defmethod init-from-entity! yakow ((obj yakow) (arg0 entity-actor))
  (stack-size-set! (-> obj main-thread) 512)
  (let ((s4-0 (new 'process 'collide-shape-moving obj (collide-list-enum hit-by-player))))
    (set! (-> s4-0 dynam) (copy *standard-dynamics* 'process))
    (set! (-> s4-0 reaction) default-collision-reaction)
    (set! (-> s4-0 no-reaction)
          (the-as (function collide-shape-moving collide-shape-intersect vector vector none) nothing)
          )
    (let ((s3-0 (new 'process 'collide-shape-prim-sphere s4-0 (the-as uint 0))))
      (set! (-> s3-0 prim-core collide-as) (collide-kind enemy))
      (set! (-> s3-0 collide-with) (collide-kind target))
      (set! (-> s3-0 prim-core action) (collide-action solid ca-10 ca-11))
      (set! (-> s3-0 prim-core offense) (collide-offense indestructible))
      (set-vector! (-> s3-0 local-sphere) 0.0 4096.0 0.0 10240.0)
      (set-root-prim! s4-0 s3-0)
      )
    (set! (-> s4-0 nav-radius) (* 0.75 (-> s4-0 root-prim local-sphere w)))
    (backup-collide-with-as s4-0)
    (set! (-> obj root-override) s4-0)
    )
  (process-drawable-from-entity! obj arg0)
  (set! (-> obj align) (new 'process 'align-control obj))
  (set! (-> obj nav) (new 'process 'nav-control (-> obj root-override) 16 40960.0))
  (logior! (-> obj nav flags) (nav-control-flags display-marks navcf3 navcf5 navcf6 navcf7))
  (set! (-> obj nav nearest-y-threshold) 409600.0)
  (set! (-> obj fact-override)
        (new 'process 'fact-info-enemy obj (pickup-type eco-pill-random) (-> *FACT-bank* default-pill-inc))
        )
  (set! (-> obj fact-override idle-distance) (-> *YAKOW-bank* default-idle-distance))
  (initialize-skeleton obj *yakow-sg* '())
  (set! (-> obj draw shadow-ctrl)
        (new 'process 'shadow-control -4096.0 4096.0 614400.0 (the-as float 24) 245760.0)
        )
  (set! (-> obj link) (new 'process 'actor-link-info obj))
  (set! (-> obj water) (new 'process 'water-control obj 6 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 home-base quad) (-> obj root-override trans quad))
  (let ((v1-40 (res-lump-struct (-> obj entity) 'alt-vector vector)))
    (when v1-40
      (set! (-> obj dest-base quad) (-> v1-40 quad))
      (set! (-> obj dest-rot) (-> v1-40 w))
      (set! (-> obj dest-base w) 1.0)
      )
    )
  (set! (-> obj vulnerable) #t)
  (set! (-> obj grazing) #f)
  (set! (-> obj enable-turn-around) #f)
  (set! (-> obj in-pen)
        (and (-> obj entity) (logtest? (-> obj entity extra perm status) (entity-perm-status complete)))
        )
  (cond
    ((-> obj in-pen)
     (set! (-> obj root-override trans quad) (-> obj dest-base quad))
     (set-yaw-angle-clear-roll-pitch! (-> obj root-override) (-> obj dest-rot))
     (go yakow-graze)
     )
    (else
      (yakow-task-complete? #f)
      (go yakow-idle)
      )
    )
  (none)
  )
