├── arena3 ├── arena.sh ├── arena2 ├── .gitattributes ├── arena1 ├── f_lemipc ├── src │ ├── error.c │ ├── msgq.c │ ├── random.c │ ├── check_player.c │ ├── coord.c │ ├── lemipc.c │ ├── launch_player.c │ └── algorithm.c ├── Makefile └── inc │ └── lemipc.h ├── f_graphic ├── src │ ├── error.c │ └── graphic.c ├── Makefile └── inc │ └── graphic.h ├── Makefile └── .gitignore /arena3: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 6 | 6 7 | 7 8 | 8 9 | 9 10 | 1 11 | 1 12 | 1 13 | 1 14 | 1 15 | 1 16 | 1 17 | 1 18 | 1 19 | -------------------------------------------------------------------------------- /arena.sh: -------------------------------------------------------------------------------- 1 | if [ "null$1" == "null" ] 2 | then 3 | echo "USAGE: ./arena.sh [FILE]" 4 | else 5 | while read player 6 | do 7 | ./lemipc $player & 8 | done < $1 9 | ./graphic & 10 | fi 11 | -------------------------------------------------------------------------------- /arena2: -------------------------------------------------------------------------------- 1 | 1 2 | 3 3 | 5 4 | 10 5 | 20 6 | 255 7 | 1 8 | 3 9 | 5 10 | 10 11 | 20 12 | 255 13 | 1 14 | 3 15 | 5 16 | 10 17 | 20 18 | 255 19 | 1 20 | 3 21 | 5 22 | 10 23 | 20 24 | 255 25 | 1 26 | 3 27 | 5 28 | 10 29 | 20 30 | 255 31 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /arena1: -------------------------------------------------------------------------------- 1 | 3 2 | 255 3 | 3 4 | 255 5 | 3 6 | 255 7 | 3 8 | 255 9 | 3 10 | 255 11 | 3 12 | 255 13 | 3 14 | 255 15 | 3 16 | 255 17 | 3 18 | 255 19 | 3 20 | 255 21 | 3 22 | 255 23 | 3 24 | 255 25 | 3 26 | 255 27 | 3 28 | 255 29 | 3 30 | 255 31 | 3 32 | 255 33 | 3 34 | 255 35 | 3 36 | 255 37 | 3 38 | 255 39 | 3 40 | 255 41 | 3 42 | 255 43 | 3 44 | 255 45 | 3 46 | 255 47 | 3 48 | 255 49 | 3 50 | 255 51 | 3 52 | 255 53 | 3 54 | 255 55 | 3 56 | 255 57 | 3 58 | 255 59 | 3 60 | 255 61 | 3 62 | 255 63 | 3 64 | 255 65 | 3 66 | 255 67 | -------------------------------------------------------------------------------- /f_lemipc/src/error.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** error.c for error.c in /home/loeb_t/rendu/PSU_2013_lemipc 3 | ** 4 | ** Made by LOEB Thomas 5 | ** Login 6 | ** 7 | ** Started on Wed Mar 19 19:05:38 2014 LOEB Thomas 8 | ** Last update Wed Mar 19 19:06:26 2014 LOEB Thomas 9 | */ 10 | 11 | #include "lemipc.h" 12 | 13 | int lemipc_error(char *message) 14 | { 15 | fprintf(stderr, "%s\n", message); 16 | return (EXIT_FAILURE); 17 | } 18 | 19 | int lemipc_perror(char *message) 20 | { 21 | perror(message); 22 | return (EXIT_FAILURE); 23 | } 24 | -------------------------------------------------------------------------------- /f_graphic/src/error.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** error.c for error.c in /home/loeb_t/rendu/PSU_2013_lemipc/f_graphic 3 | ** 4 | ** Made by LOEB Thomas 5 | ** Login 6 | ** 7 | ** Started on Sat Mar 22 13:46:24 2014 LOEB Thomas 8 | ** Last update Sat Mar 22 13:47:13 2014 LOEB Thomas 9 | */ 10 | 11 | #include "graphic.h" 12 | 13 | int graphic_error(char *message) 14 | { 15 | fprintf(stderr, "%s\n", message); 16 | return (EXIT_FAILURE); 17 | } 18 | 19 | int graphic_perror(char *message) 20 | { 21 | perror(message); 22 | return (EXIT_FAILURE); 23 | } 24 | -------------------------------------------------------------------------------- /f_graphic/Makefile: -------------------------------------------------------------------------------- 1 | ## 2 | ## Makefile for Makefile in /home/loeb_t/rendu/PSU_2013_lemipc 3 | ## 4 | ## Made by LOEB Thomas 5 | ## Login 6 | ## 7 | ## Started on Wed Mar 19 14:51:36 2014 LOEB Thomas 8 | ## Last update Sat Mar 22 13:46:21 2014 LOEB Thomas 9 | ## 10 | 11 | NAME = graphic 12 | 13 | F_SRC = src/ 14 | 15 | F_INC = inc/ 16 | 17 | SRC = $(F_SRC)graphic.c \ 18 | $(F_SRC)error.c 19 | 20 | OBJ = $(SRC:.c=.o) 21 | 22 | CFLAGS = -Wall -Werror -Wextra -I $(F_INC) 23 | 24 | SDL = -lSDL 25 | 26 | all: $(NAME) 27 | 28 | $(NAME): $(OBJ) 29 | cc -o $(NAME) $(OBJ) $(SDL) 30 | 31 | clean: 32 | rm -f $(OBJ) 33 | 34 | fclean: clean 35 | rm -f $(NAME) 36 | 37 | re: fclean all 38 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ## 2 | ## Makefile for Makefile in /home/loeb_t/rendu/PSU_2013_lemipc 3 | ## 4 | ## Made by LOEB Thomas 5 | ## Login 6 | ## 7 | ## Started on Sat Mar 22 13:17:49 2014 LOEB Thomas 8 | ## Last update Sun Mar 23 16:02:42 2014 abgral_f 9 | ## 10 | 11 | PROG = lemipc 12 | 13 | F_PROG = f_$(PROG)/ 14 | 15 | GUI = graphic 16 | 17 | F_GUI = f_$(GUI)/ 18 | 19 | all: $(PROG) $(GUI) 20 | 21 | $(PROG): 22 | make -C $(F_PROG) all 23 | cp $(F_PROG)$(PROG) . 24 | 25 | $(GUI): 26 | make -C $(F_GUI) all 27 | cp $(F_GUI)$(GUI) . 28 | 29 | clean: 30 | make -C $(F_PROG) clean 31 | make -C $(F_GUI) clean 32 | 33 | fclean: 34 | rm -f $(PROG) 35 | rm -f $(GUI) 36 | make -C $(F_PROG) fclean 37 | make -C $(F_GUI) fclean 38 | 39 | re: fclean all 40 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /f_lemipc/Makefile: -------------------------------------------------------------------------------- 1 | ## 2 | ## Makefile for Makefile in /home/loeb_t/rendu/PSU_2013_lemipc 3 | ## 4 | ## Made by LOEB Thomas 5 | ## Login 6 | ## 7 | ## Started on Wed Mar 19 14:51:36 2014 LOEB Thomas 8 | ## Last update Sun Mar 23 16:02:55 2014 abgral_f 9 | ## 10 | 11 | NAME = lemipc 12 | 13 | F_SRC = src/ 14 | 15 | F_INC = inc/ 16 | 17 | SRC = $(F_SRC)lemipc.c \ 18 | $(F_SRC)launch_player.c \ 19 | $(F_SRC)check_player.c \ 20 | $(F_SRC)algorithm.c \ 21 | $(F_SRC)random.c \ 22 | $(F_SRC)error.c \ 23 | $(F_SRC)msgq.c \ 24 | $(F_SRC)coord.c 25 | 26 | OBJ = $(SRC:.c=.o) 27 | 28 | CFLAGS = -Wall -Werror -Wextra -I $(F_INC) 29 | 30 | MATH = -lm 31 | 32 | all: $(NAME) 33 | 34 | $(NAME): $(OBJ) 35 | cc -o $(NAME) $(OBJ) $(MATH) 36 | 37 | clean: 38 | rm -f $(OBJ) 39 | 40 | fclean: clean 41 | rm -f $(NAME) 42 | 43 | re: fclean all 44 | -------------------------------------------------------------------------------- /f_lemipc/src/msgq.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** msgq.c for msgq.c in /home/abgral_f/PSU_2013_lemipc/f_lemipc/src 3 | ** 4 | ** Made by abgral_f 5 | ** Login 6 | ** 7 | ** Started on Sun Mar 23 15:57:38 2014 abgral_f 8 | ** Last update Mon Mar 24 14:33:04 2014 LOEB Thomas 9 | */ 10 | 11 | #include "lemipc.h" 12 | 13 | int send_msg(t_lemipc *lemipc) 14 | { 15 | t_msg msg; 16 | 17 | msg.type = lemipc->killed_by; 18 | memset(msg.no_team, 0, MSG_LEN); 19 | sprintf(msg.no_team, MSG, lemipc->no_team, lemipc->killed_by); 20 | if (msgsnd(lemipc->msg_id, &msg, sizeof(msg), 0) == -1) 21 | return (lemipc_error(MSGSND_ERROR)); 22 | return (EXIT_SUCCESS); 23 | } 24 | 25 | void recieve_msg(t_lemipc *lemipc) 26 | { 27 | t_msg msg; 28 | 29 | if (msgrcv(lemipc->msg_id, &msg, sizeof(msg), 30 | lemipc->no_team, IPC_NOWAIT) != -1) 31 | printf("%s\n", msg.no_team); 32 | } 33 | -------------------------------------------------------------------------------- /f_lemipc/src/random.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** random.c for random.c in /home/loeb_t/rendu/PSU_2013_lemipc/f_lemipc 3 | ** 4 | ** Made by LOEB Thomas 5 | ** Login 6 | ** 7 | ** Started on Sat Mar 22 17:06:02 2014 LOEB Thomas 8 | ** Last update Sun Mar 23 15:27:41 2014 LOEB Thomas 9 | */ 10 | 11 | #include "lemipc.h" 12 | 13 | static int switch_char(t_lemipc *lemipc, int incr, 14 | char rand, char check) 15 | { 16 | if ((check & rand) == rand) 17 | return (rand); 18 | if (((rand == 1 && lemipc->pos % WIN_SIZE_X + 1 < WIN_SIZE_X) || 19 | (rand == 2 && (int)lemipc->pos % WIN_SIZE_X - 1 >= 0) || 20 | (rand == 4 && lemipc->pos / WIN_SIZE_X + 1 < WIN_SIZE_Y) || 21 | (rand == 8 && (int)lemipc->pos / WIN_SIZE_X - 1 >= 0)) && 22 | lemipc->map[lemipc->pos + incr] == 0) 23 | { 24 | lemipc->map[lemipc->pos] = 0; 25 | lemipc->pos += incr; 26 | lemipc->map[lemipc->pos] = lemipc->no_team; 27 | return (15); 28 | } 29 | else 30 | return (rand); 31 | } 32 | 33 | static int g_inc[NB_DIRECTIONS] = 34 | { 35 | 1, 36 | -1, 37 | WIN_SIZE_X, 38 | -WIN_SIZE_X 39 | }; 40 | 41 | void move_random(t_lemipc *lemipc) 42 | { 43 | unsigned char direction; 44 | unsigned char check; 45 | 46 | check = 0; 47 | while (check < 15) 48 | { 49 | direction = rand() % NB_DIRECTIONS; 50 | check |= switch_char(lemipc, g_inc[direction], pow(2, direction), check); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /f_graphic/inc/graphic.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** graphic.h for graphic.h in /home/loeb_t/rendu/PSU_2013_lemipc/f_graphic 3 | ** 4 | ** Made by LOEB Thomas 5 | ** Login 6 | ** 7 | ** Started on Sat Mar 22 13:30:32 2014 LOEB Thomas 8 | ** Last update Sun Mar 23 18:35:39 2014 abgral_f 9 | */ 10 | 11 | #ifndef GRAPHIC_H_ 12 | # define GRAPHIC_H_ 13 | 14 | # include 15 | # include 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | # include 23 | 24 | # define KEY_GEN "/" 25 | # define GAME_NAME "lemipc | loeb_t - abgral_f" 26 | # define SIZE_SPRITE 32 27 | # define WIN_SIZE_X 30 28 | # define WIN_SIZE_Y 30 29 | # define MAP_SIZE (WIN_SIZE_X * WIN_SIZE_Y) 30 | # define WIN_X (WIN_SIZE_X * SIZE_SPRITE) 31 | # define WIN_Y (WIN_SIZE_Y * SIZE_SPRITE) 32 | # define FLG(x) (x * IPC_CREAT) | SHM_R | SHM_W 33 | # define SLEEP 75000 34 | 35 | typedef enum e_bool 36 | { 37 | TRUE = 0, 38 | FALSE = 1 39 | } bool; 40 | 41 | typedef struct s_graphic 42 | { 43 | key_t key; 44 | int shm_id; 45 | unsigned char *map; 46 | SDL_Surface *screen; 47 | SDL_Rect rect; 48 | } t_graphic; 49 | 50 | /* 51 | ** PROTOTYPES 52 | */ 53 | 54 | /* 55 | ** ERROR_HANDLING 56 | */ 57 | 58 | int graphic_error(char *); 59 | 60 | # define USAGE "USAGE: ./graphic [PATH]" 61 | 62 | int graphic_perror(char *); 63 | 64 | # define FTOK_ERROR "ERROR: System call 'ftok' failed" 65 | # define SHMGET_ERROR "ERROR: System call 'shmget' failed" 66 | # define SHMAT_ERROR "ERROR: System call 'shmat' failed" 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /f_lemipc/src/check_player.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** check_player.c for check_player.c in /home/loeb_t/rendu/PSU_2013_lemipc 3 | ** 4 | ** Made by LOEB Thomas 5 | ** Login 6 | ** 7 | ** Started on Fri Mar 21 16:40:51 2014 LOEB Thomas 8 | ** Last update Mon Mar 24 14:40:10 2014 LOEB Thomas 9 | */ 10 | 11 | #include "lemipc.h" 12 | 13 | static bool check_around(unsigned char around[8], 14 | t_lemipc *lemipc) 15 | { 16 | unsigned char count; 17 | unsigned char tmp; 18 | 19 | count = -1; 20 | while (++count < 8) 21 | { 22 | tmp = -1; 23 | while (++tmp < 8) 24 | if (tmp != count && 25 | around[tmp] == around[count] && 26 | around[tmp] != 0 && around[tmp] != lemipc->no_team) 27 | { 28 | lemipc->killed_by = around[tmp]; 29 | return (TRUE); 30 | } 31 | } 32 | return (FALSE); 33 | } 34 | 35 | bool player_is_surrounded(t_lemipc *lemipc) 36 | { 37 | unsigned char around[8]; 38 | 39 | memset(around, 0, 8); 40 | if (lemipc->pos >= WIN_SIZE_X) 41 | { 42 | if (lemipc->pos % WIN_SIZE_X > 0) 43 | around[0] = lemipc->map[lemipc->pos - WIN_SIZE_X - 1]; 44 | around[1] = lemipc->map[lemipc->pos - WIN_SIZE_X]; 45 | if (lemipc->pos % WIN_SIZE_X < WIN_SIZE_X - 1) 46 | around[2] = lemipc->map[lemipc->pos - WIN_SIZE_X + 1]; 47 | } 48 | if (lemipc->pos % WIN_SIZE_X > 0) 49 | around[3] = lemipc->map[lemipc->pos - 1]; 50 | if (lemipc->pos % WIN_SIZE_X < WIN_SIZE_X - 1) 51 | around[4] = lemipc->map[lemipc->pos + 1]; 52 | if (lemipc->pos < MAP_SIZE - WIN_SIZE_X) 53 | { 54 | if (lemipc->pos % WIN_SIZE_X > 0) 55 | around[5] = lemipc->map[lemipc->pos + WIN_SIZE_X - 1]; 56 | around[6] = lemipc->map[lemipc->pos + WIN_SIZE_X]; 57 | if (lemipc->pos % WIN_SIZE_X < WIN_SIZE_X - 1) 58 | around[7] = lemipc->map[lemipc->pos + WIN_SIZE_X + 1]; 59 | } 60 | return (check_around(around, lemipc)); 61 | } 62 | 63 | bool player_is_alone(t_lemipc *lemipc, bool flag) 64 | { 65 | unsigned int count; 66 | 67 | count = -1; 68 | while (++count < MAP_SIZE) 69 | if (lemipc->map[count] != 0 && 70 | (flag == TRUE || lemipc->map[count] != lemipc->no_team)) 71 | return (FALSE); 72 | return (TRUE); 73 | } 74 | -------------------------------------------------------------------------------- /f_lemipc/src/coord.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** coord.c for coord.c in /home/abgral_f/PSU_2013_lemipc/f_lemipc/src 3 | ** 4 | ** Made by abgral_f 5 | ** Login 6 | ** 7 | ** Started on Sun Mar 23 13:44:42 2014 abgral_f 8 | ** Last update Mon Mar 24 15:42:57 2014 LOEB Thomas 9 | */ 10 | 11 | #include "lemipc.h" 12 | 13 | static void assign_new_pos(t_lemipc *lemipc, int prio, int other) 14 | { 15 | if (lemipc->map[prio + lemipc->pos] == 0) 16 | { 17 | lemipc->map[lemipc->pos] = 0; 18 | lemipc->pos = prio + lemipc->pos; 19 | lemipc->map[lemipc->pos] = lemipc->no_team; 20 | } 21 | else if (lemipc->map[other + lemipc->pos] == 0) 22 | { 23 | lemipc->map[lemipc->pos] = 0; 24 | lemipc->pos = other + lemipc->pos; 25 | lemipc->map[lemipc->pos] = lemipc->no_team; 26 | } 27 | else 28 | move_random(lemipc); 29 | } 30 | 31 | static int compare_coord(int coord_one, int coord_two) 32 | { 33 | if (coord_one < coord_two) 34 | return (1); 35 | if (coord_one > coord_two) 36 | return (-1); 37 | return (0); 38 | } 39 | 40 | void move_to(int pos, t_lemipc *lemipc) 41 | { 42 | int move_x; 43 | int move_y; 44 | unsigned int rand_pos; 45 | 46 | move_x = compare_coord((int)lemipc->pos % WIN_SIZE_X, pos % WIN_SIZE_X); 47 | move_y = compare_coord((int)lemipc->pos / WIN_SIZE_X, pos / WIN_SIZE_X); 48 | if (move_x == 0) 49 | assign_new_pos(lemipc, move_y * WIN_SIZE_X, move_y * WIN_SIZE_X); 50 | else if (move_y == 0) 51 | assign_new_pos(lemipc, move_x, move_x); 52 | else 53 | { 54 | rand_pos = rand() % 2; 55 | if (rand_pos == 0) 56 | assign_new_pos(lemipc, move_x, move_y * WIN_SIZE_X); 57 | else 58 | assign_new_pos(lemipc, move_y * WIN_SIZE_X, move_x); 59 | } 60 | } 61 | 62 | bool is_less_far(t_lemipc *lemipc, int pos_one, int pos_two) 63 | { 64 | int x_one; 65 | int y_one; 66 | int x_two; 67 | int y_two; 68 | 69 | if ((x_one = lemipc->pos % WIN_SIZE_X - pos_one % WIN_SIZE_X) < 0) 70 | x_one *= -1; 71 | if ((y_one = lemipc->pos / WIN_SIZE_X - pos_one / WIN_SIZE_X) < 0) 72 | y_one *= -1; 73 | if ((x_two = lemipc->pos % WIN_SIZE_X - pos_two % WIN_SIZE_X) < 0) 74 | x_two *= -1; 75 | if ((y_two = lemipc->pos / WIN_SIZE_X - pos_two / WIN_SIZE_X) < 0) 76 | y_two *= -1; 77 | if ((y_two + x_two) < (y_one + x_one)) 78 | return (TRUE); 79 | return (FALSE); 80 | } 81 | -------------------------------------------------------------------------------- /f_lemipc/src/lemipc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** main.c for main.c in /home/loeb_t/rendu/PSU_2013_lemipc 3 | ** 4 | ** Made by LOEB Thomas 5 | ** Login 6 | ** 7 | ** Started on Wed Mar 19 14:52:56 2014 LOEB Thomas 8 | ** Last update Tue Feb 10 09:48:34 2015 abgral_f 9 | */ 10 | 11 | #include "lemipc.h" 12 | 13 | static int get_sem(t_lemipc *lemipc) 14 | { 15 | if ((lemipc->sem_id = semget(lemipc->key, 1, FLG(0))) == -1) 16 | { 17 | if ((lemipc->sem_id = semget(lemipc->key, 1, FLG(1))) == -1) 18 | return (lemipc_perror(SEMGET_ERROR)); 19 | else if (semctl(lemipc->sem_id, 0, SETVAL, 1) == -1) 20 | return (lemipc_perror(SEMCTL_ERROR)); 21 | } 22 | return (EXIT_SUCCESS); 23 | } 24 | 25 | static int get_shm(t_lemipc *lemipc) 26 | { 27 | unsigned int count; 28 | 29 | if ((lemipc->shm_id = shmget(lemipc->key, MAP_SIZE, FLG(0))) == -1) 30 | if ((lemipc->shm_id = shmget(lemipc->key, MAP_SIZE, FLG(1))) == -1) 31 | return (lemipc_perror(SHMGET_ERROR)); 32 | else if ((lemipc->map = shmat(lemipc->shm_id, NULL, FLG(0))) == (void *)-1) 33 | return (lemipc_perror(SHMAT_ERROR)); 34 | else 35 | { 36 | count = -1; 37 | while (++count < MAP_SIZE) 38 | lemipc->map[count] = 0; 39 | } 40 | else 41 | if ((lemipc->map = shmat(lemipc->shm_id, NULL, FLG(0))) == (void *)-1) 42 | return (lemipc_perror(SHMAT_ERROR)); 43 | return (EXIT_SUCCESS); 44 | } 45 | 46 | static int get_msgq(t_lemipc *lemipc) 47 | { 48 | if ((lemipc->msg_id = msgget(lemipc->key, FLG(0))) == -1) 49 | if ((lemipc->msg_id = msgget(lemipc->key, FLG(1))) == -1) 50 | return (lemipc_perror(MSGGET_ERROR)); 51 | return (EXIT_SUCCESS); 52 | } 53 | 54 | static int lemipc_bsc(int no_team, char *path) 55 | { 56 | t_lemipc lemipc; 57 | 58 | if (no_team < 1 || no_team > 255) 59 | return (lemipc_error(NO_TEAM_ERROR)); 60 | if ((lemipc.key = ftok(path, 0)) == -1) 61 | return (lemipc_perror(FTOK_ERROR)); 62 | lemipc.no_team = no_team; 63 | lemipc.pos = -1; 64 | lemipc.killed_by = 0; 65 | lemipc.is_alone = TRUE; 66 | lemipc.is_dead = FALSE; 67 | if (get_shm(&lemipc) == EXIT_FAILURE || 68 | get_sem(&lemipc) == EXIT_FAILURE || 69 | get_msgq(&lemipc) == EXIT_FAILURE || 70 | launch_player(&lemipc) == EXIT_FAILURE) 71 | return (EXIT_FAILURE); 72 | return (EXIT_SUCCESS); 73 | } 74 | 75 | int main(int ac, char **av) 76 | { 77 | if (ac != 2 && ac != 3) 78 | return (lemipc_error(USAGE)); 79 | srand(clock()); 80 | return (lemipc_bsc(atoi(av[1]), (av[2] == NULL) ? KEY_GEN : av[2])); 81 | } 82 | -------------------------------------------------------------------------------- /f_lemipc/src/launch_player.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** launch_player.c for launch_player.c in /home/abgral_f/dev/lemipc2/src 3 | ** 4 | ** Made by abgral_f 5 | ** Login 6 | ** 7 | ** Started on Thu Mar 20 14:45:54 2014 abgral_f 8 | ** Last update Mon Mar 24 14:31:27 2014 LOEB Thomas 9 | */ 10 | 11 | #include "lemipc.h" 12 | 13 | static int put_player_on_map(t_lemipc *lemipc) 14 | { 15 | unsigned int rand_pos; 16 | 17 | rand_pos = rand() % MAP_SIZE; 18 | lemipc->pos = rand_pos; 19 | while (lemipc->map[lemipc->pos] != 0) 20 | { 21 | lemipc->pos = (lemipc->pos + 1) % MAP_SIZE; 22 | if (lemipc->pos == rand_pos) 23 | return (lemipc_error(NO_SPACE_ERROR)); 24 | } 25 | lemipc->map[lemipc->pos] = lemipc->no_team; 26 | return (EXIT_SUCCESS); 27 | } 28 | 29 | static int set_sem(t_lemipc *lemipc, int status) 30 | { 31 | struct sembuf sem_op; 32 | 33 | sem_op.sem_num = 0; 34 | sem_op.sem_flg = 0; 35 | sem_op.sem_op = status; 36 | if (semop(lemipc->sem_id, &sem_op, 1) == -1) 37 | return (lemipc_perror(SEMOP_ERROR)); 38 | return (EXIT_SUCCESS); 39 | } 40 | 41 | static int player_turn(t_lemipc *lemipc) 42 | { 43 | if (set_sem(lemipc, LOCK) == EXIT_FAILURE) 44 | return (EXIT_FAILURE); 45 | if ((int)lemipc->pos == -1 && put_player_on_map(lemipc) == EXIT_FAILURE) 46 | return (set_sem(lemipc, UNLOCK)); 47 | if (lemipc->is_alone == TRUE) 48 | lemipc->is_alone = player_is_alone(lemipc, FALSE); 49 | else if (player_is_alone(lemipc, FALSE) == TRUE) 50 | { 51 | lemipc->is_dead = TRUE; 52 | lemipc->is_alone = TRUE; 53 | } 54 | if (lemipc->is_alone == FALSE) 55 | { 56 | if (lemipc->is_dead == FALSE) 57 | lemipc->is_dead = player_is_surrounded(lemipc); 58 | if (lemipc->is_dead == FALSE) 59 | move_algorithm(lemipc); 60 | } 61 | usleep(SLEEP); 62 | if (lemipc->killed_by != 0 && send_msg(lemipc) == EXIT_FAILURE) 63 | return (EXIT_FAILURE); 64 | recieve_msg(lemipc); 65 | return (set_sem(lemipc, UNLOCK)); 66 | } 67 | 68 | int launch_player(t_lemipc *lemipc) 69 | { 70 | while (lemipc->is_dead == FALSE) 71 | if (player_turn(lemipc) == EXIT_FAILURE) 72 | return (EXIT_FAILURE); 73 | lemipc->map[lemipc->pos] = 0; 74 | if (lemipc->is_alone == TRUE && player_is_alone(lemipc, TRUE) == TRUE) 75 | { 76 | shmdt(lemipc->map); 77 | usleep(SLEEP); 78 | semctl(lemipc->sem_id, 0, IPC_RMID); 79 | shmctl(lemipc->shm_id, IPC_RMID, NULL); 80 | msgctl(lemipc->msg_id, IPC_RMID, NULL); 81 | } 82 | else 83 | shmdt(lemipc->map); 84 | return (EXIT_SUCCESS); 85 | } 86 | -------------------------------------------------------------------------------- /f_graphic/src/graphic.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** graphic.c for graphic.c in /home/loeb_t/rendu/PSU_2013_lemipc/f_lemipc 3 | ** 4 | ** Made by LOEB Thomas 5 | ** Login 6 | ** 7 | ** Started on Sat Mar 22 13:30:05 2014 LOEB Thomas 8 | ** Last update Thu Mar 27 18:39:16 2014 LOEB Thomas 9 | */ 10 | 11 | #include "graphic.h" 12 | 13 | static int display_map(t_graphic *gui) 14 | { 15 | unsigned int count; 16 | unsigned int color; 17 | 18 | count = -1; 19 | while (++count < MAP_SIZE) 20 | if (gui->map[count] != 0) 21 | { 22 | gui->rect.x = (count % WIN_SIZE_X) * SIZE_SPRITE; 23 | gui->rect.y = (count / WIN_SIZE_X) * SIZE_SPRITE; 24 | color = gui->map[count] * ((0xFFFFFF - 0x222222) / 254) + 0x222222; 25 | if (SDL_FillRect(gui->screen, &gui->rect, 26 | SDL_MapRGB(gui->screen->format, 27 | (color >> 16) & 0xFF, 28 | (color >> 8) & 0xFF, 29 | color & 0xFF)) == -1) 30 | return (graphic_error(SDL_GetError())); 31 | } 32 | return (EXIT_SUCCESS); 33 | } 34 | 35 | static bool map_empty(t_graphic *gui) 36 | { 37 | unsigned int count; 38 | 39 | count = -1; 40 | while (++count < MAP_SIZE) 41 | if (gui->map[count] != 0) 42 | return (FALSE); 43 | return (TRUE); 44 | } 45 | 46 | static int get_shm(t_graphic *gui) 47 | { 48 | if ((gui->shm_id = shmget(gui->key, MAP_SIZE, FLG(0))) == -1) 49 | return (graphic_perror(SHMGET_ERROR)); 50 | else if ((gui->map = shmat(gui->shm_id, NULL, FLG(0))) == (void *)-1) 51 | return (graphic_perror(SHMAT_ERROR)); 52 | return (EXIT_SUCCESS); 53 | } 54 | 55 | static int graphic_bsc(char *path) 56 | { 57 | t_graphic gui; 58 | 59 | if ((gui.key = ftok(path, 0)) == -1) 60 | return (graphic_perror(FTOK_ERROR)); 61 | if (get_shm(&gui) == EXIT_FAILURE) 62 | return (EXIT_FAILURE); 63 | if (SDL_Init(SDL_INIT_VIDEO) == -1 || 64 | (gui.screen = SDL_SetVideoMode(WIN_X, WIN_Y, SIZE_SPRITE, 65 | SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL) 66 | return (graphic_error(SDL_GetError())); 67 | SDL_WM_SetCaption(GAME_NAME, NULL); 68 | gui.rect.w = SIZE_SPRITE; 69 | gui.rect.h = SIZE_SPRITE; 70 | while (map_empty(&gui) == FALSE) 71 | if (SDL_FillRect(gui.screen, NULL, 0) == -1) 72 | return (graphic_error(SDL_GetError())); 73 | else if (display_map(&gui) == EXIT_FAILURE) 74 | return (EXIT_FAILURE); 75 | else if (SDL_Flip(gui.screen) == -1) 76 | return (graphic_error(SDL_GetError())); 77 | else 78 | usleep(SLEEP); 79 | SDL_Quit(); 80 | shmdt(gui.map); 81 | return (EXIT_SUCCESS); 82 | } 83 | 84 | int main(int ac, char **av) 85 | { 86 | if (ac > 2) 87 | return (graphic_error(USAGE)); 88 | return (graphic_bsc((av[1] == NULL) ? KEY_GEN : av[1])); 89 | } 90 | -------------------------------------------------------------------------------- /f_lemipc/inc/lemipc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** lemipc.h for lemipc.h in /home/loeb_t/rendu/PSU_2013_lemipc 3 | ** 4 | ** Made by LOEB Thomas 5 | ** Login 6 | ** 7 | ** Started on Wed Mar 19 14:53:06 2014 LOEB Thomas 8 | ** Last update Mon Mar 24 15:56:02 2014 LOEB Thomas 9 | */ 10 | 11 | #ifndef LEMIPC_H_ 12 | # define LEMIPC_H_ 13 | 14 | # include 15 | # include 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | # include 23 | # include 24 | # include 25 | 26 | # define NB_DIRECTIONS 4 27 | # define KEY_GEN "/" 28 | # define WIN_SIZE_X 30 29 | # define WIN_SIZE_Y 30 30 | # define LARGE_PERIM_A (WIN_SIZE_X * 2) 31 | # define LARGE_PERIM_E 21 32 | # define SHORT_PERIM 3 33 | # define MAP_SIZE (WIN_SIZE_X * WIN_SIZE_Y) 34 | # define LOCK -1 35 | # define UNLOCK 1 36 | # define FLG(x) (x * IPC_CREAT) | SHM_R | SHM_W 37 | # define SLEEP 75000 38 | 39 | # define MSG "A member of the team %d was killed by the team %d !" 40 | # define MSG_LEN 64 41 | 42 | typedef enum e_bool 43 | { 44 | TRUE = 0, 45 | FALSE = 1 46 | } bool; 47 | 48 | typedef struct s_msg 49 | { 50 | long type; 51 | char no_team[MSG_LEN]; 52 | } t_msg; 53 | 54 | typedef struct s_lemipc 55 | { 56 | key_t key; 57 | int shm_id; 58 | int sem_id; 59 | int msg_id; 60 | unsigned char *map; 61 | unsigned char no_team; 62 | unsigned int pos; 63 | bool is_alone; 64 | bool is_dead; 65 | unsigned char killed_by; 66 | } t_lemipc; 67 | 68 | typedef struct s_rand 69 | { 70 | int (*switch_char)(t_lemipc *, int, char, char); 71 | int incr; 72 | } t_rand; 73 | 74 | /* 75 | ** PROTOTYPES 76 | */ 77 | 78 | void recieve_msg(t_lemipc *); 79 | int send_msg(t_lemipc *); 80 | void move_to(int, t_lemipc *); 81 | bool is_less_far(t_lemipc *, int, int); 82 | int launch_player(t_lemipc *); 83 | bool player_is_surrounded(t_lemipc *); 84 | bool player_is_alone(t_lemipc *, bool); 85 | void move_algorithm(t_lemipc *); 86 | void move_random(t_lemipc *); 87 | 88 | /* 89 | ** ERROR_HANDLING 90 | */ 91 | 92 | int lemipc_error(char *); 93 | 94 | # define USAGE "USAGE: ./lemipc [N°TEAM] [PATH]" 95 | # define NO_TEAM_ERROR "ERROR: Team number must be between 1 and 255" 96 | # define NO_SPACE_ERROR "ERROR: No more space on the map" 97 | 98 | int lemipc_perror(char *); 99 | 100 | # define FTOK_ERROR "ERROR: System call 'ftok' failed" 101 | # define SHMGET_ERROR "ERROR: System call 'shmget' failed" 102 | # define SEMGET_ERROR "ERROR: System call 'semget' failed" 103 | # define SHMAT_ERROR "ERROR: System call 'shmat' failed" 104 | # define SEMCTL_ERROR "ERROR: System call 'semctl' failed" 105 | # define SEMOP_ERROR "ERROR: System call 'semop' failed" 106 | # define MSGGET_ERROR "ERROR: System call 'msgget' failed" 107 | # define MSGSND_ERROR "ERROR: System call 'msgsnd' failed" 108 | 109 | #endif 110 | -------------------------------------------------------------------------------- /f_lemipc/src/algorithm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** algorithm.c for algorithm.c in /home/abgral_f/rendu/PSU_2013_lemipc/src 3 | ** 4 | ** Made by abgral_f 5 | ** Login 6 | ** 7 | ** Started on Fri Mar 21 18:19:38 2014 abgral_f 8 | ** Last update Thu Mar 27 18:47:17 2014 LOEB Thomas 9 | */ 10 | 11 | #include "lemipc.h" 12 | 13 | static bool is_in_perim(int pos_ennemy, int pos_ally, int perim) 14 | { 15 | int y; 16 | int x; 17 | 18 | y = pos_ennemy / WIN_SIZE_X - perim / 2; 19 | while (y != pos_ennemy / WIN_SIZE_X + perim / 2) 20 | { 21 | x = pos_ennemy % WIN_SIZE_X - perim / 2; 22 | while (x != pos_ennemy % WIN_SIZE_X + perim / 2) 23 | if (x + y * WIN_SIZE_X == pos_ally) 24 | return (TRUE); 25 | else 26 | x++; 27 | y++; 28 | } 29 | return (FALSE); 30 | } 31 | 32 | static bool is_common_ally(int pos_ennemy, t_lemipc *lemipc, int perim) 33 | { 34 | int y; 35 | int x; 36 | 37 | y = lemipc->pos / WIN_SIZE_X - perim / 2; 38 | while (y != (int)lemipc->pos / WIN_SIZE_X + perim / 2) 39 | { 40 | x = lemipc->pos % WIN_SIZE_X - perim / 2; 41 | while (x != (int)lemipc->pos % WIN_SIZE_X + perim / 2) 42 | if (x >= 0 && x < WIN_SIZE_X && y >= 0 && y < WIN_SIZE_Y 43 | && lemipc->map[x + y * WIN_SIZE_X] == lemipc->no_team 44 | && (x + y * WIN_SIZE_X) != (int)lemipc->pos 45 | && is_in_perim(pos_ennemy, x + y * WIN_SIZE_X, perim) == TRUE) 46 | return (TRUE); 47 | else 48 | ++x; 49 | ++y; 50 | } 51 | return (FALSE); 52 | } 53 | 54 | static int check_ennemy_to_attack(t_lemipc *lemipc, int perim) 55 | { 56 | int y; 57 | int x; 58 | 59 | y = lemipc->pos / WIN_SIZE_X - perim / 2; 60 | while (y != (int)lemipc->pos / WIN_SIZE_X + perim / 2) 61 | { 62 | x = lemipc->pos % WIN_SIZE_X - perim / 2; 63 | while (x != (int)lemipc->pos % WIN_SIZE_X + perim / 2) 64 | if (x >= 0 && x < WIN_SIZE_X && y >= 0 && y < WIN_SIZE_Y 65 | && lemipc->map[x + y * WIN_SIZE_X] != lemipc->no_team 66 | && lemipc->map[x + y * WIN_SIZE_X] != 0 67 | && is_common_ally(x + y * WIN_SIZE_X, lemipc, perim) == TRUE) 68 | return (x + y * WIN_SIZE_X); 69 | else 70 | ++x; 71 | ++y; 72 | } 73 | return (-1); 74 | } 75 | 76 | static int nearest_ally(t_lemipc *lemipc, int perim) 77 | { 78 | int pos; 79 | int y; 80 | int x; 81 | 82 | pos = -1; 83 | y = lemipc->pos / WIN_SIZE_X - perim / 2; 84 | while (y != (int)lemipc->pos / WIN_SIZE_X + perim / 2) 85 | { 86 | x = lemipc->pos % WIN_SIZE_X - perim / 2; 87 | while (x != (int)lemipc->pos % WIN_SIZE_X + perim / 2) 88 | { 89 | if (x >= 0 && x < WIN_SIZE_X && y >= 0 && y < WIN_SIZE_Y 90 | && lemipc->map[x + y * WIN_SIZE_X] == lemipc->no_team 91 | && (x + y * WIN_SIZE_X) != (int)lemipc->pos && 92 | (pos == -1 || !is_less_far(lemipc, pos, x + y * WIN_SIZE_X))) 93 | pos = x + y * WIN_SIZE_X; 94 | ++x; 95 | } 96 | ++y; 97 | } 98 | return (pos); 99 | } 100 | 101 | void move_algorithm(t_lemipc *lemipc) 102 | { 103 | int pos_ennemy; 104 | int pos_ally; 105 | 106 | if ((pos_ally = nearest_ally(lemipc, LARGE_PERIM_A)) != -1) 107 | if ((pos_ennemy = check_ennemy_to_attack(lemipc, LARGE_PERIM_E)) != -1) 108 | move_to(pos_ennemy, lemipc); 109 | else 110 | if (nearest_ally(lemipc, SHORT_PERIM) != -1) 111 | if ((pos_ennemy = check_ennemy_to_attack(lemipc, LARGE_PERIM_A)) != -1) 112 | move_to(pos_ennemy, lemipc); 113 | else 114 | move_random(lemipc); 115 | else 116 | move_to(pos_ally, lemipc); 117 | else 118 | move_random(lemipc); 119 | } 120 | --------------------------------------------------------------------------------