summaryrefslogtreecommitdiff
path: root/src/modules/submit/submit.c
blob: ebda531238f5c0b3d2c871ed3666178bbc7d4845 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
 *  Copyright (C) 2018 - Vito Caputo - <vcaputo@pengaru.com>
 *
 *  This program is free software: you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 2 as published
 *  by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#include "til.h"
#include "til_fb.h"
#include "til_settings.h"
#include "til_util.h"

#include "grid/grid.h"

#define NUM_PLAYERS	8
#define GRID_SIZE	60
#define TICKS_PER_FRAME 8000

typedef struct color_t {
	float	r, g, b;
} color_t;

static color_t colors[NUM_PLAYERS + 1] = {
	{},		 	/* uninitialized cell starts black, becomes winner colors */
	{1.f, .317f, 0.f },	/* orange */
	{.627f, .125f, 1.f },	/* blue */
	{.878f, 0.f, 0.f },	/* red */
	{.165f, .843f, .149f },	/* green */
	{0.f, .878f, .815f },	/* cyan */
	{.878f, 0.f, 1.f },	/* purple */
	{.906f, .937f, 0.f }, 	/* yellow */
	{}, 			/* black */
};


typedef struct submit_context_t {
	grid_t		*grid;
	grid_player_t	*players[NUM_PLAYERS];
	uint32_t	seq;
	uint32_t	game_winner;
	unsigned	bilerp:1;
	uint8_t		cells[GRID_SIZE * GRID_SIZE];
} submit_context_t;

typedef struct submit_setup_t {
	til_setup_t	til_setup;
	unsigned	bilerp:1;
} submit_setup_t;

static submit_setup_t submit_default_setup;

/* convert a color into a packed, 32-bit rgb pixel value (taken from libs/ray/ray_color.h) */
static inline uint32_t color_to_uint32(color_t color) {
	uint32_t	pixel;

	if (color.r > 1.0f) color.r = 1.0f;
	if (color.g > 1.0f) color.g = 1.0f;
	if (color.b > 1.0f) color.b = 1.0f;

	pixel = (uint32_t)(color.r * 255.0f);
	pixel <<= 8;
	pixel |= (uint32_t)(color.g * 255.0f);
	pixel <<= 8;
	pixel |= (uint32_t)(color.b * 255.0f);

	return pixel;
}


static inline float clamp(float x, float lowerlimit, float upperlimit) {
	if (x < lowerlimit)
		x = lowerlimit;

	if (x > upperlimit)
		x = upperlimit;

	return x;
}


/* taken from https://en.wikipedia.org/wiki/Smoothstep#Variations */
static inline float smootherstep(float edge0, float edge1, float x) {
	x = clamp((x - edge0) / (edge1 - edge0), 0.f, 1.f);

	return x * x * x * (x * (x * 6.f - 15.f) + 10.f);
}


/* linearly interpolate colors */
static color_t color_lerp(color_t *a, color_t *b, float t)
{
	color_t	res = {
		.r = a->r * (1.f - t) + b->r * t,
		.g = a->g * (1.f - t) + b->g * t,
		.b = a->b * (1.f - t) + b->b * t,
	};

	return res;
}


