;;-*-Lisp-*-
(in-package goal)
(bundles "BEA.DGO")
(require "engine/common-obs/process-taskable.gc")
(declare-type muse nav-enemy)

;; DECOMP BEGINS

(deftype sculptor (process-taskable)
  ((muse handle)))

(defskelgroup *sculptor-muse-sg*
  sculptor-muse
  sculptor-muse-lod0-jg
  sculptor-muse-idle-ja
  ((sculptor-muse-lod0-mg (meters 999999)))
  :bounds (static-spherem 0 0 0 2.5))

(defskelgroup *sculptor-sg*
  sculptor
  sculptor-lod0-jg
  sculptor-sigh-ja
  ((sculptor-lod0-mg (meters 999999)))
  :bounds (static-spherem 0 0 0 2.5)
  :shadow sculptor-shadow-mg)

(defmethod process-taskable-method-52 ((this sculptor))
  (let ((v1-1 (-> this draw shadow-ctrl)))
    (when v1-1
      (let ((f0-0 (-> this root trans y)))
        (let ((a0-2 v1-1)) (set! (-> a0-2 settings bot-plane w) (- (+ -2048.0 f0-0))))
        0
        (set! (-> v1-1 settings top-plane w) (- (+ 2048.0 f0-0))))
      0))
  (none))

(defmethod draw-npc-shadow ((this sculptor))
  (-> this draw shadow-ctrl)
  (cond
    ((and (-> this draw shadow) (zero? (-> this draw cur-lod)) (logtest? (-> this draw status) (draw-status was-drawn)))
     (let ((v1-9 (-> this draw shadow-ctrl))) (logclear! (-> v1-9 settings flags) (shadow-flags disable-draw)))
     0
     (update-direction-from-time-of-day (-> this draw shadow-ctrl)))
    (else (let ((v1-14 (-> this draw shadow-ctrl))) (logior! (-> v1-14 settings flags) (shadow-flags disable-draw))) 0))
  (none))

