diff --git a/src/cyoa.h b/src/cyoa.h index e86f7a1..c9cdff9 100644 --- a/src/cyoa.h +++ b/src/cyoa.h @@ -2,6 +2,7 @@ #define CYOA_H #include +#include #define STATCHECK_GT 1 #define STATCHECK_LT 2 @@ -27,6 +28,15 @@ struct Choice { Choice(char*, statcheck_t *statcheck, statchange_t *statchange); }; +struct ChoiceComparator { + bool operator()(const Choice* a, const Choice* b) const { + return a->option < b->option; + } +}; + +typedef std::set ChoiceList; + + enum class FooterType { End, Goto, @@ -36,7 +46,7 @@ enum class FooterType { struct Footer { FooterType type; union { - std::vector *choices; + ChoiceList *choices; char *link; }; diff --git a/src/cyoa.leg b/src/cyoa.leg index 4e0f249..b934fc7 100644 --- a/src/cyoa.leg +++ b/src/cyoa.leg @@ -16,7 +16,7 @@ std::vector pages; Page *emit_ending(); Page *emit_goto(char*); -Page *emit_choices(std::vector*); +Page *emit_choices(ChoiceList*); void append_body(Page *page, char *str); @@ -24,7 +24,7 @@ union value { Page *page; char *string; Choice *choice; - std::vector *choices; + ChoiceList *choices; int num; statcheck_t *statcheck; statchange_t *statchange; @@ -57,8 +57,8 @@ Goto = 'GOTO' - < i:Identifier > Newline Ending = 'THE END' Newline -ChoiceList = c:Choice l:ChoiceList { $$ = l; $$.choices->push_back(c.choice); } - | c:Choice { $$.choices = new std::vector(); $$.choices->push_back(c.choice); } +ChoiceList = c:Choice l:ChoiceList { $$ = l; $$.choices->insert(c.choice); } + | c:Choice { $$.choices = new ChoiceList(); $$.choices->insert(c.choice); } Choice = b:Bullet f:Freetext r:Redirect { @@ -135,7 +135,7 @@ Page *emit_ending() { return mypage; } -Page *emit_choices(std::vector *choices) { +Page *emit_choices(ChoiceList *choices) { Page *mypage = new Page; mypage->footer.type = FooterType::Choices; mypage->footer.choices = choices; diff --git a/src/storybook.cpp b/src/storybook.cpp index 98f8277..87ca76f 100644 --- a/src/storybook.cpp +++ b/src/storybook.cpp @@ -62,6 +62,10 @@ void Storybook::Play() { printf("Press ENTER to continue..."); std::cin.get(); break; + case FooterType::Choices: + for (Choice *c : *current->footer.choices) { + printf("%d) %s\n", c->option, c->flavor); + } default: IsEnded = true; // TODO }