/* bilinearly interpolate colors from 4 cells */
static inline uint32_t sample_grid_bilerp(submit_context_t *ctxt, float x, float y)
{
	int		i, ix = x, iy = y;
	float		x_t, y_t;
	uint8_t		corners[2][2];
	color_t		x1, x2;

	i = iy * GRID_SIZE + ix;

	/* ix,iy forms the corner of a 2x2 kernel, determine which corner */
	if (x > ix + .5f) {
		x_t = x - ((float)ix + .5f);

		if (y > iy + .5f) {
			/* NW corner */
			y_t = y - ((float)iy + .5f);

			corners[0][0] = ctxt->cells[i];
			corners[0][1] = ctxt->cells[i + 1];
			corners[1][0] = ctxt->cells[i + GRID_SIZE];
			corners[1][1] = ctxt->cells[i + GRID_SIZE + 1];
		} else {
			/* SW corner */
			y_t = 1.f - (((float)iy + .5f) - y);

			corners[1][0] = ctxt->cells[i];
			corners[1][1] = ctxt->cells[i + 1];
			corners[0][0] = ctxt->cells[i - GRID_SIZE];
			corners[0][1] = ctxt->cells[i - GRID_SIZE + 1];
		}
	} else {
		x_t = 1.f - (((float)ix + .5f) - x);

		if (y > iy + .5f) {
			/* NE corner */
			y_t = y - ((float)iy + .5f);

			corners[0][1] = ctxt->cells[i];
			corners[0][0] = ctxt->cells[i - 1];
			corners[1][1] = ctxt->cells[i + GRID_SIZE];
			corners[1][0] = ctxt->cells[i + GRID_SIZE - 1];
		} else {
			/* SE corner */
			y_t = 1.f - (((float)iy + .5f) - y);

			corners[1][1] = ctxt->cells[i];
			corners[1][0] = ctxt->cells[i - 1];
			corners[0][1] = ctxt->cells[i - GRID_SIZE];
			corners[0][0] = ctxt->cells[i - GRID_SIZE - 1];
		}
	}

	/* short-circuit cases where interpolation obviously wouldn't do anything */
	if (corners[0][0] == corners[0][1] &&
	    corners[0][1] == corners[1][1] &&
	    corners[1][1] == corners[1][0])
		return color_to_uint32(colors[corners[0][0]]);

	x_t = smootherstep(0.f, 1.f, x_t);
	y_t = smootherstep(0.f, 1.f, y_t);

	x1 = color_lerp(&colors[corners[0][0]], &colors[corners[0][1]], x_t);
	x2 = color_lerp(&colors[corners[1][0]], &colors[corners[1][1]], x_t);

	return color_to_uint32(color_lerp(&x1, &x2, y_t));
}


static inline uint32_t sample_grid(submit_context_t *ctxt, float x, float y)
{
	return color_to_uint32(colors[ctxt->cells[(int)y * GRID_SIZE + (int)x]]);
}


static void draw_grid(submit_context_t *ctxt, til_fb_fragment_t *fragment)
{
	float	xscale = ((float)GRID_SIZE - 1.f) / (float)fragment->frame_width;
	float	yscale = ((float)GRID_SIZE - 1.f) / (float)fragment->frame_height;

	for (int y = 0; y < fragment->height; y++) {
		for (int x = 0; x < fragment->width; x++) {
			uint32_t	color;

			/* TODO: this could be optimized a bit! i.e. don't recompute the y for every x etc. */
			color = sample_grid(ctxt, .5f + ((float)(fragment->x + x)) * xscale, .5f + ((float)(fragment->y + y)) * yscale);
			til_fb_fragment_put_pixel_unchecked(fragment, fragment->x + x, fragment->y + y, color);
		}
	}
}


static void draw_grid_bilerp(submit_context_t *ctxt, til_fb_fragment_t *fragment)
{
	float	xscale = ((float)GRID_SIZE - 1.f) / (float)fragment->frame_width;
	float	yscale = ((float)GRID_SIZE - 1.f) / (float)fragment->frame_height;

	for (int y = 0; y < fragment->height; y++) {
		for (int x = 0; x < fragment->width; x++) {
			uint32_t	color;

			/* TODO: this could be optimized a bit! i.e. don't recompute the y for every x etc. */
			color = sample_grid_bilerp(ctxt, .5f + ((float)(fragment->x + x)) * xscale, .5f + ((float)(fragment->y + y)) * yscale);
			til_fb_fragment_put_pixel_unchecked(fragment, fragment->x + x, fragment->y + y, color);
		}
	}
}


