(begin

; general
(define nil (quote ()))
(define fst car)
(define (snd lst) (car (cdr lst)))
(define (square x) (* x x))
(define (flip func) (lambda (arg1 arg2) (func arg2 arg1)))
(define (foldl func accum lst) (if (null? lst) accum (foldl func (func accum (car lst)) (cdr lst))))
(define (foldr func end lst) (if (null? lst) end (func (car lst) (foldr func end (cdr lst)))))
(define (sum lst) (foldl + 0 lst))
;(define (map func lst) (foldr (lambda (x y) (cons (func x) y)) () lst))
(define (map proc items)
  (if (null? items)
      nil
      (cons (proc (car items))
            (map proc (cdr items)))))
(define (filter pred lst) (foldr (lambda (x y) (if (pred x) (cons x y) y)) () lst))
(define (zip a b)
	(if (or (null? a) (null? b))
		nil
		(cons (cons (car a) (car b))
			(zip (cdr a) (cdr b)))))
(define (zip3 a b c)
	(if (or (null? a) (or (null? b) (null? c)))
		nil
		(cons (list (car a) (car b) (car c))
			(zip3 (cdr a) (cdr b) (cdr c)))))
(define (clamp x minimum maximum) (if (> x maximum) maximum (if (< x minimum) minimum x)))
(define (saturate x) (clamp x 0 1))
(define (sign x) (if (>= x 0) 1 -1))

; create a function which when executed checks if cond has been true
; consecutively for the specified amount of time (in seconds)
(define (makeDebounce time)
	(define timer 0)
	(lambda (cond) (begin
		(if cond (if (>= timer 0) (set! timer (+ timer AGC.dt)) ()) (set! timer 0))
		(if (> timer time) (begin (set! timer -1) (id #t)) (id #f))))
)

; same as above, but only fires once
(define (makeDebounceOneshot time)
	(define timer 0)
	(lambda (cond) (begin
		(if cond (if (>= timer 0) (set! timer (+ timer AGC.dt)) ()) ())
		(if (> timer time) (begin (set! timer -1) (id #t)) (id #f))))
)

; create a function which, when executed, takes an error value and
; returns a control value which should reduce the error
(define (makePIDController Kp Ki Kd maximum minimum)
	(define prevError 0)
	(define intAccum 0)
	(lambda (error) (begin
		(set! intAccum (+ intAccum (* error AGC.dt)))
		(define action (+ (* Kp error) (* Ki intAccum) (* Kd (/ (- error prevError) AGC.dt))))
		(define clamped (clamp action minimum maximum))
		(if (not (= clamped action)) (set! intAccum (- intAccum (* error AGC.dt))) ())
		(set! prevError error)
		(id action)
		)
	)
)

; same as above but works on 3 dimensional values
(define (makePIDController3d Kp Ki Kd maximum minimum)
	(define prevError (vec3 0 0 0))
	(define intAccum (vec3 0 0 0))
	(lambda (error) (begin
		(set! intAccum (add3 intAccum (mul3 error (toVec3 AGC.dt))))
		(define action (add3 (add3 (mul3 Kp error) (mul3 Ki intAccum)) (mul3 Kd (div3 (sub3 error prevError) (toVec3 AGC.dt)))))
		(define clamped (clamp3 action minimum maximum))
		(if (> (distance3 action clamped) 0.01) (set! intAccum (sub3 intAccum (mul3 error (toVec3 AGC.dt)))) ())
		(set! prevError error)
		(id action)
		)
	)
)

; 3d vector library

; constructors
(define vec3 list)
(define (toVec3 n) (list n n n))

; accessors
(define vec3.x car)
(define (vec3.y v) (car (cdr v)))
(define (vec3.z v) (car (cdr (cdr v))))

; operations
(define (mapBinary3 a b func) (map 
	(lambda (abpair) (func (fst abpair) (snd abpair)))
	(zip a b)))
(define (add3 a b) (mapBinary3 a b +))
(define (sub3 a b) (mapBinary3 a b -))
(define (mul3 a b) (mapBinary3 a b *))
(define (inv3 a) (div3 (toVec3 1) a))
(define (div3 a b) (mapBinary3 a b /))
(define (dot3 a b) (sum (mul3 a b)))
(define (magnitude3 a) (sqrt (dot3 a a)))
(define (distance3 a b) (magnitude3 (sub3 a b)))
(define (clamp3 v minimum3 maximum3) (map 
	(lambda (n_minimum_maximum) (apply clamp n_minimum_maximum))
	(zip3 v minimum3 maximum3)))
(define (sign3 v) (map sign v))

; quaternion library
; any angles are in degrees

; constructors
(define quat list)
(define quatId (list 0 0 0 1))

; defined internally:
; quatEulerAngles quat -> vec3
; quatMul lhs rhs -> quat
; quatAngle quat -> number
; quatEuler vec3 -> quat
; quatInverse quat -> quat
; quatSlerp from to t -> quat
; quatMulVec quat vec3 -> vec3

)
