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

(import "goal_src/jak1/import/spider-egg-ag.gc")

;; definition of type mother-spider-egg
(deftype mother-spider-egg (process-drawable)
  ((parent-override    (pointer mother-spider)          :offset         12)
   (root-override      collide-shape-moving             :offset        112)
   (anim-speed         float                            :offset-assert 176)
   (part2              sparticle-launch-control         :offset-assert 180)
   (falling-start-time time-frame                       :offset-assert 184)
   (fall-dest          vector                   :inline :offset-assert 192)
   (fall-dest-normal   vector                   :inline :offset-assert 208)
   (broken-look        lod-set                  :inline :offset-assert 224)
   (traj               trajectory               :inline :offset-assert 272)
   (shadow-pos         vector                   :inline :offset-assert 320)
   )
  :heap-base #xe0
  :method-count-assert 22
  :size-assert         #x150
  :flag-assert         #x1600e00150
  (:methods
    (dummy-20 (_type_) none 20)
    (draw-egg-shadow (_type_ vector symbol) symbol 21)
    )
  (:states
    mother-spider-egg-die
    mother-spider-egg-die-exit
    mother-spider-egg-die-while-falling
    mother-spider-egg-falling
    mother-spider-egg-hatch
    mother-spider-egg-on-ground
    )
  )

