From 7abc6dcdbe60dce9b8efad07917fb274da06687a Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Sat, 14 Nov 2020 00:28:36 +0100
Subject: [PATCH] Add item picking and dropping.

---
 config.json                         |  2 ++
 plomrogue/tasks.py                  | 38 ++++++++++++++++++++++++++++-
 plomrogue/things.py                 |  1 +
 rogue_chat.py                       |  6 +++--
 rogue_chat_curses.py                | 10 ++++++++
 rogue_chat_nocanvas_monochrome.html | 14 +++++++++++
 6 files changed, 68 insertions(+), 3 deletions(-)

diff --git a/config.json b/config.json
index d1682c3..5bce774 100644
--- a/config.json
+++ b/config.json
@@ -6,6 +6,8 @@
     "switch_to_study": "?",
     "switch_to_edit": "m",
     "flatten": "F",
+    "take_thing": "z",
+    "drop_thing": "u",
     "help": "h",
     "toggle_map_mode": "M",
     "hex_move_upleft": "w",
diff --git a/plomrogue/tasks.py b/plomrogue/tasks.py
index e73b174..6d1a32a 100644
--- a/plomrogue/tasks.py
+++ b/plomrogue/tasks.py
@@ -20,7 +20,7 @@ class Task_WAIT(Task):
     todo = 1
 
     def do(self):
-        return 'success'
+        pass
 
 
 
@@ -44,6 +44,8 @@ class Task_MOVE(Task):
 
     def do(self):
         self.thing.position = self.get_move_target()
+        if self.thing.carrying:
+            self.thing.carrying.position = self.thing.position
 
 
 
@@ -75,3 +77,37 @@ class Task_FLATTEN_SURROUNDINGS(Task):
                 if not self.thing.game.can_do_tile_with_pw(yx, self.args[0]):
                     continue
                 self.thing.game.map[yx] = '.'
+
+
+
+class Task_PICK_UP(Task):
+    todo = 1
+
+    def check(self):
+        if self.thing.carrying:
+            raise PlayError('already carrying')
+        nothing_to_pick_up = True
+        for t in [t for t in self.thing.game.things
+                  if t != self.thing and t.position == self.thing.position
+                  and t.type_ != 'Player']:
+            nothing_to_pick_up = False
+            break
+        if nothing_to_pick_up:
+            raise PlayError('nothing to pick up')
+
+    def do(self):
+        to_pick_up = [t for t in self.thing.game.things
+                      if t != self.thing and t.position == self.thing.position][0]
+        self.thing.carrying = to_pick_up
+
+
+
+class Task_DROP(Task):
+    todo = 1
+
+    def check(self):
+        if not self.thing.carrying:
+            raise PlayError('nothing to drop')
+
+    def do(self):
+        self.thing.carrying = None
diff --git a/plomrogue/things.py b/plomrogue/things.py
index d1fc711..b6fe5e1 100644
--- a/plomrogue/things.py
+++ b/plomrogue/things.py
@@ -108,3 +108,4 @@ class Thing_Player(ThingAnimate):
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
         self.nickname = 'undefined'
+        self.carrying = None
diff --git a/rogue_chat.py b/rogue_chat.py
index e25eeac..e15ee24 100755
--- a/rogue_chat.py
+++ b/rogue_chat.py
@@ -7,8 +7,8 @@ from plomrogue.commands import (cmd_ALL, cmd_LOGIN, cmd_NICK, cmd_PING, cmd_THIN
                                 cmd_ANNOTATE, cmd_PORTAL, cmd_GET_GAMESTATE,
                                 cmd_TASKS, cmd_MAP_CONTROL_LINE, cmd_MAP_CONTROL_PW,
                                 cmd_GOD_ANNOTATE, cmd_GOD_PORTAL, cmd_THING_TYPES)
-from plomrogue.tasks import (Task_WAIT, Task_MOVE, Task_WRITE,
-                             Task_FLATTEN_SURROUNDINGS)
+from plomrogue.tasks import (Task_WAIT, Task_MOVE, Task_WRITE, Task_PICK_UP,
+                             Task_DROP, Task_FLATTEN_SURROUNDINGS)
 from plomrogue.things import Thing_Player, Thing_Stone
 import sys
 
@@ -39,6 +39,8 @@ game.register_task(Task_WAIT)
 game.register_task(Task_MOVE)
 game.register_task(Task_WRITE)
 game.register_task(Task_FLATTEN_SURROUNDINGS)
