Mirai's Miscellaneous Misadventures

M46 / core / controls.c

/* license: AGPLv3 or later */
/* copyright 2023 zamfofex */

#include <mimimi.h>

void mimimi_history_tick(struct mimimi_history *history, unsigned char left, unsigned char right)
{
	history->left <<= 1;
	history->right <<= 1;
	
	history->left &= 0xFFFF;
	history->right &= 0xFFFF;
	
	if (left != 0) history->left |= 1;
	if (right != 0) history->right |= 1;
}

static int mimimi_count_oscillations(unsigned int history)
{
	int count, i;
	unsigned char prev;
	
	count = 0;
	prev = history & 1;
	for (i = 0 ; i < 16 ; i++)
	{
		if ((history & 1) != prev) count++;
		prev = history & 1;
		history >>= 1;
	}
	
	return count;
}

void mimimi_controls_tick(struct mimimi_walk *walk, struct mimimi_history *history, void (*jump)(void *data), void *data)
{
	int left_oscillations, right_oscillations;
	
	left_oscillations = mimimi_count_oscillations(history->left);
	right_oscillations = mimimi_count_oscillations(history->right);
	
	switch (walk->direction)
	{
	case 0:
		if ((history->left & 1) != 0)
		{
			walk->direction = 1;
			if (left_oscillations > 1 && (history->left&2) == 0)
				(*jump)(data);
		}
		if ((history->right & 1) != 0)
		{
			walk->direction = 2;
			if (right_oscillations > 1 && (history->right&2) == 0)
				(*jump)(data);
		}
		break;
	case 1:
		if ((history->left & 1) == 0)
			walk->direction = 0;
		break;
	case 2:
		if ((history->right & 1) == 0)
			walk->direction = 0;
		break;
	}
}