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

;; name: krimson-wall.gc
;; name in dgo: krimson-wall
;; dgos: CWI

;; DECOMP BEGINS

(defpartgroup group-krimson-wall
  :id 329
  :flags (sp0 sp4)
  :bounds (static-bspherem 0 0 0 10)
  :parts ((sp-item 1419 :flags (is-3d sp7) :period (seconds 2) :length (seconds 0.035))
    (sp-item 1420 :fade-after (meters 40) :flags (is-3d sp7))
    )
  )

(defpart 1419
  :init-specs ((:texture (lasersmoke-00 level-default-sprite))
    (:num 1.0)
    (:y (meters 11))
    (:scale-x (meters 18))
    (:rot-x (degrees 0) 1 (degrees 180))
    (:rot-z (degrees 90))
    (:scale-y (meters 54))
    (:r 128.0)
    (:g 0.0)
    (:b 0.0)
    (:a 16.0)
    (:timer (seconds 2))
    (:flags (sp-cpuinfo-flag-3))
    (:userdata :data (new 'static 'boxed-array :type int32
                       20
                       1
                       0
                       #x401b00
                       #x401c00
                       #x401d00
                       #x401e00
                       #x401f00
                       #x402000
                       #x402100
                       #x402200
                       #x402300
                       #x402400
                       #x402500
                       #x402600
                       #x402700
                       #x402800
                       #x402900
                       #x402a00
                       #x402b00
                       #x402c00
                       #x402d00
                       #x402e00
                       #x402f00
                       #x403000
                       #x403100
                       #x403200
                       #x403300
                       #x403400
                       #x403500
                       #x403600
                       #x403700
                       #x403800
                       )
               )
    (:func 'sparticle-texture-animate)
    (:rotate-y (degrees 0))
    )
  )

(defpart 1420
  :init-specs ((:texture (static1 level-default-sprite))
    (:num 1.0)
    (:y (meters 11))
    (:scale-x (meters 18))
    (:rot-x (degrees 0) 1 (degrees 180))
    (:rot-z (degrees 90) 1 (degrees 180))
    (:scale-y (meters 180))
    (:r 255.0)
    (:g 10.0)
    (:b 10.0)
    (:a 4.0 16.0)
    (:timer (seconds 0.017))
    (:flags (sp-cpuinfo-flag-3))
    (:rotate-y (degrees 0))
    )
  )

(defskelgroup skel-krimson-wall-break krimson-wall-break krimson-wall-break-lod0-jg krimson-wall-break-idle-ja
              ((krimson-wall-break-lod0-mg (meters 999999)))
              :bounds (static-spherem -40 0 0 400)
              :origin-joint-index 3
              )

(deftype krimson-wall-break (process-drawable)
  ((art-name    string)
   (anim        spool-anim)
   (pillar      (pointer manipy)  2)
   (level-name  symbol)
   )
  (:state-methods
    idle
    explode
    done
    )
  )


(defstate idle (krimson-wall-break)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('break)
       (go-virtual explode)
       )
      )
    )
  :enter (behavior ()
    '()
    )
  :trans (behavior ()
    (matrix-copy! (new 'stack-no-clear 'matrix) (-> self node-list data 2 bone transform))
    )
  :code sleep-code
  :post ja-post
  )

