From b66e5302aff3691947ae72b7ad783f6c79f015a9 Mon Sep 17 00:00:00 2001 From: Sverre Rabbelier Date: Mon, 14 Apr 2008 23:48:22 +0200 Subject: [PATCH] Cosmetic improvements with EditorMovement (and removed the #if 0 ... #endif-ed stuff). --- src/Core/Editors/EditorMovement.cpp | 423 +++++++++++------------------------- src/Core/Editors/EditorMovement.h | 6 +- 2 files changed, 128 insertions(+), 301 deletions(-) rewrite src/Core/Editors/EditorMovement.cpp (68%) diff --git a/src/Core/Editors/EditorMovement.cpp b/src/Core/Editors/EditorMovement.cpp dissimilarity index 68% index 03e2455..eaad812 100644 --- a/src/Core/Editors/EditorMovement.cpp +++ b/src/Core/Editors/EditorMovement.cpp @@ -1,297 +1,126 @@ -/*************************************************************************** - * Copyright (C) 2008 by Sverre Rabbelier * - * sverre@rabbelier.nl * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ - -#include "EditorAccount.h" -#include "EditorMovement.h" -#include "UBSocket.h" -#include "Array.h" -#include "Assert.h" -#include "StringUtilities.h" -#include "Account.h" -#include "Character.h" -#include "PCharacter.h" -#include "PCharacterManager.h" -#include "Chunk.h" -#include "ChunkManager.h" -#include "Managers.h" -#include "DirectionParser.h" -#include "Exit.h" -#include "ExitManager.h" - -using mud::PCharacter; - -EditorMovement::EditorMovement(UBSocket* sock, mud::PCharacterPtr character) : -Editor(sock), -m_char(character) -{ - Assert(character); -} - -EditorMovement::EditorMovement(UBSocket* sock, mud::PCharacterPtr character, cstring line) : -Editor(sock), -m_char(character), -m_line(line) -{ - Assert(character); -} - -EditorMovement::~EditorMovement(void) -{ - -} - -void EditorMovement::OnLine(const std::string& str) -{ - if(str.empty()) - { - m_sock->Send("Please specify where you want to move to.\n"); - return; - } - - DirectionParser p(str); - Coordinates result = p.getResult(); - - if(!result.size()) - { - OnLine(Global::Get()->EmptyString); - m_sock->Sendf("'%s' is not a valid direction.\n"); - return; - } - - int i = 1; - for(Coordinates::const_iterator it = result.begin(); it != result.end(); it++) - { - Coordinate coordinate = *it; - if(!coordinate.isDirection()) - { - m_sock->Sendf("Your %dth direction is not a direction.\n", i); - m_sock->Send("Please consult the helpfile on directions.\n"); - return; - } - - i++; - } - - for(Coordinates::const_iterator it = result.begin(); it != result.end(); it++) - { - Coordinate coordinate = *it; - - value_type chunkid = m_char->getChunk(); - mud::ChunkPtr chunk = mud::Managers::Get()->Chunk->GetByKey(chunkid); - Assert(chunk); - - value_type exitid = chunk->getExitAt(coordinate); - if(!exitid) - { - m_sock->Sendf("There is no exit to the %s.\n", coordinate.toDirectionString().c_str()); - return; - } - - mud::ExitPtr exit = mud::Managers::Get()->Exit->GetByKey(exitid); - Assert(exit->getFromChunk() == chunkid); - - value_type tochunkid = exit->getToChunk(); - Assert(tochunkid); - - m_char->MoveToChunk(tochunkid); - } -} - -void EditorMovement::OnFocus() -{ - OnLine(m_line); - m_sock->PopEditor(); -} - -void EditorMovement::listCommands(const std::string& argument) -{ - // TODO implement - - return; -} - -void EditorMovement::quitEditor(const std::string& argument) -{ - m_sock->Send("Ok.\n"); - m_sock->PopEditor(); - return; -} - -#if 0 -void EditorRoom::North::Run(EditorRoom* editor, const std::string&) -{ - if(editor->m_ypos <= 1) - { - editor->m_m_sock->Send("You can't go further north!\n"); - return; - } - editor->m_ypos--; - editor->getRoom(Global::Get()->EmptyString); - editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor-> m_ypos)); -} - -void EditorRoom::NorthEast::Run(EditorRoom* editor, const std::string& argument) -{ - long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->AREAS, editor->m_cluster); - if(count <= 0) { - Global::Get()->bug("EditorMap::NorthEast::Run() with clustercount <= 0!\n"); - editor->m_m_sock->Send("For some reason the cluster number of the room you are belongs to, does not correspond with anything in the database?!\n"); - return; - } - if(count >= 2) { - Global::Get()->bugf("EditorMap::NorthEast::Run() with clustercount '%d'!\n", count); - editor->m_m_sock->Sendf("For some reason the cluster the room you are in exists multiple times in the database?!n"); - return; - } - Cluster* cluster = Cache::Get()->GetCluster(editor->m_cluster); - if(editor->m_ypos <= 1 || editor->m_xpos+1 > cluster->getWidth()) - { - editor->m_m_sock->Send("You can't go further northeast!\n"); - return; - } - editor->m_ypos--; - editor->m_xpos++; - editor->getRoom(Global::Get()->EmptyString); - editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos)); -} - -void EditorRoom::East::Run(EditorRoom* editor, const std::string& argument) -{ - long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->AREAS, editor->m_cluster); - if(count <= 0) { - Global::Get()->bug("EditorMap::East::Run() with clustercount <= 0!\n"); - editor->m_m_sock->Send("For some reason the cluster number of the room you are belongs to, does not correspond with anything in the database?!\n"); - return; - } - if(count >= 2) { - Global::Get()->bugf("EditorMap::East::Run() with clustercount '%d'!\n", count); - editor->m_m_sock->Sendf("For some reason the cluster the room you are in exists multiple times in the database?!n"); - return; - } - Cluster* cluster = Cache::Get()->GetCluster(editor->m_cluster); - if(editor->m_xpos+1 > cluster->getWidth()) - { - editor->m_m_sock->Send("You can't go further east!\n"); - return; - } - editor->m_xpos++; - editor->getRoom(Global::Get()->EmptyString); - editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos)); -} - -void EditorRoom::SouthEast::Run(EditorRoom* editor, const std::string& argument) -{ - long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->AREAS, editor->m_cluster); - if(count <= 0) { - Global::Get()->bug("EditorMap::SouthEast::Run() with clustercount <= 0!\n"); - editor->m_m_sock->Send("For some reason the cluster number of the room you are belongs to, does not correspond with anything in the database?!\n"); - return; - } - if(count >= 2) { - Global::Get()->bugf("EditorMap::SouthEast::Run() with clustercount '%d'!\n", count); - editor->m_m_sock->Sendf("For some reason the cluster the room you are in exists multiple times in the database?!n"); - return; - } - Cluster* cluster = Cache::Get()->GetCluster(editor->m_cluster); - if(editor->m_ypos+1 > cluster->getHeight() || editor->m_xpos+1 > cluster->getWidth()) - { - editor->m_m_sock->Send("You can't go further southeast!\n"); - return; - } - editor->m_ypos++; - editor->m_xpos++; - editor->getRoom(Global::Get()->EmptyString); - editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos)); -} - -void EditorRoom::South::Run(EditorRoom* editor, const std::string& argument) -{ - long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->AREAS, editor->m_cluster); - if(count <= 0) { - Global::Get()->bug("EditorMap::South::Run() with clustercount <= 0!\n"); - editor->m_m_sock->Send("For some reason the cluster number of the room you are belongs to, does not correspond with anything in the database?!\n"); - return; - } - if(count >= 2) { - Global::Get()->bugf("EditorMap::South::Run() with clustercount '%d'!\n", count); - editor->m_m_sock->Sendf("For some reason the cluster the room you are in exists multiple times in the database?!n"); - return; - } - Cluster* cluster = Cache::Get()->GetCluster(editor->m_cluster); - if(editor->m_ypos+1 > cluster->getHeight()) - { - editor->m_m_sock->Send("You can't go further south!\n"); - return; - } - editor->m_ypos++; - editor->getRoom(Global::Get()->EmptyString); - editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos)); -} - -void EditorRoom::SouthWest::Run(EditorRoom* editor, const std::string& argument) -{ - long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->AREAS, editor->m_cluster); - if(count <= 0) { - Global::Get()->bug("EditorMap::SouthWest::Run() with clustercount <= 0!\n"); - editor->m_m_sock->Send("For some reason the cluster number of the room you are belongs to, does not correspond with anything in the database?!\n"); - return; - } - if(count >= 2) { - Global::Get()->bugf("EditorMap::SouthWest::Run() with clustercount '%d'!\n", count); - editor->m_m_sock->Sendf("For some reason the cluster the room you are in exists multiple times in the database?!n"); - return; - } - Cluster* cluster = Cache::Get()->GetCluster(editor->m_cluster); - if(editor->m_xpos <= 1 || editor->m_ypos+1 > cluster->getHeight()) - { - editor->m_m_sock->Send("You can't go further southwest!\n"); - return; - } - editor->m_xpos--; - editor->m_ypos++; - editor->getRoom(Global::Get()->EmptyString); - editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos)); -} - -void EditorRoom::West::Run(EditorRoom* editor, const std::string& argument) -{ - if(editor->m_xpos <= 1) - { - editor->m_m_sock->Send("You can't go further west!\n"); - return; - } - editor->m_xpos--; - editor->getRoom(Global::Get()->EmptyString); - editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos)); -} - -void EditorRoom::NorthWest::Run(EditorRoom* editor, const std::string& argument) -{ - if(editor->m_ypos <= 1 || editor->m_xpos <= 1) - { - editor->m_m_sock->Send("You can't go further northwest!\n"); - return; - } - editor->m_xpos--; - editor->m_ypos--; - editor->getRoom(Global::Get()->EmptyString); - editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos)); -} -#endif +/*************************************************************************** + * Copyright (C) 2008 by Sverre Rabbelier * + * sverre@rabbelier.nl * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "EditorAccount.h" +#include "EditorMovement.h" +#include "UBSocket.h" +#include "Array.h" +#include "Assert.h" +#include "StringUtilities.h" +#include "Account.h" +#include "Character.h" +#include "PCharacter.h" +#include "PCharacterManager.h" +#include "Chunk.h" +#include "ChunkManager.h" +#include "Managers.h" +#include "DirectionParser.h" +#include "Exit.h" +#include "ExitManager.h" + +using mud::PCharacter; + +EditorMovement::EditorMovement(UBSocket* sock, mud::PCharacterPtr character) : +Editor(sock), +m_char(character) +{ + Assert(character); +} + +EditorMovement::EditorMovement(UBSocket* sock, mud::PCharacterPtr character, cstring line) : +Editor(sock), +m_char(character), +m_line(line) +{ + Assert(character); +} + +EditorMovement::~EditorMovement(void) +{ + +} + +void EditorMovement::OnLine(const std::string& str) +{ + DirectionParser p(str); + Coordinates result = p.getResult(); + + if(!result.size()) + { + m_sock->Sendf("'%s' is not a valid direction.\n", str.c_str()); + return; + } + + if(result.size() > 1) + { + m_sock->Send("Let's get movin':\n"); + + for(Coordinates::const_iterator it = result.begin(); it != result.end(); it++) + { + Coordinate coordinate = *it; + Assert(coordinate.isDirection()); + + m_sock->Send(coordinate.toDirectionString()); + m_sock->Send("\n"); + } + + m_sock->Send("\n"); + } + + for(Coordinates::const_iterator it = result.begin(); it != result.end(); it++) + { + Coordinate coordinate = *it; + + value_type chunkid = m_char->getChunk(); + mud::ChunkPtr chunk = mud::Managers::Get()->Chunk->GetByKey(chunkid); + Assert(chunk); + + value_type exitid = chunk->getExitAt(coordinate); + if(!exitid) + { + m_sock->Sendf("There is no exit to the %s.\n", coordinate.toDirectionString().c_str()); + return; + } + + mud::ExitPtr exit = mud::Managers::Get()->Exit->GetByKey(exitid); + Assert(exit->getFromChunk() == chunkid); + + value_type tochunkid = exit->getToChunk(); + Assert(tochunkid); + + m_char->MoveToChunk(tochunkid); + } +} + +void EditorMovement::OnFocus() +{ + if(m_line.size()) + { + OnLine(m_line); + m_sock->PopEditor(); + } +} + +void EditorMovement::OnEmptyLine() +{ + m_sock->Send("Ok.\n"); + m_sock->PopEditor(); + return; +} diff --git a/src/Core/Editors/EditorMovement.h b/src/Core/Editors/EditorMovement.h index 8cd0b1d..5876199 100644 --- a/src/Core/Editors/EditorMovement.h +++ b/src/Core/Editors/EditorMovement.h @@ -36,13 +36,11 @@ public: ~EditorMovement(void); std::string name() { return "Movement"; }; - std::string prompt() { return "> "; }; + std::string prompt() { return "go: "; }; void OnLine(const std::string& line); void OnFocus(); - - void listCommands(const std::string& argument); - void quitEditor(const std::string& argument); + void OnEmptyLine(); private: mud::PCharacterPtr m_char; // current active PCharacter -- 2.11.4.GIT