Mirai's Miscellaneous Misadventures

M54 / chapter-I / main.c

1/* license: AGPLv3 or later */
2/* copyright 2024 zamfofex */
3
4#include <stdlib.h>
5#include "../mimimi.h"
6#include "ground.h"
7#include "background.h"
8
9struct mimimi_chapter_I {
10	unsigned char colors[1024 * 512];
11	struct mimimi_history history;
12	int progression;
13	struct mimimi_toast_dialogue dialogue0, dialogue1;
14	struct mimimi_toast_dialogue tutorial, dialogue;
15	struct mimimi_stage stage;
16};
17
18static void mimimi_chapter_I_background(struct mimimi_chapter_I *chapter)
19{
20	int i;
21	struct mimimi_area *area;
22	struct mimimi_image *image;
23	
24	for (i = 0 ; i < chapter->stage.area_count ; i++) {
25		area = chapter->stage.areas + i;
26		area->background_count = 1;
27		area->backgrounds = malloc(sizeof *area->backgrounds);
28		if (area->backgrounds == NULL) exit(1);
29		mimimi_background(&area->backgrounds[0].image, area->ground, mimimi_chapter_I_colors[i]);
30		area->backgrounds[0].x = 0;
31		area->backgrounds[0].y = 0;
32		area->backgrounds[0].z = 0;
33	}
34	
35	area = chapter->stage.areas;
36	
37	image = &area->backgrounds[0].image;
38	free(image->colors);
39	image->width = mimimi_chapter_I_background_width;
40	image->height = mimimi_chapter_I_background_height;
41	image->colors = malloc(image->width * image->height);
42	if (image->colors == NULL) exit(1);
43	mimimi_unpack_bits(image->width * image->height, image->colors, mimimi_chapter_I_background_packed);
44	area->backgrounds[0].x = -88;
45	area->backgrounds[0].y = 0;
46	area->backgrounds[0].z = 0;
47	
48	area->foregrounds = malloc(sizeof *area->foregrounds);
49	if (area->foregrounds == NULL) exit(1);
50	
51	image = &area->foregrounds[0].image;
52	image->width = mimimi_chapter_I_foreground_width;
53	image->height = mimimi_chapter_I_foreground_height;
54	image->colors = malloc(image->width * image->height);
55	if (image->colors == NULL) exit(1);
56	mimimi_unpack_bits(image->width * image->height, image->colors, mimimi_chapter_I_foreground_packed);
57	area->foreground_count = 1;
58	area->foregrounds[0].x = 0;
59	area->foregrounds[0].y = 0;
60	area->foregrounds[0].z = 0;
61}
62
63static void mimimi_chapter_I_size(struct mimimi_image *image)
64{
65	int width, height;
66	
67	if (image->width / image->height < 2) {
68		height = 256;
69		width = height * image->width / image->height;
70	}
71	else {
72		width = 512;
73		height = width * image->height / image->width;
74	}
75	
76	if (width < 256) {
77		width = 256;
78		height = width * image->height / image->width;
79		if (height > 384) height = 384;
80	}
81	if (height < 192) {
82		height = 192;
83		width = height * image->width / image->height;
84		if (width > 512) width = 512;
85	}
86	
87	image->width = width;
88	image->height = height;
89}
90
91static void mimimi_chapter_I_tick(void *data, struct mimimi_input *input)
92{
93	static struct mimimi_sound music = {
94#include "../sounds/caramelldansen.txt"
95	};
96	
97	struct mimimi_chapter_I *chapter;
98	struct mimimi_sprite *player;
99	struct mimimi_stage_jump jump;
100	
101	chapter = data;
102	player = chapter->stage.sprites;
103	
104	mimimi_chapter_I_size(&input->image);
105	input->image.colors = chapter->colors;
106	
107	jump.stage = &chapter->stage;
108	jump.sprite = player;
109	
110	mimimi_history_tick(&chapter->history, input->left, input->right);
111	mimimi_controls_tick(&player->walk, &chapter->history, &mimimi_stage_jump, &jump);
112	mimimi_stage_tick(&chapter->stage, input);
113	
114	if (chapter->progression == 0) {
115		if (player->position.x < -0x1800) chapter->progression++;
116	}
117	if (chapter->progression == 1) {
118		if (mimimi_toast_dialogue_tick(&chapter->tutorial, &input->image) != 0) chapter->progression++;
119	}
120	if (chapter->progression == 2) {
121		if (player->position.x < -0x5600) chapter->progression++;
122	}
123	if (chapter->progression == 3) {
124		if (mimimi_toast_dialogue_tick(&chapter->dialogue0, &input->image) != 0) chapter->progression++;
125	}
126	if (chapter->progression == 4) {
127		if (chapter->stage.platform_sets[0].angular_platforms[0].physics.other.airborne == 0) chapter->progression++;
128	}
129	if (chapter->progression == 5) {
130		if (mimimi_toast_dialogue_tick(&chapter->dialogue1, &input->image) != 0) chapter->progression++;
131	}
132	if (chapter->progression == 6) {
133		if (player->position.y <= -0x2C00 && player->physics.airborne == 0) chapter->progression++;
134	}
135	if (chapter->progression == 7) {
136		if (mimimi_toast_dialogue_tick(&chapter->dialogue, &input->image) != 0) chapter->progression++;
137	}
138	if (chapter->progression == 8) {
139		mimimi_play_sound(&chapter->stage, &music);
140		chapter->progression++;
141	}
142}
143
144int main(void)
145{
146	static unsigned char colors0[] = {0, 0x6B};
147	static unsigned char colors1[] = {0, 8};
148	static struct mimimi_ground ground0 = {4, 3, {0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0}};
149	static struct mimimi_ground ground1 = {5, 3, {0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0}};
150	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!"}, };
151	static struct mimimi_dialogue_paragraph paragraphs0[] = {{NULL, "Haruka", "Press a direction twice to jump!"}, {NULL, "Mirai", "Hmm, I wonder what's on top of those platforms..."}, {NULL, "Mirai", "Oh, it seems like I need to be quick if I want to get to the top."}, };
152	
153	struct mimimi_dialogue dialogue;
154	struct mimimi_chapter_I *chapter;
155	struct mimimi_platform_set *platforms;
156	struct mimimi_image *image;
157	
158	paragraphs0[0].model = mimimi_haruka_flash;
159	paragraphs0[1].model = mimimi_mirai_flash;
160	paragraphs0[2].model = mimimi_mirai_flash;
161	paragraphs[0].model = mimimi_mirai_flash;
162	paragraphs[1].model = mimimi_ryouna_flash;
163	paragraphs[2].model = mimimi_ryoubi_flash;
164	paragraphs[3].model = mimimi_ryouna_flash;
165	paragraphs[4].model = mimimi_mirai_flash;
166	paragraphs[5].model = mimimi_haruka_flash;
167	
168	chapter = malloc(sizeof *chapter);
169	if (chapter == NULL) exit(1);
170	
171	mimimi_stage(&chapter->stage, mimimi_chapter_I_stage, sizeof mimimi_chapter_I_stage / sizeof *mimimi_chapter_I_stage);
172	mimimi_chapter_I_background(chapter);
173	
174	chapter->progression = 0;
175	
176	dialogue.count = 1;
177	dialogue.paragraphs = paragraphs0;
178	mimimi_toast_dialogue(&chapter->tutorial, &dialogue);
179	
180	dialogue.count = 1;
181	dialogue.paragraphs = paragraphs0 + 1;
182	mimimi_toast_dialogue(&chapter->dialogue0, &dialogue);
183	
184	dialogue.count = 1;
185	dialogue.paragraphs = paragraphs0 + 2;
186	mimimi_toast_dialogue(&chapter->dialogue1, &dialogue);
187	
188	dialogue.count = sizeof paragraphs / sizeof *paragraphs;
189	dialogue.paragraphs = paragraphs;
190	mimimi_toast_dialogue(&chapter->dialogue, &dialogue);
191	
192	mimimi_spawn(&chapter->stage, mimimi_mirai_flash, 0, 0, 80, 250);
193	
194	image = malloc(sizeof *image);
195	if (image == NULL) return 1;
196	
197	mimimi_background(image, &ground0, colors0);
198	
199	platforms = mimimi_add_platform_set(&chapter->stage);
200	platforms->angular_weight = 512;
201	platforms->strength = 128;
202	
203	mimimi_spawn_angular_platform(&chapter->stage, platforms, &ground0, image, -0x5800, -0x1C80, -80, 256, 1024);
204	mimimi_spawn_angular_platform(&chapter->stage, platforms, &ground0, image, -0x5800, -0x1C80, -80, 256, 2048);
205	mimimi_spawn_angular_platform(&chapter->stage, platforms, &ground0, image, -0x5800, -0x1C80, -80, 256, 3072);
206	
207	platforms = mimimi_add_platform_set(&chapter->stage);
208	platforms->angular_weight = 512;
209	platforms->strength = 128;
210	
211	mimimi_spawn_angular_platform(&chapter->stage, platforms, &ground0, image, -0x6500, -0x2100, 80, 256, 1024);
212	mimimi_spawn_angular_platform(&chapter->stage, platforms, &ground0, image, -0x6500, -0x2100, 80, 256, 2048);
213	mimimi_spawn_angular_platform(&chapter->stage, platforms, &ground0, image, -0x6500, -0x2100, 80, 256, 3072);
214	
215	image = malloc(sizeof *image);
216	if (image == NULL) return 1;
217	
218	mimimi_background(image, &ground1, colors1);
219	
220	platforms = mimimi_add_platform_set(&chapter->stage);
221	platforms->strength = 0;
222	platforms->min = 0;
223	platforms->max = 1024;
224	
225	mimimi_spawn_linear_platform(&chapter->stage, platforms, &ground1, image, -0x4B00, -0x700, 256);
226	mimimi_spawn_linear_platform(&chapter->stage, platforms, &ground1, image, -0x4800, -0x800, -256);
227	mimimi_spawn_linear_platform(&chapter->stage, platforms, &ground1, image, -0x4F00, -0xC00, 256);
228	mimimi_spawn_linear_platform(&chapter->stage, platforms, &ground1, image, -0x4F00, -0xD00, -256);
229	
230	chapter->history.left = 0;
231	chapter->history.right = 0;
232	
233	mimimi_main(&mimimi_chapter_I_tick, chapter);
234	return 1;
235}