;;-*-Lisp-*-
(in-package goal)
(bundles "ENGINE.CGO" "GAME.CGO")
(require "engine/level/bsp.gc")
(require "engine/ui/text.gc")
(require "engine/game/task/hint-control.gc")
(require "engine/game/settings.gc")
(define-extern level-hint-init-by-other (function text-id string entity none :behavior level-hint))

(define-extern ambient-hint-init-by-other (function string vector symbol none :behavior level-hint))

;; DECOMP BEGINS

(defmethod mem-usage ((this drawable-ambient) (usage memory-usage-block) (flags int))
  (set! (-> usage length) (max 50 (-> usage length)))
  (set! (-> usage data 49 name) "ambient")
  (+! (-> usage data 49 count) 1)
  (let ((v1-6 (asize-of this))) (+! (-> usage data 49 used) v1-6) (+! (-> usage data 49 total) (logand -16 (+ v1-6 15))))
  (mem-usage (-> this ambient) usage (logior flags 128))
  (the-as drawable-ambient 0))

(defmethod mem-usage ((this drawable-inline-array-ambient) (usage memory-usage-block) (flags int))
  (set! (-> usage length) (max 1 (-> usage length)))
  (set! (-> usage data 0 name) (symbol->string 'drawable-group))
  (+! (-> usage data 0 count) 1)
  (let ((v1-7 32)) (+! (-> usage data 0 used) v1-7) (+! (-> usage data 0 total) (logand -16 (+ v1-7 15))))
  (dotimes (s3-0 (-> this length))
    (mem-usage (-> this data s3-0) usage flags))
  (the-as drawable-inline-array-ambient 0))

(define *hint-semaphore* (the-as (pointer level-hint) #f))

(defun level-hint-process-cmd ((arg0 (pointer int32)) (arg1 int) (arg2 int))
  (let ((v1-2 (-> arg0 arg1))
        (gp-0 (+ arg1 1)))
    (cond
      ((< gp-0 arg2)
       (let ((a0-2 (-> arg0 gp-0))
             (a1-2 v1-2))
         (cond
           ((zero? a1-2) (if (task-known? (the-as game-task a0-2)) (set! gp-0 -1)))
           ((= a1-2 1) (if (not (task-known? (the-as game-task a0-2))) (set! gp-0 -1)))
           ((= a1-2 2) (if (nonzero? (get-task-status (the-as game-task a0-2))) (set! gp-0 -1)))
           ((= a1-2 3) (if (!= (get-task-status (the-as game-task a0-2)) (task-status need-introduction)) (set! gp-0 -1)))
           ((= a1-2 4) (if (!= (get-task-status (the-as game-task a0-2)) (task-status need-reminder)) (set! gp-0 -1)))
           ((= a1-2 5) (if (!= (get-task-status (the-as game-task a0-2)) (task-status need-reminder-a)) (set! gp-0 -1)))
           ((= a1-2 13)
            (let ((v1-15 (get-task-status (the-as game-task a0-2))))
              (if (or (< (the-as int v1-15) 2) (< 4 (the-as int v1-15))) (set! gp-0 -1))))
           ((= a1-2 6) (if (!= (get-task-status (the-as game-task a0-2)) (task-status need-reward-speech)) (set! gp-0 -1)))
           ((= a1-2 7) (close-specific-task! (the-as game-task a0-2) (task-status need-hint)))
           ((= a1-2 8) (close-specific-task! (the-as game-task a0-2) (task-status need-introduction)))
           ((= a1-2 9) (close-specific-task! (the-as game-task a0-2) (task-status need-reminder)))
           ((= a1-2 10) (close-specific-task! (the-as game-task a0-2) (task-status need-reminder-a)))
           ((= a1-2 11) (close-specific-task! (the-as game-task a0-2) (task-status need-reward-speech)))
           ((= a1-2 12) (close-specific-task! (the-as game-task a0-2) (task-status need-resolution)))
           (else (format #t "Unknown hint command ~D~%" v1-2)))))
      (else
       (format #t
               "Missing task ID for hint command ~S~%"
               (cond
                 ((= v1-2 13) "if-at-most-need-reminder-a")
                 ((= v1-2 12) "close-need-resolution")
                 ((= v1-2 11) "close-need-reward-speech")
                 ((= v1-2 10) "close-need-reminder-a")
                 ((= v1-2 9) "close-need-reminder")
                 ((= v1-2 8) "close-need-introduction")
                 ((= v1-2 7) "close-need-hint")
                 ((= v1-2 6) "if-need-reward-speech")
                 ((= v1-2 5) "if-need-reminder-a")
                 ((= v1-2 4) "if-need-reminder")
                 ((= v1-2 3) "if-need-introduction")
                 ((= v1-2 2) "if-resolved")
                 ((= v1-2 1) "if-known")
                 ((zero? v1-2) "if-unknown")
                 (else "*unknown*")))))
    gp-0))

(defun level-hint-task-process ((arg0 entity) (arg1 uint128) (arg2 string))
  (local-vars (sv-16 res-tag))
  (let ((gp-0 (res-lump-value arg0 'text-id int :default arg1))
        (s5-0 0))
    (cond
      ((can-hint-be-played? (the-as text-id gp-0) arg0 arg2)
       (set! sv-16 (new 'static 'res-tag))
       (let ((s4-1 ((method-of-type res-lump get-property-data)
                    arg0
                    'cmds
                    'exact
                    -1000000000.0
                    (the-as pointer #f)
                    (& sv-16)
                    *res-static-buf*)))
         (when s4-1
           (while (and (>= s5-0 0) (< s5-0 (the-as int (-> sv-16 elt-count))))
             (set! s5-0 (level-hint-process-cmd (the-as (pointer int32) s4-1) s5-0 (the-as int (-> sv-16 elt-count))))
             (if (>= s5-0 0) (set! s5-0 (+ s5-0 1))))))
       0)
      (else (set! s5-0 -1)))
    (cond
      ((>= s5-0 0) (empty) gp-0)
      (else -1))))

(defun level-hint-surpress! ()
  (set-time! (-> *game-info* hint-play-time))
  0
  (none))

(defun can-grab-display? ((arg0 process))
  (let ((v1-2 (handle->process (-> *game-info* display-text-handle))))
    (when (and (or (not v1-2) (= v1-2 arg0) (time-elapsed? (-> *game-info* display-text-time) (seconds 0.1)))
               (and *target*
                    (!= (-> *target* next-state name) 'target-look-around)
                    (not (logtest? (-> *target* state-flags) (state-flags being-attacked dying)))
                    (= *master-mode* 'game)))
      (set! (-> *game-info* display-text-handle) (process->handle arg0))
      (set-time! (-> *game-info* display-text-time))
      #t)))

(defun level-hint-displayed? ()
  (let ((a0-0 (ppointer->process *hint-semaphore*)))
    (the-as symbol
            (and (the-as level-hint a0-0)
                 (and (= (-> (the-as level-hint a0-0) next-state name) 'level-hint-normal)
                      (not (appeared-for-long-enough? (the-as level-hint a0-0))))))))

(defun level-hint-spawn ((arg0 text-id) (arg1 string) (arg2 entity) (arg3 process-tree) (arg4 game-task))
  (if (< (the-as uint 1) (the-as uint arg4)) (close-specific-task! arg4 (task-status need-hint)))
  (let ((s3-1 (level-hint-task-process arg2 (the-as uint128 arg0) arg1)))
    (when (!= s3-1 -1)
      (kill-current-level-hint '() '() 'exit)
      (process-spawn level-hint s3-1 arg1 arg2 :to arg3)))
  0
  (none))

(defun ambient-hint-spawn ((arg0 string) (arg1 vector) (arg2 process-tree) (arg3 symbol))
  (case arg3
    (('camera) (kill-current-level-hint '() '() 'exit)))
  (and (not *hint-semaphore*) (process-spawn level-hint :init ambient-hint-init-by-other arg0 arg1 arg3 :to arg2)))

(defun kill-current-level-hint ((arg0 pair) (arg1 pair) (arg2 symbol))
  (when *hint-semaphore*
    (let ((s4-0 (ppointer->process *hint-semaphore*)))
      (if (and (or (null? arg0) (member (-> (the-as level-hint s4-0) mode) arg0))
               (not (member (-> (the-as level-hint s4-0) mode) arg1)))
        (send-event (the-as level-hint s4-0) arg2))))
  0
  (none))

;; og:preserve-this
(#when PC_PORT
  (define *level-hint-spool-name* (the string #f)))

(defbehavior level-hint-init-by-other level-hint ((arg0 text-id) (arg1 string) (arg2 entity))
  (mark-text-as-seen *game-info* arg0)
  (set! (-> self text-id-to-display) arg0)
  (set! (-> self sound-to-play) arg1)
  (set! (-> self total-time) 0)
  (set! (-> self total-off-time) 0)
  (set-time! (-> self last-time))
  (set! *hint-semaphore* (the-as (pointer level-hint) (process->ppointer self)))
  (add-setting! 'hint (process->ppointer self) 0.0 0)
  (set! (-> self voicebox) (the-as handle #f))
  (let ((s5-1 (res-lump-struct arg2 'play-mode structure))
        (a0-6 (the-as string
                      ((method-of-type res-lump get-property-struct)
                       arg2
                       'sound-name
                       'interp
                       -1000000000.0
                       (-> self sound-to-play)
                       (the-as (pointer res-tag) #f)
                       *res-static-buf*))))
    (if (and (not s5-1) a0-6)
      (set! s5-1
            (if (or (and (= (-> a0-6 data 0) 115) (= (-> a0-6 data 1) 97)) (and (= (-> a0-6 data 0) 97) (= (-> a0-6 data 1) 115)))
              'voicebox
              'sidekick)))
    (set! (-> self mode) (the-as symbol s5-1))
    (if (or (= s5-1 'sidekick) (= s5-1 'voicebox)) (go level-hint-sidekick a0-6)))
  (go level-hint-normal)
  0
  (none))

(defbehavior ambient-hint-init-by-other level-hint ((arg0 string) (arg1 vector) (arg2 symbol))
  (set! (-> self sound-to-play) arg0)
  (set! (-> self total-time) 0)
  (set! (-> self total-off-time) 0)
  (set-time! (-> self last-time))
  (set! (-> self trans) arg1)
  (set! *hint-semaphore* (the-as (pointer level-hint) (process->ppointer self)))
  (add-setting! 'hint (process->ppointer self) 0.0 0)
  (set! (-> self mode) arg2)
  (if (or (= arg2 'camera) (= arg2 'ambient) (= arg2 'stinger)) (add-setting! 'ambient (process->ppointer self) 0.0 0))
  (apply-settings *setting-control*)
  (set! (-> self event-hook)
        (lambda :behavior level-hint ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
          (case arg2
            (('die 'exit)
             (if (= (ppointer->process *hint-semaphore*) self) (set! *hint-semaphore* (the-as (pointer level-hint) #f)))
             (deactivate self)))))
  (go level-hint-ambient-sound arg0)
  0
  (none))

(defmethod print-text ((this level-hint))
  (when (!= *common-text* #f)
    (let ((s5-0 (new 'stack 'font-context *font-default-matrix* 56 160 0.0 (font-color default) (font-flags shadow kerning))))
      (let ((v1-3 s5-0)) (set! (-> v1-3 width) (the float 400)))
      (let ((v1-4 s5-0)) (set! (-> v1-4 height) (the float 96)))
      (set! (-> s5-0 flags) (font-flags shadow kerning middle large))
      (print-game-text (lookup-text! *common-text* (-> this text-id-to-display) #f) s5-0 #f 128 22)))
  0
  (none))

(defmethod appeared-for-long-enough? ((this level-hint))
  (and (!= (-> this next-state name) 'level-hint-sidekick) (< (seconds 5) (-> this total-time))))

(defstate level-hint-normal (level-hint)
  :event
    (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
      (case message
        (('exit) (go level-hint-exit))
        (('die) (deactivate self))))
  :exit
    (behavior ()
      (if (= (ppointer->process *hint-semaphore*) self) (set! *hint-semaphore* (the-as (pointer level-hint) #f))))
  :code
    (behavior ()
      (cond
        ((= (-> self text-id-to-display) (text-id zero)) (if *debug-segment* (go level-hint-error "UNKNOWN TEXT ID" "")))
        (else
         (while (>= (seconds 8) (-> self total-time))
           (hide-bottom-hud)
           (suspend)
           (cond
             ((and (bottom-hud-hidden?)
                   (not (paused?))
                   (not (movie?))
                   (= *master-mode* 'game)
                   (or (not *target*) (!= (-> *target* cam-user-mode) 'look-around))
                   (not (-> *hud-parts* money-all)))
              (print-text self)
              (+! (-> self total-time) (- (current-time) (-> self last-time)))
              (set-time! (-> self last-time)))
             (else
              (+! (-> self total-off-time) (- (current-time) (-> self last-time)))
              (if (or (< (seconds 6) (-> self total-time)) (< (seconds 10) (-> self total-off-time))) (go level-hint-exit))))
           (set-time! (-> self last-time)))))
      (go level-hint-exit)))

(defstate level-hint-sidekick (level-hint)
  :event
    (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
      (case message
        (('exit)
         (when (nonzero? (-> self sound-id))
           (sound-stop (-> self sound-id))
           (set! (-> self sound-id) (new 'static 'sound-id))
           0)
         (go level-hint-exit))
        (('die) (deactivate self))))
  :exit
    (behavior ()
      ;; og:preserve-this
      (#when PC_PORT
        (set! *level-hint-spool-name* #f))
      (if (nonzero? (-> self sound-id)) (sound-stop (-> self sound-id)))
      (remove-setting! 'music-volume)
      (remove-setting! 'sfx-volume)
      (remove-setting! 'dialog-volume)
      (if (= (ppointer->process *hint-semaphore*) self) (set! *hint-semaphore* (the-as (pointer level-hint) #f)))
      (send-event (handle->process (-> self voicebox)) 'die))
  :code
    (behavior ((arg0 string))
      ;; og:preserve-this
      (#when PC_PORT
        (set! *level-hint-spool-name* arg0))
      (when (and (-> *setting-control* current play-hints) (< 0.0 (-> *setting-control* current dialog-volume)))
        (case (-> self mode)
          (('voicebox) (if *target* (set! (-> self voicebox) (ppointer->handle (voicebox-spawn *target* (target-pos 0)))))))
        (while (handle->process (-> *game-info* auto-save-proc))
          (suspend))
        (while (not *sound-player-enable*)
          (suspend))
        (let ((s5-1 sound-play-by-name)
              (s4-1 string->sound-name))
          (format (clear *temp-string*) "spool-~S" arg0)
          (let ((s5-2 (s5-1 (s4-1 *temp-string*) (new-sound-id) 1024 0 0 (sound-group sfx) #t)))
            (set! (-> self sound-id) s5-2)
            (let ((s4-3 (current-time))) (while (and (!= (current-str-id) s5-2) (not (time-elapsed? s4-3 (seconds 5)))) (suspend)))
            (cond
              ((= (current-str-id) s5-2)
               (add-setting! 'music-volume 'rel (-> *setting-control* current music-volume-movie) 0)
               (add-setting! 'sfx-volume 'rel (-> *setting-control* current sfx-volume-movie) 0)
               (add-setting! 'dialog-volume 'rel (-> *setting-control* current dialog-volume-hint) 0)
               ;; og:preserve-this PAL patch here
               (let ((s5-1 -1)
                     (s3-1 (current-time))
                     (s4-4 (current-time)))
                 (label cfg-27)
                 (let ((v1-36 (current-str-pos s5-2)))
                   (when (< s5-1 v1-36)
                     (if (> v1-36 0) (set! s5-1 v1-36))
                     (set! s3-1 (-> *display* base-frame-counter)))
                   (when (not (and (or (< v1-36 0) (time-elapsed? s3-1 (seconds 3)) (time-elapsed? s4-4 (seconds 60))) *sound-player-enable*))
                     (suspend)
                     (goto cfg-27)))))
              (else (go level-hint-error "NO SOUND " arg0))))))
      (go level-hint-exit)))

(defstate level-hint-ambient-sound (level-hint)
  :event
    (-> level-hint-sidekick
        event)
  :exit
    (behavior ()
      (if (nonzero? (-> self sound-id)) (sound-stop (-> self sound-id)))
      (remove-setting! 'music-volume)
      (remove-setting! 'sfx-volume)
      (if (= (ppointer->process *hint-semaphore*) self) (set! *hint-semaphore* (the-as (pointer level-hint) #f))))
  :code
    (behavior ((arg0 string))
      (while (not *sound-player-enable*)
        (suspend))
      (cond
        ((-> self trans)
         (let ((s5-0 sound-play-by-name)
               (s4-0 string->sound-name))
           (format (clear *temp-string*) "spool-~S" arg0)
           (set! (-> self sound-id)
                 (s5-0 (s4-0 *temp-string*) (new-sound-id) 1024 1 0 (sound-group sfx) (the-as symbol (-> self trans))))))
        (else
         (let ((s5-1 sound-play-by-name)
               (s4-2 string->sound-name))
           (format (clear *temp-string*) "spool-~S" arg0)
           (set! (-> self sound-id) (s5-1 (s4-2 *temp-string*) (new-sound-id) 1024 0 0 (sound-group sfx) #t)))))
      (let ((s5-2 (current-time)))
        (while (and (!= (current-str-id) (-> self sound-id)) (not (time-elapsed? s5-2 (seconds 5))))
          (suspend)))
      (cond
        ((= (current-str-id) (-> self sound-id))
         (when (or (= (-> self mode) 'stinger) (= (-> self mode) 'camera))
           (add-setting! 'music-volume 'rel (-> *setting-control* current music-volume-movie) 0)
           (add-setting! 'sfx-volume 'rel (-> *setting-control* current sfx-volume-movie) 0))
         ;; og:preserve-this PAL patch here
         (let ((gp-1 -1)
               (s4-4 (-> *display* base-frame-counter))
               (s5-4 (-> *display* base-frame-counter)))
           (label cfg-19)
           (let ((v1-24 (current-str-pos (-> self sound-id))))
             (when (< gp-1 v1-24)
               (if (> v1-24 0) (set! gp-1 v1-24))
               (set! s4-4 (-> *display* base-frame-counter)))
             (when (not (and (or (< v1-24 0)
                                 (>= (- (-> *display* base-frame-counter) s4-4) (seconds 3))
                                 (>= (- (-> *display* base-frame-counter) s5-4) (seconds 60)))
                             *sound-player-enable*))
               (suspend)
               (goto cfg-19)))))
        (else (go level-hint-error "NO SOUND " arg0)))
      (go level-hint-exit)))

(defstate level-hint-error (level-hint)
  :event
    (-> level-hint-normal
        event)
  :code
    (behavior ((arg0 string) (arg1 string))
      (remove-setting! 'hint)
      (suspend-for (seconds 1)
                   (when (and *debug-segment* (not (paused?)) (not (str-is-playing?)) (bottom-hud-hidden?))
                     (let ((s3-0 (new 'stack 'font-context *font-default-matrix* 56 160 0.0 (font-color default) (font-flags shadow kerning))))
                       (let ((v1-7 s3-0)) (set! (-> v1-7 width) (the float 400)))
                       (let ((v1-8 s3-0)) (set! (-> v1-8 height) (the float 96)))
                       (set! (-> s3-0 flags) (font-flags shadow kerning middle))
                       (let ((s2-0 print-game-text)) (format (clear *temp-string*) "~S~S" arg0 arg1) (s2-0 *temp-string* s3-0 #f 128 22)))))
      (go level-hint-exit)))

(defstate level-hint-exit (level-hint)
  :code
    (behavior ()
      (if (= (ppointer->process *hint-semaphore*) self) (set! *hint-semaphore* (the-as (pointer level-hint) #f)))))

(defun ambient-type-error ((arg0 drawable-ambient) (arg1 vector))
  (when *display-entity-errors*
    (let ((s2-0 (-> arg0 ambient)))
      (format (clear *temp-string*)
              "~2j~s art error for ~s ~s"
              (res-lump-struct s2-0 'effect-name structure :time 0.0)
              (res-lump-struct s2-0 'type structure)
              (res-lump-struct s2-0 'name structure)))
    *temp-string*
    (add-debug-text-3d #t (bucket-id debug-no-zbuf) *temp-string* (-> arg0 bsphere) (font-color red) (the-as vector2h #f)))
  0
  (none))

(defun ambient-type-poi ((arg0 drawable-ambient) (arg1 vector))
  0
  (none))

(defun ambient-type-hint ((arg0 drawable-ambient) (arg1 vector))
  (with-pp
    (let ((s5-0 (level-hint-task-process (-> arg0 ambient) (the-as uint128 0) (the-as string #f))))
      (cond
        ((zero? s5-0)
         (when (and *debug-segment* (not (paused?)) (not (str-is-playing?)) (bottom-hud-hidden?))
           (let ((a1-3 (new 'stack 'font-context *font-default-matrix* 56 160 0.0 (font-color default) (font-flags shadow kerning))))
             (let ((v1-5 a1-3)) (set! (-> v1-5 width) (the float 400)))
             (let ((v1-6 a1-3)) (set! (-> v1-6 height) (the float 96)))
             (set! (-> a1-3 flags) (font-flags shadow kerning middle large))
             (print-game-text "AS: UNKNOWN ID" a1-3 #f 128 22))))
        ((!= s5-0 -1) (kill-current-level-hint '() '() 'exit) (process-spawn level-hint s5-0 #f (-> arg0 ambient) :to pp))))
    0
    (none)))

(defun ambient-type-sound ((arg0 drawable-ambient) (arg1 vector))
  (local-vars (sv-16 string) (sv-112 res-tag))
  (let* ((s5-0 (-> arg0 ambient))
         (s4-0 (-> s5-0 ambient-data user-uint64 0)))
    (when (>= (current-time) (the-as time-frame s4-0))
      (let ((v1-5 (res-lump-data s5-0 'cycle-speed pointer)))
        (set! (-> s5-0 ambient-data user-uint64 0)
              (the-as uint
                      (+ (current-time)
                         (the int (* 300.0 (-> (the-as (pointer float) v1-5) 0)))
                         (rand-vu-int-count (the int (* 300.0 (-> (the-as (pointer float) v1-5) 1))))))))
      (when (< (the-as uint (- (current-time) (the-as int s4-0))) (the-as uint 300))
        (let ((f30-0 (the float (rand-vu-int-count (the-as int (-> s5-0 ambient-data user-float 2))))))
          (set! sv-16 (symbol->string (res-lump-struct s5-0 'effect-name symbol :time f30-0)))
          (let ((s4-2 (new 'stack 'sound-spec)))
            (set! (-> s4-2 sound-name) (string->sound-name sv-16))
            (logior! (-> s4-2 mask) (sound-mask volume))
            (set! (-> s4-2 volume) 1024)
            (logior! (-> s4-2 mask) (sound-mask bend))
            (set! (-> s4-2 bend) (the int (* 327.66998 (rand-vu-float-range -100.0 100.0))))
            (set! sv-112 (new 'static 'res-tag))
            (let ((a1-7 ((method-of-type res-lump get-property-data)
                         s5-0
                         'effect-param
                         'exact
                         f30-0
                         (the-as pointer #f)
                         (& sv-112)
                         *res-static-buf*)))
              (if a1-7 (effect-param->sound-spec s4-2 (the-as (pointer float) a1-7) (the-as int (-> sv-112 elt-count)))))
            (when *debug-effect-control*
              (format #t "(~5D) effect sound ~A ~S " (current-time) (res-lump-struct s5-0 'name structure) sv-16)
              (format #t
                      "volume: ~f  pitch-mod: ~f~%"
                      (* 0.09765625 (the float (-> s4-2 volume)))
                      (* 0.000656168 (the float (-> s4-2 pitch-mod)))))
            (sound-play-by-spec s4-2 (new-sound-id) (-> arg0 bsphere)))))))
  0
  (none))

(defun ambient-type-sound-loop ((arg0 drawable-ambient) (arg1 vector))
  (let* ((s5-0 (-> arg0 ambient))
         (s2-0 (symbol->string (the-as symbol (-> s5-0 ambient-data user-float 2))))
         (s3-0 (the-as object (-> s5-0 ambient-data user-float 1)))
         (s4-0 (new 'stack 'sound-spec)))
    (set! (-> s4-0 sound-name) (string->sound-name s2-0))
    (logior! (-> s4-0 mask) (sound-mask volume))
    (set! (-> s4-0 volume) 1024)
    (when (the-as (pointer res-tag) s3-0)
      (let ((t9-2 effect-param->sound-spec)
            (a0-5 s4-0)
            (v1-8 (-> arg0 ambient))
            (a1-2 (-> (the-as (pointer res-tag) s3-0) 0)))
        (t9-2 a0-5
              (the-as (pointer float) (&+ (-> v1-8 data-base) (-> a1-2 data-offset)))
              (the-as int (-> (the-as (pointer res-tag) s3-0) 0 elt-count)))))
    (sound-play-by-spec s4-0 (the-as sound-id (-> s5-0 ambient-data user-float 0)) (-> arg0 bsphere)))
  0
  (none))

(defun ambient-type-light ((arg0 drawable-ambient) (arg1 vector))
  (when *target*
    (let ((s4-0 (-> arg0 ambient))
          (s5-0 #t))
      (let ((s3-0 (-> ((method-of-type res-lump lookup-tag-idx) s4-0 'vol 'exact 0.0) lo)))
        (when (>= (the-as int s3-0) 0)
          (let ((s2-0 (the-as int s3-0))
                (s1-0 (-> s4-0 tag s3-0)))
            0
            (while (= (-> s1-0 name) (-> s4-0 tag s3-0 name))
              (let ((v1-8 (the-as object (make-property-data s4-0 0.0 (the-as res-tag-pair s2-0) (the-as pointer #f)))))
                (set! s5-0 #t)
                (countdown (a0-6 (-> s1-0 elt-count))
                  (when (< (-> arg1 w)
                           (- (vector-dot arg1 (-> (the-as (inline-array vector) v1-8) a0-6)) (-> (the-as (inline-array vector) v1-8) a0-6 w)))
                    (set! s5-0 #f)
                    (goto cfg-9))))
              (label cfg-9)
              (if s5-0 (goto cfg-15))
              (+! s2-0 1)
              (set! s1-0 (-> s4-0 tag s2-0))))))
      (label cfg-15)
      (when s5-0)))
  0
  (none))

(defun ambient-type-dark ((arg0 drawable-ambient) (arg1 vector))
  (when *target*
    (let ((s4-0 (-> arg0 ambient))
          (s5-0 #t))
      (let ((s3-0 (-> ((method-of-type res-lump lookup-tag-idx) s4-0 'vol 'exact 0.0) lo)))
        (when (>= (the-as int s3-0) 0)
          (let ((s2-0 (the-as int s3-0))
                (s1-0 (-> s4-0 tag s3-0)))
            0
            (while (= (-> s1-0 name) (-> s4-0 tag s3-0 name))
              (let ((v1-8 (the-as object (make-property-data s4-0 0.0 (the-as res-tag-pair s2-0) (the-as pointer #f)))))
                (set! s5-0 #t)
                (countdown (a0-6 (-> s1-0 elt-count))
                  (when (< (-> arg1 w)
                           (- (vector-dot arg1 (-> (the-as (inline-array vector) v1-8) a0-6)) (-> (the-as (inline-array vector) v1-8) a0-6 w)))
                    (set! s5-0 #f)
                    (goto cfg-9))))
              (label cfg-9)
              (if s5-0 (goto cfg-15))
              (+! s2-0 1)
              (set! s1-0 (-> s4-0 tag s2-0))))))
      (label cfg-15)
      (if s5-0 (set! (-> *target* draw secondary-interp) 1.0))))
  0
  (none))

(defun ambient-type-weather-off ((arg0 drawable-ambient) (arg1 vector))
  (when *target*
    (let ((s4-0 (-> arg0 ambient))
          (s5-0 #t))
      (let ((s3-0 (-> ((method-of-type res-lump lookup-tag-idx) s4-0 'vol 'exact 0.0) lo)))
        (when (>= (the-as int s3-0) 0)
          (let ((s2-0 (the-as int s3-0))
                (s1-0 (-> s4-0 tag s3-0)))
            0
            (while (= (-> s1-0 name) (-> s4-0 tag s3-0 name))
              (let ((v1-8 (the-as object (make-property-data s4-0 0.0 (the-as res-tag-pair s2-0) (the-as pointer #f)))))
                (set! s5-0 #t)
                (countdown (a0-6 (-> s1-0 elt-count))
                  (when (< (-> arg1 w)
                           (- (vector-dot arg1 (-> (the-as (inline-array vector) v1-8) a0-6)) (-> (the-as (inline-array vector) v1-8) a0-6 w)))
                    (set! s5-0 #f)
                    (goto cfg-9))))
              (label cfg-9)
              (if s5-0 (goto cfg-15))
              (+! s2-0 1)
              (set! s1-0 (-> s4-0 tag s2-0))))))
      (label cfg-15)
      (if s5-0 (set! *weather-off* #t))))
  0
  (none))

(defun ambient-type-ocean-off ((arg0 drawable-ambient) (arg1 vector))
  (set! *ocean-off* #t)
  0
  (none))

(defun ambient-type-ocean-near-off ((arg0 drawable-ambient) (arg1 vector))
  (set! *ocean-near-off* #t)
  0
  (none))

(defun ambient-type-music ((arg0 drawable-ambient) (arg1 vector))
  (let ((gp-0 (-> arg0 ambient)))
    (if (-> gp-0 ambient-data user-float 0)
      (set! (-> *setting-control* default music) (the-as symbol (-> gp-0 ambient-data user-float 0))))
    (when (nonzero? (-> gp-0 ambient-data user-float 1))
      (let ((f0-0 (res-lump-float gp-0 'priority :default 10.0)))
        (when (>= f0-0 (-> *setting-control* default sound-flava-priority))
          ;; og:preserve-this TODO this following form has been manually modified to work
          (set! (-> *setting-control* default sound-flava) (the-as uint (-> gp-0 ambient-data user-int32 1)))
          (set! (-> *setting-control* default sound-flava-priority) f0-0)))))
  0
  (none))

(defmethod collect-ambients ((this drawable-ambient) (arg0 sphere) (arg1 int) (arg2 ambient-list))
  (dotimes (s2-0 arg1)
    (when (spheres-overlap? arg0 (the-as sphere (-> this bsphere)))
      (set! (-> arg2 items (-> arg2 num-items)) this)
      (+! (-> arg2 num-items) 1))
    (&+! this 32))
  0
  (none))

(defmethod collect-ambients ((this drawable-inline-array-ambient) (arg0 sphere) (arg1 int) (arg2 ambient-list))
  (collect-ambients (the-as drawable-ambient (-> this data)) arg0 (-> this length) arg2)
  0
  (none))

(defmethod collect-ambients ((this drawable-tree-ambient) (arg0 sphere) (arg1 int) (arg2 ambient-list))
  (collect-ambients (-> this data 0) arg0 (-> this length) arg2)
  0
  (none))

(defmethod birth-ambient! ((this entity-ambient))
  (set! (-> this ambient-data quad) (the-as uint128 0))
  (set! (-> this ambient-data function) ambient-type-error)
  (case (res-lump-struct this 'type structure)
    (('sound)
     (let ((s5-0 (res-lump-struct this 'effect-name structure :time 0.0))
           (a0-7 (res-lump-data this 'cycle-speed pointer)))
       (when (and s5-0 a0-7)
         (cond
           ((>= (-> (the-as (pointer float) a0-7)) 0.0)
            (set! (-> this ambient-data function) ambient-type-sound)
            (set! (-> this ambient-data user-float 2) (the-as float 0))
            (let ((s5-1 (-> ((method-of-type res-lump lookup-tag-idx) this 'effect-name 'exact 0.0) lo)))
              (when (>= (the-as int s5-1) 0)
                (let ((s4-0 (the-as int s5-1))
                      (v1-14 (-> this tag s5-1)))
                  0
                  (while (= (-> v1-14 name) (-> this tag s5-1 name))
                    (make-property-data this 0.0 (the-as res-tag-pair s4-0) (the-as pointer #f))
                    (set! (-> this ambient-data user-float 2) (the-as float (+ (the-as int (-> this ambient-data user-float 2)) 1)))
                    (+! s4-0 1)
                    (set! v1-14 (-> this tag s4-0)))))))
           (else
            (set! (-> this ambient-data user-float 0) (the-as float (new-sound-id)))
            (let ((v1-28 ((method-of-type res-lump lookup-tag-idx) this 'effect-param 'exact 0.0)))
              (set! (-> this ambient-data user-float 1)
                    (the-as float (if (< (the-as int v1-28) 0) (the-as (pointer res-tag) #f) (&-> (-> this tag) (-> v1-28 lo))))))
            (set! (-> this ambient-data user-float 2) (the-as float s5-0))
            (set! (-> this ambient-data function) ambient-type-sound-loop))))))
    (('poi)
     (let ((s5-2 (res-lump-struct this 'effect-name structure :time 0.0)))
       (when (res-lump-value this 'loc-name-id uint128)
         (set! (-> this ambient-data user-float 2) (the-as float s5-2))
         (set! (-> this ambient-data function) ambient-type-poi))))
    (('hint)
     (let ((s5-3 (res-lump-struct this 'effect-name structure :time 0.0)))
       (when (res-lump-value this 'text-id uint128)
         (set! (-> this ambient-data user-float 2) (the-as float s5-3))
         (set! (-> this ambient-data function) ambient-type-hint))))
    (('light)
     (set! (-> this ambient-data user-float 2) (res-lump-struct this 'effect-name float :time 0.0))
     (set! (-> this ambient-data function) ambient-type-light))
    (('dark)
     (set! (-> this ambient-data user-float 2) (res-lump-struct this 'effect-name float :time 0.0))
     (set! (-> this ambient-data function) ambient-type-dark))
    (('weather-off)
     (set! (-> this ambient-data user-float 2) (res-lump-struct this 'effect-name float :time 0.0))
     (set! (-> this ambient-data function) ambient-type-weather-off))
    (('ocean-off)
     (set! (-> this ambient-data user-float 2) (res-lump-struct this 'effect-name float :time 0.0))
     (set! (-> this ambient-data function) ambient-type-ocean-off))
    (('ocean-near-off)
     (set! (-> this ambient-data user-float 2) (res-lump-struct this 'effect-name float :time 0.0))
     (set! (-> this ambient-data function) ambient-type-ocean-near-off))
    (('music)
     (set! (-> this ambient-data user-float 2) (res-lump-struct this 'effect-name float :time 0.0))
     (set! (-> this ambient-data user-float 0) (res-lump-struct this 'music float))
     (set! (-> this ambient-data user-float 1) (res-lump-value this 'flava float))
     (set! (-> this ambient-data function) ambient-type-music)))
  (none))

(define *execute-ambients* #t)

(defmethod execute-ambient ((this drawable-ambient) (arg0 vector))
  ((-> this ambient ambient-data function) this arg0)
  0
  (none))

;; ERROR: function was not converted to expressions. Cannot decompile.

(defmethod draw-debug ((this entity-ambient))
  (local-vars (sv-16 uint128))
  (let ((gp-0 (-> this trans))
        (s5-0 (res-lump-struct this 'type symbol))
        (s3-0 #f))
    (cond
      ((= s5-0 'sound)
       (when *display-ambient-sound-marks*
         (add-debug-text-3d #t
                            (bucket-id debug-no-zbuf)
                            (symbol->string (res-lump-struct this 'effect-name symbol :time 0.0))
                            gp-0
                            (font-color white)
                            (new 'static 'vector2h :y 24))
         (set! s3-0 #t)))
      ((= s5-0 'hint)
       (when *display-ambient-hint-marks*
         (set! sv-16 (res-lump-value this 'text-id uint128))
         (let ((s3-2 add-debug-text-3d)
               (s2-1 #t)
               (s1-1 68))
           (format (clear *temp-string*) "TEXT ID #x~X" sv-16)
           (s3-2 s2-1 (the-as bucket-id s1-1) *temp-string* gp-0 (font-color white) (new 'static 'vector2h :y 24)))
         (set! s3-0 #t)))
      ((= s5-0 'poi)
       (when *display-ambient-poi-marks*
         (let ((a1-7 (res-lump-value this 'loc-name-id uint128)))
           (when (and (nonzero? a1-7) *common-text*)
             (add-debug-text-3d #t
                                (bucket-id debug-no-zbuf)
                                (lookup-text! *common-text* (the-as text-id a1-7) #f)
                                gp-0
                                (font-color white)
                                (new 'static 'vector2h :y 24))
             (set! s3-0 #t)))))
      ((= s5-0 'light) (if *display-ambient-light-marks* (set! s3-0 #t)))
      ((= s5-0 'dark) (if *display-ambient-dark-marks* (set! s3-0 #t)))
      ((= s5-0 'weather-off) (if *display-ambient-weather-off-marks* (set! s3-0 #t)))
      ((= s5-0 'ocean-off) (if *display-ambient-ocean-off-marks* (set! s3-0 #t)))
      ((= s5-0 'ocean-near-off) (if *display-ambient-ocean-near-off-marks* (set! s3-0 #t)))
      ((= s5-0 'music) (if *display-ambient-music-marks* (set! s3-0 #t))))
    (when s3-0
      (let ((t9-10 add-debug-sphere)
            (a0-11 #t)
            (a1-9 67)
            (a2-9 gp-0)
            (a3-7 (-> gp-0 w))
            (v1-53 s5-0))
        (t9-10 a0-11
               (the-as bucket-id a1-9)
               a2-9
               a3-7
               (cond
                 ((= v1-53 'sound) (new 'static 'rgba :r #xff :g #xff :a #x80))
                 ((= v1-53 'poi) (new 'static 'rgba :g #x80 :b #xff :a #x80))
                 (else (new 'static 'rgba :r #xff :a #x80)))))
      (add-debug-text-3d #t
                         (bucket-id debug-no-zbuf)
                         (res-lump-struct this 'name string)
                         gp-0
                         (font-color white)
                         (new 'static 'vector2h :y 8))
      (add-debug-text-3d #t
                         (bucket-id debug-no-zbuf)
                         (symbol->string s5-0)
                         gp-0
                         (font-color white)
                         (new 'static 'vector2h :y 16))))
  0
  (none))
