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

;; definition of type baseplat
(deftype baseplat (process-drawable)
  ((root-override collide-shape-moving         :offset        112)
   (smush         smush-control        :inline :offset-assert 176)
   (basetrans     vector               :inline :offset-assert 208)
   (bouncing      symbol                       :offset-assert 224)
   )
  :heap-base #x80
  :method-count-assert 27
  :size-assert         #xe4
  :flag-assert         #x1b008000e4
  (:methods
    (dummy-20 (_type_) none 20)
    (dummy-21 (_type_) none 21)
    (dummy-22 (_type_) none 22)
    (get-unlit-skel (_type_) skeleton-group 23)
    (dummy-24 (_type_) none 24)
    (TODO-RENAME-25 (_type_) sparticle-launch-group 25)
    (dummy-26 (_type_) none 26)
    )
  )

;; definition for method 3 of type baseplat
(defmethod inspect baseplat ((obj baseplat))
  (let ((t9-0 (method-of-type process-drawable inspect)))
    (t9-0 obj)
    )
  (format #t "~T~Tsmush: #<smush-control @ #x~X>~%" (-> obj smush))
  (format #t "~T~Tbasetrans: #<vector @ #x~X>~%" (-> obj basetrans))
  (format #t "~T~Tbouncing: ~A~%" (-> obj bouncing))
  obj
  )

;; definition for method 21 of type baseplat
;; INFO: Used lq/sq
;; INFO: Return type mismatch int vs none.
(defmethod dummy-21 baseplat ((obj baseplat))
  (logior! (-> obj skel status) (janim-status inited))
  (set! (-> obj basetrans quad) (-> obj root-override trans quad))
  (set! (-> obj bouncing) #f)
  0
  (none)
  )

;; definition for method 22 of type baseplat
;; INFO: Return type mismatch int vs none.
(defmethod dummy-22 baseplat ((obj baseplat))
  (activate! (-> obj smush) -1.0 60 150 1.0 1.0)
  (set! (-> obj bouncing) #t)
  (logclear! (-> obj mask) (process-mask sleep))
  (logclear! (-> obj mask) (process-mask sleep-code))
  0
  (none)
  )

;; definition for function plat-code
(defbehavior plat-code baseplat ()
  (transform-post)
  (suspend)
  (transform-post)
  (suspend)
  (loop
    (when (not (-> self bouncing))
      (logior! (-> self mask) (process-mask sleep))
      (suspend)
      0
      )
    (while (-> self bouncing)
      (suspend)
      )
    )
  (none)
  )

;; definition for function plat-trans
;; INFO: Used lq/sq
(defbehavior plat-trans baseplat ()
  (rider-trans)
  (cond
    ((-> self bouncing)
     (let ((gp-0 (new 'stack-no-clear 'vector)))
       (set! (-> gp-0 quad) (-> self basetrans quad))
       (+! (-> gp-0 y) (* 819.2 (update! (-> self smush))))
       (move-to-point! (-> self root-override) gp-0)
       )
     (if (not (!= (-> self smush amp) 0.0))
         (set! (-> self bouncing) #f)
         )
     )
    (else
      (move-to-point! (-> self root-override) (-> self basetrans))
      )
    )
  (none)
  )

;; definition for function plat-post
;; INFO: Return type mismatch int vs none.
(defbehavior plat-post baseplat ()
  (dummy-20 self)
  (rider-post)
  (none)
  )

;; definition for method 25 of type baseplat
;; INFO: Return type mismatch int vs sparticle-launch-group.
(defmethod TODO-RENAME-25 baseplat ((obj baseplat))
  (the-as sparticle-launch-group 0)
  )

;; definition for method 20 of type baseplat
;; INFO: Return type mismatch object vs none.
(defmethod dummy-20 baseplat ((obj baseplat))
  (if (nonzero? (-> obj part))
      (spawn (-> obj part) (-> obj root-override trans))
      )
  (none)
  )

;; definition for function plat-event
;; INFO: Return type mismatch none vs object.
(defbehavior plat-event baseplat ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
  (let ((v1-0 arg2))
    (the-as object (if (= v1-0 'bonk)
                       (dummy-22 self)
                       )
            )
    )
  )

;; definition of type eco-door
(deftype eco-door (process-drawable)
  ((root-override  collide-shape          :offset        112)
   (speed          float                  :offset-assert 176)
   (open-distance  float                  :offset-assert 180)
   (close-distance float                  :offset-assert 184)
   (out-dir        vector         :inline :offset-assert 192)
   (open-sound     sound-name             :offset-assert 208)
   (close-sound    sound-name             :offset-assert 224)
   (state-actor    entity-actor           :offset-assert 240)
   (flags          eco-door-flags         :offset-assert 244)
   (locked         symbol                 :offset-assert 248)
   (auto-close     symbol                 :offset-assert 252)
   (one-way        symbol                 :offset-assert 256)
   )
  :heap-base #xa0
  :method-count-assert 27
  :size-assert         #x104
  :flag-assert         #x1b00a00104
  (:methods
    (door-closed () _type_ :state 20)
    (door-opening () _type_ :state 21)
    (door-open () _type_ :state 22)
    (door-closing () _type_ :state 23)
    (TODO-RENAME-24 (_type_) none 24)
    (TODO-RENAME-25 (_type_) none 25)
    (dummy-26 (_type_) none 26)
    )
  )

;; definition for method 3 of type eco-door
;; INFO: Used lq/sq
(defmethod inspect eco-door ((obj eco-door))
  (let ((t9-0 (method-of-type process-drawable inspect)))
    (t9-0 obj)
    )
  (format #t "~T~Tspeed: ~f~%" (-> obj speed))
  (format #t "~T~Topen-distance: ~f~%" (-> obj open-distance))
  (format #t "~T~Tclose-distance: ~f~%" (-> obj close-distance))
  (format #t "~T~Tout-dir: #<vector @ #x~X>~%" (-> obj out-dir))
  (format #t "~T~Topen-sound: ~D~%" (-> obj open-sound))
  (format #t "~T~Tclose-sound: ~D~%" (-> obj close-sound))
  (format #t "~T~Tstate-actor: ~A~%" (-> obj state-actor))
  (format #t "~T~Tflags: ~D~%" (-> obj flags))
  (format #t "~T~Tlocked: ~A~%" (-> obj locked))
  (format #t "~T~Tauto-close: ~A~%" (-> obj auto-close))
  (format #t "~T~Tone-way: ~A~%" (-> obj one-way))
  obj
  )

;; definition for function eco-door-event-handler
;; INFO: Return type mismatch symbol vs object.
(defbehavior eco-door-event-handler eco-door ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
  (let ((v1-0 arg2))
    (the-as object (when (= v1-0 'trigger)
                     (set! (-> self locked) (not (-> self locked)))
                     (cond
                       ((-> self locked)
                        (if (= (-> self next-state name) 'door-closed)
                            (sound-play "door-lock")
                            )
                        )
                       (else
                         (sound-play "door-unlock")
                         )
                       )
                     #t
                     )
            )
    )
  )

;; failed to figure out what this is:
eco-door-event-handler

;; failed to figure out what this is:
(defstate door-closed (eco-door)
  :virtual #t
  :event eco-door-event-handler
  :code (behavior ()
    (ja :num-func num-func-identity :frame-num 0.0)
    (suspend)
    (update-transforms! (-> self root-override))
    (ja-post)
    (loop
      (when (and *target* (>= (-> self open-distance)
                              (vector-vector-distance (-> self root-override trans) (-> *target* control trans))
                              )
                 )
        (dummy-26 self)
        (if (and (not (-> self locked))
                 (or (and (-> self entity) (logtest? (-> self entity extra perm status) (entity-perm-status complete)))
                     (send-event *target* 'query 'powerup (pickup-type eco-blue))
                     (and (-> self one-way) (< (vector4-dot (-> self out-dir) (target-pos 0)) -8192.0))
                     )
                 )
            (go-virtual door-opening)
            )
        (level-hint-spawn (game-text-id zero) (the-as string #f) (-> self entity) *entity-pool* (game-task none))
        )
      (suspend)
      )
    (none)
    )
  )

;; failed to figure out what this is:
(defstate door-opening (eco-door)
  :virtual #t
  :event eco-door-event-handler
  :code (behavior ()
    (let ((gp-0
            (and (not (and (-> self entity) (logtest? (-> self entity extra perm status) (entity-perm-status complete))))
                 (send-event *target* 'query 'powerup (pickup-type eco-blue))
                 )
            )
          )
      (if gp-0
          (sound-play "blue-eco-on" :position (the-as symbol (-> self root-override trans)))
          )
      (sound-play-by-name (-> self open-sound) (new-sound-id) 1024 0 0 (sound-group sfx) #t)
      (clear-collide-with-as (-> self root-override))
      (until (ja-done? 0)
        (ja :num! (seek! max (-> self speed)))
        (if (and gp-0 (rand-vu-percent? 0.5))
            (spawn-projectile-blue *target*)
            )
        (suspend)
        )
      )
    (go-virtual door-open)
    (none)
    )
  :post (the-as (function none :behavior eco-door) transform-post)
  )

;; failed to figure out what this is:
(defstate door-open (eco-door)
  :virtual #t
  :event eco-door-event-handler
  :code (behavior ()
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (process-entity-status! self (entity-perm-status complete) #t)
    (clear-collide-with-as (-> self root-override))
    (ja :num-func num-func-identity :frame-num max)
    (logior! (-> self draw status) (draw-status hidden))
    (suspend)
    (update-transforms! (-> self root-override))
    (ja-post)
    (loop
      (let ((f30-0 (vector4-dot (-> self out-dir) (target-pos 0)))
            (f28-0 (vector4-dot (-> self out-dir) (camera-pos)))
            )
        (when (and (-> self auto-close)
                   (or (not *target*) (< (-> self close-distance)
                                         (vector-vector-distance (-> self root-override trans) (-> *target* control trans))
                                         )
                       )
                   )
          (if (and (>= (* f30-0 f28-0) 0.0) (< 16384.0 (fabs f28-0)))
              (go-virtual door-closing)
              )
          )
        )
      (suspend)
      )
    (none)
    )
  )

;; failed to figure out what this is:
(defstate door-closing (eco-door)
  :virtual #t
  :event eco-door-event-handler
  :code (behavior ()
    (restore-collide-with-as (-> self root-override))
    (logclear! (-> self draw status) (draw-status hidden))
    (let ((gp-0 (new 'stack 'overlaps-others-params)))
      (set! (-> gp-0 options) (the-as uint 1))
      (set! (-> gp-0 tlist) #f)
      (while (find-overlapping-shapes (-> self root-override) gp-0)
        (suspend)
        )
      )
    (sound-play-by-name (-> self close-sound) (new-sound-id) 1024 0 0 (sound-group sfx) #t)
    (until (ja-done? 0)
      (ja :num! (seek! 0.0 (-> self speed)))
      (suspend)
      )
    (if (-> self locked)
        (sound-play "door-lock")
        )
    (go-virtual door-closed)
    (none)
    )
  :post (the-as (function none :behavior eco-door) transform-post)
  )

;; definition for method 26 of type eco-door
;; INFO: Return type mismatch int vs none.
(defmethod dummy-26 eco-door ((obj eco-door))
  (when (-> obj state-actor)
    (if (logtest? (-> obj state-actor extra perm status) (entity-perm-status complete))
        (set! (-> obj locked) (logtest? (-> obj flags) (eco-door-flags ecdf01)))
        (set! (-> obj locked) (logtest? (-> obj flags) (eco-door-flags ecdf00)))
        )
    )
  0
  (none)
  )

;; definition for method 24 of type eco-door
;; INFO: Return type mismatch int vs none.
(defmethod TODO-RENAME-24 eco-door ((obj eco-door))
  (let ((s5-0 (new 'process 'collide-shape obj (collide-list-enum hit-by-player))))
    (let ((s4-0 (new 'process 'collide-shape-prim-mesh s5-0 (the-as uint 0) (the-as uint 0))))
      (set! (-> s4-0 prim-core collide-as) (collide-kind wall-object))
      (set! (-> s4-0 collide-with) (collide-kind target))
      (set! (-> s4-0 prim-core action) (collide-action solid))
      (set! (-> s4-0 prim-core offense) (collide-offense indestructible))
      (set! (-> s4-0 transform-index) 0)
      (set-vector! (-> s4-0 local-sphere) 0.0 0.0 0.0 16384.0)
      (set-root-prim! s5-0 s4-0)
      )
    (set! (-> s5-0 nav-radius) (* 0.75 (-> s5-0 root-prim local-sphere w)))
    (backup-collide-with-as s5-0)
    (set! (-> obj root-override) s5-0)
    )
  0
  (none)
  )

;; definition for method 25 of type eco-door
;; INFO: Return type mismatch int vs none.
(defmethod TODO-RENAME-25 eco-door ((obj eco-door))
  0
  (none)
  )

;; definition for method 11 of type eco-door
;; INFO: Return type mismatch object vs none.
(defmethod init-from-entity! eco-door ((obj eco-door) (arg0 entity-actor))
  (TODO-RENAME-24 obj)
  (process-drawable-from-entity! obj arg0)
  (let ((f0-0 (res-lump-float (-> obj entity) 'scale :default 1.0)))
    (set-vector! (-> obj root-override scale) f0-0 f0-0 f0-0 1.0)
    )
  (set! (-> obj open-distance) 32768.0)
  (set! (-> obj close-distance) 49152.0)
  (set! (-> obj speed) 1.0)
  (set! (-> obj state-actor) #f)
  (let ((v1-5 (entity-actor-lookup arg0 'state-actor 0)))
    (if v1-5
        (set! (-> obj state-actor) v1-5)
        )
    )
  (set! (-> obj locked) #f)
  (set! (-> obj flags) (res-lump-value arg0 'flags eco-door-flags))
  (dummy-26 obj)
  (set! (-> obj auto-close) (logtest? (-> obj flags) (eco-door-flags auto-close)))
  (set! (-> obj one-way) (logtest? (-> obj flags) (eco-door-flags one-way)))
  (vector-z-quaternion! (-> obj out-dir) (-> obj root-override quat))
  (set! (-> obj out-dir w) (- (vector-dot (-> obj out-dir) (-> obj root-override trans))))
  (update-transforms! (-> obj root-override))
  (TODO-RENAME-25 obj)
  (if (and (not (-> obj auto-close))
           (-> obj entity)
           (logtest? (-> obj entity extra perm status) (entity-perm-status complete))
           )
      (go (method-of-object obj door-open))
      (go (method-of-object obj door-closed))
      )
  (none)
  )
