Added to project file and made it compilable.
[UnsignedByte.git] / src / Core / Editors / EditorMovement.cpp
blob6d5180b37df031cbfb489200b75dab103749754a
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 <boost/spirit/core.hpp>
22 #include <boost/spirit/symbols/symbols.hpp>
24 #include "EditorAccount.h"
25 #include "EditorMovement.h"
26 #include "UBSocket.h"
27 #include "Array.h"
28 #include "Assert.h"
29 #include "StringUtilities.h"
30 #include "Account.h"
31 #include "Character.h"
32 #include "PCharacter.h"
33 #include "PCharacterManager.h"
34 #include "Chunk.h"
35 #include "ChunkManager.h"
36 #include "Managers.h"
37 #include "Coordinate.h"
39 using namespace boost::spirit;
40 using mud::PCharacter;
42 struct dirs : symbols<Coordinate>
44 dirs()
46 add
47 ("n", Coordinate(1, 0, 0))
48 ("s", Coordinate(-1, 0, 0))
49 ("e", Coordinate(0, 1, 0))
50 ("w", Coordinate(0, -1, 0))
51 ("u", Coordinate(0, 0, 1))
52 ("d", Coordinate(0, 0, -1))
56 } dirs_p;
58 struct add_dir
60 add_dir(Coordinate& coordinate) : m_coordinate(coordinate) {}
61 void operator()(Coordinate& rhs) const { m_coordinate += rhs; }
62 Coordinate& m_coordinate;
65 struct add_and_reset
67 add_and_reset(std::vector<Coordinate>& c) : m_coordinates(c) { }
68 void operator()(Coordinate& rhs) const {
69 m_coordinates.push_back(rhs);
70 rhs = rhs - rhs;
72 std::vector<Coordinate>& m_coordinates;
76 struct direction : public grammar<direction>
78 template <typename ScannerT>
79 struct definition
81 definition(direction const& self)
83 first = //(
84 +dirs_p[add_dir(self.m_coordinate)]; // >> ch_p(';')
85 //)[add_and_reset(self.m_coordinates)];
89 rule<ScannerT> first;
90 rule<ScannerT> const& start() const { return first; }
93 direction(std::vector<Coordinate>& coordinates, Coordinate& coordinate) :
94 m_coordinate(coordinate),
95 m_coordinates(coordinates)
99 Coordinate& m_coordinate;
100 std::vector<Coordinate>& m_coordinates;
103 EditorMovement::EditorMovement(UBSocket* sock, mud::PCharacterPtr character) :
104 Editor(sock),
105 m_char(character)
110 EditorMovement::~EditorMovement(void)
115 std::string EditorMovement::lookup(const std::string& str)
117 if (str.empty())
118 return Global::Get()->EmptyString;
120 std::vector<Coordinate> coordinates;
121 Coordinate coordinate(0,0,0);
123 direction direction_p(coordinates, coordinate);
125 bool match = parse(str.c_str(), direction_p).full;
127 if (match)
128 return str;
129 else
130 return Global::Get()->EmptyString;
133 void EditorMovement::dispatch(const std::string& action, const std::string& argument)
135 // TODO implement
138 void EditorMovement::listCommands(const std::string& argument)
140 // TODO implement
142 return;
145 void EditorMovement::quitEditor(const std::string& argument)
147 m_sock->Send("Ok.\n");
148 m_sock->PopEditor();
149 return;
152 #if 0
153 void EditorRoom::North::Run(EditorRoom* editor, const std::string&)
155 if(editor->m_ypos <= 1)
157 editor->m_m_sock->Send("You can't go further north!\n");
158 return;
160 editor->m_ypos--;
161 editor->getRoom(Global::Get()->EmptyString);
162 editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor-> m_ypos));
165 void EditorRoom::NorthEast::Run(EditorRoom* editor, const std::string& argument)
167 long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->AREAS, editor->m_cluster);
168 if(count <= 0) {
169 Global::Get()->bug("EditorMap::NorthEast::Run() with clustercount <= 0!\n");
170 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");
171 return;
173 if(count >= 2) {
174 Global::Get()->bugf("EditorMap::NorthEast::Run() with clustercount '%d'!\n", count);
175 editor->m_m_sock->Sendf("For some reason the cluster the room you are in exists multiple times in the database?!n");
176 return;
178 Cluster* cluster = Cache::Get()->GetCluster(editor->m_cluster);
179 if(editor->m_ypos <= 1 || editor->m_xpos+1 > cluster->getWidth())
181 editor->m_m_sock->Send("You can't go further northeast!\n");
182 return;
184 editor->m_ypos--;
185 editor->m_xpos++;
186 editor->getRoom(Global::Get()->EmptyString);
187 editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos));
190 void EditorRoom::East::Run(EditorRoom* editor, const std::string& argument)
192 long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->AREAS, editor->m_cluster);
193 if(count <= 0) {
194 Global::Get()->bug("EditorMap::East::Run() with clustercount <= 0!\n");
195 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");
196 return;
198 if(count >= 2) {
199 Global::Get()->bugf("EditorMap::East::Run() with clustercount '%d'!\n", count);
200 editor->m_m_sock->Sendf("For some reason the cluster the room you are in exists multiple times in the database?!n");
201 return;
203 Cluster* cluster = Cache::Get()->GetCluster(editor->m_cluster);
204 if(editor->m_xpos+1 > cluster->getWidth())
206 editor->m_m_sock->Send("You can't go further east!\n");
207 return;
209 editor->m_xpos++;
210 editor->getRoom(Global::Get()->EmptyString);
211 editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos));
214 void EditorRoom::SouthEast::Run(EditorRoom* editor, const std::string& argument)
216 long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->AREAS, editor->m_cluster);
217 if(count <= 0) {
218 Global::Get()->bug("EditorMap::SouthEast::Run() with clustercount <= 0!\n");
219 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");
220 return;
222 if(count >= 2) {
223 Global::Get()->bugf("EditorMap::SouthEast::Run() with clustercount '%d'!\n", count);
224 editor->m_m_sock->Sendf("For some reason the cluster the room you are in exists multiple times in the database?!n");
225 return;
227 Cluster* cluster = Cache::Get()->GetCluster(editor->m_cluster);
228 if(editor->m_ypos+1 > cluster->getHeight() || editor->m_xpos+1 > cluster->getWidth())
230 editor->m_m_sock->Send("You can't go further southeast!\n");
231 return;
233 editor->m_ypos++;
234 editor->m_xpos++;
235 editor->getRoom(Global::Get()->EmptyString);
236 editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos));
239 void EditorRoom::South::Run(EditorRoom* editor, const std::string& argument)
241 long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->AREAS, editor->m_cluster);
242 if(count <= 0) {
243 Global::Get()->bug("EditorMap::South::Run() with clustercount <= 0!\n");
244 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");
245 return;
247 if(count >= 2) {
248 Global::Get()->bugf("EditorMap::South::Run() with clustercount '%d'!\n", count);
249 editor->m_m_sock->Sendf("For some reason the cluster the room you are in exists multiple times in the database?!n");
250 return;
252 Cluster* cluster = Cache::Get()->GetCluster(editor->m_cluster);
253 if(editor->m_ypos+1 > cluster->getHeight())
255 editor->m_m_sock->Send("You can't go further south!\n");
256 return;
258 editor->m_ypos++;
259 editor->getRoom(Global::Get()->EmptyString);
260 editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos));
263 void EditorRoom::SouthWest::Run(EditorRoom* editor, const std::string& argument)
265 long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->AREAS, editor->m_cluster);
266 if(count <= 0) {
267 Global::Get()->bug("EditorMap::SouthWest::Run() with clustercount <= 0!\n");
268 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");
269 return;
271 if(count >= 2) {
272 Global::Get()->bugf("EditorMap::SouthWest::Run() with clustercount '%d'!\n", count);
273 editor->m_m_sock->Sendf("For some reason the cluster the room you are in exists multiple times in the database?!n");
274 return;
276 Cluster* cluster = Cache::Get()->GetCluster(editor->m_cluster);
277 if(editor->m_xpos <= 1 || editor->m_ypos+1 > cluster->getHeight())
279 editor->m_m_sock->Send("You can't go further southwest!\n");
280 return;
282 editor->m_xpos--;
283 editor->m_ypos++;
284 editor->getRoom(Global::Get()->EmptyString);
285 editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos));
288 void EditorRoom::West::Run(EditorRoom* editor, const std::string& argument)
290 if(editor->m_xpos <= 1)
292 editor->m_m_sock->Send("You can't go further west!\n");
293 return;
295 editor->m_xpos--;
296 editor->getRoom(Global::Get()->EmptyString);
297 editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos));
300 void EditorRoom::NorthWest::Run(EditorRoom* editor, const std::string& argument)
302 if(editor->m_ypos <= 1 || editor->m_xpos <= 1)
304 editor->m_m_sock->Send("You can't go further northwest!\n");
305 return;
307 editor->m_xpos--;
308 editor->m_ypos--;
309 editor->getRoom(Global::Get()->EmptyString);
310 editor->m_m_sock->Send(Room::CreateMap(editor->m_cluster, editor->m_xpos, editor->m_ypos));
312 #endif