Mirai's Miscellaneous Misadventures

M29 / test / chapter.c

// copyright 2022 zamfofex
// license: AGPLv3 or later

#include <mimimi/chapters.h>
#include <mimimi/senran-kagura/characters.h>
#include <mimimi/appearances.h>
#include <mimimi/allocators.h>
#include <mimimi/ground.h>
#include <mimimi/compound-behaviors.h>
#include <mimimi/geometry.h>
#include <mimimi/assets.h>
#include <mimimi/engines.h>
#include <mimimi/controls.h>
#include <mimimi/sprites.h>
#include <mimimi/cameras.h>
#include <mimimi/behaviors.h>
#include <mimimi/font.h>
#include <mimimi/fonts.h>
#include <mimimi/animations.h>
#include <mimimi/collisions.h>
#include <mimimi/test.h>

#include "ground.c"

static void mimimi_test_animation_set(struct mimimi_animation_set *set, struct mimimi_allocator *allocator)
{
	int animation_count = 8;
	int image_count = 6;
	int width = 48;
	int height = 48;
	
	int total_animation_count = 3 + animation_count;
	int total_image_count = 2 + image_count * animation_count + image_count;
	
	struct mimimi_animation *animations = mimimi_allocate(allocator, total_animation_count * sizeof *animations);
	struct mimimi_image *images = mimimi_allocate(allocator, total_image_count * sizeof *images);
	
	set->standing = animations;
	set->standing_animation_count = animation_count;
	animations += animation_count;
	
	for (int i = 0 ; i < animation_count ; i++)
	{
		struct mimimi_animation *animation = set->standing + i;
		animation->count = image_count;
		animation->images = images;
		images += image_count;
		
		for (int i = 0 ; i < image_count ; i++)
		{
			struct mimimi_image *image = animation->images + i;
			image->width = width;
			image->height = height;
			image->colors = mimimi_allocate(allocator, width * height);
			for (int i = 0 ; i < width * height ; i++) image->colors[i] = 0;
		}
	}
	
	struct mimimi_animation *knocked = animations++;
	knocked->count = 1;
	knocked->images = images++;
	knocked->images[0].width = width;
	knocked->images[0].height = height;
	knocked->images[0].colors = mimimi_allocate(allocator, width * height);
	for (int i = 0 ; i < width * height ; i++) knocked->images[0].colors[i] = 0;
	
	struct mimimi_animation *falling = animations++;
	falling->count = 1;
	falling->images = images++;
	falling->images[0].width = width;
	falling->images[0].height = height;
	falling->images[0].colors = mimimi_allocate(allocator, width * height);
	for (int i = 0 ; i < width * height ; i++) falling->images[0].colors[i] = 0;
	
	set->knocked = knocked;
	set->falling = falling;
	
	for (int i = 0 ; i < width * height ; i++)
	{
		knocked->images[0].colors[i] = 0;
		falling->images[0].colors[i] = 0;
	}
	
	set->jumping = animations++;
	set->jumping->count = image_count;
	set->jumping->images = images;
	images += image_count;
	
	for (int i = 0 ; i < image_count ; i++)
	{
		struct mimimi_image *image = set->jumping->images + i;
		image->width = width;
		image->height = height;
		image->colors = mimimi_allocate(allocator, width * height);
		for (int i = 0 ; i < width * height ; i++) image->colors[i] = 0;
	}
}

static struct mimimi_appearance *mimimi_test_appearance(struct mimimi_allocator *allocator)
{
	struct mimimi_appearance *appearance = mimimi_allocate(allocator, sizeof *appearance);
	
	mimimi_test_animation_set(&appearance->left, allocator);
	mimimi_test_animation_set(&appearance->right, allocator);
	
	return appearance;
}

struct mimimi_test_sprite
{
	int x, y;
	struct mimimi_model *model;
};

static void mimimi_test_player(struct mimimi_behavior *compound, struct mimimi_chapter *chapter, struct mimimi_sprite *player, struct mimimi_allocator *allocator)
{
	struct mimimi_allocator *compound_allocator = mimimi_compound_allocator(compound, allocator);
	
	struct mimimi_history *history = mimimi_allocate(compound_allocator, sizeof *history);
	mimimi_compound(compound, mimimi_history(history, chapter, allocator));
	
	struct mimimi_jump *jump = mimimi_allocate(compound_allocator, sizeof *jump);
	struct mimimi_behavior *jump_behavior = mimimi_jump(jump, player->physics, compound_allocator);
	
	struct mimimi_behavior *jump_choice = mimimi_choice(&player->physics->airborne, mimimi_noop, jump_behavior, compound_allocator);
	
	struct mimimi_controls *mirai_controls = mimimi_allocate(compound_allocator, sizeof *mirai_controls);
	player->awake_behavior = mimimi_controls(mirai_controls, player->walk, history, compound_allocator);
	mirai_controls->jump = jump_choice;
}

struct mimimi_chapter *mimimi_test(struct mimimi_engine *engine)
{
	struct mimimi_behavior *compound = mimimi_compound_behavior(engine->allocator);
	struct mimimi_allocator *compound_allocator = mimimi_compound_allocator(compound, engine->allocator);
	
