Mirai's Miscellaneous Misadventures

M55 / chapter-I / main.c

1/* license: AGPLv3 or later */
2/* copyright 2024 zamfofex */
3
4#include <stdlib.h>
5#include "chapter.h"
6
7struct mimimi_chapter_I {
8	unsigned char colors[1024 * 512];
9	struct mimimi_history history;
10	int progression;
11	struct mimimi_toast_dialogue tutorial, dialogue;
12	struct mimimi_stage stage;
13};
14
15static void mimimi_chapter_I_background(struct mimimi_chapter_I *chapter)
16{
17	int i;
18	struct mimimi_area *area;
19	struct mimimi_image *image;
20	
21	for (i = 0 ; i < chapter->stage.area_count ; i++) {
22		area = chapter->stage.areas + i;
23		area->background_count = 1;
24		area->backgrounds = malloc(sizeof *area->backgrounds);
25		if (area->backgrounds == NULL) exit(1);
26		mimimi_background(&area->backgrounds[0].image, area->ground);
27		area->backgrounds[0].x = 0;
28		area->backgrounds[0].y = 0;
29		area->backgrounds[0].z = 0;
30	}
31	
32	area = chapter->stage.areas;
33	
34	image = &area->backgrounds[0].image;
35	free(image->colors);
36	image->width = mimimi_chapter_I_background_width;
37	image->height = mimimi_chapter_I_background_height;
38	image->colors = malloc(image->width * image->height);
39	if (image->colors == NULL) exit(1);
40	mimimi_unpack_bits(image->width * image->height, image->colors, mimimi_chapter_I_background_packed);
41	area->backgrounds[0].x = -88;
42	area->backgrounds[0].y = 0;
43	area->backgrounds[0].z = 0;
44	
45	area->foregrounds = malloc(sizeof *area->foregrounds);
46	if (area->foregrounds == NULL) exit(1);
47	
48	image = &area->foregrounds[0].image;
49	image->width = mimimi_chapter_I_foreground_width;
50	image->height = mimimi_chapter_I_foreground_height;
51	image->colors = malloc(image->width * image->height);
52	if (image->colors == NULL) exit(1);
53	mimimi_unpack_bits(image->width * image->height, image->colors, mimimi_chapter_I_foreground_packed);
54	area->foreground_count = 1;
55	area->foregrounds[0].x = 0;
56	area->foregrounds[0].y = 0;
57	area->foregrounds[0].z = 0;
58}
59
60static void mimimi_chapter_I_size(struct mimimi_image *image)
61{
62	int width, height;
63	
64	if (image->width / image->height < 2) {
65		height = 256;
66		width = height * image->width / image->height;
67	}
68	else {
69		width = 512;
70		height = width * image->height / image->width;
71	}
72	
73	if (width < 256) {
74		width = 256;
75		height = width * image->height / image->width;
76		if (height > 384) height = 384;
77	}
78	if (height < 192) {
79		height = 192;
80		width = height * image->width / image->height;
81		if (width > 512) width = 512;
82	}
83	
84	image->width = width;
85	image->height = height;
86}
87
88static void mimimi_chapter_I_tick(void *data, struct mimimi_input *input)
89{
90	struct mimimi_chapter_I *chapter;
91	struct mimimi_sprite *player;
92	struct mimimi_stage_jump jump;
93	
94	chapter = data;
95	player = chapter->stage.sprites;
96	
97	mimimi_chapter_I_size(&input->image);
98	input->image.colors = chapter->colors;
99	
100	jump.stage = &chapter->stage;
101	jump.sprite = player;
102	
103	mimimi_history_tick(&chapter->history, input->left, input->right);
104	mimimi_controls_tick(&player->walk, &chapter->history, &mimimi_stage_jump, &jump);
105	mimimi_stage_tick(&chapter->stage, input);
106	
107	if (chapter->progression == 0) {
108		if (chapter->stage.area_index == 1) chapter->progression++;
109	}
110	if (chapter->progression == 1) {
111		if (mimimi_toast_dialogue_tick(&chapter->tutorial, &input->image) != 0) chapter->progression++;
112	}
113	if (chapter->progression == 2) {
114		if (chapter->stage.area_index == chapter->stage.area_count - 1) chapter->progression++;
115	}
116	if (chapter->progression == 3) {
117		if (mimimi_toast_dialogue_tick(&chapter->dialogue, &input->image) != 0) chapter->progression++;
118	}
119	if (chapter->progression == 4) {
120		mimimi_play_sound(&chapter->stage, &mimimi_chapter_I_music);
121		chapter->progression++;
122	}
123}
124
125int main(void)
126{
127	static struct mimimi_ground ground0 = {4, 3, {0, 0x6B}, {0}, {0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0}};
128	static struct mimimi_ground ground1 = {5, 3, {0, 8}, {0}, {0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0}};
129	static struct mimimi_dialogue_paragraph paragraphs[] = {{NULL, "Mirai", "There seems to be nothing for me to do. Hmmm... Maybe I can find something to do later."}, {NULL, "Ryouna", "Hear that? If you come back later, then maybe you can find something to do!"}, {NULL, "Ryoubi", "Who are you talking to? Are you stupid or something?"}, {NULL, "Ryouna", "Aaah! Ryoubi, I really like when you yell insults at me!"}, {NULL, "Mirai", "Wha-? What are you two doing here?"}, {NULL, "Haruka", "Hehe, don't worry about it!"}};
130	static struct mimimi_dialogue_paragraph tutorial = {NULL, "Haruka", "Press a direction twice to jump!"};
131	
132	struct mimimi_dialogue dialogue;
133	struct mimimi_chapter_I *chapter;
134	struct mimimi_platform_set *platforms;
135	struct mimimi_image *image;
136	int count;
137	
138	tutorial.model = mimimi_haruka_flash;
139	paragraphs[0].model = mimimi_mirai_flash;
140	paragraphs[1].model = mimimi_ryouna_flash;
141	paragraphs[2].model = mimimi_ryoubi_flash;
142	paragraphs[3].model = mimimi_ryouna_flash;
143	paragraphs[4].model = mimimi_mirai_flash;
144	paragraphs[5].model = mimimi_haruka_flash;
145	
146	chapter = malloc(sizeof *chapter);
147	if (chapter == NULL) exit(1);
148	
149	count = 0;
150	while (mimimi_chapter_I_stage[count]->width != 0) count++;
151	
152	mimimi_stage(&chapter->stage, mimimi_chapter_I_stage, count);
153	mimimi_chapter_I_background(chapter);
154	
155	chapter->progression = 0;
156	
157	dialogue.count = 1;
158	dialogue.paragraphs = &tutorial;
159	mimimi_toast_dialogue(&chapter->tutorial, &dialogue);
160	
161	dialogue.count = sizeof paragraphs / sizeof *paragraphs;
162	dialogue.paragraphs = paragraphs;
163	mimimi_toast_dialogue(&chapter->dialogue, &dialogue);
164	
165	mimimi_spawn(&chapter->stage, mimimi_mirai_flash, 0, 0, 80, 256);
166	
167	image = malloc(sizeof *image);
168	if (image == NULL) return 1;
169	
170	mimimi_background(image, &ground0);
171	
172	platforms = mimimi_add_platform_set(&chapter->stage);
173	platforms->angular_weight = 512;
174	platforms->strength = 128;
175	
176	mimimi_spawn_angular_platform(&chapter->stage, platforms, &ground0, image, -0x5800, -0x1C80, -80, 256, 1024);
177	mimimi_spawn_angular_platform(&chapter->stage, platforms, &ground0, image, -0x5800, -0x1C80, -80, 256, 2048);
178	mimimi_spawn_angular_platform(&chapter->stage, platforms, &ground0, image, -0x5800, -0x1C80, -80, 256, 3072);
179	
180	platforms = mimimi_add_platform_set(&chapter->stage);
181	platforms->angular_weight = 512;
182	platforms->strength = 128;
183	
184	mimimi_spawn_angular_platform(&chapter->stage, platforms, &ground0, image, -0x6500, -0x2100, 80, 256, 1024);
185	mimimi_spawn_angular_platform(&chapter->stage, platforms, &ground0, image, -0x6500, -0x2100, 80, 256, 2048);
186	mimimi_spawn_angular_platform(&chapter->stage, platforms, &ground0, image, -0x6500, -0x2100, 80, 256, 3072);
187	
188	image = malloc(sizeof *image);
189	if (image == NULL) return 1;
190	
191	mimimi_background(image, &ground1);
192	
193	platforms = mimimi_add_platform_set(&chapter->stage);
194	platforms->strength = 0;
195	platforms->min = 0;
196	platforms->max = 1024;
197	
198	mimimi_spawn_linear_platform(&chapter->stage, platforms, &ground1, image, -0x4B00, -0x700, 256);
199	mimimi_spawn_linear_platform(&chapter->stage, platforms, &ground1, image, -0x4800, -0x800, -256);
200	mimimi_spawn_linear_platform(&chapter->stage, platforms, &ground1, image, -0x4F00, -0xC00, 256);
201	mimimi_spawn_linear_platform(&chapter->stage, platforms, &ground1, image, -0x4F00, -0xD00, -256);
202	
203	chapter->history.left = 0;
204	chapter->history.right = 0;
205	
206	mimimi_main(&mimimi_chapter_I_tick, chapter);
207	return 1;
208}