Improved EditorMovement, it should be fully functional now.
[UnsignedByte.git] / src / Core / Editors / EditorMovement.cpp
blob03e2455ab556c55e1503522ab20da2daa1b78c3c
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "EditorAccount.h"
22 #include "EditorMovement.h"
23 #include "UBSocket.h"
24 #include "Array.h"
25 #include "Assert.h"
26 #include "StringUtilities.h"
27 #include "Account.h"
28 #include "Character.h"
29 #include "PCharacter.h"
30 #include "PCharacterManager.h"
31 #include "Chunk.h"
32 #include "ChunkManager.h"
33 #include "Managers.h"
34 #include "DirectionParser.h"
35 #include "Exit.h"
36 #include "ExitManager.h"
38 using mud::PCharacter;
40 EditorMovement::EditorMovement(UBSocket* sock, mud::PCharacterPtr character) :
41 Editor(sock),
42 m_char(character)
44 Assert(character);
47 EditorMovement::EditorMovement(UBSocket* sock, mud::PCharacterPtr character, cstring line) :
48 Editor(sock),
49 m_char(character),
50 m_line(line)
52 Assert(character);
55 EditorMovement::~EditorMovement(void)
60 void EditorMovement::OnLine(const std::string& str)
62 if(str.empty())
64 m_sock->Send("Please specify where you want to move to.\n");
65 return;
68 DirectionParser p(str);
69 Coordinates result = p.getResult();
71 if(!result.size())
73 OnLine(Global::Get()->EmptyString);
74 m_sock->Sendf("'%s' is not a valid direction.\n");
75 return;
78 int i = 1;
79 for(Coordinates::const_iterator it = result.begin(); it != result.end(); it++)
81 Coordinate coordinate = *it;
82 if(!coordinate.isDirection())
84 m_sock->Sendf("Your %dth direction is not a direction.\n", i);
85 m_sock->Send("Please consult the helpfile on directions.\n");
86 return;
89 i++;
92 for(Coordinates::const_iterator it = result.begin(); it != result.end(); it++)
94 Coordinate coordinate = *it;
96 value_type chunkid = m_char->getChunk();
97 mud::ChunkPtr chunk = mud::Managers::Get()->Chunk->GetByKey(chunkid);
98 Assert(chunk);
100 value_type exitid = chunk->getExitAt(coordinate);
101 if(!exitid)
103 m_sock->Sendf("There is no exit to the %s.\n", coordinate.toDirectionString().c_str());
104 return;
107 mud::ExitPtr exit = mud::Managers::Get()->Exit->GetByKey(exitid);
108 Assert(exit->getFromChunk() == chunkid);
110 value_type tochunkid = exit->getToChunk();
111 Assert(tochunkid);
113 m_char->MoveToChunk(tochunkid);
117 void EditorMovement::OnFocus()
119 OnLine(m_line);
120 m_sock->PopEditor();
123 void EditorMovement::listCommands(const std::string& argument)
125 // TODO implement
127 return;
130 void EditorMovement::quitEditor(const std::string& argument)
132 m_sock->Send("Ok.\n");
133 m_sock->PopEditor();
134 return;
137 #if 0
138 void EditorRoom::North::Run(EditorRoom* editor, const std::string&)
140 if(editor->m_ypos <= 1)
142 editor->m_m_sock->Send("You can't go further north!\n");
143 return;
145 editor->m_ypos--;
146 editor->getRoom(Global::Get()->EmptyString);
147 editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor-> m_ypos));
150 void EditorRoom::NorthEast::Run(EditorRoom* editor, const std::string& argument)
152 long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->AREAS, editor->m_cluster);
153 if(count <= 0) {
154 Global::Get()->bug("EditorMap::NorthEast::Run() with clustercount <= 0!\n");
155 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");
156 return;
158 if(count >= 2) {
159 Global::Get()->bugf("EditorMap::NorthEast::Run() with clustercount '%d'!\n", count);
160 editor->m_m_sock->Sendf("For some reason the cluster the room you are in exists multiple times in the database?!n");
161 return;
163 Cluster* cluster = Cache::Get()->GetCluster(editor->m_cluster);
164 if(editor->m_ypos <= 1 || editor->m_xpos+1 > cluster->getWidth())
166 editor->m_m_sock->Send("You can't go further northeast!\n");
167 return;
169 editor->m_ypos--;
170 editor->m_xpos++;
171 editor->getRoom(Global::Get()->EmptyString);
172 editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos));
175 void EditorRoom::East::Run(EditorRoom* editor, const std::string& argument)
177 long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->AREAS, editor->m_cluster);
178 if(count <= 0) {
179 Global::Get()->bug("EditorMap::East::Run() with clustercount <= 0!\n");
180 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");
181 return;
183 if(count >= 2) {
184 Global::Get()->bugf("EditorMap::East::Run() with clustercount '%d'!\n", count);
185 editor->m_m_sock->Sendf("For some reason the cluster the room you are in exists multiple times in the database?!n");
186 return;
188 Cluster* cluster = Cache::Get()->GetCluster(editor->m_cluster);
189 if(editor->m_xpos+1 > cluster->getWidth())
191 editor->m_m_sock->Send("You can't go further east!\n");
192 return;
194 editor->m_xpos++;
195 editor->getRoom(Global::Get()->EmptyString);
196 editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos));
199 void EditorRoom::SouthEast::Run(EditorRoom* editor, const std::string& argument)
201 long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->AREAS, editor->m_cluster);
202 if(count <= 0) {
203 Global::Get()->bug("EditorMap::SouthEast::Run() with clustercount <= 0!\n");
204 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");
205 return;
207 if(count >= 2) {
208 Global::Get()->bugf("EditorMap::SouthEast::Run() with clustercount '%d'!\n", count);
209 editor->m_m_sock->Sendf("For some reason the cluster the room you are in exists multiple times in the database?!n");
210 return;
212 Cluster* cluster = Cache::Get()->GetCluster(editor->m_cluster);
213 if(editor->m_ypos+1 > cluster->getHeight() || editor->m_xpos+1 > cluster->getWidth())
215 editor->m_m_sock->Send("You can't go further southeast!\n");
216 return;
218 editor->m_ypos++;
219 editor->m_xpos++;
220 editor->getRoom(Global::Get()->EmptyString);
221 editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos));
224 void EditorRoom::South::Run(EditorRoom* editor, const std::string& argument)
226 long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->AREAS, editor->m_cluster);
227 if(count <= 0) {
228 Global::Get()->bug("EditorMap::South::Run() with clustercount <= 0!\n");
229 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");
230 return;
232 if(count >= 2) {
233 Global::Get()->bugf("EditorMap::South::Run() with clustercount '%d'!\n", count);
234 editor->m_m_sock->Sendf("For some reason the cluster the room you are in exists multiple times in the database?!n");
235 return;
237 Cluster* cluster = Cache::Get()->GetCluster(editor->m_cluster);
238 if(editor->m_ypos+1 > cluster->getHeight())
240 editor->m_m_sock->Send("You can't go further south!\n");
241 return;
243 editor->m_ypos++;
244 editor->getRoom(Global::Get()->EmptyString);
245 editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos));
248 void EditorRoom::SouthWest::Run(EditorRoom* editor, const std::string& argument)
250 long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->AREAS, editor->m_cluster);
251 if(count <= 0) {
252 Global::Get()->bug("EditorMap::SouthWest::Run() with clustercount <= 0!\n");
253 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");
254 return;
256 if(count >= 2) {
257 Global::Get()->bugf("EditorMap::SouthWest::Run() with clustercount '%d'!\n", count);
258 editor->m_m_sock->Sendf("For some reason the cluster the room you are in exists multiple times in the database?!n");
259 return;
261 Cluster* cluster = Cache::Get()->GetCluster(editor->m_cluster);
262 if(editor->m_xpos <= 1 || editor->m_ypos+1 > cluster->getHeight())
264 editor->m_m_sock->Send("You can't go further southwest!\n");
265 return;
267 editor->m_xpos--;
268 editor->m_ypos++;
269 editor->getRoom(Global::Get()->EmptyString);
270 editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos));
273 void EditorRoom::West::Run(EditorRoom* editor, const std::string& argument)
275 if(editor->m_xpos <= 1)
277 editor->m_m_sock->Send("You can't go further west!\n");
278 return;
280 editor->m_xpos--;
281 editor->getRoom(Global::Get()->EmptyString);
282 editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos));
285 void EditorRoom::NorthWest::Run(EditorRoom* editor, const std::string& argument)
287 if(editor->m_ypos <= 1 || editor->m_xpos <= 1)
289 editor->m_m_sock->Send("You can't go further northwest!\n");
290 return;
292 editor->m_xpos--;
293 editor->m_ypos--;
294 editor->getRoom(Global::Get()->EmptyString);
295 editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos));
297 #endif