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

;; definition for symbol *cavecrystal-engine*, type engine
(define *cavecrystal-engine* (new 'loading-level 'engine 'cavecrystal 64))

;; definition of type cavecrystal-light
(deftype cavecrystal-light (structure)
  ((next           cavecrystal-light         :offset-assert   0)
   (crystal-id     int32                     :offset-assert   4)
   (intensity      float                     :offset-assert   8)
   (fade-start     float                     :offset-assert  12)
   (fade-end       float                     :offset-assert  16)
   (crystal-handle handle                    :offset-assert  24)
   (trans          vector            :inline :offset-assert  32)
   )
  :method-count-assert 9
  :size-assert         #x30
  :flag-assert         #x900000030
  )

;; definition for method 3 of type cavecrystal-light
(defmethod inspect cavecrystal-light ((obj cavecrystal-light))
  (format #t "[~8x] ~A~%" obj 'cavecrystal-light)
  (format #t "~Tnext: #<cavecrystal-light @ #x~X>~%" (-> obj next))
  (format #t "~Tcrystal-id: ~D~%" (-> obj crystal-id))
  (format #t "~Tintensity: ~f~%" (-> obj intensity))
  (format #t "~Tfade-start: ~f~%" (-> obj fade-start))
  (format #t "~Tfade-end: ~f~%" (-> obj fade-end))
  (format #t "~Tcrystal-handle: ~D~%" (-> obj crystal-handle))
  (format #t "~Ttrans: #<vector @ #x~X>~%" (-> obj trans))
  obj
  )

;; definition of type cavecrystal-light-control
(deftype cavecrystal-light-control (basic)
  ((active-count          int32                       :offset-assert   4)
   (head                  cavecrystal-light           :offset-assert   8)
   (last-known-valid-time time-frame                  :offset-assert  16)
   (crystal               cavecrystal-light 7 :inline :offset-assert  32)
   )
  :method-count-assert 15
  :size-assert         #x170
  :flag-assert         #xf00000170
  (:methods
    (TODO-RENAME-9 (_type_ int float process-drawable) none 9)
    (TODO-RENAME-10 (_type_ vector) float 10)
    (inc-intensities! (_type_) none 11)
    (TODO-RENAME-12 (_type_) none 12)
    (create-connection! (_type_ process-drawable res-lump (function object object object object object) int float) connection 13)
    (execute-connections (_type_) int 14)
    )
  )

;; definition for method 3 of type cavecrystal-light-control
(defmethod inspect cavecrystal-light-control ((obj cavecrystal-light-control))
  (format #t "[~8x] ~A~%" obj (-> obj type))
  (format #t "~Tactive-count: ~D~%" (-> obj active-count))
  (format #t "~Thead: #<cavecrystal-light @ #x~X>~%" (-> obj head))
  (format #t "~Tlast-known-valid-time: ~D~%" (-> obj last-known-valid-time))
  (format #t "~Tcrystal[7] @ #x~X~%" (-> obj crystal))
  obj
  )

;; definition for symbol *cavecrystal-light-control*, type cavecrystal-light-control
(define *cavecrystal-light-control* (new 'static 'cavecrystal-light-control :head #f))

;; definition for function cavecrystal-light-control-default-callback
;; INFO: Used lq/sq
;; INFO: Return type mismatch vector vs none.
(defun cavecrystal-light-control-default-callback ((arg0 (pointer process-drawable)) (arg1 int) (arg2 float))
  (let ((s5-0 (new 'stack-no-clear 'vector)))
    (let ((v1-1 (-> arg0 0 node-list)))
      (if (and (>= arg1 0) (nonzero? v1-1))
          (vector<-cspace! s5-0 (-> v1-1 data arg1))
          (set! (-> s5-0 quad) (-> arg0 0 root trans quad))
          )
      )
    (set! (-> s5-0 w) arg2)
    (let ((f0-1 (TODO-RENAME-10 *cavecrystal-light-control* s5-0))
          (a2-2 (new 'static 'vector :x 1.0 :y 1.0 :z 1.0 :w 1.0))
          (a1-5 (new 'static 'vector :x 0.05 :y 0.05 :z 0.05 :w 1.0))
          )
      (vector-lerp! (-> arg0 0 draw color-mult) a1-5 a2-2 f0-1)
      )
    )
  (none)
  )

;; definition for method 13 of type cavecrystal-light-control
(defmethod create-connection! cavecrystal-light-control ((obj cavecrystal-light-control)
                                                       (arg0 process-drawable)
                                                       (arg1 res-lump)
                                                       (arg2 (function object object object object object))
                                                       (arg3 int)
                                                       (arg4 float)
                                                       )
  (if (nonzero? (res-lump-value arg1 'crystal-light uint128))
      (add-connection *cavecrystal-engine* arg0 arg2 (process->ppointer arg0) arg3 arg4)
      )
  )

;; definition for method 14 of type cavecrystal-light-control
(defmethod execute-connections cavecrystal-light-control ((obj cavecrystal-light-control))
  (execute-connections *cavecrystal-engine* #f)
  )

;; definition for method 12 of type cavecrystal-light-control
;; INFO: Return type mismatch symbol vs none.
(defmethod TODO-RENAME-12 cavecrystal-light-control ((obj cavecrystal-light-control))
  (let ((v1-0 (-> obj last-known-valid-time))
        (a1-1 (-> *display* base-frame-counter))
        )
    (when (!= v1-0 a1-1)
      (set! (-> obj last-known-valid-time) a1-1)
      (cond
        ((zero? v1-0)
         (set! (-> obj active-count) 0)
         (set! (-> obj head) #f)
         (dotimes (v1-1 7)
           (let ((a1-5 (-> obj crystal v1-1)))
             (set! (-> a1-5 next) #f)
             (set! (-> a1-5 crystal-id) v1-1)
             (set! (-> a1-5 intensity) 0.0)
             (set! (-> a1-5 fade-start) 20480.0)
             (set! (-> a1-5 fade-end) 286720.0)
             (set! (-> a1-5 crystal-handle) (the-as handle #f))
             )
           )
         )
        (else
          (when (> (-> obj active-count) 0)
            (let ((v1-4 (the-as cavecrystal-light #f))
                  (a1-7 (-> obj head))
                  )
              (while a1-7
                (cond
                  ((handle->process (-> a1-7 crystal-handle))
                   (set! v1-4 a1-7)
                   (set! a1-7 (-> a1-7 next))
                   )
                  (else
                    (let ((a2-4 (-> a1-7 next)))
                      (+! (-> obj active-count) -1)
                      (set! (-> a1-7 next) #f)
                      (set! (-> a1-7 intensity) 0.0)
                      (if v1-4
                          (set! (-> obj head) a2-4)
                          (set! (-> v1-4 next) a2-4)
                          )
                      (set! a1-7 a2-4)
                      )
                    )
                  )
                )
              )
            )
          )
        )
      )
    )
  (none)
  )

;; definition for method 11 of type cavecrystal-light-control
;; INFO: Return type mismatch int vs none.
(defmethod inc-intensities! cavecrystal-light-control ((obj cavecrystal-light-control))
  (set! (-> obj head) #f)
  (let ((a1-0 (the-as cavecrystal-light #f))
        (v0-0 0)
        )
    (dotimes (v1-0 7)
      (let ((a2-3 (-> obj crystal v1-0)))
        (set! (-> a2-3 next) #f)
        (when (< 0.0 (-> a2-3 intensity))
          (+! v0-0 1)
          (if a1-0
              (set! (-> a1-0 next) a2-3)
              (set! (-> obj head) a2-3)
              )
          (set! a1-0 a2-3)
          )
        )
      )
    (set! (-> obj active-count) v0-0)
    )
  (none)
  )

;; definition for method 9 of type cavecrystal-light-control
;; INFO: Used lq/sq
;; INFO: Return type mismatch object vs none.
;; WARN: Function (method 9 cavecrystal-light-control) has a return type of none, but the expression builder found a return statement.
(defmethod TODO-RENAME-9 cavecrystal-light-control ((obj cavecrystal-light-control) (arg0 int) (arg1 float) (arg2 process-drawable))
  (TODO-RENAME-12 obj)
  (when (or (< arg0 0) (>= arg0 7))
    (format 0 "ERROR: Bogus cavecrystal id!~%")
    (return #f)
    )
  (let ((s3-0 (-> obj crystal arg0)))
    (cond
      ((and (< 0.0 arg1) (>= 0.0 (-> s3-0 intensity)))
       (+! (-> obj active-count) 1)
       (set! (-> s3-0 intensity) arg1)
       (set! (-> s3-0 crystal-handle) (process->handle arg2))
       (set! (-> s3-0 trans quad) (-> arg2 root trans quad))
       (inc-intensities! obj)
       )
      ((and (>= 0.0 arg1) (< 0.0 (-> s3-0 intensity)))
       (+! (-> obj active-count) -1)
       (set! (-> s3-0 intensity) 0.0)
       (inc-intensities! obj)
       )
      ((< 0.0 arg1)
       (set! (-> s3-0 intensity) arg1)
       )
      )
    (set-fade! *palette-fade-controls* (+ arg0 1) arg1 0.0 (-> s3-0 trans))
    )
  (none)
  )

;; definition for method 10 of type cavecrystal-light-control
(defmethod TODO-RENAME-10 cavecrystal-light-control ((obj cavecrystal-light-control) (arg0 vector))
  (TODO-RENAME-12 obj)
  (let ((s5-1 (-> obj head))
        (f30-0 0.0)
        )
    (when s5-1
      (until (not s5-1)
        (let* ((f26-0 (-> s5-1 fade-start))
               (f28-0 (- (-> s5-1 fade-end) f26-0))
               (f1-1 (fmin (fmax 0.0 (- (- (vector-vector-distance (-> s5-1 trans) arg0) (-> arg0 w)) f26-0)) f28-0))
               (f0-7 (* (- 1.0 (/ f1-1 f28-0)) (-> s5-1 intensity)))
               )
          (if (< f30-0 f0-7)
              (set! f30-0 f0-7)
              )
          )
        (set! s5-1 (-> s5-1 next))
        )
      (set! f30-0 (fmin 2.0 f30-0))
      )
    f30-0
    )
  )
