Split and rename things.py .
authorNeil Moore <neil@s-z.org>
Wed, 28 May 2008 23:35:16 +0000 (19:35 -0400)
committerNeil Moore <neil@s-z.org>
Wed, 28 May 2008 23:35:16 +0000 (19:35 -0400)
New module creature.py
  Split off Creature, Player, and slotabbrev from things.py.

Rename things.py to thing.py.

Adjust level.py and roguelike.py to reflect changes.

creature.py [new file with mode: 0644]
level.py
roguelike.py
thing.py [moved from things.py with 62% similarity]

diff --git a/creature.py b/creature.py
new file mode 100644 (file)
index 0000000..f2018b1
--- /dev/null
@@ -0,0 +1,117 @@
+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# Copyright © 2008 Neil Moore <neil@s-z.org>.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#  
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+import random
+
+import loc
+import thing
+import ui
+
+slotabbrevs = { 'hand': 'H', 'arm': 'A', 'body': 'B' }
+
+class Creature(thing.Thing):
+    def __init__(self, location=None):
+        thing.Thing.__init__(self, location)
+        self.inv = loc.Inventory(self)
+        self.slots = { 'hand': None, 'arm': None, 'body': None }
+    def holding(self, name):
+        return self.slots[name]
+    def is_wielding(self, item):
+        return item in self.slots
+    def invalidate(self):
+        self.location.invalidate(important = True)
+    def vis_radius(self):
+        return 5
+    def can_see(self, y, x):
+        loc = self.location
+        if (y - loc.y)**2 + (x - loc.x)**2 <= self.vis_radius()**2:
+            return self.level().visible_path(loc.y, loc.x, y, x)
+        return False
+    def visibles(self):
+        vis = []
+        py = self.location.y
+        px = self.location.x
+        rad = self.vis_radius()
+        (ymin,xmin) = self.level().clip(py - rad, px - rad)
+        (ymax,xmax) = self.level().clip(py + rad, px + rad)
+        for y in range(ymin, ymax+1):
+            for x in range(xmin, xmax+1):
+                if self.can_see(y,x):
+                    vis.append((y,x))
+        return vis
+
+    def is_creature(self):
+        return True
+    def can_swim(self):
+        return False
+    def render(self, pic=None):
+        return ( "?", ui.Color('BrightMagenta').cp )
+    def move_north(self):
+        return self.try_move(self.location.north())
+    def move_south(self):
+        return self.try_move(self.location.south())
+    def move_west(self):
+        return self.try_move(self.location.west())
+    def move_east(self):
+        return self.try_move(self.location.east())
+    def pick_up(self):
+        it = self.location.top_item()
+        if it:
+            it.try_move(self.inv)
+            return True
+        else:
+            return False
+    def wield(self, item):
+        assert item in self.inv, (
+                "%s is wielding item %s not in inventory" % (self, item))
+        assert item.wieldable(), (
+                "%s trying to wield the unwieldable %s" % (self, item))
+        assert not item.wielded, (
+                "%s trying to wield %s, already wielded by %s"
+                % (self, item, item.wielded))
+        slot = item.slot
+        if self.holding(slot):
+            self.unwield(self.holding(slot))
+        self.slots[slot] = item
+        item.wielded = (self, slot)
+        return True
+
+    def unwield(self, item):
+        assert item in self.inv, (
+                "%s is unwielding item %s not in inventory" % (self, item))
+        assert item.wielded, (
+                "%s trying to unwield already unwielded %s" % (self, item))
+
+        (creat, slot) = item.wielded
+        assert creat is self, "%s unwielding %s's %s" % (self, creat, item)
+        assert self.holding(slot) is item, (
+                "%s unwielding %s from wrong hand %s?" % (self, item, slot))
+
+        item.wielded = None
+        self.slots[slot] = None
+        return True
+
+class Player(Creature):
+    def __init__(self, location=None):
+        Creature.__init__(self, location)
+        self.active = False
+    def render(self, pic=None):
+        return ("@", ui.Color(('Bright' if self.active else '') + 'Magenta').cp)
+    def can_swim(self):
+        return random.random() < .5
index 28f13fe..d5369ae 100644 (file)
--- a/level.py
+++ b/level.py
@@ -24,7 +24,7 @@ import re
 from cStringIO import StringIO
 
 import loc
-import things
+import thing
 import cacher
 import __main__
 
@@ -426,13 +426,13 @@ class Level (object):
         if r >= 10:
             return []
         elif r >= 5:
-            return [ things.ItemClass("gold piece")() ]
+            return [ thing.ItemClass("gold piece")() ]
         elif r >= 2:
-            return [ things.ItemClass("gem")() ]
+            return [ thing.ItemClass("gem")() ]
         elif r >= 1:
-            return [ things.ItemClass("shield")() ]
+            return [ thing.ItemClass("shield")() ]
         else:
-            return [ things.ItemClass("sword")() ]
+            return [ thing.ItemClass("sword")() ]
 
     def loc(self, i, j):
         return self.grid[i][j]
index 7f3b0ea..cbffbf7 100755 (executable)
@@ -23,7 +23,8 @@ import curses.ascii
 import curses.panel
 import cProfile
 
-import things
+import thing
+import creature
 import level
 import ui
 
@@ -170,7 +171,7 @@ class AppUI:
                 (ascii, attr) = item.render()
                 win.addstr(i+1, w-3, ascii, attr)
                 if item.wielded:
-                    slotabbr = things.slotabbrevs[item.wielded[1]]
+                    slotabbr = creature.slotabbrevs[item.wielded[1]]
                     win.addstr(i+1, w-2, slotabbr, ui.Color('Cyan').cp)
 
         self.invpanel.show()
@@ -327,12 +328,12 @@ class AppUI:
 
 if __name__ == '__main__':
     def main(stdscr):
-        me = things.Player()
-        him = things.Player()
+        me = creature.Player()
+        him = creature.Player()
         ui = AppUI(stdscr, [me, him])
 
-        things.Material.loadall(open("materials/materials.mtl"))
-        things.ItemClass.loadall(open("items/items.itm"))
+        thing.Material.loadall(open("materials/materials.mtl"))
+        thing.ItemClass.loadall(open("items/items.itm"))
 
         lvl = level.Level("level0")
         me.place(lvl.loc(min(13, lvl.h - 2), min(2, lvl.w - 2)))
similarity index 62%
rename from things.py
rename to thing.py
index 46a2aed..72cf2ad 100644 (file)
--- a/things.py
+++ b/thing.py
@@ -167,95 +167,3 @@ class Item(Thing):
             # Have the holder unwield it
             self.wielded[0].unwield(self)
 
-slotabbrevs = { 'hand': 'H', 'arm': 'A', 'body': 'B' }
-
-class Creature(Thing):
-    def __init__(self, location=None):
-        Thing.__init__(self, location)
-        self.inv = loc.Inventory(self)
-        self.slots = { 'hand': None, 'arm': None, 'body': None }
-    def holding(self, name):
-        return self.slots[name]
-    def is_wielding(self, item):
-        return item in self.slots
-    def invalidate(self):
-        self.location.invalidate(important = True)
-    def vis_radius(self):
-        return 5
-    def can_see(self, y, x):
-        loc = self.location
-        if (y - loc.y)**2 + (x - loc.x)**2 <= self.vis_radius()**2:
-            return self.level().visible_path(loc.y, loc.x, y, x)
-        return False
-    def visibles(self):
-        vis = []
-        py = self.location.y
-        px = self.location.x
-        rad = self.vis_radius()
-        (ymin,xmin) = self.level().clip(py - rad, px - rad)
-        (ymax,xmax) = self.level().clip(py + rad, px + rad)
-        for y in range(ymin, ymax+1):
-            for x in range(xmin, xmax+1):
-                if self.can_see(y,x):
-                    vis.append((y,x))
-        return vis
-
-    def is_creature(self):
-        return True
-    def can_swim(self):
-        return False
-    def render(self, pic=None):
-        return ( "?", ui.Color('BrightMagenta').cp )
-    def move_north(self):
-        return self.try_move(self.location.north())
-    def move_south(self):
-        return self.try_move(self.location.south())
-    def move_west(self):
-        return self.try_move(self.location.west())
-    def move_east(self):
-        return self.try_move(self.location.east())
-    def pick_up(self):
-        it = self.location.top_item()
-        if it:
-            it.try_move(self.inv)
-            return True
-        else:
-            return False
-    def wield(self, item):
-        assert item in self.inv, (
-                "%s is wielding item %s not in inventory" % (self, item))
-        assert item.wieldable(), (
-                "%s trying to wield the unwieldable %s" % (self, item))
-        assert not item.wielded, (
-                "%s trying to wield %s, already wielded by %s"
-                % (self, item, item.wielded))
-        slot = item.slot
-        if self.holding(slot):
-            self.unwield(self.holding(slot))
-        self.slots[slot] = item
-        item.wielded = (self, slot)
-        return True
-
-    def unwield(self, item):
-        assert item in self.inv, (
-                "%s is unwielding item %s not in inventory" % (self, item))
-        assert item.wielded, (
-                "%s trying to unwield already unwielded %s" % (self, item))
-
-        (creat, slot) = item.wielded
-        assert creat is self, "%s unwielding %s's %s" % (self, creat, item)
-        assert self.holding(slot) is item, (
-                "%s unwielding %s from wrong hand %s?" % (self, item, slot))
-
-        item.wielded = None
-        self.slots[slot] = None
-        return True
-
-class Player(Creature):
-    def __init__(self, location=None):
-        Creature.__init__(self, location)
-        self.active = False
-    def render(self, pic=None):
-        return ("@", ui.Color(('Bright' if self.active else '') + 'Magenta').cp)
-    def can_swim(self):
-        return random.random() < .5