(defstate explode (krimson-wall-break)
  :virtual #t
  :enter (behavior ()
    (set-time! (-> self state-time))
    )
  :trans (behavior ()
    (when (not (time-elapsed? (-> self state-time) (seconds 0.5)))
      (let ((gp-0 (new 'stack-no-clear 'vector)))
        (vector-copy! gp-0 (-> self root trans))
        (+! (-> gp-0 z) (* 4096.0 (rand-vu-float-range -60.0 60.0)))
        (+! (-> gp-0 x) (* 4096.0 (rand-vu-float-range -20.0 20.0)))
        (+! (-> gp-0 y) (* 4096.0 (rand-vu-float-range 0.0 25.0)))
        (let ((v1-9 (rand-vu-int-count 2)))
          (cond
            ((zero? v1-9)
             (cond
               ((logtest? (-> group-kg-huge-explosion flags) (sp-group-flag sp13))
                (vector-copy! (-> *launch-matrix* trans) gp-0)
                (part-tracker-spawn part-tracker-subsampler :to self :group group-kg-huge-explosion)
                )
               (else
                 (vector-copy! (-> *launch-matrix* trans) gp-0)
                 (part-tracker-spawn part-tracker :to self :group group-kg-huge-explosion)
                 )
               )
             )
            ((= v1-9 1)
             (cond
               ((logtest? (-> group-kg-big-explosion flags) (sp-group-flag sp13))
                (vector-copy! (-> *launch-matrix* trans) gp-0)
                (part-tracker-spawn part-tracker-subsampler :to self :group group-kg-big-explosion)
                )
               (else
                 (vector-copy! (-> *launch-matrix* trans) gp-0)
                 (part-tracker-spawn part-tracker :to self :group group-kg-big-explosion)
                 )
               )
             )
            )
          )
        )
      )
    )
  :code (behavior ()
    (let* ((gp-0 (-> self draw art-group))
           (s5-0 (method-of-object gp-0 get-art-by-name-method))
           )
      (format (clear *temp-string*) "~S-end" (-> self art-name))
      (let ((gp-1 (s5-0 gp-0 *temp-string* art-joint-anim)))
        (ja-play-spooled-anim
          (-> self anim)
          (ja-group)
          ;; og:preserve-this
          (the art-joint-anim gp-1)
          (the-as (function process-drawable symbol) false-func)
          (spooler-flags)
          )
        (ja-channel-set! 1)
        (set! (-> self skel root-channel 0 frame-group) (the art-joint-anim gp-1))
        )
      )
    (suspend)
    (go-virtual done)
    )
  :post ja-post
  )

(defstate done (krimson-wall-break)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('done)
       #t
       )
      )
    )
  :code sleep-code
  )

