version 0.7
[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   else if (strcmp(trackname,"wave")==0)
92   {
93     a1=150;
94     a2=130;
95     o=1.5;
96     m=0.1;
97     s=car->speed/3.3+0.4;
98     og=120;
99   }
100   else if (strcmp(trackname,"bio")==0)
101   {
102     a1=190;
103     a2=180;
104     o=0.3;
105     m=0.1;
106     s=car->speed/3.3+0.4;
107     og=120;
108   }
109   else if (strcmp(trackname,"city")==0)
110   {
111     a1=150;
112     a2=130;
113     o=1.2;
114     m=0.1;
115     s=car->speed/3.3+0.4;
116     og=120;
117   }
118   else if (strcmp(trackname,"desert")==0)
119   {
120     a1=150;
121     a2=130;
122     o=1.0;
123     m=0.1;
124     s=car->speed/3.3+0.4;
125     og=120;
126   }
127   else if (strcmp(trackname,"http")==0)
128   {
129     a1=190;
130     a2=170;
131     o=1.2;
132     m=0.3;
133     s=car->speed/3.4+0.4;
134     og=240;
135   }
136   else if (strcmp(trackname,"kart")==0)
137   {
138     a1=180;
139     a2=160;
140     o=0.8;
141     m=0.8;
142     s=car->speed/3.3+0.4;
143     og=120;
144   }
145   /* some default values that may work on some tracks */
146   else
147   {
148     a1=190;
149     a2=180;
150     o=0.2;
151     m=0.1;
152     s=car->speed/3.3+0.4;
153     og=1;
154   }
155   
156   /* should we accelerate or brake ? */
157   c=getpixel(fun,car->x,car->y);
158   *ku=1,*kd=0;
159   for (l=0;l<a1;l++)
160   {
161     x1=car->x-cos(car->angle)*l*s;
162     y1=car->y-sin(car->angle)*l*s;
163     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);
164     SDL_GetRGB(c,fun->format,&t,&g,&t);
165     if (g<og && car->speed>m) { *ku=0,*kd=1; break; }
166   }
167   
168   /* should we turn ? left or right ? */
169   *kl=0;
170   *kr=0;
171   for (l=0;l<a2;l++)
172   {
173     x2=car->x-cos(car->angle)*l*s;
174     y2=car->y-sin(car->angle)*l*s;
175     x3=car->x-cos(car->angle-o)*l*s;
176     y3=car->y-sin(car->angle-o)*l*s;
177     x4=car->x-cos(car->angle+o)*l*s;
178     y4=car->y-sin(car->angle+o)*l*s;
179     
180     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);
181     SDL_GetRGB(c,fun->format,&t,&tg,&t);
182     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);
183     SDL_GetRGB(c,fun->format,&t,&g,&t);
184     if (g>tg) { *kr=1; break; } else if (g<tg) { *kl=1; break; }
185   }
186 }