Intrinsics cleanup.
authorNeil Moore <neil@s-z.org>
Sat, 31 May 2008 11:02:25 +0000 (07:02 -0400)
committerNeil Moore <neil@s-z.org>
Sat, 31 May 2008 11:02:25 +0000 (07:02 -0400)
Remove Creature.{gain,lose}_intr (.add/|= and .remove/-= are easier).

Add Creature.has to test for an intrinsic.

Rename item attribute 'intrinsic' to 'intrinsics'.

ItemClass.__new__: initialize a set of Intrinsics from a string.

ItemClass.__new__: hasstr() helper function.

creature.py
items/items.itm
level.py
thing.py

index ae3b055..c9f938e 100644 (file)
@@ -41,26 +41,10 @@ class Creature(thing.Thing):
         self.inv = loc.Inventory(self)
         self.slots = { 'hand': None, 'arm': None, 'body': None, 'head': None }
 
-    def gain_intr(self, intrinsic, permanent = True):
-        if isinstance(intrinsic, thing.Intrinsic):
-            intrinsic = set((intrinsic,))
-        if isinstance(intrinsic, set):
-            if permanent:
-                self.intrinsics |= intrinsic
-            else:
-                self.extrinsics |= intrinsic
-        else:
-            raise Exception("Trying to gain non-intrinsic %s", intrinsic)
-    def lose_intr(self, intrinsic, permanent = True):
-        if isinstance(intrinsic, thing.Intrinsic):
-            intrinsic = set((intrinsic,))
-        if isinstance(intrinsic, set):
-            if permanent:
-                self.intrinsics -= intrinsic
-            else:
-                self.extrinsics -= intrinsic
-        else:
-            raise Exception("Trying to lose non-intrinsic %s", intrinsic)
+    def has(self, intrinsic):
+        if isinstance(intrinsic, basestring):
+            intrinsic = thing.Intrinsic(intrinsic)
+        return intrinsic in self.intrinsics or intrinsic in self.extrinsics
 
     def holding(self, name):
         return self.slots[name]
@@ -94,8 +78,7 @@ class Creature(thing.Thing):
 
     def can_breathe(self):
         if isinstance(self.location, loc.Water):
-            return thing.Intrinsic("waterbreathing") in (
-                    self.intrinsics | self.extrinsics)
+            return self.has("waterbreathing")
         else:
             return True
 
@@ -151,9 +134,9 @@ class Creature(thing.Thing):
             self.unwield(self.holding(slot))
         self.slots[slot] = item
         item.wielded = (self, slot)
-        intrs = item.intrinsics()
-        if intrs:
-            self.gain_intr(set(intrs), permanent = False)
+
+        if item.intrinsics:
+            self.extrinsics |= item.intrinsics
             
         return True
 
@@ -170,15 +153,15 @@ class Creature(thing.Thing):
 
         item.wielded = None
         self.slots[slot] = None
-        if item.intrinsics():
+        if item.intrinsics:
             self.recompute_extrinsics()
         return True
 
     def recompute_extrinsics(self):
         self.extrinsics.clear()
         for equip in self.slots.values():
-            if equip and equip.intrinsics():
-                self.gain_intr(set(equip.intrinsics()), permanent = False)
+            if equip and equip.intrinsics:
+                self.extrinsics |= equip.intrinsics
 
 
 
index 63ed869..2d38872 100644 (file)
@@ -14,7 +14,7 @@ money {
 
 wieldable {
        slot: unspecified,
-       intrinsic: "",
+       intrinsics: "",
 },
 
 weapon {
@@ -53,5 +53,5 @@ mask {
 "mask of water breathing" {
        isa: mask,
        material: lapis,
-       intrinsic: "waterbreathing",
+       intrinsics: "waterbreathing",
 },
index 6be9176..35977b2 100644 (file)
--- a/level.py
+++ b/level.py
@@ -261,17 +261,14 @@ class Level (object):
                     else:
                         self.grid[i][j] = loc.Floor(self, i, j)
 
-        ct = 0
         for rno in xrange(1,nrooms):
             nct = 1 if rno == 1 or random.random() < 0.8 else 2
             for tgt in random.sample(range(0, rno), nct):
                 self.connect(rooms[rno], rooms[tgt])
-                ct = ct + 1
 
         for row in self.grid:
             for tile in row:
                 self.activate(tile)
-        __main__.AppUI.instance.message("ct is now %s" % (ct,))
     
     def activate(self, tile):
         (y, x) = (tile.y, tile.x)
index 6ff6cd3..3372441 100644 (file)
--- a/thing.py
+++ b/thing.py
@@ -103,9 +103,18 @@ class ItemClass (type):
         base = Item
         props = properties.copy()
         props['name'] = name
-        if 'material' in props and isinstance(props['material'], str):
+        def hasstr(key):
+            return key in props and isinstance(props[key], basestring)
+
+        if hasstr('material'):
             props['material'] = Material(props['material'])
-        if 'isa' in props:
+
+        if hasstr('intrinsics'):
+            props['intrinsics'] = set(
+                    Intrinsic(i) for i in props['intrinsics'].split(" ")
+                    if i != "")
+
+        if hasstr('isa'):
             base = ItemClass(props['isa'])
             del props['isa']
         return type.__new__(cls, name + " item", (base,), props)
@@ -171,8 +180,10 @@ class Thing (object):
 class Item(Thing):
     wielded = None
     slot = None
-    def intrinsics(self):
-        return [ Intrinsic(i) for i in self.intrinsic.split(" ") if i ]
+
+    def __init__(self, location=None):
+        super(Item, self).__init__(location)
+
     def is_item(self):
         return True
     def render(self, pic=None):