<commit_before>/*  Competence Builder.c
    Competence Builder - AI Programmiercamp 2011, DHBW-Mannheim

    Programmrumpf
*/

// Header files
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#ifdef _WIN32 // _WIN32 is defined by many compilers available for the Windows operating system, but not by others.
#include "C:\MinGW\include\SDL\SDL.h"
#else
#include "SDL/SDL.h"
#endif

#define TILE_BACKGROUND 5
#define TILE_FLOOR 6
#define TILE_TABLE 4
#define MIN_PLAYER_X  2*Size_tile
//Set player 20px above REAL floor
#define CHARACTER_FLOOR Win_floor_y

// Graphics data - SDL variables
SDL_Surface *graphics, *screen;  // Graphics data, screen data

// Wenn die Autimatik l�uft
const int Auto_control=0;

// Definition of sizes of Graphic elements, according to graphics in competence_builder.bmp
const int Size_comp=50;                       // Size (x==y) of competence element
const int Size_tile=96;                       // Size (x==y) of player and background graphics
const int Size_digit_x=40, Size_digit_y=52;   // Size of large digits for number display
const int Size_smalldigit_x=12, Size_smalldigit_y=21;   // Size of small digits for number display

// Basic data of Window layout and physics
// Better not change this data -> game behaviours should be comparable in competition
const int Win_width=1024, Win_height=760;     // screen resolution
const int Win_floor_y=630;                    // Position of floor
const int Element_start_x=100;                // Start position of element
const float Player_v_x=8.0f;                 // Player speed
const float Gravity=0.03f;                   // Gravity 

// Important data structures, TODO: change/extend if needed

// Competence element
typedef struct {
   float x, y;            // Position
   float vx, vy;          // Speed
   char comp;             // Type of competence 0 - 3,  crash=4, deleted 5
   unsigned short int  points;	  //Points per elemnt
   unsigned short int  countdown;	  //counts down to 0 if a block explodes -> explode vanish
   // TODO: Extend if needed
} element_type;

// Player data
typedef struct {
	float x, y;           // Position
	int steps;            // TODO: You can use this for animating the moving player
} player_data_type;

// Current game information/state
typedef struct {
	// Data to be read from file
	int levels;              // Number of game levels
	int element_pause;       // Time interval between elements
	int cur_max;             // Number of curriculum elements
	char *curriculum;        // Array of competence numbers (0-3)
	  
	// Current game state
	element_type *element;   // array of competence elements -> TODO: use malloc
	int element_countdown;   // Countdown until next element is launched
	int cur_act;             // Current element 
	unsigned int all_points; // Gesamtpunktzahl
} game_state_type;


/*************************************************************************************
 * Initialization
 */

// Load lecture/level plan from file
int load_game_data(game_state_type *g, char *filename)
{
	FILE *file;
	short int cur_count=0; 	// Zähler für das Einlesen des Curriculums

    // Open the file for reading
    file=fopen(filename,"r"); 
    if(file==NULL) {
        printf("Error: can't open file.\n");
        return -1;
    }
    
    // TODO: Read level data, allocate memory if needed
    
	fscanf(file,"%d",&g->levels);		// Lese aus File erste Zahl: Level Anzahl
	fscanf(file,"%d %d",&g->cur_max, &g->element_pause);		// Lese aus File Anzahl Blöcke, Wartezeit für Block

	g->curriculum = (char *)malloc(sizeof(char) * g->cur_max);  //Seicher für Array curriculum allokieren

	fscanf(file,"\r");		// Lese aus File: Zeilenumbruch

	for(cur_count=0;cur_count<(g->cur_max);cur_count++) { 		//Schleife um alle Blöcke  nacheinander auszulesen
		fscanf(file,"%c ",&(g->curriculum[cur_count]) );		// Lese aus File Blöcke...
		g->curriculum[cur_count] = g->curriculum[cur_count] - 48; // ASCII Konvertieren
	}

    fclose(file);    // close file
    return 0;
}

// Initialize next level / player
void init_level(game_state_type *g, player_data_type *p )
{
	g->element=(element_type *)malloc(sizeof(element_type)*g->cur_max);   // Simple example for one element, TODO: need to change this COMPLETED
	g->element_countdown = g->element_pause / 20; //1200ms /20ms (20ms braucht ca. ein programmdurchlauf)
	g->cur_act=0;
	g->all_points=4096;
	// Init player position
	p->x=Win_width/3.f;
	p->y=(float)CHARACTER_FLOOR;

}

/***************************************************************************
 * Main game loop: Logic
 ***************************************************************************/
