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

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

;; definition of type spider-egg
(deftype spider-egg (process-drawable)
  ((root-override collide-shape-moving         :offset        112)
   (notify-actor  entity-actor                 :offset-assert 176)
   (broken-look   lod-set              :inline :offset-assert 180)
   )
  :heap-base #x70
  :method-count-assert 20
  :size-assert         #xd5
  :flag-assert         #x14007000d5
  (:states
    spider-egg-die
    spider-egg-hatch
    (spider-egg-idle symbol)
    )
  )

;; definition for method 3 of type spider-egg
(defmethod inspect spider-egg ((obj spider-egg))
  (let ((t9-0 (method-of-type process-drawable inspect)))
    (t9-0 obj)
    )
  (format #t "~T~Tnotify-actor: ~A~%" (-> obj notify-actor))
  (format #t "~T~Tbroken-look: #<lod-set @ #x~X>~%" (-> obj broken-look))
  obj
  )

;; failed to figure out what this is:
(defskelgroup *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)
              )

;; failed to figure out what this is:
(defskelgroup *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:
(defstate spider-egg-idle (spider-egg)
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (local-vars (v0-0 none))
    (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 'can-spawn?)
                        (return (the-as object #t))
                        v0-0
                        )
                       ((= v1-0 'notify-spawned)
                        (go spider-egg-hatch)
                        )
                       ((= v1-0 'attack)
                        (go spider-egg-die)
                        )
                       )
              )
      )
    )
  :enter (behavior ((arg0 symbol))
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (none)
    )
  :code (behavior ((arg0 symbol))
    (let ((f30-0 (rand-vu-float-range 0.8 1.2)))
      (cond
        (arg0
          (ja-channel-set! 1)
          (ja-no-eval :group! spider-egg-idle-ja
                      :num! (seek! max f30-0)
                      :frame-num (rand-vu-float-range 0.0 (the float (+ (-> (ja-group) data 0 length) -1)))
                      )
          (until (ja-done? 0)
            (suspend)
            (ja :num! (seek! max f30-0))
            )
          )
        (else
          (ja-channel-push! 1 (seconds 0.2))
          )
        )
      (loop
        (let ((gp-2 (rand-vu-int-range 2 6)))
          (dotimes (s5-0 gp-2)
            (ja-no-eval :group! spider-egg-idle-ja :num! (seek! max f30-0) :frame-num 0.0)
            (until (ja-done? 0)
              (suspend)
              (ja :num! (seek! max f30-0))
              )
            )
          )
        (ja-channel-push! 1 (seconds 0.1))
        (ja-no-eval :group! spider-egg-twitch-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 spider-egg) ja-post)
  )

;; failed to figure out what this is:
(defstate spider-egg-hatch (spider-egg)
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (local-vars (v0-0 symbol))
    (case arg2
      (('can-spawn?)
       (return (the-as object #f))
       v0-0
       )
      )
    )
  :code (behavior ()
    (cleanup-for-death self)
    (logclear! (-> self mask) (process-mask actor-pause))
    (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! spider-egg-crack-ja :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek!))
      )
    (logior! (-> self draw status) (draw-status hidden))
    (until (not (-> self child))
      (suspend)
      )
    (none)
    )
  :post (the-as (function none :behavior spider-egg) ja-post)
  )

;; failed to figure out what this is:
(defstate spider-egg-die (spider-egg)
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (local-vars (v0-0 symbol))
    (case arg2
      (('can-spawn?)
       (return (the-as object #f))
       v0-0
       )
      )
    )
  :code (behavior ()
    (cleanup-for-death self)
    (logclear! (-> self mask) (process-mask actor-pause))
    (when (-> self notify-actor)
      (let ((a1-0 (new 'stack-no-clear 'event-message-block)))
        (set! (-> a1-0 from) self)
        (set! (-> a1-0 num-params) 1)
        (set! (-> a1-0 message) 'notify)
        (set! (-> a1-0 param 0) (the-as uint 'die))
        (let ((t9-1 send-event-function)
              (v1-9 (-> self notify-actor))
              )
          (t9-1
            (if v1-9
                (-> v1-9 extra process)
                )
            a1-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))
    (clear-collide-with-as (-> self root-override))
    (ja-no-eval :group! spider-egg-die-ja :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek!))
      )
    (logior! (-> self draw status) (draw-status hidden))
    (until (not (-> self child))
      (suspend)
      )
    (none)
    )
  :post (the-as (function none :behavior spider-egg) ja-post)
  )

;; definition for method 11 of type spider-egg
;; INFO: Used lq/sq
;; INFO: Return type mismatch object vs none.
(defmethod init-from-entity! spider-egg ((obj spider-egg) (arg0 entity-actor))
  (set! (-> obj mask) (logior (process-mask enemy) (-> obj mask)))
  (set! (-> obj mask) (logior (process-mask attackable) (-> obj mask)))
  (let ((s4-0 (new 'process 'collide-shape-moving obj (collide-list-enum usually-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))
      (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-0 s3-0)
      )
    (set! (-> s4-0 nav-radius) 4096.0)
    (backup-collide-with-as s4-0)
    (set! (-> obj root-override) s4-0)
    )
  (process-drawable-from-entity! obj arg0)
  (initialize-skeleton obj *spider-egg-unbroken-sg* '())
  (setup-lods! (-> obj broken-look) *spider-egg-broken-sg* (-> obj draw art-group) (-> obj entity))
  (set-vector! (-> obj root-override scale) 0.4 0.4 0.4 1.0)
  (if (not (move-to-ground (-> obj root-override) 12288.0 40960.0 #t (collide-kind background)))
      (go process-drawable-art-error "no ground")
      )
  (set! (-> obj root-override trans y) (+ -409.6 (-> obj root-override trans y)))
  (let ((s4-1 (new 'stack-no-clear 'vector)))
    (set! (-> s4-1 quad) (-> obj root-override surface-normal quad))
    (+! (-> s4-1 x) (rand-vu-float-range -0.2 0.2))
    (+! (-> s4-1 z) (rand-vu-float-range -0.2 0.2))
    (vector-normalize! s4-1 1.0)
    (quaternion-axis-angle!
      (-> obj root-override quat)
      (-> s4-1 x)
      (-> s4-1 y)
      (-> s4-1 z)
      (rand-vu-float-range 0.0 65536.0)
      )
    )
  (update-transforms! (-> obj root-override))
  (nav-mesh-connect obj (-> obj root-override) (the-as nav-control #f))
  (if (> (entity-actor-count arg0 'alt-actor) 0)
      (set! (-> obj notify-actor) (entity-actor-lookup arg0 'alt-actor 0))
      (set! (-> obj notify-actor) #f)
      )
  (go spider-egg-idle #t)
  (none)
  )
