From 70291cc1f49340fc07bf1e3fec8f604c52e781ac Mon Sep 17 00:00:00 2001 From: Christoph Sommer Date: Sun, 9 Apr 2006 13:32:19 +0000 Subject: [PATCH] Console commands can pass arguments SVN-Revision: 3278 --- src/console.cpp | 22 +++++++++++++++++++--- src/console.hpp | 24 +++++++++++++++++++++++- src/game_session.cpp | 17 ++++++++--------- src/game_session.hpp | 2 +- 4 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/console.cpp b/src/console.cpp index 32c115036..f3e022072 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -126,15 +126,31 @@ Console::addLine(std::string s) void Console::parse(std::string s) { - std::map >::iterator i = commands.find(s); + // split line into list of args + std::vector args; + size_t start = 0; + size_t end = 0; + while (1) { + start = s.find_first_not_of(" ,", end); + end = s.find_first_of(" ,", start); + if (start == s.npos) break; + args.push_back(s.substr(start, end-start)); + } + + // command is args[0] + std::string command = args.front(); + args.erase(args.begin()); + + // look up registered ccr + std::map >::iterator i = commands.find(command); if ((i == commands.end()) || (i->second.size() == 0)) { - addLine("unknown command: \"" + s + "\""); + addLine("unknown command: \"" + command + "\""); return; } // send command to the most recently registered ccr ConsoleCommandReceiver* ccr = i->second.front(); - if (ccr->consoleCommand(s) != true) msg_warning << "Sent command to registered ccr, but command was unhandled" << std::endl; + if (ccr->consoleCommand(command, args) != true) msg_warning << "Sent command to registered ccr, but command was unhandled" << std::endl; } bool diff --git a/src/console.hpp b/src/console.hpp index 98571ab23..445c91245 100644 --- a/src/console.hpp +++ b/src/console.hpp @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -53,6 +54,27 @@ class Console static bool hasFocus(); /**< true if characters should be sent to the console instead of their normal target */ static void registerCommand(std::string command, ConsoleCommandReceiver* ccr); /**< associate command with the given CCR */ static void unregisterCommand(std::string command, ConsoleCommandReceiver* ccr); /**< dissociate command and CCR */ + static void unregisterCommands(ConsoleCommandReceiver* ccr); /**< dissociate all commands of given CCR */ + + template static bool string_is(std::string s) { + std::istringstream iss(s); + T i; + if ((iss >> i) && iss.eof()) { + return true; + } else { + return false; + } + } + + template static T string_to(std::string s) { + std::istringstream iss(s); + T i; + if ((iss >> i) && iss.eof()) { + return i; + } else { + return T(); + } + } protected: static std::list lines; /**< backbuffer of lines sent to the console */ @@ -91,7 +113,7 @@ class ConsoleCommandReceiver { //Console::registerCommandReceiver(this); } - virtual bool consoleCommand(std::string command) = 0; /**< callback from Console; return false if command was unknown, true otherwise */ + virtual bool consoleCommand(std::string command, std::vector arguments) = 0; /**< callback from Console; return false if command was unknown, true otherwise */ virtual ~ConsoleCommandReceiver() { //Console::unregisterCommandReceiver(this); diff --git a/src/game_session.cpp b/src/game_session.cpp index 35c482969..b502668c6 100644 --- a/src/game_session.cpp +++ b/src/game_session.cpp @@ -83,9 +83,8 @@ namespace { "grow", "fire", "ice", - "lifeup", + "coins", "numberofthebeast", - "lifedown", "grease", "invincible", "mortal", @@ -348,7 +347,7 @@ GameSession::process_events() } bool -GameSession::consoleCommand(std::string command) +GameSession::consoleCommand(std::string command, std::vector arguments) { if (command == "foo") { msg_info << "bar" << std::endl; @@ -372,18 +371,18 @@ GameSession::consoleCommand(std::string command) tux.set_bonus(ICE_BONUS, false); return true; } - if (command == "lifeup") { - player_status->incLives(); + if (command == "coins") { + if ((arguments.size() < 1) || (!Console::string_is(arguments[0]))) { + msg_info << "Usage: coins " << std::endl; + } else { + player_status->coins = Console::string_to(arguments[0]); + } return true; } if (command == "numberofthebeast") { player_status->coins += 55; return true; } - if (command == "lifedown") { - player_status->coins = std::max(player_status->coins-25, 0); - return true; - } if (command == "grease") { tux.physic.set_velocity_x(tux.physic.get_velocity_x()*3); return true; diff --git a/src/game_session.hpp b/src/game_session.hpp index 57edaa4fe..3b14707c7 100644 --- a/src/game_session.hpp +++ b/src/game_session.hpp @@ -95,7 +95,7 @@ public: * resources for the current level/world */ std::string get_working_directory(); - bool consoleCommand(std::string command); /**< callback from Console; return false if command was unknown, true otherwise */ + bool consoleCommand(std::string command, std::vector arguments); /**< callback from Console; return false if command was unknown, true otherwise */ private: void restart_level(bool fromBeginning = true); -- 2.11.0