48 lines
1.3 KiB
Common Lisp
48 lines
1.3 KiB
Common Lisp
(defpackage :advtrains-helpers
|
|
(:use :cl :parse-float)
|
|
(:nicknames :helpers :aux)
|
|
(:export :parse-lua-number :adj-vector-of
|
|
:v3d :make-v3d :v3d-x :v3d-y :v3d-z :v3d-p
|
|
:string-to-v3d :hash-table-to-v3d))
|
|
(in-package :aux)
|
|
|
|
(defmacro adj-vector-of (element-type)
|
|
`(make-array 0
|
|
:element-type ,element-type
|
|
:adjustable t
|
|
:fill-pointer t))
|
|
|
|
(deftype lua-number () '(or float integer))
|
|
|
|
(defmacro parse-lua-number (str)
|
|
(alexandria:with-gensyms (n)
|
|
`(let ((,n (parse-float ,str :type 'real)))
|
|
(if (typep ,n 'ratio) (coerce ,n 'float) ,n))))
|
|
|
|
(defstruct v3d
|
|
(x 0 :type lua-number)
|
|
(y 0 :type lua-number)
|
|
(z 0 :type lua-number))
|
|
|
|
(defmethod print-object ((obj v3d) stream)
|
|
(with-accessors ((x v3d-x) (y v3d-y) (z v3d-z)) obj
|
|
(print-unreadable-object (obj stream)
|
|
(format stream "~a,~a,~a" x y z))))
|
|
|
|
(defmacro string-to-v3d (str)
|
|
(alexandria:with-gensyms (xs ys zs)
|
|
`(cl-ppcre:register-groups-bind
|
|
(,xs ,ys ,zs)
|
|
("^\\((-?[0-9]+),(-?[0-9]+),(-?[0-9]+)\\)$" ,str)
|
|
(when (and ,xs ,ys ,zs)
|
|
(make-v3d :x (parse-integer ,xs)
|
|
:y (parse-integer ,ys)
|
|
:z (parse-integer ,zs))))))
|
|
|
|
(defmacro hash-table-to-v3d (ht)
|
|
(alexandria:once-only
|
|
(ht)
|
|
`(make-v3d :x (gethash "x" ,ht)
|
|
:y (gethash "y" ,ht)
|
|
:z (gethash "z" ,ht))))
|