Use a set for story options

This commit is contained in:
Dane Johnson 2021-12-26 17:01:00 -06:00
parent b30463dfd2
commit e94e43f547
3 changed files with 20 additions and 6 deletions

View File

@ -2,6 +2,7 @@
#define CYOA_H
#include <vector>
#include <set>
#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<Choice*, ChoiceComparator> ChoiceList;
enum class FooterType {
End,
Goto,
@ -36,7 +46,7 @@ enum class FooterType {
struct Footer {
FooterType type;
union {
std::vector<Choice*> *choices;
ChoiceList *choices;
char *link;
};

View File

@ -16,7 +16,7 @@ std::vector<Page*> pages;
Page *emit_ending();
Page *emit_goto(char*);
Page *emit_choices(std::vector<Choice*>*);
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<Choice*> *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<Choice*>(); $$.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<Choice*> *choices) {
Page *emit_choices(ChoiceList *choices) {
Page *mypage = new Page;
mypage->footer.type = FooterType::Choices;
mypage->footer.choices = choices;

View File

@ -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
}