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

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

;; definition of type cavecrystal
(deftype cavecrystal (process-drawable)
  ((root-override              collide-shape         :offset        112)
   (is-master?                 symbol                :offset-assert 176)
   (crystal-id                 int32                 :offset-assert 180)
   (glow-u                     float                 :offset-assert 184)
   (glow-wf-period             int32                 :offset-assert 188)
   (glow-wf-offset             int32                 :offset-assert 192)
   (prev-compute-glow-time     time-frame            :offset-assert 200)
   (start-fade-time            time-frame            :offset-assert 208)
   (end-fade-time              time-frame            :offset-assert 216)
   (activated-time             time-frame            :offset-assert 224)
   (last-updated-user-lighting time-frame            :offset-assert 232)
   (player-attack-id           uint64                :offset-assert 240)
   (on-color-mult              vector        :inline :offset-assert 256)
   (on-color-emissive          vector        :inline :offset-assert 272)
   (off-color-mult             vector        :inline :offset-assert 288)
   (off-color-emissive         vector        :inline :offset-assert 304)
   )
  :heap-base #xd0
  :method-count-assert 22
  :size-assert         #x140
  :flag-assert         #x1600d00140
  (:methods
    (update-connected-crystals! (_type_) none 20)
    (compute-glow (_type_) float 21)
    )
  (:states
    cavecrystal-active
    cavecrystal-idle
    )
  )

