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

;; name: ctywide-init.gc
;; name in dgo: ctywide-init
;; dgos: CWI

(define-extern restore-city-speeches (function none))

;; DECOMP BEGINS

(defun update-level-info ((arg0 level))
  (let ((v1-0 *traffic-info*))
    (dotimes (a0-1 44)
      (let ((a1-1 (-> v1-0 traffic-object-type-from-vehicle-type a0-1)))
        (if (< (the-as uint a1-1) (the-as uint 29))
            (set! (-> v1-0 traffic-object-levels a1-1) (-> v1-0 vehicle-levels a0-1))
            )
        )
      )
    (let ((a0-4 *traffic-engine*))
      (when a0-4
        (dotimes (a1-5 29)
          (set! (-> a0-4 object-type-info-array a1-5 level) (-> v1-0 traffic-object-levels a1-5))
          )
        )
      )
    )
  0
  (none)
  )

(deftype dummy-alloc (structure)
  ((val  uint32)
   )
  )


(defun ctywide-login ((arg0 level))
  (format 0 "citywide-login~%")
  (malloc 'loading-level #x1b800)
  (set! *traffic-engine* (new 'loading-level 'traffic-engine))
  (set! *ff-squad-control* (new 'loading-level 'ff-squad-control))
  (set! *kg-squad-control* (new 'loading-level 'kg-squad-control))
  (set! *mh-squad-control* (new 'loading-level 'mh-squad-control))
  (set! *city-borrow-manager* (new 'loading-level 'cty-borrow-manager))
  (set! *cty-faction-manager* (new 'loading-level 'cty-faction-manager))
  (initialize-cty-attack-controller)
  (let ((t9-9 cty-attack-reset)
        (a0-9 #f)
        (a1-8 #f)
        )
    (t9-9 a0-9 a1-8 #f)
    (init! *city-borrow-manager*)
    (initialize *ff-squad-control* (the-as process a1-8))
    (initialize *kg-squad-control* (the-as process a1-8))
    (initialize *mh-squad-control* (the-as process a1-8))
    )
  (clear-borrow-holds! *city-borrow-manager*)
  (set! *city-mode* 'ctywide)
  0
  (none)
  )

(defun ctywide-activate ((arg0 level) (arg1 symbol))
  (cty-attack-reset #f #f #f)
  (format 0 "ctywide-activate (mode ~s, *spawn-actors* ~s)~%" arg1 (if *spawn-actors*
                                                                       "#t"
                                                                       "#f"
                                                                       )
          )
  (let ((v1-1 *traffic-info*))
    (when v1-1
      (set! (-> v1-1 ctywide-level) arg0)
      (set! (-> v1-1 vehicle-level) arg0)
      (set! (-> v1-1 restore-speech-callback) restore-city-speeches)
      )
    )
  (vehicle-manager-kill)
  (set! *traffic-fast-spawn* (or (= arg1 'life) (= arg1 'debug)))
  (cond
    ((and (= arg1 'debug) (not *spawn-actors*))
     (traffic-kill)
     )
    (else
      (restore-defaults *cty-faction-manager*)
      (traffic-start)
      (clear-borrow-holds! *city-borrow-manager*)
      (add-squad-to-traffic-engine *ff-squad-control* *traffic-engine*)
      (squad-control-method-10 *ff-squad-control*)
      (add-squad-to-traffic-engine *kg-squad-control* *traffic-engine*)
      (squad-control-method-10 *kg-squad-control*)
      (add-squad-to-traffic-engine *mh-squad-control* *traffic-engine*)
      (squad-control-method-10 *mh-squad-control*)
      (restore-default-settings *traffic-engine*)
      )
    )
  (let ((v1-27 *traffic-info*))
    (if (not (-> v1-27 vehicle-levels 6))
        (set! (-> v1-27 vehicle-levels 6) 'ctywide)
        )
    )
  (when *traffic-engine*
    (let ((v1-31 (-> *traffic-engine* object-type-info-array 28)))
      (logior! (-> v1-31 flags) (traffic-type-flag ttf3))
      )
    )
  0
  (none)
  )

(defun ctywide-deactivate ((arg0 level))
  (format 0 "ctywide-deactivate~%")
  (set! (-> *traffic-info* ctywide-level) #f)
  (persist-with-delay *setting-control* 'cty-faction-music 0 'music 'city1 0.0 0)
  0
  (none)
  )

(defun ctywide-logout ((arg0 level))
  (set! *traffic-engine* #f)
  (set! *ff-squad-control* #f)
  (set! *kg-squad-control* #f)
  (set! *mh-squad-control* #f)
  (clear-callback! *city-borrow-manager*)
  (set! *city-borrow-manager* #f)
  (set! *cty-faction-manager* #f)
  (set! *trail-graph* #f)
  (set! *city-mode* #f)
  (set! *cty-attack-controller* #f)
  (let ((v1-2 *traffic-info*))
    (set! (-> v1-2 vehicle-levels 6) #f)
    (set! (-> v1-2 restore-speech-callback) #f)
    )
  (when *traffic-engine*
    (let ((v1-7 (-> *traffic-engine* object-type-info-array 28)))
      (logclear! (-> v1-7 flags) (traffic-type-flag ttf3))
      )
    )
  0
  (none)
  )

(define *traffic-alert-level-force* #f)

(defun lwide-activate ((arg0 level) (arg1 symbol))
  (format #t "lwide-activate ~s~%" arg0)
  (let ((v1-0 *traffic-info*)
        (a0-2 (-> arg0 name))
        )
    (set! (-> v1-0 restore-speech-callback) restore-city-speeches)
    (case (-> arg0 name)
      (('ctycara)
       (set! (-> v1-0 vehicle-levels 3) a0-2)
       (set! (-> v1-0 vehicle-levels 4) a0-2)
       (set! (-> v1-0 vehicle-levels 5) a0-2)
       )
      (('ctycarb)
       (set! (-> v1-0 vehicle-levels 0) a0-2)
       (set! (-> v1-0 vehicle-levels 1) a0-2)
       (set! (-> v1-0 vehicle-levels 2) a0-2)
       )
      (('ctypepa)
       (set! (-> v1-0 traffic-object-levels 0) a0-2)
       (set! (-> v1-0 traffic-object-levels 1) a0-2)
       (set! (-> v1-0 traffic-object-levels 2) a0-2)
       (set! (-> v1-0 traffic-object-levels 3) a0-2)
       )
      (('ctypesb)
       (set! (-> v1-0 traffic-object-levels 8) a0-2)
       (set! (-> v1-0 traffic-object-levels 9) a0-2)
       )
      (('ctypepb)
       (set! (-> v1-0 traffic-object-levels 10) a0-2)
       )
      (('ctypesa)
       (set! (-> v1-0 traffic-object-levels 4) a0-2)
       (set! (-> v1-0 traffic-object-levels 6) a0-2)
       (set! (-> v1-0 traffic-object-levels 13) a0-2)
       )
      (('ctypesc)
       (set! (-> v1-0 traffic-object-levels 14) a0-2)
       (set! (-> v1-0 traffic-object-levels 15) a0-2)
       (set! (-> v1-0 traffic-object-levels 16) a0-2)
       )
      (('lctyhijk)
       (set! (-> v1-0 vehicle-levels 11) a0-2)
       )
      )
    (update-level-info (the-as level a0-2))
    )
  0
  (none)
  )

(defun lwide-deactivate ((arg0 level))
  (let ((v1-0 *traffic-info*))
    (dotimes (a1-0 44)
      (if (= (-> v1-0 vehicle-levels a1-0) (-> arg0 name))
          (set! (-> v1-0 vehicle-levels a1-0) #f)
          )
      )
    (dotimes (a1-3 29)
      (if (= (-> v1-0 traffic-object-levels a1-3) (-> arg0 name))
          (set! (-> v1-0 traffic-object-levels a1-3) #f)
          )
      )
    )
  (let ((v1-3 *traffic-info*))
    (set! (-> v1-3 vehicle-level) #f)
    (set! (-> v1-3 restore-speech-callback) #f)
    )
  (update-level-info arg0)
  0
  (none)
  )
