-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
106 lines (98 loc) · 2.36 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
** main.c for in /home/ernst_m/Documents/Tek2/lemipc
**
** Made by ernst_m
** Login <ernst_m@epitech.net>
**
** Started on Fri Mar 17 14:24:34 2017 ernst_m
** Last update Fri Mar 24 16:59:01 2017 Valentin Nasraty
*/
#include "lemipc.h"
void init_ncurses(SCREEN *win)
{
win = newterm(NULL, stderr, stdin);
set_term(win);
refresh();
keypad(stdscr, 1);
curs_set(0);
start_color();
noecho();
set_bordure(6);
init_mouse();
}
void spawn(t_util *util, int teamnb)
{
int x;
int y;
x = rand() % WIDTH;
y = rand() % HEIGTH;
if ((lock_sem(x, y, util)) == 0)
{
util->tab[y][x].nteam = teamnb;
go_move(util, x, y, teamnb);
}
else
spawn(util, teamnb);
}
void core(int teamnb, char *path)
{
SCREEN *win;
t_util *util;
util = malloc(sizeof(t_util));
util->key = ftok(path, 0);
util->shm_id = shmget(util->key, WIDTH * HEIGTH * sizeof(t_case), SHM_R | SHM_W);
if ((strcmp(path, "exit")) == 0)
{
util->key = ftok("./", 0);
util->shm_id = shmget(util->key, WIDTH * HEIGTH * sizeof(t_case), SHM_R | SHM_W);
util->msg_id = msgget(util->key, SHM_R | SHM_W);
msgctl(util->msg_id, IPC_RMID, NULL);
delete_sems(util);
shmctl(util->shm_id, IPC_RMID, NULL);
return ;
}
if (util->shm_id == -1)
{
win = NULL;
util->shm_id = shmget(util->key, WIDTH * HEIGTH * sizeof(t_case),
IPC_CREAT | SHM_R | SHM_W);
util->msg_id = msgget(util->key, IPC_CREAT | SHM_R | SHM_W);
init_ncurses(win);
fill_struct(util);
fill_map(util);
create_sems(util);
print_map(util);
}
else
{
util->my_team = teamnb;
fill_struct(util);
spawn(util, teamnb);
}
}
int check_params(int ac, char **av)
{
if (ac < 3)
{
printf("Usage : ./lemipc path_to_key team_number\n\npath_to_key is a valid path that will be used by ftok\nteam_number is the team number assigned to the current player");
return (1);
}
else
{
if ((atoi(av[2])) <= 0)
{
printf("Usage : ./lemipc path_to_key team_number\n\npath_to_key is a valid path that will be used by ftok\nteam_number is the team number assigned to the current player");
return (1);
}
}
return (0);
}
int main(int ac, char **av)
{
if (check_params(ac, av) == 1)
return (1);
srand(time(NULL));
core(atoi(av[2]), av[1]);
endwin();
return (0);
}