version 0.5
[zeRace] / bot_anticip.c
1 /*
2  * zeRace "anticip" bot
3  * http://royale.zerezo.com/zerace/
4  * 
5  * Copyright (C) 2004  Antoine Jacquet <royale@zerezo.com>
6  * Licensed under GPL
7  *
8  * This robot uses the idea described on this page :
9  * http://rars.sourceforge.net/selection/felix.html
10  * Thanks to Doug Eleveld
11  */
12
13 #include "bot.h"
14
15 char *bot_name()
16 {
17   return "Anticip";
18 }
19
20 void bot_ia(char *trackname,struct _car *car,SDL_Surface *fun,int *ku,int *kd,int *kl,int *kr)
21 {
22   int a1,a2,l,x1,x2,x3,x4,y1,y2,y3,y4;
23   float o,m,s;
24   Uint32 c;
25   Uint8 g,og,tg,t;
26   
27   /* adjust the properties depending of the track, this was "manually" optimized ;) */
28   if (strcmp(trackname,"car")==0)
29   {
30     a1=190;
31     a2=180;
32     o=0.1;
33     m=0.1;
34     s=car->speed/3.3+0.4;
35     og=130;
36   }
37   else if (strcmp(trackname,"first")==0)
38   {
39     a1=240;
40     a2=230;
41     o=0.1;
42     m=0.1;
43     s=car->speed/3.3+0.3;
44     og=120;
45   }
46   else if (strcmp(trackname,"icy")==0)
47   {
48     a1=250;
49     a2=240;
50     o=0.3;
51     m=0.1;
52     s=car->speed/3.3+0.3;
53     og=120;
54   }
55   else if (strcmp(trackname,"hairpins")==0)
56   {
57     a1=220;
58     a2=210;
59     o=0.6;
60     m=0.1;
61     s=car->speed/3.3+0.4;
62     og=120;
63   }
64   else if (strcmp(trackname,"simple")==0)
65   {
66     a1=190;
67     a2=180;
68     o=0.2;
69     m=0.1;
70     s=car->speed/3.3+0.4;
71     og=1;
72   }
73   else if (strcmp(trackname,"loop")==0)
74   {
75     a1=150;
76     a2=90;
77     o=0.8;
78     m=0.1;
79     s=car->speed/3.3+0.4;
80     og=1;
81   }
82   else if (strcmp(trackname,"formula")==0)
83   {
84     a1=150;
85     a2=90;
86     o=0.8;
87     m=0.1;
88     s=car->speed/3.3+0.4;
89     og=120;
90   }
91   /* some default values that may work on some tracks */
92   else
93   {
94     a1=190;
95     a2=180;
96     o=0.2;
97     m=0.1;
98     s=car->speed/3.3+0.4;
99     og=1;
100   }
101   
102   /* should we accelerate or brake ? */
103   c=getpixel(fun,car->x,car->y);
104   *ku=1,*kd=0;
105   for (l=0;l<a1;l++)
106   {
107     x1=car->x-cos(car->angle)*l*s;
108     y1=car->y-sin(car->angle)*l*s;
109     if (x1>0 && y1>0 && x1<fun->w && y1<fun->h) c=getpixel(fun,x1,y1); else c=SDL_MapRGB(fun->format,0,0,0);
110     SDL_GetRGB(c,fun->format,&t,&g,&t);
111     if (g<og && car->speed>m) { *ku=0,*kd=1; break; }
112   }
113   
114   /* should we turn ? left or right ? */
115   *kl=0;
116   *kr=0;
117   for (l=0;l<a2;l++)
118   {
119     x2=car->x-cos(car->angle)*l*s;
120     y2=car->y-sin(car->angle)*l*s;
121     x3=car->x-cos(car->angle-o)*l*s;
122     y3=car->y-sin(car->angle-o)*l*s;
123     x4=car->x-cos(car->angle+o)*l*s;
124     y4=car->y-sin(car->angle+o)*l*s;
125     
126     if (x3>0 && y3>0 && x3<fun->w && y3<fun->h) c=getpixel(fun,x3,y3); else c=SDL_MapRGB(fun->format,0,0,0);
127     SDL_GetRGB(c,fun->format,&t,&tg,&t);
128     if (x4>0 && y4>0 && x4<fun->w && y4<fun->h) c=getpixel(fun,x4,y4); else c=SDL_MapRGB(fun->format,0,0,0);
129     SDL_GetRGB(c,fun->format,&t,&g,&t);
130     if (g>tg) { *kr=1; break; } else if (g<tg) { *kl=1; break; }
131   }
132 }