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

(import "goal_src/jak1/import/darkvine-ag.gc")

;; definition of type darkvine
(deftype darkvine (process-drawable)
  ((root-override    collide-shape  :offset        112)
   (speed            float          :offset-assert 176)
   (tip-index        int8           :offset-assert 180)
   (dangerous        symbol         :offset-assert 184)
   (vulnerable       symbol         :offset-assert 188)
   (hit-player       symbol         :offset-assert 192)
   (touch-time       time-frame     :offset-assert 200)
   (player-attack-id int32          :offset-assert 208)
   )
  :heap-base #x70
  :method-count-assert 20
  :size-assert         #xd4
  :flag-assert         #x14007000d4
  (:states
    (darkvine-die symbol)
    darkvine-idle
    darkvine-retreat
    )
  )

;; definition for method 3 of type darkvine
(defmethod inspect darkvine ((obj darkvine))
  (let ((t9-0 (method-of-type process-drawable inspect)))
    (t9-0 obj)
    )
  (format #t "~T~Tspeed: ~f~%" (-> obj speed))
  (format #t "~T~Ttip-index: ~D~%" (-> obj tip-index))
  (format #t "~T~Tdangerous: ~A~%" (-> obj dangerous))
  (format #t "~T~Tvulnerable: ~A~%" (-> obj vulnerable))
  (format #t "~T~Thit-player: ~A~%" (-> obj hit-player))
  (format #t "~T~Ttouch-time: ~D~%" (-> obj touch-time))
  (format #t "~T~Tplayer-attack-id: ~D~%" (-> obj player-attack-id))
  obj
  )

;; definition for method 12 of type darkvine
(defmethod run-logic? darkvine ((obj darkvine))
  (or (zero? (logand (-> obj mask) (process-mask actor-pause)))
      (or (and (nonzero? (-> obj draw))
               (logtest? (-> obj draw status) (draw-status was-drawn))
               (>= (+ (-> *ACTOR-bank* pause-dist) (-> obj root-override pause-adjust-distance))
                   (vector-vector-distance (-> obj root-override trans) (math-camera-pos))
                   )
               )
          (and (nonzero? (-> obj skel)) (!= (-> obj skel root-channel 0) (-> obj skel channel)))
          (and (nonzero? (-> obj draw)) (logtest? (-> obj draw status) (draw-status no-skeleton-update)))
          )
      )
  )

;; failed to figure out what this is:
(defskelgroup *darkvine-sg* darkvine darkvine-lod0-jg darkvine-idle-ja
              ((darkvine-lod0-mg (meters 20)) (darkvine-lod1-mg (meters 999999)))
              :bounds (static-spherem 0 2 0 3.5)
              )

;; failed to figure out what this is:
(defpartgroup group-darkvine-puffs
  :id 175
  :duration 150
  :flags (use-local-clock)
  :bounds (static-bspherem 0 2 0 3)
  :parts ((sp-item 800) (sp-item 801) (sp-item 802))
  )

;; failed to figure out what this is:
(defpart 800
  :init-specs ((sp-tex spt-texture (new 'static 'texture-id :index #x19 :page #x2))
    (sp-flt spt-num 0.8)
    (sp-rnd-flt spt-x (meters 0) (meters 0.5) 1.0)
    (sp-rnd-flt spt-scale-x (meters 1) (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 30.0 20.0 1.0)
    (sp-rnd-flt spt-g 25.0 10.0 1.0)
    (sp-rnd-flt spt-b 10.0 5.0 1.0)
    (sp-rnd-flt spt-a 0.0 32.0 1.0)
    (sp-rnd-flt spt-vel-x (meters 0.0016666667) (meters 0.0033333334) 1.0)
    (sp-flt spt-vel-y (meters 0.013333334))
    (sp-rnd-int-flt spt-rotvel-z (degrees -0.3) 1 109.22667)
    (sp-flt spt-fade-a 0.1882353)
    (sp-flt spt-accel-y -2.048)
    (sp-int spt-timer 255)
    (sp-cpuinfo-flags bit2 bit12)
    (sp-rnd-flt spt-rotate-y (degrees 0.0) (degrees 360.0) 1.0)
    )
  )

;; failed to figure out what this is:
(defpart 802
  :init-specs ((sp-tex spt-texture (new 'static 'texture-id :index #x1d :page #x2))
    (sp-flt spt-num 0.25)
    (sp-rnd-flt spt-x (meters 0) (meters 0.6) 1.0)
    (sp-rnd-flt spt-scale-x (meters 0.025) (meters 0.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 45.0 45.0 1.0)
    (sp-rnd-flt spt-g 45.0 10.0 1.0)
    (sp-rnd-flt spt-b 20.0 6.0 1.0)
    (sp-flt spt-a 96.0)
    (sp-rnd-flt spt-vel-x (meters 0.0016666667) (meters 0.0033333334) 1.0)
    (sp-rnd-flt spt-vel-y (meters 0.013333334) (meters 0.013333334) 1.0)
    (sp-rnd-int-flt spt-rotvel-z (degrees -0.3) 1 109.22667)
    (sp-flt spt-accel-y -2.7306666)
    (sp-int spt-timer 375)
    (sp-cpuinfo-flags bit2 bit12)
    (sp-rnd-flt spt-rotate-y (degrees 0.0) (degrees 360.0) 1.0)
    )
  )

;; failed to figure out what this is:
(defpart 801
  :init-specs ((sp-tex spt-texture (new 'static 'texture-id :index #x17 :page #x2))
    (sp-flt spt-num 0.5)
    (sp-rnd-flt spt-x (meters 0) (meters 0.6) 1.0)
    (sp-rnd-flt spt-scale-x (meters 0.3) (meters 0.4) 1.0)
    (sp-rnd-flt spt-rot-z (degrees 0.0) (degrees 360.0) 1.0)
    (sp-rnd-flt spt-scale-y (meters 0.1) (meters 0.1) 1.0)
    (sp-rnd-flt spt-r 10.0 6.0 1.0)
    (sp-rnd-flt spt-g 40.0 90.0 1.0)
    (sp-rnd-flt spt-b 10.0 6.0 1.0)
    (sp-flt spt-a 96.0)
    (sp-rnd-flt spt-vel-x (meters 0.0016666667) (meters 0.0033333334) 1.0)
    (sp-rnd-flt spt-vel-y (meters 0.01) (meters 0.013333334) 1.0)
    (sp-rnd-int-flt spt-rotvel-z (degrees -1.2) 1 436.90668)
    (sp-flt spt-accel-y -1.3653333)
    (sp-int spt-timer 600)
    (sp-cpuinfo-flags bit2 bit12)
    (sp-rnd-flt spt-rotate-y (degrees 0.0) (degrees 360.0) 1.0)
    )
  )

;; definition for function darkvine-event-handler
;; INFO: Return type mismatch none vs object.
;; WARN: rewrite_to_get_var got a none typed variable. Is there unreachable code? [OP: 50]
;; WARN: rewrite_to_get_var got a none typed variable. Is there unreachable code? [OP: 52]
(defbehavior darkvine-event-handler darkvine ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
  (let ((v1-0 arg2))
    (the-as object (cond
                     ((= v1-0 'touch)
                      (do-push-aways! (-> self root-override))
                      (when (-> self dangerous)
                        (if (send-event arg0 'attack (-> arg3 param 0) (new 'static 'attack-info))
                            (set-collide-offense (-> self root-override) 2 (collide-offense no-offense))
                            )
                        )
                      )
                     ((= v1-0 'attack)
                      (let ((v1-10 (-> arg3 param 2)))
                        (cond
                          ((!= v1-10 (-> self player-attack-id))
                           (set! (-> self player-attack-id) (the-as int v1-10))
                           (when (-> self vulnerable)
                             (do-push-aways! (-> self root-override))
                             (go darkvine-retreat)
                             )
                           )
                          (else
                            'push
                            )
                          )
                        )
                      )
                     )
            )
    )
  )

;; failed to figure out what this is:
(defstate darkvine-idle (darkvine)
  :event darkvine-event-handler
  :code (behavior ()
    (set! (-> self dangerous) #t)
    (set! (-> self vulnerable) #t)
    (let ((f30-0 0.0))
      (loop
        (if (logtest? (get-reminder (get-task-control (game-task jungle-plant)) 0) 1)
            (go darkvine-die #f)
            )
        (ja-no-eval :group! darkvine-idle-ja :num! (seek! max (-> self speed)) :frame-num 0.0)
        (until (ja-done? 0)
          (if (and (>= (ja-aframe-num 0) 120.0) (>= 180.0 (ja-aframe-num 0)))
              (seek-toward-yaw-angle! (-> self root-override) f30-0 32768.0 (seconds 0.5))
              )
          (suspend)
          (ja :num! (seek! max (-> self speed)))
          )
        (set! f30-0 (if (rand-vu-percent? 0.5)
                        (+ 16384.0 f30-0)
                        (+ -16384.0 f30-0)
                        )
              )
        )
      )
    (none)
    )
  :post (behavior ()
    (when (and (-> self hit-player)
               (or (not *target*) (>= (- (-> *display* base-frame-counter) (-> self touch-time)) (seconds 0.05)))
               )
      (set-collide-offense (-> self root-override) 2 (collide-offense indestructible))
      (set! (-> self hit-player) #f)
      )
    (transform-post)
    (if *target*
        (look-at-enemy!
          (-> *target* neck)
          (vector<-cspace! (new 'stack-no-clear 'vector) (-> self node-list data (-> self tip-index)))
          'nothing
          self
          )
        )
    (none)
    )
  )

;; failed to figure out what this is:
(defstate darkvine-retreat (darkvine)
  :event darkvine-event-handler
  :code (behavior ()
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (set! (-> self dangerous) #f)
    (set! (-> self vulnerable) #f)
    (logclear! (-> self mask) (process-mask actor-pause))
    (ja-channel-push! 1 (seconds 0.15))
    (ja-no-eval :group! darkvine-retreat-ja :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (sp-launch-particles-var
        *sp-particle-system-2d*
        (-> *part-id-table* 800)
        (-> self root-override trans)
        (the-as sparticle-launch-state #f)
        (the-as sparticle-launch-control #f)
        1.0
        )
      (suspend)
      (ja :num! (seek!))
      )
    (ja-channel-set! 0)
    (let ((gp-0 (-> *display* base-frame-counter)))
      (until (>= (- (-> *display* base-frame-counter) gp-0) (seconds 2))
        (suspend)
        )
      )
    (process-spawn
      part-tracker
      :init part-tracker-init
      (-> *part-group-id-table* 175)
      -1
      #f
      #f
      #f
      (-> self root-override trans)
      :to *entity-pool*
      )
    (let ((gp-2 (-> *display* base-frame-counter)))
      (until (>= (- (-> *display* base-frame-counter) gp-2) (seconds 0.5))
        (suspend)
        )
      )
    (set! (-> self dangerous) #t)
    (logior! (-> self mask) (process-mask actor-pause))
    (ja-channel-set! 1)
    (ja-no-eval :group! darkvine-pushup-ja :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek!))
      )
    (go darkvine-idle)
    (none)
    )
  :post (-> darkvine-idle post)
  )

;; failed to figure out what this is:
(defstate darkvine-die (darkvine)
  :code (behavior ((arg0 symbol))
    (logclear! (-> self mask) (process-mask actor-pause))
    (if arg0
        (ja-channel-set! 1)
        (ja-channel-push! 1 (seconds 0.5))
        )
    (clear-collide-with-as (-> self root-override))
    (ja :group! darkvine-dead-ja :num! min)
    (while (!= (-> self skel root-channel 0) (-> self skel channel))
      (suspend)
      )
    (logior! (-> self mask) (process-mask sleep))
    (anim-loop)
    (none)
    )
  :post (-> darkvine-idle post)
  )

;; definition for method 11 of type darkvine
;; INFO: Return type mismatch object vs none.
(defmethod init-from-entity! darkvine ((obj darkvine) (arg0 entity-actor))
  (set! (-> obj mask) (logior (process-mask enemy) (-> obj mask)))
  (let ((s4-0 (new 'process 'collide-shape obj (collide-list-enum hit-by-player))))
    (let ((s3-0 (new 'process 'collide-shape-prim-group s4-0 (the-as uint 4) 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 ca-10 ca-11))
      (set-vector! (-> s3-0 local-sphere) 0.0 8192.0 0.0 16384.0)
      (set-root-prim! s4-0 s3-0)
      (let ((s2-0 (new 'process 'collide-shape-prim-sphere s4-0 (the-as uint 2))))
        (set! (-> s2-0 prim-core collide-as) (collide-kind enemy))
        (set! (-> s2-0 collide-with) (collide-kind target))
        (set! (-> s2-0 prim-core action) (collide-action solid ca-10 ca-11))
        (set! (-> s2-0 prim-core offense) (collide-offense indestructible))
        (set! (-> s2-0 transform-index) 5)
        (set-vector! (-> s2-0 local-sphere) 0.0 0.0 0.0 3276.8)
        (append-prim s3-0 s2-0)
        )
      (let ((s2-1 (new 'process 'collide-shape-prim-sphere s4-0 (the-as uint 2))))
        (set! (-> s2-1 prim-core collide-as) (collide-kind enemy))
        (set! (-> s2-1 collide-with) (collide-kind target))
        (set! (-> s2-1 prim-core action) (collide-action solid ca-10 ca-11))
        (set! (-> s2-1 prim-core offense) (collide-offense indestructible))
        (set! (-> s2-1 transform-index) 6)
        (set-vector! (-> s2-1 local-sphere) 0.0 0.0 0.0 2867.2)
        (append-prim s3-0 s2-1)
        )
      (let ((s2-2 (new 'process 'collide-shape-prim-sphere s4-0 (the-as uint 2))))
        (set! (-> s2-2 prim-core collide-as) (collide-kind enemy))
        (set! (-> s2-2 collide-with) (collide-kind target))
        (set! (-> s2-2 prim-core action) (collide-action solid ca-10 ca-11))
        (set! (-> s2-2 prim-core offense) (collide-offense indestructible))
        (set! (-> s2-2 transform-index) 7)
        (set-vector! (-> s2-2 local-sphere) 0.0 0.0 0.0 2457.6)
        (append-prim s3-0 s2-2)
        )
      (let ((s2-3 (new 'process 'collide-shape-prim-sphere s4-0 (the-as uint 2))))
        (set! (-> s2-3 prim-core collide-as) (collide-kind enemy))
        (set! (-> s2-3 collide-with) (collide-kind target))
        (set! (-> s2-3 prim-core action) (collide-action solid ca-10 ca-11))
        (set! (-> s2-3 prim-core offense) (collide-offense indestructible))
        (set! (-> s2-3 transform-index) 8)
        (set-vector! (-> s2-3 local-sphere) 0.0 0.0 0.0 2048.0)
        (append-prim s3-0 s2-3)
        )
      )
    (set! (-> s4-0 nav-radius) 2048.0)
    (backup-collide-with-as s4-0)
    (set! (-> obj root-override) s4-0)
    )
  (set! (-> obj tip-index) 8)
  (process-drawable-from-entity! obj arg0)
  (initialize-skeleton obj *darkvine-sg* '())
  (nav-mesh-connect obj (-> obj root-override) (the-as nav-control #f))
  (set! (-> obj hit-player) #f)
  (set! (-> obj speed) (rand-vu-float-range 0.95 1.05))
  (if (logtest? (get-reminder (get-task-control (game-task jungle-plant)) 0) 1)
      (go darkvine-die #t)
      )
  (go darkvine-idle)
  (none)
  )
