Newer
Older
atlantis / lisp / creator.lisp
;;;
;;; Atlantis is a framework for creating text-adventure worlds.
;;; This is the Common Lisp implementation.
;;;
;;; This is an ATL code-generating module to ease the world creation
;;; process for non-coders (and those too lazy to write more than
;;; necessary...).
;;;
;;; Licensed under the terms of the GNU GPLv3.
;;; author: Daniel Vedder
;;; date: 20/06/2015
;;;

(defun import-spreadsheet (spreadsheet atl-file)
	"Import and convert a spreadsheet (requires LibreOffice)"
	(let* ((ods-pathname (parse-namestring spreadsheet))
			  (csv-pathname (make-pathname
								:directory (pathname-directory ods-pathname)
								:name (pathname-name ods-pathname)
								:type "csv")))
		;; Convert the spreadsheet to csv (only works with clisp!)
		(ext:shell (format nil "libreoffice --headless --convert-to csv ~A~A~A"
					   (namestring ods-pathname) " --outdir "
					   (namestring (make-pathname :directory
									   (pathname-directory ods-pathname)))))
		(csv-to-atl csv-pathname atl-file)))

(defun csv-to-atl (csv-pathname atl-file &aux (atl-code ""))
	"Convert a csv file to ATL"
	(setf atl-code ";; This code has been automatically generated by the
;; Atlantis world creator.")
	(do* ((csv-file (load-text-file csv-pathname)) (line-nr 2 (1+ line-nr))
			 (object-type (read-from-string
			 			  (first (split-string (first csv-file) #\,))))
			 (object-characteristics (split-string (second csv-file) #\,))
			 (line (nth line-nr csv-file) (nth line-nr csv-file))
			 (line-values (split-string line #\,) (split-string line #\,)))
		((null line) NIL)
		;; Start a new define-command
		(setf atl-code (format NIL "~A~&~%~A" atl-code
						   (concatenate 'string "define-"
							   (string-downcase (to-string object-type))
							   " " (to-string #\") (first line-values)
							   (to-string #\"))))
		;; Enter the value for each characteristic
		(dotimes (i (1- (length object-characteristics)))
			(setf atl-code (format NIL "~A~&~A" atl-code
							   (concatenate 'string (to-string #\tab)
								   (nth (1+ i) object-characteristics) " "
								   (nth (1+ i) line-values))))))
	;; Write the generated code to file
	(with-open-file (atl atl-file :direction :output)
		(format atl "~A" atl-code)))

(defun world-creator ()
	"The UI for the functions in this module"
	(clear-screen)
	(format t "~&Welcome to the Atlantis world creator!")
	(format t "~&~%What do you want to do?")
	(case (choose-number-option '("Import a spreadsheet" "Nothing"))
		(0 (progn
			   (format t "~&What OpenDocument spreadsheet do you want to import?")
			   (input-string s)
			   (format t "~&What ATL file do you want to export it to?")
			   (input-string a)
			   (import-spreadsheet s a)
			   (if (y-or-n-p "Done. Create something else?")
				   (world-creator) (start-menu))))
		(1 (start-menu))))