Skip to content
Snippets Groups Projects
Commit 22b6e2fe authored by maberet's avatar maberet
Browse files

recup render

parents 5977d61a 0e08094b
No related branches found
No related tags found
No related merge requests found
Showing
with 495 additions and 462 deletions
travail_de_groupe/jeu_appren_par_renfo/Res/racket.png

17.4 KiB

......@@ -7,13 +7,12 @@ void initBall()
{
ball.x = 5 * BLOCK_SIZE;
ball.y = 5 * BLOCK_SIZE;
ball.z = player.h/BLOCK_SIZE;
ball.z = player.h / BLOCK_SIZE;
ball.h = 0.5 * BLOCK_SIZE;
ball.w = 0.5 * BLOCK_SIZE;
ball.w = 0.5 * BLOCK_SIZE;
ball.isHit = 0;
ball.angle = -pi;
ball.speed = 0;
}
//ball_t ball;
int trajectoireAntoine[NUMBERPOINT_TRAJEC][2];
......@@ -127,3 +126,22 @@ void calculTrajectoireAntoine2(int xd, int yd, int xf, int yf, int xt, int yt)
trajectoireAntoine[NUMBERPOINT_TRAJEC - 1][0] = xt;
trajectoireAntoine[NUMBERPOINT_TRAJEC - 1][1] = yt;
}
void updateBall()
{
ball.x = ball.x + ball.speed * cos(ball.angle);
ball.y = ball.y + ball.speed * sin(ball.angle);
if (ball.isHit)
{
// landingPoint est déjà / BLOCK_SIZE de base
ball.z = lagrangeInterpolation(ball.x / BLOCK_SIZE, lastHitPoint[0] / BLOCK_SIZE, lastHitPoint[1] / BLOCK_SIZE, 15, 2 * player.h / BLOCK_SIZE, landingPoint[0], 0);
}
if ((int)ball.z == 0)
{
ball.x = 0;
ball.y = 0;
ball.z = 0;
ball.speed = 0;
}
}
......@@ -13,6 +13,9 @@
#define NUMBERPOINT_TRAJEC 50
#define PLAYER 0
#define AI 1
typedef enum
{
false, true
......@@ -29,6 +32,7 @@ typedef struct ball
int isHit;
float angle;
int speed;
int isTravelingTo;
} ball_t;
extern ball_t ball;
......@@ -40,9 +44,12 @@ typedef struct point{
//extern ball_t ball;
extern int trajectoireAntoine[NUMBERPOINT_TRAJEC][2];
void initBall();
float defineAngle(int, int, int, int);
void updateBall();
point_t initCanon (point_t canon);
point_t initDropPoint (point_t dropPoint);
float defineAngleF(int, int, int, int);
float defineAngleF(int canonX, int canonY, int xDropPoint, int yDropPoint);
float defineAngleH(int xCanon, int xDropPoint);
float lagrangeInterpolation(float, int, int, int, int, int, int);
......
#include "player.h"
int angleF;
int angleH;
int ennemyZone;
int canonZone;
int action;
void initEnnemy()
{
ennemy.h = 2 * BLOCK_SIZE;
ennemy.w = 2 * BLOCK_SIZE;
ennemy.x = 25 * BLOCK_SIZE;
ennemy.y = 5 * BLOCK_SIZE;
ennemy.angle = -pi;
ennemy.speed = MOVEMENT_SPEED;
}
void manageEnnemyMovement()
{
if (ball.isTravelingTo == AI){
angleF = defineAngleF(lastHitPoint[0], lastHitPoint[1], landingPoint[0], landingPoint[1]);
angleF = converterIntoAngleF(angleF);
angleH = defineAngleH(lastHitPoint[0], landingPoint[0]);
angleH = converterIntoAngleH(angleH);
ennemyZone = convertIntoZone(ennemy.x, ennemy.y);
canonZone = convertIntoZone(lastHitPoint[0], lastHitPoint[1]);
action = takeAction(ennemy.x, ennemy.y, Q, canonZone, angleH, angleF, 0);
switch (action)
{
case BACK:
ennemy.x += MOVEMENT_SPEED;
break;
case FOWARD:
ennemy.x -= MOVEMENT_SPEED;
break;
case UP:
ennemy.y -= MOVEMENT_SPEED;
break;
case DOWN:
ennemy.y += MOVEMENT_SPEED;
break;
default:
break;
}
}
}
void manageEnnemy(){
manageEnnemyMovement();
}
\ No newline at end of file
#ifndef ENNEMY_H
#define ENNEMY_H
#include "player.h"
#include "map.h"
#include "render.h"
#include "ball.h"
#include "qlearn.h"
void initEnnemy();
void manageEnnemy();
#endif
\ No newline at end of file
......@@ -53,6 +53,7 @@ void gestGame()
break;
case SDL_MOUSEMOTION:
// mouvement de caméra
if (event.motion.xrel > 0)
{
player.angle += 0.01;
......@@ -104,6 +105,16 @@ void gestGame()
Keys[1] = 1;
continue;
case SDLK_h:
if (showHub == 0)
{
showHub = 1;
}
else
{
showHub = 0;
}
default:
break;
}
......@@ -134,10 +145,12 @@ void gestGame()
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT)
{
if (player.isHitting){
if (player.isHitting)
{
player.isHitting = 0;
}
else {
else
{
player.isHitting = 1;
}
}
......
#ifndef _GEST_EVENT_H_
#define _GEST_EVENT_H_
#include "main.h"
#include "render.h"
......@@ -12,4 +11,4 @@ extern int Keys[NB_KEYS];
void *EventLoop(void *arg);
void initKeys();
#endif
\ No newline at end of file
#endif
\ No newline at end of file
......@@ -2,10 +2,10 @@
int running;
int game_state;
float ***** Q;
int main(){
float ***** Q = allocateAndInitiateQ();
Q = allocateAndInitiateQ();
readQFromFile(Q);
int i= 10;
srand ( time(NULL));
......@@ -22,4 +22,6 @@ int main(){
// mainLoop();
}
\ No newline at end of file
mainLoop();
}
#ifndef _MAIN_HEADER_
#define _MAIN_HEADER_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
......@@ -14,17 +13,18 @@
#include <pthread.h>
#include "render.h"
#include "gest_event.h"
#include "player.h"
#include "ennemy.h"
#include "map.h"
#include "qlearn.h"
#include "render.h"
#define MENU 0
#define GAME 1
extern int running;
extern int game_state;
extern float ***** Q;
#endif
\ No newline at end of file
#endif
\ No newline at end of file
......@@ -2,27 +2,32 @@
int map[MAP_HEIGHT][MAP_WIDTH];
void readMapFromFile(char *file_name){
void readMapFromFile(char *file_name)
{
FILE *file = fopen(file_name, "r");
if (file == NULL){
if (file == NULL)
{
printf("Couldn't open map file.");
exit(EXIT_FAILURE);
}
int i, j;
for (i = 0; i < MAP_HEIGHT ; i++){
for (j = 0; j < MAP_WIDTH; j++){
for (i = 0; i < MAP_HEIGHT; i++)
{
for (j = 0; j < MAP_WIDTH; j++)
{
fscanf(file, "%d", &map[i][j]);
}
}
fclose(file);
}
void printMap(){
void printMap()
{
int i, j;
for (i = 0; i < MAP_HEIGHT; i++){
for (j = 0; j < MAP_WIDTH; j++){
for (i = 0; i < MAP_HEIGHT; i++)
{
for (j = 0; j < MAP_WIDTH; j++)
{
printf("%d ", map[i][j]);
}
printf("\n");
......
#ifndef MAP_HEADER_
#define MAP_HEADER_
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "main.h"
#define MAP_WIDTH 31
#define MAP_HEIGHT 11
extern int map[MAP_HEIGHT][MAP_WIDTH];
void readMapFromFile(char *file_name);
......
......@@ -35,10 +35,10 @@ int *generateLandingPoint(int rxWall)
int *landingPoint = malloc(sizeof(int) * 2);
srand(time(NULL));
int randomPointX = MAP_WIDTH/2 + 1 + rand()%(rxWall/BLOCK_SIZE - (MAP_WIDTH/2));
int randomPointX = MAP_WIDTH / 2 + 1 + rand() % (rxWall / BLOCK_SIZE - (MAP_WIDTH / 2));
int randomPointY = -1;
landingPoint[0] = randomPointX ;
landingPoint[0] = randomPointX;
landingPoint[1] = randomPointY / BLOCK_SIZE;
landingPointIsFind = 1;
......@@ -72,16 +72,16 @@ void hitBall()
float distanceNet;
if (player.isHitting)
{
castSingleRay(player.angle, &distanceWall, &distanceNet, &rxWall, &ryWall, &rxNet, &ryNet);
// printf("hit\n");
if (rxWall > MAP_WIDTH/2)
castSingleRay(player.angle, &distanceWall, &distanceNet, &rxWall, &ryWall, &rxNet, &ryNet);
if (rxWall > MAP_WIDTH / 2)
{
freeIntList(lastHitPoint);
lastHitPoint = allocLastHitPoint();
//cherche et trouve point de chute, UNE SEULE FOIS!
if(landingPointIsFind == 0){
// cherche et trouve point de chute, UNE SEULE FOIS!
if (landingPointIsFind == 0)
{
freeIntList(landingPoint);
landingPoint = generateLandingPoint(rxWall);
printf("landing point: x=%d; y=%d\n", landingPoint[0], landingPoint[1]);
......@@ -94,47 +94,12 @@ void hitBall()
ball.speed = 2 * HIT_FORCE;
ball.z = player.h;
ball.isHit = 1;
ball.isTravelingTo = AI;
}
// printf("valid hit\n");
}
else
{
// printf("unvalid hit\n");
}
}
//}
}
void updateBall()
{
ball.x = ball.x + ball.speed * cos(ball.angle);
ball.y = ball.y + ball.speed * sin(ball.angle);
if (ball.isHit)
{
// landingPoint est déjà / BLOCK_SIZE de base
ball.z = lagrangeInterpolation(ball.x / BLOCK_SIZE, lastHitPoint[0] / BLOCK_SIZE, lastHitPoint[1] / BLOCK_SIZE, 15, 2 * player.h / BLOCK_SIZE, landingPoint[0], 0);
if (ball.z > 0)
{
/*printf("param. lagrange : xp=%f, xd=%d, yd=%d, xf=%d, yf=%d, xt=%d, yt=%d\n",
ball.x / BLOCK_SIZE,
lastHitPoint[0] / BLOCK_SIZE,
lastHitPoint[1] / BLOCK_SIZE,
15,
2 * player.h / BLOCK_SIZE,
landingPoint[0],
0);
printf("ballZ: %f\n", ball.z);*/
}
}
if ((int)ball.z == 0)
{
ball.x = 0;
ball.y = 0;
ball.z = 0;
ball.speed = 0;
}
}
void manageMovement()
{
......@@ -144,9 +109,10 @@ void manageMovement()
// d : keys[3] : droite
float x_increment = (Keys[0] - Keys[2]) * player.deltax + (Keys[3] - Keys[1]) * sin(player.angle);
float y_increment = (Keys[0] - Keys[2]) * player.deltay + (Keys[1] - Keys[3]) * cos(player.angle);
float newpos_x = (player.x + x_increment) / BLOCK_SIZE;
float newpos_y = (player.y + y_increment) / BLOCK_SIZE;
if (newpos_x >= 0 && newpos_x < MAP_WIDTH && newpos_y >= 0 && newpos_y < MAP_HEIGHT)
float newpos_x = (player.x + x_increment * MOVEMENT_SPEED) / BLOCK_SIZE;
float newpos_y = (player.y + y_increment * MOVEMENT_SPEED) / BLOCK_SIZE;
int widthColli = player.w / (3 * BLOCK_SIZE);
if (newpos_x > widthColli && newpos_x < MAP_WIDTH - widthColli && newpos_y > widthColli && newpos_y < MAP_HEIGHT - widthColli)
{
if (map[(int)newpos_y][(int)newpos_x] != 1)
{
......@@ -160,5 +126,4 @@ void managePlayer()
{
manageMovement();
hitBall();
updateBall();
}
\ No newline at end of file
......@@ -11,10 +11,11 @@
#define ENTITIES_RIGHT 3
#define HIT_RANGE 2
#define HIT_FORCE 20
#define HIT_FORCE 10
#define MOVEMENT_SPEED 10
#define MOVEMENT_SPEED 20
typedef struct player{
typedef struct player
{
float x;
float y;
float z;
......@@ -36,6 +37,9 @@ typedef struct player{
extern player_t player;
extern player_t ennemy;
extern int *landingPoint;
extern int *lastHitPoint;
extern int landingPointIsFind;
void initPlayer();
void managePlayer();
......
......@@ -110,6 +110,23 @@ void writeQ(float *****Q){
fclose(fp);
}
void readQFromFile(float *****Q){
int i, j, k, l, m ;
FILE * fp = fopen("q.txt", "r");
for(i = 0; i < NUMBER_ZONE_RECEIVER; i++){
for(j = 0; j < NUMBER_ZONE_SHOOTER; j++){
for(k = 0; k < 3; k++){
for(l= 0; l < 5; l++){
for(m= 0; m <5; m++){
fscanf(fp, "%f ", &Q[i][j][k][l][m]);
}
}
}
}
}
fclose(fp);
}
int argmax(float * arr){
int i;
......@@ -408,63 +425,4 @@ void traningAgent ( int numberRun, int numberStep, float *****Q) {// pour avoir
numberRun--;
}
freeStack(stack);
// printf("%d %d %d %d \n",dropZone, canonZone,zoneAngleH,zoneAngleF);
// printf("%d %d \n",agent->x, agent->y);
// for (i=0; i<numberStep-1;i++){
// action = takeAction(agent->x,agent->y,Q,canonZone,zoneAngleH,zoneAngleF,greedy);
// agentZone = convertIntoZone(agent->x, agent->y);
// line.receiverZone=agentZone;
// line.shooterZone =canonZone;
// line.angleHZone= zoneAngleH;
// line.angleFZone= zoneAngleF;
// line.action= action;
// line.reward= reward ;
// actionStack(stack,line);
// moveAgent(agent, action);
// }
// action = takeAction(agent->x, agent->y,Q,canonZone,zoneAngleH,zoneAngleF,greedy);
// agentZone = convertIntoZone(agent->x, agent->y);
// line.receiverZone=agentZone;
// line.shooterZone =canonZone;
// line.angleHZone= zoneAngleH;
// line.angleFZone= zoneAngleF;
// line.action= action;
// line.reward = 0;
// // actionStack(stack,line);
// moveAgent(agent, action);
// if (agentZone==dropZone){
// reward=1;
// }
// else{reward= 0;}
// Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone][line.action] +=
// + LEARN_RATE* ( reward - Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone][line.action] );
// while (!emptyStack(stack)){
// maxAction= argmax(Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone]);
// reward=line.reward;
// line=unStack(stack);
// Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone][line.action] +=
// + LEARN_RATE* ( reward + DISCOUNT*Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone][maxAction]
// - Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone][line.action] );
// }
// numberRun--;
// greedy=greedy-1/((float)numberRun);
// if ( numberRun%1000000==1){printf (" %d \n ", numberRun);}
// }
// freeStack(stack);
}
\ No newline at end of file
......@@ -63,7 +63,7 @@ int convertIntoZone(int ,int y);
int convertIntoZoneCanon(int xCanon,int yCanon);
int converterIntoAngleF(float);
int converterIntoAngleH(float);
int takeAction(int ,int , float ***** , int , int, int, float );
int takeAction(int xAgent, int yAgent, float ***** Q, int canonZone, int angleHZone, int angleFZone, float eps);
int setReward(int , int , int );
stack_t* initStack (int nbelt);
int emptyStack (stack_t *stack);
......@@ -72,4 +72,5 @@ void actionStack(stack_t *stack, line_t line);
line_t unStack(stack_t *stack);
void freeStack(stack_t *stack);
void traningAgent( int numberRun, int numberStep, float *****Q);
void readQFromFile(float *****Q);
#endif
\ No newline at end of file
This diff is collapsed.
......@@ -8,7 +8,7 @@
#define FPS_TO_GET 60
#define CELL_SIZE 10
#define DOF 100
#define BLOCK_SIZE 64
#define DR 0.0174533
......@@ -18,27 +18,31 @@
#define NB_RAYS (screenDimension.w)
#define ANGLE_INC ((DR * FOV_ANGLE) / NB_RAYS)
typedef struct rayInfo{
typedef struct rayInfo
{
float ra;
float distT;
int r;
int isTransparent;
int direction;
int direction;
float htexture;
int rx;
int ry;
struct rayInfo * next;
struct rayInfo *next;
} rayInfo_t;
extern SDL_Window *window;
extern SDL_Renderer *renderer;
extern SDL_DisplayMode screenDimension;
extern int * ray1;
extern int * ray2;
extern int *ray1;
extern int *ray2;
extern int showHub;
void castSingleRay(float angle, float *distanceWall, float *distanceNet, int *returnXWall, int *returnYWall, int *returnXNet, int *returnYNet);
void drawString(char *str, int x, int y, int w, int h, int r, int g, int b, int a);
void mainLoop();
void drawHub();
#endif
\ No newline at end of file
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment