]> git.localhorst.tv Git - blank.git/blobdiff - src/shared/cli.cpp
less restrictive teleport command
[blank.git] / src / shared / cli.cpp
index aa6629f34741221e9cd45fe10ac71360333e58d3..bb4aa6de706e3259f25f374a9fdfe15d57808870 100644 (file)
@@ -1,4 +1,5 @@
 #include "CLI.hpp"
+#include "CLIContext.hpp"
 #include "commands.hpp"
 
 #include "../io/TokenStreamReader.hpp"
@@ -7,6 +8,7 @@
 
 #include <iostream>
 #include <sstream>
+#include <glm/gtx/io.hpp>
 
 using namespace std;
 
@@ -29,7 +31,7 @@ void CLI::AddCommand(const string &name, Command *cmd) {
        commands[name] = cmd;
 }
 
-void CLI::Execute(Player &player, const string &line) {
+void CLI::Execute(CLIContext &ctx, const string &line) {
        stringstream s(line);
        TokenStreamReader args(s);
        if (!args.HasMore()) {
@@ -37,48 +39,55 @@ void CLI::Execute(Player &player, const string &line) {
                return;
        }
        if (args.Peek().type != Token::IDENTIFIER) {
-               Error("I don't understand");
+               ctx.Error("I don't understand");
                return;
        }
        string name;
        args.ReadIdentifier(name);
        auto entry = commands.find(name);
        if (entry == commands.end()) {
-               Error(name + ": command not found");
+               ctx.Error(name + ": command not found");
                return;
        }
        try {
-               entry->second->Execute(*this, player, args);
+               entry->second->Execute(*this, ctx, args);
        } catch (exception &e) {
-               Error(name + ": " + e.what());
+               ctx.Error(name + ": " + e.what());
        } catch (...) {
-               Error(name + ": unknown execution error");
+               ctx.Error(name + ": unknown execution error");
        }
 }
 
-void CLI::Message(const string &msg) {
-       // TODO: display message to player
-       cout << msg << endl;
-}
+CLI::Command::~Command() {
 
-void CLI::Error(const string &msg) {
-       Message("CLI error: " + msg);
 }
 
-CLI::Command::~Command() {
 
+CLIContext::CLIContext(Player *p, Entity *e)
+: original_player(p)
+, effective_player(p)
+, original_entity(e)
+, effective_entity(e) {
+       if (!e && p) {
+               original_entity = effective_entity = &p->GetEntity();
+       }
 }
 
 
-void TeleportCommand::Execute(CLI &cli, Player &player, TokenStreamReader &args) {
+void TeleportCommand::Execute(CLI &, CLIContext &ctx, TokenStreamReader &args) {
+       if (!ctx.HasEntity()) {
+               ctx.Error("teleport needs entity to operate on");
+               return;
+       }
+
        glm::vec3 pos(args.GetFloat(), args.GetFloat(), args.GetFloat());
-       glm::ivec3 chunk(pos);
-       chunk /= Chunk::Extent();
-       pos -= chunk;
-       EntityState state = player.GetEntity().GetState();
-       state.chunk_pos = chunk;
-       state.block_pos = pos;
-       player.GetEntity().SetState(state);
+       EntityState state = ctx.GetEntity().GetState();
+       state.pos = ExactLocation(pos).Sanitize();
+       ctx.GetEntity().SetState(state);
+
+       stringstream msg;
+       msg << ctx.GetEntity().Name() << " teleported to " << pos;
+       ctx.Broadcast(msg.str());
 }
 
 }