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

;; name: wasdoors-init.gc
;; name in dgo: wasdoors-init
;; dgos: WSD

;; DECOMP BEGINS

(defun wasdoors-point-inside? ((arg0 vector))
  (let ((gp-0 (new 'stack-no-clear 'inline-array 'vector 3)))
    (vector-copy! (-> gp-0 0) (new 'static 'vector :z -1.0 :w 957235.2))
    (vector-copy! (-> gp-0 1) (new 'static 'vector :x 9246720.0 :y 125747.2 :z 625049.6 :w 450560.0))
    (set! (-> gp-0 2 x) (vector4-dot (-> gp-0 0) arg0))
    (set! (-> gp-0 2 y) (vector-vector-distance (-> gp-0 1) arg0))
    (and (< 0.0 (-> gp-0 2 x)) (< (-> gp-0 2 y) (-> gp-0 1 w)))
    )
  )

(defun wasdoors-cleanup ((arg0 level))
  (let ((gp-0 12))
    (while (>= 19 gp-0)
      (let ((s5-0 (as-type (handle->process (-> *vehicle-info* handle-by-vehicle-type gp-0)) process-focusable)))
        (if (and s5-0 (wasdoors-point-inside? (-> (the-as process-focusable s5-0) root trans)))
            (send-event s5-0 'go-die)
            )
        )
      (+! gp-0 1)
      )
    )
  0
  (none)
  )

(deftype wasdoors-manager (process)
  ()
  (:state-methods
    idle
    )
  (:methods
    (repair-vehicles (_type_) none)
    )
  )


(defmethod repair-vehicles ((this wasdoors-manager))
  (let ((gp-0 12)
        (f30-0 (* 0.2 (seconds-per-frame)))
        )
    (while (>= 19 gp-0)
      (let ((s5-0 (as-type (handle->process (-> *vehicle-info* handle-by-vehicle-type gp-0)) process-focusable)))
        (if (and s5-0
                 (not (focus-test? (the-as process-focusable s5-0) dead))
                 (wasdoors-point-inside? (-> (the-as process-focusable s5-0) root trans))
                 )
            (send-event s5-0 'repair f30-0)
            )
        )
      (+! gp-0 1)
      )
    )
  0
  (none)
  )

(defstate idle (wasdoors-manager)
  :virtual #t
  :exit (behavior ()
    (let ((gp-0 'active)
          (a0-0 *level*)
          )
      (cond
        ((= (level-status? a0-0 'desert #f) gp-0)
         (wasdoors-cleanup (the-as level a0-0))
         )
        (else
          (dotimes (gp-1 44)
            (send-event (handle->process (-> *vehicle-info* handle-by-vehicle-type gp-1)) 'go-die)
            )
          )
        )
      )
    )
  :trans (behavior ()
    (repair-vehicles self)
    )
  :code sleep-code
  )

(defbehavior wasdoors-manager-init-by-other wasdoors-manager ()
  (go-virtual idle)
  )

(define *wasdoors-manager* (the-as (pointer wasdoors-manager) #f))

(defun wasdoors-manager-start ()
  (set! *wasdoors-manager* (process-spawn wasdoors-manager :name "wasdoors-manager" :to *entity-pool*))
  0
  (none)
  )

;; WARN: Return type mismatch symbol vs none.
(defun wasdoors-manager-kill ()
  (kill-by-type wasdoors-manager *active-pool*)
  (set! *wasdoors-manager* (the-as (pointer wasdoors-manager) #f))
  (none)
  )

(defun wasdoors-activate ((arg0 level))
  (wasdoors-manager-start)
  (none)
  )

(defun wasdoors-deactivate ((arg0 level))
  (wasdoors-manager-kill)
  0
  (none)
  )
