void
Console::parse(std::string s)
{
- std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(s);
+ // split line into list of args
+ std::vector<std::string> 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<std::string, std::list<ConsoleCommandReceiver*> >::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
#include <list>
#include <map>
+#include <vector>
#include <string>
#include <sstream>
#include <iostream>
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<typename T> static bool string_is(std::string s) {
+ std::istringstream iss(s);
+ T i;
+ if ((iss >> i) && iss.eof()) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ template<typename T> 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<std::string> lines; /**< backbuffer of lines sent to the console */
{
//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<std::string> arguments) = 0; /**< callback from Console; return false if command was unknown, true otherwise */
virtual ~ConsoleCommandReceiver()
{
//Console::unregisterCommandReceiver(this);
"grow",
"fire",
"ice",
- "lifeup",
+ "coins",
"numberofthebeast",
- "lifedown",
"grease",
"invincible",
"mortal",
}
bool
-GameSession::consoleCommand(std::string command)
+GameSession::consoleCommand(std::string command, std::vector<std::string> arguments)
{
if (command == "foo") {
msg_info << "bar" << std::endl;
tux.set_bonus(ICE_BONUS, false);
return true;
}
- if (command == "lifeup") {
- player_status->incLives();
+ if (command == "coins") {
+ if ((arguments.size() < 1) || (!Console::string_is<int>(arguments[0]))) {
+ msg_info << "Usage: coins <number>" << std::endl;
+ } else {
+ player_status->coins = Console::string_to<int>(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;
* 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<std::string> arguments); /**< callback from Console; return false if command was unknown, true otherwise */
private:
void restart_level(bool fromBeginning = true);