home · contact · privacy
Dynamically decide new Thing IDs.
authorChristian Heller <c.heller@plomlompom.de>
Thu, 21 Feb 2019 17:51:45 +0000 (18:51 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 21 Feb 2019 17:51:45 +0000 (18:51 +0100)
new/plomrogue/commands.py
new/plomrogue/game.py
new/plomrogue/things.py

index 6a27f80075afda9904262942d16023506dd51010..84985f63e1453d11dce6784aeacbd10e25360c02 100644 (file)
@@ -80,12 +80,13 @@ def cmd_SAVE(game):
             write(f, 'THING_TYPE %s %s' % (thing.id_, thing.type_))
             write(f, 'THING_POS %s %s' % (thing.id_,
                                           stringify_yx(thing.position)))
-            task = thing.task
-            if task is not None:
-                task_args = task.get_args_string()
-                task_name = [k for k in game.tasks.keys()
-                             if game.tasks[k] == task.__class__][0]
-                write(f, 'SET_TASK:%s %s %s %s' % (task_name, thing.id_,
-                                                   task.todo, task_args))
+            if hasattr(thing, 'task'):
+                task = thing.task
+                if task is not None:
+                    task_args = task.get_args_string()
+                    task_name = [k for k in game.tasks.keys()
+                                 if game.tasks[k] == task.__class__][0]
+                    write(f, 'SET_TASK:%s %s %s %s' % (task_name, thing.id_,
+                                                       task.todo, task_args))
         write(f, 'PLAYER_ID %s' % game.world.player_id)
 cmd_SAVE.dont_save = True
index dcc6c3dffd917fc46b257877a0db9cc3982f4d77..a5ce4740417af3b2f83a104b2177418a28983a8d 100755 (executable)
@@ -37,6 +37,11 @@ class World(WorldBase):
         super().__init__(*args, **kwargs)
         self.player_id = 0
 
+    def new_thing_id(self):
+        if len(self.things) == 0:
+            return 0
+        return self.things[-1].id_ + 1
+
     def new_map(self, yx):
         self.map_ = self.game.map_type(yx)
 
@@ -69,6 +74,15 @@ class World(WorldBase):
 
     def make_new(self, yx, seed):
         import random
+
+        def add_thing(type_):
+            t = self.game.thing_types[type_](self)
+            t.position = [random.randint(0, yx[0] -1),
+                          random.randint(0, yx[1] - 1)]
+            self.things += [t]
+            return t
+
+        self.things = []
         random.seed(seed)
         self.turn = 0
         self.new_map(yx)
@@ -77,16 +91,13 @@ class World(WorldBase):
                 self.map_[pos] = '#'
                 continue
             self.map_[pos] = random.choice(('.', '.', '.', '.', 'x'))
-        player = self.game.thing_types['human'](self, 0)
-        player.position = [random.randint(0, yx[0] -1),
-                           random.randint(0, yx[1] - 1)]
-        npc = self.game.thing_types['monster'](self, 1)
-        npc.position = [random.randint(0, yx[0] -1),
-                        random.randint(0, yx[1] -1)]
-        item = self.game.thing_types['item'](self, 2)
-        item.position = [random.randint(0, yx[0] -1),
-                         random.randint(0, yx[1] -1)]
-        self.things = [player, npc, item]
+
+        player = add_thing('human')
+        self.player_id = player.id_
+        add_thing('monster')
+        add_thing('monster')
+        add_thing('item')
+        add_thing('item')
         return 'success'
 
 
index 386dbc4522071e059cb80b73647c80d289053ee3..243547b2ffb854b330fcb998a27882d8ecda2ef9 100644 (file)
@@ -5,10 +5,13 @@ from plomrogue.errors import GameError
 class ThingBase:
     type_ = '?'
 
-    def __init__(self, world, id_, position=[0,0]):
+    def __init__(self, world, id_=None, position=[0,0]):
         self.world = world
-        self.id_ = id_
         self.position = position
+        if id_ is None:
+            self.id_ = self.world.new_thing_id()
+        else:
+            self.id_ = id_