#!/usr/bin/gawk -f
# Cloak of Darkness demo and GNU AWK Interactive Fiction engine
# Copyright 2005 by Nick Moffitt
# This code is released under the WTFPL:
http://sam.zoy.org/wtfpl/
# Just do what you want with it!
BEGIN {
IGNORECASE = 1
EAST = "^e$|east"
WEST = "^w$|west"
NORTH = "^n$|north"
SOUTH = "^s$|south"
player = "The Player"
dark[player] = "true"
score = 0
}
#initial input sanitizing
$2 { $2 = cansee($2) }
#############################################################
# Game Data Begins Here
#############################################################
BEGIN {
foyer = "Foyer of the Opera House"
description[foyer] = "You are standing in a spacious hall, splendidly decorated in red and gold, with glittering chandeliers overhead. The entrance from the street is to the north, and there are doorways south and west."
}
here == foyer && $1 ~ NORTH {
print "You've only just arrived, and besides, the weather outside seems to be getting worse."
prompt()
}
here == foyer && $1 ~ WEST { go("Cloakroom") ; prompt() }
here == foyer && $1 ~ SOUTH { go("Foyer Bar") ; prompt() }
BEGIN {
cloakroom = "Cloakroom"
description[cloakroom] = "The walls of this small room were clearly once lined with hooks, though now only one remains. The exit is a door to the east."
}
here == cloakroom && $1 ~ EAST { go("Foyer of the Opera House") ; prompt() }
BEGIN {
hook = "small brass hook"
location[hook] = "Cloakroom"
open[hook] = 1
invisible[hook] = 1
}
$2 == hook && $1 ~ /get|take/ { print "You cannot take that!"; prompt()}
$2 == hook && $1 ~ /^l$|look|examine/ {
printf "It's just a small brass hook, "
if (location["velvet cloak"] == hook) {
printf "with a cloak hanging on it."
} else {
printf "screwed to the wall."
}
}
BEGIN {
bar = "Foyer Bar"
description[bar] = "The bar, much rougher than you'd have guessed after the opulence of the foyer to the north, is completely empty. There seems to be some sort of message scrawled in the sawdust on the floor."
dark[bar] = 1
bumblings = 0
}
here == bar && $1 ~ NORTH { go("Foyer of the Opera House") ; prompt() }
dark[bar] && here == bar {
print "In the dark? You could make quite a mess this way!"
bumblings++
prompt()
}
BEGIN {
cloak = "velvet cloak"
description[cloak] = "A handsome cloak, of velvet trimmed with satin, and slightly spattered with raindrops. Its blackness is so deep that it almost seems to suck light from the room."
location[cloak] = player
}
$2 == cloak && $1 ~ /drop|put|hang/ {
if (here == "Cloakroom") {
delete dark["Foyer Bar"]
move(cloak, "small brass hook")
print "You delicately hang the inky cloak on the small brass hook. You feel... lighter, somehow."
} else {
print "This isn't the best place to leave a smart cloak like that lying around!"
}
prompt()
}
$2 == cloak && $1 ~ /take|get/ {
if (location[$2] == player ) {
print "You are already holding the " $2 "."
prompt()
}
move($2, player)
dark["Foyer Bar"] = 1
print $2 ": Taken."
prompt()
}
BEGIN {
message = "scrawled message"
location[message] = "Foyer Bar"
invisible[message] = 1
}
$2 == message && $1 ~ /^l$|look|examine|read/ {
if (bumblings < 2) {
print "The message, neatly marked in the sawdust, reads: YOU HAVE WON."
exit 0
} else {
print "The message has been carelessly trampled, making it difficult to read. You can just distinguish the words: YOU HAVE LOST."
exit 1
}
}
BEGIN {
here = foyer
print "Hurrying through the rainswept November night, you're glad to see the bright lights of the Opera House. It's surprising that there aren't more people about but, hey, what do you expect in a cheap demo game...?"
printf "> "
}
#############################################################
# Game Data Ends Here
#############################################################
$1 ~ EAST || $1 ~ WEST || $1 ~ NORTH || $1 ~ SOUTH {
print "You cannot go that way."
prompt()
}
$1 ~ /get|take/ {
if (location[$2] == player ) {
print "You are already holding the " $2 "."
prompt()
}
move($2, player)
print $2 ": Taken."
prompt()
}
$1 ~ /^l$|look|examine/ && $2 { look($2) ; prompt()}
$1 ~ /^l$|look|examine/ { look(here) ; prompt() }
$1 ~ /^i$|^inv/ { inventory() ; prompt() }
# Default verb confusion
{
print "I don't know how to \"" $1 "\"."
prompt()
}
function prompt() {
printf "> "
next
}
function move(object, destination) {
location[object] = destination
}
function go(destination) {
here = destination
look(here)
}
function look(object) {
if (object == here) {
print here ": "
}
if (dark[here]) {
print "It is pitch black. You are likely to be eaten by a grue."
prompt()
} else {
print description[object]
}
if (object == here) {
for (item in location) {
if (location[item] == here && !invisible[item]) {
print "\tThere is a " item " here."
}
if (location[item] == here && open[item]) {
for (subitem in location) {
if (location[subitem] == item) {
print "\tThere is a " subitem " here."
}
}
}
}
}
}
function inventory() {
print "You are carrying: "
for (item in location) {
if (location[item] == player) {
print "\t" item
}
}
}
function cansee(object) {
for (item in location) {
if (location[item] == here || location[item] == player) {
if (item ~ object) {
return item
}
if (open[item]) {
for (thing in location) {
if (location[thing] == item) {
if (thing ~ object) {
return thing
}
}
}
}
}
}
print "I see no " object " here!"
prompt()
}
# ex:ts=4 sw=4 tw=72 expandtab ai