Mirai's Miscellaneous Misadventures

M40 / core / behaviors.c

1// copyright 2023 zamfofex
2// license: AGPLv3 or later
3
4#include <mimimi/behaviors.h>
5#include <mimimi/compound-behaviors.h>
6#include <mimimi/chapters.h>
7#include <mimimi/allocators.h>
8
9struct mimimi_compound_behavior
10{
11	struct mimimi_allocator *allocator;
12	struct mimimi_behavior *behavior;
13	int count;
14	struct mimimi_behavior **behaviors;
15};
16
17static void mimimi_compound_behave(void *data)
18{
19	struct mimimi_compound_behavior *compound = data;
20	for (int i = 0 ; i < compound->count ; i++)
21	{
22		struct mimimi_behavior *behavior = compound->behaviors[i];
23		(*behavior->behave)(behavior->data);
24	}
25}
26
27static void mimimi_compound_finish(void *data)
28{
29	struct mimimi_compound_behavior *compound = data;
30	for (int i = 0 ; i < compound->count ; i++)
31	{
32		struct mimimi_behavior *behavior = compound->behaviors[i];
33		(*behavior->finish)(behavior->data);
34	}
35	
36	if (compound->count != 0)
37		mimimi_deallocate(compound->allocator, compound->behaviors);
38	
39	mimimi_deallocate(compound->allocator, compound->behavior);
40	mimimi_deallocate(compound->allocator, compound);
41}
42
43struct mimimi_behavior *mimimi_compound_behavior(struct mimimi_allocator *allocator)
44{
45	struct mimimi_compound_behavior *data = mimimi_allocate(allocator, sizeof *data);
46	
47	data->allocator = allocator;
48	data->behavior = mimimi_allocate(allocator, sizeof *data->behavior);
49	data->behavior->behave = &mimimi_compound_behave;
50	data->behavior->finish = &mimimi_compound_finish;
51	data->behavior->data = data;
52	
53	data->count = 0;
54	data->behaviors = allocator->null;
55	
56	return data->behavior;
57}
58
59void mimimi_compound(struct mimimi_behavior *behavior, struct mimimi_behavior *other)
60{
61	struct mimimi_compound_behavior *compound = behavior->data;
62	
63	compound->count++;
64	compound->behaviors = mimimi_reallocate(compound->allocator, compound->behaviors, compound->count * sizeof *compound->behaviors);
65	compound->behaviors[compound->count - 1] = other;
66}
67
68void mimimi_compound_remove(struct mimimi_behavior *behavior, struct mimimi_behavior *other)
69{
70	struct mimimi_compound_behavior *compound = behavior->data;
71	for (int i = 0 ; i < compound->count ; i++)
72	{
73		struct mimimi_behavior *behavior = compound->behaviors[i];
74		if (behavior == other)
75		{
76			compound->count--;
77			for (int j = i ; j < compound->count ; j++)
78				compound->behaviors[j] = compound->behaviors[j + 1];
79			
80			if (compound->count == 0)
81				mimimi_deallocate(compound->allocator, compound->behaviors);
82			else
83				compound->behaviors = mimimi_reallocate(compound->allocator, compound->behaviors, compound->count * sizeof *compound->behaviors);
84			
85			break;
86		}
87	}
88}
89
90unsigned char mimimi_compound_empty(struct mimimi_behavior *behavior)
91{
92	struct mimimi_compound_behavior *compound = behavior->data;
93	if (compound->count == 0) return 1;
94	else return 0;
95}
96
97static void mimimi_ignore(void *data) { (void) data; }
98
99static struct mimimi_behavior mimimi_noop_value = {&mimimi_ignore, &mimimi_ignore, 0};
100struct mimimi_behavior *mimimi_noop = &mimimi_noop_value;
101
102struct mimimi_function
103{
104	struct mimimi_allocator *allocator;
105	struct mimimi_behavior *behavior;
106	struct mimimi_behavior *other;
107};
108
109static void mimimi_function_behave(void *data)
110{
111	struct mimimi_function *function = data;
112	(*function->other->behave)(function->other->data);
113}
114
115static void mimimi_function_finish(void *data)
116{
117	struct mimimi_function *function = data;
118	(*function->other->finish)(function->other->data);
119	mimimi_deallocate(function->allocator, function->other);
120	mimimi_deallocate(function->allocator, function->behavior);
121	mimimi_deallocate(function->allocator, function);
122}
123
124struct mimimi_behavior *mimimi_function(void *data, void (*behave)(void *data), struct mimimi_allocator *allocator)
125{
126	struct mimimi_function *function = mimimi_allocate(allocator, sizeof *function);
127	function->allocator = allocator;
128	
129	function->other = mimimi_allocate(allocator, sizeof *function->other);
130	function->other->behave = behave;
131	function->other->finish = &mimimi_ignore;
132	function->other->data = data;
133	
134	function->behavior = mimimi_allocate(allocator, sizeof *function->behavior);
135	function->behavior->behave = &mimimi_function_behave;
136	function->behavior->finish = &mimimi_function_finish;
137	function->behavior->data = function;
138	
139	return function->behavior;
140}
141
142struct mimimi_behavior *mimimi_finalizer(void *data, void (*finish)(void *data), struct mimimi_allocator *allocator)
143{
144	struct mimimi_function *function = mimimi_allocate(allocator, sizeof *function);
145	function->allocator = allocator;
146	
147	function->other = mimimi_allocate(allocator, sizeof *function->other);
148	function->other->behave = &mimimi_ignore;
149	function->other->finish = finish;
150	function->other->data = data;
151	
152	function->behavior = mimimi_allocate(allocator, sizeof *function->behavior);
153	function->behavior->behave = &mimimi_function_behave;
154	function->behavior->finish = &mimimi_function_finish;
155	function->behavior->data = function;
156	
157	return function->behavior;
158}
159
160void *mimimi_compound_allocate(struct mimimi_behavior *compound, unsigned int size, struct mimimi_allocator *allocator)
161{
162	struct mimimi_allocator *compound_allocator = mimimi_compound_allocator(compound, allocator);
163	return mimimi_allocate(compound_allocator, size);
164}
165
166struct mimimi_choice
167{
168	struct mimimi_allocator *allocator;
169	struct mimimi_behavior *behavior;
170	unsigned char *predicate;
171	struct mimimi_behavior *nonzero;
172	struct mimimi_behavior *zero;
173};
174
175static void mimimi_choose(void *data)
176{
177	struct mimimi_choice *choice = data;
178	struct mimimi_behavior *nonzero = choice->nonzero;
179	struct mimimi_behavior *zero = choice->zero;
180	
181	if (*choice->predicate == 0)
182		(*zero->behave)(zero->data);
183	else
184		(*nonzero->behave)(nonzero->data);
185}
186
187static void mimimi_choice_finish(void *data)
188{
189	struct mimimi_choice *choice = data;
190	(*choice->nonzero->finish)(choice->nonzero->data);
191	(*choice->zero->finish)(choice->zero->data);
192	mimimi_deallocate(choice->allocator, choice->behavior);
193	mimimi_deallocate(choice->allocator, choice);
194}
195
196struct mimimi_behavior *mimimi_choice(unsigned char *predicate, struct mimimi_behavior *nonzero, struct mimimi_behavior *zero, struct mimimi_allocator *allocator)
197{
198	struct mimimi_choice *choice = mimimi_allocate(allocator, sizeof *choice);
199	choice->allocator = allocator;
200	
201	choice->predicate = predicate;
202	choice->nonzero = nonzero;
203	choice->zero = zero;
204	
205	choice->behavior = mimimi_allocate(allocator, sizeof *choice->behavior);
206	choice->behavior->behave = &mimimi_choose;
207	choice->behavior->finish = &mimimi_choice_finish;
208	choice->behavior->data = choice;
209	
210	return choice->behavior;
211}
212
213struct mimimi_throttle
214{
215	struct mimimi_allocator *allocator;
216	struct mimimi_behavior *behavior;
217	struct mimimi_behavior *underlying;
218	int time;
219	int countdown;
220};
221
222static void mimimi_throttle_behave(void *data)
223{
224	struct mimimi_throttle *throttle = data;
225	if (throttle->countdown == 0)
226	{
227		throttle->countdown = throttle->time;
228		(*throttle->underlying->behave)(throttle->underlying->data);
229	}
230}
231
232static void mimimi_throttle_finish(void *data)
233{
234	struct mimimi_throttle *throttle = data;
235	(*throttle->underlying->finish)(throttle->underlying->data);
236	mimimi_deallocate(throttle->allocator, throttle->behavior);
237	mimimi_deallocate(throttle->allocator, throttle);
238}
239
240static void mimimi_countdown(void *data)
241{
242	int *n = data;
243	if (*n != 0) n[0]--;
244}
245
246struct mimimi_behavior *mimimi_throttle(struct mimimi_behavior *compound, struct mimimi_behavior *underlying, int time, struct mimimi_allocator *allocator)
247{
248	struct mimimi_throttle *throttle = mimimi_allocate(allocator, sizeof *throttle);
249	throttle->allocator = allocator;
250	
251	throttle->underlying = underlying;
252	throttle->time = time;
253	throttle->countdown = 0;
254	
255	throttle->behavior = mimimi_allocate(allocator, sizeof *throttle->behavior);
256	throttle->behavior->behave = &mimimi_throttle_behave;
257	throttle->behavior->finish = &mimimi_throttle_finish;
258	throttle->behavior->data = throttle;
259	
260	mimimi_compound(compound, mimimi_function(&throttle->countdown, mimimi_countdown, allocator));
261	
262	return throttle->behavior;
263}