+ /* should we check for collisions ? */
+ if (col)
+ {
+ /* check for collisions */
+ for (i=0;i<MAX_CLIENTS;i++) clients[i].dx=clients[i].dy=0;
+ for (i=0;i<MAX_CLIENTS;i++) if (clients[i].connected)
+ for (j=i+1;j<MAX_CLIENTS;j++) if (clients[j].connected)
+ if (i!=j)
+ if (abs(clients[i].car.x-clients[j].car.x)<30 && abs(clients[i].car.y-clients[j].car.y)<30)
+ {
+ int x1,y1,x2,y2;
+ for (x1=0;x1<30;x1++)
+ for (y1=0;y1<30;y1++)
+ if (getpixel(cars[(unsigned char)(256*clients[i].car.angle/2.0/M_PI)%256],x1,y1)!=0)
+ {
+ x2=x1+clients[i].car.x-clients[j].car.x;
+ y2=y1+clients[i].car.y-clients[j].car.y;
+ if (x2>0 && x2<30 && y2>0 && y2<30)
+ {
+ if (getpixel(cars[(unsigned char)(256*clients[j].car.angle/2.0/M_PI)%256],x2,y2)!=0)
+ {
+ if (x1<30/2) { clients[i].dx++; clients[j].dx--; } else { clients[i].dx--; clients[j].dx++; }
+ if (y1<30/2) { clients[i].dy++; clients[j].dy--; } else { clients[i].dy--; clients[j].dy++; }
+ }
+ }
+ }
+ }
+
+ /* now compute the collisions */
+ for (i=0;i<MAX_CLIENTS;i++) if (clients[i].connected) if (clients[i].dx || clients[i].dy)
+ {
+ Uint32 c;
+ Uint8 g,t;
+ int dx=clients[i].dx;
+ int dy=clients[i].dy;
+ /* do not jump to much */
+ while (abs(dx)>5) dx/=2;
+ while (abs(dy)>5) dy/=2;
+ /* get the pixel color under the center of car in the function map */
+ c=getpixel(fun,clients[i].car.x+dx,clients[i].car.y+dy);
+ /* green layer (road quality) */
+ SDL_GetRGB(c,fun->format,&t,&g,&t);
+ /* if the destination is not a wall and not outside of the track */
+ if (g!=0 && clients[i].car.x>cars[0]->w && clients[i].car.x<fun->w-cars[0]->w && clients[i].car.y>cars[0]->h && clients[i].car.y<fun->h-cars[0]->h)
+ {
+ int round;
+ clients[i].car.x+=dx;
+ clients[i].car.y+=dy;
+ tmp=packet->data;
+ strcpy(tmp,"collision");
+ tmp+=strlen(tmp)+1;
+ SDLNet_Write32(time,tmp);
+ tmp+=4;
+ round=(clients[i].car.x+100)*65536;
+ clients[i].car.x=(float)round/65536-100;
+ SDLNet_Write32(round,tmp);
+ tmp+=4;
+ round=(clients[i].car.y+100)*65536;
+ clients[i].car.y=(float)round/65536-100;
+ SDLNet_Write32(round,tmp);
+ tmp+=4;
+ round=(clients[i].car.speed+100)*65536;
+ clients[i].car.speed=(float)round/65536-100;
+ SDLNet_Write32(round,tmp);
+ tmp+=4;
+ round=(clients[i].car.angle+100)*65536;
+ clients[i].car.angle=(float)round/65536-100;
+ SDLNet_Write32(round,tmp);
+ tmp+=4;
+ packet->len=(void *)tmp-(void *)packet->data;
+ packet->address=clients[i].address;
+ SDLNet_UDP_Send(udpsock,-1,packet);
+ clients[i].lasttime=time;
+ }
+ }
+ }
+