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

;; name: bug-report.gc
;; name in dgo: bug-report
;; dgos: GAME

(declare-type bug-report process)

(define-extern *bug-report* (pointer bug-report))

;; DECOMP BEGINS

;; og:preserve-this this file was manually rewritten
(declare-file (debug))

(define *continue-bug-report*
      (new 'static 'continue-point
        :name "wasall-start"
        :level #f
        :flags (continue-flags test)
        :trans (new 'static 'vector :x 9283373.0 :y 126422.22 :z 1057314.9 :w 1.0)
        :camera-trans (new 'static 'vector :x 9272728.0 :y 147014.05 :z 1009150.4 :w 1.0)
        :quat (new 'static 'vector4h :data (new 'static 'array int16 4 -36 #x5dfc 22 #x56de))
        :camera-rot (new 'static 'array int16 9 #x796a 0 -10364 #x4b5 #x7f20 #xe21 #x2834 -3810 #x7899)
        :on-goto #f
        :vis-nick 'wasall
        :vehicle-type #x1b
        :want-count 4
        :want (new 'static 'inline-array level-buffer-state-small 4
          (new 'static 'level-buffer-state-small :name 'wasall :display? 'special)
          (new 'static 'level-buffer-state-small :name 'desert-game :display? 'display)
          (new 'static 'level-buffer-state-small :name 'wasdoors :display? 'display)
          (new 'static 'level-buffer-state-small :name 'desertb :display? 'display)
          )
        :want-sound (new 'static 'array symbol 3 'wasall1 'desert1 'desert2)
        )
      )

(deftype bug-report (process)
  ((bug-number uint32 5)
   (digit      uint32)
   (state-time time-frame)
   (next-down  time-frame)
   )
  (:state-methods
    idle
    )
  (:methods
    (bug-report-method-15 (_type_) none)
    (bug-report-method-16 (_type_) none)
    )
  )

(define *bug-report* (the (pointer bug-report) #f))

(defmethod inspect ((this bug-report))
  (call-parent-method this)
  (format #t "~2Tbug-number[5] @ #x~X~%" (-> this bug-number))
  (dotimes (i 5)
    (format #t "~T  [~D]~2Tbug-number: ~D~%" i (-> this bug-number i))
    )
  (format #t "~2Tdigit: ~D~%" (-> this digit))
  (format #t "~2Tstate-time: ~D~%" (-> this state-time))
  (format #t "~2Tnext-down: ~D~%" (-> this next-down))
  this
  )

(defmethod deactivate ((this bug-report))
  "Make a process dead, clean it up, remove it from the active pool, and return to dead pool."
  (set! *bug-report* (the (pointer bug-report) #f))
  (call-parent-method this)
  )

(defbehavior bug-report-init bug-report ()
  (set! *bug-report* (the (pointer bug-report) (process->ppointer self)))
  (set! (-> self bug-number 0) (the-as uint 0))
  (set! (-> self bug-number 1) (the-as uint 0))
  (set! (-> self bug-number 2) (the-as uint 0))
  (set! (-> self bug-number 3) (the-as uint 0))
  (set! (-> self bug-number 4) (the-as uint 0))
  (set! (-> self digit) (the-as uint 0))
  (set! (-> self next-down) 0)
  (go-virtual idle)
  )

(defun bug-report-stop ()
  (kill-by-type bug-report *active-pool*)
  (none)
  )

(defun bug-report-start ()
  (bug-report-stop)
  (process-spawn bug-report :init bug-report-init :name "bug-report")
  (none)
  )

(defstate idle (bug-report)
  :virtual #t
  :enter (behavior () (set-time! (-> self state-time)))
  :code sleep-code
  :trans (behavior ()
    (when (cpad-hold? 0 l2)
      (when (>= (- (current-time) (-> self next-down)) 0)
        (when (cpad-hold? 0 left)
          (set! (-> self next-down) (+ (current-time) (seconds 0.2)))
          (if (zero? (-> self digit))
              (set! (-> self digit) (the-as uint 4))
              (+! (-> self digit) -1)
              )
          )
        (when (cpad-hold? 0 right)
          (set! (-> self next-down) (+ (current-time) (seconds 0.2)))
          (cond
            ((= (-> self digit) 4)
              (set! (-> self digit) (the-as uint 0))
              0
              )
            (else
              (+! (-> self digit) 1)
              )
            )
          )
        (when (cpad-hold? 0 up)
          (set! (-> self next-down) (+ (current-time) (seconds 0.2)))
          (cond
            ((= (-> self bug-number (-> self digit)) 9)
              (set! (-> self bug-number (-> self digit)) (the-as uint 0))
              0
              )
            (else
              (+! (-> self bug-number (-> self digit)) 1)
              )
            )
          )
        (when (cpad-hold? 0 down)
          (set! (-> self next-down) (+ (current-time) (seconds 0.2)))
          (if (zero? (-> self bug-number (-> self digit)))
              (set! (-> self bug-number (-> self digit)) (the-as uint 9))
              (+! (-> self bug-number (-> self digit)) -1)
              )
          )
        )
      (if (cpad-pressed? 0 x)
          (bug-report-method-16 self)
          )
      0
      )
    (with-dma-buffer-add-bucket ((s5-0 (-> *display* frames (-> *display* on-screen) debug-buf))
                                  (bucket-id hud-draw-hud-alpha)
                                  )
      (draw-string-xy "Use L2 + X to Load" s5-0 32 80 (font-color white) (font-flags kerning))
      (draw-string-xy "Use L2 + D-Pad to change bug number" s5-0 32 112 (font-color white) (font-flags kerning))
      (dotimes (s4-0 5)
        (let ((s3-0 (clear *temp-string*)))
          (format s3-0 "~1,'0D" (-> self bug-number s4-0))
          (draw-string-xy
            s3-0
            s5-0
            (+ (* 24 s4-0) 32)
            132
            (if (= (-> self digit) s4-0)
                (font-color red)
                (font-color yellow)
                )
            (font-flags kerning large)
            )
          )
        )
      )
    )
  )

(defmethod bug-report-method-15 ((this bug-report))
  (if (not *target*)
      (return #f)
      )
  (let ((a0-1 (-> *target* control))
        (s5-0 *continue-bug-report*)
        )
    (set! (-> s5-0 trans quad) (-> a0-1 trans quad))
    (set-vector!
      (-> s5-0 quat)
      (the int (* 32767.0 (-> a0-1 quat x)))
      (the int (* 32767.0 (-> a0-1 quat y)))
      (the int (* 32767.0 (-> a0-1 quat z)))
      (the int (* 32767.0 (-> a0-1 quat w)))
      )
    (let ((v1-6 *math-camera*))
      (set! (-> s5-0 camera-trans quad) (-> v1-6 trans quad))
      (set! (-> s5-0 camera-rot 0) (the int (* 32767.0 (-> v1-6 inv-camera-rot rvec x))))
      (set! (-> s5-0 camera-rot 1) (the int (* 32767.0 (-> v1-6 inv-camera-rot rvec y))))
      (set! (-> s5-0 camera-rot 2) (the int (* 32767.0 (-> v1-6 inv-camera-rot rvec z))))
      (set! (-> s5-0 camera-rot 3) (the int (* 32767.0 (-> v1-6 inv-camera-rot uvec x))))
      (set! (-> s5-0 camera-rot 4) (the int (* 32767.0 (-> v1-6 inv-camera-rot uvec y))))
      (set! (-> s5-0 camera-rot 5) (the int (* 32767.0 (-> v1-6 inv-camera-rot uvec z))))
      (set! (-> s5-0 camera-rot 6) (the int (* 32767.0 (-> v1-6 inv-camera-rot fvec x))))
      (set! (-> s5-0 camera-rot 7) (the int (* 32767.0 (-> v1-6 inv-camera-rot fvec y))))
      (set! (-> s5-0 camera-rot 8) (the int (* 32767.0 (-> v1-6 inv-camera-rot fvec z))))
      )
    (dotimes (s4-0 4)
      (let ((v1-10 (lookup-level-info (-> *load-state* want s4-0 name))))
        (cond
          ((and v1-10 (!= (-> v1-10 memory-mode) 10))
           (set! (-> s5-0 want s4-0 name) (-> *load-state* want s4-0 name))
           (set! (-> s5-0 want s4-0 display?) (-> *load-state* want s4-0 display?))
           )
          (else
            (set! (-> s5-0 want s4-0 name) #f)
            (set! (-> s5-0 want s4-0 display?) #f)
            )
          )
        )
      )
    )
  (format
    (clear *temp-string*)
    "db/bug-report/bug-report-~D~D~D~D~D.txt"
    (-> this bug-number 0)
    (-> this bug-number 1)
    (-> this bug-number 2)
    (-> this bug-number 3)
    (-> this bug-number 4)
    )
  *temp-string*
  (let ((gp-1 (new 'stack 'file-stream *temp-string* 'write)))
    (let ((s5-2 format)
          (s4-1 gp-1)
          (s3-0 "nick ~S~%")
          (v1-29 (lookup-level-info (-> *load-state* vis-nick)))
          )
      (s5-2 s4-1 s3-0 (if v1-29
                          (-> v1-29 name)
                          )
            )
      )
    (let ((t9-6 format)
          (a0-37 gp-1)
          (a1-16 "continue ~S~%")
          (v1-31 (-> *game-info* current-continue))
          )
      (t9-6 a0-37 a1-16 (if v1-31
                            (-> v1-31 name)
                            )
            )
      )
    (dotimes (s5-3 10)
      (format gp-1 "level ~D ~A ~A~%" s5-3 (-> *load-state* want s5-3 name) (-> *load-state* want s5-3 display?))
      )
    (format gp-1 "music ~-8S~%" (-> *setting-control* user-current music))
    (dotimes (s5-4 6)
      (format gp-1 "sound ~D ~-8S~%" s5-4 (-> *level* sound-bank s5-4 name))
      )
    (let ((v1-43 (target-pos 0)))
      (format gp-1 "target ~f ~f ~f~%" (-> v1-43 x) (-> v1-43 y) (-> v1-43 z))
      )
    (let ((s5-5 *math-camera*))
      (format gp-1 "camera-trans ~f ~f ~f~%" (-> s5-5 trans x) (-> s5-5 trans y) (-> s5-5 trans z))
      (format
        gp-1
        "camera-rot ~f ~f ~f "
        (-> s5-5 inv-camera-rot rvec x)
        (-> s5-5 inv-camera-rot rvec y)
        (-> s5-5 inv-camera-rot rvec z)
        )
      (format
        gp-1
        "~f ~f ~f "
        (-> s5-5 inv-camera-rot uvec x)
        (-> s5-5 inv-camera-rot uvec y)
        (-> s5-5 inv-camera-rot uvec z)
        )
      (format
        gp-1
        "~f ~f ~f~%"
        (-> s5-5 inv-camera-rot fvec x)
        (-> s5-5 inv-camera-rot fvec y)
        (-> s5-5 inv-camera-rot fvec z)
        )
      )
    (let ((s5-6 (-> *game-info* sub-task-list)))
      (dotimes (s4-2 (-> s5-6 length))
        (when (nonzero? s4-2)
          (let ((s3-1 (-> s5-6 s4-2)))
            (if (game-task-node-info-method-12 s3-1)
                (format gp-1 "open-node ~s~%" (-> s3-1 name))
                )
            )
          )
        )
      )
    (file-stream-close gp-1)
    (file-stream-close gp-1)
    )
  (none)
  )

(defmethod bug-report-method-16 ((this bug-report))
  (local-vars
    (sv-128 string)
    (sv-132 string)
    (sv-136 continue-point)
    (sv-140 int)
    (sv-144 vector)
    (sv-148 vector)
    )
  (format
    (clear *temp-string*)
    "db/bug-report/bug-report-~D~D~D~D~D.txt"
    (-> this bug-number 0)
    (-> this bug-number 1)
    (-> this bug-number 2)
    (-> this bug-number 3)
    (-> this bug-number 4)
    )
  *temp-string*
  (let ((gp-1 (new 'stack 'file-stream *temp-string* 'read)))
    (when (zero? (-> gp-1 file))
      (set! sv-128 (new 'debug 'string 1024 (the-as string #f)))
      (set! sv-132 (new 'debug 'string 64 (the-as string #f)))
      (set! sv-136 *continue-bug-report*)
      (set! sv-140 0)
      (set! sv-144 (new 'stack-no-clear 'vector))
      (set! sv-148 (new 'stack-no-clear 'vector))
      (file-stream-read-string gp-1 sv-128)
      (while (string-get-arg!! sv-132 sv-128)
        (cond
          ((string= "nick" sv-132)
           (string-get-arg!! sv-132 sv-128)
           )
          ((string= "continue" sv-132)
           (string-get-arg!! sv-132 sv-128)
           )
          ((string= "level" sv-132)
           (string-get-int32!! (the-as (pointer int32) (& sv-140)) sv-128)
           (string-get-arg!! sv-132 sv-128)
           (set! (-> sv-136 want sv-140 name) (string->symbol sv-132))
           (string-get-arg!! sv-132 sv-128)
           (set! (-> sv-136 want sv-140 display?) (string->symbol sv-132))
           )
          ((string= "music" sv-132)
           (string-get-arg!! sv-132 sv-128)
           )
          ((string= "target" sv-132)
           (string-get-float!! (the-as (pointer float) (-> sv-136 trans)) sv-128)
           (string-get-float!! (&-> sv-136 trans y) sv-128)
           (string-get-float!! (&-> sv-136 trans z) sv-128)
           )
          ((string= "camera-trans" sv-132)
           (string-get-float!! (the-as (pointer float) (-> sv-136 camera-trans)) sv-128)
           (string-get-float!! (&-> sv-136 camera-trans y) sv-128)
           (string-get-float!! (&-> sv-136 camera-trans z) sv-128)
           )
          ((string= "camera-rot" sv-132)
           (let ((s5-1 (new 'stack-no-clear 'array 'float 9)))
             (string-get-float!! (&-> s5-1 0) sv-128)
             (string-get-float!! (&-> s5-1 1) sv-128)
             (string-get-float!! (&-> s5-1 2) sv-128)
             (string-get-float!! (&-> s5-1 3) sv-128)
             (string-get-float!! (&-> s5-1 4) sv-128)
             (string-get-float!! (&-> s5-1 5) sv-128)
             (string-get-float!! (&-> s5-1 6) sv-128)
             (string-get-float!! (&-> s5-1 7) sv-128)
             (string-get-float!! (&-> s5-1 8) sv-128)
             (dotimes (v1-34 9)
               (set! (-> sv-136 camera-rot v1-34) (the int (* 32767.0 (-> s5-1 v1-34))))
               )
             )
           )
          ((string= "open-node" sv-132)
           (string-get-arg!! sv-132 sv-128)
           (let ((a0-48 (task-node-index-by-name sv-132)))
             (if a0-48
                 (task-node-open! (the-as game-task-node a0-48) 'menu)
                 )
             )
           )
          )
        )
      (file-stream-close gp-1)
      (stop 'debug)
      (start 'debug *continue-bug-report*)
      )
    (file-stream-close gp-1)
    )
  (none)
  )