static void taken(void *ctx, uint32_t x, uint32_t y, uint32_t player)
{
	submit_context_t	*c = ctx;

	c->cells[y * GRID_SIZE + x] = player;
}


static void won(void *ctx, uint32_t player)
{
	submit_context_t	*c = ctx;

	c->game_winner = player;
}


static grid_ops_t submit_ops = {
	.taken = taken,
	.won = won,
};


static void setup_grid(submit_context_t *ctxt)
{
	grid_ops_t	*ops = &submit_ops;

	if (ctxt->grid)
		grid_free(ctxt->grid);

	ctxt->grid = grid_new(NUM_PLAYERS, GRID_SIZE, GRID_SIZE);
	for (int i = 0; i < NUM_PLAYERS; i++, ops = NULL)
		ctxt->players[i] = grid_player_new(ctxt->grid, ops, ctxt);

	memset(ctxt->cells, 0, sizeof(ctxt->cells));

	/* this makes the transition between games less visually jarring */
	colors[0] = colors[ctxt->game_winner];

	ctxt->game_winner = ctxt->seq = 0;
}


static void * submit_create_context(unsigned ticks, unsigned num_cpus, til_setup_t *setup)
{
	submit_context_t	*ctxt;

	if (!setup)
		setup = &submit_default_setup.til_setup;

	ctxt = calloc(1, sizeof(submit_context_t));
	if (!ctxt)
		return NULL;

	ctxt->bilerp = ((submit_setup_t *)setup)->bilerp;
	setup_grid(ctxt);

	return ctxt;
}


static void submit_destroy_context(void *context)
{
	submit_context_t	*ctxt = context;

	grid_free(ctxt->grid);
	free(ctxt);
}


static int submit_fragmenter(void *context, unsigned n_cpus, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment)
{
	return til_fb_fragment_tile_single(fragment, 32, number, res_fragment);
}


static void submit_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
	submit_context_t	*ctxt = context;

	*res_fragmenter = submit_fragmenter;

	if (ctxt->game_winner)
		setup_grid(ctxt);

	for (int i = 0; i < NUM_PLAYERS; i++) {
		int	moves = rand() % TICKS_PER_FRAME;

		for (int j = 0; j < moves; j++)
			grid_player_plan(ctxt->players[i], ctxt->seq++, rand() % GRID_SIZE, rand() % GRID_SIZE);
	}

	for (int j = 0; j < TICKS_PER_FRAME; j++)
		grid_tick(ctxt->grid);
}


static void submit_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
	submit_context_t	*ctxt = context;

	if (!ctxt->bilerp)
		draw_grid(ctxt, fragment);
	else
		draw_grid_bilerp(ctxt, fragment);
}


static int submit_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup)
{
	const char	*values[] = {
				"off",
				"on",
				NULL
			};
	const char	*bilerp;
	int		r;

	r = til_settings_get_and_describe_value(settings,
						&(til_setting_desc_t){
							.name = "Bilinearly interpolate cell colors",
							.key = "bilerp",
							.regex = NULL,
							.preferred = values[0],
							.values = values,
							.annotations = NULL
						},
						&bilerp,
						res_setting,
						res_desc);
	if (r)
		return r;

	if (res_setup) {
		submit_setup_t	*setup;

		setup = til_setup_new(sizeof(*setup), (void(*)(til_setup_t *))free);
		if (!setup)
			return -ENOMEM;

		if (!strcasecmp(bilerp, "on"))
			setup->bilerp = 1;

		*res_setup = &setup->til_setup;
	}

	return 0;
}


til_module_t	submit_module = {
	.create_context = submit_create_context,
	.destroy_context = submit_destroy_context,
	.prepare_frame = submit_prepare_frame,
	.render_fragment = submit_render_fragment,
	.name = "submit",
	.description = "Cellular automata conquest game sim (threaded (poorly))",
	.author = "Vito Caputo <vcaputo@pengaru.com>",
	.setup = submit_setup,
};
© All Rights Reserved