+game.register_task(Task_PICK_UP)
+game.register_task(Task_DROP)
 game.register_thing_type(Thing_Player)
 game.register_thing_type(Thing_Stone)
 game.read_savefile()
diff --git a/rogue_chat_curses.py b/rogue_chat_curses.py
index 0a5ced7..f69153f 100755
--- a/rogue_chat_curses.py
+++ b/rogue_chat_curses.py
@@ -257,6 +257,8 @@ class TUI:
             'switch_to_study': '?',
             'switch_to_edit': 'm',
             'flatten': 'F',
+            'take_thing': 'z',
+            'drop_thing': 'u',
             'toggle_map_mode': 'M',
             'hex_move_upleft': 'w',
             'hex_move_upright': 'e',
@@ -533,6 +535,10 @@ class TUI:
                 content += "Available actions:\n"
                 if 'MOVE' in self.game.tasks:
                     content += "[%s] – move player\n" % ','.join(self.movement_keys)
+                if 'PICK_UP' in self.game.tasks:
+                    content += "[%s] – take thing under player\n" % self.keys['take_thing']
+                if 'DROP' in self.game.tasks:
+                    content += "[%s] – drop carried thing\n" % self.keys['drop_thing']
                 if 'FLATTEN_SURROUNDINGS' in self.game.tasks:
                     content += "[%s] – flatten player's surroundings\n" % self.keys['flatten']
                 content += 'Other modes available from here:\n'
@@ -721,6 +727,10 @@ class TUI:
                 elif key == self.keys['flatten'] and\
                      'FLATTEN_SURROUNDINGS' in self.game.tasks:
                     self.send('TASK:FLATTEN_SURROUNDINGS ' + quote(self.password))
+                elif key == self.keys['take_thing'] and 'PICK_UP' in self.game.tasks:
+                    self.send('TASK:PICK_UP')
+                elif key == self.keys['drop_thing'] and 'DROP' in self.game.tasks:
+                    self.send('TASK:DROP')
                 elif key in self.movement_keys and 'MOVE' in self.game.tasks:
                     self.send('TASK:MOVE ' + self.movement_keys[key])
             elif self.mode == self.mode_edit:
diff --git a/rogue_chat_nocanvas_monochrome.html b/rogue_chat_nocanvas_monochrome.html
index d9732da..f978421 100644
--- a/rogue_chat_nocanvas_monochrome.html
+++ b/rogue_chat_nocanvas_monochrome.html
@@ -23,6 +23,8 @@ move down-left (hex grid): <input id="key_hex_move_downleft" type="text" value="
 move left (hex grid): <input id="key_hex_move_left" type="text" value="a" /><br />
 help: <input id="key_help" type="text" value="h" /><br />
 flatten surroundings: <input id="key_flatten" type="text" value="F" /><br />
+take thing under player: <input id="key_take_thing" type="text" value="z" /><br />
+drop carried thing: <input id="key_drop_thing" type="text" value="u" /><br />
 switch to chat mode: <input id="key_switch_to_chat" type="text" value="t" /><br />
 switch to play mode: <input id="key_switch_to_play" type="text" value="p" /><br />
 switch to study mode: <input id="key_switch_to_study" type="text" value="?" /><br />
@@ -537,6 +539,12 @@ let tui = {
           if (game.tasks.includes('MOVE')) {
               content += "[" + movement_keys_desc + "] – move player\n";
           }
+          if (game.tasks.includes('PICK_UP')) {
+              content += "[" + this.keys.take_thing + "] – take thing under player\n";
+          }
+          if (game.tasks.includes('DROP')) {
+              content += "[" + this.keys.drop_thing + "] – drop carried thing\n";
+          }
           if (game.tasks.includes('FLATTEN_SURROUNDINGS')) {
               content += "[" + tui.keys.flatten + "] – flatten player's surroundings\n";
           }
@@ -826,6 +834,12 @@ tui.inputEl.addEventListener('keydown', (event) => {
           } else if (event.key === tui.keys.flatten
                      && game.tasks.includes('FLATTEN_SURROUNDINGS')) {
               server.send(["TASK:FLATTEN_SURROUNDINGS", tui.password]);
+          } else if (event.key === tui.keys.take_thing
+                     && game.tasks.includes('PICK_UP')) {
+              server.send(["TASK:PICK_UP"]);
+          } else if (event.key === tui.keys.drop_thing
+                     && game.tasks.includes('DROP')) {
+              server.send(["TASK:DROP"]);
           } else if (event.key in tui.movement_keys
                      && game.tasks.includes('MOVE')) {
               server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
-- 
2.30.2