-
Notifications
You must be signed in to change notification settings - Fork 1
/
patch.c
288 lines (250 loc) · 6.77 KB
/
patch.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "header.h"
#include "patch_agent_header.h"
#include "commons.h"
#include <math.h>
/* patch calories and patch information and calorie proportion of ecah clan workforce over the total clans in the patch*/
int patchtype()
{
int pt=get_tpatch(),pID=get_patchID();
int px=get_x(),py=get_y();
if(px==0){
set_tpatch(HM);
set_repows(HMRWS);
set_repods(HMRDS);
}
else if(px==1){
set_tpatch(LM);
set_repows(LMRWS);
set_repods(LMRDS);
}
else if ((py==0)||(py==GRIDSIZE-1)||(px==GRIDSIZE-1)){
set_tpatch(C);
set_repows(CRWS);
set_repods(CRDS);
}
else if ((px<=GRIDSIZE-1)/2){
set_tpatch(DP);
set_repows(DPRWS);
set_repods(DPRDS);
}
else{
set_tpatch(WP);
set_repows(WPRWS);
set_repods(WPRDS);
}
set_repo( get_repows() );
return 0;
}
int infoClansInPatch()
{
int pt=get_tpatch(),pID=get_patchID();
int px=get_x(),py=get_y();
if(px==0){
set_tpatch(HM);
set_repows(HMRWS);
set_repods(HMRDS);
}
else if(px==1){
set_tpatch(LM);
set_repows(LMRWS);
set_repods(LMRDS);
}
else if ((py==0)||(py==GRIDSIZE-1)||(px==GRIDSIZE-1)){
set_tpatch(C);
set_repows(CRWS);
set_repods(CRDS);
}
else if ((px<=GRIDSIZE-1)/2){
set_tpatch(DP);
set_repows(DPRWS);
set_repods(DPRDS);
}
else{
set_tpatch(WP);
set_repows(WPRWS);
set_repods(WPRDS);
}
set_repo( get_repows() );
add_clanspatch_message(get_x(), get_y(), get_pclans());
return 0;
}
int patchcalories()
{
int pID = get_patchID(), availc=get_pcalories(), rem_cal=get_pcalories(), cal;
int twf = 0;
int i, nc = 0;
float prop;
START_CLAN_INFO_MESSAGE_LOOP
clans_in_patch[nc].cID = clan_info_message->cID;
clans_in_patch[nc].wf = clan_info_message->wf;
clans_in_patch[nc].maxcc = clan_info_message->maxcc;
nc++;
twf += clans_in_patch[nc].wf;
FINISH_CLAN_INFO_MESSAGE_LOOP
for( i = 0; i < nc; i++ ){
prop = (1.0*clans_in_patch[i].wf)/twf;
cal = prop*availc;
if ( cal > clans_in_patch[i].maxcc ) cal = clans_in_patch[i].maxcc;
rem_cal -= cal;
add_clangetcalories_message( clans_in_patch[i].cID, cal );
}
set_pcalories(rem_cal);
return 0;
}
int regenerate()
{
int avail = get_pcalories();
if ( avail <= 0 ) avail = 1000000;
else avail += avail*get_repo()+10000;
set_pcalories( avail );
return 0;
}
int guanacos_move()
{
int adultos = get_adultos();
START_ADULTOSPATCH_MESSAGE_LOOP
if(adultospatch_message->sentido == 1){
adultos+=adultospatch_message->adultos;
}
if(adultospatch_message->sentido == 0){
adultos-=adultospatch_message->adultos;
}
FINISH_ADULTOSPATCH_MESSAGE_LOOP
set_adultos(adultos);
set_gcalories(adultos*CAL_ADULTO);
if(get_adultos()<0){
set_adultos(0);
}
return 0;
}
int snregenerate()
{
int pt=get_tpatch();
int season = get_season();
if (season == 0){
set_season( 1 );
set_repo( get_repods());
} else {
set_season( 0 );
set_repo( get_repows());
}
return 0;
}
int reproduccion_guanacos()
{
int r, i, aux;
int familias=0, crias=0, hembras=0, machos=0;
if( get_adultos() > 0){
START_REPRODUCCIONGUANACOS_MESSAGE_LOOP
familias += reproduccionguanacos_message->familia; //cuento las familias
if(reproduccionguanacos_message->familia){
machos += reproduccionguanacos_message->familia; //cuento el macho de cada familia
crias += reproduccionguanacos_message->count;
hembras += reproduccionguanacos_message->adultos-1;
}
else machos += reproduccionguanacos_message->count; //si no es familia todos son machos (incluidas las crias)
FINISH_REPRODUCCIONGUANACOS_MESSAGE_LOOP
crias -= (machos+hembras); //sabemos el numero de crias que contiene el patch
r = rand();
if(r <= 0) r = 2;
if(crias >0){
r = r % crias;
r = crias - r;
hembras += r; //hembras que eran crias ahora son adultas
crias -= r;
machos += crias; //nuevos machos
}
aux = 1+((hembras-1)/MAX_FAMILIA); //numero de familias nuevas que se crean
/*for(i = 0; i < aux; i++){
if(hembras > MAX_FAMILIA-1){
add_manada_guanacos_agent(0, 1, get_x(), get_y(), (MAX_FAMILIA*2)-1, 0, MAX_FAMILIA); //creamos familias
machos--;
hembras -= MAX_FAMILIA-1;
}
else{
add_manada_guanacos_agent(0, 1, get_x(), get_y(), (2*hembras)+1, 0, hembras+1);
hembras = 0;
machos--;
}
}*/
while (hembras > MAX_FAMILIA-1){
//add_manada_guanacos_agent(1, get_x(), get_y(), get_x(), get_y(),(MAX_FAMILIA*2)-1, 0, MAX_FAMILIA, get_season()); //creamos familias
machos--;
hembras -= MAX_FAMILIA-1;
}
if (hembras >= 1){
//add_manada_guanacos_agent(1, get_x(), get_y(), get_x(), get_y(), (hembras*2)+1, 0, hembras+1, get_season());
machos--;
hembras = 0;
}
while(machos >= MAX_MANADA){
//add_manada_guanacos_agent(0, get_x(), get_y(), get_x(), get_y(), MAX_MANADA, 0, MAX_MANADA, get_season()); //creamos manadas
machos-= MAX_MANADA;
}
if(machos >= 1){
//add_manada_guanacos_agent(0, get_x(), get_y(), get_x(), get_y(), machos, 0, machos, get_season()); //creamos la manada con los ultimos machos
machos= 0;
}
}
return 0;
}
int idle_patch()
{
return 0;
}
int infoGuanacosPatch(){
add_guanacospatch_message(get_x(), get_y(), get_adultos(), get_season());
return 0;
}
int clans_move(){
int clanes = get_pclans();
START_CLANMOVE_MESSAGE_LOOP
if(clanmove_message->sentido == 1){
clanes++;
}
if(clanmove_message->sentido == 0){
clanes--;
}
FINISH_CLANMOVE_MESSAGE_LOOP
if(clanes < 0) clanes = 0;
set_pclans(clanes);
return 0;
}
int info_hunters()
{
int message =0;
START_NUMGUANACOS_MESSAGE_LOOP
message ++;
FINISH_NUMGUANACOS_MESSAGE_LOOP
if (message >0)
// envio de cuantos guanacos hay en el patch
add_infoGuanacos_message (get_x(),get_y(),get_adultos());
return 0;
}
int give_calHunters()
{
int message = 0, cal = 0, hunters=0, hero=-1,cal_per=0;
int_array clan_list;
init_int_array (&clan_list);
// Recivir partida de cazadores
START_INFOHUNTER_MESSAGE_LOOP
hunters += infoHunter_message->hunters;
add_int(&clan_list,infoHunter_message->clans[0]);
add_int(&clan_list,infoHunter_message->clans[1]);
message ++;
FINISH_INFOHUNTER_MESSAGE_LOOP
if (message > 0)
{
// Calcular las calorias obtenidas segun la cantidad de guanacos cazados
cal =60000 *(rand()%get_adultos());
// Eliminar a los guanacos cazados
// Determinar quien acaba con mas guanacos de forma aleatoria
hero = rand()%clan_list.size;
// Repartir calorias obtenidas por personas. Despues cada partida
// calcula segun su num de miembros, las cal que les coresponden
cal_per = cal / hunters;
add_repCal_message(get_x(),get_y(),cal_per);
}
free_int_array (&clan_list);
return 0;
}