;; definition for method 3 of type cavecrystal
(defmethod inspect cavecrystal ((obj cavecrystal))
  (let ((t9-0 (method-of-type process-drawable inspect)))
    (t9-0 obj)
    )
  (format #t "~T~Tis-master?: ~A~%" (-> obj is-master?))
  (format #t "~T~Tcrystal-id: ~D~%" (-> obj crystal-id))
  (format #t "~T~Tglow-u: ~f~%" (-> obj glow-u))
  (format #t "~T~Tglow-wf-period: ~D~%" (-> obj glow-wf-period))
  (format #t "~T~Tglow-wf-offset: ~D~%" (-> obj glow-wf-offset))
  (format #t "~T~Tprev-compute-glow-time: ~D~%" (-> obj prev-compute-glow-time))
  (format #t "~T~Tstart-fade-time: ~D~%" (-> obj start-fade-time))
  (format #t "~T~Tend-fade-time: ~D~%" (-> obj end-fade-time))
  (format #t "~T~Tactivated-time: ~D~%" (-> obj activated-time))
  (format #t "~T~Tlast-updated-user-lighting: ~D~%" (-> obj last-updated-user-lighting))
  (format #t "~T~Tplayer-attack-id: ~D~%" (-> obj player-attack-id))
  (format #t "~T~Ton-color-mult: #<vector @ #x~X>~%" (-> obj on-color-mult))
  (format #t "~T~Ton-color-emissive: #<vector @ #x~X>~%" (-> obj on-color-emissive))
  (format #t "~T~Toff-color-mult: #<vector @ #x~X>~%" (-> obj off-color-mult))
  (format #t "~T~Toff-color-emissive: #<vector @ #x~X>~%" (-> obj off-color-emissive))
  obj
  )

;; failed to figure out what this is:
(defskelgroup *cavecrystal-sg* cavecrystal cavecrystal-lod0-jg -1
              ((cavecrystal-lod0-mg (meters 20)) (cavecrystal-lod1-mg (meters 999999)))
              :bounds (static-spherem 0 4.7 0 5.4)
              )

;; definition for method 20 of type cavecrystal
;; INFO: Return type mismatch int vs none.
(defmethod update-connected-crystals! cavecrystal ((obj cavecrystal))
  (when (-> obj is-master?)
    (let ((v1-2 (-> *display* base-frame-counter)))
      (when (!= (-> obj last-updated-user-lighting) v1-2)
        (set! (-> obj last-updated-user-lighting) v1-2)
        (execute-connections *cavecrystal-light-control*)
        )
      )
    )
  (none)
  )

;; definition for method 21 of type cavecrystal
(defmethod compute-glow cavecrystal ((obj cavecrystal))
  (set! (-> obj prev-compute-glow-time) (-> *display* game-frame-counter))
  (let* ((gp-1 (max 1 (+ (- 1 (-> obj activated-time)) (-> *display* game-frame-counter))))
         (f0-2
           (/ (the float (mod (+ (-> *display* base-frame-counter) (-> obj glow-wf-offset)) (-> obj glow-wf-period)))
              (the float (-> obj glow-wf-period))
              )
           )
         (f30-1 (* 0.1 (cos (* 65536.0 f0-2))))
         )
    (cond
      ((>= (seconds 0.067) gp-1)
       (let ((v1-11 (* 0.05 (the float gp-1))))
         (fmax 0.0 (fmin 2.0 (+ (* 2.0 v1-11) (* v1-11 f30-1))))
         )
       )
      ((>= (seconds 0.6) gp-1)
       (let ((a2-0 (* 0.00625 (the float (+ gp-1 (seconds -0.067))))))
         (fmin 2.0 (+ (lerp 2.0 1.0 a2-0) f30-1))
         )
       )
      ((>= (-> obj start-fade-time) gp-1)
       (+ 1.0 f30-1)
       )
      (else
        (let ((v1-20 (fmin 1.0 (* 0.004761905 (the float (- gp-1 (-> obj start-fade-time)))))))
          (fmax 0.0 (- (+ 1.0 (* (- 1.0 v1-20) f30-1)) v1-20))
          )
        )
      )
    )
  )

;; failed to figure out what this is:
(defstate cavecrystal-idle (cavecrystal)
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (case arg2
      (('attack)
       (go cavecrystal-active)
       )
      )
    )
  :trans (behavior ()
    (if (and *target* (>= 40960.0 (vector-vector-distance (-> self root-override trans) (-> *target* control trans))))
        (level-hint-spawn
          (game-text-id darkcave-light-hint)
          "sksp0333"
          (the-as entity #f)
          *entity-pool*
          (game-task none)
          )
        )
    (update-connected-crystals! self)
    (none)
    )
  :code (behavior ()
    (logior! (-> self mask) (process-mask sleep-code))
    (suspend)
    0
    (none)
    )
  )

;; failed to figure out what this is:
(defstate cavecrystal-active (cavecrystal)
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (let ((v1-0 arg2))
      (the-as object (when (= v1-0 'attack)
                       (let ((v1-1 (-> arg3 param 2)))
                         (cond
                           ((!= v1-1 (-> self player-attack-id))
                            (set! (-> self player-attack-id) v1-1)
                            (set! (-> self activated-time) (-> *display* game-frame-counter))
                            #t
                            )
                           (else
                             #f
                             )
                           )
                         )
                       )
              )
      )
    )
  :enter (behavior ()
    (logclear! (-> self mask) (process-mask actor-pause))
    (set! (-> self activated-time) (-> *display* game-frame-counter))
    (set! (-> self prev-compute-glow-time) (-> *display* game-frame-counter))
    (none)
    )
  :exit (behavior ()
    (stop! (-> self sound))
    (if (not (-> self is-master?))
        (logior! (-> self mask) (process-mask actor-pause))
        )
    (none)
    )
  :trans (behavior ()
    (let ((f30-0 (compute-glow self)))
      (set! (-> self glow-u) f30-0)
      (let ((gp-0 (new 'stack-no-clear 'vector))
            (f28-0 1.993)
            )
        (let ((s5-0 (-> self draw color-mult)))
          (vector-! gp-0 (-> self on-color-mult) (-> self off-color-mult))
          (vector+*! s5-0 (-> self off-color-mult) gp-0 f30-0)
          (set! (-> s5-0 x) (fmax 0.0 (fmin (-> s5-0 x) f28-0)))
          (set! (-> s5-0 y) (fmax 0.0 (fmin (-> s5-0 y) f28-0)))
          (set! (-> s5-0 z) (fmax 0.0 (fmin (-> s5-0 z) f28-0)))
          (set! (-> s5-0 w) 1.0)
          )
        (let ((s5-1 (-> self draw color-emissive)))
          (vector-! gp-0 (-> self on-color-emissive) (-> self off-color-emissive))
          (vector+*! s5-1 (-> self off-color-emissive) gp-0 f30-0)
          (set! (-> s5-1 x) (fmax 0.0 (fmin (-> s5-1 x) f28-0)))
          (set! (-> s5-1 y) (fmax 0.0 (fmin (-> s5-1 y) f28-0)))
          (set! (-> s5-1 z) (fmax 0.0 (fmin (-> s5-1 z) f28-0)))
          (set! (-> s5-1 w) 0.0)
          )
        )
      (TODO-RENAME-9 *cavecrystal-light-control* (-> self crystal-id) (-> self glow-u) self)
      (update-connected-crystals! self)
      (when (>= 0.0 f30-0)
        (level-hint-spawn
          (game-text-id darkcave-light-end)
          "sksp0332"
          (the-as entity #f)
          *entity-pool*
          (game-task none)
          )
        (go cavecrystal-idle)
        )
      )
    (update! (-> self sound))
    (none)
    )
  :code (behavior ()
    (logior! (-> self mask) (process-mask sleep-code))
    (suspend)
    0
    (none)
    )
  )

;; definition for method 10 of type cavecrystal
(defmethod deactivate cavecrystal ((obj cavecrystal))
  (if (nonzero? (-> obj sound))
      (stop! (-> obj sound))
      )
  ((method-of-type process-drawable deactivate) obj)
  (none)
  )

;; definition for method 11 of type cavecrystal
;; INFO: Return type mismatch object vs none.
;; Used lq/sq
(defmethod init-from-entity! cavecrystal ((obj cavecrystal) (arg0 entity-actor))
  (set! (-> obj glow-u) 0.0)
  (set! (-> obj player-attack-id) (the-as uint 0))
  (set! (-> obj last-updated-user-lighting) 0)
  (set-vector! (-> obj off-color-mult) 0.0 0.0 0.0 1.0)
  (set-vector! (-> obj off-color-emissive) 0.0 0.0 0.0 0.0)
  (set-vector! (-> obj on-color-mult) 1.0 1.0 1.0 1.0)
  (set-vector! (-> obj on-color-emissive) 0.0 0.0 0.0 0.0)
  (let ((s4-0 (new 'process 'collide-shape obj (collide-list-enum hit-by-others))))
    (let ((s3-0 (new 'process 'collide-shape-prim-mesh s4-0 (the-as uint 0) (the-as uint 0))))
      (set! (-> s3-0 prim-core collide-as) (collide-kind wall-object))
      (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 indestructible))
      (set! (-> s3-0 transform-index) 0)
      (set-vector! (-> s3-0 local-sphere) 0.0 19251.2 0.0 22118.4)
      (set-root-prim! s4-0 s3-0)
      )
    (set! (-> s4-0 nav-radius) 4915.2)
    (backup-collide-with-as s4-0)
    (set! (-> obj root-override) s4-0)
    )
  (set! (-> obj link) (new 'process 'actor-link-info obj))
  (set! (-> obj crystal-id) (actor-count-before (-> obj link)))
  (set! (-> obj is-master?) (= (-> obj crystal-id) 3))
  (set! (-> obj glow-wf-period) (rand-vu-int-range 225 375))
  (set! (-> obj glow-wf-offset) (rand-vu-int-range 0 300))
  (if (-> obj is-master?)
      (logclear! (-> obj mask) (process-mask actor-pause))
      )
  (set! (-> obj start-fade-time)
        (the-as time-frame (the int (* 300.0 (res-lump-float (-> obj entity) 'timeout :default 8.0))))
        )
  (process-drawable-from-entity! obj arg0)
  (initialize-skeleton obj *cavecrystal-sg* '())
  (nav-mesh-connect obj (-> obj root-override) (the-as nav-control #f))
  (set! (-> obj draw color-mult quad) (-> obj off-color-mult quad))
  (set! (-> obj draw color-emissive quad) (-> obj off-color-emissive quad))
  (ja-channel-push! 1 0)
  (let ((s5-1 (-> obj skel root-channel 0)))
    (joint-control-channel-group-eval!
      s5-1
      (the-as art-joint-anim (-> obj draw art-group data 3))
      num-func-identity
      )
    (set! (-> s5-1 frame-num) 0.0)
    )
  (ja-post)
  (update-transforms! (-> obj root-override))
  (TODO-RENAME-9 *cavecrystal-light-control* (-> obj crystal-id) 0.0 obj)
  (set! (-> obj sound)
        (new 'process 'ambient-sound (static-sound-spec "crystal-on" :fo-max 80) (-> obj root-override trans))
        )
  (go cavecrystal-idle)
  (none)
  )
