-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadbare.h
More file actions
133 lines (121 loc) · 4.07 KB
/
Copy paththreadbare.h
File metadata and controls
133 lines (121 loc) · 4.07 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
#ifndef THREADBARE_H
#define THREADBARE_H
#include <bn_array.h>
#include <bn_string.h>
#include <bn_sstream.h>
#include <bn_vector.h>
#include <bn_bitset.h>
#include "script.yarn.h"
#include "tb_variables.h"
namespace ThreadBare {
using os = bn::ostringstream &;
enum TBState : int {Line, Options, Timer, Paused, Off, Working};
enum VisitedNodeKey: int{};
class TBScriptRunner;
union AttributeParameterValue {int i; const char* str; bool b;
AttributeParameterValue(int val) : i(val) {}
AttributeParameterValue(const char* s) : str(s) {}
AttributeParameterValue(bool val) : b(val) {}
};
class Markup {
public:
bn::vector<LineTag, MAX_TAGS_COUNT> tags;
bn::vector<int, MAX_TAG_PARAMS_COUNT> tagParams;
bn::vector<Attribute, MAX_ATTRIBUTES_COUNT> attributes;
bn::vector<int, MAX_ATTRIBUTES_COUNT> attributePositions;
bn::vector<AttributeParameterValue, MAX_ATTRIBUTE_PARAMS_COUNT> attributeParams;
void clear() {
this->tags.clear();
this->tagParams.clear();
this->attributes.clear();
this->attributePositions.clear();
this->attributeParams.clear();
};
};
template<int MaxSize>
class TextBuffer {
protected:
bn::string<MaxSize> text;
bn::ostringstream textStream;
public:
Markup markup;
bool condition = true;
TextBuffer(): textStream(text){};
void StartNewLine() {
text.clear();
markup.clear(); // Can probably optimize away redundant clears if needed
condition = true;
}
auto GetStringView() -> bn::string_view {
return bn::string_view(this->text);
}
template <typename T>
TextBuffer& operator<<(const T& x) {
textStream << x;
return *this;
}
TextBuffer& operator<<(bn::ostringstream& (*manip)(bn::ostringstream&)) {
textStream << manip;
return *this;
}
int length() {
return text.length();
}
};
template<int MaxSize>
class Option : public TextBuffer<MaxSize> {
public:
int nextStep;
Option(int setNextStep): TextBuffer<MaxSize>() {
this->nextStep = setNextStep;
};
Option(int setNextStep, bn::string_view text): TextBuffer<MaxSize>() {
this->nextStep = setNextStep;
this->textStream << text;
};
};
class NodeState {
NodeState() = delete;
public:
void (*nodeFn)(TBScriptRunner&, NodeState&);
int nextStep = 0;
bn::vector<NodeTag, MAX_TAGS_COUNT> tags;
bn::vector<int, MAX_TAG_PARAMS_COUNT> tagParams;
NodeState(void (*node)(TBScriptRunner&, NodeState&)){
this->nodeFn = node;
}
void callNodeFn(TBScriptRunner& runner){
BN_ASSERT(this->nodeFn != nullptr, "nodeStateFn is null");
this->nodeFn(runner, *this);
}
};
class TBScriptRunner {
private:
uint8_t onceStorage[ONCE_VARIABLE_COUNT/8];
public:
TBState state = TBState::Off;
int waitTimer = 0;
TextBuffer<LINE_BUFFER_SIZE> currentLine;
bn::vector<Option<OPTION_BUFFER_SIZE>, MAX_OPTIONS_COUNT> options;
TBVariables variables;
bn::vector<NodeState, 4> nodeStates;
bn::bitset_ref visitedNodes;
bn::array<int, VISIT_COUNT_NODE_COUNT> visitCountNodes;
void Jump(void (*node)(TBScriptRunner&, NodeState&));
void Detour(void (*node)(TBScriptRunner&, NodeState&));
void StartTimer(int seconds, int toNextStep = 0 );
void StartTimer(bn::fixed seconds, int toNextStep = 0);
void EndNode();
void Stop();
void FinishLine(int toNextStep);
void ReturnAndGoto(int toNextStep);
void WaitTick();
void ChooseOption(int i);
bool VisitedNode(VisitedNodeName key);
int VisitedCountNode(VisitCountedNodeName key);
void SetVisitedState(VisitedNodeName key);
void IncrementVisitCount(VisitCountedNodeName key);
TBState Execute();
};
}
#endif