;; definition for method 3 of type mother-spider-egg
(defmethod inspect mother-spider-egg ((obj mother-spider-egg))
  (let ((t9-0 (method-of-type process-drawable inspect)))
    (t9-0 obj)
    )
  (format #t "~T~Tanim-speed: ~f~%" (-> obj anim-speed))
  (format #t "~T~Tpart2: ~A~%" (-> obj part2))
  (format #t "~T~Tfalling-start-time: ~D~%" (-> obj falling-start-time))
  (format #t "~T~Tfall-dest: #<vector @ #x~X>~%" (-> obj fall-dest))
  (format #t "~T~Tfall-dest-normal: #<vector @ #x~X>~%" (-> obj fall-dest-normal))
  (format #t "~T~Tbroken-look: #<lod-set @ #x~X>~%" (-> obj broken-look))
  (format #t "~T~Ttraj: #<trajectory @ #x~X>~%" (-> obj traj))
  (format #t "~T~Tshadow-pos: #<vector @ #x~X>~%" (-> obj shadow-pos))
  obj
  )

;; failed to figure out what this is:
(defskelgroup *mother-spider-egg-unbroken-sg* spider-egg spider-egg-unbroken-lod0-jg -1
              ((spider-egg-unbroken-lod0-mg (meters 20))
               (spider-egg-unbroken-lod1-mg (meters 40))
               (spider-egg-unbroken-lod2-mg (meters 999999))
               )
              :bounds (static-spherem 0 1 0 2)
              :shadow spider-egg-unbroken-shadow-mg
              )

;; failed to figure out what this is:
(defskelgroup *mother-spider-egg-broken-sg* spider-egg spider-egg-broken-lod0-jg -1
              ((spider-egg-broken-lod0-mg (meters 20))
               (spider-egg-broken-lod1-mg (meters 40))
               (spider-egg-broken-lod2-mg (meters 999999))
               )
              :bounds (static-spherem 0 1 0 2)
              )

;; failed to figure out what this is:
(defpartgroup group-spider-egg-hatches
  :id 324
  :duration 5
  :linger-duration 900
  :flags (use-local-clock)
  :bounds (static-bspherem 0 0 0 8)
  :parts ((sp-item 2018 :fade-after (meters 50) :falloff-to (meters 50))
    (sp-item 2071 :fade-after (meters 50) :falloff-to (meters 50))
    )
  )

;; failed to figure out what this is:
(defpart 2071
  :init-specs ((sp-tex spt-texture (new 'static 'texture-id :index #xf :page #x2))
    (sp-flt spt-num 16.0)
    (sp-flt spt-y (meters 0.5))
    (sp-flt spt-scale-x (meters 0.1))
    (sp-int spt-rot-x 4)
    (sp-rnd-flt spt-rot-z (degrees 0.0) (degrees 360.0) 1.0)
    (sp-flt spt-scale-y (meters 0.1))
    (sp-flt spt-r 255.0)
    (sp-flt spt-g 64.0)
    (sp-flt spt-b 255.0)
    (sp-rnd-flt spt-a 32.0 96.0 1.0)
    (sp-int spt-timer 225)
    (sp-cpuinfo-flags bit2 bit3)
    (sp-int spt-next-time 75)
    (sp-launcher-by-id spt-next-launcher 2072)
    )
  )

;; failed to figure out what this is:
(defpart 2072
  :init-specs ((sp-flt spt-scale-x (meters 0.1))
    (sp-flt spt-scale-y (meters 2))
    (sp-flt spt-scalevel-y (meters 0.2))
    (sp-flt spt-fade-a -1.4222223)
    )
  )

;; failed to figure out what this is:
(defpart 2018
  :init-specs ((sp-tex spt-texture (new 'static 'texture-id :page #x2))
    (sp-flt spt-num 8.0)
    (sp-rnd-flt spt-y (meters 0.5) (meters 0.5) 1.0)
    (sp-rnd-flt spt-scale-x (meters 0.5) (meters 1) 1.0)
    (sp-rnd-flt spt-rot-z (degrees 0.0) (degrees 360.0) 1.0)
    (sp-copy-from-other spt-scale-y -4)
    (sp-rnd-flt spt-r 32.0 32.0 1.0)
    (sp-rnd-flt spt-g 0.0 16.0 1.0)
    (sp-rnd-flt spt-b 32.0 32.0 1.0)
    (sp-flt spt-a 0.0)
    (sp-rnd-flt spt-vel-y (meters 0.0033333334) (meters 0.0033333334) 1.0)
    (sp-flt spt-scalevel-x (meters 0.006666667))
    (sp-rnd-flt spt-rotvel-z (degrees -1.2) (degrees 1.2) 1.0)
    (sp-copy-from-other spt-scalevel-y -4)
    (sp-flt spt-fade-a 1.4222223)
    (sp-flt spt-accel-y 0.27306667)
    (sp-int spt-timer 375)
    (sp-cpuinfo-flags bit2 bit12 bit14)
    (sp-int spt-next-time 45)
    (sp-launcher-by-id spt-next-launcher 2073)
    (sp-flt spt-conerot-x (degrees 90.0))
    (sp-rnd-flt spt-conerot-y (degrees 0.0) (degrees 360.0) 1.0)
    (sp-rnd-flt spt-conerot-radius (meters 0) (meters 0.5) 1.0)
    )
  )

;; failed to figure out what this is:
(defpart 2073
  :init-specs ((sp-flt spt-fade-a -0.21333334))
  )

;; failed to figure out what this is:
(defpartgroup group-spider-egg-explodes
  :id 325
  :duration 5
  :linger-duration 375
  :flags (use-local-clock)
  :bounds (static-bspherem 0 0 0 8)
  :parts ((sp-item 2074 :fade-after (meters 50) :falloff-to (meters 50)))
  )

;; failed to figure out what this is:
(defpart 2074
  :init-specs ((sp-tex spt-texture (new 'static 'texture-id :page #x2))
    (sp-flt spt-num 8.0)
    (sp-rnd-flt spt-y (meters 0.5) (meters 0.5) 1.0)
    (sp-rnd-flt spt-scale-x (meters 1.5) (meters 2) 1.0)
    (sp-rnd-flt spt-rot-z (degrees 0.0) (degrees 360.0) 1.0)
    (sp-copy-from-other spt-scale-y -4)
    (sp-rnd-flt spt-r 32.0 32.0 1.0)
    (sp-rnd-flt spt-g 0.0 16.0 1.0)
    (sp-rnd-flt spt-b 32.0 32.0 1.0)
    (sp-rnd-flt spt-a 32.0 32.0 1.0)
    (sp-rnd-flt spt-vel-y (meters 0.0033333334) (meters 0.0033333334) 1.0)
    (sp-flt spt-scalevel-x (meters 0.006666667))
    (sp-rnd-flt spt-rotvel-z (degrees -1.2) (degrees 1.2) 1.0)
    (sp-copy-from-other spt-scalevel-y -4)
    (sp-flt spt-fade-a -1.4222223)
    (sp-flt spt-accel-y 0.27306667)
    (sp-int spt-timer 375)
    (sp-cpuinfo-flags bit2 bit12 bit14)
    (sp-flt spt-conerot-x (degrees 90.0))
    (sp-rnd-flt spt-conerot-y (degrees 0.0) (degrees 360.0) 1.0)
    (sp-rnd-flt spt-conerot-radius (meters 0) (meters 0.5) 1.0)
    )
  )

;; definition for method 21 of type mother-spider-egg
;; INFO: Used lq/sq
(defmethod draw-egg-shadow mother-spider-egg ((obj mother-spider-egg) (arg0 vector) (arg1 symbol))
  (cond
    ((and (-> obj draw shadow)
          (zero? (-> obj draw cur-lod))
          (logtest? (-> obj draw status) (draw-status was-drawn))
          )
     (let ((s5-0 (new 'stack-no-clear 'collide-tri-result))
           (a1-1 (new 'stack-no-clear 'vector))
           (a2-1 (new 'stack-no-clear 'vector))
           )
       (set! (-> a1-1 quad) (-> obj root-override trans quad))
       (set! (-> a1-1 y) (+ 1228.8 (-> a1-1 y)))
       (set-vector! a2-1 0.0 -61440.0 0.0 1.0)
       (cond
         ((>= (fill-and-probe-using-line-sphere
                *collide-cache*
                a1-1
                a2-1
                7372.8
                (collide-kind background)
                obj
                s5-0
                (new 'static 'pat-surface :noentity #x1)
                )
              0.0
              )
          (let ((v1-11 (-> obj draw shadow-ctrl)))
            (logclear! (-> v1-11 settings flags) (shadow-flags disable-draw))
            )
          0
          (let ((v1-14 (-> obj draw shadow-ctrl)))
            (set! (-> v1-14 settings bot-plane w) (- (+ -4096.0 (-> s5-0 intersect y))))
            )
          0
          (let ((v1-17 (-> obj draw shadow-ctrl)))
            (set! (-> v1-17 settings top-plane w) (- (+ 6144.0 (-> s5-0 intersect y))))
            )
          0
          (return #t)
          )
         (else
           (let ((v1-22 (-> obj draw shadow-ctrl)))
             (logior! (-> v1-22 settings flags) (shadow-flags disable-draw))
             )
           0
           )
         )
       )
     )
    (else
      (let ((v1-25 (-> obj draw shadow-ctrl)))
        (logior! (-> v1-25 settings flags) (shadow-flags disable-draw))
        )
      0
      )
    )
  #f
  )

;; failed to figure out what this is:
(defstate mother-spider-egg-falling (mother-spider-egg)
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (case arg2
      (('touch 'attack)
       (go mother-spider-egg-die-while-falling)
       )
      )
    )
  :enter (behavior ()
    (set! (-> self falling-start-time) (-> *display* base-frame-counter))
    (none)
    )
  :trans (behavior ()
    (let ((f30-0
            (fmin (the float (- (-> *display* base-frame-counter) (-> self falling-start-time))) (-> self traj time))
            )
          )
      (let ((f28-0 (/ f30-0 (-> self traj time))))
        (eval-position! (-> self traj) f30-0 (-> self root-override trans))
        (let ((f0-3 (lerp 0.3 0.4 f28-0)))
          (set-vector! (-> self root-override scale) f0-3 f0-3 f0-3 1.0)
          )
        )
      (when (= f30-0 (-> self traj time))
        (sound-play "eggs-lands")
        (go mother-spider-egg-on-ground)
        )
      )
    (draw-egg-shadow self (-> self shadow-pos) #t)
    (none)
    )
  :code (behavior ()
    (ja-channel-push! 1 0)
    (loop
      (ja-no-eval :group! (-> self draw art-group data 9) :num! (seek! max (-> self anim-speed)) :frame-num 0.0)
      (until (ja-done? 0)
        (suspend)
        (ja :num! (seek! max (-> self anim-speed)))
        )
      )
    (none)
    )
  :post (the-as (function none :behavior mother-spider-egg) transform-post)
  )

;; failed to figure out what this is:
(defstate mother-spider-egg-on-ground (mother-spider-egg)
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (let ((v1-0 arg2))
      (the-as object (cond
                       ((= v1-0 'touch)
                        (send-shove-back
                          (-> self root-override)
                          arg0
                          (the-as touching-shapes-entry (-> arg3 param 0))
                          0.7
                          6144.0
                          16384.0
                          )
                        )
                       ((= v1-0 'attack)
                        (go mother-spider-egg-die)
                        )
                       )
              )
      )
    )
  :enter (behavior ()
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (if (not (draw-egg-shadow self (-> self shadow-pos) #t))
        (set! (-> self shadow-pos quad) (-> self fall-dest quad))
        )
    (none)
    )
  :trans (behavior ()
    (if (>= (- (-> *display* base-frame-counter) (-> self state-time)) (seconds 2))
        (go mother-spider-egg-hatch)
        )
    (draw-egg-shadow self (-> self shadow-pos) #f)
    (none)
    )
  :code (behavior ()
    (ja-channel-push! 1 (seconds 0.1))
    (let ((gp-0 (new 'stack-no-clear 'quaternion))
          (s5-0 (new 'stack-no-clear 'quaternion))
          )
      (let ((s3-0 (new 'stack-no-clear 'vector))
            (s4-0 (new 'stack-no-clear 'vector))
            )
        (quaternion-copy! gp-0 (-> self root-override quat))
        (set-vector! s4-0 0.0 1.0 0.0 1.0)
        (set! (-> s3-0 quad) (-> self fall-dest-normal quad))
        (vector-normalize! s3-0 1.0)
        (quaternion-from-two-vectors! s5-0 s4-0 s3-0)
        )
      (quaternion*! s5-0 s5-0 gp-0)
      (ja-no-eval :group! (-> self draw art-group data 11) :num! (seek! max 1.3) :frame-num 0.0)
      (until (ja-done? 0)
        (let* ((f0-8 (ja-frame-num 0))
               (v1-19 (ja-group))
               (f0-9 (/ f0-8 (the float (+ (-> v1-19 data 0 length) -1))))
               )
          (quaternion-slerp! (-> self root-override quat) gp-0 s5-0 f0-9)
          )
        (suspend)
        (ja :num! (seek! max 1.3))
        )
      )
    (loop
      (ja-no-eval :group! (-> self draw art-group data 9) :num! (seek! max (-> self anim-speed)) :frame-num 0.0)
      (until (ja-done? 0)
        (suspend)
        (ja :num! (seek! max (-> self anim-speed)))
        )
      )
    (none)
    )
  :post (the-as (function none :behavior mother-spider-egg) transform-post)
  )

;; failed to figure out what this is:
(defstate mother-spider-egg-hatch (mother-spider-egg)
  :trans (behavior ()
    (when (and (zero? (-> self draw cur-lod)) (logtest? (-> self draw status) (draw-status was-drawn)))
      (let ((a1-0 (new 'stack-no-clear 'vector)))
        (set! (-> a1-0 quad) (-> self fall-dest quad))
        (compute-and-draw-shadow
          (-> self root-override trans)
          a1-0
          (-> self fall-dest-normal)
          (the-as vector 7372.8)
          81920.0
          (the-as float 0)
          )
        )
      )
    (none)
    )
  :code (behavior ()
    (send-event (ppointer->process (-> self parent-override)) 'trigger)
    (clear-collide-with-as (-> self root-override))
    (process-spawn
      part-tracker
      :init part-tracker-init
      (-> *part-group-id-table* 324)
      -1
      #f
      #f
      #f
      (-> self root-override trans)
      :to *entity-pool*
      )
    (lods-assign! (-> self draw) (-> self broken-look))
    (ja-channel-push! 1 (seconds 0.2))
    (ja-no-eval :group! (-> self draw art-group data 10) :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek!))
      )
    (let ((v1-37 (-> self draw shadow-ctrl)))
      (logior! (-> v1-37 settings flags) (shadow-flags disable-draw))
      )
    0
    (go mother-spider-egg-die-exit)
    (none)
    )
  :post (the-as (function none :behavior mother-spider-egg) transform-post)
  )

;; failed to figure out what this is:
(defstate mother-spider-egg-die (mother-spider-egg)
  :code (behavior ()
    (logclear! (-> self mask) (process-mask actor-pause))
    (let ((v1-3 (-> self draw shadow-ctrl)))
      (logior! (-> v1-3 settings flags) (shadow-flags disable-draw))
      )
    0
    (lods-assign! (-> self draw) (-> self broken-look))
    (ja-channel-push! 1 (seconds 0.1))
    (clear-collide-with-as (-> self root-override))
    (process-spawn
      part-tracker
      :init part-tracker-init
      (-> *part-group-id-table* 325)
      -1
      #f
      #f
      #f
      (-> self root-override trans)
      :to *entity-pool*
      )
    (ja-no-eval :group! (-> self draw art-group data 12) :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek!))
      )
    (go mother-spider-egg-die-exit)
    (none)
    )
  :post (the-as (function none :behavior mother-spider-egg) ja-post)
  )

;; failed to figure out what this is:
(defstate mother-spider-egg-die-while-falling (mother-spider-egg)
  :trans (behavior ()
    (let ((f0-2
            (fmin (the float (- (-> *display* base-frame-counter) (-> self falling-start-time))) (-> self traj time))
            )
          )
      (eval-position! (-> self traj) f0-2 (-> self root-override trans))
      )
    (none)
    )
  :code (behavior ()
    (logclear! (-> self mask) (process-mask actor-pause))
    (clear-collide-with-as (-> self root-override))
    (let ((v1-5 (-> self draw shadow-ctrl)))
      (logior! (-> v1-5 settings flags) (shadow-flags disable-draw))
      )
    0
    (process-spawn
      part-tracker
      :init part-tracker-init
      (-> *part-group-id-table* 325)
      -1
      #f
      #f
      #f
      (-> self root-override trans)
      :to *entity-pool*
      )
    (lods-assign! (-> self draw) (-> self broken-look))
    (ja-channel-push! 1 (seconds 0.1))
    (ja-no-eval :group! (-> self draw art-group data 12) :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek!))
      )
    (go mother-spider-egg-die-exit)
    (none)
    )
  :post (the-as (function none :behavior mother-spider-egg) ja-post)
  )

;; failed to figure out what this is:
(defstate mother-spider-egg-die-exit (mother-spider-egg)
  :code (behavior ()
    (send-event (ppointer->process (-> self parent-override)) 'untrigger)
    (logior! (-> self draw status) (draw-status hidden))
    (let ((v1-8 (-> self draw shadow-ctrl)))
      (logior! (-> v1-8 settings flags) (shadow-flags disable-draw))
      )
    0
    (logclear! (-> self root-override nav-flags) (nav-flags navf0))
    (logclear! (-> self root-override nav-flags) (nav-flags navf1))
    (clear-collide-with-as (-> self root-override))
    (until (not (-> self child))
      (suspend)
      )
    (none)
    )
  )

;; definition for function mother-spider-egg-init-by-other
;; INFO: Used lq/sq
;; INFO: Return type mismatch object vs none.
(defbehavior mother-spider-egg-init-by-other mother-spider-egg ((arg0 entity-actor) (arg1 vector) (arg2 vector) (arg3 vector))
  (set! (-> self entity) arg0)
  (set! (-> self anim-speed) (rand-vu-float-range 0.8 1.2))
  (set! (-> self falling-start-time) (-> *display* base-frame-counter))
  (set! (-> self fall-dest quad) (-> arg2 quad))
  (set! (-> self fall-dest-normal quad) (-> arg3 quad))
  (let ((s4-1 (new 'process 'collide-shape-moving self (collide-list-enum usually-hit-by-player))))
    (set! (-> s4-1 dynam) (copy *standard-dynamics* 'process))
    (set! (-> s4-1 reaction) default-collision-reaction)
    (set! (-> s4-1 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-1 (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))
      (set! (-> s3-0 prim-core offense) (collide-offense touch))
      (set-vector! (-> s3-0 local-sphere) 0.0 2048.0 0.0 3686.4)
      (set-root-prim! s4-1 s3-0)
      )
    (set! (-> s4-1 nav-radius) 4096.0)
    (backup-collide-with-as s4-1)
    (set! (-> self root-override) s4-1)
    )
  (set! (-> self root-override trans quad) (-> arg1 quad))
  (set-vector! (-> self root-override scale) 0.3 0.3 0.3 1.0)
  (quaternion-copy! (-> self root-override quat) (-> self parent-override 0 root-override quat))
  (logior! (-> self mask) (process-mask actor-pause))
  (set! (-> self mask) (logior (process-mask enemy) (-> self mask)))
  (set! (-> self mask) (logior (process-mask attackable) (-> self mask)))
  (initialize-skeleton self *mother-spider-egg-unbroken-sg* '())
  (setup-lods! (-> self broken-look) *mother-spider-egg-broken-sg* (-> self draw art-group) (-> self entity))
  (set! (-> self draw shadow-ctrl) (new 'process 'shadow-control 0.0 0.0 614400.0 (the-as float 60) 245760.0))
  (set! (-> self nav) (new 'process 'nav-control (-> self root-override) 16 40960.0))
  (logior! (-> self nav flags) (nav-control-flags display-marks navcf3 navcf5 navcf6 navcf7))
  (logclear! (-> self root-override nav-flags) (nav-flags navf0))
  (logior! (-> self root-override nav-flags) (nav-flags navf1))
  (set! (-> self nav extra-nav-sphere quad) (-> self fall-dest quad))
  (set! (-> self nav extra-nav-sphere w) 4096.0)
  (setup-from-to-height! (-> self traj) (-> self root-override trans) arg2 4096.0 -4.551111)
  (create-connection!
    *cavecrystal-light-control*
    self
    (-> self entity)
    (the-as (function object object object object object) cavecrystal-light-control-default-callback)
    -1
    4096.0
    )
  (go mother-spider-egg-falling)
  (none)
  )
