X-Git-Url: http://royale.zerezo.com/git/?p=zeRace;a=blobdiff_plain;f=zeRace.c;fp=zeRace.c;h=6f787e059854f8975a5204079e4157ac3f0ff85d;hp=c88d6cf944ab78ef56efe088057aca7041f328a1;hb=ee6ec6d95d78f74973a2ba97077cc94709bb6c61;hpb=dde2df6289f6daf23ee1632560c4c89061ef2f4a diff --git a/zeRace.c b/zeRace.c index c88d6cf..6f787e0 100644 --- a/zeRace.c +++ b/zeRace.c @@ -1,5 +1,5 @@ /* - * zeRace 0.3, a funny retro racing game + * zeRace 0.4, a funny retro racing game * http://royale.zerezo.com/zerace/ * * Copyright (C) 2004 Antoine Jacquet @@ -33,6 +33,7 @@ #include "car.h" #include "tracklist.h" #include "network.h" +#include /* configuration constants */ #define COEFF 1 @@ -57,7 +58,8 @@ struct _config SDLKey left; SDLKey right; int color; -} config = {"anonymous","",0,0,1,SDLK_UP,SDLK_DOWN,SDLK_LEFT,SDLK_RIGHT,6}; + SDLKey boss; +} config = {"anonymous","",0,0,1,SDLK_UP,SDLK_DOWN,SDLK_LEFT,SDLK_RIGHT,6,SDLK_b}; /* full script for a lap */ struct _record @@ -133,14 +135,14 @@ void zeRace_check_version() printf("checking version... "); fflush(stdout); - if(SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1) + if (SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1) { fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError()); return; } tcpsock=SDLNet_TCP_Open(&ip); - if(!tcpsock) + if (!tcpsock) { fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError()); return; @@ -148,7 +150,7 @@ void zeRace_check_version() len=strlen(request); result=SDLNet_TCP_Send(tcpsock,request,len); - if(resultnext!=loopcheck) + { + zeRace_download_file(tracklist->full); + zeRace_download_file(tracklist->function); + tracklist=tracklist->next; + } + srand(time(NULL)); if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)<0) @@ -293,7 +365,7 @@ void zeRace_init() } atexit(SDL_Quit); - if(SDLNet_Init()==-1) + if (SDLNet_Init()==-1) { fprintf(stderr,"could not initialize SDLNet : %s\n",SDLNet_GetError()); zeRace_exit(); @@ -333,7 +405,7 @@ void zeRace_init() /* send the best time for this race to the web server */ -void zeRace_send_time(struct _record record/*float x,float y,float speed,float angle,int btime,char *bkeys*/) +void zeRace_send_time(struct _record record) { IPaddress ip; TCPsocket tcpsock; @@ -356,17 +428,20 @@ void zeRace_send_time(struct _record record/*float x,float y,float speed,float a char *msg9="&bkeys="; int len,result; + /* if the best time is small enought to save all keys, send it */ + if (record.time>=MAXRECORDKEYS) return; + printf("sending time... "); fflush(stdout); - if(SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1) + if (SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1) { fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError()); return; } tcpsock=SDLNet_TCP_Open(&ip); - if(!tcpsock) + if (!tcpsock) { fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError()); return; @@ -377,7 +452,7 @@ void zeRace_send_time(struct _record record/*float x,float y,float speed,float a len=strlen(temp); result=SDLNet_TCP_Send(tcpsock,temp,len); - if(resultfull); fun=IMG_Load(tracklist->function); @@ -483,13 +573,6 @@ void zeRace_launch(int alltime,int go) switch (event.key.keysym.sym) { case SDLK_ESCAPE: - /* free memory */ - Mix_FreeMusic(light); - Mix_FreeMusic(engine); - Mix_FreeMusic(crash); - Mix_FreeMusic(slide); - /* if the best time is small enought to save all keys, send it */ - if (best.timedata,"positions")==0) { - /*struct _car netcar;*/ int servertime,clienttime,nb; - memcpy(&servertime,packet->data+strlen("positions")+1,sizeof(int)); - memcpy(&clienttime,packet->data+strlen("positions")+1+sizeof(int),sizeof(int)); - memcpy(&nb,packet->data+strlen("positions")+1+sizeof(int)+sizeof(int),sizeof(int)); + servertime=SDLNet_Read32(packet->data+strlen("positions")+1); + clienttime=SDLNet_Read32(packet->data+strlen("positions")+1+4); + nb=SDLNet_Read16(packet->data+strlen("positions")+1+4+4); if (clienttime>lastack) { memcpy(net.keys,net.keys+clienttime-lastack,net.time+1); @@ -547,19 +644,31 @@ void zeRace_launch(int alltime,int go) if (clienttime>servertime+5) delay+=DELAY; if (clienttimedata+strlen("positions")+1+sizeof(int)*3,sizeof(struct _car)*nb); + for (i=0;idata+strlen("positions")+1+4+4+2+i*8); + newnetpos[i].y=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*8+2); + newnetpos[i].angle=(float)SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*8+2+2)/1000; + newnetpos[i].color=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*8+2+2+2); + } lastack=clienttime; } - } else return; + } + else /* end of this network race */ + { + zeRace_send_time(best); + free_mem(); + return; + } if (strlen(net.keys)!=0) { tmp=packet->data; strcpy(tmp,"keys"); tmp+=strlen(tmp)+1; - memcpy(tmp,&lastack,sizeof(int)); - tmp+=sizeof(int); + SDLNet_Write32(lastack,tmp); + tmp+=4; strcpy(tmp,net.keys); tmp+=strlen(tmp)+1; packet->len=(void *)tmp-(void *)packet->data+10; @@ -664,6 +773,19 @@ void zeRace_launch(int alltime,int go) if (config.sound) Mix_PlayMusic(crash,1)==-1; } + /* game time */ + current.time++; + net.time++; + if (udpsock && net.time>MAX_LAG) + { + print(screen,WIDTH/2-strlen("Timeout !")*5,HEIGHT/2-10,"Timeout !"); + SDL_Flip(screen); + zeRace_send_time(best); + free_mem(); + return; + } + alltime++; + /* if we completed a lap */ if (car.lapflag) { @@ -684,16 +806,6 @@ void zeRace_launch(int alltime,int go) /* let the system breath */ SDL_Delay(delay); - /* game time */ - current.time++; - net.time++; - if (udpsock && net.time>MAX_LAG) - { - print(screen,WIDTH/2-strlen("Timeout !")*5,HEIGHT/2-10,"Timeout !"); - SDL_Flip(screen); - return; - } - alltime++; } } @@ -719,7 +831,7 @@ void zeRace_splash() print(screen,screen->w/2-strlen("zeRace " VERSION)*5,screen->h/2-splash->h/2-20,"zeRace " VERSION); SDL_FreeSurface(splash); SDL_Flip(screen); - /*SDL_Delay(2000);*/ + SDL_Delay(2000); } @@ -803,15 +915,15 @@ void zeRace_top10(char *buf) int i,nb,tmp; SDL_Rect pos; SDL_FillRect(screen,NULL,0x000000); - memcpy(&nb,buf,sizeof(int)); - buf+=sizeof(int); + nb=SDLNet_Read16(buf); + buf+=2; print(screen,WIDTH/2-16*5,HEIGHT/14,"* Race results *"); for (i=0;ilen=(void *)tmp-(void *)packet->data; SDLNet_UDP_Send(udpsock,-1,packet); /* network loop */ @@ -861,9 +973,9 @@ void zeRace_connect(char *host,int port) zeRace_exit(); } tmp+=strlen(tmp)+1; - memcpy(&time,tmp,sizeof(int)); - tmp+=sizeof(int); - memcpy(&network_speed,tmp,sizeof(int)); + time=SDLNet_Read32(tmp); + tmp+=4; + network_speed=SDLNet_Read32(tmp); zeRace_launch(time,go); if (strcmp(packet->data,"finish")==0) zeRace_top10(packet->data+strlen(packet->data)+1); lag=0; @@ -974,14 +1086,14 @@ void zeRace_internet() printf("dowloading list of servers... "); fflush(stdout); - if(SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1) + if (SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1) { fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError()); return; } tcpsock=SDLNet_TCP_Open(&ip); - if(!tcpsock) + if (!tcpsock) { fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError()); return; @@ -989,7 +1101,7 @@ void zeRace_internet() len=strlen(request); result=SDLNet_TCP_Send(tcpsock,request,len); - if(result"); print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*(CONFIG_OPTIONS+2),"Back to main menu"); SDL_Flip(screen); } @@ -1157,7 +1270,8 @@ void zeRace_config() if (config.color<0) config.color=11; if (config.color>11) config.color=0; break; - case 10: + case 10: config.boss=0; update(); config.boss=read_key(); break; + case 11: return; } update();