(defmethod init-from-entity! ((this krimson-wall-break) (arg0 entity-actor))
  "Set up a newly created process from the entity that created it."
  (local-vars (sv-96 entity))
  (set! (-> this level) (level-get *level* (-> this level-name)))
  (stack-size-set! (-> this main-thread) 512)
  (set! (-> this root) (new 'process 'trsqv))
  (process-drawable-from-entity! this arg0)
  (initialize-skeleton
    this
    (the-as skeleton-group (art-group-get-by-name *level* "skel-krimson-wall-break" (the-as (pointer level) #f)))
    (the-as pair 0)
    )
  (set! (-> this anim)
        (new 'static 'spool-anim :name "krimson-wall-break-1" :anim-name "break" :parts 2 :command-list '())
        )
  (set! (-> this art-name) (res-lump-struct arg0 'art-name string))
  (let ((s4-1 (-> this root trans))
        (s5-1 (-> this root trans))
        )
    (let ((s2-0 (entity-actor-lookup arg0 'alt-actor 0))
          (v0-8 (entity-actor-lookup arg0 'alt-actor 1))
          )
      (if s2-0
          (set! s4-1 (-> s2-0 extra trans))
          )
      (if v0-8
          (set! s5-1 (-> v0-8 extra trans))
          )
      )
    (let ((s3-1 (get-process *default-dead-pool* manipy #x20000 1)))
      (set! (-> this pillar 0)
            (the-as
              (pointer manipy)
              (when s3-1
                (let ((t9-10 (method-of-type manipy activate)))
                  (t9-10 (the-as manipy s3-1) this "krimson-wall-break" (the-as pointer #x70004000))
                  )
                (let ((s2-1 run-function-in-process)
                      (s1-0 s3-1)
                      (s0-0 manipy-init)
                      )
                  (set! sv-96 (-> this level entity data 0 entity))
                  (let ((t0-1 (art-group-get-by-name *level* "skel-krimson-wall-break" (the-as (pointer level) #f)))
                        (t1-1 #f)
                        (t2-1 0)
                        )
                    ((the-as (function object object object object object object object none) s2-1)
                     s1-0
                     s0-0
                     s4-1
                     sv-96
                     t0-1
                     t1-1
                     t2-1
                     )
                    )
                  )
                (-> s3-1 ppointer)
                )
              )
            )
      )
    (send-event (ppointer->process (-> this pillar 0)) 'anim-mode 'clone-anim)
    (send-event (ppointer->process (-> this pillar 0)) 'prefix "a-")
    (set! (-> this pillar 1)
          (process-spawn
            manipy
            :init manipy-init
            s5-1
            (-> this level entity data 0 entity)
            (art-group-get-by-name *level* "skel-krimson-wall-break" (the-as (pointer level) #f))
            #f
            0
            :name "krimson-wall-break"
            :to this
            :stack-size #x20000
            )
          )
    )
  (send-event (ppointer->process (-> this pillar 1)) 'anim-mode 'clone-anim)
  (send-event (ppointer->process (-> this pillar 1)) 'prefix "b-")
  (add-process *gui-control* this (gui-channel art-load) (gui-action queue) (-> this anim name) -99.0 0)
  (go (method-of-object this idle))
  )

(defbehavior krimson-wall-break-init-by-other krimson-wall-break ((arg0 entity-actor) (arg1 symbol))
  (set! (-> self level-name) arg1)
  (init-from-entity! self arg0)
  )

(defskelgroup skel-krimson-wall krimson-wall krimson-wall-lod0-jg krimson-wall-idle-ja
              ((krimson-wall-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 13 0 16)
              )

(deftype krimson-wall (process-focusable)
  ((alt-actor       entity-actor  2)
   (collision-part  int32)
   (width           float)
   )
  (:state-methods
    idle
    die
    )
  (:methods
    (krimson-wall-method-30 (_type_ symbol) none)
    )
  )


(defstate idle (krimson-wall)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('break)
       (when (and (-> self alt-actor 0) (-> self alt-actor 1))
         (send-event (-> self alt-actor 0 extra process) 'kill)
         (send-event (-> self alt-actor 1 extra process) 'kill)
         (go-virtual die)
         )
       )
      (('kill)
       (go-virtual die)
       )
      )
    )
  :trans (behavior ()
    (when (nonzero? (-> self collision-part))
      )
    )
  :code (behavior ()
    (until #f
      (ja-no-eval :group! (ja-group) :num! (seek!) :frame-num 0.0)
      (until (ja-done? 0)
        (suspend)
        (ja :num! (seek!))
        )
      )
    #f
    )
  :post ja-post
  )

(defstate die (krimson-wall)
  :virtual #t
  :code (behavior ()
    (suspend)
    (cleanup-for-death self)
    )
  )

(defmethod krimson-wall-method-30 ((this krimson-wall) (arg0 symbol))
  (cond
    (arg0
      (let ((gp-0 (new 'static 'inline-array vector 2 (new 'static 'vector) (new 'static 'vector))))
        (let ((f30-0 (-> this width)))
          (let ((t9-0 vector-matrix*!)
                (a0-1 (-> gp-0 0))
                (a1-1 (new 'stack-no-clear 'vector))
                )
            (set! (-> a1-1 x) 0.0)
            (set! (-> a1-1 y) 0.0)
            (set! (-> a1-1 z) f30-0)
            (set! (-> a1-1 w) 1.0)
            (t9-0 a0-1 a1-1 (-> this node-list data 2 bone transform))
            )
          (let ((t9-1 vector-matrix*!)
                (a0-2 (-> gp-0 1))
                (a1-2 (new 'stack-no-clear 'vector))
                )
            (set! (-> a1-2 x) 0.0)
            (set! (-> a1-2 y) 0.0)
            (set! (-> a1-2 z) (- f30-0))
            (set! (-> a1-2 w) 1.0)
            (t9-1 a0-2 a1-2 (-> this node-list data 2 bone transform))
            )
          )
        (if (nonzero? (-> this alt-actor 0))
            (vector-copy! (-> gp-0 0) (-> this alt-actor 0 extra trans))
            )
        (if (nonzero? (-> this alt-actor 1))
            (vector-copy! (-> gp-0 1) (-> this alt-actor 1 extra trans))
            )
        (spawn-security-wall this (-> gp-0 0) (-> gp-0 1) 77824.0 #f)
        (let ((s4-1 (vector-! (new 'stack-no-clear 'vector) (-> gp-0 1) (-> gp-0 0))))
          (vector-normalize! s4-1 40960.0)
          (vector+float*! (-> gp-0 0) (-> gp-0 0) s4-1 -1.0)
          (vector+float*! (-> gp-0 1) (-> gp-0 1) s4-1 1.0)
          )
        (+! (-> gp-0 0 y) 61440.0)
        (+! (-> gp-0 1 y) 61440.0)
        (spawn-security-wall this (-> gp-0 0) (-> gp-0 1) 102400.0 #t)
        )
      )
    (else
      (let ((gp-1 (-> this child)))
        (while gp-1
          (send-event (ppointer->process gp-1) 'off)
          (set! gp-1 (-> gp-1 0 brother))
          )
        )
      )
    )
  0
  (none)
  )

(defmethod init-from-entity! ((this krimson-wall) (arg0 entity-actor))
  "Set up a newly created process from the entity that created it."
  (let ((s4-0 (new 'process 'collide-shape this (collide-list-enum usually-hit-by-player))))
    (let ((s3-0 (new 'process 'collide-shape-prim-group s4-0 (the-as uint 1) 0)))
      (set! (-> s4-0 total-prims) (the-as uint 2))
      (set! (-> s3-0 prim-core collide-as) (collide-spec obstacle))
      (set! (-> s3-0 prim-core collide-with) (collide-spec jak player-list))
      (set! (-> s3-0 prim-core action) (collide-action solid rideable no-standon))
      (set-vector! (-> s3-0 local-sphere) 0.0 53248.0 0.0 65536.0)
      (set! (-> s4-0 root-prim) s3-0)
      )
    (let ((v1-8 (new 'process 'collide-shape-prim-mesh s4-0 (the-as uint 0) (the-as uint 1))))
      (set! (-> v1-8 prim-core collide-as) (collide-spec obstacle))
      (set! (-> v1-8 prim-core collide-with) (collide-spec jak player-list))
      (set! (-> v1-8 prim-core action) (collide-action solid no-standon))
      (set! (-> v1-8 transform-index) 3)
      (set-vector! (-> v1-8 local-sphere) 0.0 53248.0 0.0 65536.0)
      )
    (set! (-> s4-0 nav-radius) (* 0.75 (-> s4-0 root-prim local-sphere w)))
    (let ((v1-11 (-> s4-0 root-prim)))
      (set! (-> s4-0 backup-collide-as) (-> v1-11 prim-core collide-as))
      (set! (-> s4-0 backup-collide-with) (-> v1-11 prim-core collide-with))
      )
    (set! (-> this root) s4-0)
    )
  (set! (-> this entity) arg0)
  (process-drawable-from-entity! this arg0)
  (initialize-skeleton
    this
    (the-as skeleton-group (art-group-get-by-name *level* "skel-krimson-wall" (the-as (pointer level) #f)))
    (the-as pair 0)
    )
  (logclear! (-> this mask) (process-mask actor-pause))
  (transform-post)
  (set! (-> this collision-part) (res-lump-value arg0 'extra-id int :time -1000000000.0))
  (set! (-> this width) (res-lump-float arg0 'dist :default 81920.0))
  (set! (-> this alt-actor 0) (entity-actor-lookup arg0 'alt-actor 0))
  (set! (-> this alt-actor 1) (entity-actor-lookup arg0 'alt-actor 1))
  (format #t "collision part ~D~%" (-> this collision-part))
  (if (nonzero? (-> this collision-part))
      (krimson-wall-method-30 this #t)
      )
  (go (method-of-object this idle))
  )