void init_next_element(game_state_type *g, player_data_type *pl)
{
	// TO DO: ...
	// TO DO: Initialize a new competence element, when/before it is thrown by the teacher
	// TO DO: Check if there are further elements or whether the level is completed
	// TO DO: Keep in mind the correct waiting time between elements
	// TO DO: Use the following calculation to initialize x, y, vx, vy (as explained in game rules)
	g->element_countdown--;
	if(g->element_countdown == 0 && g->cur_act!=g->cur_max)//Soll er gerade werfen und sind noch Elemente zum Werfen da
	{
		element_type el;
		//el=(element_type *)malloc(sizeof(element_type));
		g->element_countdown = g->element_pause / 20;
		// The teacher throws in such direction that the competence lands at the current player position
		// For the competition these calculations must be left unchanged!
		el.x=(float)Element_start_x;
		el.y=(float)Win_floor_y;
		el.vy=-(float)sqrt(el.y * 2. * Gravity);
		el.vx=(float)((pl->x-el.x)/(-el.vy/Gravity*2.));
		el.comp = g->curriculum[g->cur_act];
		g->element[g->cur_act]=el;
		//g->element[g->cur_act]=*el;
		g->cur_act++;
		printf("cur_act: %d\n",g->cur_act);
	}

	// Just in case, some notes regarding the physics - you don't need to understand that
    // h max = v2 / (2g)       v=sqrt(h max * 2 * g)
	// R = v^2 / g * sin (2 beta)
	// R * g = v2 * sin b * cos b
	// Steigzeit: v0/g
}

void explode(element_type *el) {
	el->comp = 4;
	el->vx=0;
	el->vy=0;
	el->countdown=10;
}