(defbehavior muse-to-idle sculptor ((arg0 muse))
  (if (not (handle->process (-> arg0 incomming-attack-id)))
    (set! (-> arg0 incomming-attack-id)
          (ppointer->handle (manipy-spawn (-> arg0 collide-info trans) (-> arg0 entity) *sculptor-muse-sg* #f :to arg0))))
  (let ((v1-11 (handle->process (-> arg0 incomming-attack-id))))
    (if v1-11 (set! (-> (the-as muse v1-11) draw light-index) (the-as uint 3))))
  (send-event (handle->process (-> arg0 incomming-attack-id)) 'anim-mode 'loop)
  (send-event (handle->process (-> arg0 incomming-attack-id)) 'art-joint-anim "idle" 0)
  (send-event (handle->process (-> arg0 incomming-attack-id)) 'draw #t)
  (send-event (handle->process (-> arg0 incomming-attack-id)) 'origin-joint-index 26))

(defstate give-cell (sculptor)
  :virtual #t
  :enter
    (behavior ()
      (muse-to-idle (the-as muse self))))

(defmethod play-anim! ((this sculptor) (arg0 symbol))
  (case (current-status (-> this tasks))
    (((task-status need-hint) (task-status need-introduction))
     (if arg0 (close-status! (-> this tasks) (task-status need-introduction)))
     (new 'static
          'spool-anim
          :name "sculptor-introduction"
          :index 16
          :parts 14
          :command-list
          '((0 display-level beach special)
           (0 kill "med-res-level-2")
           (0 kill "med-res-level-4")
           (0 kill "med-res-level-6")
           (0 kill "med-res-level-7")
           (0 kill "med-res-level-8")
           (0 kill "med-res-level-11")
           (0 kill "med-res-level-14")
           (0 kill "med-res-level-22")
           (0 kill "med-res-level-23")
           (285 joint "cameraB")
           (331 joint "camera")
           (614 joint "cameraB")
           (760 joint "camera")
           (1183 joint "cameraB")
           (1278 joint "camera")
           (1433 joint "cameraB"))))
    (((task-status need-reminder))
     (set! (-> this skippable) #t)
     (new 'static 'spool-anim :name "sculptor-reminder-1" :index 17 :parts 2 :command-list '()))
    (((task-status need-reward-speech))
     (when arg0
       (set! (-> this cell-for-task) (current-task (-> this tasks)))
       (close-current! (-> this tasks))
       (set! (-> this muse)
             (ppointer->handle (manipy-spawn (-> this root trans) (-> this entity) *sculptor-muse-sg* #f :to this)))
       (let ((v1-18 (handle->process (-> this muse))))
         (if v1-18 (set! (-> (the-as muse v1-18) draw light-index) (the-as uint 3))))
       (send-event (handle->process (-> this muse)) 'center-joint 4)
       (send-event (handle->process (-> this muse)) 'anim-mode 'clone-anim))
     (new 'static
          'spool-anim
          :name "sculptor-resolution"
          :index 18
          :parts 4
          :command-list
          '((51 joint "cameraB") (87 joint "camera"))))
    (else
     (if arg0
       (format 0
               "ERROR: <GMJ>: ~S playing anim for task status ~S~%"
               (-> this name)
               (task-status->string (current-status (-> this tasks)))))
     (-> this draw art-group data 4))))

(defmethod get-art-elem ((this sculptor))
  (case (current-status (-> this tasks))
    (((task-status invalid) (task-status need-resolution)) (-> this draw art-group data 11))
    (else (-> this draw art-group data 3))))

(defmethod process-taskable-method-43 ((this sculptor))
  (when (ambient-control-method-10 (-> this ambient) (new 'stack-no-clear 'vector) (seconds 30) 122880.0 this)
    (let ((f0-2 (rand-float-gen)))
      (cond
        ((< 0.85714287 f0-2) (play-ambient (-> this ambient) "SCU-LO01" #f (-> this root trans)))
        ((< 0.71428573 f0-2) (play-ambient (-> this ambient) "SCU-AM05" #f (-> this root trans)))
        ((< 0.5714286 f0-2) (play-ambient (-> this ambient) "SCU-AM06" #f (-> this root trans)))
        ((< 0.42857143 f0-2) (play-ambient (-> this ambient) "SCU-AM03" #f (-> this root trans)))
        ((< 0.2857143 f0-2) (play-ambient (-> this ambient) "SCU-AM04" #f (-> this root trans)))
        ((< 0.14285715 f0-2) (play-ambient (-> this ambient) "SCU-AM01" #f (-> this root trans)))
        (else (play-ambient (-> this ambient) "SCU-AM02" #f (-> this root trans)))))))

(defstate idle (sculptor)
  :virtual #t
  :code
    (behavior ()
      (when (!= (ja-group) (get-art-elem self))
        (ja-channel-push! 1 (seconds 0.2))
        (ja :group! (get-art-elem self)))
      (ja-no-eval :num! (seek!))
      (while (not (ja-done? 0))
        (suspend)
        (ja-eval))
      (case (current-status (-> self tasks))
        (((task-status invalid) (task-status need-resolution))
         (ja-no-eval :group! sculptor-from-looking-ja :num! (seek!) :frame-num 0.0)
         (until (ja-done? 0)
           (suspend)
           (ja :num! (seek!)))
         (muse-to-idle (the-as muse self))))
      (loop
        (let ((v1-43 (current-status (-> self tasks))))
          (cond
            ((or (= v1-43 (task-status invalid)) (= v1-43 (task-status need-resolution)))
             (ja-no-eval :group! sculptor-to-small-ja :num! (seek!) :frame-num 0.0)
             (until (ja-done? 0)
               (suspend)
               (ja :num! (seek!)))
             (let* ((f30-0 4.0)
                    (v1-68 (/ (the-as int (rand-uint31-gen *random-generator*)) 256))
                    (v1-69 (the-as number (logior #x3f800000 v1-68))))
               (countdown (gp-1 (+ (the int (* f30-0 (+ -1.0 (the-as float v1-69)))) 4))
                 (ja-no-eval :group! sculptor-small-ja :num! (seek!) :frame-num 0.0)
                 (until (ja-done? 0)
                   (suspend)
                   (ja :num! (seek!)))))
             (let ((f30-1 (rand-float-gen)))
               (ja-no-eval :group! sculptor-small-to-looking-ja :num! (seek!) :frame-num 0.0)
               (until (ja-done? 0)
                 (suspend)
                 (ja :num! (seek!)))
               (ja-no-eval :group! sculptor-looking-ja :num! (seek!) :frame-num 0.0)
               (until (ja-done? 0)
                 (suspend)
                 (ja :num! (seek!)))
               (ja-no-eval :group! sculptor-from-looking-ja :num! (seek!) :frame-num 0.0)
               (until (ja-done? 0)
                 (suspend)
                 (ja :num! (seek!)))
               (when (< f30-1 0.5)
                 (ja-no-eval :group! sculptor-to-huge-ja :num! (seek!) :frame-num 0.0)
                 (until (ja-done? 0)
                   (suspend)
                   (ja :num! (seek!)))
                 (let* ((f30-2 2.0)
                        (v1-190 (/ (the-as int (rand-uint31-gen *random-generator*)) 256))
                        (v1-191 (the-as number (logior #x3f800000 v1-190))))
                   (countdown (gp-2 (+ (the int (* f30-2 (+ -1.0 (the-as float v1-191)))) 2))
                     (ja-no-eval :group! sculptor-huge-ja :num! (seek!) :frame-num 0.0)
                     (until (ja-done? 0)
                       (suspend)
                       (ja :num! (seek!)))))
                 (ja-no-eval :group! sculptor-huge-to-looking-ja :num! (seek!) :frame-num 0.0)
                 (until (ja-done? 0)
                   (suspend)
                   (ja :num! (seek!)))
                 (ja-no-eval :group! sculptor-looking-ja :num! (seek!) :frame-num 0.0)
                 (until (ja-done? 0)
                   (suspend)
                   (ja :num! (seek!)))
                 (ja-no-eval :group! sculptor-from-looking-ja :num! (seek!) :frame-num 0.0)
                 (until (ja-done? 0)
                   (suspend)
                   (ja :num! (seek!))))))
            ((begin
               (process-taskable-method-43 self)
               (let* ((f30-3 4.0)
                      (v1-287 (/ (the-as int (rand-uint31-gen *random-generator*)) 256))
                      (v1-288 (the-as number (logior #x3f800000 v1-287))))
                 (countdown (gp-3 (+ (the int (* f30-3 (+ -1.0 (the-as float v1-288)))) 4))
                   (ja-no-eval :group! sculptor-idle-ja :num! (seek!) :frame-num 0.0)
                   (until (ja-done? 0)
                     (suspend)
                     (ja :num! (seek!)))))
               (< (rand-float-gen) 0.5))
             (ja-no-eval :group! sculptor-sigh-ja :num! (seek!) :frame-num 0.0)
             (until (ja-done? 0)
               (suspend)
               (ja :num! (seek!))))
            (else
             (ja-no-eval :group! sculptor-strikestart-ja :num! (seek!) :frame-num 0.0)
             (until (ja-done? 0)
               (suspend)
               (ja :num! (seek!)))
             (ja-no-eval :group! sculptor-strikemiddle-ja :num! (seek!) :frame-num 0.0)
             (until (ja-done? 0)
               (suspend)
               (ja :num! (seek!)))
             (ja-no-eval :group! sculptor-strikeend-ja :num! (seek!) :frame-num 0.0)
             (until (ja-done? 0)
               (suspend)
               (ja :num! (seek!)))))))))

(defmethod init-from-entity! ((this sculptor) (arg0 entity-actor))
  (process-taskable-method-40 this arg0 *sculptor-sg* 3 40 (new 'static 'vector :w 4096.0) 5)
  (set! (-> this tasks) (get-task-control (game-task misty-muse)))
  (set! (-> this muse) (the-as handle #f))
  (set! (-> this sound-flava) (music-flava sculptor))
  (set! (-> this draw light-index) (the-as uint 3))
  (process-taskable-method-42 this)
  (none))