	struct mimimi_chapter *chapter = mimimi_allocate(compound_allocator, sizeof *chapter);
	chapter->behavior = compound;
	
	struct mimimi_position *camera = mimimi_allocate(compound_allocator, sizeof *camera);
	
	struct mimimi_test_sprite infos[] =
	{
		{110, 36, mimimi_mirai_flash},
		// {105, 36, mimimi_ryoubi_flash},
		// {100, 36, mimimi_ryouna_flash},
		// {95, 36, mimimi_haruka_flash},
	};
	
	int count = sizeof infos / sizeof *infos;
	
	struct mimimi_position **positions = mimimi_allocate(compound_allocator, count * sizeof *positions);
	mimimi_compound(compound, mimimi_collision(count, positions, 128, 256, 512, 64, engine->allocator));
	
	int *directions = mimimi_allocate(compound_allocator, count * sizeof *directions);
	
	struct mimimi_sprite **sprites = mimimi_allocate(engine->allocator, count * sizeof *sprites);
	
	for (int i = 0 ; i < count ; i++)
	{
		struct mimimi_test_sprite *info = infos + i;
		
		struct mimimi_sprite *sprite = mimimi_sprite(&mimimi_test_ground, info->x * 128, info->y * 128, 80, 250, engine->allocator);
		mimimi_compound(compound, sprite->behavior);
		sprites[i] = sprite;
		
		if (i == 0)
		{
			mimimi_test_player(compound, chapter, sprite, engine->allocator);
			mimimi_compound(compound, mimimi_camera(camera, sprite->position, 0, -512, engine->allocator));
		}
		else
		{
			mimimi_compound(compound, mimimi_stationary(directions + i, &sprites[0]->position->x, &sprites[i]->position->x, engine->allocator));
		}
		
		positions[i] = sprite->position;
	}
	
	struct mimimi_image *background = mimimi_allocate(compound_allocator, sizeof *background);
	background->colors = mimimi_allocate(compound_allocator, mimimi_test_ground.width * 16 * mimimi_test_ground.height * 16);
	
	background->width = mimimi_test_ground.width * 16;
	background->height = mimimi_test_ground.height * 16;
	
	static unsigned char ground_colors[] = {0x0D, 0x05, 0x24, 0x28, 0x92, 0xBC, 0x77};
	
	for (int y = 0 ; y < mimimi_test_ground.height * 16 ; y++)
	for (int x = 0 ; x < mimimi_test_ground.width * 16 ; x++)
	{
		unsigned char color = ground_colors[mimimi_test_ground.tiles[x / 16 + y / 16 * mimimi_test_ground.width]];
		background->colors[x + y * mimimi_test_ground.width * 16] = color;
	}
	
	void *background_texture = (*engine->texture)(engine->data, background);
	mimimi_compound(compound, mimimi_background(engine, camera, background_texture, 0, engine->allocator));
	
	for (int i = 0 ; i < count ; i++)
	{
		struct mimimi_test_sprite *info = infos + i;
		struct mimimi_sprite *sprite = sprites[i];
		
		struct mimimi_appearance *appearance = mimimi_test_appearance(compound_allocator);
		mimimi_appearance(appearance, info->model, 24, 48, engine->allocator);
		
		struct mimimi_video_appearance *video_appearance = mimimi_allocate(compound_allocator, sizeof *video_appearance);
		mimimi_video_appearance(video_appearance, appearance, engine, compound_allocator);
		
		mimimi_compound(compound, mimimi_display(engine, camera, sprite, video_appearance, directions + i, 24, 48, engine->allocator));
	}
	
	mimimi_deallocate(engine->allocator, sprites);
	
	struct mimimi_image *overlay = mimimi_allocate(compound_allocator, sizeof *overlay);
	overlay->width = 256;
	overlay->height = 64;
	overlay->colors = mimimi_allocate(compound_allocator, overlay->width * overlay->height);
	
	for (int i = 0 ; i < overlay->width * overlay->height ; i++)
		overlay->colors[i] = 0;
	
	struct mimimi_image *glyphs = mimimi_allocate(engine->allocator, mimimi_font_image_count * sizeof *glyphs);
	unsigned char *colors = mimimi_allocate(engine->allocator, mimimi_font_color_count);
	
	for (int i = 0 ; i < mimimi_font_image_count ; i++)
		glyphs[i].colors = colors + i * mimimi_font_image_color_count;
	
	mimimi_glyphs(glyphs, mimimi_font, 0x76);
	
	mimimi_text(overlay, glyphs, 16, 16, "Mirai's Miscellaneous Misadventures M29");
	mimimi_text(overlay, glyphs, 16, 32, "a Senran Kagura fangame, August 7 of 2022");
	
	mimimi_deallocate(engine->allocator, colors);
	mimimi_deallocate(engine->allocator, glyphs);
	
	void *overlay_texture = (*engine->texture)(engine->data, overlay);
	mimimi_compound(compound, mimimi_overlay(engine, overlay_texture, 0, 0, engine->allocator));
	
	return chapter;
}