Mirai's Miscellaneous Misadventures

M55 / core / controls.c

1/* license: AGPLv3 or later */
2/* copyright 2024 zamfofex */
3
4#include "../mimimi.h"
5
6void mimimi_history_tick(struct mimimi_history *history, unsigned char left, unsigned char right)
7{
8	history->left <<= 1;
9	history->right <<= 1;
10	
11	history->left &= 0xFF;
12	history->right &= 0xFF;
13	
14	if (left != 0) history->left |= 1;
15	if (right != 0) history->right |= 1;
16}
17
18static int mimimi_count_oscillations(unsigned int history)
19{
20	int count, i;
21	unsigned char prev;
22	
23	count = 0;
24	prev = history & 1;
25	for (i = 0 ; i < 16 ; i++) {
26		if ((history & 1) != prev) count++;
27		prev = history & 1;
28		history >>= 1;
29	}
30	
31	return count;
32}
33
34void mimimi_controls_tick(struct mimimi_walk *walk, struct mimimi_history *history, void (*jump)(void *data), void *data)
35{
36	int left_oscillations, right_oscillations;
37	
38	left_oscillations = mimimi_count_oscillations(history->left);
39	right_oscillations = mimimi_count_oscillations(history->right);
40	
41	switch (walk->direction) {
42	case 0:
43		if ((history->right & 1) != 0) {
44			walk->direction = 2;
45			if (right_oscillations > 1 && (history->right & 2) == 0) (*jump)(data);
46		}
47		if ((history->left & 1) != 0) {
48			walk->direction = 1;
49			if (left_oscillations > 1 && (history->left & 2) == 0) (*jump)(data);
50		}
51		break;
52	case 1:
53		if ((history->left & 1) == 0) walk->direction = 0;
54		break;
55	case 2:
56		if ((history->right & 1) == 0) walk->direction = 0;
57		break;
58	}
59}