-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
194 lines (169 loc) · 8.18 KB
/
Copy pathmain.cpp
File metadata and controls
194 lines (169 loc) · 8.18 KB
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
#include<iostream>
#include<vector>
#include "play.h"
#include "parsing.h"
#include "edit.h"
#include "files.h"
#include "logging.h"
#include "platform_utils.h"
using std::cout;
using std::getline;
vector<string> mainMenuOptions = {"Play", "New", "Edit", "Open", "Exit"};
int main(int argc, char* argv[]) {
// Initialize the logging system
if (!initLog("application.log")) {
std::cerr << "Failed to initialize logging system!" << std::endl;
return 1;
}
dbg(LOG_NOTE, "Logging initialised...");
dbg(LOG_NOTE, "BrickStoryEngine, www.timopollarini.nl/bse");
dbg(LOG_NOTE, "VERSION 1.2");
// annoying thingymabob to get the enviroment var working
bool programActive = true;
string homeDir = PlatformUtils::getDocumentsPath();
dbg(LOG_NOTE, "Home directory detected as: " + homeDir);
string mode = "Main Menu";
if (argc > 2 && argv[1][0] != '-') {
dbg(LOG_NOTE, std::string("Opened BSE with .story file: ") + argv[1]);
mode = "Play";
} else if (argc != 1) { cerr << "Invalid cl arguments amount" << endl; }
string storyPath;
vector<string> lines;
if (argc == 2) {
storyPath = argv[1];
lines = readFile(storyPath);
}
if (storyPath.empty()) { dbg(LOG_ERROR, "NO FILE OPEN"); }
vector<Situation> story = parseStoryFile(lines);
while (programActive) {
dbg(LOG_NOTE, "Main loop interation, current mode: " + mode);
if (mode == "Main Menu") {
size_t selectedIndex = showOptions(mainMenuOptions, "BrickStoryEngine");
if (selectedIndex == mainMenuOptions.size() - 1) {
programActive = false;
}
else if (selectedIndex == 0) {
mode = "Play";
}
else if (selectedIndex == 1) {
mode = "New";
}
else if (selectedIndex == 2) {
mode = "Edit";
}
else if (selectedIndex == 3) {
mode = "Open";
}
if (mode != "Main Menu") { dbg(LOG_NOTE, "User chose " + mode + " from main menu."); }
} else if (mode == "Play") {
// check if a file is open
if (storyPath.empty()) { selectNewFile(storyPath, lines, story, homeDir); }
// Play currently parsed story
playGame(story);
mode = "Main Menu";
} else if (mode == "New") {
// New story, ask for first sit.
story = {};
newSit(story);
mode = "Edit";
} else if (mode == "Open") {
// Open file selection menu
selectNewFile(storyPath, lines, story, homeDir);
// go back to main menu
mode = "Main Menu";
} else if (mode == "Edit") {
// check if a file is open and story is empty
if (storyPath.empty() && story.empty()) { selectNewFile(storyPath, lines, story, homeDir); }
// Edit
size_t selectedSituationIndex = listOptionsPlus("Edit", {"Back", "Play", "Save", "Save As...", "Add situation", "Delete Situation..."}, "sits", story);
if (selectedSituationIndex == 0) { // Back
mode = "Main Menu";
continue;
} else if (selectedSituationIndex == 1) { // Play
playGame(story);
continue;
} else if (selectedSituationIndex == 2) { // Save
dbg(LOG_NOTE, "Saving to " + storyPath + "...");
vector<string> strs = sitsToFileStrings(story);
writeFile(strs, storyPath);
continue;
} else if (selectedSituationIndex == 3) { // Save As...
cout << "\033[2J\033[H"; // clear screen
cout << "Save as: ";
string name; cin >> name;
dbg(LOG_NOTE, "Save As user input: " + name);
vector<string> strs = sitsToFileStrings(story);
writeFile(strs, name, true);
continue;
} else if (selectedSituationIndex == 4) { // Add situation selected
newSit(story);
continue;
} else if (selectedSituationIndex == 5) { // Delete Situation
dbg(LOG_NOTE, "User selected Delete Situation.");
size_t sitIndexToDelete = showOptions(getSitsShortname(story), "Select Situation to Delete:");
story.erase(story.begin() + sitIndexToDelete);
continue;
}
// This code runs when a Situation has been selected (not Back, play, save or New Situation)
selectedSituationIndex -= 6; // adjust index for list of situations
// per situation code
size_t selectedOptionIndex = listOptionsPlus(story[selectedSituationIndex].title, {"Back", "Edit Title", "New option", "Delete Option..."}, "opts", story, selectedSituationIndex);
if (selectedOptionIndex == 0) {
continue; // Back (will go back to while programActive, and mode is still Edit so it will show the Edit Menu)
} else if (selectedOptionIndex == 1) { // edit title
cout << "\033[2J\033[H"; // clear screen
cout << "Enter new title (was \"" << story[selectedSituationIndex].title << "\"): ";
cout.flush();
string newTitle;
getline(cin, newTitle);
if (newTitle.empty()) { continue; }
story[selectedSituationIndex].title = newTitle;
continue;
} else if (selectedOptionIndex == 2) { // New Option
cout << "\033[2J\033[H"; // clear screen
cout << "New option text: ";
string newOptionText;
getline(cin, newOptionText);
// Show situation selection menu
vector<string> situationNames = getSitsShortname(story);
situationNames.insert(situationNames.begin(), "0: END GAME");
size_t referSituationIndex = showOptions(situationNames, "Select situation to refer to:");
size_t referSituationNum;
if (referSituationIndex == 0) { referSituationNum = 0; } else {
referSituationNum = story[referSituationIndex - 1].situationNum;
}
story[selectedSituationIndex].options.push_back({newOptionText, referSituationNum});
continue;
} else if (selectedOptionIndex == 3) { // Delete Option
dbg(LOG_NOTE, "User selected Delete Option.");
cout << "\033[2J\033[H"; // clear screen
size_t optionIndexToDelete = showOptions(getOptsShortname(story[selectedSituationIndex]), "Select Option to Delete:");
dbg(LOG_NOTE, "Deleting option: " + story[selectedSituationIndex].options[optionIndexToDelete].optionText);
story[selectedSituationIndex].options.erase(story[selectedSituationIndex].options.begin() + optionIndexToDelete);
continue;
}
selectedOptionIndex -= 4; // to compensate for the previous options
// option selected, edit it
cout << "\033[2J\033[H"; // clear screen
cout << "New option text (was \"" << story[selectedSituationIndex].options[selectedOptionIndex].optionText << "\"): ";
string newOptionText;
getline(cin, newOptionText);
if (newOptionText.empty()) { newOptionText = story[selectedSituationIndex].options[selectedOptionIndex].optionText; }
// Show situation selection menu
vector<string> situationNames = getSitsShortname(story);
situationNames.insert(situationNames.begin(), "0: END GAME");
size_t referSituationIndex = showOptions(situationNames, "Select situation to refer to:");
size_t referSituationNum;
if (referSituationIndex == 0) {
referSituationNum = 0;
} else {
referSituationNum = story[referSituationIndex - 1].situationNum;
}
story[selectedSituationIndex].options[selectedOptionIndex] = {newOptionText, referSituationNum};
}
}
// Close the logging system
dbg(LOG_NOTE, "Application Shutting Down From Loop.");
closeLog();
return 0;
}