home · contact · privacy
Add inventory / item pickup/drop server-side.
authorChristian Heller <c.heller@plomlompom.de>
Wed, 27 Feb 2019 00:33:20 +0000 (01:33 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 27 Feb 2019 00:33:20 +0000 (01:33 +0100)
new/plomrogue/commands.py
new/plomrogue/game.py
new/plomrogue/mapping.py
new/plomrogue/tasks.py
new/plomrogue/things.py

index 84985f63e1453d11dce6784aeacbd10e25360c02..280b73603e7b5f14e3dbe4424b4c33f567ecf211 100644 (file)
@@ -41,6 +41,11 @@ def cmd_THING_POS(game, i, yx):
     t.position = list(yx)
 cmd_THING_POS.argtypes = 'int:nonneg yx_tuple:nonneg'
 
+def cmd_THING_INVENTORY(game, id_, ids):
+    t = game.world.get_thing(id_)
+    t.inventory = [ids]  # TODO: test whether valid IDs
+cmd_THING_INVENTORY.argtypes = 'int:nonneg, seq:int:nonneg'
+
 def cmd_TERRAIN_LINE(game, y, terrain_line):
     game.world.map_.set_line(y, terrain_line)
 cmd_TERRAIN_LINE.argtypes = 'int:nonneg string'
@@ -80,6 +85,9 @@ 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)))
+            write(f, 'THING_INVENTORY %s %s' % (thing.id_,
+                                                ','.join([str(i) for i in
+                                                          thing.inventory])))
             if hasattr(thing, 'task'):
                 task = thing.task
                 if task is not None:
index a5ce4740417af3b2f83a104b2177418a28983a8d..51bb37271f31bbb6db66787ddc9cbc38025fcd7b 100755 (executable)
@@ -1,4 +1,4 @@
-from plomrogue.tasks import Task_WAIT, Task_MOVE
+from plomrogue.tasks import Task_WAIT, Task_MOVE, Task_PICKUP, Task_DROP
 from plomrogue.errors import ArgError
 from plomrogue.commands import (cmd_GEN_WORLD, cmd_GET_GAMESTATE, cmd_MAP,
                                 cmd_MAP, cmd_THING_TYPE, cmd_THING_POS,
@@ -107,7 +107,10 @@ class Game:
     def __init__(self, game_file_name):
         self.io = GameIO(game_file_name, self)
         self.map_type = MapHex
-        self.tasks = {'WAIT': Task_WAIT, 'MOVE': Task_MOVE}
+        self.tasks = {'WAIT': Task_WAIT,
+                      'MOVE': Task_MOVE,
+                      'PICKUP': Task_PICKUP,
+                      'DROP': Task_DROP}
         self.commands = {'GEN_WORLD': cmd_GEN_WORLD,
                          'GET_GAMESTATE': cmd_GET_GAMESTATE,
                          'MAP': cmd_MAP,
@@ -147,6 +150,13 @@ class Game:
                                               stringify_yx(thing.position)))
         player = self.world.get_player()
         self.io.send('PLAYER_POS %s' % (stringify_yx(player.position)))
+        self.io.send('PLAYER_INVENTORY %s' % ','.join([str(i) for i in
+                                                       player.inventory]))
+        for id_ in player.inventory:
+            thing = self.world.get_thing(id_)
+            self.io.send('THING_TYPE %s %s' % (thing.id_, thing.type_))
+            self.io.send('THING_POS %s %s' % (thing.id_,
+                                              stringify_yx(thing.position)))
         self.io.send('GAME_STATE_COMPLETE')
 
     def proceed(self):
index aa76b61d1123dee7c7625f03302a8e16bc399300..e21def88a96dbecfda2b3196e5c7e62c68d2cb7c 100644 (file)
@@ -62,6 +62,7 @@ class Map(MapBase):
 
     def get_neighbors(self, pos):
         neighbors = {}
+        pos = tuple(pos)
         if not hasattr(self, 'neighbors_to'):
             self.neighbors_to = {}
         if pos in self.neighbors_to:
index 262576273f4726301777fe0297f7e0b441d4c0a8..58ee46d616925d50c678aa1a0030b5f5d4fa4fa4 100644 (file)
@@ -46,3 +46,46 @@ class Task_MOVE(Task):
     def do(self):
         self.thing.position = self.thing.world.map_.move(self.thing.position,
                                                          self.args[0])
+        for id_ in self.thing.inventory:
+            t = self.thing.world.get_thing(id_)
+            t.position[:] = self.thing.position
+
+
+
+class Task_PICKUP(Task):
+    argtypes = 'int:nonneg'
+
+    def check(self):
+        to_pick_up = self.thing.world.get_thing(self.args[0],
+                                                create_unfound=False)
+        if to_pick_up is None:
+            raise GameError('no thing of ID %s to pick up' % self.args[0])
+        if not (self.thing.position == to_pick_up.position or
+                tuple(to_pick_up.position) in
+                self.thing.world.map_.get_neighbors(self.thing.position)):
+            raise GameError('thing of ID %s not in reach to pick up'
+                            % self.args[0])
+
+    def do(self):
+        to_pick_up = self.thing.world.get_thing(self.args[0])
+        self.thing.inventory += [self.args[0]]
+        to_pick_up.in_inventory = True
+
+
+
+class Task_DROP(Task):
+    argtypes = 'int:nonneg'
+
+    def check(self):
+        to_pick_up = self.thing.world.get_thing(self.args[0],
+                                                create_unfound=False)
+        if to_pick_up is None:
+            raise GameError('no thing of ID %s to drop' % self.args[0])
+        if to_pick_up.id_ not in self.thing.inventory:
+            raise GameError('no thing of ID %s to drop in inventory'
+                            % 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
index 243547b2ffb854b330fcb998a27882d8ecda2ef9..5ef429754c41373dbf9a9b986de63ef88e684303 100644 (file)
@@ -17,6 +17,11 @@ class ThingBase:
 
 class Thing(ThingBase):
     blocking = False
+    in_inventory = False
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.inventory = []
 
     def proceed(self):
         pass
@@ -140,7 +145,7 @@ class ThingAnimate(Thing):
         stencil = self.get_stencil()
         visible_things = []
         for thing in self.world.things:
-            if stencil[thing.position] == '.':
+            if (not thing.in_inventory) and stencil[thing.position] == '.':
                 visible_things += [thing]
         return visible_things