home · contact · privacy
Enforce sane create_unfound decisions.
authorChristian Heller <c.heller@plomlompom.de>
Thu, 9 Jul 2020 23:05:06 +0000 (01:05 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 9 Jul 2020 23:05:06 +0000 (01:05 +0200)
new/example_client.py
new/plomrogue/commands.py
new/plomrogue/game.py
new/plomrogue/tasks.py
new/plomrogue/things.py

index bd8bcfd1a8a9667f762e74b22414cb13e860663a..19538161a5b3d575df3d80fe9cfe77c42f3b2f26 100755 (executable)
@@ -103,13 +103,13 @@ def cmd_GAME_STATE_COMPLETE(game):
 
 
 def cmd_THING_TYPE(game, i, type_):
-    t = game.get_thing(i)
+    t = game.get_thing(i, create_unfound=True)
     t.type_ = type_
 cmd_THING_TYPE.argtypes = 'int:nonneg string'
 
 
 def cmd_THING_POS(game, i, yx):
-    t = game.get_thing(i)
+    t = game.get_thing(i,create_unfound=True)
     t.position = YX(0,0), yx
 cmd_THING_POS.argtypes = 'int:nonneg yx_tuple:nonneg'
 
@@ -159,7 +159,7 @@ class Game(GameBase):
 
     @property
     def player(self):
-        return self.get_thing(self.player_id)
+        return self.get_thing(self.player_id, create_unfound=False)
 
     def get_command(self, command_name):
         from functools import partial
@@ -370,7 +370,7 @@ class ItemsSelectorWidget(Widget):
         counter = 0
         for id_ in self.selection:
             pointer = '*' if counter == self.tui.item_pointer else ' '
-            t = self.tui.game.get_thing(id_)
+            t = self.tui.game.get_thing(id_, create_unfound=False)
             lines += ['%s %s' % (pointer, t.type_)]
             counter += 1
         line_width = self.size.x
index c09bc4e177ff145bdcf17510d0aaf13c89bc40eb..7eecf23b77a7e2fb515674626a99f33f72389855 100644 (file)
@@ -34,7 +34,7 @@ def cmd_MAP_STATS(game, map_pos, type_, population, health):
 cmd_MAP_STATS = 'yx_tuple string:thingtype int:nonneg int:nonneg'
 
 def cmd_THING_TYPE(game, i, type_):
-    t_old = game.get_thing(i)
+    t_old = game.get_thing(i, create_unfound=True)
     t_new = game.thing_types[type_](game, i)
     #attr_names_of_old = [name for name in dir(t_old) where name[:2] != '__']
     #attr_names_of_new = [name for name in dir(t_new) where name[:2] != '__']
@@ -56,21 +56,21 @@ def cmd_THING_TYPE(game, i, type_):
 cmd_THING_TYPE.argtypes = 'int:nonneg string:thingtype'
 
 def cmd_THING_POS(game, i, big_yx, small_yx):
-    t = game.get_thing(i)
+    t = game.get_thing(i, create_unfound=True)
     t.position = (big_yx, small_yx)
 cmd_THING_POS.argtypes = 'int:nonneg yx_tuple yx_tuple:nonneg'
 
 def cmd_THING_INVENTORY(game, id_, ids):
-    carrier = game.get_thing(id_)
+    carrier = game.get_thing(id_, create_unfound=True)
     carrier.inventory = ids
     for id_ in ids:
-        t = game.get_thing(id_)
+        t = game.get_thing(id_, create_unfound=True)
         t.in_inventory = True
         t.position = carrier.position
 cmd_THING_INVENTORY.argtypes = 'int:nonneg seq:int:nonneg'
 
 def cmd_THING_HEALTH(game, id_, health):
-    t = game.get_thing(id_)
+    t = game.get_thing(id_, create_unfound=True)
     t.health = health
 cmd_THING_HEALTH.argtypes = 'int:nonneg int:nonneg'
 
index 74502cac48bfd8997b019aa7daf30372111d2548..ffedbfaff14200e0af6a4f2335af5b5e0f66448b 100755 (executable)
@@ -41,7 +41,9 @@ class GameBase:
         self.turn = 0
         self.things = []
 
-    def get_thing(self, id_, create_unfound=True):
+    def get_thing(self, id_, create_unfound):
+        # No default for create_unfound because every call to get_thing
+        # should be accompanied by serious consideration whether to use it.
         for thing in self.things:
             if id_ == thing.id_:
                 return thing
@@ -133,7 +135,7 @@ class Game(GameBase):
         else:
             self.io.send('PLAYER_INVENTORY ,')
         for id_ in self.player.inventory:
-            thing = self.get_thing(id_)
+            thing = self.get_thing(id_, create_unfound=False)
             send_thing(thing)
         self.io.send('GAME_STATE_COMPLETE')
 
@@ -199,15 +201,17 @@ class Game(GameBase):
 
     @property
     def player(self):
-        return self.get_thing(self.player_id)
+        return self.get_thing(self.player_id, create_unfound=False)
 
     def new_thing_id(self):
         if len(self.things) == 0:
             return 0
         # DANGEROUS – if anywhere we append a thing to the list of lower
         # ID than the highest-value ID, this might lead to re-using an
-        # already active ID.  This should not happen anywhere in the
-        # code, but a break here might be more visible.
+        # already active ID.  This condition /should/ not be fulfilled
+        # anywhere in the code, but if it does, trouble here is one of
+        # the more obvious indicators that it does – that's why there's
+        # no safeguard here against this.
         return self.things[-1].id_ + 1
 
     def get_map(self, map_pos):
index 11d4ba33ca69b125366f4262f4a65bfc8816617f..d97b92335e3e8c40dfefe79541340bdca8dd644a 100644 (file)
@@ -68,7 +68,8 @@ class Task_PICKUP(Task):
                             % self.args[0])
 
     def do(self):
-        to_pick_up = self.thing.game.get_thing(self.args[0])
+        to_pick_up = self.thing.game.get_thing(self.args[0],
+                                               create_unfound=False)
         self.thing.inventory += [self.args[0]]
         to_pick_up.in_inventory = True
         to_pick_up.position = self.thing.position
@@ -87,7 +88,7 @@ class TaskOnInventoryItem(Task):
         return item
 
     def _eliminate_from_inventory(self):
-        item = self.thing.game.get_thing(self.args[0])
+        item = self.thing.game.get_thing(self.args[0], create_unfound=False)
         del self.thing.inventory[self.thing.inventory.index(item.id_)]
         item.in_inventory = False
         return item
index 7789223b9c9e63abbb24874692ad63cbfc0c0a2c..8900d9a983d751490b7b0d84f0802d9b2efae1ec 100644 (file)
@@ -51,7 +51,7 @@ class Thing(ThingBase):
     def _position_set(self, pos):
         super()._position_set(pos)
         for t_id in self.inventory:
-            t = self.game.get_thing(t_id)
+            t = self.game.get_thing(t_id, create_unfound=False)
             t.position = self.position
         if not self.id_ == self.game.player_id:
             return
@@ -187,12 +187,12 @@ class ThingAnimate(Thing):
 
     def hunt_food_satisfaction(self):
         for id_ in self.inventory:
-            t = self.game.get_thing(id_)
+            t = self.game.get_thing(id_, create_unfound=False)
             if t.type_ == 'food':
                 self.set_task('EAT', (id_,))
                 return True
         for id_ in self.get_pickable_items():
-            t = self.game.get_thing(id_)
+            t = self.game.get_thing(id_, create_unfound=False)
             if t.type_ == 'food':
                 self.set_task('PICKUP', (id_,))
                 return True