-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.c
217 lines (181 loc) · 5.57 KB
/
rules.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "headers/rules.h"
#include <time.h>
//find an slot of type 'type'
coord_t neighbour(object_t **eco, config_t conf, coord_t p, int type) {
srand(time(NULL));
coord_t possible[4];
int possible_n = 0;
//left
if (p.y - 1 >= 0 && eco[p.x][p.y - 1].animal.type == type)
possible[possible_n++] = (coord_t){.x = p.x, .y = p.y - 1};
//down
if (p.x + 1 < conf.L && eco[p.x + 1][p.y].animal.type == type)
possible[possible_n++] = (coord_t){.x = p.x + 1, .y = p.y};
//right
if (p.y + 1 < conf.C && eco[p.x][p.y + 1].animal.type == type)
possible[possible_n++] = (coord_t){.x = p.x, .y = p.y + 1};
//up
if (p.x - 1 >= 0 && eco[p.x - 1][p.y].animal.type == type)
possible[possible_n++] = (coord_t){.x = p.x - 1, .y = p.y};
if (possible_n > 0){
if (possible_n > 1)
return possible[(conf.GEN + p.x +p.y) % possible_n]; //choose one of the neighbours
else
return possible[0];
}
else
return (coord_t){.x = -1, .y = -1};
}
/*******************************
--------- Rabbit Rules ---------
********************************/
// Inserts a rabbit and resolve possible conflicts
void rabbit_insert (object_t **eco, animal_t r, coord_t old, coord_t new) {
int candidate_id;
if (old.y + 1 == new.y)
candidate_id = LEFT;
else if (old.x - 1 == new.x)
candidate_id = DOWN;
else if (old.y - 1 == new.y)
candidate_id = RIGHT;
else if (old.x + 1 == new.x)
candidate_id = UP;
else if (old.x == new.x && old.y == new.y)
candidate_id = CENTER;
eco[new.x][new.y].candidates[candidate_id] = r;
}
// Make a move if a available slot is found and returns the new position
int rabbit_move (object_t **eco, config_t conf, coord_t p) {
coord_t new = neighbour(eco, conf, p, EMPTY);
if (new.x >= 0 && new.y >= 0){
if (conf.GEN - eco[p.x][p.y].animal.gen_nascimento > conf.GEN_PROC_COELHOS) { // Reproduction
rabbit_insert(eco, NEWANIMAL(RABBIT, conf.GEN, -1), p, p);
rabbit_insert(eco, NEWANIMAL(RABBIT, conf.GEN, -1), p, new);
}
else { // Move without reproducing
//rabbit_insert(eco, eco[p.x][p.y].animal, p, new);
rabbit_insert(eco, NEWANIMAL(RABBIT, eco[p.x][p.y].animal.gen_nascimento, -1), p, new);
}
return 1;
} //No neighbours, returns false
return 0;
}
void rabbit_rules (object_t **eco, config_t conf, coord_t p) {
// Movement
rabbit_move (eco, conf, p);
return;
}
/*******************************
---------- Fox Rules -----------
********************************/
/*Insert the fox f into the next_eco in the position p, resolving conflicts if they ocour*/
void fox_insert (object_t **eco, animal_t f, coord_t old, coord_t new) {
int candidate_id;
if (old.y + 1 == new.y)
candidate_id = UP;
else if (old.x - 1 == new.x)
candidate_id = RIGHT;
else if (old.y - 1 == new.y)
candidate_id = DOWN;
else if (old.x + 1 == new.x)
candidate_id = LEFT;
else if (old.x == new.x && old.y == new.y)
candidate_id = CENTER;
eco[new.x][new.y].candidates[candidate_id] = f;
}
int predation(object_t **eco, config_t conf, coord_t p) {
coord_t new = neighbour(eco, conf, p, RABBIT);
if (new.x >= 0 && new.y >= 0){
if (conf.GEN - eco[p.x][p.y].animal.gen_nascimento > conf.GEN_PROC_RAPOSAS) { /* Reproduction */
fox_insert(eco, NEWANIMAL(FOX, conf.GEN, conf.GEN), p, p);
fox_insert(eco, NEWANIMAL(FOX, conf.GEN, conf.GEN), p, new);
}
else { /* Move without reproducing */
fox_insert(eco, NEWANIMAL(FOX, eco[p.x][p.y].animal.gen_nascimento, conf.GEN), p, new);
}
return 1;
}
return 0;
}
/*TODO: fox predation, fox move, fox death*/
int fox_move (object_t **eco, config_t conf, coord_t p) {
coord_t new = neighbour(eco, conf, p, EMPTY);
if (new.x >= 0 && new.y >= 0){
if (conf.GEN - eco[p.x][p.y].animal.gen_nascimento > conf.GEN_PROC_RAPOSAS) { /* Reproduction */
fox_insert(eco, NEWANIMAL(FOX, conf.GEN, conf.GEN), p, p);
fox_insert(eco, NEWANIMAL(FOX, conf.GEN, conf.GEN), p, new);
} else { /* Move without reproducing */
fox_insert(eco, eco[p.x][p.y].animal, p, new);
}
return 1;
}
return 0;
}
void fox_rules (object_t **eco, config_t conf, coord_t p) {
/* Movement */
if (!predation(eco, conf, p)) {
if(conf.GEN - eco[p.x][p.y].animal.gen_comida >= conf.GEN_COMIDA_RAPOSAS){ //death
//clear_position(&(next_eco[p.x][p.y]));
}
else
fox_move(eco, conf, p);
}
}
animal_t choose_rabbit (object_t object) {
animal_t old_animal = object.animal;
animal_t *animals = object.candidates;
animal_t animal;
int success = 0;
for (int i = 0; i < 5; i++){
if (animals[i].type != RABBIT)
continue;
success += 1;
if (success == 1)
animal = animals[i];
if (animal.gen_nascimento < animals[i].gen_nascimento)
continue;
else
animal = animals[i];
animals[i] = NEWANIMAL(EMPTY, -1, -1);
}
if (success)
return animal;
else if (object.animal.type != RABBIT)
return old_animal;
else
return NEWANIMAL(EMPTY, -1, -1);
}
animal_t choose_fox (object_t object) {
animal_t old_animal = object.animal;
animal_t *animals = object.candidates;
animal_t animal;
int success = 0;
for (int i = 0; i < 5; i++){
if (animals[i].type != FOX)
continue;
success += 1;
if (success == 1)
animal = animals[i];
if (animal.gen_nascimento < animals[i].gen_nascimento){
continue;
}
else if (animal.gen_nascimento > animals[i].gen_nascimento){
animal = animals[i];
}
else {
if (animal.gen_comida > animals[i].gen_comida){
continue;
}
else {
animal = animals[i];
}
}
animals[i] = NEWANIMAL(EMPTY, -1, -1);
}
if (success)
return animal;
else if (object.animal.type != FOX)
return old_animal;
else
return NEWANIMAL(EMPTY, -1, -1);
}