-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove.c
119 lines (109 loc) · 2.34 KB
/
move.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
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
** move.c for in /home/ernst_m/Documents/Tek2/lemipc/PSU_2016_lemipc
**
** Made by ernst_m
** Login <[email protected]>
**
** Started on Tue Mar 21 16:14:01 2017 ernst_m
** Last update Tue Mar 28 16:47:24 2017 Valentin Nasraty
*/
#include "lemipc.h"
t_pos get_newpos_random(t_pos pos)
{
int a;
t_pos oldpos;
oldpos.x = pos.x;
oldpos.y = pos.y;
a = rand() % 2;
if (a == 1)
{
a = rand() % 2;
if (a == 1)
pos.x = pos.x + 1;
a = rand() % 2;
if (a == 1)
pos.y = pos.y + 1;
}
else
{
a = rand() % 2;
if (a == 1)
pos.x = pos.x - 1;
a = rand() % 2;
if (a == 1)
pos.y = pos.y - 1;
}
if ((pos.y < 0 || pos.x < 0 || pos.x >= WIDTH || pos.y >= HEIGTH))
return oldpos;
return pos;
}
int there_is_enemy(t_util *util)
{
int i;
int n;
i = -1;
while (++i < HEIGTH)
{
n = -1;
while (++n < WIDTH)
{
if (util->tab[i][n].nteam != util->my_team &&
util->tab[i][n].nteam != 0)
return (1);
}
}
return (0);
}
t_pos move_pos(t_util *util, t_pos pos, int teamnb)
{
t_pos newpos;
if (there_is_enemy(util) == 0)
newpos = get_newpos_random(pos);
else
newpos = get_newpos_ia(util, pos);
if ((lock_sem(newpos.x, newpos.y, util)) == 0)
{
util->tab[pos.y][pos.x].nteam = 0;
util->tab[newpos.y][newpos.x].nteam = teamnb;
unlock_sem(pos.x, pos.y, util);
pos.x = newpos.x;
pos.y = newpos.y;
}
return pos;
}
void go_move(t_util *util, int x, int y, int teamnb)
{
t_pos pos;
pos.x = x;
pos.y = y;
util->msg_id = msgget(util->key, SHM_R | SHM_W);
while (1)
{
if ((msgrcv(util->msg_id, &util->msg,
sizeof(util->msg), 1, IPC_NOWAIT)) != -1)
{
clean_all(util, pos);
printf("end received\n");
exit(0);
}
if (is_dead(util, pos) == 1)
{
printf("DEAAAAAAAAD\n");
util->tab[pos.y][pos.x].nteam = 0;
unlock_sem(pos.x, pos.y, util);
if (util->commando == true)
{
util->order.mtype = (util->my_team * 10000);
msgsnd(util->msg_id, &util->order, sizeof(util->order), 0);
}
check_last_dead(util);
exit(0);
}
if ((msgrcv(util->msg_id, &util->order,
sizeof(util->order), (util->my_team * 10000), IPC_NOWAIT)) != -1)
util->commando = true;
pos = move_pos(util, pos, teamnb);
add_player();
usleep(400000);
}
}