<< first < prev 1 2 3 4 5 6 7 8 9 next > last >> Casting SPELs
Casting SPELs

Now we're going to learn an incredibly powerful feature of Lisp: Creating SPELs!

SPEL is short for "Semantic Program Enhancement Logic" and lets us create new behavior inside the world of our computer code that changes the Lisp language at a fundamental level in order to customize its behavior for our needs- It's the part of Lisp that looks most like magic. To enable SPELs, we first need to activate SPELs inside our Lisp compiler (Don't worry about what this line does - Advanced Lispers should click here.)

(defmacro defspel [& rest] `(defmacro [email protected]))

Ok, now that they're enabled, let's cast our first spell, called walk:

(defspel walk [direction] `(walk-direction '~direction))

What this code does is it tells the Lisp compiler that the word walk is not actually the word walk but the word walk-direction and that the word direction actually has a quote in front of it, even though we can't see it. Basically we can sneak in some special code inbetween our program and the compiler that changes our code into something else before it is compiled:

Compiling a SPEL

Notice how similar this function looks to the code we had written before for describe-path: In Lisp, not only do code and data look a lot identical, but code and special commands to the compiler (the SPELs) look identical as well - A very consistent and clean design! Let's try our new spell:

(walk east)
user=> (walk east)                                             
(you are in the living room of a wizards house -
there is a wizard snoring loudly on the couch -
there is a door going west from here -
there is a stairway going upstairs from here -
you see a whiskey-bottle on the floor -
you see a bucket on the floor -)

much better!

Now we'll create a command to pickup objects in our world:

(defn pickup-object [object]
  (cond (is-at? object location object-locations)
          (def object-locations (assoc object-locations object 'body))
          `(you are now carrying the ~object))
        :else '(you cannot get that.)))

This function checks to see if the object is indeed on the floor of the current location - If it is, it pushes the new location (the player's body) onto the list (pushing means to add a new item to the list, in a way that the assoc command sees and therefore hides the previous location) and returns a sentence letting us know wether it succeeded.

Now let's cast another SPEL to make the command easier to use:

(defspel pickup [object] `(spel-print (pickup-object '~object)))

Now let's try our new SPEL:

(pickup whiskey-bottle)
user=> (pickup whiskey-bottle)
(you are now carrying the whiskey-bottle)

Now let's add a couple more useful commands - First, a command that lets us see our current inventory of items we're carrying:

(defn inventory []
  (filter (fn [x] (is-at? x 'body object-locations)) objects))

Now a function that tells us if he have a certain object on us:

(defn have? [object]
   (some #{object} (inventory)))

<< first < prev 1 2 3 4 5 6 7 8 9 next > last >> Casting SPELs