version 0.4
[zeRace] / bot_anticip.c
diff --git a/bot_anticip.c b/bot_anticip.c
new file mode 100644 (file)
index 0000000..22de8dd
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+ * zeRace "anticip" bot
+ * http://royale.zerezo.com/zerace/
+ * 
+ * Copyright (C) 2004  Antoine Jacquet <royale@zerezo.com>
+ * Licensed under GPL
+ *
+ * This robot uses the idea described on this page :
+ * http://rars.sourceforge.net/selection/felix.html
+ * Thanks to Doug Eleveld
+ */
+
+#include "bot.h"
+
+char *bot_name()
+{
+  return "Anticip";
+}
+
+void bot_ia(char *trackname,struct _car *car,SDL_Surface *fun,int *ku,int *kd,int *kl,int *kr)
+{
+  int a1,a2,c,g,tg,og,l,x1,x2,x3,x4,y1,y2,y3,y4;
+  float o,m,s;
+  
+  /* adjust the properties depending of the track, this was "manually" optimized ;) */
+  if (strcmp(trackname,"car")==0)
+  {
+    a1=190;
+    a2=180;
+    o=0.1;
+    m=0.1;
+    s=car->speed/3.3+0.4;
+    og=130;
+  }
+  else if (strcmp(trackname,"first")==0)
+  {
+    a1=240;
+    a2=230;
+    o=0.1;
+    m=0.1;
+    s=car->speed/3.3+0.3;
+    og=120;
+  }
+  else if (strcmp(trackname,"icy")==0)
+  {
+    a1=250;
+    a2=240;
+    o=0.3;
+    m=0.1;
+    s=car->speed/3.3+0.3;
+    og=120;
+  }
+  else if (strcmp(trackname,"hairpins")==0)
+  {
+    a1=220;
+    a2=210;
+    o=0.6;
+    m=0.1;
+    s=car->speed/3.3+0.4;
+    og=120;
+  }
+  else if (strcmp(trackname,"simple")==0)
+  {
+    a1=190;
+    a2=180;
+    o=0.2;
+    m=0.1;
+    s=car->speed/3.3+0.4;
+    og=1;
+  }
+  else if (strcmp(trackname,"loop")==0)
+  {
+    a1=150;
+    a2=90;
+    o=0.8;
+    m=0.1;
+    s=car->speed/3.3+0.4;
+    og=1;
+  }
+  /* some default values that may work on some tracks */
+  else
+  {
+    a1=190;
+    a2=180;
+    o=0.2;
+    m=0.1;
+    s=car->speed/3.3+0.4;
+    og=1;
+  }
+  
+  /* should we accelerate or brake ? */
+  c=getpixel(fun,car->x,car->y);
+  *ku=1,*kd=0;
+  for (l=0;l<a1;l++)
+  {
+    x1=car->x-cos(car->angle)*l*s;
+    y1=car->y-sin(car->angle)*l*s;
+    if (x1>0 && y1>0 && x1<fun->w && y1<fun->h) c=getpixel(fun,x1,y1); else c=0;
+    g=(c>>GSHIFT)&0xff;
+    if (g<og && car->speed>m) { *ku=0,*kd=1; break; }
+  }
+  
+  /* should we turn ? left or right ? */
+  *kl=0;
+  *kr=0;
+  for (l=0;l<a2;l++)
+  {
+    x2=car->x-cos(car->angle)*l*s;
+    y2=car->y-sin(car->angle)*l*s;
+    x3=car->x-cos(car->angle-o)*l*s;
+    y3=car->y-sin(car->angle-o)*l*s;
+    x4=car->x-cos(car->angle+o)*l*s;
+    y4=car->y-sin(car->angle+o)*l*s;
+    
+    if (x3>0 && y3>0 && x3<fun->w && y3<fun->h) c=getpixel(fun,x3,y3); else c=0;
+    tg=(c>>GSHIFT)&0xff;
+    if (x4>0 && y4>0 && x4<fun->w && y4<fun->h) c=getpixel(fun,x4,y4); else c=0;
+    g=(c>>GSHIFT)&0xff;
+    if (g>tg) { *kr=1; break; } else if (g<tg) { *kl=1; break; }
+  }
+}