commit bfbce31ffc091ffcda30d5b120a3c64476e85647 Author: Rudi Date: Wed Sep 30 01:44:28 2020 +0000 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5cf60c1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +Game +*~ +#* diff --git a/Game.cpp b/Game.cpp new file mode 100644 index 0000000..5088092 --- /dev/null +++ b/Game.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +typedef struct level { + int id; + string title; + string body; + int choiceCount; + vector choiceIDs; + vector choices; +}lvl; + +// Separator for 'csv' +string separator = ",,"; +// Global game_csv is the file to load +string game_csv; + +// load level for int +level GotoLevel(int); + +int main(int argc, char **argv) { + // Refuse to start without at least a game_csv + if (argc != 2) { + game_csv = "/ftp/pub/users/rudi/CSVGame/story.csv"; + } else { + // Set global game_csv + game_csv = argv[1]; + } + // Initialize nextLevel to 0 (load first level) + int nextLevel = 0; + // If user supplied a level + if (argc == 3) { + // override nextLevel with that level + nextLevel = atoi(argv[2]); + } + + // Setup the level from nextLevel + level l = GotoLevel(nextLevel); + while(l.choiceIDs.size() > 0) { + // Display the level info + cout << endl << l.id << "\t" << l.title << endl << endl << l.body << endl <> nextLevel; + } while (!count(l.choiceIDs.begin(), l.choiceIDs.end(), nextLevel)); + l = GotoLevel(nextLevel); + } +} + + +level GotoLevel(int num) { + // Read file, from the global game_csv converted to a C string (const char array) + fstream file(game_csv.c_str()); + // Go to the beginning of the file + file.seekg(ios::beg); + // skip to the line we want + for(int i = 0; i < num; i++) { + // ignore the crap we don't want + file.ignore(numeric_limits::max(),'\n'); + } + // make a new level + level l; + // make a new line + string line; + // read what we have from the file, to the string line + getline(file, line); + + //LevelID,Title,Body,ChoiceCount,ChoiceID1,....ChoiceIDN,Choice1,....ChoiceN + l.id = atoi(line.substr(0, line.find(separator)).c_str()); + line.erase(0, line.find(separator) + separator.length()); + + l.title = line.substr(0, line.find(separator)); + line.erase(0, line.find(separator) + separator.length());; + + l.body = line.substr(0, line.find(separator)); + line.erase(0, line.find(separator) + separator.length()); + + l.choiceCount = atoi(line.substr(0, line.find(separator)).c_str()); + line.erase(0, line.find(separator) + separator.length()); + + for(int i = 0; i < l.choiceCount; i++) { + int choice = atoi(line.substr(0, line.find(separator)).c_str()); + l.choiceIDs.push_back(choice); + line.erase(0, line.find(separator) + separator.length()); + } + for(int i = 0; i < l.choiceCount; i++) { + string choice = line.substr(0, line.find(separator)); + l.choices.push_back(choice); + line.erase(0, line.find(separator) + separator.length()); + } + return l; + + +} diff --git a/stories/first.csv b/stories/first.csv new file mode 100644 index 0000000..d151ee3 --- /dev/null +++ b/stories/first.csv @@ -0,0 +1,3 @@ +0,,The Last Night In Lothlem,,All things in Lothlem were old, and few things in Lothlem were built by Men, for Men were not nearly so old as those who, long ago, came from the Valley to build them. But it was presently Men that filled the city--and strange ones at that--with queer robes and cowls that covered their faces. In shadows they walked, faces downcast in the growing darkness. The streetlights had not yet been lit, and it was because of this that they strode through the city so brazenly, for they were not citizens of Lothlem--at least, not anymore.,,1,,1,,Next Page +1,,A Light in the City,,A single light flickers to life in the belly of the darkening city. You, a boy, stand at the very top of a ladder with a match in hand. At your bidding, a streetlamp sheds its warmth upon the cobblestone lane. You flick out the match and hobble down, watching your feet as you go. You sling your ladder over your shoulder, then go on to the next lamp, and then the next, and then the next after that.,,1,,2,,Next Page +2,,Strange Happenings In Lothlem,,You are the first to see the strange figures at the edge of the growing darkness. Cloaked figures shuffle at the edge of your lamplight on the corner of every lane, dressed in neither the gilded robes of the magister's men, nor the black feathered caps of the watchmen. You recognize none of them.,,2,,3,,4,,Follow the strange figures,,Continue with your work