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

(import "goal_src/jak1/import/racer-ag.gc")

;; failed to figure out what this is:
(if (not (nmember "racerp" *kernel-packages*))
    (set! *kernel-packages* (cons "racerp" *kernel-packages*))
    )

;; definition of type racer
(deftype racer (process-drawable)
  ((parent-override (pointer target)             :offset         12)
   (root-override   collide-shape-moving         :offset        112)
   (extra-trans     vector               :inline :offset-assert 176)
   (condition       int32                        :offset-assert 192)
   (cell            handle                       :offset-assert 200)
   (path-data       path-control         2       :offset-assert 208)
   (path-target     curve-control                :offset        208)
   (path-racer      path-control                 :offset        212)
   (auto-get-off    symbol                       :offset-assert 216)
   (shadow-backup   shadow-geo                   :offset-assert 220)
   )
  :heap-base #x70
  :method-count-assert 24
  :size-assert         #xe0
  :flag-assert         #x18007000e0
  (:methods
    (wait-for-start () _type_ :state 20)
    (idle () _type_ :state 21)
    (pickup ((state collectable)) _type_ :state 22)
    (wait-for-return () _type_ :state 23)
    )
  )

;; definition for method 3 of type racer
(defmethod inspect racer ((obj racer))
  (let ((t9-0 (method-of-type process-drawable inspect)))
    (t9-0 obj)
    )
  (format #t "~T~Textra-trans: ~`vector`P~%" (-> obj extra-trans))
  (format #t "~T~Tcondition: ~D~%" (-> obj condition))
  (format #t "~T~Tcell: ~D~%" (-> obj cell))
  (format #t "~T~Tpath-data[2] @ #x~X~%" (-> obj path-data))
  (format #t "~T~Tpath-target: ~A~%" (-> obj path-target))
  (format #t "~T~Tpath-racer: ~A~%" (-> obj path-racer))
  (format #t "~T~Tauto-get-off: ~A~%" (-> obj auto-get-off))
  (format #t "~T~Tshadow-backup: ~A~%" (-> obj shadow-backup))
  obj
  )

;; definition for method 7 of type racer
;; INFO: Return type mismatch process-drawable vs racer.
(defmethod relocate racer ((obj racer) (arg0 int))
  (countdown (v1-0 2)
    (if (-> obj path-data v1-0)
        (&+! (-> obj path-data v1-0) arg0)
        )
    )
  (the-as racer ((method-of-type process-drawable relocate) obj arg0))
  )

;; failed to figure out what this is:
(defskelgroup *racer-sg* racer racer-geo-jg 3
              ((racer-geo-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 3.5)
              :shadow racer-shadow-mg
              :sort 1
              )

;; failed to figure out what this is:
(defskelgroup *racer-explode-sg* racer racer-explode-lod0-jg racer-explode-idle-ja
              ((racer-explode-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 3.5)
              )

;; definition for symbol *racer-shadow-control*, type shadow-control
(define *racer-shadow-control*
  (new 'static 'shadow-control :settings (new 'static 'shadow-settings
                                           :flags (shadow-flags disable-fade shdf03)
                                           :shadow-dir (new 'static 'vector :y -1.0 :w 614400.0)
                                           :bot-plane (new 'static 'plane :y 1.0 :w 81920.0)
                                           :top-plane (new 'static 'plane :y 1.0 :w 2048.0)
                                           )
                               )
  )

;; definition for function racer-effect
;; INFO: Return type mismatch int vs none.
(defbehavior racer-effect racer ()
  (when (!= (-> self condition) 4)
    (spawn (-> self part) (-> self root-override trans))
    (update! (-> self sound))
    )
  0
  (none)
  )

;; failed to figure out what this is:
(defstate wait-for-start (racer)
  :virtual #t
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (local-vars (v0-1 structure))
    (let ((v1-0 arg2))
      (the-as
        object
        (cond
          ((= v1-0 'trans)
           (vector+! (the-as vector (-> arg3 param 0)) (-> self root-override trans) (-> self extra-trans))
           )
          ((= v1-0 'notify)
           (set! v0-1 #t)
           (set! (-> self auto-get-off) (the-as symbol v0-1))
           v0-1
           )
          ((= v1-0 'shadow)
           (cond
             ((-> arg3 param 0)
              (set! v0-1 (-> self shadow-backup))
              (set! (-> self draw shadow) (the-as shadow-geo v0-1))
              v0-1
              )
             (else
               (set! (-> self draw shadow) #f)
               #f
               )
             )
           )
          )
        )
      )
    )
  :exit (behavior ()
    (set! (-> self root-override root-prim prim-core action) (collide-action))
    (set! (-> self root-override root-prim prim-core offense) (collide-offense no-offense))
    0
    (none)
    )
  :code (behavior ()
    (label cfg-0)
    (case (-> self condition)
      ((2)
       (let ((gp-0 (get-task-control (-> self entity extra perm task))))
         (if (and *cheat-mode* (cpad-hold? 0 l3))
             (goto cfg-77)
             )
         (first-any gp-0 #t)
         (case (current-status gp-0)
           (((task-status need-reward-speech))
            (ja-channel-set! 1)
            (ja :group! (-> self draw art-group data 3))
            (set! (-> self root-override root-prim prim-core action) (collide-action solid ca-11))
            (set! (-> self root-override root-prim prim-core offense) (collide-offense indestructible))
            (ja-post)
            )
           (((task-status invalid))
            (goto cfg-77)
            )
           )
         )
       )
      ((6)
       (if (or (task-closed? (game-task village2-levitator) (task-status need-hint))
               (and *cheat-mode* (cpad-hold? 0 l3))
               (and *target* (logtest? (-> *target* control root-prim prim-core action) (collide-action ca-9)))
               )
           (goto cfg-77)
           )
       )
      ((5)
       (cond
         ((and *cheat-mode* (cpad-hold? 0 l3))
          (goto cfg-77)
          )
         ((>= (-> *game-info* fuel) 72.0)
          (goto cfg-77)
          )
         )
       )
      ((3)
       (cond
         ((and (task-complete? *game-info* (-> self entity extra perm task)) (not (handle->process (-> self cell))))
          (when (and (-> self auto-get-off) (send-event *target* 'end-mode))
            (set! (-> self auto-get-off) #f)
            (go-virtual pickup (the-as (state collectable) (method-of-object self idle)))
            )
          (set! (-> self auto-get-off) #f)
          (goto cfg-77)
          )
         ((not (handle->process (-> self cell)))
          (set! (-> self cell)
                (ppointer->handle
                  (birth-pickup-at-point
                    (vector+! (new 'stack-no-clear 'vector) (-> self root-override trans) (new 'static 'vector :y 8192.0 :w 1.0))
                    (pickup-type fuel-cell)
                    (the float (-> self entity extra perm task))
                    #f
                    self
                    (-> self fact)
                    )
                  )
                )
          )
         )
       )
      ((4)
       (if (and *target* (zero? (logand (-> *target* control root-prim prim-core action) (collide-action ca-9))))
           (goto cfg-77)
           )
       )
      (else
        (goto cfg-77)
        )
      )
    (racer-effect)
    (suspend)
    (b! #t cfg-0 :delay (nop!))
    (label cfg-77)
    (if (and *target* (logtest? (-> *target* control root-prim prim-core action) (collide-action ca-9)))
        (go-virtual wait-for-return)
        (go-virtual idle)
        )
    (none)
    )
  )

;; failed to figure out what this is:
(defstate idle (racer)
  :virtual #t
  :event (-> (method-of-type racer wait-for-start) event)
  :enter (behavior ()
    (blocking-plane-destroy)
    (blocking-plane-spawn (-> self path-target))
    (none)
    )
  :exit (-> (method-of-type racer wait-for-start) exit)
  :code (behavior ()
    (ja-channel-set! 1)
    (ja :group! (-> self draw art-group data 3))
    (set! (-> self root-override root-prim prim-core action) (collide-action solid ca-11))
    (set! (-> self root-override root-prim prim-core offense) (collide-offense indestructible))
    0.0
    (let ((f30-0 (if (= (-> self condition) 4)
                     61440.0
                     20480.0
                     )
                 )
          )
      (loop
        (if (and *target* (logtest? (-> *target* control root-prim prim-core action) (collide-action ca-9)))
            (go-virtual wait-for-return)
            )
        (when (and (and *target* (>= f30-0 (vector-vector-distance (-> self root-override trans) (-> *target* control trans))))
                   (and (not (movie?)) (not (level-hint-displayed?)))
                   )
          (hide-hud)
          (level-hint-surpress!)
          (kill-current-level-hint '() '(sidekick voicebox) 'exit)
          (when (and (hud-hidden?) (can-grab-display? self))
            (let ((gp-0
                    (new 'stack 'font-context *font-default-matrix* 32 160 0.0 (font-color default) (font-flags shadow kerning))
                    )
                  )
              (let ((v1-24 gp-0))
                (set! (-> v1-24 width) (the float 440))
                )
              (let ((v1-25 gp-0))
                (set! (-> v1-25 height) (the float 80))
                )
              (set! (-> gp-0 flags) (font-flags shadow kerning large))
              (print-game-text (lookup-text! *common-text* (game-text-id press-to-use) #f) gp-0 #f 128 22)
              )
            (if (and (or (cpad-pressed? 0 circle) (= (-> self condition) 4)) (send-event *target* 'change-mode 'racing self))
                (go-virtual pickup (the-as (state collectable) (method-of-object self wait-for-return)))
                )
            )
          )
        (racer-effect)
        (suspend)
        )
      )
    (none)
    )
  :post (the-as (function none :behavior racer) ja-post)
  )

;; failed to figure out what this is:
(defstate pickup (racer)
  :virtual #t
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (case arg2
      (('draw)
       (ja-channel-set! 1)
       (ja :group! (-> self draw art-group data 3))
       (set! (-> self root-override root-prim prim-core action) (collide-action solid ca-11))
       (set! (-> self root-override root-prim prim-core offense) (collide-offense indestructible))
       (transform-post)
       )
      (('trans)
       (vector+! (the-as vector (-> arg3 param 0)) (-> self root-override trans) (-> self extra-trans))
       )
      (('touch 'attack)
       #f
       )
      (('shadow)
       (cond
         ((-> arg3 param 0)
          (let ((v0-1 (the-as object (-> self shadow-backup))))
            (set! (-> self draw shadow) (the-as shadow-geo v0-1))
            v0-1
            )
          )
         (else
           (set! (-> self draw shadow) #f)
           #f
           )
         )
       )
      )
    )
  :enter (behavior ((arg0 (state collectable)))
    ((the-as (function none :behavior racer) (-> arg0 enter)))
    (none)
    )
  :code (behavior ((arg0 (state collectable)))
    (ja-channel-set! 0)
    (ja-post)
    (while (zero? (ja-group-size))
      (when (or (not *target*)
                (< 24576.0 (vector-vector-distance (-> self root-override trans) (-> *target* control trans)))
                )
        (when (and *target* (logtest? (-> *target* control root-prim prim-core action) (collide-action ca-9)))
          (case (-> (level-get-target-inside *level*) name)
            (('misty)
             (close-specific-task! (game-task misty-bike) (task-status need-reminder-a))
             (level-hint-spawn (game-text-id misty-bike-hint) "sksp0062" (the-as entity #f) *entity-pool* (game-task none))
             )
            )
          )
        (go arg0)
        )
      (racer-effect)
      (suspend)
      )
    (while (and *target* (logtest? (-> *target* control root-prim prim-core action) (collide-action ca-9)))
      (racer-effect)
      (suspend)
      )
    (let ((s5-0 (-> *display* base-frame-counter)))
      (until (>= (- (-> *display* base-frame-counter) s5-0) (seconds 1))
        (racer-effect)
        (suspend)
        )
      )
    (go arg0)
    (none)
    )
  )

;; failed to figure out what this is:
(defstate wait-for-return (racer)
  :virtual #t
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (if (and (or (= arg2 'touch) (= arg2 'attack)) (and (!= (-> self condition) 4) (send-event *target* 'end-mode)))
        (go-virtual pickup (the-as (state collectable) (method-of-object self idle)))
        )
    (the-as
      object
      (cond
        ((= arg2 'trans)
         (vector+! (the-as vector (-> arg3 param 0)) (-> self root-override trans) (-> self extra-trans))
         )
        ((= arg2 'shadow)
         (cond
           ((-> arg3 param 0)
            (let ((v0-3 (the-as structure (-> self shadow-backup))))
              (set! (-> self draw shadow) (the-as shadow-geo v0-3))
              v0-3
              )
            )
           (else
             (set! (-> self draw shadow) #f)
             #f
             )
           )
         )
        )
      )
    )
  :enter (behavior ()
    (blocking-plane-destroy)
    (blocking-plane-spawn (the-as curve-control (-> self path-racer)))
    (none)
    )
  :code (behavior ()
    (ja-channel-set! 0)
    (ja-post)
    (loop
      (if (not (and *target* (logtest? (-> *target* control root-prim prim-core action) (collide-action ca-9))))
          (go-virtual idle)
          )
      (racer-effect)
      (suspend)
      )
    (none)
    )
  )

;; definition for method 11 of type racer
;; INFO: Return type mismatch object vs none.
(defmethod init-from-entity! racer ((obj racer) (arg0 entity-actor))
  (let ((s4-0 (new 'process 'collide-shape-moving obj (collide-list-enum hit-by-player))))
    (set! (-> s4-0 dynam) (copy *standard-dynamics* 'process))
    (set! (-> s4-0 reaction) default-collision-reaction)
    (set! (-> s4-0 no-reaction)
          (the-as (function collide-shape-moving collide-shape-intersect vector vector none) nothing)
          )
    (let ((s3-0 (new 'process 'collide-shape-prim-sphere s4-0 (the-as uint 0))))
      (set! (-> s3-0 prim-core collide-as) (collide-kind wall-object))
      (set! (-> s3-0 collide-with) (collide-kind target))
      (set-vector! (-> s3-0 local-sphere) 0.0 0.0 0.0 6144.0)
      (set-root-prim! s4-0 s3-0)
      )
    (set! (-> s4-0 nav-radius) (* 0.75 (-> s4-0 root-prim local-sphere w)))
    (backup-collide-with-as s4-0)
    (set! (-> obj root-override) s4-0)
    )
  (process-drawable-from-entity! obj arg0)
  (set-yaw-angle-clear-roll-pitch! (-> obj root-override) (res-lump-float arg0 'rotoffset))
  (initialize-skeleton obj *racer-sg* '())
  (set! (-> obj shadow-backup) (-> obj draw shadow))
  (set! (-> obj draw shadow-ctrl) *racer-shadow-control*)
  (let ((v1-23 (-> obj node-list data)))
    (set! (-> v1-23 0 param0) cspace<-transformq+trans!)
    (set! (-> v1-23 0 param1) (the-as basic (-> obj root-override trans)))
    (set! (-> v1-23 0 param2) (the-as basic (-> obj extra-trans)))
    )
  (set! (-> obj condition) (res-lump-value arg0 'index int))
  (set! (-> obj fact)
        (new 'process 'fact-info obj (pickup-type eco-pill-random) (-> *FACT-bank* default-pill-inc))
        )
  (set! (-> obj part) (create-launch-control (-> *part-group-id-table* 115) obj))
  (dotimes (s5-1 2)
    (let ((v1-32 (new 'process 'curve-control obj 'path (the float (+ s5-1 1)))))
      (set! (-> obj path-data s5-1) v1-32)
      (if v1-32
          (logior! (-> v1-32 flags) (path-control-flag display draw-line draw-point draw-text))
          )
      )
    )
  (set! (-> obj path) (-> obj path-target))
  (set-vector! (-> obj extra-trans) 0.0 6144.0 0.0 1.0)
  (set! (-> obj auto-get-off) #f)
  (move-to-ground (-> obj root-override) 40960.0 40960.0 #t (collide-kind background))
  (set! (-> obj cell) (the-as handle #f))
  (blocking-plane-spawn
    (the-as
      curve-control
      (if (or (and *target* (logtest? (-> *target* control root-prim prim-core action) (collide-action ca-9)))
              (= (-> obj condition) 3)
              )
          (-> obj path-racer)
          (-> obj path-target)
          )
      )
    )
  (set! (-> obj sound)
        (new 'process 'ambient-sound (static-sound-spec "zoom-teleport" :fo-max 30) (-> obj root-override trans))
        )
  (go (method-of-object obj wait-for-start))
  (none)
  )
