15 #include "interface.h"
18 #include "publicServer.h"
21 static export_fce_t
*export_fce
;
23 typedef struct alternative_struct
{
30 static alternative_t
*newAlternative(int route
, int x
, int y
)
34 new = malloc(sizeof(alternative_t
));
44 static alternative_t
*cloneAlternative(alternative_t
*p
, int route
, int x
, int y
)
53 new = newAlternative(route
, p
->x
, p
->y
);
54 new->first
= p
->first
;
60 static void forkAlternative(list_t
*list
, alternative_t
*p
, int w
, int h
)
72 list_add(list
, cloneAlternative(p
, TUX_RIGHT
, x
+ (w
+ 5), y
));
73 list_add(list
, cloneAlternative(p
, TUX_LEFT
, x
- (w
+ 5), y
));
76 list_add(list
, cloneAlternative(p
, TUX_UP
, x
, y
- (h
+ 5)));
77 list_add(list
, cloneAlternative(p
, TUX_DOWN
, x
, y
+ (h
+ 5)));
80 list_add(list
, cloneAlternative(p
, TUX_UP
, x
, y
- (h
+ 5)));
81 list_add(list
, cloneAlternative(p
, TUX_DOWN
, x
, y
+ (h
+ 5)));
84 list_add(list
, cloneAlternative(p
, TUX_RIGHT
, x
+ (w
+ 5), y
));
85 list_add(list
, cloneAlternative(p
, TUX_LEFT
, x
- (w
+ 5), y
));
91 static void moveAlternative(alternative_t
*p
, int offset
)
97 /*printf("move %d %d %d\n", p->x, p->y, p->step);*/
115 static void destroyAlternative(alternative_t
*p
)
121 static void cmd_ai(char *line
)
126 static int init(export_fce_t
*p
)
133 #ifndef PUBLIC_SERVER
134 static int draw(int x
, int y
, int w
, int h
)
145 static tux_t
*findOtherTux(space_t
*space
)
149 for (i
= 0; i
< space_get_count(space
); i
++) {
152 thisTux
= space_get_item(space
, i
);
154 if (thisTux
->control
!= TUX_CONTROL_AI
) {
162 static void shotTux(arena_t
*arena
, tux_t
*tux_ai
, tux_t
*tux_rival
)
164 const int limit
= 20;
166 int x_rival
, y_rival
;
171 export_fce
->fce_tux_get_proportion(tux_ai
, &x_ai
, &y_ai
, &w
, &h
);
172 export_fce
->fce_tux_get_proportion(tux_rival
, &x_rival
, &y_rival
, &w
, &h
);
174 if (y_rival
< y_ai
&& x_rival
> x_ai
&& x_rival
< x_ai
+ limit
) {
175 export_fce
->fce_tux_action(tux_ai
, TUX_UP
);
176 export_fce
->fce_tux_action(tux_ai
, TUX_SHOT
);
179 if (x_rival
> x_ai
&& y_rival
> y_ai
&& y_rival
< y_ai
+ limit
) {
180 export_fce
->fce_tux_action(tux_ai
, TUX_RIGHT
);
181 export_fce
->fce_tux_action(tux_ai
, TUX_SHOT
);
184 if (x_rival
< x_ai
&& y_rival
> y_ai
&& y_rival
< y_ai
+ limit
) {
185 export_fce
->fce_tux_action(tux_ai
, TUX_LEFT
);
186 export_fce
->fce_tux_action(tux_ai
, TUX_SHOT
);
189 if (y_rival
> y_ai
&& x_rival
> x_ai
&& x_rival
< x_ai
+ limit
) {
190 export_fce
->fce_tux_action(tux_ai
, TUX_DOWN
);
191 export_fce
->fce_tux_action(tux_ai
, TUX_SHOT
);
195 static void tux_eventAI(tux_t
*tux
)
200 list_t
*listAlternative
;
205 int rival_x
, rival_y
;
213 export_fce
->fce_tux_get_proportion(tux
, &x
, &y
, &w
, &h
);
214 /*printf("tux AI %d %d\n", x, y);*/
216 arena
= export_fce
->fce_arena_get_current();
218 rivalTux
= findOtherTux(arena
->spaceTux
);
220 if (rivalTux
== NULL
|| rivalTux
->status
!= TUX_STATUS_ALIVE
) {
224 listAlternative
= list_new();
225 listDst
= list_new();
226 listFork
= list_new();
228 shotTux(arena
, tux
, rivalTux
);
230 export_fce
->fce_tux_get_proportion(rivalTux
, &rival_x
, &rival_y
, NULL
, NULL
);
231 /*printf("tux rival %d %d\n", rival_x, rival_y);*/
233 list_add(listAlternative
, newAlternative(TUX_UP
, x
, y
- (h
+ 10)));
234 list_add(listAlternative
, newAlternative(TUX_RIGHT
, x
+ (w
+ 10), y
));
235 list_add(listAlternative
, newAlternative(TUX_LEFT
, x
- (w
+ 10), y
));
236 list_add(listAlternative
, newAlternative(TUX_DOWN
, x
, y
+ (h
+ 10)));
243 if (my_index
< 0 || my_index
>= listAlternative
->count
) {
247 /*printf("listFork->count = %d\n", listFork->count);*/
249 for (j
= 0; j
< listFork
->count
; j
++) {
250 list_add(listAlternative
, listFork
->list
[j
]);
253 list_do_empty(listFork
);
258 if (listAlternative
->count
== 0) {
262 this = (alternative_t
*) listAlternative
->list
[my_index
];
264 if (++countDo
== 100)
267 if (this->step
> 100) {
268 list_del_item(listAlternative
, my_index
, destroyAlternative
);
274 moveAlternative(this, w
* 2);
276 if (export_fce
->fce_arena_is_free_space(arena
, this->x
, this->y
, w
, h
) == 1) {
277 forkAlternative(listFork
, this, 2 * w
, 2 * h
);
283 fce_arena_conflict_space(this->x
, this->y
, w
, h
, rival_x
, rival_y
, w
,
285 /*printf("this->step = %d\n", this->step);*/
287 list_del(listAlternative
, my_index
);
288 list_add(listDst
, this);
294 if (export_fce
->fce_arena_is_free_space(arena
, this->x
, this->y
, w
, h
) == 0) {
295 /*forkAlternative(listFork, this, w*2, h*2);*/
296 list_del_item(listAlternative
, my_index
, destroyAlternative
);
308 /*printf("------------\n");*/
309 for (i
= 0; i
< listDst
->count
; i
++) {
312 this = (alternative_t
*) listDst
->list
[i
];
314 /*printf("XXX step %d route = %d\n", this->step, this->first);*/
315 if (this->step
< minStep
) {
316 minStep
= this->step
;
317 recRoute
= this->first
;
322 export_fce
->fce_tux_action(tux
, recRoute
);
325 list_destroy_item(listFork
, destroyAlternative
);
326 list_destroy_item(listAlternative
, destroyAlternative
);
327 list_destroy_item(listDst
, destroyAlternative
);
329 printf("countFork = %d\n", countFork);
330 printf("countDel = %d\n", countDel);
331 printf("countLimit = %d\n", countLimit);
332 printf("countDo = %d\n", countDo);
336 static void action_tuxAI(space_t
*space
, tux_t
*tux
, void *p
)
341 if (tux
->control
== TUX_CONTROL_AI
&& tux
->status
== TUX_STATUS_ALIVE
) {
348 static my_time_t lastEvent
= 0;
349 my_time_t curentTime
;
354 if (lastEvent
== 0) {
355 lastEvent
= export_fce
->fce_timer_get_current_time();
358 curentTime
= export_fce
->fce_timer_get_current_time();
360 if (curentTime
- lastEvent
< 25) {
364 lastEvent
= export_fce
->fce_timer_get_current_time();
365 /*printf("event AI\n");*/
367 arena
= export_fce
->fce_arena_get_current();
375 space_action(arena
->spaceTux
, action_tuxAI
, NULL
);
377 for( i = 0 ; i < arena->spaceTux->list->count ; i++ )
381 thisTux = arena->spaceTux->list->list[i];
383 if( thisTux->control == TUX_CONTROL_AI &&
384 thisTux->status == TUX_STATUS_ALIVE )
394 static int isConflict(int x
, int y
, int w
, int h
)
404 static void cmdArena(char *line
)
406 if (strncmp(line
, "ai", 2) == 0)
410 static void recvMsg(char *msg
)
420 mod_sym_t modai_sym
= { &init
,
421 #ifndef PUBLIC_SERVER