// TODO (optional/suggestion):
//        Write a function that checks whether/how two competence elements are colliding
//        Feel free to use the TODO suggestions - or completely ignore them, if you have your own ideas
// return:
// 0 no coll
// 1 side coll
// 2
// 3
int check_collision(element_type *el1, element_type *el2)
{
	// TODO: maybe it helps to distinguish between the current, flying element and the others
	// TODO: overlap between elements?
	// TODO: Reflection on the side?
	// TODO: landing/contact on top?
	// TODO: What about the case, where an element lands on two other elements?

	const float buffer=20.0;


	if(el1->x + Size_comp > el2->x && el1->x + Size_comp - buffer < el2->x) {
		if(el1->y + Size_comp > el2->y && el1->y < el2->y + Size_comp) {
			el1->vx=(-0.9)*el1->vx;
		}
	}
	else if(el1->x + Size_comp > el2->x && el1->x < el2->x + Size_comp) {<commit_msg>timer explode 50<commit_after>/*  Competence Builder.c
    Competence Builder - AI Programmiercamp 2011, DHBW-Mannheim

    Programmrumpf
*/

// Header files
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#ifdef _WIN32 // _WIN32 is defined by many compilers available for the Windows operating system, but not by others.
#include "C:\MinGW\include\SDL\SDL.h"
#else
#include "SDL/SDL.h"
#endif

#define TILE_BACKGROUND 5
#define TILE_FLOOR 6
#define TILE_TABLE 4
#define MIN_PLAYER_X  2*Size_tile
//Set player 20px above REAL floor
#define CHARACTER_FLOOR Win_floor_y

// Graphics data - SDL variables
SDL_Surface *graphics, *screen;  // Graphics data, screen data

// Wenn die Autimatik l�uft
const int Auto_control=0;

// Definition of sizes of Graphic elements, according to graphics in competence_builder.bmp
const int Size_comp=50;                       // Size (x==y) of competence element
const int Size_tile=96;                       // Size (x==y) of player and background graphics
const int Size_digit_x=40, Size_digit_y=52;   // Size of large digits for number display
const int Size_smalldigit_x=12, Size_smalldigit_y=21;   // Size of small digits for number display

// Basic data of Window layout and physics
// Better not change this data -> game behaviours should be comparable in competition
const int Win_width=1024, Win_height=760;     // screen resolution
const int Win_floor_y=630;                    // Position of floor
const int Element_start_x=100;                // Start position of element
const float Player_v_x=8.0f;                 // Player speed
const float Gravity=0.03f;                   // Gravity 

// Important data structures, TODO: change/extend if needed

// Competence element
typedef struct {
   float x, y;            // Position
   float vx, vy;          // Speed
   char comp;             // Type of competence 0 - 3,  crash=4, deleted 5
   unsigned short int  points;	  //Points per elemnt
   unsigned short int  countdown;	  //counts down to 0 if a block explodes -> explode vanish
   // TODO: Extend if needed
} element_type;

// Player data
typedef struct {
	float x, y;           // Position
	int steps;            // TODO: You can use this for animating the moving player
} player_data_type;

// Current game information/state
typedef struct {
	// Data to be read from file
	int levels;              // Number of game levels
	int element_pause;       // Time interval between elements
	int cur_max;             // Number of curriculum elements
	char *curriculum;        // Array of competence numbers (0-3)
	  
	// Current game state
	element_type *element;   // array of competence elements -> TODO: use malloc
	int element_countdown;   // Countdown until next element is launched
	int cur_act;             // Current element 
	unsigned int all_points; // Gesamtpunktzahl
} game_state_type;


/*************************************************************************************
 * Initialization
 */

// Load lecture/level plan from file
int load_game_data(game_state_type *g, char *filename)
{
	FILE *file;
	short int cur_count=0; 	// Zähler für das Einlesen des Curriculums

    // Open the file for reading
    file=fopen(filename,"r"); 
    if(file==NULL) {
        printf("Error: can't open file.\n");
        return -1;
    }
    
    // TODO: Read level data, allocate memory if needed
    
	fscanf(file,"%d",&g->levels);		// Lese aus File erste Zahl: Level Anzahl
	fscanf(file,"%d %d",&g->cur_max, &g->element_pause);		// Lese aus File Anzahl Blöcke, Wartezeit für Block

	g->curriculum = (char *)malloc(sizeof(char) * g->cur_max);  //Seicher für Array curriculum allokieren

	fscanf(file,"\r");		// Lese aus File: Zeilenumbruch

	for(cur_count=0;cur_count<(g->cur_max);cur_count++) { 		//Schleife um alle Blöcke  nacheinander auszulesen
		fscanf(file,"%c ",&(g->curriculum[cur_count]) );		// Lese aus File Blöcke...
		g->curriculum[cur_count] = g->curriculum[cur_count] - 48; // ASCII Konvertieren
	}

    fclose(file);    // close file
    return 0;
}

// Initialize next level / player
void init_level(game_state_type *g, player_data_type *p )
{
	g->element=(element_type *)malloc(sizeof(element_type)*g->cur_max);   // Simple example for one element, TODO: need to change this COMPLETED
	g->element_countdown = g->element_pause / 20; //1200ms /20ms (20ms braucht ca. ein programmdurchlauf)
	g->cur_act=0;
	g->all_points=4096;
	// Init player position
	p->x=Win_width/3.f;
	p->y=(float)CHARACTER_FLOOR;

}

/***************************************************************************
 * Main game loop: Logic
 ***************************************************************************/
void init_next_element(game_state_type *g, player_data_type *pl)
{
	// TO DO: ...
	// TO DO: Initialize a new competence element, when/before it is thrown by the teacher
	// TO DO: Check if there are further elements or whether the level is completed
	// TO DO: Keep in mind the correct waiting time between elements
	// TO DO: Use the following calculation to initialize x, y, vx, vy (as explained in game rules)
	g->element_countdown--;
	if(g->element_countdown == 0 && g->cur_act!=g->cur_max)//Soll er gerade werfen und sind noch Elemente zum Werfen da
	{
		element_type el;
		//el=(element_type *)malloc(sizeof(element_type));
		g->element_countdown = g->element_pause / 20;
		// The teacher throws in such direction that the competence lands at the current player position
		// For the competition these calculations must be left unchanged!
		el.x=(float)Element_start_x;
		el.y=(float)Win_floor_y;
		el.vy=-(float)sqrt(el.y * 2. * Gravity);
		el.vx=(float)((pl->x-el.x)/(-el.vy/Gravity*2.));
		el.comp = g->curriculum[g->cur_act];
		g->element[g->cur_act]=el;
		//g->element[g->cur_act]=*el;
		g->cur_act++;
		printf("cur_act: %d\n",g->cur_act);
	}

	// Just in case, some notes regarding the physics - you don't need to understand that
    // h max = v2 / (2g)       v=sqrt(h max * 2 * g)
	// R = v^2 / g * sin (2 beta)
	// R * g = v2 * sin b * cos b
	// Steigzeit: v0/g
}

void explode(element_type *el) {
	el->comp = 4;
	el->vx=0;
	el->vy=0;
	el->countdown=50;
}

// TODO (optional/suggestion):
//        Write a function that checks whether/how two competence elements are colliding
//        Feel free to use the TODO suggestions - or completely ignore them, if you have your own ideas
// return:
// 0 no coll
// 1 side coll
// 2
// 3
int check_collision(element_type *el1, element_type *el2)
{
	// TODO: maybe it helps to distinguish between the current, flying element and the others
	// TODO: overlap between elements?
	// TODO: Reflection on the side?
	// TODO: landing/contact on top?
	// TODO: What about the case, where an element lands on two other elements?

	const float buffer=20.0;


	if(el1->x + Size_comp > el2->x && el1->x + Size_comp - buffer < el2->x) {
		if(el1->y + Size_comp > el2->y && el1->y < el2->y + Size_comp) {
			el1->vx=(-0.9)*el1->vx;
		}
	}
	else if(el1->x + Size_comp > el2->x && el1->x < el2->x + Size_comp) {
{'fraction_non_alphanumeric': 0.14669612944614727, 'fraction_numerical': 0.016679734834984673, 'mean_word_length': 3.1693907875185734, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 121, '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': '490900', 'n_tokens_mistral': 4753, 'n_tokens_neox': 4466, 'n_words': 1474}