-
Notifications
You must be signed in to change notification settings - Fork 1
/
GrammarState.gd
187 lines (132 loc) · 3.52 KB
/
GrammarState.gd
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
@tool
extends Resource
class_name GrammarState
var rng = RandomNumberGenerator.new()
var gm = GrammarMesh.new()
# Grammar configuration
@export var grammar : Grammar:
set(gr):
gm.set_grammar_state(self)
grammar = gr
clear_state()
if gr != null:
initial_symbol = 0
self.notify_property_list_changed()
var default_seed = "".num_uint64(rng.randi_range(0, 0x7FFFFFFFFFFF), 16, true)
@export var seed : String = default_seed:
set(s):
var u64 = s.substr(0, 12).hex_to_int()
rng.seed = u64
if auto_generate:
self.generate_all()
get:
var str = "".num_uint64(rng.seed, 16, true).substr(0, 12)
return str
@export var generations : int = 0:
set(nog):
if nog < 0:
nog = 0
generations = nog
if auto_generate:
self.generate_all()
@export var auto_generate : bool = true
var initial_symbol : int:
set(i):
initial_symbol = i
if grammar != null:
_initial_symbol2 = grammar.from_enum(i)
var initial_symbol_override : String:
set(str):
initial_symbol_override = str
if grammar != null and grammar.has(str):
_initial_symbol2 = str
else:
_initial_symbol2 = grammar.from_enum(initial_symbol)
var _initial_symbol2 : String:
set(str):
_initial_symbol2 = str
initial_shape = grammar.get_shape(str)
if auto_generate:
self.generate_all()
var initial_shape : GrammarShape
# State
var shapes : Array[GrammarShape] = []
var terminals : Array[GrammarShape] = []
var generation_count = -1
func _get_property_list():
var properties = []
var property_usage = PROPERTY_USAGE_NO_EDITOR
var hint_string = ""
if self.grammar != null:
property_usage = PROPERTY_USAGE_DEFAULT
hint_string = self.grammar.get_hints()
properties = []
properties.append({
"name": "initial_symbol",
"type": TYPE_INT,
"usage": property_usage,
"hint": PROPERTY_HINT_ENUM,
"hint_string": hint_string
})
return properties
func _add_terminal(terminal_shape):
self.terminals.push_back(terminal_shape)
func _clear_terminals():
self._terminals = []
func clear_state():
self.shapes = []
self.terminals = []
self.generation_count = -1
self.emit_changed()
func start():
if self.grammar != null and initial_shape != null:
self.shapes = [initial_shape]
self.terminals = []
self.generation_count = 0
self.rng.state = 0
return false
else:
self.clear_state()
return true
func next_generation_no_signal():
if self.generation_count == -1:
if self.start():
return
if self.shapes.size() == 0:
return
var old_shape_names = []
var new_shape_names = []
var new_shapes = []
for shape in self.shapes:
old_shape_names.append(shape.symbol.text)
# Get the rule
var grammar_rule = shape.symbol.select_rule(self.rng)
# No rules, keep the shape
if grammar_rule == null:
new_shapes.append(shape)
else:
var shapes = grammar_rule.fulfill(shape)
new_shapes.append_array(shapes)
# Split the new array between shapes and terminals
self.shapes = []
for shape in new_shapes:
new_shape_names.append(shape.symbol.text)
if shape.symbol.is_terminal():
self._add_terminal(shape)
else:
self.shapes.push_back(shape)
self.generation_count += 1
func next_generation():
self.next_generation_no_signal()
self.emit_changed()
func generate_all():
self.nth_gen(self.generations)
func nth_gen(n=self.generations):
self.start()
for i in n:
self.next_generation_no_signal()
self.emit_changed()
func total_size():
return self.shapes.size() + self.terminals.size()
func get_grammar_mesh() -> GrammarMesh:
return self.gm