Commit f6ac9d3d authored by Timothy LAIRD's avatar Timothy LAIRD

bugfix + elimination player w/o node

parent 679eff42
...@@ -18,7 +18,7 @@ include_directories(${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/src) ...@@ -18,7 +18,7 @@ include_directories(${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/src)
include_directories( ${PROJECT_SOURCE_DIR}/dpd/include ) include_directories( ${PROJECT_SOURCE_DIR}/dpd/include )
link_directories( ${PROJECT_SOURCE_DIR}/dpd ) link_directories( ${PROJECT_SOURCE_DIR}/dpd )
add_executable(nw-viewer src/main-viewer.c src/networld.c src/controlpanel.c src/entity.c src/player.c) add_executable(nw-viewer src/main-viewer.c src/networld.c src/controlpanel.c src/entity.c src/player.c src/random-map.c)
target_link_libraries(nw-viewer raylib pthread dl rt X11 m) target_link_libraries(nw-viewer raylib pthread dl rt X11 m)
#without cmake package... #without cmake package...
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
#include "networld.h" #include "networld.h"
#include "player.h" #include "player.h"
#include "controlpanel.h" #include "controlpanel.h"
#include "randomMap.h" #include "random-map.h"
// Program attributes // Program attributes
//------------------- //-------------------
...@@ -30,9 +30,10 @@ bool game_end; ...@@ -30,9 +30,10 @@ bool game_end;
int main(int nbArg, char ** arg) int main(int nbArg, char ** arg)
{ {
int n = 5; int nodeCount = 5;
NetWorld * world= NetWorld_new(n); int playerCount = 3;
world = Random_map(world, n); NetWorld * world= NetWorld_new(nodeCount, playerCount);
Random_map(world);
//Node_set(&(world->nodes[0]), (Vector2){1.4f, 1.28f}, ORANGE); //Node_set(&(world->nodes[0]), (Vector2){1.4f, 1.28f}, ORANGE);
...@@ -46,11 +47,6 @@ int main(int nbArg, char ** arg) ...@@ -46,11 +47,6 @@ int main(int nbArg, char ** arg)
// Game Initialization // Game Initialization
//-------------------- //--------------------
game_end= false; game_end= false;
//NetWorld_connect(world, 0, 1);
//NetWorld_connect(world, 1, 2);
//NetWorld_biconnect(world, 2, 0);
Panel * panel= Panel_new(); Panel * panel= Panel_new();
Panel_initialize(panel, world); Panel_initialize(panel, world);
...@@ -60,26 +56,11 @@ int main(int nbArg, char ** arg) ...@@ -60,26 +56,11 @@ int main(int nbArg, char ** arg)
for(int index = 0; index < world->size; index++){ for(int index = 0; index < world->size; index++){
Player_add_Node(&(players[index % playerCount]), &(world->nodes[index])); Player_add_Node(&(players[index % playerCount]), &(world->nodes[index]));
} }
Player_remove_Node(&(players[0]), &(world->nodes[3]));
Player_add_Node(&(players[1]), &(world->nodes[3]));
// Raylib Initialization // Raylib Initialization
//---------------------- //----------------------
InitWindow(screenWidth, screenHeight, "NetWorld basic viewer"); InitWindow(screenWidth, screenHeight, "NetWorld basic viewer");
SetTargetFPS(targetFPS); SetTargetFPS(targetFPS);
// Some verificcations
//--------------------
/*
puts("world variable:");
NetWorld_print(world);
puts("world expected:\n[10.4, 12.8]\n[110.4, 52.8]\n[384.5, 422.2]");
Panel_print(panel);
Vector2 position= {1.f, 1.f};
position= Panel_pixelFromPosition( panel, &position );
printf("[%.2f,%.2f]\n", position.x, position.y);
*/
// Main game loop // Main game loop
world->currentPlayer = 0; world->currentPlayer = 0;
Player_start_turn(&(players[world->currentPlayer])); Player_start_turn(&(players[world->currentPlayer]));
...@@ -109,6 +90,7 @@ void game_update(NetWorld * world, Player * players) ...@@ -109,6 +90,7 @@ void game_update(NetWorld * world, Player * players)
{ {
Player * currentPlayer = &(players[world->currentPlayer]); Player * currentPlayer = &(players[world->currentPlayer]);
int playerCount = world->playerCount; int playerCount = world->playerCount;
Player * newPlayers = malloc(sizeof(Player));
switch(currentPlayer->turnPhase){ switch(currentPlayer->turnPhase){
case 1: case 1:
if(currentPlayer->soldiers > 0){ if(currentPlayer->soldiers > 0){
...@@ -178,16 +160,15 @@ void game_update(NetWorld * world, Player * players) ...@@ -178,16 +160,15 @@ void game_update(NetWorld * world, Player * players)
} }
} }
break; break;
case -1 :
Player_end_turn(currentPlayer);
printf("Turn ended\n");
world->currentPlayer = (world->currentPlayer + 1) % playerCount;
printf("Player changed\n");
Player_start_turn(&(players[world->currentPlayer]));
printf("Turn started\n");
printf("Recrutement phase\n");
break;
default:; default:;
} }
if(currentPlayer->turnPhase == -1){
Player_end_turn(currentPlayer);
printf("Turn ended\n");
world->currentPlayer = (world->currentPlayer + 1) % playerCount;
printf("Player changed\n");
Player_start_turn(&(players[world->currentPlayer]));
printf("Turn started\n");
printf("Recrutement phase\n");
}
} }
...@@ -54,18 +54,18 @@ void Player_add_Node( Player * self, Node * node ) ...@@ -54,18 +54,18 @@ void Player_add_Node( Player * self, Node * node )
void Player_remove_Node( Player * self, Node * node ) void Player_remove_Node( Player * self, Node * node )
{ {
Node ** newNodes = malloc(sizeof(Node*)*(self->nodeCount - 1));
bool nodeFound = false; bool nodeFound = false;
for( int i = 0 ; i < self->nodeCount-1 ; i++ ) for( int i = 0 ; i < self->nodeCount-1 ; i++ )
{ {
if(nodeFound || self->nodes[i] == node){ if(!nodeFound && self->nodes[i] == node){
self->nodes[i] = self->nodes[i+1]; nodeFound = true;
}
if(nodeFound){
newNodes[i] = self->nodes[i+1];
}else{
newNodes[i] = self->nodes[i];
} }
}
Node ** newNodes = malloc(sizeof(Node*)*(self->nodeCount - 1));
for( int i = 0 ; i < self->nodeCount-1 ; i++ )
{
newNodes[i] = self->nodes[i];
} }
free(self->nodes); free(self->nodes);
...@@ -75,14 +75,19 @@ void Player_remove_Node( Player * self, Node * node ) ...@@ -75,14 +75,19 @@ void Player_remove_Node( Player * self, Node * node )
} }
void Player_start_turn(Player * self){ void Player_start_turn(Player * self){
int totalSoldierCount = 0; if(self->nodeCount == 0){
for(int nodeIndex = 0; nodeIndex < self->nodeCount; nodeIndex++){ self->turnPhase = -1;
totalSoldierCount += self->nodes[nodeIndex]->soldiers; }else{
int totalSoldierCount = 0;
for(int nodeIndex = 0; nodeIndex < self->nodeCount; nodeIndex++){
totalSoldierCount += self->nodes[nodeIndex]->soldiers;
}
self->soldiers = fmax(1, totalSoldierCount/3);
self->turnPhase = 1;
printf("Current player : %i\n", self->ID);
printf("Current player soldiers : %i\n", self->soldiers);
} }
self->soldiers = fmax(1, totalSoldierCount/3);
self->turnPhase = 1;
printf("Current player : %i\n", self->ID);
printf("Current player soldiers : %i\n", self->soldiers);
} }
void Player_end_turn(Player * self){ void Player_end_turn(Player * self){
......
#include "randomMap.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <time.h>
#include "networld.h" #include "networld.h"
#include "controlpanel.h" #include "controlpanel.h"
#include "random-map.h"
#include "raylib.h" #include "raylib.h"
...@@ -21,14 +22,14 @@ double randfrom(double min, double max) ...@@ -21,14 +22,14 @@ double randfrom(double min, double max)
double dist(Node node1, Node node2){ double dist(Node node1, Node node2){
return(sqrt( (node2.position.x - node1.position.x)*(node2.position.x - node1.position.x) + (node2.position.y - node1.position.y)*(node2.position.y - node1.position.y) )); return(sqrt( (node2.position.x - node1.position.x)*(node2.position.x - node1.position.x) + (node2.position.y - node1.position.y)*(node2.position.y - node1.position.y) ));
} }
NetWorld * Random_map(NetWorld * world, int nbNode) void Random_map(NetWorld * world)
{ {
int i ; int nbNode = world->size;
float randomX, randomY; float randomX, randomY;
/* Intializes random number generator */ /* Intializes random number generator */
srand (time ( NULL)); srand(time(NULL));
/* Print 5 random numbers from -15.00 to 15.00 */ /* Print 5 random numbers from -15.00 to 15.00 */
for( i = 0 ; i < nbNode ; i++ ) { for( int i = 0 ; i < nbNode ; i++ ) {
double randomX = randfrom(-15.0, 15.0); double randomX = randfrom(-15.0, 15.0);
double randomY = randfrom(-15.0, 15.0); double randomY = randfrom(-15.0, 15.0);
char * name; char * name;
...@@ -37,15 +38,8 @@ NetWorld * Random_map(NetWorld * world, int nbNode) ...@@ -37,15 +38,8 @@ NetWorld * Random_map(NetWorld * world, int nbNode)
Node_set( &(world->nodes[i]), (Vector2){randomX, randomY}, RED, name); Node_set( &(world->nodes[i]), (Vector2){randomX, randomY}, RED, name);
printf("%f\n", randomX); printf("%f\n", randomX);
printf("%f\n", randomY); printf("%f\n", randomY);
/* if(i!= nbNode-1){
NetWorld_connect(world, i, i+1);
}
else{
NetWorld_connect(world, i, 0);
} */
} }
//Gabriel graph sort //Gabriel graph sort
for( int i = 0; i <nbNode-1 ; i++){ for( int i = 0; i <nbNode-1 ; i++){
for( int j = i+1; j <nbNode ; j++){ for( int j = i+1; j <nbNode ; j++){
int compteurPointDansLeCercle = 0; int compteurPointDansLeCercle = 0;
...@@ -56,11 +50,7 @@ NetWorld * Random_map(NetWorld * world, int nbNode) ...@@ -56,11 +50,7 @@ NetWorld * Random_map(NetWorld * world, int nbNode)
printf("ym : %f\n", ym); printf("ym : %f\n", ym);
printf("distCenterOfij : %f\n", distCenterOfij); printf("distCenterOfij : %f\n", distCenterOfij);
printf("dist %d%d : %f\n", i ,j ,dist((world->nodes[i]),world->nodes[j])); printf("dist %d%d : %f\n", i ,j ,dist((world->nodes[i]),world->nodes[j]));
//printf("dist sqrt : %f\n", sqrt(dist((world->nodes[i]),world->nodes[p])*dist((world->nodes[i]),world->nodes[p]) + dist((world->nodes[j]),world->nodes[p])*dist((world->nodes[j]),world->nodes[p])));
for(int k = 0 ; k < nbNode ; k++){ for(int k = 0 ; k < nbNode ; k++){
/* if((xm - distCenterOfij < world->nodes[k].position.x < xm + distCenterOfij) && (ym - distCenterOfij < world->nodes[k].position.y < ym + distCenterOfij) ){
compteurPointDansLeCercle ++;
} */
printf(" dist center - Node k %f\n", sqrt((world->nodes[k].position.x - xm)*(world->nodes[k].position.x - xm) + (world->nodes[k].position.y - ym)*(world->nodes[k].position.y - ym))); printf(" dist center - Node k %f\n", sqrt((world->nodes[k].position.x - xm)*(world->nodes[k].position.x - xm) + (world->nodes[k].position.y - ym)*(world->nodes[k].position.y - ym)));
if( sqrt((world->nodes[k].position.x - xm)*(world->nodes[k].position.x - xm) + (world->nodes[k].position.y - ym)*(world->nodes[k].position.y - ym)) < distCenterOfij -0.05){ if( sqrt((world->nodes[k].position.x - xm)*(world->nodes[k].position.x - xm) + (world->nodes[k].position.y - ym)*(world->nodes[k].position.y - ym)) < distCenterOfij -0.05){
compteurPointDansLeCercle ++; compteurPointDansLeCercle ++;
...@@ -68,11 +58,10 @@ NetWorld * Random_map(NetWorld * world, int nbNode) ...@@ -68,11 +58,10 @@ NetWorld * Random_map(NetWorld * world, int nbNode)
} }
printf("compteurPointDansLeCercle : %i\n", compteurPointDansLeCercle); printf("compteurPointDansLeCercle : %i\n", compteurPointDansLeCercle);
if(compteurPointDansLeCercle == 0){ if(compteurPointDansLeCercle == 0){
NetWorld_connect(world, i, j); NetWorld_biconnect(world, i, j);
} }
} }
} }
return world;
} }
\ No newline at end of file
#ifndef RANDOMMAP_H
#define RANDOMMAP_H
#include "raylib.h" #include "raylib.h"
#include "networld.h" #include "networld.h"
void Random_map(NetWorld * world);
NetWorld * Random_map(NetWorld * world, int nbNode);
double randfrom(double min, double max); double randfrom(double min, double max);
double dist(Node node1, Node node2); double dist(Node node1, Node node2);
\ No newline at end of file
#endif
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment