Rearrange AI code.
authorNeil Moore <neil@s-z.org>
Sat, 31 May 2008 13:03:40 +0000 (09:03 -0400)
committerNeil Moore <neil@s-z.org>
Sat, 31 May 2008 13:03:40 +0000 (09:03 -0400)
Move Monster to module ai (from creature).

Fold AI class into Monster.

ai.py
creature.py
roguelike.py

diff --git a/ai.py b/ai.py
index d5063ed..b925c32 100644 (file)
--- a/ai.py
+++ b/ai.py
 
 import math
 
-import __main__
+import creature
 
-class AI (object):
-    def __init__(self, creature):
-        self.body = creature
+class Monster (creature.Creature):
+    def __init__(self, location=None):
+        super(Monster,self).__init__(location)
 
+    def tick(self):
+        super(Monster, self).tick()
+        if not self.dead:
+            self.act()
+    
     def vis_creatures(self):
-        loc = self.body.location
-        (my, mx) = (loc.y, loc.x)
+        (my, mx) = (self.location.y, self.location.x)
 
         return sorted(
                 (critter
-                    for (y, x) in self.body.visibles()
-                    if (y, x) != (my, mx)
-                    for critter in self.body.level().loc(y, x)._creatures),
+                    for (cy, cx) in self.visibles()
+                    if (cy, cx) != (my, mx)
+                    for critter in self.level().loc(cy, cx)._creatures),
                 key = lambda c: math.sqrt(
                     (c.location.y - my)**2 + (c.location.x - mx)**2))
 
     def act(self):
-        loc = self.body.location
-        (my, mx) = (loc.y, loc.x)
+        (my, mx) = (self.location.y, self.location.x)
 
         for c in self.vis_creatures():
-            (cy, cx) = (c.location.y, c.location.x)
-            (dy, dx) = (cy - my, cx - mx)
+            (dy, dx) = (c.location.y - my, c.location.x - mx)
 
             if abs(dy) + abs(dx) <= 1:
                 pass
             elif abs(dy) > abs(dx):
                 if dy < 0:
-                    self.body.move_north()
+                    self.move_north()
                 else:
-                    self.body.move_south()
+                    self.move_south()
             else:
                 if dx < 0:
-                    self.body.move_west()
+                    self.move_west()
                 else:
-                    self.body.move_east()
+                    self.move_east()
 
             break
-
-    def die(self, cause):
-        __main__.AppUI.instance.message("AI %s died: %s" % (self, cause))
-
index e9e6c4d..db3bcaf 100644 (file)
@@ -20,7 +20,6 @@
 import random
 
 import cacher
-import ai
 import loc
 import thing
 import ui
@@ -168,20 +167,6 @@ class Creature(thing.Thing):
             if equip and equip.intrinsics:
                 self.extrinsics |= equip.intrinsics
 
-class Monster (Creature):
-    def __init__(self, location=None):
-        super(Monster,self).__init__(location)
-        self.ai = ai.AI(self)
-    def tick(self):
-        super(Monster, self).tick()
-        if self.dead:
-            self.ai = None
-        else:
-            self.ai.act()
-    def die(self, cause):
-        self.ai.die(cause)
-        super(Monster,self).die(cause)
-
 
 class Player (Creature):
     def __init__(self, location=None):
index 53ecad1..bb72a46 100755 (executable)
@@ -25,6 +25,7 @@ import cProfile
 
 import thing
 import creature
+import ai
 import level
 import ui
 
@@ -361,7 +362,7 @@ if __name__ == '__main__':
     def main(stdscr):
         me = creature.Player()
         him = creature.Player()
-        it = creature.Monster()
+        it = ai.Monster()
         ui = AppUI(stdscr, [me, him])
 
         thing.Material.loadall(open("materials/materials.mtl"))