<commit_before>#include <genesis.h>
#include <gfx.h>
#include "utils.h"
#include "fix32_patch.h"
#include "game_shufflepuck.h"
#include "game_ai.h"
#include "game_player.h"
#include "resources.h"

extern shuffle_racket ai;
extern shuffle_racket player;

void game_ShufflePuck()
{
	char str[32];	/* debug string */
	u16 vblCount = 0;
	u16 vramIndex = TILE_USERINDEX;
	Sprite *sprites[16];
	Vect3D_f32 pvect;
	fix32 joy_x, joy_y;

	/* Ball sprite coordinates */
	int ball_2d_x,
		ball_2d_y,
		ball_2d_scale;

	/* AI racket sprite coordinates */
	int ai_2d_x,
		ai_2d_y,
		ai_2d_scale;

	/* Player racket sprite coordinates */
	int player_2d_x,
		player_2d_y,
		player_2d_scale;


	const fix32 persp_coef[] = {fix32DivFloats(1.0, 132.0), fix32DivFloats(5, 132.0), fix32DivFloats(9, 132.0), fix32DivFloats(13, 132.0), fix32DivFloats(17, 132.0), 
								fix32DivFloats(22, 132.0), fix32DivFloats(27, 132.0), fix32DivFloats(34, 132.0), fix32DivFloats(42, 132.0), fix32DivFloats(51, 132.0), 
								fix32DivFloats(64, 132.0), fix32DivFloats(80, 132.0), fix32DivFloats(102, 132.0), fix32DivFloats(132, 132.0)};

	/*	Specific 3D -> 2D Projection */
	static Vect3D_f32 inline project3DTo2D(fix32 x, fix32 z)
	{
		const fix32 top_left_x = FIX32(120);
		const fix32 top_right_x = FIX32(320 - 120);
		const fix32 top_y = FIX32(8);

		const fix32 bottom_left_x = FIX32(0);
		const fix32 bottom_right_x = FIX32(320);
		const fix32 bottom_y = FIX32(130);

		fix32 top_2d_x, bottom_2d_x;

		Vect3D_f32 ret_tuple;

		fix32 norm_x = fix32Add(RSE_fix32Div(x, board_width), FIX32(0.5));
		fix32 norm_y = fix32Add(RSE_fix32Div(z, board_length), FIX32(0.5));

		norm_y = fix32mapValueToArray(norm_y, FIX32(0.0), FIX32(1.0), persp_coef, 14);
		
		top_2d_x = fix32Add(RSE_fix32Mul(fix32InvCoef(norm_x), top_left_x), RSE_fix32Mul(norm_x, top_right_x));
		bottom_2d_x = fix32Add(RSE_fix32Mul(fix32InvCoef(norm_x), bottom_left_x), RSE_fix32Mul(norm_x, bottom_right_x));

		ret_tuple.x = fix32Add(RSE_fix32Mul(fix32InvCoef(norm_y), top_2d_x),  RSE_fix32Mul(norm_y, bottom_2d_x)); /* proj_2d_x */
		ret_tuple.y = fix32Add(RSE_fix32Mul(fix32InvCoef(norm_y), top_y), RSE_fix32Mul(norm_y, bottom_y));	/* proj_2d_y */
		ret_tuple.z = fix32Sub(FIX32(1.0), norm_y); // fix32RangeAdjust(norm_y, FIX32(0.0), FIX32(1.0), FIX32(0.285), FIX32(1.0));	/* proj_scale */

		return ret_tuple;
	}

	/*	Ball logic */
	void ball_reset(void){
		// global pos_x, pos_z, initial_pox_x, initial_pox_z
		ball.inertia = FIX32(0.1);
		ball.radius = FIX32(0.5);
		ball.initial_pox_x = 0;
		ball.initial_pox_z = 0;
		ball.pos_x = ball.initial_pox_x;
		ball.pos_z = ball.initial_pox_z;
		ball.prev_pos_x = ball.pos_x;
		ball.prev_pos_z = ball.pos_z;
		ball.velocity_x = 0;
		ball.velocity_z = 0;
	}

	void ball_setImpulse(fix32 x, fix32 z){
		ball.velocity_x = x;
		ball.velocity_z = z;
	}

	void ball_bounceX(void){
		ball.velocity_x = RSE_fix32Mul(ball.velocity_x, FIX32(-1.0));
	}	

	void ball_bounceZ(void){
		ball.velocity_z = RSE_fix32Mul(ball.velocity_z, FIX32(-1.0));
	}

	void ball_setPosition(fix32 x, fix32 z){
		ball.pos_x = x;
		ball.pos_z = z;
	}

	void ball_update(fix32 dt){
		// global pos_x, pos_z, velocity_x, velocity_z, prev_pos_x, prev_pos_z

		/*  Keep track of the ball's previous position */
		ball.prev_pos_x = ball.pos_x;
		ball.prev_pos_z = ball.pos_z;

		/* Move the ball according to its velocity */
		ball.pos_x = fix32Add(ball.pos_x, RSE_fix32Mul(ball.velocity_x, dt));
		ball.pos_z = fix32Add(ball.pos_z, RSE_fix32Mul(ball.velocity_z, dt));

		/* basic dynamics & collision */
		if (ball.pos_x > RSE_fix32Mul(board_width, FIX32(0.5))){
			ball.pos_x = RSE_fix32Mul(board_width, FIX32(0.5));
			ball_bounceX();
		}
		else{
			if (ball.pos_x < RSE_fix32Mul(board_width, FIX32(-0.5))){
				ball.pos_x = RSE_fix32Mul(board_width, FIX32(-0.5));
				ball_bounceX();
			}
		}

		if (ball.pos_z > RSE_fix32Mul(board_length, FIX32(0.5))){
			ball.pos_z = RSE_fix32Mul(board_length, FIX32(0.5));
			ball_bounceZ();
		}
		else{
			if (ball.pos_z < RSE_fix32Mul(board_length, FIX32(-0.5))){
				ball.pos_z = RSE_fix32Mul(board_length, FIX32(-0.5));
				ball_bounceZ();
			}
		}

		// if (ball.pos_z > RSE_fix32Mul(board_length, FIX32(0.5))){
		// 	ball.pos_x = ball.initial_pox_x;
		// 	ball.pos_z = ball.initial_pox_z;
		// 	ball.prev_pos_x = ball.pos_x;
		// 	ball.prev_pos_z = ball.pos_z;
		// }

		/*	Limit the friction/damping to the areas
			where the puck can be reached by one of the players */
		// if (abs(pos_z) > board_length * 0.25):
		// 	friction_x, friction_z = mulVectorByScalar(velocity_x, velocity_z, -inertia * dt)
		// 	velocity_x, velocity_z = addVectors(velocity_x, velocity_z, friction_x, friction_z)	

		BMP_drawText("dt = ", 0, 0);
		fix32ToStr(dt, str, 8);
		BMP_drawText(str, 6, 0);	

		fix32ToStr(ball.velocity_x, str, 8);
		BMP_drawText(str, 0, 1);	
		fix32ToStr(ball.velocity_z, str, 8);
		BMP_drawText(str, 10, 1);
		fix32ToStr(ball.pos_x, str, 8);
		BMP_drawText(str, 0, 2);	
		fix32ToStr(ball.pos_z, str, 8);
		BMP_drawText(str, 10, 2);			
	}

	void renderBall(int ball_2d_x, int ball_2d_y, int ball_2d_scale){
		// ball_2d_y += (240 - 136);
<commit_msg> - Added a damping to the puck physics.<commit_after>#include <genesis.h>
#include <gfx.h>
#include "utils.h"
#include "fix32_patch.h"
#include "game_shufflepuck.h"
#include "game_ai.h"
#include "game_player.h"
#include "resources.h"

extern shuffle_racket ai;
extern shuffle_racket player;

void game_ShufflePuck()
{
	char str[32];	/* debug string */
	u16 vblCount = 0;
	u16 vramIndex = TILE_USERINDEX;
	Sprite *sprites[16];
	Vect3D_f32 pvect;
	fix32 joy_x, joy_y;

	/* Ball sprite coordinates */
	int ball_2d_x,
		ball_2d_y,
		ball_2d_scale;

	/* AI racket sprite coordinates */
	int ai_2d_x,
		ai_2d_y,
		ai_2d_scale;

	/* Player racket sprite coordinates */
	int player_2d_x,
		player_2d_y,
		player_2d_scale;


	const fix32 persp_coef[] = {fix32DivFloats(1.0, 132.0), fix32DivFloats(5, 132.0), fix32DivFloats(9, 132.0), fix32DivFloats(13, 132.0), fix32DivFloats(17, 132.0), 
								fix32DivFloats(22, 132.0), fix32DivFloats(27, 132.0), fix32DivFloats(34, 132.0), fix32DivFloats(42, 132.0), fix32DivFloats(51, 132.0), 
								fix32DivFloats(64, 132.0), fix32DivFloats(80, 132.0), fix32DivFloats(102, 132.0), fix32DivFloats(132, 132.0)};

	/*	Specific 3D -> 2D Projection */
	static Vect3D_f32 inline project3DTo2D(fix32 x, fix32 z)
	{
		const fix32 top_left_x = FIX32(120);
		const fix32 top_right_x = FIX32(320 - 120);
		const fix32 top_y = FIX32(8);

		const fix32 bottom_left_x = FIX32(0);
		const fix32 bottom_right_x = FIX32(320);
		const fix32 bottom_y = FIX32(130);

		fix32 top_2d_x, bottom_2d_x;

		Vect3D_f32 ret_tuple;

		fix32 norm_x = fix32Add(RSE_fix32Div(x, board_width), FIX32(0.5));
		fix32 norm_y = fix32Add(RSE_fix32Div(z, board_length), FIX32(0.5));

		norm_y = fix32mapValueToArray(norm_y, FIX32(0.0), FIX32(1.0), persp_coef, 14);
		
		top_2d_x = fix32Add(RSE_fix32Mul(fix32InvCoef(norm_x), top_left_x), RSE_fix32Mul(norm_x, top_right_x));
		bottom_2d_x = fix32Add(RSE_fix32Mul(fix32InvCoef(norm_x), bottom_left_x), RSE_fix32Mul(norm_x, bottom_right_x));

		ret_tuple.x = fix32Add(RSE_fix32Mul(fix32InvCoef(norm_y), top_2d_x),  RSE_fix32Mul(norm_y, bottom_2d_x)); /* proj_2d_x */
		ret_tuple.y = fix32Add(RSE_fix32Mul(fix32InvCoef(norm_y), top_y), RSE_fix32Mul(norm_y, bottom_y));	/* proj_2d_y */
		ret_tuple.z = fix32Sub(FIX32(1.0), norm_y); // fix32RangeAdjust(norm_y, FIX32(0.0), FIX32(1.0), FIX32(0.285), FIX32(1.0));	/* proj_scale */

		return ret_tuple;
	}

	/*	Ball logic */
	void ball_reset(void){
		// global pos_x, pos_z, initial_pox_x, initial_pox_z
		ball.inertia = FIX32(0.1);
		ball.radius = FIX32(0.5);
		ball.initial_pox_x = 0;
		ball.initial_pox_z = 0;
		ball.pos_x = ball.initial_pox_x;
		ball.pos_z = ball.initial_pox_z;
		ball.prev_pos_x = ball.pos_x;
		ball.prev_pos_z = ball.pos_z;
		ball.velocity_x = 0;
		ball.velocity_z = 0;
	}

	void ball_setImpulse(fix32 x, fix32 z){
		ball.velocity_x = x;
		ball.velocity_z = z;
	}

	void ball_bounceX(void){
		ball.velocity_x = RSE_fix32Mul(ball.velocity_x, FIX32(-1.0));
	}	

	void ball_bounceZ(void){
		ball.velocity_z = RSE_fix32Mul(ball.velocity_z, FIX32(-1.0));
	}

	void ball_setPosition(fix32 x, fix32 z){
		ball.pos_x = x;
		ball.pos_z = z;
	}

	void ball_update(fix32 dt){
		// global pos_x, pos_z, velocity_x, velocity_z, prev_pos_x, prev_pos_z

		/*  Keep track of the ball's previous position */
		ball.prev_pos_x = ball.pos_x;
		ball.prev_pos_z = ball.pos_z;

		/* Move the ball according to its velocity */
		ball.pos_x = fix32Add(ball.pos_x, RSE_fix32Mul(ball.velocity_x, dt));
		ball.pos_z = fix32Add(ball.pos_z, RSE_fix32Mul(ball.velocity_z, dt));

		/* basic dynamics & collision */
		if (ball.pos_x > RSE_fix32Mul(board_width, FIX32(0.5))){
			ball.pos_x = RSE_fix32Mul(board_width, FIX32(0.5));
			ball_bounceX();
		}
		else{
			if (ball.pos_x < RSE_fix32Mul(board_width, FIX32(-0.5))){
				ball.pos_x = RSE_fix32Mul(board_width, FIX32(-0.5));
				ball_bounceX();
			}
		}

		if (ball.pos_z > RSE_fix32Mul(board_length, FIX32(0.5))){
			ball.pos_z = RSE_fix32Mul(board_length, FIX32(0.5));
			ball_bounceZ();
		}
		else{
			if (ball.pos_z < RSE_fix32Mul(board_length, FIX32(-0.5))){
				ball.pos_z = RSE_fix32Mul(board_length, FIX32(-0.5));
				ball_bounceZ();
			}
		}

		// if (ball.pos_z > RSE_fix32Mul(board_length, FIX32(0.5))){
		// 	ball.pos_x = ball.initial_pox_x;
		// 	ball.pos_z = ball.initial_pox_z;
		// 	ball.prev_pos_x = ball.pos_x;
		// 	ball.prev_pos_z = ball.pos_z;
		// }

		/*	Limit the friction/damping to the areas
			where the puck can be reached by one of the players */
		if (abs(ball.pos_z) > RSE_fix32Mul(board_length , FIX32(0.25)))
		{
			ball.velocity_x = fix32Sub(ball.velocity_x, RSE_fix32Mul(RSE_fix32Mul(ball.velocity_x, ball.inertia), dt));
			ball.velocity_z = fix32Sub(ball.velocity_z, RSE_fix32Mul(RSE_fix32Mul(ball.velocity_z, ball.inertia), dt));
		}

		// BMP_drawText("dt = ", 0, 0);
		// fix32ToStr(dt, str, 8);
		// BMP_drawText(str, 6, 0);	

		// fix32ToStr(ball.velocity_x, str, 8);
		// BMP_drawText(str, 0, 1);	
		// fix32ToStr(ball.velocity_z, str, 8);
		// BMP_drawText(str, 10, 1);
		// fix32ToStr(ball.pos_x, str, 8);
		// BMP_drawText(str, 0, 2);	
		// fix32ToStr(ball.pos_z, str, 8);
		// BMP_drawText(str, 10, 2);			
	}

	void renderBall(int ball_2d_x, int ball_2d_y, int ball_2d_scale){
		// ball_2d_y += (240 - 136);

{'fraction_non_alphanumeric': 0.1455538520505941, 'fraction_numerical': 0.08432349559218091, 'mean_word_length': 4.195121951219512, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1692414', 'n_tokens_mistral': 5556, 'n_tokens_neox': 4731, 'n_words': 811}