;;-*-Lisp-*-
(in-package goal)
(bundles "ENGINE.CGO" "GAME.CGO")
(require "kernel-defs.gc")
(declare-type joint-control basic)

(declare-type effect-control basic)

(define-extern cspace<-parented-transformq-joint! (function cspace transformq none))

(define-extern cspace<-transformq! (function cspace transformq matrix))

(define-extern vector<-cspace! (function vector cspace vector))

(define-extern create-interpolated-joint-animation-frame (function (inline-array vector) int process-drawable int))

(defenum janim-status
  :type uint16
  :bitfield #t
  (inited 0)
  (drawn 1)
  (done 2)
  (blerc 3)
  (skelf04 4)
  (spool 5)
  (blerc-done 6)
  (eye-done 7)
  (eye 8))

;; DECOMP BEGINS

;; These types are specific to joint control.
;; See mspace-h.gc for the actual joint types

;; A single joint control channel. It can control some number of joints through a single animation.
;; Multiple channels are blended together to create smooth transitions between animations.
(deftype joint-control-channel (structure)
  ((parent           joint-control)
   (command          symbol)
   (frame-interp     float)
   (frame-group      art-joint-anim)
   (frame-num        float)
   (num-func         (function joint-control-channel float float float))
   (param            float 2)
   (group-sub-index  int16)
   (group-size       int16)
   (dist             meters)
   (eval-time        uint32)
   (inspector-amount float))
  (:methods
   (debug-print-frames (_type_) _type_)))

(deftype joint-control (basic)
  ((status                  janim-status)
   (allocated-length        int16)
   (root-channel            (inline-array joint-control-channel) :offset 16)
   (blend-index             int32)
   (active-channels         int32)
   (generate-frame-function (function (inline-array vector) int process-drawable int))
   (prebind-function        (function pointer int process-drawable none))
   (postbind-function       (function process-drawable none))
   (effect                  effect-control)
   (channel                 joint-control-channel 3 :inline)
   (frame-group0            art-joint-anim :overlay-at (-> channel 0 frame-group))
   (frame-num0              float :overlay-at (-> channel 0 frame-num))
   (frame-interp0           float :overlay-at (-> channel 0 frame-interp))
   (frame-group1            art-joint-anim :offset 108)
   (frame-num1              float :offset 112)
   (frame-interp1           float :offset 104)
   (frame-group2            art-joint-anim :offset 156)
   (frame-num2              float :offset 160)
   (frame-interp2           float :offset 152))
  (:methods
   (new (symbol type int) _type_)
   (current-cycle-distance (_type_) float)
   (debug-print-channels (_type_ symbol) int)))

(deftype matrix-stack (structure)
  ((top  matrix)
   (data matrix 24 :inline)))

(deftype channel-upload-info (structure)
  ((fixed     joint-anim-compressed-fixed)
   (fixed-qwc int32)
   (frame     joint-anim-compressed-frame)
   (frame-qwc int32)
   (amount    float)
   (interp    float))
  :pack-me)

(deftype joint-work (structure)
  ((temp-mtx       matrix :inline)
   (joint-stack    matrix-stack :inline)
   (fix-jmp-table  (function none) 16)
   (frm-jmp-table  (function none) 16)
   (pair-jmp-table (function none) 16)
   (uploads        channel-upload-info 24 :inline)
   (num-uploads    int32)
   (mtx-acc        matrix 2 :inline)
   (tq-acc         transformq 100 :inline)
   (jacp-hdr       joint-anim-compressed-hdr :inline)
   (fixed-data     joint-anim-compressed-fixed :inline)
   (frame-data     joint-anim-compressed-frame 2 :inline)
   (flatten-array  float 576 :overlay-at mtx-acc)
   (flattened      vector 24 :inline :overlay-at mtx-acc)))
