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

;; name: elec-gate.gc
;; name in dgo: elec-gate
;; dgos: SEH, DESG, MIA, CIA, STAA

(define-extern set-sewg-electricity-scale! (function float int none))
(define-extern set-sewh-electricity-scale! (function float int none))

;; DECOMP BEGINS

(deftype elec-gate-params (structure)
  ((bolt-spec        lightning-spec)
   (ring-spec        lightning-spec)
   (ring-radius-min  float)
   (ring-radius-max  float)
   (speed-mult       float)
   (min-dist         float)
   (max-dist         float)
   (plane-expand-xz  float)
   (plane-expand-y   float)
   (plane-shift-z    float)
   )
  )


(deftype elec-gate-bolt (structure)
  ((ring         lightning-control  2)
   (bolt         lightning-control)
   (ring-radius  float)
   (pos          float)
   )
  )


(deftype elec-wall (structure)
  ((pos  vector  :inline)
   (dir  vector  :inline)
   )
  )


(deftype elec-gate (process-drawable)
  ((params              elec-gate-params)
   (path-l              path-control              :overlay-at path)
   (path-r              path-control)
   (l-bolt              elec-gate-bolt            5 :inline)
   (part-on             sparticle-launch-control  :overlay-at part)
   (part-off            sparticle-launch-control)
   (part-spawner-left   part-spawner)
   (part-spawner-right  part-spawner)
   (on-start            pair)
   (on-stop             pair)
   (on-shutdown         pair)
   (on-trigger          pair)
   (dividing-wall       elec-wall                 :inline)
   (plane               elec-wall                 2 :inline)
   (wall-y              float)
   (wall-xz             float)
   (lightning-quality   float)
   (quality-enabled?    symbol)
   )
  (:state-methods
    idle
    active
    shutdown-camera
    shutdown
    )
  (:methods
    (get-params (_type_) elec-gate-params)
    (elec-gate-method-25 (_type_) none)
    (elec-gate-method-26 (_type_) none)
    (go-initial-state (_type_) object)
    (spawn-particles (_type_ sparticle-launch-control) none)
    (set-elec-scale! (_type_ float) none)
    (elec-gate-method-30 (_type_ float) none)
    )
  )


