059f10f16431

Use pattern matching to clean things up
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 20 Jan 2017 00:02:27 +0000
parents 0d7c61117f41
children dfe140066ff6
branches/tags (none)
files magitek.asd package.lisp src/robots/rpg-shopkeeper.lisp

Changes

--- a/magitek.asd	Thu Jan 19 23:48:34 2017 +0000
+++ b/magitek.asd	Fri Jan 20 00:02:27 2017 +0000
@@ -13,6 +13,8 @@
                :cl-arrows
                :clss
                :drakma
+               :fare-quasiquote
+               :fare-quasiquote-readtable
                :flexi-streams
                :html-entities
                :iterate
@@ -23,6 +25,8 @@
                :sanitize
                :split-sequence
                :sqlite
+               :trivia
+               :trivia.quasiquote
 
                )
 
--- a/package.lisp	Thu Jan 19 23:48:34 2017 +0000
+++ b/package.lisp	Fri Jan 20 00:02:27 2017 +0000
@@ -52,6 +52,7 @@
     :iterate
     :cl-arrows
     :losh
+    :trivia
     :chancery
     :magitek.quickutils)
   (:export :random-string))
--- a/src/robots/rpg-shopkeeper.lisp	Thu Jan 19 23:48:34 2017 +0000
+++ b/src/robots/rpg-shopkeeper.lisp	Fri Jan 20 00:02:27 2017 +0000
@@ -1,5 +1,9 @@
 (in-package :magitek.robots.rpg-shopkeeper)
-(named-readtables:in-readtable :chancery)
+
+(named-readtables:defreadtable :nutbags-readtable
+  (:fuze :chancery :fare-quasiquote))
+
+(named-readtables:in-readtable :nutbags-readtable)
 
 ;;;; Utils --------------------------------------------------------------------
 (eval-when (:compile-toplevel :load-toplevel :execute)
@@ -234,6 +238,7 @@
   (:element element)
   (:bonus bonus))
 
+
 (defun enchantment-multiplier (enchantment)
   (declare (optimize (debug 3)))
   (ecase (first enchantment)
@@ -243,43 +248,23 @@
     (:bonus (expt 2.0 (second enchantment)))))
 
 
-(defgeneric enchanted-armor-description
-  (base enchantment-type enchantment-arguments))
-
-(defmethod enchanted-armor-description
-    (base (e (eql :protection)) enchantment-args)
-  (destructuring-bind (monster) enchantment-args
-    (format nil "~A of protection from ~A" base (monster-plural monster))))
-
-(defmethod enchanted-armor-description
-    (base (e (eql :resistance)) enchantment-args)
-  (destructuring-bind ((noun adjective)) enchantment-args
-    (declare (ignore adjective))
-    (format nil "~A of ~A resistance" base noun)))
-
-(defmethod enchanted-armor-description
-    (base (e (eql :bonus)) enchantment-args)
-  (destructuring-bind (val) enchantment-args
-    (format nil "+~D ~A" val base)))
+(defun enchanted-armor-description (base enchantment)
+  (ematch enchantment
+    (`(:protection ,monster)
+     (format nil "~A of protection from ~A" base (monster-plural monster)))
+    (`(:resistance (,element ,_))
+     (format nil "~A of ~A resistance" base element))
+    (`(:bonus ,val)
+     (format nil "+~D ~A" val base))))
 
-
-(defgeneric enchanted-weapon-description
-  (base enchantment-type enchantment-arguments))
-
-(defmethod enchanted-weapon-description
-    (base (e (eql :slaying)) enchantment-args)
-  (destructuring-bind (monster) enchantment-args
-    (format nil "~A of ~A-slaying" base (monster-singular monster))))
-
-(defmethod enchanted-weapon-description
-    (base (e (eql :element)) enchantment-args)
-  (destructuring-bind (element) enchantment-args
-    (format nil "~A ~A" (second element) base)))
-
-(defmethod enchanted-weapon-description
-    (base (e (eql :bonus)) enchantment-args)
-  (destructuring-bind (val) enchantment-args
-    (format nil "+~D ~A" val base)))
+(defun enchanted-weapon-description (base enchantment)
+  (ematch enchantment
+    (`(:slaying ,monster)
+     (format nil "~A of ~A-slaying" base (monster-singular monster)))
+    (`(:element (,_ ,element))
+     (format nil "~A ~A" element base))
+    (`(:bonus ,val)
+     (format nil "+~D ~A" val base))))
 
 
 ;;;; Armor --------------------------------------------------------------------
@@ -361,8 +346,7 @@
     (concatenate 'string
                  (if enchantment
                    (enchanted-armor-description vanilla-description
-                                                (first enchantment)
-                                                (rest enchantment))
+                                                enchantment)
                    vanilla-description)
                  (if ornament
                    (format nil ", ~A" ornament)
@@ -467,8 +451,7 @@
     (concatenate 'string
                  (if enchantment
                    (enchanted-weapon-description vanilla-description
-                                                 (first enchantment)
-                                                 (rest enchantment))
+                                                 enchantment)
                    vanilla-description)
                  (if ornament
                    (format nil ", ~A" ornament)