home · contact · privacy
Don't generate objects at illegal positions. Plus, refactor.
[plomrogue2-experiments] / new / plomrogue / tasks.py
index 6f2f1be9ee4397630059c35eece39ebd48bda99f..dfd22f7d50094d3732a327e370080e1085ef81ca 100644 (file)
@@ -37,10 +37,12 @@ class Task_MOVE(Task):
 
     def check(self):
         test_pos = self.thing.world.map_.move(self.thing.position, self.args[0])
+        if test_pos is None:
+            raise GameError('would move outside map bounds')
         if self.thing.world.map_[test_pos] != '.':
             raise GameError('%s would move into illegal terrain' % self.thing.id_)
-        for t in self.thing.world.things:
-            if t.blocking and t.position == test_pos:
+        for t in self.thing.world.things_at_pos(test_pos):
+            if t.blocking:
                 raise GameError('%s would move into other thing' % self.thing.id_)
 
     def do(self):
@@ -71,18 +73,45 @@ class Task_PICKUP(Task):
 
 
 
-class Task_DROP(Task):
+class TaskOnInventoryItem(Task):
+    argtypes = 'int:nonneg'
+
+    def _basic_inventory_item_check(self):
+        item = self.thing.world.get_thing(self.args[0], create_unfound=False)
+        if item is None:
+            raise GameError('no thing of ID %s' % self.args[0])
+        if item.id_ not in self.thing.inventory:
+            raise GameError('no thing of ID %s in inventory' % self.args[0])
+        return item
+
+    def _eliminate_from_inventory(self):
+        item = self.thing.world.get_thing(self.args[0])
+        del self.thing.inventory[self.thing.inventory.index(item.id_)]
+        item.in_inventory = False
+        return item
+
+
+
+class Task_DROP(TaskOnInventoryItem):
     argtypes = 'int:nonneg'
 
     def check(self):
-        to_drop = self.thing.world.get_thing(self.args[0], create_unfound=False)
-        if to_drop is None:
-            raise GameError('no thing of ID %s to drop' % self.args[0])
-        if to_drop.id_ not in self.thing.inventory:
-            raise GameError('no thing of ID %s to drop in inventory'
-                            % self.args[0])
+        self._basic_inventory_item_check()
+
+    def do(self):
+        self._eliminate_from_inventory()
+
+
+
+class Task_EAT(TaskOnInventoryItem):
+    argtypes = 'int:nonneg'
+
+    def check(self):
+        to_eat = self._basic_inventory_item_check()
+        if to_eat.type_ != 'food':
+            raise GameError('thing of ID %s s not food' % self.args[0])
 
     def do(self):
-        to_drop = self.thing.world.get_thing(self.args[0])
-        del self.thing.inventory[self.thing.inventory.index(to_drop.id_)]
-        to_drop.in_inventory = False
+        to_eat = self._eliminate_from_inventory()
+        del self.thing.world.things[self.thing.world.things.index(to_eat)]
+        self.thing.health += 50