(define *default-elec-gate-params* (new 'static 'elec-gate-params
                                     :bolt-spec (new 'static 'lightning-spec
                                       :name #f
                                       :flags (lightning-spec-flags lsf2)
                                       :start-color (new 'static 'rgba :r #xff :g #xff :b #xff :a #x80)
                                       :end-color (new 'static 'rgba :a #x80)
                                       :fade-to-color (new 'static 'rgba :r #xbf :b #x8f :a #x5)
                                       :fade-start-factor 0.2
                                       :fade-time 120.0
                                       :texture (new 'static 'texture-id :index #x8f :page #x4)
                                       :reduction 0.42
                                       :num-points 16
                                       :box-size 8601.6
                                       :merge-factor 0.5
                                       :merge-count 2
                                       :radius 1638.4
                                       :duration -1.0
                                       :sound #f
                                       )
                                     :ring-spec (new 'static 'lightning-spec
                                       :name #f
                                       :flags (lightning-spec-flags lsf2)
                                       :rand-func #x3
                                       :start-color (new 'static 'rgba :r #xff :g #xff :b #xff :a #x80)
                                       :end-color (new 'static 'rgba :r #xff :g #xff :b #xff :a #x80)
                                       :fade-to-color (new 'static 'rgba :r #xbf :b #x8f :a #x5)
                                       :fade-start-factor 0.2
                                       :fade-time 120.0
                                       :texture (new 'static 'texture-id :index #x3f :page #x4)
                                       :reduction 0.42
                                       :num-points 12
                                       :box-size 3072.0
                                       :merge-factor 0.5
                                       :radius 2048.0
                                       :duration -1.0
                                       :sound #f
                                       )
                                     :ring-radius-min 1638.4
                                     :ring-radius-max 2867.2
                                     :speed-mult 1.0
                                     :min-dist 163840.0
                                     :max-dist 491520.0
                                     :plane-expand-xz 8192.0
                                     :plane-expand-y 81920.0
                                     )
        )

(defbehavior elec-gate-post elec-gate ()
  (local-vars (sv-96 lightning-control) (sv-112 vector))
  (let ((gp-0 (-> self params)))
    (dotimes (s5-0 5)
      (let* ((s2-0 (-> self l-bolt s5-0))
             (s4-0 (get-point-at-percent-along-path! (-> self path) (new 'stack-no-clear 'vector) (-> s2-0 pos) 'interp))
             (s3-0 (get-point-at-percent-along-path! (-> self path-r) (new 'stack-no-clear 'vector) (-> s2-0 pos) 'interp))
             )
        (let ((a0-2 (-> s2-0 bolt))
              (v1-4 s4-0)
              )
          (set! (-> a0-2 state meet data 0 quad) (-> v1-4 quad))
          )
        (let ((a0-5 (-> s2-0 bolt))
              (v1-6 s3-0)
              )
          (vector-copy! (-> a0-5 state meet data (+ (-> a0-5 state points-to-draw) -1)) v1-6)
          )
        (when (-> gp-0 ring-spec)
          (let ((s1-0 (-> gp-0 ring-spec num-points))
                (s0-0 (-> s2-0 ring 0))
                )
            (set! sv-96 (-> s2-0 ring 1))
            (set! sv-112 (new 'stack-no-clear 'vector))
            (set! (-> sv-112 x) 0.0)
            (set! (-> sv-112 y) 0.0)
            (set! (-> sv-112 z) (-> s2-0 ring-radius))
            (set! (-> sv-112 w) 0.0)
            (let ((f30-0 (* 65536.0 (/ 1.0 (the float (+ s1-0 -1))))))
              (dotimes (s2-1 s1-0)
                (set-point! s0-0 s2-1 (vector+! (new 'stack-no-clear 'vector) s4-0 sv-112))
                (set-point! sv-96 s2-1 (vector+! (new 'stack-no-clear 'vector) s3-0 sv-112))
                (vector-rotate-y! sv-112 sv-112 f30-0)
                )
              )
            )
          )
        )
      )
    )
  (debug-draw (-> self path))
  (debug-draw (-> self path-r))
  (none)
  )

(defstate idle (elec-gate)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('trigger)
       (let ((gp-0 (-> self on-trigger)))
         (if gp-0
             (script-eval gp-0 :vector (-> self root trans))
             )
         )
       (go-virtual active)
       )
      )
    )
  :enter (behavior ()
    (process-entity-status! self (entity-perm-status subtask-complete) #t)
    (dotimes (v1-0 5)
      (let ((a0-3 (-> self l-bolt v1-0 bolt))
            (a1-1 0)
            )
        (let ((a2-2 (!= a1-1 (-> a0-3 state mode))))
          (case a1-1
            ((3)
             (if a2-2
                 (set! (-> a0-3 state counter) 0.0)
                 )
             )
            ((1)
             (set! (-> a0-3 state start-color) (-> a0-3 spec start-color))
             (set! (-> a0-3 state end-color) (-> a0-3 spec end-color))
             )
            )
          )
        (set! (-> a0-3 state mode) (the-as uint a1-1))
        )
      (when (-> self params ring-spec)
        (let ((a0-9 (-> self l-bolt v1-0 ring 0))
              (a1-2 0)
              )
          (let ((a2-12 (!= a1-2 (-> a0-9 state mode))))
            (case a1-2
              ((3)
               (if a2-12
                   (set! (-> a0-9 state counter) 0.0)
                   )
               )
              ((1)
               (set! (-> a0-9 state start-color) (-> a0-9 spec start-color))
               (set! (-> a0-9 state end-color) (-> a0-9 spec end-color))
               )
              )
            )
          (set! (-> a0-9 state mode) (the-as uint a1-2))
          )
        (let ((a0-12 (-> self l-bolt v1-0 ring 1))
              (a1-3 0)
              )
          (let ((a2-22 (!= a1-3 (-> a0-12 state mode))))
            (case a1-3
              ((3)
               (if a2-22
                   (set! (-> a0-12 state counter) 0.0)
                   )
               )
              ((1)
               (set! (-> a0-12 state start-color) (-> a0-12 spec start-color))
               (set! (-> a0-12 state end-color) (-> a0-12 spec end-color))
               )
              )
            )
          (set! (-> a0-12 state mode) (the-as uint a1-3))
          )
        )
      )
    )
  :code sleep-code
  :post (behavior ()
    (set-elec-scale! self 0.0)
    (if (nonzero? (-> self part-off))
        (spawn-particles self (-> self part-off))
        )
    )
  )

(defstate active (elec-gate)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('shutdown)
       (go-virtual shutdown-camera)
       )
      )
    )
  :enter (behavior ()
    (process-entity-status! self (entity-perm-status subtask-complete) #f)
    (if (-> self on-start)
        (script-eval (-> self on-start) :vector (-> self root trans))
        )
    (dotimes (v1-7 5)
      (set! (-> self l-bolt v1-7 pos) (+ -1.0 (* 0.2 (the float v1-7))))
      (let ((a0-9 (-> self l-bolt v1-7 bolt))
            (a1-3 0)
            )
        (let ((a2-3 (!= a1-3 (-> a0-9 state mode))))
          (case a1-3
            ((3)
             (if a2-3
                 (set! (-> a0-9 state counter) 0.0)
                 )
             )
            ((1)
             (set! (-> a0-9 state start-color) (-> a0-9 spec start-color))
             (set! (-> a0-9 state end-color) (-> a0-9 spec end-color))
             )
            )
          )
        (set! (-> a0-9 state mode) (the-as uint a1-3))
        )
      (when (-> self params ring-spec)
        (let ((a0-15 (-> self l-bolt v1-7 ring 0))
              (a1-4 0)
              )
          (let ((a2-13 (!= a1-4 (-> a0-15 state mode))))
            (case a1-4
              ((3)
               (if a2-13
                   (set! (-> a0-15 state counter) 0.0)
                   )
               )
              ((1)
               (set! (-> a0-15 state start-color) (-> a0-15 spec start-color))
               (set! (-> a0-15 state end-color) (-> a0-15 spec end-color))
               )
              )
            )
          (set! (-> a0-15 state mode) (the-as uint a1-4))
          )
        (let ((a0-18 (-> self l-bolt v1-7 ring 1))
              (a1-5 0)
              )
          (let ((a2-23 (!= a1-5 (-> a0-18 state mode))))
            (case a1-5
              ((3)
               (if a2-23
                   (set! (-> a0-18 state counter) 0.0)
                   )
               )
              ((1)
               (set! (-> a0-18 state start-color) (-> a0-18 spec start-color))
               (set! (-> a0-18 state end-color) (-> a0-18 spec end-color))
               )
              )
            )
          (set! (-> a0-18 state mode) (the-as uint a1-5))
          )
        )
      )
    (let ((gp-0 (new 'stack-no-clear 'inline-array 'vector 2)))
      (dotimes (v1-10 2)
        (set! (-> gp-0 v1-10 quad) (the-as uint128 0))
        )
      (let ((s5-0 (new 'stack-no-clear 'vector)))
        0.0
        (get-point-in-path! (-> self path) (-> gp-0 0) 0.0 'interp)
        (get-point-in-path! (-> self path-r) (-> gp-0 1) 0.0 'interp)
        (vector-! s5-0 (-> gp-0 1) (-> gp-0 0))
        (vector-normalize! s5-0 (+ (-> self params plane-expand-xz) (res-lump-float (-> self entity) 'width)))
        (vector+! (-> gp-0 1) (-> gp-0 1) s5-0)
        (vector-negate! s5-0 s5-0)
        (vector+! (-> gp-0 0) (-> gp-0 0) s5-0)
        (vector-! s5-0 (-> gp-0 1) (-> gp-0 0))
        (vector-normalize! s5-0 -1.0)
        (vector-cross! s5-0 s5-0 *y-vector*)
        (vector+float*! (-> gp-0 0) (-> gp-0 0) s5-0 (-> self params plane-shift-z))
        (vector+float*! (-> gp-0 1) (-> gp-0 1) s5-0 (-> self params plane-shift-z))
        (vector-normalize-copy! s5-0 *up-vector* -8192.0)
        (vector+! (-> gp-0 0) (-> gp-0 0) s5-0)
        (vector+! (-> gp-0 1) (-> gp-0 1) s5-0)
        )
      (let ((f0-12
              (+ (- (-> (get-point-in-path! (-> self path) (new 'stack-no-clear 'vector) (get-num-segments (-> self path)) 'interp)
                        y
                        )
                    (-> (get-point-in-path! (-> self path) (new 'stack-no-clear 'vector) 0.0 'interp) y)
                    )
                 (-> self params plane-expand-y)
                 )
              )
            )
        (blocking-plane-spawn (the-as curve-control #f) gp-0 f0-12)
        )
      )
    )
  :trans (behavior ()
    (local-vars
      (sv-176 lightning-spec)
      (sv-192 int)
      (sv-208 symbol)
      (sv-224 symbol)
      (sv-240 int)
      (sv-256 symbol)
      (sv-272 symbol)
      )
    (let ((gp-0 *target*))
      (when gp-0
        (when (not (focus-test? gp-0 disable dead ignore grabbed pilot-riding pilot mech teleporting invulnerable))
          (let* ((v1-5 (get-trans gp-0 0))
                 (a1-2 (vector-! (new 'stack-no-clear 'vector) v1-5 (-> self dividing-wall pos)))
                 (s5-0 (-> self plane (if (< 0.0 (vector-dot a1-2 (-> self dividing-wall dir)))
                                          0
                                          1
                                          )
                           )
                       )
                 (s4-1 (vector-! (new 'stack-no-clear 'vector) v1-5 (-> s5-0 pos)))
                 )
            (when (and (< (vector-dot (vector-normalize-copy! (new 'stack-no-clear 'vector) s4-1 1.0) (-> s5-0 dir)) 0.0)
                       (let ((v1-10 s4-1))
                         (< (sqrtf (+ (* (-> v1-10 x) (-> v1-10 x)) (* (-> v1-10 z) (-> v1-10 z)))) (-> self wall-xz))
                         )
                       (< (fabs (-> s4-1 y)) (-> self wall-y))
                       )
              (send-event
                gp-0
                'attack
                #f
                (static-attack-info :mask (vehicle-impulse-factor) ((id (new-attack-id))
                                                                    (damage 2.0)
                                                                    (vehicle-damage-factor 1.0)
                                                                    (vehicle-impulse-factor 1.0)
                                                                    (vector (-> s5-0 dir))
                                                                    (shove-back (meters 6))
                                                                    (shove-up (meters 3))
                                                                    (control (if (focus-test? gp-0 board)
                                                                                 1.0
                                                                                 0.0
                                                                                 )
                                                                             )
                                                                    )
                                    )
                )
              (let* ((s5-1 (-> self l-bolt))
                     (s4-2 (get-point-at-percent-along-path! (-> self path) (new 'stack-no-clear 'vector) (-> s5-1 0 pos) 'interp))
                     (s5-2
                       (get-point-at-percent-along-path! (-> self path-r) (new 'stack-no-clear 'vector) (-> s5-1 0 pos) 'interp)
                       )
                     )
                (let ((s3-0 (get-process *default-dead-pool* lightning-tracker #x4000 0)))
                  (when s3-0
                    (let ((t9-6 (method-of-type lightning-tracker activate)))
                      (t9-6 (the-as lightning-tracker s3-0) *entity-pool* "lightning-tracker" (the-as pointer #x70004000))
                      )
                    (let ((s2-0 run-function-in-process)
                          (s1-0 s3-0)
                          (s0-0 lightning-tracker-init)
                          )
                      (set! sv-176 (-> self params bolt-spec))
                      (set! sv-192 15)
                      (set! sv-208 (the-as symbol #f))
                      (set! sv-224 (the-as symbol #f))
                      (let ((t3-0 (get-trans gp-0 3)))
                        ((the-as (function object object object object object object object object none) s2-0)
                         s1-0
                         s0-0
                         sv-176
                         sv-192
                         sv-208
                         sv-224
                         s4-2
                         t3-0
                         )
                        )
                      )
                    (-> s3-0 ppointer)
                    )
                  )
                (let ((s4-3 (get-process *default-dead-pool* lightning-tracker #x4000 0)))
                  (when s4-3
                    (let ((t9-10 (method-of-type lightning-tracker activate)))
                      (t9-10 (the-as lightning-tracker s4-3) *entity-pool* "lightning-tracker" (the-as pointer #x70004000))
                      )
                    (let ((s3-1 run-function-in-process)
                          (s2-1 s4-3)
                          (s1-1 lightning-tracker-init)
                          (s0-1 (-> self params bolt-spec))
                          )
                      (set! sv-240 15)
                      (set! sv-256 (the-as symbol #f))
                      (set! sv-272 (the-as symbol #f))
                      (let ((t3-1 (get-trans gp-0 3)))
                        ((the-as (function object object object object object object object object none) s3-1)
                         s2-1
                         s1-1
                         s0-1
                         sv-240
                         sv-256
                         sv-272
                         s5-2
                         t3-1
                         )
                        )
                      )
                    (-> s4-3 ppointer)
                    )
                  )
                )
              )
            )
          )
        (set! (-> self lightning-quality) (lerp-scale
                                            0.0
                                            1.0
                                            (vector-vector-distance (get-trans gp-0 0) (-> self root trans))
                                            (-> self params max-dist)
                                            (-> self params min-dist)
                                            )
              )
        )
      )
    (let ((gp-1 (+ (the int (* 5.0 (-> self lightning-quality))) 1)))
      (dotimes (s5-4 5)
        (let ((s3-3 (-> self l-bolt s5-4))
              (s2-3 (-> self params))
              (s4-5 (if (and (-> self quality-enabled?) (zero? (mod s5-4 gp-1)))
                        0
                        -1
                        )
                    )
              )
          (seek! (-> s3-3 pos) 1.5 (* (-> s2-3 speed-mult) (+ 0.2 (fabs (-> s3-3 pos))) (seconds-per-frame)))
          (cond
            ((>= (-> s3-3 pos) 1.5)
             (set! (-> s3-3 ring-radius) (rand-vu-float-range (-> s2-3 ring-radius-min) (-> s2-3 ring-radius-max)))
             (set! (-> s3-3 pos) 0.0)
             )
            ((>= (-> s3-3 pos) 1.0)
             (let ((v1-56 (-> self l-bolt s5-4 bolt))
                   (a0-40 (logand 0 s4-5))
                   )
               (let ((a1-21 (!= a0-40 (-> v1-56 state mode))))
                 (case a0-40
                   ((3)
                    (if a1-21
                        (set! (-> v1-56 state counter) 0.0)
                        )
                    )
                   ((1)
                    (set! (-> v1-56 state start-color) (-> v1-56 spec start-color))
                    (set! (-> v1-56 state end-color) (-> v1-56 spec end-color))
                    )
                   )
                 )
               (set! (-> v1-56 state mode) (the-as uint a0-40))
               )
             (when (-> self params ring-spec)
               (let ((v1-62 (-> self l-bolt s5-4 ring 0))
                     (a0-41 (logand 0 s4-5))
                     )
                 (let ((a1-31 (!= a0-41 (-> v1-62 state mode))))
                   (case a0-41
                     ((3)
                      (if a1-31
                          (set! (-> v1-62 state counter) 0.0)
                          )
                      )
                     ((1)
                      (set! (-> v1-62 state start-color) (-> v1-62 spec start-color))
                      (set! (-> v1-62 state end-color) (-> v1-62 spec end-color))
                      )
                     )
                   )
                 (set! (-> v1-62 state mode) (the-as uint a0-41))
                 )
               (let ((a0-42 (-> self l-bolt s5-4 ring 1))
                     (v1-65 (logand 0 s4-5))
                     )
                 (let ((a1-41 (!= v1-65 (-> a0-42 state mode))))
                   (case v1-65
                     ((3)
                      (if a1-41
                          (set! (-> a0-42 state counter) 0.0)
                          )
                      )
                     ((1)
                      (set! (-> a0-42 state start-color) (-> a0-42 spec start-color))
                      (set! (-> a0-42 state end-color) (-> a0-42 spec end-color))
                      )
                     )
                   )
                 (set! (-> a0-42 state mode) (the-as uint v1-65))
                 )
               )
             )
            ((< 0.0 (-> s3-3 pos))
             (when (zero? (-> self l-bolt s5-4 bolt state mode))
               (let ((v1-74 (-> self l-bolt s5-4 bolt))
                     (a0-43 (logand s4-5 1))
                     )
                 (let ((a1-51 (!= a0-43 (-> v1-74 state mode))))
                   (case a0-43
                     ((3)
                      (if a1-51
                          (set! (-> v1-74 state counter) 0.0)
                          )
                      )
                     ((1)
                      (set! (-> v1-74 state start-color) (-> v1-74 spec start-color))
                      (set! (-> v1-74 state end-color) (-> v1-74 spec end-color))
                      )
                     )
                   )
                 (set! (-> v1-74 state mode) (the-as uint a0-43))
                 )
               (when (-> self params ring-spec)
                 (let ((v1-80 (-> self l-bolt s5-4 ring 0))
                       (a0-44 (logand s4-5 1))
                       )
                   (let ((a1-61 (!= a0-44 (-> v1-80 state mode))))
                     (case a0-44
                       ((3)
                        (if a1-61
                            (set! (-> v1-80 state counter) 0.0)
                            )
                        )
                       ((1)
                        (set! (-> v1-80 state start-color) (-> v1-80 spec start-color))
                        (set! (-> v1-80 state end-color) (-> v1-80 spec end-color))
                        )
                       )
                     )
                   (set! (-> v1-80 state mode) (the-as uint a0-44))
                   )
                 (let ((a0-45 (-> self l-bolt s5-4 ring 1))
                       (v1-83 (logand s4-5 1))
                       )
                   (let ((a1-71 (!= v1-83 (-> a0-45 state mode))))
                     (case v1-83
                       ((3)
                        (if a1-71
                            (set! (-> a0-45 state counter) 0.0)
                            )
                        )
                       ((1)
                        (set! (-> a0-45 state start-color) (-> a0-45 spec start-color))
                        (set! (-> a0-45 state end-color) (-> a0-45 spec end-color))
                        )
                       )
                     )
                   (set! (-> a0-45 state mode) (the-as uint v1-83))
                   )
                 )
               )
             )
            )
          )
        )
      )
    )
  :code sleep-code
  :post (behavior ()
    (set-elec-scale! self 1.0)
    (if (nonzero? (-> self part))
        (spawn-particles self (-> self part))
        )
    (update! (-> self sound))
    (elec-gate-post)
    )
  )

(defstate shutdown-camera (elec-gate)
  :virtual #t
  :code (behavior ()
    (suspend-for (seconds 1)
      )
    (format 0 "~s~%" (-> self name))
    (if (res-lump-struct (-> self entity) 'camera-name structure)
        (process-spawn
          external-camera-controller
          (-> self entity)
          600
          #f
          :name "external-camera-controller"
          :to *entity-pool*
          )
        )
    (suspend-for (seconds 1)
      )
    (go-virtual shutdown)
    )
  :post (behavior ()
    (elec-gate-post)
    )
  )

(defstate shutdown (elec-gate)
  :virtual #t
  :enter (behavior ()
    (process-entity-status! self (entity-perm-status subtask-complete) #t)
    (stop! (-> self sound))
    (sound-play "elec-gate-off")
    (blocking-plane-destroy)
    (if (-> self on-stop)
        (script-eval (-> self on-stop) :vector (-> self root trans))
        )
    )
  :trans (behavior ()
    (let ((gp-0 #t))
      (dotimes (s5-0 5)
        (let ((s4-0 (-> self l-bolt s5-0)))
          (seek! (-> s4-0 pos) 0.0 (seconds-per-frame))
          (set! gp-0 (cond
                       ((or (< 1.0 (-> s4-0 pos)) (>= 0.0 (-> s4-0 pos)))
                        (when (= (-> self l-bolt s5-0 bolt state mode) 2)
                          (let ((v1-9 (-> self l-bolt s5-0 bolt))
                                (a0-6 3)
                                )
                            (let ((a1-2 (!= a0-6 (-> v1-9 state mode))))
                              (case a0-6
                                ((3)
                                 (if a1-2
                                     (set! (-> v1-9 state counter) 0.0)
                                     )
                                 )
                                ((1)
                                 (set! (-> v1-9 state start-color) (-> v1-9 spec start-color))
                                 (set! (-> v1-9 state end-color) (-> v1-9 spec end-color))
                                 )
                                )
                              )
                            (set! (-> v1-9 state mode) (the-as uint a0-6))
                            )
                          (when (-> self params ring-spec)
                            (let ((v1-14 (-> self l-bolt s5-0 ring 0))
                                  (a0-8 3)
                                  )
                              (let ((a1-12 (!= a0-8 (-> v1-14 state mode))))
                                (case a0-8
                                  ((3)
                                   (if a1-12
                                       (set! (-> v1-14 state counter) 0.0)
                                       )
                                   )
                                  ((1)
                                   (set! (-> v1-14 state start-color) (-> v1-14 spec start-color))
                                   (set! (-> v1-14 state end-color) (-> v1-14 spec end-color))
                                   )
                                  )
                                )
                              (set! (-> v1-14 state mode) (the-as uint a0-8))
                              )
                            (let ((v1-17 (-> self l-bolt s5-0 ring 1))
                                  (a0-9 3)
                                  )
                              (let ((a1-22 (!= a0-9 (-> v1-17 state mode))))
                                (case a0-9
                                  ((3)
                                   (if a1-22
                                       (set! (-> v1-17 state counter) 0.0)
                                       )
                                   )
                                  ((1)
                                   (set! (-> v1-17 state start-color) (-> v1-17 spec start-color))
                                   (set! (-> v1-17 state end-color) (-> v1-17 spec end-color))
                                   )
                                  )
                                )
                              (set! (-> v1-17 state mode) (the-as uint a0-9))
                              )
                            )
                          )
                        gp-0
                        )
                       (else
                         #f
                         )
                       )
                )
          )
        )
      (when gp-0
        (let ((gp-1 (-> self on-shutdown)))
          (if gp-1
              (script-eval gp-1 :vector (-> self root trans))
              )
          )
        (go-virtual idle)
        )
      )
    )
  :code sleep-code
  :post (behavior ()
    (set-elec-scale! self 0.0)
    (if (nonzero? (-> self part-off))
        (spawn-particles self (-> self part-off))
        )
    (elec-gate-post)
    )
  )

(defmethod set-elec-scale! ((this elec-gate) (arg0 float))
  (if (< (vector-vector-distance (-> this root trans) (target-pos 0)) 327680.0)
      (elec-gate-method-30 this arg0)
      )
  0
  (none)
  )

(defmethod elec-gate-method-30 ((this elec-gate) (arg0 float))
  0
  (none)
  )

(defmethod spawn-particles ((this elec-gate) (arg0 sparticle-launch-control))
  (if (-> this part-spawner-left)
      (spawn arg0 (the-as vector (&-> this part-spawner-left child 8)))
      )
  (if (-> this part-spawner-right)
      (spawn arg0 (the-as vector (&-> this part-spawner-right child 8)))
      )
  0
  (none)
  )

;; WARN: Return type mismatch process-drawable vs elec-gate.
(defmethod relocate ((this elec-gate) (offset int))
  (dotimes (v1-0 5)
    (let ((a2-2 (-> this l-bolt v1-0)))
      (if (nonzero? (-> a2-2 bolt))
          (&+! (-> a2-2 bolt) offset)
          )
      (if (nonzero? (-> a2-2 ring 0))
          (&+! (-> a2-2 ring 0) offset)
          )
      (if (nonzero? (-> a2-2 ring 1))
          (&+! (-> a2-2 ring 1) offset)
          )
      )
    )
  (if (nonzero? (-> this path-r))
      (&+! (-> this path-r) offset)
      )
  (when (nonzero? (-> this part-off))
    (if (nonzero? (-> this part-off))
        (&+! (-> this part-off) offset)
        )
    )
  (the-as elec-gate ((method-of-type process-drawable relocate) this offset))
  )

(defmethod deactivate ((this elec-gate))
  "Make a process dead, clean it up, remove it from the active pool, and return to dead pool."
  (if (nonzero? (-> this root))
      (set-elec-scale! this 0.0)
      )
  (if (nonzero? (-> this part-off))
      (kill-particles (-> this part-off))
      )
  (call-parent-method this)
  (none)
  )

(defmethod get-params ((this elec-gate))
  *default-elec-gate-params*
  )

(defmethod elec-gate-method-25 ((this elec-gate))
  0
  (none)
  )

(defmethod elec-gate-method-26 ((this elec-gate))
  0
  (none)
  )

;; WARN: Return type mismatch int vs object.
(defmethod go-initial-state ((this elec-gate))
  (if (or (logtest? (actor-option user17) (-> this fact options))
          (and (-> this entity) (logtest? (-> this entity extra perm status) (entity-perm-status subtask-complete)))
          )
      (go (method-of-object this idle))
      (go (method-of-object this active))
      )
  0
  )

(defmethod init-from-entity! ((this elec-gate) (arg0 entity-actor))
  "Set up a newly created process from the entity that created it."
  (set! (-> this root) (new 'process 'trsqv))
  (process-drawable-from-entity! this arg0)
  (set! (-> this entity) arg0)
  (set! (-> this fact)
        (new 'process 'fact-info this (pickup-type eco-pill-random) (-> *FACT-bank* default-eco-pill-green-inc))
        )
  (set! (-> this params) (get-params this))
  (logclear! (-> this mask) (process-mask actor-pause))
  (set! (-> this path) (new 'process 'path-control this 'pathl 0.0 (the-as entity #f) #f))
  (logior! (-> this path flags) (path-control-flag display draw-line draw-point draw-text))
  (set! (-> this path-r) (new 'process 'path-control this 'pathr 0.0 (the-as entity #f) #f))
  (logior! (-> this path-r flags) (path-control-flag display draw-line draw-point draw-text))
  (set! (-> this part-spawner-left) (the-as part-spawner (entity-actor-lookup arg0 'alt-actor 0)))
  (set! (-> this part-spawner-right) (the-as part-spawner (entity-actor-lookup arg0 'alt-actor 1)))
  (let ((s5-1 (-> this params)))
    (dotimes (s4-0 5)
      (let ((s3-0 (-> this l-bolt s4-0)))
        (set! (-> s3-0 bolt) (new 'process 'lightning-control (-> s5-1 bolt-spec) this 0.0))
        (when (-> s5-1 ring-spec)
          (set! (-> s3-0 ring 0) (new 'process 'lightning-control (-> s5-1 ring-spec) this 0.0))
          (set! (-> s3-0 ring 1) (new 'process 'lightning-control (-> s5-1 ring-spec) this 0.0))
          )
        (set! (-> s3-0 ring-radius) (rand-vu-float-range (-> s5-1 ring-radius-min) (-> s5-1 ring-radius-max)))
        (set! (-> s3-0 pos) (* 0.2 (the float s4-0)))
        )
      )
    )
  (let* ((s4-1 (get-point-in-path! (-> this path) (new 'stack-no-clear 'vector) 0.0 'interp))
         (v1-29 (get-point-in-path! (-> this path-r) (new 'stack-no-clear 'vector) 0.0 'interp))
         (a1-15 (vector-! (new 'stack-no-clear 'vector) v1-29 s4-1))
         (s5-3 (vector+float*! (new 'stack-no-clear 'vector) s4-1 a1-15 0.5))
         (v1-31 (vector-normalize-copy! (new 'stack-no-clear 'vector) a1-15 1.0))
         )
    (vector-cross! v1-31 v1-31 *up-vector*)
    (set! (-> this dividing-wall pos quad) (-> s5-3 quad))
    (vector-copy! (-> this dividing-wall dir) v1-31)
    (vector+float*! (-> this plane 0 pos) s5-3 v1-31 12288.0)
    (vector-copy! (-> this plane 0 dir) v1-31)
    (vector-float*! v1-31 v1-31 -1.0)
    (vector+float*! (-> this plane 1 pos) s5-3 v1-31 12288.0)
    (vector-copy! (-> this plane 1 dir) v1-31)
    )
  (set! (-> this wall-xz)
        (vector-vector-distance
          (get-point-in-path! (-> this path) (new 'stack-no-clear 'vector) 0.0 'interp)
          (get-point-in-path! (-> this path-r) (new 'stack-no-clear 'vector) 0.0 'interp)
          )
        )
  (set! (-> this wall-xz) (/ (-> this wall-xz) 2))
  (+! (-> this wall-xz) 4096.0)
  (set! (-> this wall-y)
        (fabs
          (- (-> (get-point-in-path! (-> this path) (new 'stack-no-clear 'vector) (get-num-segments (-> this path)) 'interp)
                 y
                 )
             (-> (get-point-in-path! (-> this path) (new 'stack-no-clear 'vector) 0.0 'interp) y)
             )
          )
        )
  (+! (-> this wall-y) 4096.0)
  (set! (-> this quality-enabled?) #t)
  (set! (-> this lightning-quality) 1.0)
  (set! (-> this sound)
        (new 'process 'ambient-sound (static-sound-spec "electric-gate" :group 0 :fo-max 70) (-> this root trans) 0.0)
        )
  (set! (-> this on-start) (res-lump-struct (-> this entity) 'on-start pair))
  (set! (-> this on-stop) (res-lump-struct (-> this entity) 'on-stop pair))
  (set! (-> this on-shutdown) (res-lump-struct (-> this entity) 'on-shutdown pair))
  (set! (-> this on-trigger) (res-lump-struct (-> this entity) 'on-trigger pair))
  (elec-gate-method-25 this)
  (elec-gate-method-26 this)
  (go-initial-state this)
  )

(deftype sewer-elec-gate (elec-gate)
  ((gate-index  int32)
   )
  )


(defmethod set-elec-scale! ((this sewer-elec-gate) (arg0 float))
  (set-sewh-electricity-scale! arg0 (-> this gate-index))
  (if (zero? (-> this gate-index))
      (set-sewg-electricity-scale! arg0 (-> this gate-index))
      )
  0
  (none)
  )

(defmethod init-from-entity! ((this sewer-elec-gate) (arg0 entity-actor))
  "Set up a newly created process from the entity that created it."
  (set! (-> this gate-index) (res-lump-value (-> this entity) 'index int :time -1000000000.0))
  (call-parent-method this arg0)
  )
