7drl 2015 day 5 type directed spell system development
I want my 7drl game Scroll to have lots of interesting spells.
So, as I'm designing its spell system, I've been looking at the types, and
considering the whole universe of possible spells that fit within the
constraints of the types.
My first throught was that a spell would be a function from
World -> World.
That allows any kind of spell that manipulates the game map. Like, for
instance a "whiteout" that projects a stream of whitespace from the
Since Scroll has a state monad, I quickly generalized that; making spell
actions a state monad
M (), which lets spells reuse other monadic actions,
and affect the whole game state, including the player. Now I could write
a spell like "teleport", or "grow".
But it quickly became apparent this was too limiting: While spells could change
the World map, the player, and even change the list of supported spells, they
had no way to prompting for input.
I tried a few types of the
Event -> M () variety, but they were all too
limiting. Finally, I settled on this type for spell actions:
M NextStep -> M NextStep.
And then I spent 3 hours exploring the universe of spells that type allows!
To understand them, it helps to see what a NextStep is:
type Step = Event -> M NextStep
data NextStep = NextStep View (Maybe Step)
NextStep is a continuation, spells take the original continuation,
and can not only modify the game state, but can return an altered
continuation. Such as one that prompts for input before performing the
spell, and then calls the original continuation to get on with the game.
That let me write "new", a most interesting spell, that lets the player
add a new way to cast an existing spell. Spells are cast using ingredients,
and so this prompts for a new ingredient to cast a spell. (I hope that
"new farming" will be one mode of play to possibly win Scroll.)
And, it lets me write spells that fail in game-ending ways. (Ie, "genocide @").
A spell can cause the game to end by returning a continuation that
Nothing as its next step.
Even better, I could write spells that return a continuation that contains a
forked background task, using the 66 line contiuation based threading system I
built in day 3. This allows writing lots
of fun spells that have an effect that lasts for a while. Things like letting
the player quickly digest letters they eat, or slow down the speed of
And then I thought of "dream". This spell stores the input continuation and
game state, and returns a modified continuation that lets the game continue
until it ends, and then restores from the point it saved. So, the player dreams
they're playing, and wakes back up where they cast the spell. A wild spell,
which can have different variants, like precognitive dreams where the same
random numbers are used as will be used upon awaking, or dreams where
knowledge carries back over to the real world in different ways.
(Supports Inception too..)
Look how easy it was to implement dreaming,
in this game that didn't have any notion of "save" or "restore"!
runDream :: M NextStep -> M NextStep -> (S -> S) -> M NextStep
runDream sleepcont wakecont wakeupstate = go =<< sleepcont
go (NextStep v ms) = return $ NextStep v $ Just $
maybe wake (go <=<) ms
wake _evt = do
I imagine that, if I were not using Haskell, I'd have just made the spell
be an action, that can do IO in arbitrary ways. Such a spell system can
of course do everything I described above and more. But, I think that
using a general IO action is so broad that it hides the interesting
possibilities like "dream".
By starting with a limited type for spells, and exploring toward
more featureful types, I was able to think about the range of
possibilities of spells that each type allowed, be inspired with
interesting ideas, and implement them quickly.
Just what I need when writing a roguelike in just 7 days!
Syndicated 2015-03-11 22:51:26 from see shy jo