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

;; name: squad-control-city.gc
;; name in dgo: squad-control-city
;; dgos: DESRESC, WWD, CWI

;; DECOMP BEGINS

(defmethod squad-control-method-10 ((this squad-control-city))
  (init! (-> this alert-state))
  (set! (-> this city-mode) (squad-city-mode zero))
  (call-parent-method this)
  (none)
  )

(defmethod add-squad-to-traffic-engine ((this squad-control-city) (arg0 traffic-engine))
  (set! (-> this traffic) arg0)
  (add-squad-ctrl arg0 this)
  (none)
  )

(defmethod squad-control-city-method-34 ((this squad-control-city))
  (dotimes (s5-0 (the-as int (-> this traffic tracker0 active-object-count)))
    (let ((a0-1 (-> this traffic tracker0 active-object-type-list s5-0)))
      (when (logtest? (-> this traffic object-type-info-array a0-1 flags) (traffic-type-flag ttf4))
        (let ((a0-5 (handle->process (-> this traffic tracker0 active-object-list s5-0))))
          (if (and (the-as vehicle a0-5)
                   (not (logtest? (-> (the-as vehicle a0-5) v-flags) (vehicle-flag persistent)))
                   (not (focus-test? (the-as vehicle a0-5) dead inactive))
                   (or (zero? (-> (the-as vehicle a0-5) draw))
                       (not (logtest? (-> (the-as vehicle a0-5) draw status) (draw-control-status on-screen)))
                       )
                   )
              (send-event a0-5 'traffic-off-force)
              )
          )
        )
      )
    )
  (dotimes (s5-1 (the-as int (-> this traffic tracker1 active-object-count)))
    (let ((a0-8 (-> this traffic tracker1 active-object-type-list s5-1)))
      (when (logtest? (-> this traffic object-type-info-array a0-8 flags) (traffic-type-flag ttf4))
        (let ((a0-12 (handle->process (-> this traffic tracker1 active-object-list s5-1))))
          (if (and (the-as process-focusable a0-12)
                   (not (focus-test? (the-as process-focusable a0-12) dead inactive))
                   (or (zero? (-> (the-as process-focusable a0-12) draw))
                       (not (logtest? (-> (the-as process-focusable a0-12) draw status) (draw-control-status on-screen)))
                       )
                   )
              (send-event a0-12 'traffic-off-force)
              )
          )
        )
      )
    )
  #f
  )

(defmethod update ((this squad-control-city))
  (call-parent-method this)
  (squad-control-city-method-34 this)
  (squad-control-method-24 this)
  0
  (none)
  )

(defmethod squad-control-city-method-38 ((this squad-control-city) (arg0 process-focusable) (arg1 process-focusable))
  (let* ((s5-0 (get-trans arg1 0))
         (f0-0 (vector-vector-xz-distance-squared (-> arg0 root trans) s5-0))
         (f1-0 245760.0)
         )
    (or (< f0-0 (* f1-0 f1-0)) (< (fabs (- (-> s5-0 y) (-> arg0 root trans y))) 61440.0))
    )
  )

(defmethod squad-control-city-method-39 ((this squad-control-city) (arg0 process-focusable) (arg1 process-focusable))
  0.0
  )

(defmethod squad-control-city-method-41 ((this squad-control-city) (arg0 process-focusable) (arg1 process-focusable))
  #t
  )

(defmethod squad-control-city-method-40 ((this squad-control-city) (arg0 process-focusable) (arg1 process-focusable))
  0
  (none)
  )

(defmethod squad-control-city-method-42 ((this squad-control-city))
  1.0
  )

(defmethod squad-control-city-method-43 ((this squad-control-city) (arg0 process-focusable))
  #t
  )

;; WARN: Return type mismatch process-drawable vs process-focusable.
(defmethod squad-control-city-method-35 ((this squad-control-city) (arg0 citizen) (arg1 handle))
  (local-vars (sv-16 vector) (sv-32 vector) (sv-48 vector))
  (let ((s2-0 (as-type (handle->process arg1) process-focusable))
        (s1-0 #f)
        (s4-1 arg0)
        )
    (let ((s3-0 (the-as process-drawable #f)))
      (let ((v1-5 #f))
        (if (or (not s2-0) (focus-test? s2-0 disable dead inactive))
            (set! s1-0 #t)
            (set! v1-5 (squad-control-city-method-38 this s4-1 s2-0))
            )
        (if (and (not s1-0) (or v1-5 (logtest? (-> s4-1 flags) (citizen-flag in-pursuit))))
            (set! s3-0 s2-0)
            )
        (when (or s1-0 (and (not (logtest? (enemy-flag lock-focus) (-> s4-1 enemy-flags)))
                            (or (not v1-5) (squad-control-city-method-43 this arg0))
                            )
                  )
          (when (= (-> s4-1 level name) 'ctypesb)
            (nop!)
            0
            )
          (when (!= (-> s4-1 faction-mode) 1)
            (set! s3-0 (get-focus-in-range this s4-1))
            (goto cfg-121)
            )
          (let* ((f30-0 0.0)
                 (f0-0 40960000.0)
                 (f28-0 (* f0-0 f0-0))
                 )
            (when #t
              (let ((v1-22 (-> *collide-player-list* alive-list next0)))
                *collide-player-list*
                (let ((s2-1 (-> v1-22 next0)))
                  (while (!= v1-22 (-> *collide-player-list* alive-list-end))
                    (let* ((v1-23 (the-as collide-shape (-> (the-as connection v1-22) param1)))
                           (a0-21 (-> v1-23 root-prim))
                           )
                      (when (logtest? (collide-spec jak civilian enemy vehicle-sphere hit-by-others-list player-list bot-targetable jak-vehicle)
                                      (-> a0-21 prim-core collide-as)
                                      )
                        (let ((s1-1 (as-type (-> v1-23 process) process-focusable)))
                          (when (and s1-1
                                     (not (focus-test? (the-as process-focusable s1-1) disable dead inactive))
                                     (!= arg0 s1-1)
                                     (squad-control-city-method-38 this s4-1 (the-as process-focusable s1-1))
                                     )
                            (squad-control-city-method-40 this s4-1 (the-as process-focusable s1-1))
                            (when (squad-control-city-method-41 this s4-1 (the-as process-focusable s1-1))
                              (let ((f26-0 (squad-control-city-method-39 this s4-1 (the-as process-focusable s1-1))))
                                (let ((v1-36 (get-idx-in-status-arr this (process->handle s1-1))))
                                  (if (>= v1-36 0)
                                      (+! f26-0 (-> this alert-state target-status-array v1-36 threat-level))
                                      )
                                  )
                                (cond
                                  ((< f30-0 f26-0)
                                   (set! f30-0 f26-0)
                                   (set! f28-0
                                         (vector-vector-xz-distance-squared (-> arg0 root trans) (get-trans (the-as process-focusable s1-1) 3))
                                         )
                                   (set! s3-0 s1-1)
                                   )
                                  ((= f26-0 f30-0)
                                   (let ((s0-2 vector-vector-xz-distance-squared))
                                     (set! sv-16 (-> arg0 root trans))
                                     (let* ((a1-25 (get-trans (the-as process-focusable s1-1) 3))
                                            (f0-4 (* (s0-2 sv-16 a1-25) (squad-control-city-method-42 this)))
                                            )
                                       (when (< f0-4 f28-0)
                                         (set! f28-0 f0-4)
                                         (set! s3-0 s1-1)
                                         )
                                       )
                                     )
                                   )
                                  )
                                )
                              )
                            )
                          )
                        )
                      )
                    (set! v1-22 s2-1)
                    *collide-player-list*
                    (set! s2-1 (-> s2-1 next0))
                    )
                  )
                )
              )
            (when #t
              (when #f
                (let ((v1-56 (-> *collide-hit-by-player-list* alive-list next0)))
                  *collide-hit-by-player-list*
                  (let ((s2-2 (-> v1-56 next0)))
                    (while (!= v1-56 (-> *collide-hit-by-player-list* alive-list-end))
                      (let* ((v1-57 (the-as collide-shape (-> (the-as connection v1-56) param1)))
                             (a0-46 (-> v1-57 root-prim))
                             )
                        (when (logtest? (collide-spec jak civilian enemy vehicle-sphere hit-by-others-list player-list bot-targetable jak-vehicle)
                                        (-> a0-46 prim-core collide-as)
                                        )
                          (let ((s1-2 (as-type (-> v1-57 process) process-focusable)))
                            (when (and s1-2
                                       (not (focus-test? (the-as process-focusable s1-2) disable dead inactive))
                                       (!= arg0 s1-2)
                                       (squad-control-city-method-38 this s4-1 (the-as process-focusable s1-2))
                                       )
                              (squad-control-city-method-40 this s4-1 (the-as process-focusable s1-2))
                              (when (squad-control-city-method-41 this s4-1 (the-as process-focusable s1-2))
                                (let ((f26-2 (squad-control-city-method-39 this s4-1 (the-as process-focusable s1-2))))
                                  (let ((v1-70 (get-idx-in-status-arr this (process->handle s1-2))))
                                    (if (>= v1-70 0)
                                        (+! f26-2 (-> this alert-state target-status-array v1-70 threat-level))
                                        )
                                    )
                                  (cond
                                    ((< f30-0 f26-2)
                                     (set! f30-0 f26-2)
                                     (set! f28-0
                                           (vector-vector-xz-distance-squared (-> arg0 root trans) (get-trans (the-as process-focusable s1-2) 3))
                                           )
                                     (set! s3-0 s1-2)
                                     )
                                    ((= f26-2 f30-0)
                                     (let ((s0-5 vector-vector-xz-distance-squared))
                                       (set! sv-32 (-> arg0 root trans))
                                       (let* ((a1-41 (get-trans (the-as process-focusable s1-2) 3))
                                              (f0-7 (* (s0-5 sv-32 a1-41) (squad-control-city-method-42 this)))
                                              )
                                         (when (< f0-7 f28-0)
                                           (set! f28-0 f0-7)
                                           (set! s3-0 s1-2)
                                           )
                                         )
                                       )
                                     )
                                    )
                                  )
                                )
                              )
                            )
                          )
                        )
                      (set! v1-56 s2-2)
                      *collide-hit-by-player-list*
                      (set! s2-2 (-> s2-2 next0))
                      )
                    )
                  )
                )
              (when #t
                (let ((v1-89 (-> *collide-hit-by-others-list* alive-list next0)))
                  *collide-hit-by-others-list*
                  (let ((s2-3 (-> v1-89 next0)))
                    (while (!= v1-89 (-> *collide-hit-by-others-list* alive-list-end))
                      (let* ((v1-90 (the-as collide-shape (-> (the-as connection v1-89) param1)))
                             (a0-71 (-> v1-90 root-prim))
                             )
                        (when (logtest? (collide-spec jak civilian enemy vehicle-sphere hit-by-others-list player-list bot-targetable jak-vehicle)
                                        (-> a0-71 prim-core collide-as)
                                        )
                          (let ((s1-3 (as-type (-> v1-90 process) process-focusable)))
                            (when (and s1-3
                                       (not (focus-test? (the-as process-focusable s1-3) disable dead inactive))
                                       (!= arg0 s1-3)
                                       (squad-control-city-method-38 this s4-1 (the-as process-focusable s1-3))
                                       )
                              (squad-control-city-method-40 this s4-1 (the-as process-focusable s1-3))
                              (when (squad-control-city-method-41 this s4-1 (the-as process-focusable s1-3))
                                (let ((f26-4 (squad-control-city-method-39 this s4-1 (the-as process-focusable s1-3))))
                                  (let ((v1-103 (get-idx-in-status-arr this (process->handle s1-3))))
                                    (if (>= v1-103 0)
                                        (+! f26-4 (-> this alert-state target-status-array v1-103 threat-level))
                                        )
                                    )
                                  (cond
                                    ((< f30-0 f26-4)
                                     (set! f30-0 f26-4)
                                     (set! f28-0
                                           (vector-vector-xz-distance-squared (-> arg0 root trans) (get-trans (the-as process-focusable s1-3) 3))
                                           )
                                     (set! s3-0 s1-3)
                                     )
                                    ((= f26-4 f30-0)
                                     (let ((s0-8 vector-vector-xz-distance-squared))
                                       (set! sv-48 (-> arg0 root trans))
                                       (let* ((a1-57 (get-trans (the-as process-focusable s1-3) 3))
                                              (f0-10 (* (s0-8 sv-48 a1-57) (squad-control-city-method-42 this)))
                                              )
                                         (when (< f0-10 f28-0)
                                           (set! f28-0 f0-10)
                                           (set! s3-0 s1-3)
                                           )
                                         )
                                       )
                                     )
                                    )
                                  )
                                )
                              )
                            )
                          )
                        )
                      (set! v1-89 s2-3)
                      *collide-hit-by-others-list*
                      (set! s2-3 (-> s2-3 next0))
                      )
                    )
                  )
                )
              )
            )
          )
        )
      (label cfg-121)
      (the-as process-focusable s3-0)
      )
    )
  )

(defmethod get-handle-pos ((this squad-control-city) (arg0 handle) (arg1 vector))
  (let ((s4-0 (as-type (handle->process arg0) process-focusable))
        (s5-1 (new 'stack-no-clear 'vector))
        )
    (when s4-0
      (cond
        ((valid-target-handle? this arg0)
         (set! (-> s5-1 quad) (the-as uint128 0))
         (dotimes (v1-7 16)
           (vector+! s5-1 s5-1 (the-as vector (+ (the-as uint (-> this primary-target-history 0 velocity)) (* v1-7 32))))
           )
         (vector-float*! s5-1 s5-1 0.0625)
         )
        (else
          (vector-copy! s5-1 (-> (the-as process-focusable s4-0) root transv))
          )
        )
      (set! (-> s5-1 y) 0.0)
      (vector+float*! arg1 (-> (the-as process-focusable s4-0) root trans) s5-1 1.5)
      )
    )
  arg1
  )

(defmethod get-city-mode ((this squad-control-city))
  (-> this city-mode)
  )

(defmethod squad-control-city-method-37 ((this squad-control-city))
  0
  (none)
  )
