From ecf3799b03f0a9098956d529f26be54f37c6534b Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Fri, 10 Jul 2020 01:05:06 +0200 Subject: [PATCH] Enforce sane create_unfound decisions. --- new/example_client.py | 8 ++++---- new/plomrogue/commands.py | 10 +++++----- new/plomrogue/game.py | 14 +++++++++----- new/plomrogue/tasks.py | 5 +++-- new/plomrogue/things.py | 6 +++--- 5 files changed, 24 insertions(+), 19 deletions(-) diff --git a/new/example_client.py b/new/example_client.py index bd8bcfd..1953816 100755 --- a/new/example_client.py +++ b/new/example_client.py @@ -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 diff --git a/new/plomrogue/commands.py b/new/plomrogue/commands.py index c09bc4e..7eecf23 100644 --- a/new/plomrogue/commands.py +++ b/new/plomrogue/commands.py @@ -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' diff --git a/new/plomrogue/game.py b/new/plomrogue/game.py index 74502ca..ffedbfa 100755 --- a/new/plomrogue/game.py +++ b/new/plomrogue/game.py @@ -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): diff --git a/new/plomrogue/tasks.py b/new/plomrogue/tasks.py index 11d4ba3..d97b923 100644 --- a/new/plomrogue/tasks.py +++ b/new/plomrogue/tasks.py @@ -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 diff --git a/new/plomrogue/things.py b/new/plomrogue/things.py index 7789223..8900d9a 100644 --- a/new/plomrogue/things.py +++ b/new/plomrogue/things.py @@ -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 -- 2.30.2