-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
923 lines (844 loc) · 33 KB
/
main.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
/**
* @file Main.cpp
* Main file for the game.
*
* \licence Open-source
* @date 14/08/2011
* @brief The main file for txtEngine.
*
* @author Michael Abrams
* @author James Boocock
* @author Toby Herbert
* @author Tatai Nikora
* @version 0.3
* @remark Parser code is freely distributed TinyXML library
*
*/
/*! \mainpage Documentation for the txtEngine Project
*
* \section date_sec Date Updated:
* 29-11-2011
*
* \section about_sec What is txtEngine?
* txtEngine is an interpreter for text only adventure games. Games are
* written using the XML language making it easy for anyone to write and
* play their own games.
*
* \section doc_sec Documentation:
* To view the txtEngine Language Specification and Documentation
* click here: <a href="../DocumentationMain.pdf" target="_blank">
* txtEngine Documentation</a>
*
* \section links_sec Links:
*
* <ul>
* <li>txtEngine Project Site on Github:
* <a href="https://github.com/smilefreak/txtEngine">
* https://github.com/smilefreak/txtEngine</a><br></li>
*
* <li>TinyXML Documentation:
* <a href="http://www.grinninglizard.com/tinyxmldocs/index.html">
* http://www.grinninglizard.com/tinyxmldocs/index.html</a><br></li>
* </ul>
*
* \section bugs_sec Report Bugs:
* Please report any bugs here:
* <a href="https://github.com/smilefreak/txtEngine/issues">
* https://github.com/smilefreak/txtEngine/issues</a><br>
*
* \section author_sec Authors:
* Toby Herbert, Michael Abrams, James Boocock, Tatai Nikora
*
*
*/
//------------------------------------------------------------------------------
/* Include */
//------------------------------------------------------------------------------
#include <iostream>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <string>
#include "parser.h"
#include "Constants.h"
//------------------------------------------------------------------------------
/* Global Variables */
//------------------------------------------------------------------------------
World *world; ///< The world object.
bool game_over = false; ///< Flag to end a game.
std::vector<std::string> commandList; ///< List of commands used.
std::vector<std::string> filterList; ///< List of words to filter from input.
//------------------------------------------------------------------------------
/* Method Prototypes */
//------------------------------------------------------------------------------
/**
@brief The main gameloop.
*/
void gameloop();
/**
@brief A method to handle one word commands.
@param[in] command A single word command in the form of a string.
@return Output of the command.
*/
std::string one_word_command(std::string command);
/**
@brief A method to handle two word commands.
@param[in] command1 First command in the form of a string.
@param[in] command2 Second command in the form of a string.
@return Output of the command.
*/
std::string two_word_command(std::string command1, std::string command2);
/**
A method for handling three word commands.
For example: combine pen paper.
@param[in] command The three word command.
@return A string to output.
*/
std::string three_word_command(std::string command);
/**
Prints out the contents of the inventory vector.
*/
void print_inventory();
/**
Wraps the output to a specified size.
@param[in] input_string The output of the game to be wrapped
@return A wrapped string, properly formatted.
*/
std::string word_wrap(std::string input_string);
/**
This method is used for debug purposes only:
Prints out the parsed XML file in a tree structure.
*/
void print_world_tree();
/**
Loads a game from a .sav file.
*/
void load(char* const file);
/**
Saves a game to a .sav file by dumping the command list vector
to a file.
*/
void save(char* const file);
/**
Checks the input string for words that are in the filterList
vector. If they are in the list they are removed from the string.
@param[in] input_string A string to be filtered
@return A string with words from filterList removed.
*/
std::string input_filter(std::string input_string);
/**
Reads words from a specified file into
the filterList vector.
@param[in] str A string of a file path to a list of words to ignore.
*/
void read_filter_list(std::string str);
/**
Processes input and calls the appropriate method for the number
of words in the command. Commands are stored in the commandList
and if too many words are in the string a feedback message is
output. If the game is in load mode then no output is displayed.
@param[in] to_process An input string to process.
@param[in] load A flag for whether the game is in load mode.
*/
void process_input(std::string to_process, bool load);
/**
Reads words from a specified file into
the filterList vector.
@param[in] file A char array - the file path to a list of words to ignore.
*/
void read_filter_list(const char* file);
/**
Get all of the area commands for the current area.
@return A string containing all of the area commands.
*/
std::string get_all_area_commands();
/**
Gets all valid item commands for an inventory item.
@param[in] temp_item A pointer to an item.
@param[in] item An integer.
@return The valid item commands.
*/
std::string valid_item_command_inv(Item * temp_item, int item);
/**
Gets the valid area commands for items in the current area.
@param[in] temp_item A pointer to the item.
@param[in] items Number of items.
@return The valid item commands for items in the area.
*/
std::string valid_item_command_area(Item* temp_item, int items);
/**
Reads words from a specified file into
the filterList vector.
@return A string of a file path to a list of words to ignore.
*/
std::string get_all_item_commands();
/**
External output method used by web interface.
@param[in] command Input commands to be processed.
*/
void external_output(std::string command);
//------------------------------------------------------------------------------
/* Method Definitions */
//------------------------------------------------------------------------------
void print_world_tree() {
std::ostringstream sin;
sin << "World:\n";
for(int area = 0; area < world->get_num_areas(); area++) {
Area *temp_area = world->get_area(area);
sin << "\tArea: ";
sin << temp_area->get_id();
sin << "\n";
for(int state_desc = 0; state_desc < temp_area->get_num_descriptions(); state_desc++) {
StateDescriptor *temp_desc = temp_area->get_descriptor(state_desc);
sin << "\t\tDesc: ";
sin << temp_desc->get_description();
sin << "\n";
}
for(int command = 0; command < temp_area->get_num_commands(); command++) {
AreaCommand *temp_command = temp_area->get_command(command);
sin << "\t\tCommand: ";
sin << temp_command->get_name();
sin << "\n\t\t\t";
sin << temp_command->get_message();
sin << "\n";
}
for(int item = 0; item < temp_area->get_num_items(); item++) {
Item *temp_item = temp_area->get_item(item);
sin << "\t\tItem:";
sin << temp_item->get_id();
sin << "\n";
for(int state_desc = 0; state_desc < temp_item->get_num_descriptions(); state_desc++) {
StateDescriptor *temp_desc = temp_item->get_descriptor(state_desc);
sin << "\t\t\tDesc: ";
sin << temp_desc->get_description();
sin << "\n";
}
for(int command = 0; command < temp_item->get_num_commands(); command++) {
ItemCommand *temp_command = temp_item->get_command(command);
sin << "\t\t\tCommand: ";
sin << temp_command->get_name();
sin << "\n\t\t\t";
sin << temp_command->get_message();
sin << "\n";
}
}
}
std::string message = sin.str();
fprintf(stderr, "Attempt at printing world.\n%s\n", message.c_str());
}
void process_input(std::string line, bool load) {
commandList.push_back(line);
std::ostringstream commandstream;
std::string command1 , command2, command3;
std::string checkmorewords;
std::istringstream iss(line);
if(iss >> command1) {
if (iss >> command2) {
if(!(iss >> command3)) {
commandstream << two_word_command(command1 ,command2);
}
else if(!(iss >> checkmorewords)){
std::stringstream s2;
s2 << command1 << " " << command2 << " " << command3;
checkmorewords = s2.str();
commandstream << three_word_command(checkmorewords);
} else {
std::cout << TOOMANYWORDS << std::endl;
}
} else {
std::string from_one_word = one_word_command(command1);
commandstream << from_one_word;
}
}
else {
std::cout << TOOMANYWORDS << std::endl;
}
if(!load){
std::cout << "\n" << commandstream.str() << std::endl;
}
}
/*
* Return all possible area commands availiable for the current state of the world
*
* @return string containing all availiable area commando on one line each
*
* */
std::string get_all_area_commands(){
std::string commands;
for(int ac = 0; ac < world->get_active_area()->get_num_commands(); ac++){
AreaCommand *temp_area_command = world->get_active_area()->get_command(ac);
assert(temp_area_command != NULL);
if(world->get_area(INVENTORY)->has_item(temp_area_command->get_depends()) ||
!strcmp(temp_area_command->get_depends().c_str(), NONE)) {
if(!temp_area_command->is_locked()) {
commands += "{\"";
commands += temp_area_command->get_name();
commands += "\"}";
}
}
}
return commands;
}
std::string valid_item_command_inv(Item * temp_item, int item){
std::string command_output = "";
for(int i = 0; i < temp_item->get_num_commands(); i++){
ItemCommand * temp_item_command= temp_item->get_command(i);
assert(temp_item_command != NULL);
if(temp_item_command->get_collect_dependent() == temp_item->is_collectable()) {
command_output += "{\"";
command_output += temp_item_command->get_name();
command_output +=" ";
command_output += temp_item->get_name();
command_output += "\"}";
}
}
return command_output;
}
std::string valid_item_command_area(Item* temp_item, int items){
std::string command_output = "";
unsigned int pv;
if(world->get_area(INVENTORY)->has_item(world->get_active_area()->get_item(temp_item->get_id(),pv)->get_depends()) ||
!strcmp(world->get_active_area()->get_id().c_str(), world->get_active_area()->get_item(temp_item->get_id(),pv)->get_depends().c_str()) ||
!strcmp(world->get_active_area()->get_item(temp_item->get_id(),pv)->get_depends().c_str(), NONE)){
for(int i = 0; i < temp_item->get_num_commands(); i++){
ItemCommand * temp_item_command = temp_item->get_command(i);
assert(temp_item_command != NULL);
if(world->get_area(INVENTORY)->has_item(temp_item_command->get_depends()) ||
!strcmp(world->get_active_area()->get_id().c_str(), temp_item_command->get_depends().c_str()) ||
!strcmp(temp_item_command->get_depends().c_str(), NONE)) {
if(temp_item_command->get_collect_dependent() == temp_item->is_collectable()) {
if(temp_item_command->unlocks()) {
if(temp_item_command->get_unlock_string().find_first_of('/') == std::string::npos){
std::string temp_item_id = temp_item_command->unlock_area_string();
if(world->get_active_area()->has_item(temp_item_id) ||
world->get_area(INVENTORY)->has_item(temp_item_id)){
command_output += "{\"";
command_output += temp_item_command->get_name();
command_output += temp_item_command->get_name();
command_output += temp_item->get_name();
command_output += "\"}";
command_output+=",";
}
}else if(!temp_item_command->unlock_area_string().compare(world->get_active_area()->get_id()))
{
command_output += "{\"";
command_output += temp_item_command->get_name();
command_output += " ";
command_output += temp_item->get_name();
command_output += "\"}";
command_output+=",";
}
} else {
command_output += "{\"";
command_output += temp_item_command->get_name();
command_output += " ";
command_output += temp_item->get_name();
command_output += "\"}";
command_output+=",";
}
}
}
}
}
return command_output;
}
/* :
* Return all the possible commands availiable for the current stage the world
*
* @return string containing each availiable command on one line each
*
*/
std::string get_all_item_commands(){
std::stringstream itemstream;
std::string command_output = "";
for(int item = 0; item < world->get_area(INVENTORY)->get_num_items(); item++){
Item* temp_item = world->get_area(INVENTORY)->get_item(item);
assert(temp_item != NULL);
command_output += valid_item_command_inv(temp_item, item);
if(!temp_item->is_locked() && temp_item->has_container()){
command_output += "{ \"put item ";
command_output += temp_item->get_name();
command_output += "\"},";
for(int item_in_item = 0; item_in_item < temp_item->get_num_items(); item_in_item++){
Item * within = temp_item->get_item(item_in_item);
command_output += valid_item_command_area(within,item_in_item);
}
}
combine * temp_combine = temp_item->get_combine();
if(temp_combine != NULL){
std::cout << temp_combine->get_first_id()<< " " << temp_combine->get_second_id() << std::endl;
command_output += "{ \"combine ";
command_output += temp_combine->get_first_id();
command_output += " ";
command_output += temp_combine->get_second_id();
command_output += "\"},";
}
}
for(int items = 0; items < world->get_active_area()->get_num_items(); items++){
Item * temp_item = world->get_active_area()->get_item(items);
command_output += valid_item_command_area(temp_item, items);
if(!temp_item->is_locked() && temp_item->has_container()){
command_output += "{ \"put item ";
command_output += temp_item->get_name();
command_output += "\"},";
for(int item_in_item = 0; item_in_item < temp_item->get_num_items(); item_in_item++){
Item * within = temp_item->get_item(item_in_item);
command_output += valid_item_command_area(within,item_in_item);
}
}
}
return command_output;
}
void external_output(std::string command){
std::stringstream print;
std::cout << "{";
std::cout << "\"response\": \"";
process_input(command, false);
std::cout << "\",";
std::cout << "\"inventory\": [";
for(int items = 0; items < world->get_area(INVENTORY)->get_num_items(); items++) {
std::cout << "{\"name\":\"" << world->get_area(INVENTORY)->get_item(items)->get_name() << "\",";
std::cout << "\"description\":\"" << world->get_area(INVENTORY)->get_item(items)->get_description() << "\"},";
}
std::cout << "],";
std::cout << "\"areaname\": \"" << world->get_active_area()->get_area_name() << "\",";
std::cout << "\"areadescription\": \"" << world->get_active_area()->get_description() << "\"";
std::cout << "\"commandlist\": [";
std::cout << "{\"look\"},";
std::cout << get_all_item_commands();
std::cout << get_all_area_commands();
std::cout << "]";
std::cout << "}";
}
void gameloop() {
std::string last_area = DEFAULT_VALUE;
while(!game_over) {
std::ostringstream sin;
if(last_area.compare(world->get_active_area()->get_id()) != 0) {
last_area = world->get_active_area()->get_id();
sin << world->get_active_area()->get_description();
if(!strcmp(world->get_active_area()->get_status().c_str(), WIN) || !strcmp(world->get_active_area()->get_status().c_str(), DIE)) {
game_over = true;
}
std::cout << word_wrap(sin.str())<<std::endl;
}
if(!game_over) {
std::cout << ">>";
std::string line;
std::getline(std::cin, line);
line = input_filter(line);
//external_output("");
process_input(line, false);
}
}
}
std::string two_word_command(std::string command1, std::string command2) {
std::ostringstream result;
std::transform(command1.begin(), command1.end(),
command1.begin(), ::tolower);
std::transform(command2.begin(), command2.end(),
command2.begin(), ::tolower);
std::vector<std::string> containers_in_area;
if(!strcmp(command1.c_str(), GO)) {
return one_word_command(command2);
}
unsigned int item =-1 ;
Item *temp_item = world->get_active_area()->get_item(command2, item);
if(temp_item != NULL) {
if(world->get_area(INVENTORY)->has_item(temp_item->get_depends()) ||
!strcmp(world->get_active_area()->get_id().c_str(), world->get_active_area()->get_item(temp_item->get_id(),item)->get_depends().c_str()) ||
!strcmp(world->get_active_area()->get_item(temp_item->get_id(),item)->get_depends().c_str(), NONE)){
ItemCommand *temp_item_command = temp_item->get_command(command1);
if(temp_item_command != NULL) {
if(world->get_area(INVENTORY)->has_item(temp_item_command->get_depends()) ||
!strcmp(world->get_active_area()->get_id().c_str(), temp_item_command->get_depends().c_str()) ||
!strcmp(temp_item_command->get_depends().c_str(), NONE)) {
if(temp_item_command->get_collect_dependent() == temp_item->is_collectable()) {
if(!strcmp(temp_item_command->get_status().c_str(), WIN)||
!strcmp(temp_item_command->get_status().c_str() ,DIE)) {
game_over = true;
}
temp_item->state_change(temp_item_command->get_state_change());
temp_item->change_collectable(temp_item_command->get_change_collect());
if(temp_item_command->unlocks()) {
if(temp_item_command->get_unlock_string().find_first_of('/') == std::string::npos){
std::string temp_item_id = temp_item_command->unlock_area_string();
if(world->get_active_area()->has_item(temp_item_id)){
unsigned int this_is_pointless = 0;
world->get_active_area()->get_item(temp_item_id, this_is_pointless)->flip_locked();
} else if(world->get_area(INVENTORY)->has_item(temp_item_id)){
unsigned int this_is_pointless = 0;
world->get_area(INVENTORY)->get_item(temp_item_id, this_is_pointless)->flip_locked();
} else {
std:: cout << "congrats you found a bug";
}
} else {
if(!temp_item_command->unlock_area_string().compare(world->get_active_area()->get_id())) {
world->get_active_area()->unlock(temp_item_command->unlock_areacommand_string());
}
else {
return "You can't do that. \n";
}
}
}
world->get_active_area()->remove_item(temp_item->get_id());
if(world->get_area(temp_item_command->get_area_change()) != NULL) {
world->get_area(temp_item_command->get_area_change())->add_item(temp_item);
}
else {
world->get_active_area()->add_item(temp_item);
}
result << temp_item_command->get_message();
result << "\n";
return result.str();
}
}
else {
return "You can't do that.\n";
}
}
else {
//Command does not exist for that item.
result << "There is no command ";
result << command1;
result << " for item ";
result << command2;
return result.str();
}
}
}
temp_item = NULL;
temp_item = world->get_area(INVENTORY)->get_item(command2, item);
if(temp_item != NULL) {
ItemCommand *temp_item_command = temp_item->get_command(command1);
if(temp_item_command != NULL) {
if(temp_item_command->get_collect_dependent() == temp_item->is_collectable()) {
if(!strcmp(world->get_active_area()->get_status().c_str(),WIN)||!strcmp(world->get_active_area()->get_status().c_str() ,DIE)) {
game_over = true;
}
temp_item->state_change(temp_item_command->get_state_change());
temp_item->change_collectable(temp_item_command->get_change_collect());
if(temp_item_command->unlocks()) {
if(temp_item_command->get_unlock_string().find_first_of('/') == std::string::npos){
std::string temp_item_id = temp_item_command->unlock_area_string();
unsigned int unknown = 0;
if(world->get_active_area()->has_item(temp_item_id)){
world->get_active_area()->get_item(temp_item_id, unknown)->flip_locked();
} else if(world->get_area(INVENTORY)->has_item(temp_item_id)){
world->get_area(INVENTORY)->get_item(temp_item_id, unknown)->flip_locked();
} else {
std:: cout << "You cant do that\n";
}
} else {
if(!temp_item_command->unlock_area_string().compare(world->get_active_area()->get_id())) {
world->get_active_area()->unlock(temp_item_command->unlock_areacommand_string());
}
else {
return "You can't do that. \n";
}
}
}
world->get_area(INVENTORY)->remove_item(temp_item->get_id());
if( world->get_area(temp_item_command->get_area_change()) !=NULL) {
world->get_area(temp_item_command->get_area_change())->add_item(temp_item);
}
else {
world->get_active_area()->add_item(temp_item);
}
result << temp_item_command->get_message();
result << "\n";
return result.str();
}
}
else {
result << "There is no command ";
result << command1;
result << " for item ";
result << command2;
return result.str();
}
}
return "I don't understand that. \n";
}
std::string three_word_command(std::string command){
std::transform(command.begin(), command.end(),
command.begin(), ::tolower);
std::stringstream word(command);
std::string first_id;
std::string second_id;
std::string action;
word >> action >>first_id >>second_id;
if(!action.compare(COMBINE) || !action.compare(MIX)) {
unsigned int item_1;
unsigned int item_2;
Item * have_item_1 = world->get_area(INVENTORY)->get_item(first_id,item_1);
Item * have_item_2 = world->get_area(INVENTORY)->get_item(second_id,item_2);
if(have_item_2 != NULL && have_item_1 != NULL){
combine * temp_combine;
if(have_item_1->has_combine()){
std::string id_combine = have_item_2->get_id();
temp_combine = have_item_1->get_combine();
if(!temp_combine->get_second_id().compare(id_combine)){
world->get_area(GARBAGE)->add_item(have_item_2);
world->get_area(GARBAGE)->add_item(have_item_1);
world->get_area(INVENTORY)->remove_item(have_item_2->get_id());
world->get_area(INVENTORY)->remove_item(have_item_1->get_id());
world->get_area(INVENTORY)->add_item(temp_combine->get_combination());
return temp_combine->get_description() + "\n";
}
}
else if(have_item_2->has_combine()){
std::string id_combine = have_item_2->get_id();
temp_combine = have_item_2->get_combine();
if(!temp_combine->get_first_id().compare(id_combine)){
world->get_area(GARBAGE)->add_item(have_item_2);
world->get_area(GARBAGE)->add_item(have_item_1);
world->get_area(INVENTORY)->remove_item(have_item_1->get_id());
world->get_area(INVENTORY)->remove_item(have_item_2->get_id());
world->get_area(INVENTORY)->add_item(temp_combine->get_combination());
return temp_combine->get_description() + "\n";
}
else {
return "no combine information found for these two items\n";
}
} else {
return "no combine information found for these two items\n";
}
} else {
return "One or more item names incorrect for combine \n";
}
} else if(!action.compare(PUT) || !action.compare(STORE)){
unsigned int item;
unsigned int item2;
Item * have_item = world->get_area(INVENTORY)->get_item(first_id,item);
if(have_item != NULL){
Item* box = world->get_area(INVENTORY)->get_item(second_id,item2);
if(box == NULL) {
box = world->get_active_area()->get_item(second_id,item2);
}
if(box != NULL){
if(box->has_container()){
if(!box->is_locked()){
box->add_item(have_item);
world->get_area(INVENTORY)->remove_item(item);
std::stringstream ss;
ItemCommand *temp_item_command = have_item->get_command("drop");
if(temp_item_command != NULL) {
have_item->state_change(temp_item_command->get_state_change());
} else {
ss << "BUG: \n";
ss << "There is no drop command in XML for that item.";
ss << " so description hasn't changed. Item still moved.\n";
}
ss <<"You put the "<< first_id << " in the " << second_id << "\n";
have_item->change_collectable(true);
return ss.str();
}
return "The box is locked";
}
return "this item has no storage";
}
return "second item is not in inventory or the area you are in. \n";
}
return "I don't understand that. \n";
}
return "I don't understand that. \n";
}
std::string one_word_command(std::string command) {
std::transform(command.begin(), command.end(),
command.begin(), ::tolower);
if(!command.compare(HELP)) {
return HELP_COMMAND;
}
if(!command.compare(N)) {
command = NORTH;
}
if(!command.compare(S)) {
command = SOUTH;
}
if(!command.compare(E)) {
command = EAST;
}
if(!command.compare(W)) {
command = WEST;
}
if(!command.compare(QUIT)) {
game_over = true;
return DEFAULT_VALUE;
}
if(!command.compare(LOOK)) {
std::stringstream itemstream;
std::string combinelist;
for(int items = 0; items < world->get_active_area()->get_num_items(); items++) {
if(world->get_area(INVENTORY)->has_item(world->get_active_area()->get_item(items)->get_depends()) ||
!strcmp(world->get_active_area()->get_id().c_str(), world->get_active_area()->get_item(items)->get_depends().c_str()) ||
!strcmp(world->get_active_area()->get_item(items)->get_depends().c_str(), NONE)){
itemstream << world->get_active_area()->get_item(items)->get_description();
if(world->get_active_area()->get_item(items)->has_container() &&
!world->get_active_area()->get_item(items)->is_locked()){
combinelist+= ((world->get_active_area()->get_item(items)->print_contained_items()));
combinelist +="\n";
}
itemstream << "\n";
}
}
std::stringstream sSs;
sSs << word_wrap(itemstream.str())<< std::endl << combinelist;
return sSs.str();
}
if (!command.compare(SAVE)) {
std::cout << "Please enter a save filename: " << std::endl;
std::string filename;
getline(std::cin, filename);
save((char* const)filename.c_str());
return "saved";
}
else if(!command.compare(BAG)) {
print_inventory();
return "";
}
else if(!command.compare(INVENTORY)) {
print_inventory();
return "";
}
AreaCommand *temp_area_command = world->get_active_area()->has_command(command);
if(temp_area_command != NULL) {
if(world->get_area(INVENTORY)->has_item(temp_area_command->get_depends()) ||
!strcmp(temp_area_command->get_depends().c_str(), NONE)) {
if(!temp_area_command->is_locked()) {
world->change_area(temp_area_command->get_area());
return temp_area_command->get_message();
}
return "That way is locked";
}
else {
return "You can't do that.\n";
}
}
else {
return "I don't understand that. \n";
}
}
void print_inventory() {
std::ostringstream sin;
int num = 1;
sin << "INVENTORY: \n" ;
std::string inv = INVENTORY;
for(int items = 0; items < world->get_area(inv)->get_num_items(); items++) {
sin << num++ << " = " << world->get_area(INVENTORY)->get_item(items)->get_description();
if(items < world->get_area(inv)->get_num_items() - 1) {
sin << "\n";
}
}
std::cout << "\n" << sin.str();
}
std::string word_wrap(std::string input_string) {
std::istringstream iss(input_string);
std::string formatted;
std::string line;
std::string word;
while (iss >> word) {
if (line.length() + word.length() > MAX_CHARACTERS_PER_LINE) {
formatted +=line+"\n";
line.clear();
}
line += word + " ";
}
if (!line.empty()) {
formatted += line +"\n";
}
return formatted + "\n";
}
void load(char* const file) {
std::ifstream myfile (file);
if (myfile.is_open())
{
while ( myfile.good() )
{
std::string line;
getline (myfile,line);
process_input(line, true); // true because we are loading.
}
myfile.close();
}
}
void save(char* const file) {
std::ofstream saveFile;
saveFile.open(file);
for (unsigned int i=0; i < commandList.size(); i++) {
if(commandList[i].compare(SAVE))
saveFile << commandList[i] << std::endl;
}
saveFile.close();
}
std::string input_filter(std::string str){
std::istringstream iss(str);
std::string ret ="";
std::string word;
while (iss >> word) {
bool contains = false;
for(unsigned int i = 0; i < filterList.size(); i++){
if(word==filterList[i]) contains = true;
}
if(!contains) ret += word + " ";
}
return ret;
}
void read_filter_list(const char* file){
std::ifstream myfile (file);
if (myfile.is_open())
{
while ( myfile.good() )
{
std::string line;
getline (myfile,line);
filterList.push_back(line);
}
myfile.close();
}else{
std::cout << IGNORELISTERROR << std::endl;
}
}
/**
The main method of the program.
@param[in] argc The number of command line args.
@param[in] argv Char* array of command line args.
@return An integer.
*/
int main(int argc, char** argv) {
std::string userinput;
if(argc == 1) {
std::cout << "Usage:\n txtgame input-file [save-file]" << std::endl;
} else if (strstr(argv[1], "-exec") && argc == 5) {
world = read_file(argv[2], world);
if(world != NULL) {
load(argv[3]);
external_output(argv[4]);
save(argv[3]);
}
} else if(argc > 1) {
do {
world = read_file(argv[1], world);
if(world != NULL) {
//Debug Only
// print_world_tree();
const char *file_f = (IGNORELIST);
read_filter_list(file_f);
if (argc > 2)
load(argv[2]);
gameloop();
delete world;
std::cout << "Would you like to play again? (please enter yes for affirmative)" << std::endl;
std::getline (std::cin,userinput);
game_over = true;
if(!userinput.compare("yes") || !userinput.compare("y")) {
game_over = false;
}
}
else {
game_over = true;
}
} while (!game_over);
}
return 0;
}