From 1bb87f7a1151a6f244974c99cd7c392eeb0a8a35 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Sun, 6 Dec 2020 21:55:00 +0100 Subject: [PATCH 1/1] Enable selecting specific thing for pick-up. --- config.json | 2 +- plomrogue/tasks.py | 25 ++++++++-------- rogue_chat.html | 69 ++++++++++++++++++++++++++++++++------------ rogue_chat_curses.py | 48 +++++++++++++++++++++++------- 4 files changed, 101 insertions(+), 43 deletions(-) diff --git a/config.json b/config.json index 81d6c11..a7a51ad 100644 --- a/config.json +++ b/config.json @@ -14,7 +14,7 @@ "switch_to_control_tile_type": "Q", "switch_to_admin_thing_protect": "T", "flatten": "F", - "take_thing": "z", + "switch_to_take_thing": "z", "drop_thing": "u", "teleport": "p", "door": "D", diff --git a/plomrogue/tasks.py b/plomrogue/tasks.py index 8fa3a17..1050023 100644 --- a/plomrogue/tasks.py +++ b/plomrogue/tasks.py @@ -76,24 +76,25 @@ class Task_FLATTEN_SURROUNDINGS(Task): class Task_PICK_UP(Task): + argtypes = 'int:nonneg' def check(self): if self.thing.carrying: raise PlayError('already carrying something') - nothing_to_pick_up = True - for t in [t for t in self.thing.game.things - if t.portable - and 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') + to_pick_up = self.thing.game.get_thing(self.args[0]) + if to_pick_up is None: + raise PlayError('no thing of ID %s exists %s' % self.args[0]) + elif to_pick_up == self.thing: + raise PlayError('cannot pick up oneself') + elif to_pick_up.type_ == 'Player': + raise PlayError('cannot pick up player') + elif to_pick_up.position != self.thing.position: + raise PlayError('thing of ID %s not in reach' % self.args[0]) + elif not to_pick_up.portable: + raise PlayError('thing of ID %s not portable' % self.args[0]) def do(self): - to_pick_up = [t for t in self.thing.game.things - if t.portable - and t != self.thing and t.position == self.thing.position][0] + to_pick_up = self.thing.game.get_thing(self.args[0]) self.thing.carrying = to_pick_up diff --git a/rogue_chat.html b/rogue_chat.html index ef87c0f..2b104a3 100644 --- a/rogue_chat.html +++ b/rogue_chat.html @@ -51,7 +51,7 @@ keyboard input/control: - + @@ -96,10 +96,10 @@ keyboard input/control:
  • help:
  • flatten surroundings:
  • teleport: -
  • pick up thing:
  • drop thing:
  • open/close:
  • consume: +
  • @@ -143,6 +143,10 @@ let mode_helps = { 'short': 'command thing', 'long': 'Enter a command to the thing you carry. Enter nothing to return to play mode.' }, + 'take_thing': { + 'short': 'take thing', + 'long': 'You see a list of things which you could pick up. Enter the target thing\'s index, or, to leave, nothing.' + }, 'admin_thing_protect': { 'short': 'change thing protection', 'long': 'Change protection character for thing here.' @@ -208,7 +212,6 @@ let key_descriptions = { 'help': 'help', 'flatten': 'flatten surroundings', 'teleport': 'teleport', - 'take_thing': 'pick up thing', 'drop_thing': 'drop thing', 'door': 'open/close', 'consume': 'consume', @@ -461,6 +464,7 @@ let server = { game.tasks = tokens[1].split(','); tui.mode_write.legal = game.tasks.includes('WRITE'); tui.mode_command_thing.legal = game.tasks.includes('WRITE'); + tui.mode_take_thing.legal = game.tasks.includes('PICK_UP'); } else if (tokens[0] === 'THING_TYPE') { game.thing_types[tokens[1]] = tokens[2] } else if (tokens[0] === 'TERRAIN') { @@ -621,6 +625,7 @@ let tui = { mode_password: new Mode('password', true), mode_name_thing: new Mode('name_thing', true, true), mode_command_thing: new Mode('command_thing', true), + mode_take_thing: new Mode('take_thing', true), mode_admin_enter: new Mode('admin_enter', true), mode_admin: new Mode('admin'), mode_control_pw_pw: new Mode('control_pw_pw', true), @@ -640,8 +645,8 @@ let tui = { init: function() { this.mode_chat.available_modes = ["play", "study", "edit", "admin_enter"] this.mode_play.available_modes = ["chat", "study", "edit", "admin_enter", - "command_thing"] - this.mode_play.available_actions = ["move", "take_thing", "drop_thing", + "command_thing", "take_thing"] + this.mode_play.available_actions = ["move", "drop_thing", "teleport", "door", "consume"]; this.mode_study.available_modes = ["chat", "play", "admin_enter", "edit"] this.mode_study.available_actions = ["toggle_map_mode", "move_explorer"]; @@ -770,6 +775,25 @@ let tui = { } } else if (this.mode.is_single_char_entry) { this.show_help = true; + } else if (this.mode.name == 'take_thing') { + this.log_msg("selectable things:"); + const player = game.things[game.player_id]; + let selectables = []; + for (const t_id in game.things) { + const t = game.things[t_id]; + if (t.position[0] == player.position[0] + && t.position[1] == player.position[1] + && t != player && t.type_ != 'Player') { + selectables.push([t_id, t]); + } + }; + if (selectables.length == 0) { + this.log_msg('none') + } else { + for (const t of selectables) { + this.log_msg(t[0] + ' ' + explorer.get_thing_info(t[1])); + } + } } else if (this.mode.name == 'command_thing') { server.send(['TASK:COMMAND', 'HELP']); } else if (this.mode.name == 'admin_enter') { @@ -1271,18 +1295,11 @@ let explorer = { for (let t_id in game.things) { let t = game.things[t_id]; if (t.position[0] == this.position[0] && t.position[1] == this.position[1]) { - let symbol = game.thing_types[t.type_]; + info_to_cache += "THING: " + this.get_thing_info(t); let protection = t.protection; if (protection == '.') { protection = 'none'; } - info_to_cache += "THING: " + t.type_ + " / " + symbol; - if (t.thing_char) { - info_to_cache += t.thing_char; - }; - if (t.name_) { - info_to_cache += " (" + t.name_ + ")"; - } info_to_cache += " / protection: " + protection + "\n"; } } @@ -1296,6 +1313,17 @@ let explorer = { this.info_cached = info_to_cache; return this.info_cached; }, + get_thing_info: function(t) { + const symbol = game.thing_types[t.type_]; + let info = t.type_ + " / " + symbol; + if (t.thing_char) { + info += t.thing_char; + }; + if (t.name_) { + info += " (" + t.name_ + ")"; + } + return info; + }, annotate: function(msg) { if (msg.length == 0) { msg = " "; // triggers annotation deletion @@ -1352,6 +1380,14 @@ tui.inputEl.addEventListener('keydown', (event) => { server.send(['TASK:COMMAND', tui.inputEl.value]); tui.inputEl.value = ""; } + } else if (tui.mode.name == 'take_thing' && event.key == 'Enter') { + if (tui.inputEl.value.length == 0) { + tui.log_msg('@ aborted'); + } else { + server.send(['TASK:PICK_UP', tui.inputEl.value]); + } + tui.inputEl.value = ""; + tui.switch_mode('play'); } else if (tui.mode.name == 'control_pw_pw' && event.key == 'Enter') { if (tui.inputEl.value.length == 0) { tui.log_msg('@ aborted'); @@ -1438,8 +1474,6 @@ tui.inputEl.addEventListener('keydown', (event) => { } else if (tui.mode.name == 'play') { if (tui.mode.mode_switch_on_key(event)) { null; - } else if (event.key === tui.keys.take_thing && tui.task_action_on('take_thing')) { - server.send(["TASK:PICK_UP"]); } else if (event.key === tui.keys.drop_thing && tui.task_action_on('drop_thing')) { server.send(["TASK:DROP"]); } else if (event.key === tui.keys.consume && tui.task_action_on('consume')) { @@ -1553,11 +1587,8 @@ document.getElementById("toggle_map_mode").onclick = function() { tui.toggle_map_mode(); tui.full_refresh(); }; -document.getElementById("take_thing").onclick = function() { - server.send(['TASK:PICK_UP']); -}; document.getElementById("drop_thing").onclick = function() { - server.send(['TASK:DROP']); + server.send(['TASK:DROP']); }; document.getElementById("flatten").onclick = function() { server.send(['TASK:FLATTEN_SURROUNDINGS', tui.password]); diff --git a/rogue_chat_curses.py b/rogue_chat_curses.py index be3c53f..b098e04 100755 --- a/rogue_chat_curses.py +++ b/rogue_chat_curses.py @@ -31,6 +31,10 @@ mode_helps = { 'short': 'command thing', 'long': 'Enter a command to the thing you carry. Enter nothing to return to play mode.' }, + 'take_thing': { + 'short': 'take thing', + 'long': 'You see a list of things which you could pick up. Enter the target thing\'s index, or, to leave, nothing.' + }, 'admin_thing_protect': { 'short': 'change thing protection', 'long': 'Change protection character for thing here.' @@ -259,6 +263,7 @@ def cmd_TASKS(game, tasks_comma_separated): game.tasks = tasks_comma_separated.split(',') game.tui.mode_write.legal = 'WRITE' in game.tasks game.tui.mode_command_thing.legal = 'COMMAND' in game.tasks + game.tui.mode_take_thing.legal = 'PICK_UP' in game.tasks cmd_TASKS.argtypes = 'string' def cmd_THING_TYPE(game, thing_type, symbol_hint): @@ -390,6 +395,7 @@ class TUI: mode_password = Mode('password', has_input_prompt=True) mode_name_thing = Mode('name_thing', has_input_prompt=True, shows_info=True) mode_command_thing = Mode('command_thing', has_input_prompt=True) + mode_take_thing = Mode('take_thing', has_input_prompt=True) is_admin = False tile_draw = False @@ -397,8 +403,8 @@ class TUI: import os import json self.mode_play.available_modes = ["chat", "study", "edit", "admin_enter", - "command_thing"] - self.mode_play.available_actions = ["move", "take_thing", "drop_thing", + "command_thing", "take_thing"] + self.mode_play.available_actions = ["move", "drop_thing", "teleport", "door", "consume"] self.mode_study.available_modes = ["chat", "play", "admin_enter", "edit"] self.mode_study.available_actions = ["toggle_map_mode", "move_explorer"] @@ -441,7 +447,7 @@ class TUI: 'switch_to_control_tile_type': 'Q', 'switch_to_admin_thing_protect': 'T', 'flatten': 'F', - 'take_thing': 'z', + 'switch_to_take_thing': 'z', 'drop_thing': 'u', 'teleport': 'p', 'consume': 'C', @@ -594,6 +600,17 @@ class TUI: self.send('LOGIN ' + quote(self.login_name)) else: self.log_msg('@ enter username') + elif self.mode.name == 'take_thing': + self.log_msg('selectable things:') + player = self.game.get_thing(self.game.player_id) + selectables = [t for t in self.game.things + if t != player and t.type_ != 'Player' + and t.position == player.position] + if len(selectables) == 0: + self.log_msg('none') + else: + for t in selectables: + self.log_msg(str(t.id_) + ' ' + self.get_thing_info(t)) elif self.mode.name == 'command_thing': self.send('TASK:COMMAND ' + quote('HELP')) elif self.mode.name == 'admin_enter': @@ -646,15 +663,10 @@ class TUI: info_to_cache += 'PROTECTION: %s\n' % protection for t in self.game.things: if t.position == self.explorer: + info_to_cache += 'THING: %s' % self.get_thing_info(t) protection = t.protection if protection == '.': protection = 'none' - info_to_cache += 'THING: %s / %s' %\ - (t.type_, self.game.thing_types[t.type_]) - if hasattr(t, 'thing_char'): - info_to_cache += t.thing_char - if hasattr(t, 'name'): - info_to_cache += ' (%s)' % t.name info_to_cache += ' / protection: %s\n' % protection if self.explorer in self.game.portals: info_to_cache += 'PORTAL: ' +\ @@ -667,6 +679,15 @@ class TUI: self.info_cached = info_to_cache return self.info_cached + def get_thing_info(self, t): + info = '%s / %s' %\ + (t.type_, self.game.thing_types[t.type_]) + if hasattr(t, 'thing_char'): + info += t.thing_char + if hasattr(t, 'name'): + info += ' (%s)' % t.name + return info + def loop(self, stdscr): import datetime @@ -974,6 +995,13 @@ class TUI: self.login_name = self.input_ self.send('LOGIN ' + quote(self.input_)) self.input_ = "" + elif self.mode.name == 'take_thing' and key == '\n': + if self.input_ == '': + self.log_msg('@ aborted') + else: + self.send('TASK:PICK_UP ' + quote(self.input_)) + self.input_ = '' + self.switch_mode('play') elif self.mode.name == 'command_thing' and key == '\n': if self.input_ == '': self.log_msg('@ aborted') @@ -1070,8 +1098,6 @@ class TUI: elif self.mode.name == 'play': if self.mode.mode_switch_on_key(self, key): continue - elif key == self.keys['take_thing'] and task_action_on('take_thing'): - self.send('TASK:PICK_UP') elif key == self.keys['drop_thing'] and task_action_on('drop_thing'): self.send('TASK:DROP') elif key == self.keys['door'] and task_action_on('door'): -- 2.30.2