summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Kluyver <takowl@gmail.com>2013-01-13 15:23:46 +0000
committerThomas Kluyver <takowl@gmail.com>2013-01-13 15:23:46 +0000
commit8ac7a65685ffe58529087bbe5a0063d1e0168f5c (patch)
treec25e5bac9f8c51627f2f9f1c9fa8380d075f7841
parentd327e21c9c8fec8dea82035e87bfd1a3ba2b813c (diff)
New framework to allow combining items.
-rw-r--r--weatbag/action.py29
-rw-r--r--weatbag/items/__init__.py26
-rw-r--r--weatbag/items/unlit_torch.py6
-rw-r--r--weatbag/tiles/n2.py19
-rw-r--r--weatbag/words.py7
5 files changed, 81 insertions, 6 deletions
diff --git a/weatbag/action.py b/weatbag/action.py
index 9ef0f47..c2274ac 100644
--- a/weatbag/action.py
+++ b/weatbag/action.py
@@ -1,5 +1,6 @@
import sys
from . import words
+from weatbag.items import combine
def get_action():
"""Prompts for an action, splits it into words, and removes any prepositions.
@@ -18,7 +19,7 @@ def get_action():
move_directions = {'n','e','s','w','north','east','south','west'}
def is_move(do):
- return len(do) == 2 and (do[0] in words.move) and (do[1] in move_directions)
+ return len(do) == 2 and (do[0] in words.move) and (do[1] in move_directions)
def handle_action(tile, player, do):
if len(do) == 1 and do[0] == 'quit':
@@ -35,6 +36,32 @@ def handle_action(tile, player, do):
break
print(item, '(%d)' % n)
+ elif len(do) >= 3 and (do[0] in words.combine):
+ for split in range(2, len(do)):
+ item1 = ' '.join(do[1:split])
+ item2 = ' '.join(do[split:])
+ if player.has(item1):
+ if player.has(item2):
+ results = combine(item1, item2)
+ if results is not None:
+ player.take(item1)
+ player.take(item2)
+ player.inventory.update(results)
+ else:
+ print("You can't combine {} with {}".format(item1, item2))
+
+ else:
+ print("You don't have a {}".format(item2))
+
+ return
+
+ elif player.has(item2):
+ print("You don't have a {}".format(item1))
+ return
+
+ else:
+ print("You don't have those items.")
+
else:
tile.action(player, do)
diff --git a/weatbag/items/__init__.py b/weatbag/items/__init__.py
new file mode 100644
index 0000000..3d0922f
--- /dev/null
+++ b/weatbag/items/__init__.py
@@ -0,0 +1,26 @@
+from importlib import import_module
+from types import ModuleType
+
+def get_item_module(name):
+ """Returns the module for that item, or a blank module if no module exists.
+ """
+ name = name.replace(' ','_')
+ try:
+ return import_module('weatbag.items.' + name)
+ except ImportError:
+ return ModuleType(name, "Dummy Module")
+
+
+def _combine(item1, item2):
+ "Try combining items one way"
+ try:
+ return get_item_module(item1).combine(item2)
+ except AttributeError:
+ pass
+
+def combine(item1, item2):
+ "Try combining items either way round."
+ results = _combine(item1, item2)
+ if results is None:
+ results = _combine(item2, item1)
+ return results
diff --git a/weatbag/items/unlit_torch.py b/weatbag/items/unlit_torch.py
new file mode 100644
index 0000000..1f313ca
--- /dev/null
+++ b/weatbag/items/unlit_torch.py
@@ -0,0 +1,6 @@
+def combine(other):
+ if other == 'match':
+ print("The torch sputters into life.")
+ return ['flaming torch']
+
+ return None
diff --git a/weatbag/tiles/n2.py b/weatbag/tiles/n2.py
index 32e3ce4..70f6051 100644
--- a/weatbag/tiles/n2.py
+++ b/weatbag/tiles/n2.py
@@ -1,10 +1,16 @@
from weatbag import words
+from weatbag.utils import transfer
class Tile:
+ def __init__(self):
+ self.contents = {'match': 42}
+
def describe(self):
print("You are standing in the mouth of a cave. The air feels cool and "
"is filled with the stench of rotting flesh. "
"A dark path leads to the north. An exit to the south.")
+ if self.contents['match'] > 0:
+ print("Someone has dropped a box of matches on the ground.")
def leave(self, player, direction):
if direction=='s':
@@ -15,14 +21,21 @@ class Tile:
"that stalwart friend of the adventurer, the flaming torch.")
def action(self, player, do):
- if (do[0] in words.use or do[0]=='light') and do[1]=='torch':
- if player.has('unlit torch'):
+ if (do[0] in words.take) and ('matches' in do):
+ if transfer('match', self.contents, player.inventory, n=42):
+ print("You pick up the matches.")
+ else:
+ print("You've already taken the matches.")
+
+ elif (do[0] in words.use or do[0]=='light') and do[1]=='torch':
+ if player.has('unlit torch') and player.has('match'):
player.take('unlit torch')
+ player.take('match')
player.give('flaming torch')
print("The torch catches, casting a flickering light on the "
"cave walls.")
else:
- print("Good plan, but you don't have a torch.")
+ print("You'll need a torch and a match.")
else:
print("Sorry, I don't understand")
diff --git a/weatbag/words.py b/weatbag/words.py
index 25f762e..94d2134 100644
--- a/weatbag/words.py
+++ b/weatbag/words.py
@@ -6,7 +6,7 @@ e.g. This will recognise take, pick, get or collect::
if do[0] in weatbag.words.take:
#...
"""
-
+# Verbs
move = {'move','walk','go'}
give = {'give', 'feed', 'present'}
use = {'eat', 'use', 'wear'}
@@ -15,7 +15,10 @@ drop = {'drop'}
take = {'pick', 'take', 'get', 'collect'}
look = {'look', 'inspect', 'examine'}
attack = {'attack', 'swing', 'hit', 'punch', 'fight'}
+combine = {'combine', 'join', 'mix'}
+
+# Nouns
inventory = {'inventory', 'possessions', 'belongings', 'bag'}
surroundings = {'surroundings', 'around', 'scenery'}
-prepositions = {'up', 'down', 'on', 'under', 'in', 'at', 'to'}
+prepositions = {'up', 'down', 'on', 'under', 'in', 'at', 'to', 'with', 'and'}