-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cpp
325 lines (277 loc) · 9.6 KB
/
Parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
#############################################################################################
# Tux in Space - space exploration game #
# Copyright (C) 2016-2017 [email protected] #
# #
# 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, version 3 or compatibles. #
# #
# 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. #
#############################################################################################
*
* This is the instruction's parser. elaborate the input and do what these sentences in this strange human language would say.
*
*/
#include "Parser.h"
using namespace std;
// internal functions
void parser_Reask(const setting&, const string&);
void sys_Save(const setting&, system_c&);
void parser_Create(const setting&, system_c&);
void parser_Delete(const setting&, system_c&);
bool parser_Quit (const setting& set, system_c&);
void parser_Distance(const setting&, system_c&);
time_sim parser_Jump(const setting& set, time_sim&, const time_raw&);
time_sim parser_Wait(const setting& set, time_sim&, const time_raw&);
void parser_Information (const setting&, system_c&);
void parser_Edit (const setting&, system_c&);
/**
* The main function
* of the parser
*/
time_sim Parser(setting& set, system_c& sys, bool& quit) {
// what is scanned
string input;
// the time we want (simulation)
time_sim t = sys.stime;
// Now for every possible command call the correct command's function or simply do it if is short.
in_inline_s(input);
// continue
if ((!input.compare("step")) || (!input.compare("s"))) {
t.Add(sys.precision.time());
}
// help
else if ((!input.compare("help")) || (!input.compare("h"))) {
OPS(set, "HELP\n\nYou have to use commands to manage the system. Some commands are:\n-step (s)\n-create (c)\n-jump (j)\n-wait (w)\n-information (i)\n-settings\n-save\n-edit (e)\n-distance\n-quit\n-delete\n\nPress something to continue...");
in_s(input);
}
// Jump
else if ((!input.compare("jump")) || (!input.compare("j")))
t = parser_Jump(set, sys.stime, sys.precision);
// Settings
else if ((!input.compare("information")) || (!input.compare("i")) || (!input.compare("info")))
parser_Information(set, sys);
// Wait
else if ((!input.compare("wait")) || (!input.compare("w")))
t = parser_Wait(set, sys.stime, sys.precision);
// Create
else if (!(input.compare("create")) || (!input.compare("c")))
sys.NewObj(set);
// Quit / exit
else if (!(input.compare("quit")) || (!input.compare("exit")))
quit = parser_Quit(set, sys);
// save
else if (!input.compare("save"))
sys.Save(set);
// distance
else if (!input.compare("distance"))
parser_Distance(set, sys);
// delete an object
else if (!(input.compare("delete")) || (!input.compare("remove")))
parser_Delete(set, sys);
// edit an object
else if (!(input.compare("edit")) || (!input.compare("e")))
parser_Edit(set, sys);
// settings
else if (!(input.compare("settings")) || (!input.compare("configuration")))
menu_Settings(set);
// wrong command
else
parser_Reask(set, input);
in_clear();
return t;
}
/**
* The parser_Reask function. parser_Reask the input
*/
void parser_Reask(const setting& set, const string& command){
OPS_Error(set, "Sorry. But the command " + command + " that you wrote is unknown. Press something to continue:");
in_s();
}
/***
* This command calculate and show the distance between two objects
*/
void parser_Distance(const setting& set, system_c& sys){
string name;
object *o;
object *u;
tlength distance;
// ask which two object
OPS(set, "DISTANCE\n\nCalculate the distance between two objects.\n\n&t2Insert the name of the first object:\n\n'n' to exit");
in_s(name);
if(!name.compare("n"))
return;
o = sys.SearchObj(name);
if(o == nullptr) {
OPS_Error(set, "There isn't any object whit that name!\n\nInsert a new command");
return;
}
OPS(set, "DISTANCE\n\nCalculate the distance between two objects.\n\n&t2Insert the name of the second object:\n\n'n' to exit");
in_s(name);
if(!name.compare("n"))
return;
u = sys.SearchObj(name);
if(u == nullptr) {
OPS_Error(set, "There isn't any object whit that name!\n\nInsert a new command");
in_s();
return;
}
distance = o->Distance(*u).scalar();
stringstream ss;
ss << "DISTANCE\n\nThe distance between the two object is:\n&td" << distance.value() << " m/s\n\n&t0Press something to continue...";
OPS(set, ss.str());
in_s();
}
/***
* This function prepare the parser to parser_Quit
*/
bool parser_Quit (const setting& set, system_c& sys){
string input;
// ask for confirm to quit
OPS(set, "QUIT\n%r-Are you sure you want to quit? [y/n]\nOr you want to save before go? [s]");
in_s(input);
if (input[0] == 's') {
sys.Save(set);
}
else if (input[0] != 'y')
return false;
return true;
}
/***
* This function delete an object
*/
void parser_Delete(const setting& set, system_c& sys) {
string name; //the name of the object
object *obj; //the pointer to the object
// ask the user for the name
OPS(set, "DELETE\n%r-Which object do you want do delete?\n\n&t1Press 'n' to go back");
in_s(name);
if (!name.compare("n"))
return;
// look for the object
obj = sys.SearchObj(name);
if (obj == nullptr) { //if there isn't any object whit that name
OPS_Error(set, "There isn't any object whit that name!\nPress something to continue");
in_s();
return;
}
// delete the object
sys.RemoveObj(*obj);
}
/***
* parser_Settings about the system and the objects
*/
void parser_Information(const setting& set, system_c& sys) {
object *obj; // The object to describe
string input;
stringstream ss;
// Generic informations about the system
ss << "INFORMATIONS\n%r-System " << sys.name << " with " << sys.o.size()
<< " objects\n";
if(sys.o.size() > 0)
ss << "Of which object do you want informations?\n\npress 'n' to exit";
else {
ss << "&t2Press something to continue...";
OPS(set, ss.str());
in_s();
return;
}
OPS(set, ss.str());
in_s(input);
if(!input.compare("n"))
return;
// information about a precise object
obj = sys.SearchObj(input);
if(obj == nullptr){
OPS_Error(set, "No object with this name has been found.\n\n&t2Press something to continue...");
in_s();
return;
}
ss.clear();
ss.str("");
ss << "Informations about " << obj->name << "%s\n\n%r-type: " << obj->typ->name
<< "\n%r-color: &td \nred: " << obj->colour.red << "\ngreen: " << obj->colour.green
<< "\nblue: " << obj->colour.blue << " &t0 \n%r-mass: " << obj->Mass().value()
<< " Kg\n%r-radius: " << obj->Radius().value() << " m\n%r-x: " << obj->Pos().x().value() << " m\ny: "
<< obj->Pos().y().value() << " m\nz: " << obj->Pos().z().value() << " m\n%r-velocity in x: "
<< obj->Vel().x().value() << " m/s\nvelocity in y: " << obj->Vel().y().value() << " m/s\nvelocity in z: "
<< obj->Vel().z().value() << " m/s\n%r-\nPress somthing to continue...";
OPS(set, ss.str());
in_s();
}
/***
* Make the simulation wait for a while
*/
time_sim parser_Wait(const setting& set, time_sim& now, const time_raw& precision) {
OPS(set, "WAIT\n%r-Insert the amount of simulation-time you want to wait\n<year> <day> <hour> <minute> <second>\nThe operation will be made whit an error of max " + to_string(precision.time()) + " seconds");
//scanf the time
int y, d, h, m;
float s;
// year
y = in_inline_i();
// day
d = in_inline_i();
// hour
h = in_inline_i();
// minute
m = in_inline_i();
// second
s = in_f();
time_sim t (y, d, h, m, s);
return now + t;
}
/***
* Make the simulation Jump to a determined time
*/
time_sim parser_Jump(const setting& set, time_sim& now, const time_raw& precision) {
time_sim t;
stringstream ss;
ss << "JUMP\n%r-Insert the information about the moment you want to jump\n<year> <day> <hour> <minute> <second>\nThe actual time is: YEAR "
<< now.Year() << " DAY " << now.Day() << " TIME " << now.Hour() << ":"
<< now.Minute() << ":" << now.Second() << "\nThe jump will be made whit an error of max "
<< precision.time() << " seconds";
OPS(set, ss.str());
while(1) {
int y, d, h, m;
float s;
// year
y = in_inline_i();
// day
d = in_inline_i();
// hour
h = in_inline_i();
// minute
m = in_inline_i();
// second
s = in_f();
time_sim t(y, d, h, m, s);
if(t.Compare(now) == comparison::major) {
// Error message if the time given isn't valid
t = now;
OPS_Error(set, "The time given is out of range! Please put a time that is future to the actual time, that is:\nYEAR " + to_string(t.Year()) + " DAY " + to_string(t.Day()) + " TIME " + to_string(t.Hour()) + ":" + to_string(t.Minute()) + ":" + to_string(t.Second()) + "\nThe jump will be made whit an error of max " + to_string(precision.time()) + " seconds");
}
else
break;
}
return t;
}
void parser_Edit (const setting& set, system_c& sys){
OPS(set, "EDIT\n\nWhat object do you want to edit?");
string name;
in_s(name);
object *obj = sys.SearchObj(name);
if(obj == nullptr){
OPS_Error(set, "There is no object with such a name!\n\n&t2Press something to continue...");
in_s();
return;
}
sys.EditObj(set, *obj);
}