diff options
author | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2010-10-31 12:58:24 +0000 |
---|---|---|
committer | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2010-10-31 12:58:24 +0000 |
commit | dfc039b43230da075d6e84a6019958e977163a17 (patch) | |
tree | e9df864842cbb92b654614921dfa2ca6b4f1c633 | |
parent | 6a5f96bba765d89596227d072e8790d9f7d0ff2f (diff) | |
download | 2010-netlemmings-dfc039b43230da075d6e84a6019958e977163a17.tar.gz 2010-netlemmings-dfc039b43230da075d6e84a6019958e977163a17.tar.bz2 2010-netlemmings-dfc039b43230da075d6e84a6019958e977163a17.zip |
Séparation des fonctions avec des includes de .c directement. DAms continue a améliorer le code, et ludo prends des fonctions petit à petit pour les intégrées dans le tronc principal des sources (/trunk/src)
git-svn-id: file:///var/svn/2010-netlemmings/trunk@122 077b3477-7977-48bd-8428-443f22f7bfda
3 files changed, 3397 insertions, 3394 deletions
diff --git a/sdl-test/SDL_tuto/TestParserLemmingsLVL/fonctions_integrees.c b/sdl-test/SDL_tuto/TestParserLemmingsLVL/fonctions_integrees.c new file mode 100644 index 0000000..6d115b4 --- /dev/null +++ b/sdl-test/SDL_tuto/TestParserLemmingsLVL/fonctions_integrees.c @@ -0,0 +1,53 @@ +// Est inclus en direct dans parse_ini.yy +int init() +{ + + //Initialisation de tous les sous-systèmes de SDL + if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) + { + return 90; + } + + //Mise en place de l'écran + screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE | SDL_DOUBLEBUF ); + + //-DEBUG-printf("DEBUG ludo : screen->w==%i, screen->h==%i\n", screen->w, screen->h); + //S'il y a une erreur lors de la mise en place de l'écran + if( screen == NULL ) { + //-DEBUG-printf("problème\n"); + return 2; + } + + pTerrain = SDL_CreateRGBSurface(SDL_HWSURFACE, LEVEL_WIDTH, LEVEL_HEIGHT, + SCREEN_BPP,0,0,0,0);//(ccc_black>> 16) & 0xff, (ccc_black>> 8)&0xff,(ccc_black& 0xff),0); + if( pTerrain == NULL ) { + return 3; + } + + pSpr_Lem = SDL_CreateRGBSurface(SDL_HWSURFACE, LEVEL_WIDTH, LEVEL_HEIGHT, + SCREEN_BPP,0,0,0,0); + + if( pSpr_Lem == NULL ) { + return 3; + } + + pStencil = SDL_CreateRGBSurface(SDL_SWSURFACE, LEVEL_WIDTH, LEVEL_HEIGHT, + SCREEN_BPP,0,0,0,0); + if( pStencil == NULL ) { + return 3; + } + + pStencilFixe = SDL_CreateRGBSurface(SDL_SWSURFACE, LEVEL_WIDTH, LEVEL_HEIGHT, + SCREEN_BPP,0,0,0,0); + if( pStencil == NULL ) { + return 3; + } + //Mise en place de la barre caption + SDL_WM_SetCaption( "Test_LVL", NULL ); + //No pointer of mouse + SDL_ShowCursor(0); + + //Si tout s'est bien passé + return 0; +} + diff --git a/sdl-test/SDL_tuto/TestParserLemmingsLVL/fonctions_non_integrees.c b/sdl-test/SDL_tuto/TestParserLemmingsLVL/fonctions_non_integrees.c new file mode 100644 index 0000000..9bf4d27 --- /dev/null +++ b/sdl-test/SDL_tuto/TestParserLemmingsLVL/fonctions_non_integrees.c @@ -0,0 +1,3281 @@ + +// Est inclus en direct dans parse_ini.yy +Uint32 string_to_Uint32 (char* c,int taille){ + Uint32 u32c; + int i,n,lc = strlen(c); + int t[taille]; + if(lc-3 != taille){ + //-DEBUG-printf("ERREUR: wrong format .. expect (strlen[%s] - 3)==%d but it's = %d\n",c,taille,lc-3); + return(ccc_error); + } + u32c = 0; + for(i=0;i<taille;++i){ + n = c[i+2]; + ////-DEBUG-printf("n = %d, %c\n",n); + if (n > 47 && n < 58 ) { t[i]=(n - 48) ;} + if (n > 64 && n < 71 ) { t[i]=(n - 55) ;} + if (n > 96 && n < 103 ){ t[i]=(n - 87) ;} + } + n=0; + for(i=0;i<taille;++i){ + //-DEBUG-printf("t[%d]= %d, *(%d), %d\n",i,t[i],(1<<(4*(taille-i-1))),t[i]*(1<<(4*(taille-i-1))) ); + + n+=t[i]*(1<<(4*(taille-i-1))); + + + //-DEBUG-printf("n= %d\n",n); + } + u32c= n & 0xffffff; + return u32c; +} + + +Uint32* tab_of_string_to_Uint32 (char* c, int taille){ + int i,lc = strlen(c); + char t[10]; + int cpt=0; + int cpt2=0; + Uint32* tab=malloc(sizeof(Uint32)*(lc/(taille+3))); + t[0]='0';t[1]='x';t[8]=',';t[9]=0; + + for(i=0;i<lc;++i){ + // token + if(c[i]=='x'){cpt=0;} + if(cpt<6){ + t[cpt+2]=c[i+1]; + ++cpt; + if(cpt==6){ + tab[cpt2]=string_to_Uint32(t,taille); + cpt2++; + } + } + } + return tab; +} + +/* inverse la liste fournie en parametre et la renvoie */ +struct listeSimplementChainee* rev_listeO (struct listeSimplementChainee* liste) +{ + /* un élément temporaire */ + struct listeSimplementChainee* travail=NULL; + /* la liste inversée */ + struct listeSimplementChainee* retour=NULL; + /* on parcoure la liste entree tant qu'il y a un élément */ + while(liste!=NULL){ + /* on prend le premier element de la liste */ + travail=liste; + // //-DEBUG-printf("DEBUG:2.1 liste = %p travail = %p\n",liste,travail); + /* le début de la liste devient l'élément suivant */ + liste=travail->next; + /* on libère l'élélement de la liste et on le place en début de la + liste à renvoyer*/ + travail->next=retour; + /* l'élément qu'on vient de mettre en début de liste devient + le début de la liste de à renvoyer */ + retour=travail; + } + /* YAPUKA renvoyer la liste inversée */ + return retour; +} + + + +// returne le state de l'oblet a afficher +int anim_objet(int typeS, int typeO, int cptFps, int frames, int *cpt, struct gameInit *gInit){ + int f=0; + if(typeS == 90){typeS=4;}//Lemming anim continue + if(typeS == 91){typeS=5;}//Lemming anim at once + switch(typeS){ + // not animed + case 0 : break;//f=0 + // Object animation continue + case 1 : f=(cptFps/ANIM_STEP_FPS)%(frames);break; + // Object animation at once + case 2 : if((*cpt) > 0){ + f=(cptFps/ANIM_STEP_FPS)%(frames);--(*cpt); + } + break; + //animation at start level + case 3 : if((SDL_GetTicks()-TIME_WAIT_TRAP_START)>0){ + if(cptFps<frames*ANIM_STEP_FPS){ + f=(cptFps/ANIM_STEP_FPS)%(frames); + } else { + f=(frames-1);//on s'arrête à last frame + if(typeO==32){// entry is open + if(gInit->cptGame[16] < gInit->mapI.nbrEntry){++(gInit->cptGame[16]);} + } + } + } + break; + // Lemming animation continue + case 4 : f=(cptFps)%(frames);break; + // Lemming animation at once + case 5 : if((*cpt) > 0){ + f=(cptFps)%(frames);--(*cpt); + } else {//on s'arrête à last frame + f=(frames-1); + } + break; + default : //-DEBUG-printf("BUG typeSprite : %d\n",typeS); + exit(12); + } + /* + switch(typeO){ + case 0 : f=cptFps%(frames);break; + case 3 : f=cptFps%(frames);break;//FIXME + case 4 : f=cptFps%(frames);break;//FIXME + case 5 : f=cptFps%(frames);break;//FIXME + case 6 : f=cptFps%(frames);break;//FIXME + case 7 : f=cptFps%(frames);break;//FIXME + case 8 : f=cptFps%(frames);break;//FIXME + case 32 : f=cptFps%(frames);break;//FIXME + default : //-DEBUG-printf("BUG typeObjet : %d\n",type0); + return(12); + } + }*/ +/* # animation types +# 0 - don't animate +# 1 - animate continously +# 2 - trap - animate on trigger - else show first pic +# 3 - entry animation: animate once at level start + +# object types +# 0 - passive +# 3 - no digging to the left +# 4 - no digging to the right +# 5 - trap which makes lemmings drown (water/quick sand/mud) +# 6 - trap which replaces lemming with death animation +# 7 - trap which triggers lemming death animation +# 8 - exit +# 32 - entry*/ +return f; +} + +inline Uint32 get_pixel32( int x, int y, SDL_Surface *surface ) +{ + //Convertie les pixels en 32 bit + Uint32 *pixels = (Uint32 *)surface->pixels; + ////-DEBUG-printf("4) x=%d,y=%d,p =%X\n",x,y,pixels[ ( y * surface->w ) + x ]); + //Recupere le pixel demande + + /*//-DEBUG-printf("out get_pixel32 !! => (((y %d * surface->w %d ) + x %d) => %d >= surface->h %d *surface->w %d = %d)\n",y,surface->w,x,(y * surface->w ) + x,surface->h,surface->w,surface->h*surface->w);*/ + /* if ( surface==NULL ) { //-DEBUG-printf("ludo1\n"); + return (ccc_error); } + if ( pixels==NULL ) { + //-DEBUG-printf("ludo2\n"); + return (ccc_error); } + + if((((y * surface->w ) + x) >= surface->h*surface->w)|| + (((y * surface->w ) + x) < 0)){ + if(y==surface->h){//-DEBUG-printf("get C'EST PAS ASSEZ GROS POUR TOI !!!! y=surface->h\n"); + return (ccc_error);} + //-DEBUG-printf("out get_pixel32 !! => (((y %d * surface->w %d ) + x %d) => %d >= surface->h %d *surface->w %d = %d)\n",y,surface->w,x,(y * surface->w ) + x,surface->h,surface->w,surface->h*surface->w); + return (ccc_error); + } + */ + + return pixels[ ( y * surface->w ) + x ]; + +} + +inline int putPixel(SDL_Surface *surface,Uint16 x,Uint16 y,Uint32 colori) +{ + /* bpp de la surface ecran */ + Uint8 bpp = surface->format->BytesPerPixel; + + /* pointeur vers le pixel a remplacer */ + /* if((((y * surface->w ) + x) >= surface->h*surface->w)|| + (((y * surface->w ) + x) < 0)){ + //-DEBUG-printf("out putPixel !! => (((y %d * surface->w %d ) + x %d) => %d >= surface->h %d *surface->w %d = %d)\n",y,surface->w,x,(y * surface->w ) + x,surface->h,surface->w,surface->h*surface->w); + return (101); + } + */ + if(y==surface->h){//-DEBUG-printf("put C'EST PAS ASSEZ GROS POUR TOI !!!! y=surface->h\n"); + return (100);} + Uint8 *p = ((Uint8 *)surface->pixels) + y * surface->pitch + x * bpp; + + /*//-DEBUG-printf("pixel R = %X G = %X B = %X A=%X bpp = %d, ccc=%X \n", + ((colori>>16) & 0xff), + ((colori>>8) & 0xff), + (colori & 0xff), + ((colori>>24) & 0xff), + bpp, + colori );*/ + + switch(bpp) + { + case 1: + *p = (Uint8) colori; + break; + case 2: + *(Uint16 *)p = (Uint16) colori; + break; + case 3: + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) + { + *(Uint16 *)p = ((colori >> 8) & 0xff00) | ((colori >> 8) & 0xff); + *(p + 2) = colori & 0xffff; + } + else + { + *(Uint16 *)p = colori & 0xffff; + *(p + 2) = ((colori >> 16) & 0xffff) ; + } + break; + case 4:{ + *(Uint32 *)p = colori; + } + break; + } + return 0; +} + +//return number of free pixels below the lemming +int checkPixelDOWN(int nbr, int x, int y){ + int res; + int err; + int yb; + + if (x<0 || x >= LEVEL_WIDTH) + {return 0;} + + yb = y + 1; + + for(res=0;res<nbr;++res){ + if(yb+res >= LEVEL_HEIGHT){ + return FALL_DISTANCE_FORCE_FALL; + } + + putPixel(pSpr_Lem,x,yb+res,ccc_red); + + err=get_pixel32(x,yb+res,pStencil); + if(err==ccc_error){return 113;} + + ////-DEBUG-printf("res %d\n",res); + + if((err&0xbb)!=0xbb){// it's not an object + if(err!=ccc_bgStencil){ + return res;//return ((res == 0)? 0 : res-1);//res; + } + } + } + return res; +} + +//return number of free pixels above the lemming +int checkPixelUP(int nbr, int x, int y, int size ){ + int res; + int err; + int ym; + + if (x<0 || x >= LEVEL_WIDTH) + return 0; + + ym=midY(y,size); + + for(res=0;res<nbr;++res){ + if(ym-res <= 0){ + return -1;//splat + } + + putPixel(pSpr_Lem,x,ym-res,ccc_green); + + err=get_pixel32(x,ym-res,pStencil); + if(err==ccc_error){return 113;} + + if((err&0xbb)!=0xbb){// it's not an object + if(err!=ccc_bgStencil){ + return res;//return ((res == 0)? 0 : res-1);//res; + } + } + } + return res; +} + +//return true if Lemming has fallen to/through the bottom of the level, false otherwise +int outOfLowerMap(int y) { + if (y >= LEVEL_HEIGHT) { + //-DEBUG-printf("Play Sound SND_DIE\n"); + //GameController.sound.play(GameController.SND_DIE); + return 1; + } + return 0; +} + +//return number of pixels of walkable ground above the Lemmings foot. +int aboveGround(int x, int y) { + + int res=0; + int err; + + if (x<0 || x >= LEVEL_WIDTH) + return LEVEL_HEIGHT-1; + + if (y >= LEVEL_HEIGHT) + return LEVEL_HEIGHT-1; + + //y+=1; + + for(res=0;res<WALKER_OBSTACLE_HEIGHT;++res){ + if(y-res<0){ + return WALKER_OBSTACLE_HEIGHT +1;// forbid leaving level to the top + } + + putPixel(pSpr_Lem,x,y-res,ccc_cyan); + + err=get_pixel32(x,y-res,pStencil); + if(err==ccc_error){return 114;} + + + if((err&0xbb)==0xbb){//it'an object + return res; + } + if((err&0xcb)==0xcb){// it's a blocker + return res; + } + if(err==ccc_bgStencil){ + return res; + } + } + return res; +} +//return Position inside lemming which is used for collisions +int midY(int y, int size) { + return y - size; +} + +//return true if climber reached a plateau he can walk on, false otherwise +int reachedPlateau(int x, int y, int size, int dir) { + + int ym; + int err; + + if (x<2 || x >= LEVEL_WIDTH-2) + return 0; + + ym = midY(y,size); + if (ym>= LEVEL_HEIGHT || ym<0) + return 0; + + if (dir == 0)//LEFT + x -= 2; + else + x += 2; + + putPixel(pSpr_Lem,x,y,ccc_yellow); + + err=get_pixel32(x,y,pStencil); + if(err==ccc_error){return 115;} + + if((err&0xbb)==0xbb){//it'an object + return 1; + } + if((err&0xcb)==0xcb){// it's a blocker + return 0; + } + if(err==ccc_bgStencil){ + return 1; + } + /* + + if((err&0xbb)!=0xbb){ +//if(err!=(ccc_lWalk_on & ccc_bgStencil)){ +if((err|ccc_lWalk_on)!=ccc_bgStencil){ +return 1; +} +} + */ +return 0; +} + +int explode(int x, int y, int size){ + //-DEBUG-printf("Play Sound SND_EXPLODE\n"); + //GameController.sound.play(GameController.SND_EXPLODE); + return 0; +} + +//return stencil value from the middle of the lemming +int stencilMid(int x, int y) { + Uint32 err; + if (x>0 && x<LEVEL_WIDTH && y > 0 && y < LEVEL_HEIGHT){ + + putPixel(pSpr_Lem,x,y,0xff00ff); + err=get_pixel32(x,y,pStencil); + } + else { + err = ccc_bgStencil; + } + return err; +} + +// Check if a Lemming is to be turned by a stopper/blocker +// return true if Lemming is to be turned, false otherwise +int turnedByStopper(int x, int y, int dir) { + Uint32 err; + err=stencilMid(x,y); + if(err==ccc_error){return 116;} + if(err==ccc_lWalk_on){return 2;} + + ////-DEBUG-printf("err %p\n",err); + + if (err == ccc_lStopperLeft && dir==1) {//Right + //LEFT; + return 0; + } + if (err == ccc_lStopperRight && dir==0) {//Left + //RIGHT + return 1; + } + return 2; +} + +int stateLemming(struct gameInit *gInit){ + + struct listeSimplementChainee *k; + struct listeSimplementChainee *o; + SDL_Surface *sfT; + int boom=0; + int flip=0; + int free=0; + int fx=0; + int fy=0; + int fs=0; + int lh=0; + int lw=0; + int lState=0; + int fa; + //int checkMask; + //int sx; + //int sy; + //int idx; + //int maxF; + int err; + int levitation; + int oldX; + int oldType; + int newType; + int newID; + + k=gInit->l.ll; + while(k != NULL){ + if((k->ID<LEM_JOB)||(k->ID>19)){ + switch(k->ID){ + case 20: newID=10;break; + case 21: newID=6;break; + case 22: newID=4;break; + default: newID=k->ID;break; + } + + fx=gInit->mapI.lemmingDATA[newID].footX; + fy=gInit->mapI.lemmingDATA[newID].footY; + //k->y-=gInit->mapI.lemmingDATA[0].footY-fy; + fs=gInit->mapI.lemmingDATA[newID].footSize; + lState=gInit->mapI.lemmingDATA[newID].state; + + sfT = gInit->mapI.map.tabGif[gInit->mapI.map.tiles*2 + gInit->mapI.map.tilesObjet*2 +gInit->mapI.map.tilesSteel +(newID)*2+(k->data.pl->dir) + ADD_OBJ]; + + lh=sfT->h/lState; + lw=sfT->w; + + oldX=k->x; + oldType=k->ID; + newType=k->ID; + + // cooldown before lemming boom + boom=0; + switch(k->data.pl->cptBoom){ + case 0: boom=1;break; + case 404: break; + default: { + --k->data.pl->cptBoom; + if(paint_bomber(k->x,k->y,lw,gInit->mapI.map.tabGif[2],pSpr_Lem,(k->data.pl->cptBoom/FPS +1))!=0){return 99;} + } + } + + //flipDirBorder + if (k->data.pl->dir==1) {//right + if (k->x<0) { + k->x=0; + flip = 1; + } else if (k->x >= LEVEL_WIDTH) { + k->x = LEVEL_WIDTH-1; + flip = 1; + } + } + //if (selectCtr>0) {selectCtr--;} + if (flip) {k->data.pl->dir = (k->data.pl->dir==1) ? 0 : 1 ;} + + switch(k->ID){ + case 3 : // climber_to_walker + //-DEBUG-printf("climber_to_walker ->"); + if(boom){ + explode (k->x+fx,k->y+fy,fs); + k->ID=10;//BOMBER + break; + } + case 1 : // faller + {//-DEBUG-printf("faller ->"); + if(boom){ + explode (k->x+fx,k->y+fy,fs); + k->ID=10;//BOMBER + break; + } + ////-DEBUG-printf("x =%d y =%d x+fx= %d, y+fy =%d \n",k->x,k->y,k->x+fx,k->y+fy); + free=checkPixelDOWN(FALLER_STEP, k->x+fx,k->y+fy); + if(free==113){return 113;} + if(free==FALL_DISTANCE_FORCE_FALL) + {k->y += FALLER_STEP;} + else + {k->y += free;} // max: FALLER_STEP + err=outOfLowerMap(k->y+fy); + if(err==114){return 114;} + if(err==0){ + k->data.pl->cptFall+=free; + // check conversion to floater + if((k->data.pl->floater==1)&& + (k->data.pl->cptFall>= FALL_DISTANCE_FLOAT)){ + k->ID=22;//FLOATER_START + k->data.pl->cptFall2 = 0;// used for parachute opening "jump" up + } else if(free==0) {// check ground hit + if(k->data.pl->cptFall > gInit->mapI.MaxFallDistance){ + k->ID=5;//SPLAT + } else { + k->ID=0;//WALKER + k->y-=2; + k->data.pl->cptFall=0; + } + } + } else { + k->ID=17; + } + break; + } + case 16 : // JUMPER + { //-DEBUG-printf("JUMPER ->"); + if(boom){ + k->ID=10;//BOMBER + if(!gInit->mapI.nuke){ + //-DEBUG-printf("Play Sound SND_EXPLODE\n"); + //GameController.sound.play(GameController.SND_EXPLODE); + } + break; + } + // check collision with stopper + err=turnedByStopper(k->x+lw/2,k->y+fy,k->data.pl->dir); + if(err==116){return 116;} + if(err!=2){ + k->data.pl->dir=err; + break; + } + err = aboveGround(k->x+fx,k->y+fy); + if(err==114){return 114;} + levitation=err; + if (levitation > JUMPER_STEP) + {k->y -= JUMPER_STEP;} + else { + // conversion to walker + k->y -= levitation; + k->ID=0; //WALKER; + } + } + break; + case 0: // WALKER + { //-DEBUG-printf("WALKER ->"); + if(boom){ + k->ID=10;//BOMBER + if(!gInit->mapI.nuke){ + //-DEBUG-printf("Play Sound SND_EXPLODE\n"); + //GameController.sound.play(GameController.SND_EXPLODE); + } + break; + } + // check collision with stopper + err=turnedByStopper(k->x+lw/2,k->y+fy,k->data.pl->dir); + if(err==116){return 116;} + if(err!=2){ + k->data.pl->dir=err; + break; + } + k->x += (k->data.pl->dir==1)? WALKER_STEP : -WALKER_STEP; + // check + free=checkPixelDOWN(FALL_DISTANCE_FALL, k->x+fx,k->y+fy); + if(free==113){return 113;} + if(free==FALL_DISTANCE_FORCE_FALL) + {k->y += FALLER_STEP;} + else + { + k->y += free; // max: FALLER_STEP + k->data.pl->cptFall=free; + } + err = aboveGround(k->x+fx,k->y+fy); + if(err==114){return 137;} + levitation=err; + //-DEBUG-printf("levitation %d free %d \n",levitation,free); + if((levitation < WALKER_OBSTACLE_HEIGHT) && ((k->y+fy+lh/2)>0)) + { + if (levitation >= JUMPER_STEP) + { + k->y -= JUMPER_STEP; + k->ID = 16; + break; + } + else { + // conversion to jumper + k->y -= levitation; + } + } else { + k->x=oldX; + if(k->data.pl->climber==1){ + k->ID=2; // CLIMBER + break; + } else { + k->data.pl->dir = (k->data.pl->dir==1) ? 0 : 1 ; + } + } + if(free>0){ + // check for conversion to faller + k->data.pl->cptFall += FALLER_STEP; + if(free >= FALL_DISTANCE_FALL){ + k->ID=1; // FALLER + } + } + break; + } + case 22: // FLOATER_START + //-DEBUG-printf("FLOATER_START ->"); + if(boom){ + explode (k->x+fx,k->y+fy,fs); + k->ID=10;//BOMBER + break; + } + switch (k->data.pl->cptFall2++) { + case 0: + case 1: // keep falling with faller speed + case 2: k->y += FALLER_STEP-FLOATER_STEP; + break; + case 3: k->y -= FLOATER_STEP-1; // decelerate a little + break; + case 4: + case 5: + case 6: + case 7: k->y -= FLOATER_STEP; // decelerate some more + break; + default: + k->ID=4;//FLOATER; + } + + case 4 : //FLOATER + //-DEBUG-printf("FLOATER ->"); + if(boom){ + explode (k->x+fx,k->y+fy,fs); + k->ID=10;//BOMBER + break; + } + free=checkPixelDOWN(FALLER_STEP, k->x+fx,k->y+fy); + if(free==113){return 113;} + if(free==FALL_DISTANCE_FORCE_FALL) + {k->y += FALLER_STEP;} + else + {k->y += free;} // max: FALLER_STEP + + err=outOfLowerMap(k->y+fy); + if(err==114){return 114;} + if(err==0){ + k->data.pl->cptFall+=free; + if(free==0){ + k->ID=0;//WALKER + k->data.pl->cptFall=0; + } + } else { + k->ID=17; + } + break; + case 2 : // CLIMBER + //-DEBUG-printf("CLIMBER ->"); + if(boom){ + explode (k->x+fx,k->y+fy,fs); + k->ID=10;//BOMBER + break; + } + + if ( (++(k->data.pl->cptFall) & 1) == 1) // only every other step + {k->y -= CLIMBER_STEP;} + //fa=(k->data.pl->dir==1) ? 4 : 3 ; + err=checkPixelUP(2,k->x+fx+3,k->y+fy,fs); + if(err==113){return 127;} + if (midY(k->y+fy,fs) < 0 || err<2) { + k->data.pl->dir = (k->data.pl->dir==1) ? 0 : 1 ; + k->ID=1; //FALLER; + k->data.pl->cptFall = 0; + } else { + err=reachedPlateau(k->x+fx+3,k->y+fy,fs,k->data.pl->dir); + if (err==115){return 115;} + if(err) { + k->data.pl->cptFall = 0; + k->ID=3; //CLIMBER_TO_WALKER; + } + } + break; + + case 5: //SPLAT + //-DEBUG-printf("SPLAT ->"); + if(boom){ + explode (k->x+fx,k->y+fy,fs); + k->ID=10;//BOMBER + break; + } + if(k->data.pl->cptState==0){ + k->data.pl->cptState=gInit->mapI.lemmingDATA[newID].state; + //-DEBUG-printf("Play Sound SND_SPLAT\n"); + //GameController.sound.play(GameController.SND_SPLAT); + } + break; + /* + case 13: //DIGGER + if(boom){ + k->ID=10;//BOMBER + if(!gInit->mapI.nuke){ + //-DEBUG-printf("Play Sound SND_EXPLODE\n"); + //GameController.sound.play(GameController.SND_EXPLODE); + } + break; + } + break; + case 14: // BASHER + { + if(boom){ + k->ID=10;//BOMBER + if(!gInit->mapI.nuke){ + //-DEBUG-printf("Play Sound SND_EXPLODE\n"); + //GameController.sound.play(GameController.SND_EXPLODE); + } + break; + } + err=turnedByStopper(k->x+lw/2,k->y+fy,k->data.pl->dir); + if(err==116){return 116;} + if(err!=2){ + k->data.pl->dir=err; + k->ID=0;//WALKER + break; + } + free=checkPixelDOWN(FALLER_STEP, ccc_black, k->x+fx,k->y+fy,pStencil); + if(free==113){return 113;} + if(free==FALL_DISTANCE_FORCE_FALL) + {k->y += FALLER_STEP;} + else + {k->y += free;} // max: FALLER_STEP + + if(free != 0){ + k->data.pl->cptFall += free; + if (k->data.pl->cptFall >= BASHER_FALL_DISTANCE) + { + k->ID=1;//FALLER; + } + } else { + k->data.pl->cptFall = 0; + } + //Mask m; + //int checkMask; + maxF=gInit->mapI.lemmingDATA[newID].state + idX= maxF - k->data.pl->cptState + 1; + if (idx >= maxF) {idx=0;} + switch(idx){ + case 2 : + case 3 : + case 4 : + case 5 : { + // bash mask should have the same height as the lemming + sx=k->x; + sy=k->y; + checkMask = ccc_s|((k->data.pl->dir==0) ? ccc_oNoDigLeft : ccc_oNoDigRight); + eraseMask(sx); + } + } + } + //ETC ... + */ + case 12: //BUILDER_END + //-DEBUG-printf("BUILDER_END ->"); + if(boom){ + k->ID=10;//BOMBER + if(!gInit->mapI.nuke){ + //-DEBUG-printf("Play Sound SND_EXPLODE\n"); + //GameController.sound.play(GameController.SND_EXPLODE); + } + } + k->data.pl->cptBrick=0; + k->data.pl->cptState=0; + // k->y+=5; + k->ID=0; + + break; + + case 11: // BUILDER + { + //-DEBUG-printf("BUILDER ->"); + if(boom){ + k->ID=10;//BOMBER + if(!gInit->mapI.nuke){ + //-DEBUG-printf("Play Sound SND_EXPLODE\n"); + //GameController.sound.play(GameController.SND_EXPLODE); + } + break; + } + // check collision with stopper + err=turnedByStopper(k->x+lw/2,k->y+fy,k->data.pl->dir); + if(err==116){return 116;} + if(err!=2){ + k->data.pl->dir=err; + break; + } + /* if((k->data.pl->cptState==0)&& + (k->data.pl->cptBrick==0)){k->y-=5;}*/ + + k->data.pl->cptState++; + if(k->data.pl->cptState >= lState){ + // step created -> move up + k->data.pl->cptState=0; + k->data.pl->cptBrick++; + + if (k->data.pl->dir == 1) + { k->x += 4;} // step forward + else + { k->x -= 4;} + k->y -= 2;// step up + err = aboveGround(k->x+fx,k->y+fy);// should be 0, if not, we built into a wall -> stop + if(err==114){return 117;} + levitation=err; + // check for conversion to walker + err=checkPixelUP(8, k->x+fx,k->y+fy,fs); // check if builder is too close to ceiling + if(err==113){return 113;} + fa=err; + if(fa<8|| levitation > 0){ + // k->y+=5; + k->ID=0;//WALKER + // a lemming can jump through the ceiling like in Mayhem2-Boiler Room + if (levitation >= WALKER_OBSTACLE_HEIGHT) { + // avoid getting stuck + k->x = oldX; + k->y += 2; + } + k->data.pl->dir = (k->data.pl->dir==1) ? 0 : 1 ; + break; + } + if(k->data.pl->cptState >= STEPS_MAX){ + k->ID=12; //BUILDER_END + break; + } + } else { + if (k->data.pl->cptState==9){ + //-DEBUG-printf("insert mask\n"); + if(k->data.pl->cptBrick >= STEPS_WARNING){ + //-DEBUG-printf("Play Sound SND_TING\n"); + //GameController.sound.play(GameController.SND_TING); + } + } + } + break; + } + case 6: // STOPPER + { //-DEBUG-printf("STOPPER ->"); + if(boom){ + k->ID=10;//BOMBER + if(!gInit->mapI.nuke){ + //-DEBUG-printf("Play Sound SND_EXPLODE\n"); + //GameController.sound.play(GameController.SND_EXPLODE); + } + break; + } + //check for conversion to faller + free=checkPixelDOWN(FLOATER_STEP, k->x+fx,k->y+fy); + if(free==113){return 113;} + if(free>0){ + if(free==FALL_DISTANCE_FORCE_FALL) + {k->y += FALLER_STEP;} + else + {k->y += free;} // max: FALLER_STEP + k->data.pl->cptFall+=free; + if (k->data.pl->cptFall >= FALL_DISTANCE_FALL) + {k->ID=1;}//FALLER; + else + {k->ID=0;}//WALKER; + // conversion to faller or walker -> erase stopper mask + // Mask m = lemmings[getOrdinal(Type.STOPPER)].getMask(dir); + // m.clearType(maskX,maskY,0,Stencil.MSK_STOPPER); + } else { + k->data.pl->cptFall=0; + } + break; + } + case 21: //BOMBER_STOPPER: + //-DEBUG-printf("BOMBER_STOPPER ->"); + free=checkPixelDOWN(FLOATER_STEP, k->x+fx,k->y+fy); + if(free==113){return 113;} + if(free>0){ + k->ID=10;//BOMBER + } else {break;} + + case 10: // BOMBER + //-DEBUG-printf("BOMBER ->"); + if(k->data.pl->cptBoom==0){ + k->data.pl->cptBoom=404; + k->data.pl->cptState=gInit->mapI.lemmingDATA[10].state; + } + free=checkPixelDOWN(FLOATER_STEP, k->x+fx,k->y+fy); + if(free==113){return 113;} + if(free==FALL_DISTANCE_FORCE_FALL) + {k->y += FALLER_STEP;} + else + {k->y += free;} // max: FALLER_STEP + err=outOfLowerMap(k->y+fy); + if(err==114){return 114;} + if(err==1) {k->ID=17;} + if(k->data.pl->cptState==16){ + err=ereasePx(k->x,k->y,gInit->mapI.map.tabGif[10],gInit->mapI.map.cmap.bgColor); + if(err!=0){return err;} + //TODO + } + break; + + default: + //-DEBUG-printf("default ->"); + if(boom){ + explode (k->x+fx,k->y+fy,fs); + k->ID=10;//BOMBER + break; + } + break; + }//FIN SWITCH + + + + // trap + if(k->ID != 7 && k->ID != 8 && k->ID != 9 && k->ID != 17){ + err=get_pixel32(k->x+fx,k->y,pStencil); + if(err==ccc_error){return 12;} + ////-DEBUG-printf("k->ID => %d get_pixel32(k->x %d+fx %d => %d,k->y %d,pStencil) == %p \n",k->ID,k->x,fx,k->x+fx,k->y,err); + if(err==ccc_oTrapDrown){ + k->ID=7; + k->data.pl->cptState=gInit->mapI.lemmingDATA[k->ID].state; + } + if(err==ccc_oTrapAndLem){ + k->ID=8; + k->data.pl->cptState=gInit->mapI.lemmingDATA[k->ID].state; + } + if(err==ccc_oTrapAndNoLem){ + k->ID=17; + k->data.pl->cptState=0; + o=gInit->o.lo; + while(o!=NULL){ + ////-DEBUG-printf("gInit->mapI.map.tabDataSprO[%d].type=>%d\n(o->x%d<=(k->x+fx)%d)\n object->w %d >= (k->x+fx) %d\n o->y %d <= (k->y) %d\n",o->ID,gInit->mapI.map.tabDataSprO[o->ID].type,(o->x),(k->x+fx),gInit->mapI.map.tabGif[gInit->mapI.map.tiles*2+o->ID*2+o->data.po->UD+ADD_OBJ]->w,(k->x+fx),o->y,(k->y)); + if((gInit->mapI.map.tabDataSprO[o->ID].type==6)&& + (o->x<=(k->x+fx))&& + (gInit->mapI.map.tabGif[gInit->mapI.map.tiles*2+o->ID*2+o->data.po->UD+ADD_OBJ]->w+o->x >= (k->x+fx))&& + (o->y<=(k->y))&& + (gInit->mapI.map.tabGif[gInit->mapI.map.tiles*2+k->ID*2+k->data.po->UD+ADD_OBJ]->h+o->y >= (k->y))){ + o->data.po->cptState=gInit->mapI.map.tabDataSprO[o->ID].state; + break; + } + o=o->next; + } + } + if(err==ccc_oExit){ + k->ID=9; + k->data.pl->cptState=gInit->mapI.lemmingDATA[k->ID].state; + } + } + //-DEBUG-printf("\n"); + } + k=k->next; + }// FIN WHILE + + + return 0; +} + +int ereasePx(int x,int y,SDL_Surface *s,Uint32 bgColor){ + int err; + SDL_Rect from,to; + + from.x=0; + from.y=0; + from.w=s->w; + from.h=s->h; + + to.x=x; + to.y=y; + to.w=x+s->w; + to.h=y+s->h; + + if((err=paint_manip(s,from,pTerrain,to,bgColor,ccc_lemming,18))!=0){ + return err; + } + if((err=paint_manip(s,from,pStencilFixe,to,ccc_bgStencil,ccc_lemming,18))!=0){ + return err; + } + + + return 0; +} + +int print_num(SDL_Surface *dst, SDL_Surface *src, int x, int y, int value) +{ + char buf[9]; + int i,r,v,p=0; + int max = 1000000000; + SDL_Rect from; + if(value >= 1000000000){ + //-DEBUG-printf("value out of Bound %d\n",value); + return(15); + } + v=value; + + while(max > 1) + { + r = v / max; + v -= r * max; + max /= 10; + if(p || r) + buf[p++] = r; + } + buf[p++] = value%10; + + /* Render! */ + from.x = 0; + from.w = src->w; + from.h = src->h/10; + + for(i = 0; i < p; ++i) + { + SDL_Rect to; + to.x = x + i * (from.w + 2); + to.y = y; + from.y = buf[i] * (from.h); + test_blit(src, &from, dst, &to); + } + return 0; +} + +int paint_bomber(int x,int y,int xMax,SDL_Surface *cooldown,SDL_Surface *dst,int cpt){ + + SDL_Rect rect,to; + + to.x=x;//+xMax/2; + rect.x=0; + rect.h=cooldown->h/5; + rect.y= rect.h * (cpt-1); + to.y=y-rect.h; + rect.w=cooldown->w; + + test_blit(cooldown, &rect, dst, &to); + + return 0; +} + + +int testAlpha(SDL_Surface *dst, SDL_Surface *src,SDL_Surface *src2, int x, int y){ + SDL_Rect from; + SDL_Rect to; + int i; + + from.x = 0; + from.w = src->w; + from.h = src->h/188; + + for(i = 0; i < 188; ++i) + { + from.y = i * (from.h ) ; + to.x = x + (i%40)*16; + to.y = y + from.h * (i/40); + test_blit(src, &from, dst, &to); + //print_num(screen, src2, to.x + 20, to.y, i); + } + + return 0; + +} + +int print_alpha(SDL_Surface *dst, SDL_Surface *src, int x, int y, char* msg) +{ + SDL_Rect from; + SDL_Rect to; + int i; + int n; + + from.x = 0; + from.w = src->w; + from.h = src->h/188; + + for(i = 0; i < strlen(msg); ++i) + { + n=msg[i]; + ////-DEBUG-printf("-> %d ~ %c\n",n,n); + from.y = 404; + if (n > 32 && n < 127) { from.y = (n - 33) * (from.h) ;} + //if (n == 32) {continue;} + to.x = x + i * (from.w); + to.y = y; + if(from.y!=404){ + test_blit(src, &from, dst, &to); + } + } + return 0; +} + + +SDL_Surface* flipSurfaceUD_LR(SDL_Surface* src, enum sens sensO, Uint32 c) { + int i,j; + + SDL_Surface* copy_surface = NULL; + + // This only works for 32 bit pixel format + if( src->format->BitsPerPixel == 32 ) { + // This surface must be freed by the caller. + copy_surface = SDL_CreateRGBSurface(src->flags, src->w, src->h, + src->format->BitsPerPixel, + + src->format->Rmask, + src->format->Gmask, + src->format->Bmask, + src->format->Amask); + + SDL_SetColorKey( copy_surface, + SDL_RLEACCEL | SDL_SRCCOLORKEY, + SDL_MapRGB/*A*/( copy_surface->format, + (c >> 16) & 0xff, + (c >> 8) & 0xff, + (c & 0xff)//, + //(c >> 24) & 0xff + ) + ); + + //FIXME : lock surface + Uint32 *source = src->pixels; + Uint32 *dest = copy_surface->pixels; + + for(i = 0; i < src->h; i++) { + for(j = 0; j < src->w; j++) { + if (sensO == UD){// Sens UD + dest[ (src->h-i-1)*src->w + j ] = source[ i*src->w + j]; + } + if (sensO == LR){// Sens LR + dest[ i*src->w + (src->w-j-1) ] = source[ i*src->w + j]; + } + } + } + } + + return copy_surface; +} + +SDL_Surface *load_image( char* filename, Uint32 cbg ) +{ + //L'image qui est chargée + SDL_Surface* loadedImage = NULL; + + //L'image optimisée qu'on va utiliser + SDL_Surface* optimizedImage = NULL; + + //Chargement de l'image + loadedImage = IMG_Load( filename ); + + //Si l'image est chargée + if( loadedImage != NULL ) + { + //Création de l'image optimisée + optimizedImage = SDL_DisplayFormat( loadedImage ); + + + //Libération de l'ancienne image + SDL_FreeSurface( loadedImage ); + + //Si la création de l'image optimisée s'est bien passée + if( optimizedImage != NULL ) + { + SDL_SetColorKey( optimizedImage, + SDL_RLEACCEL | SDL_SRCCOLORKEY, + SDL_MapRGB( optimizedImage->format, + (cbg >> 16) & 0xff, + (cbg >> 8) & 0xff, + (cbg & 0xff)//, + //(cbg >> 24) & 0xff + ) + ); + } + } + + //On retourne l'image optimisée + return optimizedImage; + + return loadedImage; +} + + +int load_fields(char *folder, struct gameInit *gInit, int tabNum[255] ) { + int i, lt,total,cpt=-1,llem; + char *temp; + char *filepath; + char *folderLem; + + temp = tabString_eMapStyle[gInit->mapI.map.style]; + lt = strlen(temp); + + llem= strlen("../../../misc/"); + folderLem=malloc(sizeof(char)*(llem+1)); + sprintf(folderLem,"%s","../../../misc/"); + + total=(LEM_JOB*2) + (gInit->mapI.map.tiles)*2 + gInit->mapI.map.tilesSteel + ((gInit->mapI.map.tilesObjet)*2); + + gInit->mapI.map.tabGif=malloc((total + ADD_OBJ) * sizeof(SDL_Surface *)); + + + //stored terrains + cpt=ADD_OBJ; + + filepath = malloc(sizeof(char)*(strlen(folder)+lt+strlen("/_.gif")+(sizeof(int)*10))); + + for(i=0; i < gInit->mapI.map.tiles*2 ; i+=2) { + sprintf(filepath, "%s/%s%s%d.gif", folder,temp,"_", i/2); + gInit->mapI.map.tabGif[i+cpt] = load_image(filepath,ccc_keyGif); + gInit->mapI.map.tabGif[i+1+cpt] = flipSurfaceUD_LR(load_image(filepath,ccc_keyGif),UD,ccc_keyGif); + if((gInit->mapI.map.tabGif[i+cpt] == NULL)||(gInit->mapI.map.tabGif[i+1+cpt] == NULL)) { + //-DEBUG-printf("ERREUR load_file Objet: %s\n", filepath); + return(16); + } else { + //-DEBUG-printf("load_file ok : %i -> '%s' + flip_LR\n", i+cpt, filepath); + } + } + free(filepath); + //stored objet + cpt=ADD_OBJ+gInit->mapI.map.tiles*2; + + filepath = malloc(sizeof(char)*(strlen(folder)+lt+strlen("/o_.gif")+(sizeof(int)*10))); + + for(i=0; i < gInit->mapI.map.tilesObjet*2 ; i+=2) { + sprintf(filepath, "%s/%s%s%d.gif", folder,temp,"o_", i/2); + gInit->mapI.map.tabGif[i+cpt] = load_image(filepath,ccc_keyGif); + gInit->mapI.map.tabGif[i+1+cpt] = flipSurfaceUD_LR(load_image(filepath,ccc_keyGif),UD,ccc_keyGif); + if((gInit->mapI.map.tabGif[i+cpt] == NULL)||(gInit->mapI.map.tabGif[i+1+cpt] == NULL)) { + //-DEBUG-printf("ERREUR load_file Objet: %s\n", filepath); + return(17); + } else { + ////-DEBUG-printf("load_file ok : %i -> '%s' + flip_LR\n", i+cpt, filepath); + } + } + free(filepath); + //stored steel + cpt=ADD_OBJ+(gInit->mapI.map.tiles*2)+(gInit->mapI.map.tilesObjet*2); + + filepath = malloc(sizeof(char)*(strlen(folder)+lt+strlen("/om_.gif")+(sizeof(int)*10))); + + for(i=0; i < gInit->mapI.map.tilesSteel ; ++i) { + sprintf(filepath, "%s/%s%s%d.gif", folder,temp,"om_", tabNum[i]); + gInit->mapI.map.tabGif[i+cpt] = load_image(filepath,ccc_keyGif); + if(gInit->mapI.map.tabGif[i+cpt] == NULL) { + //-DEBUG-printf("ERREUR load_file steel: %s\n", filepath); + return(18); + } else { + ////-DEBUG-printf("load_file ok : %i -> '%s'\n", i+cpt, filepath); + } + } + free(filepath); + //stored lemmingsDATA + cpt=ADD_OBJ+(gInit->mapI.map.tilesObjet*2)+(gInit->mapI.map.tiles)*2+gInit->mapI.map.tilesSteel; + + filepath = malloc(sizeof(char)*(strlen(folderLem)+strlen("lemm")+strlen("/_.gif")+(sizeof(int)*10))); + + for(i=0; i < LEM_JOB*2 ; i+=2) { + sprintf(filepath, "%s/%s%d.gif", folderLem,"lemm_",i/2); + gInit->mapI.map.tabGif[i+1+cpt] = load_image(filepath,ccc_lemming); + gInit->mapI.map.tabGif[i+cpt] = flipSurfaceUD_LR(load_image(filepath,ccc_lemming),LR,ccc_lemming); + if((gInit->mapI.map.tabGif[i+cpt] == NULL)||(gInit->mapI.map.tabGif[i+1+cpt] == NULL)) { + //-DEBUG-printf("ERREUR load_file lemmingsDATA: %s\n", filepath); + return(19); + } else { + ////-DEBUG-printf("load_file ok : %i -> '%s' + flip_LR\n", i+cpt, filepath); + } + } + free(filepath); + //0: lemmfont.gif + gInit->mapI.map.tabGif[0]= load_image("../../../misc/lemmfont.gif",gInit->mapI.map.cmap.bgColor); + if ( gInit->mapI.map.tabGif[0] == NULL ) { + //-DEBUG-printf("ERREUR load_file lemmfont.gif: %s\n", "../../../misc/lemmfont.gif"); + return(20); + } + //1: numfont.gif + gInit->mapI.map.tabGif[1]= load_image("../../../misc/numfont.gif",gInit->mapI.map.cmap.bgColor); + if ( gInit->mapI.map.tabGif[1] == NULL ) { + //-DEBUG-printf("ERREUR load_file numfont.gif: %s\n", "../../../misc/numfont.gif"); + return(21); + } + //2: countdown.gif + gInit->mapI.map.tabGif[2]= load_image("../../../misc/countdown.gif",ccc_lemming); + if ( gInit->mapI.map.tabGif[2] == NULL ) { + //-DEBUG-printf("ERREUR load_file countdown.gif: %s\n", "../../../misc/countdown.gif"); + return(22); + } + //3: cursor.gif + gInit->mapI.map.tabGif[3]= load_image("../../../misc/cursor.gif",ccc_cursor);//gInit->mapI.map.cmap.bgColor); + if ( gInit->mapI.map.tabGif[3] == NULL ) { + //-DEBUG-printf("ERREUR load_file cursor.gif: %s\n", "../../../misc/cursor.gif"); + return(23); + } + //4: explode.gif + gInit->mapI.map.tabGif[4]= load_image("../../../misc/explode.gif",gInit->mapI.map.cmap.bgColor); + if ( gInit->mapI.map.tabGif[4] == NULL ) { + //-DEBUG-printf("ERREUR load_file explode.gif: %s\n", "../../../misc/explode.gif"); + return(24); + } + //5: border.gif + gInit->mapI.map.tabGif[5]= load_image("../../../misc/border.gif",gInit->mapI.map.cmap.bgColor); + if ( gInit->mapI.map.tabGif[5] == NULL ) { + //-DEBUG-printf("ERREUR load_file border.gif: %s\n", "../../../misc/border.gif"); + return(25); + } + //6: replay.gif + gInit->mapI.map.tabGif[6]= load_image("../../../misc/replay.gif",gInit->mapI.map.cmap.bgColor); + if ( gInit->mapI.map.tabGif[6] == NULL ) { + //-DEBUG-printf("ERREUR load_file replay.gif: %s\n", "../../../misc/replay.gif"); + return(26); + } + //7: select.gif + gInit->mapI.map.tabGif[7]= load_image("../../../misc/select.gif",gInit->mapI.map.cmap.bgColor); + if ( gInit->mapI.map.tabGif[7] == NULL ) { + //-DEBUG-printf("ERREUR load_file select.gif: %s\n", "../../../misc/select.gif"); + return(27); + } + // 8: font7x10.bmp + gInit->mapI.map.tabGif[8]= load_image("./font7x10.bmp",gInit->mapI.map.cmap.bgColor); + if ( gInit->mapI.map.tabGif[8] == NULL ) { + //-DEBUG-printf("ERREUR load_file font7x10.bmp: %s\n", "./font7x10.bmp"); + return(28); + } + // 9: alphabet.gif + gInit->mapI.map.tabGif[9]= load_image("./alphabet.gif",ccc_cursor); + if ( gInit->mapI.map.tabGif[9] == NULL ) { + //-DEBUG-printf("ERREUR load_file alphabet.gif: %s\n", "./alphabet.gif"); + return(29); + } + //10: mask_10.gif + gInit->mapI.map.tabGif[10]= load_image("../../../misc/mask_10.gif",ccc_cursor); + if ( gInit->mapI.map.tabGif[10] == NULL ) { + //-DEBUG-printf("ERREUR load_file mask_10.gif: %s\n", "../../../misc/mask_10.gif"); + return(30); + } + //11: mask_11.gif + gInit->mapI.map.tabGif[11]= load_image("../../../misc/mask_11.gif",ccc_cursor); + if ( gInit->mapI.map.tabGif[11] == NULL ) { + //-DEBUG-printf("ERREUR load_file mask_11.gif: %s\n", "../../../misc/mask_11.gif"); + return(31); + } + //12: mask_6.gif + gInit->mapI.map.tabGif[12]= load_image("../../../misc/mask_6.gif",ccc_cursor); + if ( gInit->mapI.map.tabGif[12] == NULL ) { + //-DEBUG-printf("ERREUR load_file mask_6.gif: %s\n", "../../../misc/mask_6.gif"); + return(32); + } + //13: mask_13.gif + gInit->mapI.map.tabGif[13]= load_image("../../../misc/mask_13.gif",ccc_cursor); + if ( gInit->mapI.map.tabGif[13] == NULL ) { + //-DEBUG-printf("ERREUR load_file mask_13.gif: %s\n", "../../../misc/mask_13.gif"); + return(33); + } + //14: mask_14.gif + gInit->mapI.map.tabGif[14]= load_image("../../../misc/mask_14.gif",ccc_cursor); + if ( gInit->mapI.map.tabGif[14] == NULL ) { + //-DEBUG-printf("ERREUR load_file mask_14.gif: %s\n", "../../../misc/mask_14.gif"); + return(34); + } + //15: mask_15.gif + gInit->mapI.map.tabGif[15]= load_image("../../../misc/mask_15.gif",ccc_cursor); + if ( gInit->mapI.map.tabGif[15] == NULL ) { + //-DEBUG-printf("ERREUR load_file mask_15.gif: %s\n", "../../../misc/mask_15.gif"); + return(35); + } + //16: imask_13.gif + gInit->mapI.map.tabGif[16]= load_image("../../../misc/imask_13.gif",ccc_cursor); + if ( gInit->mapI.map.tabGif[16] == NULL ) { + //-DEBUG-printf("ERREUR load_file imask_13.gif: %s\n", "../../../misc/imask_13.gif"); + return(36); + } + //17: imask_14.gif + gInit->mapI.map.tabGif[17]= load_image("../../../misc/imask_14.gif",ccc_cursor); + if ( gInit->mapI.map.tabGif[17] == NULL ) { + //-DEBUG-printf("ERREUR load_file imask_14.gif: %s\n", "../../../misc/imask_14.gif"); + return(37); + } + //18: imask_15.gif + gInit->mapI.map.tabGif[18]= load_image("../../../misc/imask_15.gif",ccc_cursor); + if ( gInit->mapI.map.tabGif[18] == NULL ) { + //-DEBUG-printf("ERREUR load_file imask_15.gif: %s\n", "../../../misc/imask_15.gif"); + return(38); + } + + //icone.gif + cpt=NBR_ADD_OBJ; + filepath = malloc(sizeof(char)*(strlen(folderLem)+strlen("icon")+strlen("/__alt.gif")+(sizeof(int)*10))); + + for(i=0; i < NBR_BUTTON_LEMMING ; ++i) { + sprintf(filepath, "%s/%s%d.gif", folderLem,"icon_",i); + //if(i<7){sprintf(filepath, "%s/%s%d.gif", folderLem,"icon_",i);} + //if(i==7){sprintf(filepath, "%s/%s.gif", folderLem,"icon_6_alt");} + //if(i>7){sprintf(filepath, "%s/%s%d.gif", folderLem,"icon_",i-1);} + gInit->mapI.map.tabGif[i+cpt] = load_image(filepath,ccc_black); + if(gInit->mapI.map.tabGif[i+cpt] == NULL) { + //-DEBUG-printf("ERREUR load_file icon: %s\n", filepath); + return(31); + } else { + ////-DEBUG-printf("load_file ok : %i -> '%s' + flip_LR\n", i+cpt, filepath); + } + } + free(filepath); + free(folderLem); + + return 0; +} + + +int load_files(struct gameInit *gInit) +{ + int lt,ltt,ltlem; + int err=0; + int res, lterrain, lobjet, lsteel; + char *folder; + char *spriteINI; + char *lemINI; + char *terrainGif; + char *objetGif; + char *steelGif; + char *temp; + int tabNum[255]; + struct dirent *lecture; + DIR *rep; + + yyparse(gInit); + + fclose(yyin); + //-DEBUG-printf("CLOSE 1\n"); + + + lt = (strlen("../../../styles/")); + ltlem = (strlen("../../../misc/lemming.ini")); + ltt = (strlen(tabString_eMapStyle[gInit->mapI.map.style])); + + folder = malloc(sizeof(char)*(lt+ltt+1)); + spriteINI = malloc(sizeof(char)*(lt+ltt*2+5));// *2:dossier/fichier, +4: .ini, +1: string + lemINI = malloc(sizeof(char)*ltlem+1); + + + //folder = "/home/jazzblue/Bureau/Projet-Lemmings/trunk/styles/dirt"; + ////-DEBUG-printf("test segFault\n"); + sprintf(folder,"../../../styles/%s",tabString_eMapStyle[gInit->mapI.map.style]); + sprintf(spriteINI,"%s/%s.ini",folder,tabString_eMapStyle[gInit->mapI.map.style]); + sprintf(lemINI,"%s","../../../misc/lemming.ini"); + ////-DEBUG-printf("test2\n"); + + temp = tabString_eMapStyle[gInit->mapI.map.style]; + lt = strlen(temp); + lterrain = lt+1; + lobjet = lt+2; + lsteel = lt+3; + terrainGif=malloc(sizeof(char)*(lterrain+1)); + objetGif=malloc(sizeof(char)*(lobjet+1)); + steelGif=malloc(sizeof(char)*(lsteel+1)); + sprintf(terrainGif,"%s_",temp); + sprintf(objetGif,"%so_",temp); + sprintf(steelGif,"%som_",temp); + + // Compter les textures de terrain + rep = opendir(folder); + if (rep==NULL) { + //-DEBUG-printf("Erreur opendir('%s')\n", folder); + return(31); + } + + while ((lecture = readdir(rep))) { + if( (res=strncmp(terrainGif, lecture->d_name,lterrain)) == 0 ){ + ++(gInit->mapI.map.tiles); + } else { + if( (res=strncmp(objetGif, lecture->d_name,lobjet)) == 0 ) { + ++(gInit->mapI.map.tilesObjet); + } else { + if ( (res=strncmp(steelGif, lecture->d_name,lsteel)) == 0 ) { + sscanf(lecture->d_name+lsteel, "%i", &(tabNum[gInit->mapI.map.tilesSteel])); + ++(gInit->mapI.map.tilesSteel); + + } else { + //-DEBUG-printf("BUG : type de fichier non reconnu [%s] \n", lecture->d_name); + } + } + } + } + closedir(rep); + + + + yyin=fopen(spriteINI, "r"); + if (yyin==NULL) { fprintf(stderr,"Filename INVALIDE: %s\n",spriteINI); + perror("Impossible d'ouvrir le fichier de configuration des sprite"); return(32); + } + yyparse(gInit); + fclose(yyin); + //-DEBUG-printf("CLOSE 2\n"); + + yyin=fopen(lemINI, "r"); + if (yyin==NULL) { fprintf(stderr,"Filename INVALIDE: %s\n",lemINI); + perror("Impossible d'ouvrir le fichier de configuration des lemmings"); return(33); + } + yyparse(gInit); + fclose(yyin); + //-DEBUG-printf("CLOSE 3\n"); + + err=load_fields(folder, gInit, tabNum); + if(err!=0){return err;} + + ////-DEBUG-printf("REVERSE t.lt\n"); + gInit->t.lt=(struct listeSimplementChainee*)rev_listeO(gInit->t.lt); + ////-DEBUG-printf("REVERSE o.lo\n"); + gInit->o.lo=(struct listeSimplementChainee*)rev_listeO(gInit->o.lo); + ////-DEBUG-printf("REVERSE s.ls\n"); + gInit->s.ls=(struct listeSimplementChainee*)rev_listeO(gInit->s.ls); + + // AFFICHAGE DES INFOS DE LA MAP + err=afficher(gInit); + if(err!=0){return err;} + + //Si tout s'est bien passé + free(terrainGif); + free(objetGif); + free(steelGif); + free(folder); + free(spriteINI); + free(lemINI); + + return 0; +} + +int afficher(struct gameInit *gInit){ + + int i; + struct listeSimplementChainee *k; + + printf("AFFICHAGE DE GAME INIT\n"); + printf("LES PARAMETRES:\n"); + for(i=0;i<IDENT_COUNT;++i){ + printf("\t%s = %d\n",tabString_eParaMap[i],gInit->mapI.paraMap[i]); + } + printf("\txPos = %d\n",gInit->mapI.xPos); + printf("\tstyle = %s\n",tabString_eMapStyle[gInit->mapI.map.style]); + + printf("LE NOM DU NIVEAU:\n"); + printf("name = %s\n",gInit->mapI.name); + printf("LES COULEURS DU NIVEAU:\n"); + printf("bgColor R = %X G = %X B = %X\n", + ((gInit->mapI.map.cmap.bgColor>>16) & 0xff), + ((gInit->mapI.map.cmap.bgColor>>8) & 0xff), + (gInit->mapI.map.cmap.bgColor & 0xff) ); + printf("debrisColor R = %X G = %X B = %X\n", + ((gInit->mapI.map.cmap.debrisColor>>16) & 0xff), + ((gInit->mapI.map.cmap.debrisColor>>8) & 0xff), + (gInit->mapI.map.cmap.debrisColor & 0xff) ); + printf("debrisColor:\n"); + for(i=0;i<PARTICULE_COUNT;++i){ + printf("\tP%d: R = %X G = %X B = %X\n",i, + ((gInit->mapI.map.cmap.particleColor[i]>>16) & 0xff), + ((gInit->mapI.map.cmap.particleColor[i]>>8) & 0xff), + (gInit->mapI.map.cmap.particleColor[i] & 0xff) ); + + } + + printf("TERRAINS:\n"); + k=gInit->t.lt; + i=0; + while ( k !=NULL ) { + if(k->data.pt != NULL){//{printf("paraTerrain non initialiser\n");return(34);} + printf("\tterrain_%d = %d, %d, %d, %d\n",i,k->ID, k->x, k->y,k->data.pt->modif); + } + ++i; + k=k->next; + } + + printf("OBJETS:\n"); + + k=gInit->o.lo; + i=0; + while ( k !=NULL ) { + if(k->data.po != NULL){ //{printf("paraObjet non initialiser\n");return(35);} + printf("\tobject_%d = %d, %d, %d, %d, %d\n",i,k->ID, k->x, k->y, k->data.po->paintMode,k->data.po->UD); + } + ++i; + k=k->next; + } + + + printf("STEELS:\n"); + k=gInit->s.ls; + i=0; + while ( k !=NULL ) { + if(k->data.ps != NULL){//{printf("paraSteel non initialiser\n");return(36);} + printf("\tsteel_%d = %d, %d, %d, %d, %d\n",i,k->ID=i, k->x, k->y, k->data.ps->w,k->data.ps->h); // FIXME big fuck here with "=" + } + ++i; + k=k->next; + } + + printf("DATA SPRITES OBJET:\n"); + for(i=0;i<gInit->mapI.map.tilesObjet;++i){ + printf("spr_%d:\n\tframes_%d = %d\n\tanim_%d = %d\n\ttype_%d = %d\n\tsound_%d = %d\n",i,i,gInit->mapI.map.tabDataSprO[i].state,i,gInit->mapI.map.tabDataSprO[i].anim,i,gInit->mapI.map.tabDataSprO[i].type,i,gInit->mapI.map.tabDataSprO[i].sound); + } + + printf("DATA JOB LEMMINGS:\n"); + for(i=0;i<LEM_JOB;++i){ + printf("lem_%d:\n\tlemm_%d = %d, %d, %d\n\tpos_%d = %d, %d, %d\n",i,i,gInit->mapI.lemmingDATA[i].state,gInit->mapI.lemmingDATA[i].dir,gInit->mapI.lemmingDATA[i].anim,i,gInit->mapI.lemmingDATA[i].footX,gInit->mapI.lemmingDATA[i].footY,gInit->mapI.lemmingDATA[i].footSize); + + if (gInit->mapI.lemmingDATA[i].mask != NULL){ + printf("\tmask_%d = %d, %d, %d\n",i,gInit->mapI.lemmingDATA[i].mask->stateM,gInit->mapI.lemmingDATA[i].mask->dirM,gInit->mapI.lemmingDATA[i].mask->cooldown); + } + + if (gInit->mapI.lemmingDATA[i].imask != NULL){ + printf("\timask_%d = %d , %d\n",i,gInit->mapI.lemmingDATA[i].imask->stateI,gInit->mapI.lemmingDATA[i].imask->dirI); + } + } + return 0; +} + +int clean_up() +{ + //On libère la feuille de sprites + // SDL_FreeSurface( faces ); + SDL_FreeSurface( pTerrain ); + //On quitte SDL + SDL_Quit(); + + return 0; +} + +int initGame(struct gameInit *gInit){ + + int i; + + // init liste + gInit->t.nbr=0; + gInit->o.nbr=0; + gInit->s.nbr=0; + gInit->l.nbrInput=0; + gInit->l.nbr=0; + gInit->l.nbrDied=0; + gInit->l.nbrSafe=0; + + gInit->t.lt=NULL; + gInit->o.lo=NULL; + gInit->s.ls=NULL; + gInit->l.ll=NULL; + + // init button state + for(i=0;i<NBR_CPT;++i){ + gInit->cptGame[i]=0; + } + gInit->cptGame[17]=404; + + // init InfoMap + // map name + gInit->mapI.name = NULL; + + // paraMap + for(i=0;i<IDENT_COUNT;++i){ + gInit->mapI.paraMap[i]=0; + } + // xPos + gInit->mapI.xPos=0; + + //MaxFallDistance + gInit->mapI.nbrEntry=0; + gInit->mapI.MaxFallDistance = 126; + gInit->mapI.nuke=0; + + // init MapStyle + gInit->mapI.map.style=undefined; + gInit->mapI.map.superLemming=0; + gInit->mapI.map.tiles=0; + gInit->mapI.map.tilesObjet=0; + gInit->mapI.map.tilesSteel=0; + gInit->mapI.map.tabDataSprO=NULL; + gInit->mapI.map.tabGif=NULL; + + // lemmingJobAndSprite + gInit->mapI.lemmingDATA=NULL; + + return 0; +} + +int test_O_UD(int UD){ + + switch(UD){ + case 4 : return 1;break; + case 6 : return 1;break; + case 12 : return 1;break; + case 14 : return 1;break; + default: return 0;break; + } + return 0; +} + +int creationLemming(struct gameInit *gInit){ + int nbLem; + struct listeSimplementChainee *k=NULL; + struct listeSimplementChainee *kTemp=NULL; + SDL_Surface *sf=NULL; + + int coorE[10][2]; + int i=0; + //DEBUT TEST CREATION UN LEMMING + + // DEBUT DEBUG: LEMMING COLLISION + //gInit->mapI.paraMap[1]=2; + // FIN DEBUG: LEMMING COLLISION + + nbLem=gInit->mapI.paraMap[1]; + //nbLem=1; + ////-DEBUG-printf("nbLem %d\n",nbLem); + + if(gInit->mapI.nbrEntry==0){ + //-DEBUG-printf("ERREUR ALGO: nbrEntry == 0\n"); + return(37); + } + + + // PREMIER CAS + gInit->l.nbrInput=1; + gInit->l.ll = malloc(sizeof(struct listeSimplementChainee)); + gInit->l.ll->ID=18; + + k=gInit->o.lo; + while ( k !=NULL ) { + if( k->ID == 1 ){// Entry detecte + sf = gInit->mapI.map.tabGif[gInit->mapI.map.tiles*2+k->ID*2+k->data.po->UD+ADD_OBJ]; + coorE[i][0]=k->x+(sf->w/2)-16; + coorE[i][1]=k->y; + ++i; + if(i>=gInit->mapI.nbrEntry){break;} + if(i>9){//-DEBUG-printf("ERREUR: too many entry\n"); + return(38);} + } + k=k->next; + } + if(k == NULL){ + //-DEBUG-printf("BUG: NO ENTRY IN THIS LEVEL ...\n"); + return(39); + } + gInit->l.ll->x=coorE[0][0]; + gInit->l.ll->y=coorE[0][1]; + + gInit->l.ll->data.pt=NULL; + gInit->l.ll->data.po=NULL; + gInit->l.ll->data.ps=NULL; + gInit->l.ll->data.pl=malloc(sizeof(struct paraLemming)); + + gInit->l.ll->data.pl->dir=1; + gInit->l.ll->data.pl->climber=0; + gInit->l.ll->data.pl->blocker=0; + gInit->l.ll->data.pl->bomber=404; + gInit->l.ll->data.pl->floater=0; + gInit->l.ll->data.pl->cptFall=0; + gInit->l.ll->data.pl->cptJump=0; + gInit->l.ll->data.pl->nuke=0; + gInit->l.ll->data.pl->cptState=0; + gInit->l.ll->data.pl->cptBoom=404; + gInit->l.ll->data.pl->cptFall2=0; + gInit->l.ll->data.pl->cptBrick=0; + + gInit->l.ll->next=NULL; + if(gInit->mapI.nbrEntry==1){i=0;} + else {i=1;} + while(--nbLem > 0){ + // TOUS LES AUTRES CAS + + kTemp=malloc(sizeof(struct listeSimplementChainee)); + kTemp=gInit->l.ll; + + gInit->l.ll=NULL; + gInit->l.ll=malloc(sizeof(struct listeSimplementChainee)); + + gInit->l.ll->next=NULL; + gInit->l.ll->next=malloc(sizeof(struct listeSimplementChainee)); + + gInit->l.ll->next=kTemp; + + gInit->l.nbrInput += 1; + gInit->l.ll->ID=18; + + gInit->l.ll->x=coorE[i%gInit->mapI.nbrEntry][0]; + gInit->l.ll->y=coorE[i%gInit->mapI.nbrEntry][1]; + + gInit->l.ll->data.pl=malloc(sizeof(struct paraLemming)); + gInit->l.ll->data.pl->dir=1; + gInit->l.ll->data.pl->climber=0; + gInit->l.ll->data.pl->blocker=0; + gInit->l.ll->data.pl->bomber=404; + gInit->l.ll->data.pl->floater=0; + gInit->l.ll->data.pl->cptFall=0; + gInit->l.ll->data.pl->cptJump=0; + gInit->l.ll->data.pl->nuke=0; + gInit->l.ll->data.pl->cptState=0; + gInit->l.ll->data.pl->cptBoom=404; + gInit->l.ll->data.pl->cptFall2=0; + gInit->l.ll->data.pl->cptBrick=0; + if(gInit->mapI.nbrEntry==1){i=0;} + else {++i;} + } + //FIN TEST CREATION UN LEMMING + + ////-DEBUG-printf("REVERSE l.ll\n"); + gInit->l.ll=(struct listeSimplementChainee*)rev_listeO(gInit->l.ll); + + return 0; +} + +int paint_terrain (struct gameInit *gInit, int choix){ + struct listeSimplementChainee *k=NULL; + SDL_Rect offset; + SDL_Surface *sf; + int err=0; + int overwrite=0; + int remove=0; + int paint=0; + int tx,ty=0; + int cpt=0; + int ok=0; + Uint32 color; + + // //-DEBUG-printf("LES TERRAINS : "); + k=gInit->t.lt; + while ( k !=NULL ) { + offset.x = k->x; + offset.y = k->y; + + sf = gInit->mapI.map.tabGif[test_O_UD(k->data.pt->modif)+k->ID*2+ADD_OBJ]; + + if((k->data.pt->modif != 0)&& + (k->data.pt->modif != 2)&& + (k->data.pt->modif != 4)&& + (k->data.pt->modif != 6)&& + (k->data.pt->modif != 8)&& + (k->data.pt->modif != 10)&& + (k->data.pt->modif != 12)&& + (k->data.pt->modif != 14)&& + (k->data.pt->modif != 15) + ) { + //-DEBUG-printf("WTF ... k->data.pt->modif == %d\n",k->data.pt->modif); + return(40); + } + for(ty=k->y;ty<sf->h+k->y;++ty){ + + if(ty<0) {ty=-1;continue;} + if(ty>=pTerrain->h){break;} + if(ty>=LEVEL_HEIGHT) {break;} + + for(tx=k->x;tx<sf->w+k->x;++tx){ + + if(tx<0) {tx=-1;continue;} + if(tx>=pTerrain->w){break;} + if(tx>=LEVEL_WIDTH) {break;} + + overwrite = ((k->data.pt->modif & 8) == 8)? 0 : 1; + remove = ((k->data.pt->modif & 2) == 2)? 1 : 0; + if(k->data.pt->modif == 15){ + overwrite=0; + remove=0; + } + + + color = get_pixel32(tx-k->x,ty-k->y,sf); + if(color==ccc_error){return 250;} + if (color == ccc_black) + { //putPixel(pStencil,tx,ty , ccc_tEmpty ); + continue; + } + paint=0; + if(!overwrite){ + if((get_pixel32(tx,ty,pStencil)==ccc_error)){return 67;} + // don't overwrite -> only paint if background is transparent + if(((get_pixel32(tx,ty,pStencil)>>24) & 0xff ) == 0) + {// ccc_tEmpty){ + paint=1; + } + } else if(remove){ + // if(((get_pixel32(tx,ty,pStencil)) & ccc_tFull) != 0){ + err=putPixel(pTerrain,tx,ty , gInit->mapI.map.cmap.bgColor); + if(err!=0){return err;} + err=putPixel(pStencil,tx,ty , ccc_tRemove );//ccc_tEmpty); + if(err!=0){return err;} + // } + + } else { + paint=1; + } + + if(paint==1){ + if(!overwrite){ + if(!remove){ + err=putPixel(pTerrain,tx,ty ,color); + if(err!=0){return err;} + if(k->data.pt->modif==15){ + err=putPixel(pStencil,tx,ty , ccc_tHidden );//ccc_tBrick); + if(err!=0){return err;} + } else { + err=putPixel(pStencil,tx,ty , ccc_tNoOverwrite );//ccc_tBrick); + if(err!=0){return err;} + } + } else { + err=putPixel(pTerrain,tx,ty ,color); + if(err!=0){return err;} + err=putPixel(pStencil,tx,ty , ccc_tRemoveNoOverwrite );//ccc_tBrick); + if(err!=0){return err;} + } + } else { + err=putPixel(pTerrain,tx,ty ,color); + if(err!=0){return err;} + err=putPixel(pStencil,tx,ty , ccc_tFull );//ccc_tBrick); + if(err!=0){return err;} + } + } + }//fin tx + }//fin ty + k=k->next; + cpt++; + ok=0; + //test_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x0, 0x0, 0x0 ) ); test_blit(pTerrain, &camera, screen, NULL); if( SDL_Flip( screen ) == -1 ) { return 1;} //scanf("%d",&pas); + } + // elimination des pixel remove + for(ty=0;ty<LEVEL_HEIGHT;++ty){ + for(tx=0;tx<LEVEL_WIDTH;++tx){ + color = get_pixel32(tx,ty,pStencil); + if(color==ccc_tRemove){ + err=putPixel(pStencil,tx,ty , ccc_bgStencil ); + if(err!=0){return err;} + } else { + if(color!=ccc_bgStencil){ + err=putPixel(pStencil,tx,ty , ccc_tBrick ); + if(err!=0){return err;} + } + } + } + } + + + // //-DEBUG-printf("LES STEEL : "); + k=gInit->s.ls; + while ( k !=NULL ) { + offset.x = k->x; + offset.y = k->y; + + sf = gInit->mapI.map.tabGif[ADD_OBJ+(gInit->mapI.map.tiles*2)+(gInit->mapI.map.tilesObjet*2)+k->ID]; + + offset.w = k->data.ps->w; + offset.h = k->data.ps->h; + ////-DEBUG-printf("H=)> %d\n",offset.h); + + for(ty=k->y;ty<offset.h+k->y;++ty){ + + if(ty<0) {ty=-1;continue;} + if(ty>pTerrain->h){break;} + if(ty>=LEVEL_HEIGHT) {break;} + + for(tx=k->x;tx<offset.w+k->x;++tx){ + + if(tx<0) {tx=-1;continue;} + if(tx>pTerrain->w){break;} + if(tx>=LEVEL_WIDTH) {break;} + + color = get_pixel32(tx,ty,pStencil); + if(color==ccc_error){return 219;} + if (color == ccc_black){ + continue; + } else { + err=putPixel(pStencil,tx,ty , ccc_s ); + if(err!=0){return err;} + } + } + } + // test_FillRect(pStencil, &offset, ccc_s); + k=k->next; + } + //} + + return 0; +} + + +int miniMap (SDL_Surface *s, int x0, int y0, float coefX, float coefY){ + + + int i,j,stepScreenX,stepScreenY,sizeMiniMapPixel_X,sizeMiniMapPixel_Y,w0,h0,x,y; + SDL_Rect rect; + Uint32 err; + //int cpt=0; + + x=x0; + y=y0; + + if((x0 < 0)|| + (y0 < 0)|| + (coefX < 0.)|| + (coefY < 0.)|| + (s->pixels == NULL) + ){ + //-DEBUG-printf("Bug Error x0 %d y0 %d coefX %f coefY %f s->pixels %p \n",x0,y0,coefX,coefY,s->pixels); + return 189; + } + + ////-DEBUG-printf("x0 %d y0 %d w0 %d h0 %d coefX %f coefY %f s->pixels %p \n",x0,y0,w0,h0,coefX,coefY,s->pixels); + + // if coef >= 1 then zoom++ else zoom--; + stepScreenX=((coefX>=1.)? 1 : ((int)(1./coefX))); + stepScreenY=((coefY>=1.)? 1 : ((int)(1./coefY))); + + sizeMiniMapPixel_X=((coefX>=1.)? (int)coefX : 1); + sizeMiniMapPixel_Y=((coefY>=1.)? (int)coefY : 1); + + /*if(stepX==0){stepX=1;} + if(stepY==0){stepY=1;} + if(sizeX==0){sizeX=1;} + if(sizeY==0){sizeY=1;} + */ + + rect.w=sizeMiniMapPixel_X; + rect.h=sizeMiniMapPixel_Y; + + ////-DEBUG-printf("sx %d sy %d w %d h %d \n",stepX,stepY,sizeX,sizeY); + ////-DEBUG-printf("h0 %d -> stepY %d, w0 %d -> stepX %d x0 %d y0 %d \n",stepX,stepY,sizeX,sizeY); + + w0=((coefX>=1.)? MINIMAP_MX/sizeMiniMapPixel_X : MINIMAP_MX*stepScreenX); + h0=((coefY>=1.)? MINIMAP_MY/sizeMiniMapPixel_Y : MINIMAP_MY*stepScreenY); + + //centrage camera, la position du curseur est placé au centre de la minimap + x0-= (w0/2 - 15); + y0-= (h0/2 - 10); + + if(x0<0){x0=0;} + if(y0<0){y0=0;} + + //centrage camera + ////-DEBUG-printf("x0 %d w0/2 %d\n",x0,w0/2); + // x0+=((x0+w0/3)>LEVEL_WIDTH)? 0 : ((coefX>=1.)? ((-w0/3)/(int)coefX) : (int)(((float)(-w0/3))/coefX) ); + + + if(w0 + x0 >LEVEL_WIDTH){ + if(coefX>1.) {w0=LEVEL_WIDTH-x0;} + else {x0=LEVEL_HEIGHT-w0;} + } + if(h0 + y0 > LEVEL_HEIGHT){ + if(coefY>1.) {h0=LEVEL_HEIGHT-y0;} + else {y0=LEVEL_HEIGHT-h0;} + } + + if((w0 + x0 >LEVEL_WIDTH)||(h0 + y0 > LEVEL_HEIGHT)){return 0;} + + for(j=0;j<h0;j+=stepScreenY){ + if((y0+j)<0){continue;} + rect.y=MINIMAP_Y0+(j/stepScreenY)*sizeMiniMapPixel_Y; + if(rect.y>MINIMAP_Y0+MINIMAP_MY){break;} + if(rect.h+rect.y>MINIMAP_Y0+MINIMAP_MY){break;} + + for(i=0;i<w0;i+=stepScreenX){ + //cpt++; + + if((x0+i)<0){continue;} + rect.x=MINIMAP_X0+(i/stepScreenX)*sizeMiniMapPixel_X; + if(rect.x>MINIMAP_X0+MINIMAP_MX){break;} + if(rect.w+rect.x>MINIMAP_X0+MINIMAP_MX){break;} + + err=get_pixel32(x0+i,y0+j,s); + if(err==ccc_error){return 123;} + + test_FillRect(screen, &rect, err); + + } + } + + //printf("cpt = %d\n",cpt); + return 0; +} + +int test_FillRect( SDL_Surface *t, SDL_Rect *to, Uint32 ccc){ + + SDL_FillRect(t, to, ccc); + return 0; +} + +int test_blit(SDL_Surface *spr,SDL_Rect *fr1,SDL_Surface *t,SDL_Rect *to1){ + + /* int size; + void *temp_pixels = NULL; + + + size=spr->h*spr->pitch; + temp_pixels=malloc(size); + + SDL_LockSurface(spr); + SDL_LockSurface(t); + memcpy(temp_pixels,spr->pixels,size); + + + SDL_UnlockSurface(t); + SDL_UnlockSurface(spr);*/ + SDL_BlitSurface(spr,fr1, t,to1); + /* SDL_LockSurface(spr); + memcpy(spr->pixels,temp_pixels,size); + SDL_UnlockSurface(spr); + + free(temp_pixels);*/ + + return 0; + // return SDL_BlitSurface(spr,fr1, t,to1); + +} + +int paint_manip(SDL_Surface *spr,SDL_Rect from,SDL_Surface *t,SDL_Rect to,Uint32 ccc_t, Uint32 ccc_spr, int mode){ + + int size,i,j,err; + void *temp_pixels = NULL; + Uint32 ccc_temp=0x00000000; + Uint32 ccc_temp2=0x00000000; + //FULL + if(mode==0){ + err=test_blit(spr,&from, t,&to); + return err; + } + + ////-DEBUG-printf("paint_manip(), from.x==%4i, from.y==%4i, from.h==%4i, from.w==%4i, from to.x==%4i, to.y==%4i\n", from.x, from.y, from.h, from.w, to.x, to.y); + + if(from.y<0 || from.y > spr->h) { + //-DEBUG-printf("simple y\n"); + return 345; + } + + if(from.x<0 || from.x > spr->w) { + //-DEBUG-printf("simple x\n"); + return 346; + } + + if(to.y<0 || to.y > t->h) { + //-DEBUG-printf("simple y2\n"); + return 91; + } + + if(to.x<0 || to.x > t->w) { + //-DEBUG-printf("simple x2\n"); + return 92; + } + + if(from.y+from.h > spr->h) { + from.h=spr->h - from.y; + } + + if(from.x+from.w > spr->w) { + from.w=spr->w - from.x; + } + + if(to.y+from.h > t->h) { + from.h=t->h - to.y; + } + + if(to.x + from.w > t->w) { + from.w=t->w - to.x; + } + + size=spr->h*spr->pitch; + temp_pixels=malloc(size); + + SDL_LockSurface(spr); + SDL_LockSurface(t); + memcpy(temp_pixels,spr->pixels,size); + + // NO OVERWRITE + if(mode==4){ + + for(j=0;j<from.h;j++) { + for(i=0;i<from.w;i++) { + /* if((to.y+j)*t->w+(to.x+i)>=(t->w*t->h)){ + //-DEBUG-printf("out of bound1 x=%d + i=%d, y=%d + j=%d * t->w %d == %d >= %d (t->h %d, from.h %d) \n",to.x,i,to.y,j,t->w,(to.y+j)*t->w+(to.x+i),(t->w*t->h),t->h,from.h); + return 56; + //continue; + }*/ + ccc_temp=((Uint32 *)t->pixels)[(to.y+j)*t->w+(to.x+i)]; + ////-DEBUG-printf("ccc_temp1 = %p == %p t->format->colorkey\n",ccc_temp,t->format->colorkey); + if (ccc_temp!=ccc_t) { + ((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i]=ccc_temp;//spr->format->colorkey; + } + } + } + } + // VIS ON TERRAIN + if(mode==8){ + for(j=0;j<from.h;j++) { + for(i=0;i<from.w;i++) { + /* if((to.y+j)*t->w+(to.x+i)>=(t->w*t->h)){ + //-DEBUG-printf("out of bound2 x=%d + i=%d, y=%d + j=%d * t->w %d == %d >= %d (t->h %d, from.h %d) \n",to.x,i,to.y,j,t->w,(to.y+j)*t->w+(to.x+i),(t->w*t->h),t->h,from.h); + return 57; + //continue; + }*/ + ccc_temp=((Uint32 *)t->pixels)[(to.y+j)*t->w+(to.x+i)]; + ////-DEBUG-printf("ccc_temp1 = %p == %p ccc_t\n",ccc_temp,ccc_t); + if (ccc_temp==ccc_t) { + ((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i]=ccc_t;//spr->format->colorkey; + + } + } + } + } + // erease pixel + if(mode==18){ + for(j=0;j<from.h;j++) { + for(i=0;i<from.w;i++) { + /* if((to.y+j)*t->w+(to.x+i)>=(t->w*t->h)){ + //-DEBUG-printf("out of bound2 x=%d + i=%d, y=%d + j=%d * t->w %d == %d >= %d (t->h %d, from.h %d) \n",to.x,i,to.y,j,t->w,(to.y+j)*t->w+(to.x+i),(t->w*t->h),t->h,from.h); + return 57; + //continue; + }*/ + ccc_temp=((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i];//((Uint32 *)t->pixels)[(to.y+j)*t->w+(to.x+i)]; + ccc_temp2=((Uint32 *)t->pixels)[(to.y+j)*t->w+(to.x+i)]; + ////-DEBUG-printf("ccc_temp1 = %p == %p ccc_t\n",ccc_temp,ccc_t); + if (ccc_temp!=ccc_spr) { + ((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i]=ccc_t;//spr->format->colorkey; + } else { + ((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i]=ccc_temp2; + } + } + } + } + + SDL_UnlockSurface(t); + SDL_UnlockSurface(spr); + test_blit(spr,&from, t,&to); + + SDL_LockSurface(spr); + memcpy(spr->pixels,temp_pixels,size); + SDL_UnlockSurface(spr); + + free(temp_pixels); + + return 0; + +} + +int paint_objet (struct gameInit *gInit, int cptFps){ + SDL_Rect offset,objetAnim,rStencil; + SDL_Surface *sf; + struct listeSimplementChainee *k=NULL; + int err=0; + int type=0; + //int ox,oy; + int mode=0; + //Uint32 color,color2,color3; + //int size; + //void *temp_pixels = NULL; + + //DEBUT TEST: SPR + k=gInit->o.lo; + while ( k !=NULL ) { + + sf = gInit->mapI.map.tabGif[gInit->mapI.map.tiles*2+k->ID*2+k->data.po->UD+ADD_OBJ]; + + type=gInit->mapI.map.tabDataSprO[k->ID].type; + + offset.x = k->x; + offset.y = k->y; + + objetAnim.x = 0; + objetAnim.h = sf->h/gInit->mapI.map.tabDataSprO[k->ID].state; + objetAnim.y = (anim_objet( + gInit->mapI.map.tabDataSprO[k->ID].anim, + type, + cptFps, + gInit->mapI.map.tabDataSprO[k->ID].state, + &k->data.po->cptState, + gInit) + )*(objetAnim.h); + objetAnim.w = sf->w; + + rStencil.x=k->x ; + rStencil.y=k->y ; + rStencil.h=objetAnim.h; + rStencil.w=objetAnim.w; + + mode=k->data.po->paintMode; + + // check combi impossible + if((mode!=8)&&((type==3)||(type==4))){ + //-DEBUG-printf("Construction impossible: mode!=%d(8) && type=%d(3 ou 4)\n",mode,type); + return 56; + } + if((mode==8)&&(type==5)){ + //-DEBUG-printf("Construction impossible: mode==%d(8) && type=%d(5)\n",mode,type); + return 57; + } + // fin check + rStencil.x=0 ; + rStencil.y=objetAnim.y ; + if((err=paint_manip(sf,rStencil,pSpr_Lem,offset,gInit->mapI.map.cmap.bgColor,ccc_keyGif,mode))!=0){ + return err; + } + k=k->next; + } + ///FIN TEST: SPR + return 0; +} + +int paint_objet_stencil (struct gameInit *gInit, int cptFps){ + SDL_Rect offset,objetAnim,rStencil; + SDL_Surface *sf; + struct listeSimplementChainee *k=NULL; + //int res; + int err=0; + int type=0; + int ox,oy; + int mode=0; + Uint32 color,color2;//,color3; + //int size; + //void *temp_pixels = NULL; + //DEBUT TEST: SPR + k=gInit->o.lo; + while ( k !=NULL ) { + + sf = gInit->mapI.map.tabGif[gInit->mapI.map.tiles*2+k->ID*2+k->data.po->UD+ADD_OBJ]; + + type=gInit->mapI.map.tabDataSprO[k->ID].type; + + + offset.x = k->x; + offset.y = k->y; + + objetAnim.x = 0; + objetAnim.h = sf->h/gInit->mapI.map.tabDataSprO[k->ID].state; + objetAnim.y = (anim_objet( + gInit->mapI.map.tabDataSprO[k->ID].anim, + type, + cptFps, + gInit->mapI.map.tabDataSprO[k->ID].state, + &k->data.po->cptState, + gInit) + )*(objetAnim.h); + objetAnim.w = sf->w; + + if(type==6||type==8){ + rStencil.w=objetAnim.w/3; + rStencil.x=k->x+objetAnim.w/2-rStencil.w/2 ; + rStencil.y=k->y ; + rStencil.h=objetAnim.h; + } else { + rStencil.x=k->x; + rStencil.y=k->y; + rStencil.w=objetAnim.w; + rStencil.h=objetAnim.h; + } + + mode=k->data.po->paintMode; + + // check combi impossible + if((mode!=8)&&((type==3)||(type==4))){ + //-DEBUG-printf("Construction impossible: mode!=%d(8) && type=%d(3 ou 4)\n",mode,type); + return 56; + } + if((mode==8)&&(type==5)){ + //-DEBUG-printf("Construction impossible: mode==%d(8) && type=%d(5)\n",mode,type); + return 57; + } + // fin check + if(type!=4&&type!=3){ + if(type==5){ + rStencil.y=k->y+(rStencil.h/6); + //rStencil.h=(rStencil.h/6 + (rStencil.h/6)%6)*5; + rStencil.h=4; + } + switch(type){ + case 0://passive + test_FillRect(pStencil, &rStencil, ccc_oPassive);break; + case 5://trap which makes lemmings drown (water/quick sand/mud) + test_FillRect(pStencil, &rStencil, ccc_oTrapDrown);break; + case 6://trap which replaces lemming with death animation + test_FillRect(pStencil, &rStencil, ccc_oTrapAndNoLem);break; + case 7://trap which triggers lemming death animation + test_FillRect(pStencil, &rStencil, ccc_oTrapAndLem);break; + case 8://exit + test_FillRect(pStencil, &rStencil, ccc_oExit);break; + case 32://entry + test_FillRect(pStencil, &rStencil, ccc_oEntry); + break; + default:test_FillRect(pStencil, &rStencil, ccc_oUnknow);break; + } + switch(mode){ + case 0:break;// FULL + case 4: {//No Overwrite + test_blit(pStencilFixe, &rStencil, pStencil, &rStencil); + break; + } + case 8: break;//Vis On Terrain + default:{//-DEBUG-printf("BUG: value is not 8 4 or 0 : %d \n",mode); + return(36);} + } + } + if(mode==8&&(type==3||type==4)){ + for(oy=k->y;oy<rStencil.h+k->y;oy+=1){ + + if(oy<0) {oy=-1;continue;} + if(oy>pSpr_Lem->h){break;} + if(oy>=LEVEL_HEIGHT) {break;} + + if(sf->pixels==NULL){break;} + + for(ox=k->x;ox<rStencil.w+k->x;ox+=1){ + + if(ox<0) {ox=-1;continue;} + if(ox>pSpr_Lem->w){break;} + if(ox>=LEVEL_WIDTH) {break;} + + //if(temp_pixels==NULL){break;} + //memcpy(sf->pixels,temp_pixels,size); + color2 = get_pixel32(objetAnim.x+ox-k->x,objetAnim.y+oy-k->y,sf); + if(color2==ccc_error){return 253;} + // NO DIG + color = get_pixel32(ox,oy,pStencil); + if(color==ccc_error){return 254;} + if(color==ccc_bgStencil){continue;} + + // pixel transparency in object.gif + if(type==3){ + err=putPixel(pStencil,ox,oy,ccc_oNoDigLeft); + if(err!=0){return err;} + } else { + err=putPixel(pStencil,ox,oy,ccc_oNoDigRight); + if(err!=0){return err;} + } + } + } + //} + }//FIN ELSE MODE + k=k->next; +} +///FIN TEST: SPR +return 0; +} + +int paint_lemming (struct gameInit *gInit, int cptFps){ + + //DEBUT TEST: LEM + SDL_Rect offset,objetAnim; + SDL_Surface *sf; + struct listeSimplementChainee *k=NULL; + int res; + int err=0; + int cpt=0; + int newID; + int fx,fy,fs,la,ls;//lt; + + k=gInit->l.ll; + + while ( k !=NULL ) { + // //-DEBUG-printf("ID= %d k->data.pl->cptState== %d\n",k->ID,k->data.pl->cptState); + // DIED + if(k->data.pl->cptState <= 0 ){ + ////-DEBUG-printf("id ? %d \n",k->ID); + switch(k->ID){ + case 10 : if(k->data.pl->cptBoom!=404) {break;} + case 8 : + case 7 : + case 5 : k->ID=17;break;// decret de décès + case 9 : k->ID=19;break;// decret de survivant + default:break; + + } + } + //8 + (99 - releaseRate) + if((gInit->cptGame[15]==0)&&(gInit->cptGame[16]==gInit->mapI.nbrEntry)){ + gInit->cptGame[15]=cptFps; + } + + if( (k->ID==18)&& + (gInit->cptGame[16]==gInit->mapI.nbrEntry)&& + (!gInit->mapI.nuke)&& + ((gInit->l.nbrInput-(gInit->l.nbr + gInit->l.nbrDied + gInit->l.nbrSafe))>0)&& + (cptFps==(gInit->cptGame[15]+(8 + 99 - gInit->mapI.paraMap[0]))) + ){ + k->ID=1; + ++(gInit->l.nbr); + gInit->cptGame[15]=cptFps; + } + + // lemmings => spr de sa mort ou vivant + switch(k->ID){ + case 20: newID=10;break; + case 21: newID=6;break; + case 22: newID=4;break; + default: newID=k->ID;break; + } + if((k->data.pl->cptState != 0 ) || (newID < LEM_JOB)){//FIXME il y a une faille dans la condition + //-DEBUG-printf("num = %d id=%d(%d)\n",cpt++,newID,k->ID); + + offset.x = k->x; + offset.y = k->y; + sf = gInit->mapI.map.tabGif[gInit->mapI.map.tiles*2+(gInit->mapI.map.tilesObjet*2)+gInit->mapI.map.tilesSteel+(newID)*2+k->data.pl->dir+ADD_OBJ]; + + fx=gInit->mapI.lemmingDATA[newID].footX; + fy=gInit->mapI.lemmingDATA[newID].footY; + fs=gInit->mapI.lemmingDATA[newID].footSize; + la=gInit->mapI.lemmingDATA[newID].anim; + //lt=gInit->mapI.lemmingDATA[newID].type; + ls=gInit->mapI.lemmingDATA[newID].state; + + objetAnim.x = 0; + objetAnim.h = sf->h/ls; + objetAnim.y = (anim_objet((la+90),0,cptFps,ls,&k->data.pl->cptState,gInit))*(objetAnim.h); // +3 : 0 == loop et dans anim_objet c'est 3 pas 0 (fichier de conf) FIXME + objetAnim.w = sf->w; + // offset.w = objetAnim.w; + // offset.h = objetAnim.h; + switch(newID){ + case 0: {// WALK_ON + //putPixel(pStencil,k->x+fx+fs,k->y+fy,ccc_lWalk_on); + //test_FillRect(pStencil, &offset, ccc_lWalk_on); + break; + } + case 6: {//BLOCKER + putPixel(pStencil,k->x+sf->w-fx-fs,k->y+fy,ccc_lStopperLeft); + putPixel(pStencil,k->x+fx+fs,k->y+fy,ccc_lStopperRight); + //test_FillRect(pStencil, &offset, ccc_lStopper); + break; + } + default:break; + } + if ((res=test_blit(sf, &objetAnim, pSpr_Lem, &offset)) != 0){ + //-DEBUG-printf("ERREUR [%d] test_blit : ",res); + } + } + k=k->next; + + } + ///FIN TEST: LEM + err=stateLemming(gInit); + if(err!=0){return err;} + + return 0; +} + + + +int giveSkill (struct gameInit *gInit, struct listeSimplementChainee* k, int skill ){ + /* + struct listeSimplementChainee *l=NULL; + if(skill==20){ + gInit->mapI.nuke=1; + l=gInit->l.ll; + while(l!=NULL){ + giveSkill (gInit,l,10); + l=l->next; + } + return 0; + }*/ + + switch(skill){ + //climber + case 2 : if((gInit->mapI.paraMap[4]>0)&& + (k->data.pl->climber==0)){ + --gInit->mapI.paraMap[4]; + k->data.pl->climber=1; + } + break; + //floater + case 4 : if((gInit->mapI.paraMap[5]>0)&& + (k->data.pl->floater==0)){ + --gInit->mapI.paraMap[5]; + k->data.pl->floater=1; + } + break; + //bomber + case 10 : + if((gInit->mapI.paraMap[6]>0)&& + (k->data.pl->cptBoom==404)){ + if(!gInit->mapI.nuke) + {--gInit->mapI.paraMap[6];} + k->data.pl->cptBoom=5*FPS; + if( k->data.pl->blocker==1){ + k->ID=21; + } /*else { + k->ID=10; + }*/ + } + break; + //blocker + case 6 : if(((k->ID==0)||((k->ID>9)&&(k->ID<17)))&& + (gInit->mapI.paraMap[7]>0) + /*&&(k->data.pl->blocker==0)*/ + ){ + --gInit->mapI.paraMap[7]; + k->data.pl->blocker=1; + k->ID=6; + } + break; + //builder + case 11 : if(((k->ID==0)||((k->ID>9)&&(k->ID<17)))&& + (gInit->mapI.paraMap[7]>0) + /*&&(k->data.pl->blocker==0)*/ + ){ + --gInit->mapI.paraMap[8]; + //k->data.pl->cptBrick=12; + k->ID=11; + } + break; + case 14 : ;break;//basher + case 15 : ;break;//miner + case 13 : ;break;//digger + + default: break; + } + + + return 0; + +} + +int findTerrain( struct gameInit *gInit,int x, int y, int x0){ + SDL_Rect to; + int i=0; + int cpt=0; + int err=0; + SDL_Surface *sf; + struct listeSimplementChainee *k=NULL; + k=gInit->t.lt; + to.x=5; + to.y=screen->h-10; + to.w=600; + to.h=10; + test_FillRect(screen, &to, ccc_black); + while(k != NULL){ + sf=gInit->mapI.map.tabGif[test_O_UD(k->data.pt->modif)+k->ID*2+ADD_OBJ]; + if( (x+15 <= k->x + sf->w)&& + (x+15 >= k->x)&& + (y+15 <= k->y + sf->h)&& + (y+15 > k->y)){//on the terrain + ++cpt; + print_num(screen, gInit->mapI.map.tabGif[1], 5 , screen->h-10, cpt); + print_num(screen, gInit->mapI.map.tabGif[1], 5 + cpt*40, screen->h-10, i); + to.x=k->x; + to.y=k->y; + to.w=sf->w; + to.h=1; + test_FillRect(pStencil, &to, ccc_red); + to.x=k->x; + to.y=k->y; + to.w=1; + to.h=sf->h; + test_FillRect(pStencil, &to, ccc_red); + to.x=k->x + sf->w; + to.y=k->y; + to.w=1; + to.h=sf->h; + test_FillRect(pStencil, &to, ccc_red); + to.x=k->x ; + to.y=k->y + sf->h; + to.w=sf->w; + to.h=1; + test_FillRect(pStencil, &to, ccc_red); + err=print_num(pStencil, gInit->mapI.map.tabGif[1], k->x + 2, k->y + 2, i); + if(err!=0){return err;} + + } + ++i; + k=k->next; + + } + return 0; +} + +int paint_cursor(SDL_Surface *dst1,SDL_Surface *dst2, SDL_Surface *src, int x, int y, int x0, struct gameInit *gInit) +{ + SDL_Rect from, to; + int f=0; + int err=0; + struct listeSimplementChainee *k=NULL; + + /* Render! */ + from.x = 0; + from.h = src->h/8; + from.y = gInit->cptGame[NBR_BUTTON_LEMMING] * (from.h); + from.w = src->w; + + to.x = (y>LEVEL_HEIGHT)? x0 : x; + to.y = y; + if (y>LEVEL_HEIGHT){// cursor in pSpr_Lem + test_blit(src, &from, dst2, &to); + } else { + k=gInit->l.ll; + while(k != NULL){ + if( (x+15 <= k->x + 24)&&//32)&& + (x+15 >= k->x + 8)&&//)&& + (y+15 <= k->y + 20)&& + (y+15 > k->y )){//on the lemming + to.x=k->x; + to.y=k->y; + from.y = 4 * (from.h); + f=1; + //begin giveSkill + if((gInit->cptGame[18]==1)&&(gInit->cptGame[17]!=404)){ + switch(gInit->cptGame[17]){ + + case 2 : err=giveSkill(gInit,k,2);break;//climber + case 3 : err=giveSkill(gInit,k,4);break;//floater + case 4 : err=giveSkill(gInit,k,10);break;//bomber + + case 5 : err=giveSkill(gInit,k,6);break;//stopper + + case 6 : err=giveSkill(gInit,k,11);break;//builder + case 7 : err=giveSkill(gInit,k,14);break;//basher + case 8 : err=giveSkill(gInit,k,15);break;//miner + case 9 : err=giveSkill(gInit,k,13);break;//digger + + //case 11 : err=giveSkill(gInit,k,20);break;//nuke + + default: break; + } + if(err!=0){return err;} + gInit->cptGame[18]=0; + } + //end giveSkill + break; + } + k=k->next; + } + to.y -= 4; + test_blit(src, &from, dst1, &to); + if(y+from.h>LEVEL_HEIGHT){ + to.x = (f==1)? k->x-x + x0:x0; + to.y = (f==1)? k->y -4 :y -4; + test_blit(src, &from, dst2, &to); + } else { + if(f==1){ + to.x = k->x-x + x0; + to.y = k->y -4 ; + test_blit(src, &from, dst2, &to); + } + } + } + return 0; +} + +int legende (struct gameInit *gInit){ + + SDL_Rect rStencil; + int decalY = 11; + int err=0; + //char* msg=NULL; + + rStencil.x= 400; + rStencil.y= LEVEL_HEIGHT + 1; + rStencil.w= 10; + rStencil.h= 10; + + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Terrain:"); + if(err!=0){return err;} + rStencil.y += decalY; + test_FillRect(screen, &rStencil, ccc_tRemove); + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y ,"Remove"); + if(err!=0){return err;} + rStencil.y += decalY; + test_FillRect(screen, &rStencil, ccc_tRemoveNoOverwrite); + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"No Overwrite Remove"); + if(err!=0){return err;} + rStencil.y += decalY; + + test_FillRect(screen, &rStencil, ccc_tFull); + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Full"); + if(err!=0){return err;} + rStencil.y += decalY; + + test_FillRect(screen, &rStencil, ccc_tNoOverwrite); + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"No Overwrite"); + if(err!=0){return err;} + rStencil.y += decalY; + + test_FillRect(screen, &rStencil, ccc_tHidden); + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Hidden"); + if(err!=0){return err;} + rStencil.y += decalY; + + // object + rStencil.x = 30; + rStencil.y = LEVEL_HEIGHT + 1; + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Objet:"); + if(err!=0){return err;} + rStencil.y += decalY; + test_FillRect(screen, &rStencil, ccc_oPassive); + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Passive"); + if(err!=0){return err;} + rStencil.y += decalY; + + test_FillRect(screen, &rStencil, ccc_oNoDigLeft); + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"No digging to the left"); + if(err!=0){return err;} + rStencil.y += decalY; + + test_FillRect(screen, &rStencil, ccc_oNoDigRight); + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"No digging to the right"); + if(err!=0){return err;} + rStencil.y += decalY; + + test_FillRect(screen, &rStencil, ccc_oExit); + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Exit"); + if(err!=0){return err;} + rStencil.y += decalY; + + test_FillRect(screen, &rStencil, ccc_oEntry); + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Entry"); + if(err!=0){return err;} + rStencil.y += decalY; + + test_FillRect(screen, &rStencil, ccc_oUnknow); + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Unknow"); + if(err!=0){return err;} + rStencil.y += decalY; + + test_FillRect(screen, &rStencil, ccc_oTrapDrown); + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Trap which makes lemmings drown (water/quick sand/mud)"); + if(err!=0){return err;} + rStencil.y += decalY; + + test_FillRect(screen, &rStencil, ccc_oTrapAndNoLem); + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Trap which replaces lemming with death animation"); + if(err!=0){return err;} + rStencil.y += decalY; + + test_FillRect(screen, &rStencil, ccc_oTrapAndLem); + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Trap which triggers lemming death animation"); + if(err!=0){return err;} + + rStencil.y += decalY + 2; + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x +15 ,rStencil.y,"Steel:"); + if(err!=0){return err;} + rStencil.y += decalY; + test_FillRect(screen, &rStencil, ccc_s); + err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Indestructible terrain"); + if(err!=0){return err;} + + return 0; +} + +int paint_interface (struct gameInit *gInit){ + int i,n; + int err=0; + SDL_Rect to,from; + SDL_Surface *sf; + + from.x = 0; + to.y = LEVEL_HEIGHT + INTER_BUTTON_Y; + + for(i=0;i<NBR_BUTTON_LEMMING;++i){ + + sf=gInit->mapI.map.tabGif[i+NBR_ADD_OBJ]; + + to.x = (i*(sf->w)) + INTER_BUTTON_X; + to.w = sf->w; + to.h = sf->h/2; + + from.y = (to.h)*(gInit->cptGame[i]); + from.w = sf->w; + from.h = to.h; + + if(i+4 < 12){ + n=gInit->mapI.paraMap[i+4]; + err=print_num(screen, gInit->mapI.map.tabGif[1], (to.x+7+(((n/10)>0)?0:5))+(sf->w*(2)), to.y-12, + n); + if(err!=0){return err;} + } + test_blit(sf, &from, screen, &to); + } + n=gInit->mapI.paraMap[0]; + err=print_num(screen, gInit->mapI.map.tabGif[1],INTER_BUTTON_X + 20 + (((n/10)>0)?0:5), to.y-12, n); + if(err!=0){return err;} + return 0; +} + +int mouse_action (struct gameInit *gInit, int x, int y, int camX, int camY ){ + + int i,numB; + SDL_Surface *sf; + struct listeSimplementChainee *l=NULL; + // size cursor + sf=gInit->mapI.map.tabGif[3]; + x+=sf->w/2; + y+=sf->h/16;// (sf->h/8)/2; + sf=gInit->mapI.map.tabGif[NBR_ADD_OBJ]; + //-DEBUG-printf("action mouse ?\n"); + // test mouse on interface button + if( (x >= INTER_BUTTON_X)&& + (x <= sf->w*NBR_BUTTON_LEMMING)&& + (y >= INTER_BUTTON_Y + LEVEL_HEIGHT)&& + (y < INTER_BUTTON_Y + LEVEL_HEIGHT + (sf->h/2))) + {// mouse is on interface button + numB=((x-INTER_BUTTON_X)/sf->w); + if((gInit->cptGame[numB]==1)&&((numB==0)||(numB==1))){ + // incr or decr lemmingRate + if((numB==0)&&(gInit->mapI.paraMap[0]>13)){ + gInit->cptGame[15]-=5; + gInit->mapI.paraMap[0]-=5; + } else{ + if((numB==0)&&(gInit->mapI.paraMap[0]>8)){ + gInit->cptGame[15]-=1; + gInit->mapI.paraMap[0]-=1; + } } + if((numB==1)&&(gInit->mapI.paraMap[0]<91)){ + gInit->cptGame[15]+=5; + gInit->mapI.paraMap[0]+=5; + } else { + if((numB==1)&&(gInit->mapI.paraMap[0]<99)){ + gInit->cptGame[15]+=1; + gInit->mapI.paraMap[0]+=1; + } } + gInit->cptGame[numB]=0; + } else { + //NUKE + if(numB==11){ + gInit->mapI.nuke=1; + l=gInit->l.ll; + while(l!=NULL){ + if(l->ID!=17&&l->ID!=8&&l->ID!=9&&l->ID!=10&&l->ID!=21){ + giveSkill (gInit,l,10); + } + l=l->next; + } + } + + for(i=0;i<NBR_BUTTON_LEMMING;++i){ + if(i != numB){ + gInit->cptGame[i]=0; + } else { + if(gInit->cptGame[i] == 1){ + gInit->cptGame[i] = 0; + gInit->cptGame[17]=404; + } else { + gInit->cptGame[i] = 1; + gInit->cptGame[17]=i; + } + } + } + } + gInit->cptGame[18]=0; + } else { + // mouse is on pSpr_Lem + //if .. + //gInit.cptGame[NBR_BUTTON_LEMMING] + gInit->cptGame[18]=1; + } + return 0; +} + +int supprLem (struct gameInit *gInit, int cptFps){ + struct listeSimplementChainee *k=NULL; + // refresh liste all of 100 tick + if(cptFps % 100 == 0) + { + k=gInit->l.ll; + if((k != NULL)&&(gInit->l.nbr == 1)){ + if(k->ID == 17){++gInit->l.nbrDied; --gInit->l.nbr;} + if(k->ID == 19){++gInit->l.nbrSafe; --gInit->l.nbr;} + if((k->ID == 17)||(k->ID == 19)) {k=NULL;} + } else { + while(k != NULL){ + if(k->next != NULL){ + switch(k->next->ID){ + // DIED + case 17 : { + k->next=k->next->next; + ++gInit->l.nbrDied; + --gInit->l.nbr; + break; + } + // SAFE + case 19 : { + k->next=k->next->next; + ++gInit->l.nbrSafe; + --gInit->l.nbr; + break; + } + default: k=k->next; break; + } + } else { + k=k->next; + } + } + } + //-DEBUG-printf("nbrInput = %d\nnbrInGame = %d\nnbrDied = %d\nnbrSafe = %d\n",gInit->l.nbrInput,gInit->l.nbr,gInit->l.nbrDied,gInit->l.nbrSafe); + } + return 0; +} + + +// Par défaut, c'est l'analyse LEXICALE qui est lancée ! +int lancement (){ + + int res,cptFps/*,i*/; + int err=0; + //Ce qui va nous permettre de quitter + int quit = 0; + SDL_Rect camera,inter; + int mouseX,mouseY=0; + long tick,temps=0; + float fps = 0.0; + int fps_count = 0; + int fps_start = 0; + int decalFps = 0; + + //La structure d'evenement + SDL_Event event; + event.type=0; + + //paint_stencil=1; + + + /* camera.x=160; + camera.y=120; + */ + camera.x=0; + camera.y=0; + + struct gameInit gInit; + + // constructor ... + err=initGame(&gInit); + if(err!=0){return err;} + + //Initialisation + err=init(); + if(err!=0){return err;} + ////-DEBUG-printf("DONE\n"); + + ////-DEBUG-printf("DEBUG3: load_file ... \n"); + //Chargement des fichiers + err=load_files(&gInit); + if(err!=0){return err;} + + // paraMap + for(res=4;res<IDENT_COUNT;++res){ + gInit.mapI.paraMap[res]=99; + } + + // CREATION OF SOME LEMMING + err=creationLemming(&gInit); + if(err!=0){return err;} + + mouseX = gInit.mapI.xPos; + camera.x=gInit.mapI.xPos; + camera.w=screen->w; + camera.h=screen->h; + inter.x=0; + inter.y=LEVEL_HEIGHT; + inter.w=SCREEN_WIDTH; + inter.h=INTERFACE_HEIGHT; + + ////-DEBUG-printf("DEBUG ludo : camera.w==%i, camera.h==%i\n", camera.w, camera.h); + + // init level map (upper part of screen) + test_FillRect( pTerrain, &pTerrain->clip_rect, + gInit.mapI.map.cmap.bgColor ); + + + + // init interface (downner part of screen) + test_FillRect( screen, &inter, ccc_black); + cptFps=0; + gInit.cptGame[15]=0; + + test_FillRect( pStencil, NULL, ccc_bgStencil); + //paint_terrain => pTerrain + err=paint_terrain(&gInit,0); + if(err!=0){return err;} + + SDL_SetColorKey(pTerrain, SDL_SRCCOLORKEY|SDL_RLEACCEL, gInit.mapI.map.cmap.bgColor); + + err=print_num(screen, gInit.mapI.map.tabGif[1], screen->w-37, screen->h-10, FPS); + if(err!=0){return err;} + + test_blit(pStencil, NULL, pStencilFixe, NULL); + SDL_SetColorKey(pStencilFixe, SDL_SRCCOLORKEY, ccc_bgStencil); + err=paint_objet_stencil (&gInit,cptFps); + if(err!=0){return err;} + test_blit(pStencil, NULL, pStencilFixe, NULL); + + //Tant que l'utilisateur n'a pas quitter + + while( quit == 0 ) + { + /* Timing */ + tick = SDL_GetTicks(); + + //Tant qu'il y a un événement + while( SDL_PollEvent( &event ) ) + { + switch(event.key.keysym.sym){ + case SDLK_HOME : decalFps = 0; break; + case SDLK_END : decalFps = FPS-11; break; + case SDLK_PAGEUP : if(decalFps>0){decalFps -=1;}break; + case SDLK_PAGEDOWN : if(decalFps<(FPS-1)){decalFps +=1;}break; + case SDLK_w : paint_stencil = (paint_stencil==0)? 1 : 0 ; break; + case SDLK_ESCAPE : quit = 1; break; + default:break; + } + // SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE); + + switch (event.type) { + case SDL_MOUSEMOTION: { mouseX = event.motion.x; + mouseY = event.motion.y;break; } + case SDL_MOUSEBUTTONDOWN: { err=mouse_action (&gInit, mouseX, mouseY,camera.x,camera.y ); + if(err!=0){return err;} + break;} + case SDL_QUIT : { + //On quitte le programme + quit = 1; + } + default:break; + } + } + + if(mouseY <= LEVEL_HEIGHT){ + if (mouseX > (SCREEN_WIDTH - BOUND_SENSIBILITE)){ + if (camera.x < (LEVEL_WIDTH - SCREEN_WIDTH ) ) + {camera.x += CAM_VITESSE;} + } + if (mouseX < BOUND_SENSIBILITE){ + if (camera.x >= CAM_VITESSE ) + {camera.x -= CAM_VITESSE;} + } + } + + test_FillRect( pSpr_Lem, NULL, gInit.mapI.map.cmap.bgColor); + test_blit(pTerrain, NULL, pSpr_Lem, NULL); + + test_FillRect( screen, &inter, ccc_black); + + test_FillRect( pStencil, NULL, ccc_bgStencil); + test_blit(pStencilFixe, NULL, pStencil, NULL); + + // SPR => pSpr_Lem + err=paint_objet (&gInit,cptFps); + if(err!=0){return err;} + + // SUPPR LEM => gInit + err=supprLem(&gInit,cptFps); + if(err!=0){return err;} + + // LEM => pSpr_Lem + //if(paint_stencil==0){ + err=paint_lemming (&gInit,cptFps); + if(err!=0){return err;} + //} + + // INTERFACE => screen + if(paint_stencil==0){ + err=paint_interface (&gInit); + if(err!=0){return err;} + } else { + //testAlpha(screen, gInit.mapI.map.tabGif[9],gInit.mapI.map.tabGif[1],2,330); + err=legende (&gInit); + if(err!=0){return err;} + } + + // CURSOR => pSpr_Lem, screen + if(paint_stencil==0){ + err=paint_cursor (pSpr_Lem,screen, gInit.mapI.map.tabGif[3], mouseX+camera.x, mouseY+camera.y, mouseX, &gInit); + if(err!=0){return err;} + } else { + err=findTerrain(&gInit,mouseX+camera.x, mouseY+camera.y, mouseX); + if(err!=0){return err;} + err=paint_cursor (pStencil,screen, gInit.mapI.map.tabGif[3], mouseX+camera.x, mouseY+camera.y, mouseX, &gInit); + if(err!=0){return err;} + //-DEBUG-printf("x=%d y=%d\n",mouseX+camera.x,mouseY+camera.y); + } + + //get_pixel32( event.motion.x + camera.x, event.motion.y, pTerrain ); + //get_pixel32( event.motion.x + camera.x, event.motion.y, pSpr_Lem ); + //get_pixel32( event.motion.x + camera.x , event.motion.y, screen ); + + + if(paint_stencil==0){ + // printf("test %p\n",pSpr_Lem); + test_blit(pSpr_Lem, &camera, screen, NULL); + err=miniMap (pSpr_Lem,mouseX+camera.x,mouseY+camera.y,5.,5.); + if( err != 0){ return err;} + } else { + test_blit(pStencil, &camera, screen, NULL); + err=miniMap (pStencil,mouseX+camera.x,mouseY+camera.y,5.,5.); + if( err != 0){ return err;} + + } + + + /* FPS counter */ + ////-DEBUG-printf("(tick %ld > fps_start %d +500)\n",tick,fps_start); + // refresh fps all 500 ms + if(tick > fps_start + 500) + { + fps = (float)fps_count * 1000.0 / (tick - fps_start); + fps_count = 0; + fps_start = tick; + } + + ++fps_count; + /* + if (cptFps == 99999) { + //if(SDL_GetTicks()==240000){ + //decalFps = 99; + quit=1; + }} + */ + // limte fps + + if(FPS<=decalFps){//-DEBUG-printf("BUG: FPS < 0 : (FPS %d - decalFps %d) < 0",FPS,decalFps); + return(42); + } else { + while((FPS>decalFps)&&(((float)fps_count * 1000.0 / (SDL_GetTicks()-fps_start)) > FPS-decalFps) ){ + if((FPS-decalFps)<=120){// useless ? + SDL_Delay(3); + } + }} + + temps = SDL_GetTicks(); + //heure + err=print_num(screen, gInit.mapI.map.tabGif[1], screen->w-160, screen->h-40, temps/(1000*3600)); + if(err!=0){return err;} + //minute + err=print_num(screen, gInit.mapI.map.tabGif[1], screen->w-130, screen->h-40, (temps/(1000*60))%60); + if(err!=0){return err;} + //seconde + err=print_num(screen, gInit.mapI.map.tabGif[1], screen->w-100, screen->h-40, (temps/1000)%60); + if(err!=0){return err;} + + err=print_num(screen, gInit.mapI.map.tabGif[1], screen->w-150, screen->h-10, cptFps); + if(err!=0){return err;} + err=print_num(screen, gInit.mapI.map.tabGif[1], screen->w-87, screen->h-10, fps); + if(err!=0){return err;} + + //Mise à jour de l'écran + if( SDL_Flip( screen ) == -1 ) { + return 4; + } + + //SDL_Delay(1); + + if(FPS<=decalFps){//-DEBUG-printf("BUG: FPS < 0 : (FPS %d - decalFps %d) < 0",FPS,decalFps); + return(43); + } else { + temps = SDL_GetTicks(); + ////-DEBUG-printf("cptFps = %d, time: %ld:%ld:%ld \n",cptFps,temps/(1000*3600),(temps/(1000*60))%60,(temps/1000)%60); + } + cptFps++; + //quit = 1; + + } + + //On libère les images et on quitte SDL + err=clean_up(); + if(err!=0){return err;} + + return 0; +} diff --git a/sdl-test/SDL_tuto/TestParserLemmingsLVL/parse_ini.yy b/sdl-test/SDL_tuto/TestParserLemmingsLVL/parse_ini.yy index 2bb9cc6..48042e0 100644 --- a/sdl-test/SDL_tuto/TestParserLemmingsLVL/parse_ini.yy +++ b/sdl-test/SDL_tuto/TestParserLemmingsLVL/parse_ini.yy @@ -912,3407 +912,76 @@ void yyerror(struct gameInit *gInit, char *s) exit(9); } -Uint32 string_to_Uint32 (char* c,int taille){ - - Uint32 u32c; - int i,n,lc = strlen(c); - int t[taille]; - if(lc-3 != taille){ - //-DEBUG-printf("ERREUR: wrong format .. expect (strlen[%s] - 3)==%d but it's = %d\n",c,taille,lc-3); - return(ccc_error); - } - u32c = 0; - for(i=0;i<taille;++i){ - n = c[i+2]; - ////-DEBUG-printf("n = %d, %c\n",n); - if (n > 47 && n < 58 ) { t[i]=(n - 48) ;} - if (n > 64 && n < 71 ) { t[i]=(n - 55) ;} - if (n > 96 && n < 103 ){ t[i]=(n - 87) ;} - } - n=0; - for(i=0;i<taille;++i){ - //-DEBUG-printf("t[%d]= %d, *(%d), %d\n",i,t[i],(1<<(4*(taille-i-1))),t[i]*(1<<(4*(taille-i-1))) ); - - n+=t[i]*(1<<(4*(taille-i-1))); - - - //-DEBUG-printf("n= %d\n",n); - } - u32c= n & 0xffffff; - return u32c; -} - - -Uint32* tab_of_string_to_Uint32 (char* c, int taille){ - int i,lc = strlen(c); - char t[10]; - int cpt=0; - int cpt2=0; - Uint32* tab=malloc(sizeof(Uint32)*(lc/(taille+3))); - t[0]='0';t[1]='x';t[8]=',';t[9]=0; - - for(i=0;i<lc;++i){ - // token - if(c[i]=='x'){cpt=0;} - if(cpt<6){ - t[cpt+2]=c[i+1]; - ++cpt; - if(cpt==6){ - tab[cpt2]=string_to_Uint32(t,taille); - cpt2++; - } - } - } - return tab; -} - -/* inverse la liste fournie en parametre et la renvoie */ -struct listeSimplementChainee* rev_listeO (struct listeSimplementChainee* liste) -{ - /* un élément temporaire */ - struct listeSimplementChainee* travail=NULL; - /* la liste inversée */ - struct listeSimplementChainee* retour=NULL; - /* on parcoure la liste entree tant qu'il y a un élément */ - while(liste!=NULL){ - /* on prend le premier element de la liste */ - travail=liste; - // //-DEBUG-printf("DEBUG:2.1 liste = %p travail = %p\n",liste,travail); - /* le début de la liste devient l'élément suivant */ - liste=travail->next; - /* on libère l'élélement de la liste et on le place en début de la - liste à renvoyer*/ - travail->next=retour; - /* l'élément qu'on vient de mettre en début de liste devient - le début de la liste de à renvoyer */ - retour=travail; - } - /* YAPUKA renvoyer la liste inversée */ - return retour; -} - - - -// returne le state de l'oblet a afficher -int anim_objet(int typeS, int typeO, int cptFps, int frames, int *cpt, struct gameInit *gInit){ - int f=0; - if(typeS == 90){typeS=4;}//Lemming anim continue - if(typeS == 91){typeS=5;}//Lemming anim at once - switch(typeS){ - // not animed - case 0 : break;//f=0 - // Object animation continue - case 1 : f=(cptFps/ANIM_STEP_FPS)%(frames);break; - // Object animation at once - case 2 : if((*cpt) > 0){ - f=(cptFps/ANIM_STEP_FPS)%(frames);--(*cpt); - } - break; - //animation at start level - case 3 : if((SDL_GetTicks()-TIME_WAIT_TRAP_START)>0){ - if(cptFps<frames*ANIM_STEP_FPS){ - f=(cptFps/ANIM_STEP_FPS)%(frames); - } else { - f=(frames-1);//on s'arrête à last frame - if(typeO==32){// entry is open - if(gInit->cptGame[16] < gInit->mapI.nbrEntry){++(gInit->cptGame[16]);} - } - } - } - break; - // Lemming animation continue - case 4 : f=(cptFps)%(frames);break; - // Lemming animation at once - case 5 : if((*cpt) > 0){ - f=(cptFps)%(frames);--(*cpt); - } else {//on s'arrête à last frame - f=(frames-1); - } - break; - default : //-DEBUG-printf("BUG typeSprite : %d\n",typeS); - exit(12); - } - /* - switch(typeO){ - case 0 : f=cptFps%(frames);break; - case 3 : f=cptFps%(frames);break;//FIXME - case 4 : f=cptFps%(frames);break;//FIXME - case 5 : f=cptFps%(frames);break;//FIXME - case 6 : f=cptFps%(frames);break;//FIXME - case 7 : f=cptFps%(frames);break;//FIXME - case 8 : f=cptFps%(frames);break;//FIXME - case 32 : f=cptFps%(frames);break;//FIXME - default : //-DEBUG-printf("BUG typeObjet : %d\n",type0); - return(12); - } - }*/ -/* # animation types -# 0 - don't animate -# 1 - animate continously -# 2 - trap - animate on trigger - else show first pic -# 3 - entry animation: animate once at level start - -# object types -# 0 - passive -# 3 - no digging to the left -# 4 - no digging to the right -# 5 - trap which makes lemmings drown (water/quick sand/mud) -# 6 - trap which replaces lemming with death animation -# 7 - trap which triggers lemming death animation -# 8 - exit -# 32 - entry*/ -return f; -} - -inline Uint32 get_pixel32( int x, int y, SDL_Surface *surface ) -{ - //Convertie les pixels en 32 bit - Uint32 *pixels = (Uint32 *)surface->pixels; - ////-DEBUG-printf("4) x=%d,y=%d,p =%X\n",x,y,pixels[ ( y * surface->w ) + x ]); - //Recupere le pixel demande - - /*//-DEBUG-printf("out get_pixel32 !! => (((y %d * surface->w %d ) + x %d) => %d >= surface->h %d *surface->w %d = %d)\n",y,surface->w,x,(y * surface->w ) + x,surface->h,surface->w,surface->h*surface->w);*/ - /* if ( surface==NULL ) { //-DEBUG-printf("ludo1\n"); - return (ccc_error); } - if ( pixels==NULL ) { - //-DEBUG-printf("ludo2\n"); - return (ccc_error); } - - if((((y * surface->w ) + x) >= surface->h*surface->w)|| - (((y * surface->w ) + x) < 0)){ - if(y==surface->h){//-DEBUG-printf("get C'EST PAS ASSEZ GROS POUR TOI !!!! y=surface->h\n"); - return (ccc_error);} - //-DEBUG-printf("out get_pixel32 !! => (((y %d * surface->w %d ) + x %d) => %d >= surface->h %d *surface->w %d = %d)\n",y,surface->w,x,(y * surface->w ) + x,surface->h,surface->w,surface->h*surface->w); - return (ccc_error); - } - */ - - return pixels[ ( y * surface->w ) + x ]; - -} - -inline int putPixel(SDL_Surface *surface,Uint16 x,Uint16 y,Uint32 colori) -{ - /* bpp de la surface ecran */ - Uint8 bpp = surface->format->BytesPerPixel; - - /* pointeur vers le pixel a remplacer */ - /* if((((y * surface->w ) + x) >= surface->h*surface->w)|| - (((y * surface->w ) + x) < 0)){ - //-DEBUG-printf("out putPixel !! => (((y %d * surface->w %d ) + x %d) => %d >= surface->h %d *surface->w %d = %d)\n",y,surface->w,x,(y * surface->w ) + x,surface->h,surface->w,surface->h*surface->w); - return (101); - } - */ - if(y==surface->h){//-DEBUG-printf("put C'EST PAS ASSEZ GROS POUR TOI !!!! y=surface->h\n"); - return (100);} - Uint8 *p = ((Uint8 *)surface->pixels) + y * surface->pitch + x * bpp; - - /*//-DEBUG-printf("pixel R = %X G = %X B = %X A=%X bpp = %d, ccc=%X \n", - ((colori>>16) & 0xff), - ((colori>>8) & 0xff), - (colori & 0xff), - ((colori>>24) & 0xff), - bpp, - colori );*/ - - switch(bpp) - { - case 1: - *p = (Uint8) colori; - break; - case 2: - *(Uint16 *)p = (Uint16) colori; - break; - case 3: - if (SDL_BYTEORDER == SDL_BIG_ENDIAN) - { - *(Uint16 *)p = ((colori >> 8) & 0xff00) | ((colori >> 8) & 0xff); - *(p + 2) = colori & 0xffff; - } - else - { - *(Uint16 *)p = colori & 0xffff; - *(p + 2) = ((colori >> 16) & 0xffff) ; - } - break; - case 4:{ - *(Uint32 *)p = colori; - } - break; - } - return 0; -} - -//return number of free pixels below the lemming -int checkPixelDOWN(int nbr, int x, int y){ - int res; - int err; - int yb; - - if (x<0 || x >= LEVEL_WIDTH) - {return 0;} - - yb = y + 1; - - for(res=0;res<nbr;++res){ - if(yb+res >= LEVEL_HEIGHT){ - return FALL_DISTANCE_FORCE_FALL; - } - - putPixel(pSpr_Lem,x,yb+res,ccc_red); - - err=get_pixel32(x,yb+res,pStencil); - if(err==ccc_error){return 113;} - - ////-DEBUG-printf("res %d\n",res); - - if((err&0xbb)!=0xbb){// it's not an object - if(err!=ccc_bgStencil){ - return res;//return ((res == 0)? 0 : res-1);//res; - } - } - } - return res; -} - -//return number of free pixels above the lemming -int checkPixelUP(int nbr, int x, int y, int size ){ - int res; - int err; - int ym; - - if (x<0 || x >= LEVEL_WIDTH) - return 0; - - ym=midY(y,size); - - for(res=0;res<nbr;++res){ - if(ym-res <= 0){ - return -1;//splat - } - - putPixel(pSpr_Lem,x,ym-res,ccc_green); - - err=get_pixel32(x,ym-res,pStencil); - if(err==ccc_error){return 113;} - - if((err&0xbb)!=0xbb){// it's not an object - if(err!=ccc_bgStencil){ - return res;//return ((res == 0)? 0 : res-1);//res; - } - } - } - return res; -} - -//return true if Lemming has fallen to/through the bottom of the level, false otherwise -int outOfLowerMap(int y) { - if (y >= LEVEL_HEIGHT) { - //-DEBUG-printf("Play Sound SND_DIE\n"); - //GameController.sound.play(GameController.SND_DIE); - return 1; - } - return 0; -} - -//return number of pixels of walkable ground above the Lemmings foot. -int aboveGround(int x, int y) { - - int res=0; - int err; - - if (x<0 || x >= LEVEL_WIDTH) - return LEVEL_HEIGHT-1; - - if (y >= LEVEL_HEIGHT) - return LEVEL_HEIGHT-1; - - //y+=1; - - for(res=0;res<WALKER_OBSTACLE_HEIGHT;++res){ - if(y-res<0){ - return WALKER_OBSTACLE_HEIGHT +1;// forbid leaving level to the top - } - - putPixel(pSpr_Lem,x,y-res,ccc_cyan); - - err=get_pixel32(x,y-res,pStencil); - if(err==ccc_error){return 114;} - - - if((err&0xbb)==0xbb){//it'an object - return res; - } - if((err&0xcb)==0xcb){// it's a blocker - return res; - } - if(err==ccc_bgStencil){ - return res; - } - } - return res; -} -//return Position inside lemming which is used for collisions -int midY(int y, int size) { - return y - size; -} - -//return true if climber reached a plateau he can walk on, false otherwise -int reachedPlateau(int x, int y, int size, int dir) { - - int ym; - int err; - - if (x<2 || x >= LEVEL_WIDTH-2) - return 0; - - ym = midY(y,size); - if (ym>= LEVEL_HEIGHT || ym<0) - return 0; - - if (dir == 0)//LEFT - x -= 2; - else - x += 2; - - putPixel(pSpr_Lem,x,y,ccc_yellow); - - err=get_pixel32(x,y,pStencil); - if(err==ccc_error){return 115;} - - if((err&0xbb)==0xbb){//it'an object - return 1; - } - if((err&0xcb)==0xcb){// it's a blocker - return 0; - } - if(err==ccc_bgStencil){ - return 1; - } - /* - - if((err&0xbb)!=0xbb){ -//if(err!=(ccc_lWalk_on & ccc_bgStencil)){ -if((err|ccc_lWalk_on)!=ccc_bgStencil){ -return 1; -} -} - */ -return 0; -} - -int explode(int x, int y, int size){ - //-DEBUG-printf("Play Sound SND_EXPLODE\n"); - //GameController.sound.play(GameController.SND_EXPLODE); - return 0; -} - -//return stencil value from the middle of the lemming -int stencilMid(int x, int y) { - Uint32 err; - if (x>0 && x<LEVEL_WIDTH && y > 0 && y < LEVEL_HEIGHT){ - - putPixel(pSpr_Lem,x,y,0xff00ff); - err=get_pixel32(x,y,pStencil); - } - else { - err = ccc_bgStencil; - } - return err; -} - -// Check if a Lemming is to be turned by a stopper/blocker -// return true if Lemming is to be turned, false otherwise -int turnedByStopper(int x, int y, int dir) { - Uint32 err; - err=stencilMid(x,y); - if(err==ccc_error){return 116;} - if(err==ccc_lWalk_on){return 2;} - - ////-DEBUG-printf("err %p\n",err); - - if (err == ccc_lStopperLeft && dir==1) {//Right - //LEFT; - return 0; - } - if (err == ccc_lStopperRight && dir==0) {//Left - //RIGHT - return 1; - } - return 2; -} - -int stateLemming(struct gameInit *gInit){ - - struct listeSimplementChainee *k; - struct listeSimplementChainee *o; - SDL_Surface *sfT; - int boom=0; - int flip=0; - int free=0; - int fx=0; - int fy=0; - int fs=0; - int lh=0; - int lw=0; - int lState=0; - int fa; - //int checkMask; - //int sx; - //int sy; - //int idx; - //int maxF; - int err; - int levitation; - int oldX; - int oldType; - int newType; - int newID; - - k=gInit->l.ll; - while(k != NULL){ - if((k->ID<LEM_JOB)||(k->ID>19)){ - switch(k->ID){ - case 20: newID=10;break; - case 21: newID=6;break; - case 22: newID=4;break; - default: newID=k->ID;break; - } - - fx=gInit->mapI.lemmingDATA[newID].footX; - fy=gInit->mapI.lemmingDATA[newID].footY; - //k->y-=gInit->mapI.lemmingDATA[0].footY-fy; - fs=gInit->mapI.lemmingDATA[newID].footSize; - lState=gInit->mapI.lemmingDATA[newID].state; - - sfT = gInit->mapI.map.tabGif[gInit->mapI.map.tiles*2 + gInit->mapI.map.tilesObjet*2 +gInit->mapI.map.tilesSteel +(newID)*2+(k->data.pl->dir) + ADD_OBJ]; - - lh=sfT->h/lState; - lw=sfT->w; - - oldX=k->x; - oldType=k->ID; - newType=k->ID; - - // cooldown before lemming boom - boom=0; - switch(k->data.pl->cptBoom){ - case 0: boom=1;break; - case 404: break; - default: { - --k->data.pl->cptBoom; - if(paint_bomber(k->x,k->y,lw,gInit->mapI.map.tabGif[2],pSpr_Lem,(k->data.pl->cptBoom/FPS +1))!=0){return 99;} - } - } - - //flipDirBorder - if (k->data.pl->dir==1) {//right - if (k->x<0) { - k->x=0; - flip = 1; - } else if (k->x >= LEVEL_WIDTH) { - k->x = LEVEL_WIDTH-1; - flip = 1; - } - } - //if (selectCtr>0) {selectCtr--;} - if (flip) {k->data.pl->dir = (k->data.pl->dir==1) ? 0 : 1 ;} - - switch(k->ID){ - case 3 : // climber_to_walker - //-DEBUG-printf("climber_to_walker ->"); - if(boom){ - explode (k->x+fx,k->y+fy,fs); - k->ID=10;//BOMBER - break; - } - case 1 : // faller - {//-DEBUG-printf("faller ->"); - if(boom){ - explode (k->x+fx,k->y+fy,fs); - k->ID=10;//BOMBER - break; - } - ////-DEBUG-printf("x =%d y =%d x+fx= %d, y+fy =%d \n",k->x,k->y,k->x+fx,k->y+fy); - free=checkPixelDOWN(FALLER_STEP, k->x+fx,k->y+fy); - if(free==113){return 113;} - if(free==FALL_DISTANCE_FORCE_FALL) - {k->y += FALLER_STEP;} - else - {k->y += free;} // max: FALLER_STEP - err=outOfLowerMap(k->y+fy); - if(err==114){return 114;} - if(err==0){ - k->data.pl->cptFall+=free; - // check conversion to floater - if((k->data.pl->floater==1)&& - (k->data.pl->cptFall>= FALL_DISTANCE_FLOAT)){ - k->ID=22;//FLOATER_START - k->data.pl->cptFall2 = 0;// used for parachute opening "jump" up - } else if(free==0) {// check ground hit - if(k->data.pl->cptFall > gInit->mapI.MaxFallDistance){ - k->ID=5;//SPLAT - } else { - k->ID=0;//WALKER - k->y-=2; - k->data.pl->cptFall=0; - } - } - } else { - k->ID=17; - } - break; - } - case 16 : // JUMPER - { //-DEBUG-printf("JUMPER ->"); - if(boom){ - k->ID=10;//BOMBER - if(!gInit->mapI.nuke){ - //-DEBUG-printf("Play Sound SND_EXPLODE\n"); - //GameController.sound.play(GameController.SND_EXPLODE); - } - break; - } - // check collision with stopper - err=turnedByStopper(k->x+lw/2,k->y+fy,k->data.pl->dir); - if(err==116){return 116;} - if(err!=2){ - k->data.pl->dir=err; - break; - } - err = aboveGround(k->x+fx,k->y+fy); - if(err==114){return 114;} - levitation=err; - if (levitation > JUMPER_STEP) - {k->y -= JUMPER_STEP;} - else { - // conversion to walker - k->y -= levitation; - k->ID=0; //WALKER; - } - } - break; - case 0: // WALKER - { //-DEBUG-printf("WALKER ->"); - if(boom){ - k->ID=10;//BOMBER - if(!gInit->mapI.nuke){ - //-DEBUG-printf("Play Sound SND_EXPLODE\n"); - //GameController.sound.play(GameController.SND_EXPLODE); - } - break; - } - // check collision with stopper - err=turnedByStopper(k->x+lw/2,k->y+fy,k->data.pl->dir); - if(err==116){return 116;} - if(err!=2){ - k->data.pl->dir=err; - break; - } - k->x += (k->data.pl->dir==1)? WALKER_STEP : -WALKER_STEP; - // check - free=checkPixelDOWN(FALL_DISTANCE_FALL, k->x+fx,k->y+fy); - if(free==113){return 113;} - if(free==FALL_DISTANCE_FORCE_FALL) - {k->y += FALLER_STEP;} - else - { - k->y += free; // max: FALLER_STEP - k->data.pl->cptFall=free; - } - err = aboveGround(k->x+fx,k->y+fy); - if(err==114){return 137;} - levitation=err; - //-DEBUG-printf("levitation %d free %d \n",levitation,free); - if((levitation < WALKER_OBSTACLE_HEIGHT) && ((k->y+fy+lh/2)>0)) - { - if (levitation >= JUMPER_STEP) - { - k->y -= JUMPER_STEP; - k->ID = 16; - break; - } - else { - // conversion to jumper - k->y -= levitation; - } - } else { - k->x=oldX; - if(k->data.pl->climber==1){ - k->ID=2; // CLIMBER - break; - } else { - k->data.pl->dir = (k->data.pl->dir==1) ? 0 : 1 ; - } - } - if(free>0){ - // check for conversion to faller - k->data.pl->cptFall += FALLER_STEP; - if(free >= FALL_DISTANCE_FALL){ - k->ID=1; // FALLER - } - } - break; - } - case 22: // FLOATER_START - //-DEBUG-printf("FLOATER_START ->"); - if(boom){ - explode (k->x+fx,k->y+fy,fs); - k->ID=10;//BOMBER - break; - } - switch (k->data.pl->cptFall2++) { - case 0: - case 1: // keep falling with faller speed - case 2: k->y += FALLER_STEP-FLOATER_STEP; - break; - case 3: k->y -= FLOATER_STEP-1; // decelerate a little - break; - case 4: - case 5: - case 6: - case 7: k->y -= FLOATER_STEP; // decelerate some more - break; - default: - k->ID=4;//FLOATER; - } - - case 4 : //FLOATER - //-DEBUG-printf("FLOATER ->"); - if(boom){ - explode (k->x+fx,k->y+fy,fs); - k->ID=10;//BOMBER - break; - } - free=checkPixelDOWN(FALLER_STEP, k->x+fx,k->y+fy); - if(free==113){return 113;} - if(free==FALL_DISTANCE_FORCE_FALL) - {k->y += FALLER_STEP;} - else - {k->y += free;} // max: FALLER_STEP - - err=outOfLowerMap(k->y+fy); - if(err==114){return 114;} - if(err==0){ - k->data.pl->cptFall+=free; - if(free==0){ - k->ID=0;//WALKER - k->data.pl->cptFall=0; - } - } else { - k->ID=17; - } - break; - case 2 : // CLIMBER - //-DEBUG-printf("CLIMBER ->"); - if(boom){ - explode (k->x+fx,k->y+fy,fs); - k->ID=10;//BOMBER - break; - } - - if ( (++(k->data.pl->cptFall) & 1) == 1) // only every other step - {k->y -= CLIMBER_STEP;} - //fa=(k->data.pl->dir==1) ? 4 : 3 ; - err=checkPixelUP(2,k->x+fx+3,k->y+fy,fs); - if(err==113){return 127;} - if (midY(k->y+fy,fs) < 0 || err<2) { - k->data.pl->dir = (k->data.pl->dir==1) ? 0 : 1 ; - k->ID=1; //FALLER; - k->data.pl->cptFall = 0; - } else { - err=reachedPlateau(k->x+fx+3,k->y+fy,fs,k->data.pl->dir); - if (err==115){return 115;} - if(err) { - k->data.pl->cptFall = 0; - k->ID=3; //CLIMBER_TO_WALKER; - } - } - break; - - case 5: //SPLAT - //-DEBUG-printf("SPLAT ->"); - if(boom){ - explode (k->x+fx,k->y+fy,fs); - k->ID=10;//BOMBER - break; - } - if(k->data.pl->cptState==0){ - k->data.pl->cptState=gInit->mapI.lemmingDATA[newID].state; - //-DEBUG-printf("Play Sound SND_SPLAT\n"); - //GameController.sound.play(GameController.SND_SPLAT); - } - break; - /* - case 13: //DIGGER - if(boom){ - k->ID=10;//BOMBER - if(!gInit->mapI.nuke){ - //-DEBUG-printf("Play Sound SND_EXPLODE\n"); - //GameController.sound.play(GameController.SND_EXPLODE); - } - break; - } - break; - case 14: // BASHER - { - if(boom){ - k->ID=10;//BOMBER - if(!gInit->mapI.nuke){ - //-DEBUG-printf("Play Sound SND_EXPLODE\n"); - //GameController.sound.play(GameController.SND_EXPLODE); - } - break; - } - err=turnedByStopper(k->x+lw/2,k->y+fy,k->data.pl->dir); - if(err==116){return 116;} - if(err!=2){ - k->data.pl->dir=err; - k->ID=0;//WALKER - break; - } - free=checkPixelDOWN(FALLER_STEP, ccc_black, k->x+fx,k->y+fy,pStencil); - if(free==113){return 113;} - if(free==FALL_DISTANCE_FORCE_FALL) - {k->y += FALLER_STEP;} - else - {k->y += free;} // max: FALLER_STEP - - if(free != 0){ - k->data.pl->cptFall += free; - if (k->data.pl->cptFall >= BASHER_FALL_DISTANCE) - { - k->ID=1;//FALLER; - } - } else { - k->data.pl->cptFall = 0; - } - //Mask m; - //int checkMask; - maxF=gInit->mapI.lemmingDATA[newID].state - idX= maxF - k->data.pl->cptState + 1; - if (idx >= maxF) {idx=0;} - switch(idx){ - case 2 : - case 3 : - case 4 : - case 5 : { - // bash mask should have the same height as the lemming - sx=k->x; - sy=k->y; - checkMask = ccc_s|((k->data.pl->dir==0) ? ccc_oNoDigLeft : ccc_oNoDigRight); - eraseMask(sx); - } - } - } - //ETC ... - */ - case 12: //BUILDER_END - //-DEBUG-printf("BUILDER_END ->"); - if(boom){ - k->ID=10;//BOMBER - if(!gInit->mapI.nuke){ - //-DEBUG-printf("Play Sound SND_EXPLODE\n"); - //GameController.sound.play(GameController.SND_EXPLODE); - } - } - k->data.pl->cptBrick=0; - k->data.pl->cptState=0; - // k->y+=5; - k->ID=0; - - break; - - case 11: // BUILDER - { - //-DEBUG-printf("BUILDER ->"); - if(boom){ - k->ID=10;//BOMBER - if(!gInit->mapI.nuke){ - //-DEBUG-printf("Play Sound SND_EXPLODE\n"); - //GameController.sound.play(GameController.SND_EXPLODE); - } - break; - } - // check collision with stopper - err=turnedByStopper(k->x+lw/2,k->y+fy,k->data.pl->dir); - if(err==116){return 116;} - if(err!=2){ - k->data.pl->dir=err; - break; - } - /* if((k->data.pl->cptState==0)&& - (k->data.pl->cptBrick==0)){k->y-=5;}*/ - - k->data.pl->cptState++; - if(k->data.pl->cptState >= lState){ - // step created -> move up - k->data.pl->cptState=0; - k->data.pl->cptBrick++; - - if (k->data.pl->dir == 1) - { k->x += 4;} // step forward - else - { k->x -= 4;} - k->y -= 2;// step up - err = aboveGround(k->x+fx,k->y+fy);// should be 0, if not, we built into a wall -> stop - if(err==114){return 117;} - levitation=err; - // check for conversion to walker - err=checkPixelUP(8, k->x+fx,k->y+fy,fs); // check if builder is too close to ceiling - if(err==113){return 113;} - fa=err; - if(fa<8|| levitation > 0){ - // k->y+=5; - k->ID=0;//WALKER - // a lemming can jump through the ceiling like in Mayhem2-Boiler Room - if (levitation >= WALKER_OBSTACLE_HEIGHT) { - // avoid getting stuck - k->x = oldX; - k->y += 2; - } - k->data.pl->dir = (k->data.pl->dir==1) ? 0 : 1 ; - break; - } - if(k->data.pl->cptState >= STEPS_MAX){ - k->ID=12; //BUILDER_END - break; - } - } else { - if (k->data.pl->cptState==9){ - //-DEBUG-printf("insert mask\n"); - if(k->data.pl->cptBrick >= STEPS_WARNING){ - //-DEBUG-printf("Play Sound SND_TING\n"); - //GameController.sound.play(GameController.SND_TING); - } - } - } - break; - } - case 6: // STOPPER - { //-DEBUG-printf("STOPPER ->"); - if(boom){ - k->ID=10;//BOMBER - if(!gInit->mapI.nuke){ - //-DEBUG-printf("Play Sound SND_EXPLODE\n"); - //GameController.sound.play(GameController.SND_EXPLODE); - } - break; - } - //check for conversion to faller - free=checkPixelDOWN(FLOATER_STEP, k->x+fx,k->y+fy); - if(free==113){return 113;} - if(free>0){ - if(free==FALL_DISTANCE_FORCE_FALL) - {k->y += FALLER_STEP;} - else - {k->y += free;} // max: FALLER_STEP - k->data.pl->cptFall+=free; - if (k->data.pl->cptFall >= FALL_DISTANCE_FALL) - {k->ID=1;}//FALLER; - else - {k->ID=0;}//WALKER; - // conversion to faller or walker -> erase stopper mask - // Mask m = lemmings[getOrdinal(Type.STOPPER)].getMask(dir); - // m.clearType(maskX,maskY,0,Stencil.MSK_STOPPER); - } else { - k->data.pl->cptFall=0; - } - break; - } - case 21: //BOMBER_STOPPER: - //-DEBUG-printf("BOMBER_STOPPER ->"); - free=checkPixelDOWN(FLOATER_STEP, k->x+fx,k->y+fy); - if(free==113){return 113;} - if(free>0){ - k->ID=10;//BOMBER - } else {break;} - - case 10: // BOMBER - //-DEBUG-printf("BOMBER ->"); - if(k->data.pl->cptBoom==0){ - k->data.pl->cptBoom=404; - k->data.pl->cptState=gInit->mapI.lemmingDATA[10].state; - } - free=checkPixelDOWN(FLOATER_STEP, k->x+fx,k->y+fy); - if(free==113){return 113;} - if(free==FALL_DISTANCE_FORCE_FALL) - {k->y += FALLER_STEP;} - else - {k->y += free;} // max: FALLER_STEP - err=outOfLowerMap(k->y+fy); - if(err==114){return 114;} - if(err==1) {k->ID=17;} - if(k->data.pl->cptState==16){ - err=ereasePx(k->x,k->y,gInit->mapI.map.tabGif[10],gInit->mapI.map.cmap.bgColor); - if(err!=0){return err;} - //TODO - } - break; - - default: - //-DEBUG-printf("default ->"); - if(boom){ - explode (k->x+fx,k->y+fy,fs); - k->ID=10;//BOMBER - break; - } - break; - }//FIN SWITCH - - - - // trap - if(k->ID != 7 && k->ID != 8 && k->ID != 9 && k->ID != 17){ - err=get_pixel32(k->x+fx,k->y,pStencil); - if(err==ccc_error){return 12;} - ////-DEBUG-printf("k->ID => %d get_pixel32(k->x %d+fx %d => %d,k->y %d,pStencil) == %p \n",k->ID,k->x,fx,k->x+fx,k->y,err); - if(err==ccc_oTrapDrown){ - k->ID=7; - k->data.pl->cptState=gInit->mapI.lemmingDATA[k->ID].state; - } - if(err==ccc_oTrapAndLem){ - k->ID=8; - k->data.pl->cptState=gInit->mapI.lemmingDATA[k->ID].state; - } - if(err==ccc_oTrapAndNoLem){ - k->ID=17; - k->data.pl->cptState=0; - o=gInit->o.lo; - while(o!=NULL){ - ////-DEBUG-printf("gInit->mapI.map.tabDataSprO[%d].type=>%d\n(o->x%d<=(k->x+fx)%d)\n object->w %d >= (k->x+fx) %d\n o->y %d <= (k->y) %d\n",o->ID,gInit->mapI.map.tabDataSprO[o->ID].type,(o->x),(k->x+fx),gInit->mapI.map.tabGif[gInit->mapI.map.tiles*2+o->ID*2+o->data.po->UD+ADD_OBJ]->w,(k->x+fx),o->y,(k->y)); - if((gInit->mapI.map.tabDataSprO[o->ID].type==6)&& - (o->x<=(k->x+fx))&& - (gInit->mapI.map.tabGif[gInit->mapI.map.tiles*2+o->ID*2+o->data.po->UD+ADD_OBJ]->w+o->x >= (k->x+fx))&& - (o->y<=(k->y))&& - (gInit->mapI.map.tabGif[gInit->mapI.map.tiles*2+k->ID*2+k->data.po->UD+ADD_OBJ]->h+o->y >= (k->y))){ - o->data.po->cptState=gInit->mapI.map.tabDataSprO[o->ID].state; - break; - } - o=o->next; - } - } - if(err==ccc_oExit){ - k->ID=9; - k->data.pl->cptState=gInit->mapI.lemmingDATA[k->ID].state; - } - } - //-DEBUG-printf("\n"); - } - k=k->next; - }// FIN WHILE - - - return 0; -} - -int ereasePx(int x,int y,SDL_Surface *s,Uint32 bgColor){ - int err; - SDL_Rect from,to; - - from.x=0; - from.y=0; - from.w=s->w; - from.h=s->h; - - to.x=x; - to.y=y; - to.w=x+s->w; - to.h=y+s->h; - - if((err=paint_manip(s,from,pTerrain,to,bgColor,ccc_lemming,18))!=0){ - return err; - } - if((err=paint_manip(s,from,pStencilFixe,to,ccc_bgStencil,ccc_lemming,18))!=0){ - return err; - } - - - return 0; -} - -int print_num(SDL_Surface *dst, SDL_Surface *src, int x, int y, int value) -{ - char buf[9]; - int i,r,v,p=0; - int max = 1000000000; - SDL_Rect from; - if(value >= 1000000000){ - //-DEBUG-printf("value out of Bound %d\n",value); - return(15); - } - v=value; - - while(max > 1) - { - r = v / max; - v -= r * max; - max /= 10; - if(p || r) - buf[p++] = r; - } - buf[p++] = value%10; - - /* Render! */ - from.x = 0; - from.w = src->w; - from.h = src->h/10; - - for(i = 0; i < p; ++i) - { - SDL_Rect to; - to.x = x + i * (from.w + 2); - to.y = y; - from.y = buf[i] * (from.h); - test_blit(src, &from, dst, &to); - } - return 0; -} - -int paint_bomber(int x,int y,int xMax,SDL_Surface *cooldown,SDL_Surface *dst,int cpt){ - - SDL_Rect rect,to; - - to.x=x;//+xMax/2; - rect.x=0; - rect.h=cooldown->h/5; - rect.y= rect.h * (cpt-1); - to.y=y-rect.h; - rect.w=cooldown->w; - - test_blit(cooldown, &rect, dst, &to); - - return 0; -} - - -int testAlpha(SDL_Surface *dst, SDL_Surface *src,SDL_Surface *src2, int x, int y){ - SDL_Rect from; - SDL_Rect to; - int i; - - from.x = 0; - from.w = src->w; - from.h = src->h/188; - - for(i = 0; i < 188; ++i) - { - from.y = i * (from.h ) ; - to.x = x + (i%40)*16; - to.y = y + from.h * (i/40); - test_blit(src, &from, dst, &to); - //print_num(screen, src2, to.x + 20, to.y, i); - } - - return 0; - -} - -int print_alpha(SDL_Surface *dst, SDL_Surface *src, int x, int y, char* msg) -{ - SDL_Rect from; - SDL_Rect to; - int i; - int n; - - from.x = 0; - from.w = src->w; - from.h = src->h/188; - - for(i = 0; i < strlen(msg); ++i) - { - n=msg[i]; - ////-DEBUG-printf("-> %d ~ %c\n",n,n); - from.y = 404; - if (n > 32 && n < 127) { from.y = (n - 33) * (from.h) ;} - //if (n == 32) {continue;} - to.x = x + i * (from.w); - to.y = y; - if(from.y!=404){ - test_blit(src, &from, dst, &to); - } - } - return 0; -} - - -int init() -{ - - //Initialisation de tous les sous-systèmes de SDL - if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) - { - return 90; - } - - //Mise en place de l'écran - screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE | SDL_DOUBLEBUF ); - - //-DEBUG-printf("DEBUG ludo : screen->w==%i, screen->h==%i\n", screen->w, screen->h); - //S'il y a une erreur lors de la mise en place de l'écran - if( screen == NULL ) { - //-DEBUG-printf("problème\n"); - return 2; - } - - pTerrain = SDL_CreateRGBSurface(SDL_HWSURFACE, LEVEL_WIDTH, LEVEL_HEIGHT, - SCREEN_BPP,0,0,0,0);//(ccc_black>> 16) & 0xff, (ccc_black>> 8)&0xff,(ccc_black& 0xff),0); - if( pTerrain == NULL ) { - return 3; - } - - pSpr_Lem = SDL_CreateRGBSurface(SDL_HWSURFACE, LEVEL_WIDTH, LEVEL_HEIGHT, - SCREEN_BPP,0,0,0,0); - - if( pSpr_Lem == NULL ) { - return 3; - } - - pStencil = SDL_CreateRGBSurface(SDL_SWSURFACE, LEVEL_WIDTH, LEVEL_HEIGHT, - SCREEN_BPP,0,0,0,0); - if( pStencil == NULL ) { - return 3; - } - - pStencilFixe = SDL_CreateRGBSurface(SDL_SWSURFACE, LEVEL_WIDTH, LEVEL_HEIGHT, - SCREEN_BPP,0,0,0,0); - if( pStencil == NULL ) { - return 3; - } - //Mise en place de la barre caption - SDL_WM_SetCaption( "Test_LVL", NULL ); - //No pointer of mouse - SDL_ShowCursor(0); - - //Si tout s'est bien passé - return 0; -} - -SDL_Surface* flipSurfaceUD_LR(SDL_Surface* src, enum sens sensO, Uint32 c) { - int i,j; - - SDL_Surface* copy_surface = NULL; - - // This only works for 32 bit pixel format - if( src->format->BitsPerPixel == 32 ) { - // This surface must be freed by the caller. - copy_surface = SDL_CreateRGBSurface(src->flags, src->w, src->h, - src->format->BitsPerPixel, - - src->format->Rmask, - src->format->Gmask, - src->format->Bmask, - src->format->Amask); - - SDL_SetColorKey( copy_surface, - SDL_RLEACCEL | SDL_SRCCOLORKEY, - SDL_MapRGB/*A*/( copy_surface->format, - (c >> 16) & 0xff, - (c >> 8) & 0xff, - (c & 0xff)//, - //(c >> 24) & 0xff - ) - ); - - //FIXME : lock surface - Uint32 *source = src->pixels; - Uint32 *dest = copy_surface->pixels; - - for(i = 0; i < src->h; i++) { - for(j = 0; j < src->w; j++) { - if (sensO == UD){// Sens UD - dest[ (src->h-i-1)*src->w + j ] = source[ i*src->w + j]; - } - if (sensO == LR){// Sens LR - dest[ i*src->w + (src->w-j-1) ] = source[ i*src->w + j]; - } - } - } - } - - return copy_surface; -} - -SDL_Surface *load_image( char* filename, Uint32 cbg ) -{ - //L'image qui est chargée - SDL_Surface* loadedImage = NULL; - - //L'image optimisée qu'on va utiliser - SDL_Surface* optimizedImage = NULL; - - //Chargement de l'image - loadedImage = IMG_Load( filename ); - - //Si l'image est chargée - if( loadedImage != NULL ) - { - //Création de l'image optimisée - optimizedImage = SDL_DisplayFormat( loadedImage ); - - - //Libération de l'ancienne image - SDL_FreeSurface( loadedImage ); - - //Si la création de l'image optimisée s'est bien passée - if( optimizedImage != NULL ) - { - SDL_SetColorKey( optimizedImage, - SDL_RLEACCEL | SDL_SRCCOLORKEY, - SDL_MapRGB( optimizedImage->format, - (cbg >> 16) & 0xff, - (cbg >> 8) & 0xff, - (cbg & 0xff)//, - //(cbg >> 24) & 0xff - ) - ); - } - } - - //On retourne l'image optimisée - return optimizedImage; - - return loadedImage; -} - - -int load_fields(char *folder, struct gameInit *gInit, int tabNum[255] ) { - int i, lt,total,cpt=-1,llem; - char *temp; - char *filepath; - char *folderLem; - - temp = tabString_eMapStyle[gInit->mapI.map.style]; - lt = strlen(temp); - - llem= strlen("../../../misc/"); - folderLem=malloc(sizeof(char)*(llem+1)); - sprintf(folderLem,"%s","../../../misc/"); - - total=(LEM_JOB*2) + (gInit->mapI.map.tiles)*2 + gInit->mapI.map.tilesSteel + ((gInit->mapI.map.tilesObjet)*2); - - gInit->mapI.map.tabGif=malloc((total + ADD_OBJ) * sizeof(SDL_Surface *)); - - - //stored terrains - cpt=ADD_OBJ; - - filepath = malloc(sizeof(char)*(strlen(folder)+lt+strlen("/_.gif")+(sizeof(int)*10))); - - for(i=0; i < gInit->mapI.map.tiles*2 ; i+=2) { - sprintf(filepath, "%s/%s%s%d.gif", folder,temp,"_", i/2); - gInit->mapI.map.tabGif[i+cpt] = load_image(filepath,ccc_keyGif); - gInit->mapI.map.tabGif[i+1+cpt] = flipSurfaceUD_LR(load_image(filepath,ccc_keyGif),UD,ccc_keyGif); - if((gInit->mapI.map.tabGif[i+cpt] == NULL)||(gInit->mapI.map.tabGif[i+1+cpt] == NULL)) { - //-DEBUG-printf("ERREUR load_file Objet: %s\n", filepath); - return(16); - } else { - //-DEBUG-printf("load_file ok : %i -> '%s' + flip_LR\n", i+cpt, filepath); - } - } - free(filepath); - //stored objet - cpt=ADD_OBJ+gInit->mapI.map.tiles*2; - - filepath = malloc(sizeof(char)*(strlen(folder)+lt+strlen("/o_.gif")+(sizeof(int)*10))); - - for(i=0; i < gInit->mapI.map.tilesObjet*2 ; i+=2) { - sprintf(filepath, "%s/%s%s%d.gif", folder,temp,"o_", i/2); - gInit->mapI.map.tabGif[i+cpt] = load_image(filepath,ccc_keyGif); - gInit->mapI.map.tabGif[i+1+cpt] = flipSurfaceUD_LR(load_image(filepath,ccc_keyGif),UD,ccc_keyGif); - if((gInit->mapI.map.tabGif[i+cpt] == NULL)||(gInit->mapI.map.tabGif[i+1+cpt] == NULL)) { - //-DEBUG-printf("ERREUR load_file Objet: %s\n", filepath); - return(17); - } else { - ////-DEBUG-printf("load_file ok : %i -> '%s' + flip_LR\n", i+cpt, filepath); - } - } - free(filepath); - //stored steel - cpt=ADD_OBJ+(gInit->mapI.map.tiles*2)+(gInit->mapI.map.tilesObjet*2); - - filepath = malloc(sizeof(char)*(strlen(folder)+lt+strlen("/om_.gif")+(sizeof(int)*10))); - - for(i=0; i < gInit->mapI.map.tilesSteel ; ++i) { - sprintf(filepath, "%s/%s%s%d.gif", folder,temp,"om_", tabNum[i]); - gInit->mapI.map.tabGif[i+cpt] = load_image(filepath,ccc_keyGif); - if(gInit->mapI.map.tabGif[i+cpt] == NULL) { - //-DEBUG-printf("ERREUR load_file steel: %s\n", filepath); - return(18); - } else { - ////-DEBUG-printf("load_file ok : %i -> '%s'\n", i+cpt, filepath); - } - } - free(filepath); - //stored lemmingsDATA - cpt=ADD_OBJ+(gInit->mapI.map.tilesObjet*2)+(gInit->mapI.map.tiles)*2+gInit->mapI.map.tilesSteel; - - filepath = malloc(sizeof(char)*(strlen(folderLem)+strlen("lemm")+strlen("/_.gif")+(sizeof(int)*10))); - - for(i=0; i < LEM_JOB*2 ; i+=2) { - sprintf(filepath, "%s/%s%d.gif", folderLem,"lemm_",i/2); - gInit->mapI.map.tabGif[i+1+cpt] = load_image(filepath,ccc_lemming); - gInit->mapI.map.tabGif[i+cpt] = flipSurfaceUD_LR(load_image(filepath,ccc_lemming),LR,ccc_lemming); - if((gInit->mapI.map.tabGif[i+cpt] == NULL)||(gInit->mapI.map.tabGif[i+1+cpt] == NULL)) { - //-DEBUG-printf("ERREUR load_file lemmingsDATA: %s\n", filepath); - return(19); - } else { - ////-DEBUG-printf("load_file ok : %i -> '%s' + flip_LR\n", i+cpt, filepath); - } - } - free(filepath); - //0: lemmfont.gif - gInit->mapI.map.tabGif[0]= load_image("../../../misc/lemmfont.gif",gInit->mapI.map.cmap.bgColor); - if ( gInit->mapI.map.tabGif[0] == NULL ) { - //-DEBUG-printf("ERREUR load_file lemmfont.gif: %s\n", "../../../misc/lemmfont.gif"); - return(20); - } - //1: numfont.gif - gInit->mapI.map.tabGif[1]= load_image("../../../misc/numfont.gif",gInit->mapI.map.cmap.bgColor); - if ( gInit->mapI.map.tabGif[1] == NULL ) { - //-DEBUG-printf("ERREUR load_file numfont.gif: %s\n", "../../../misc/numfont.gif"); - return(21); - } - //2: countdown.gif - gInit->mapI.map.tabGif[2]= load_image("../../../misc/countdown.gif",ccc_lemming); - if ( gInit->mapI.map.tabGif[2] == NULL ) { - //-DEBUG-printf("ERREUR load_file countdown.gif: %s\n", "../../../misc/countdown.gif"); - return(22); - } - //3: cursor.gif - gInit->mapI.map.tabGif[3]= load_image("../../../misc/cursor.gif",ccc_cursor);//gInit->mapI.map.cmap.bgColor); - if ( gInit->mapI.map.tabGif[3] == NULL ) { - //-DEBUG-printf("ERREUR load_file cursor.gif: %s\n", "../../../misc/cursor.gif"); - return(23); - } - //4: explode.gif - gInit->mapI.map.tabGif[4]= load_image("../../../misc/explode.gif",gInit->mapI.map.cmap.bgColor); - if ( gInit->mapI.map.tabGif[4] == NULL ) { - //-DEBUG-printf("ERREUR load_file explode.gif: %s\n", "../../../misc/explode.gif"); - return(24); - } - //5: border.gif - gInit->mapI.map.tabGif[5]= load_image("../../../misc/border.gif",gInit->mapI.map.cmap.bgColor); - if ( gInit->mapI.map.tabGif[5] == NULL ) { - //-DEBUG-printf("ERREUR load_file border.gif: %s\n", "../../../misc/border.gif"); - return(25); - } - //6: replay.gif - gInit->mapI.map.tabGif[6]= load_image("../../../misc/replay.gif",gInit->mapI.map.cmap.bgColor); - if ( gInit->mapI.map.tabGif[6] == NULL ) { - //-DEBUG-printf("ERREUR load_file replay.gif: %s\n", "../../../misc/replay.gif"); - return(26); - } - //7: select.gif - gInit->mapI.map.tabGif[7]= load_image("../../../misc/select.gif",gInit->mapI.map.cmap.bgColor); - if ( gInit->mapI.map.tabGif[7] == NULL ) { - //-DEBUG-printf("ERREUR load_file select.gif: %s\n", "../../../misc/select.gif"); - return(27); - } - // 8: font7x10.bmp - gInit->mapI.map.tabGif[8]= load_image("./font7x10.bmp",gInit->mapI.map.cmap.bgColor); - if ( gInit->mapI.map.tabGif[8] == NULL ) { - //-DEBUG-printf("ERREUR load_file font7x10.bmp: %s\n", "./font7x10.bmp"); - return(28); - } - // 9: alphabet.gif - gInit->mapI.map.tabGif[9]= load_image("./alphabet.gif",ccc_cursor); - if ( gInit->mapI.map.tabGif[9] == NULL ) { - //-DEBUG-printf("ERREUR load_file alphabet.gif: %s\n", "./alphabet.gif"); - return(29); - } - //10: mask_10.gif - gInit->mapI.map.tabGif[10]= load_image("../../../misc/mask_10.gif",ccc_cursor); - if ( gInit->mapI.map.tabGif[10] == NULL ) { - //-DEBUG-printf("ERREUR load_file mask_10.gif: %s\n", "../../../misc/mask_10.gif"); - return(30); - } - //11: mask_11.gif - gInit->mapI.map.tabGif[11]= load_image("../../../misc/mask_11.gif",ccc_cursor); - if ( gInit->mapI.map.tabGif[11] == NULL ) { - //-DEBUG-printf("ERREUR load_file mask_11.gif: %s\n", "../../../misc/mask_11.gif"); - return(31); - } - //12: mask_6.gif - gInit->mapI.map.tabGif[12]= load_image("../../../misc/mask_6.gif",ccc_cursor); - if ( gInit->mapI.map.tabGif[12] == NULL ) { - //-DEBUG-printf("ERREUR load_file mask_6.gif: %s\n", "../../../misc/mask_6.gif"); - return(32); - } - //13: mask_13.gif - gInit->mapI.map.tabGif[13]= load_image("../../../misc/mask_13.gif",ccc_cursor); - if ( gInit->mapI.map.tabGif[13] == NULL ) { - //-DEBUG-printf("ERREUR load_file mask_13.gif: %s\n", "../../../misc/mask_13.gif"); - return(33); - } - //14: mask_14.gif - gInit->mapI.map.tabGif[14]= load_image("../../../misc/mask_14.gif",ccc_cursor); - if ( gInit->mapI.map.tabGif[14] == NULL ) { - //-DEBUG-printf("ERREUR load_file mask_14.gif: %s\n", "../../../misc/mask_14.gif"); - return(34); - } - //15: mask_15.gif - gInit->mapI.map.tabGif[15]= load_image("../../../misc/mask_15.gif",ccc_cursor); - if ( gInit->mapI.map.tabGif[15] == NULL ) { - //-DEBUG-printf("ERREUR load_file mask_15.gif: %s\n", "../../../misc/mask_15.gif"); - return(35); - } - //16: imask_13.gif - gInit->mapI.map.tabGif[16]= load_image("../../../misc/imask_13.gif",ccc_cursor); - if ( gInit->mapI.map.tabGif[16] == NULL ) { - //-DEBUG-printf("ERREUR load_file imask_13.gif: %s\n", "../../../misc/imask_13.gif"); - return(36); - } - //17: imask_14.gif - gInit->mapI.map.tabGif[17]= load_image("../../../misc/imask_14.gif",ccc_cursor); - if ( gInit->mapI.map.tabGif[17] == NULL ) { - //-DEBUG-printf("ERREUR load_file imask_14.gif: %s\n", "../../../misc/imask_14.gif"); - return(37); - } - //18: imask_15.gif - gInit->mapI.map.tabGif[18]= load_image("../../../misc/imask_15.gif",ccc_cursor); - if ( gInit->mapI.map.tabGif[18] == NULL ) { - //-DEBUG-printf("ERREUR load_file imask_15.gif: %s\n", "../../../misc/imask_15.gif"); - return(38); - } - - //icone.gif - cpt=NBR_ADD_OBJ; - filepath = malloc(sizeof(char)*(strlen(folderLem)+strlen("icon")+strlen("/__alt.gif")+(sizeof(int)*10))); - - for(i=0; i < NBR_BUTTON_LEMMING ; ++i) { - sprintf(filepath, "%s/%s%d.gif", folderLem,"icon_",i); - //if(i<7){sprintf(filepath, "%s/%s%d.gif", folderLem,"icon_",i);} - //if(i==7){sprintf(filepath, "%s/%s.gif", folderLem,"icon_6_alt");} - //if(i>7){sprintf(filepath, "%s/%s%d.gif", folderLem,"icon_",i-1);} - gInit->mapI.map.tabGif[i+cpt] = load_image(filepath,ccc_black); - if(gInit->mapI.map.tabGif[i+cpt] == NULL) { - //-DEBUG-printf("ERREUR load_file icon: %s\n", filepath); - return(31); - } else { - ////-DEBUG-printf("load_file ok : %i -> '%s' + flip_LR\n", i+cpt, filepath); - } - } - free(filepath); - free(folderLem); - - return 0; -} - - -int load_files(struct gameInit *gInit) -{ - int lt,ltt,ltlem; - int err=0; - int res, lterrain, lobjet, lsteel; - char *folder; - char *spriteINI; - char *lemINI; - char *terrainGif; - char *objetGif; - char *steelGif; - char *temp; - int tabNum[255]; - struct dirent *lecture; - DIR *rep; - - yyparse(gInit); - - fclose(yyin); - //-DEBUG-printf("CLOSE 1\n"); - - - lt = (strlen("../../../styles/")); - ltlem = (strlen("../../../misc/lemming.ini")); - ltt = (strlen(tabString_eMapStyle[gInit->mapI.map.style])); - - folder = malloc(sizeof(char)*(lt+ltt+1)); - spriteINI = malloc(sizeof(char)*(lt+ltt*2+5));// *2:dossier/fichier, +4: .ini, +1: string - lemINI = malloc(sizeof(char)*ltlem+1); - - - //folder = "/home/jazzblue/Bureau/Projet-Lemmings/trunk/styles/dirt"; - ////-DEBUG-printf("test segFault\n"); - sprintf(folder,"../../../styles/%s",tabString_eMapStyle[gInit->mapI.map.style]); - sprintf(spriteINI,"%s/%s.ini",folder,tabString_eMapStyle[gInit->mapI.map.style]); - sprintf(lemINI,"%s","../../../misc/lemming.ini"); - ////-DEBUG-printf("test2\n"); - - temp = tabString_eMapStyle[gInit->mapI.map.style]; - lt = strlen(temp); - lterrain = lt+1; - lobjet = lt+2; - lsteel = lt+3; - terrainGif=malloc(sizeof(char)*(lterrain+1)); - objetGif=malloc(sizeof(char)*(lobjet+1)); - steelGif=malloc(sizeof(char)*(lsteel+1)); - sprintf(terrainGif,"%s_",temp); - sprintf(objetGif,"%so_",temp); - sprintf(steelGif,"%som_",temp); - - // Compter les textures de terrain - rep = opendir(folder); - if (rep==NULL) { - //-DEBUG-printf("Erreur opendir('%s')\n", folder); - return(31); - } - - while ((lecture = readdir(rep))) { - if( (res=strncmp(terrainGif, lecture->d_name,lterrain)) == 0 ){ - ++(gInit->mapI.map.tiles); - } else { - if( (res=strncmp(objetGif, lecture->d_name,lobjet)) == 0 ) { - ++(gInit->mapI.map.tilesObjet); - } else { - if ( (res=strncmp(steelGif, lecture->d_name,lsteel)) == 0 ) { - sscanf(lecture->d_name+lsteel, "%i", &(tabNum[gInit->mapI.map.tilesSteel])); - ++(gInit->mapI.map.tilesSteel); - - } else { - //-DEBUG-printf("BUG : type de fichier non reconnu [%s] \n", lecture->d_name); - } - } - } - } - closedir(rep); - - - - yyin=fopen(spriteINI, "r"); - if (yyin==NULL) { fprintf(stderr,"Filename INVALIDE: %s\n",spriteINI); - perror("Impossible d'ouvrir le fichier de configuration des sprite"); return(32); - } - yyparse(gInit); - fclose(yyin); - //-DEBUG-printf("CLOSE 2\n"); - - yyin=fopen(lemINI, "r"); - if (yyin==NULL) { fprintf(stderr,"Filename INVALIDE: %s\n",lemINI); - perror("Impossible d'ouvrir le fichier de configuration des lemmings"); return(33); - } - yyparse(gInit); - fclose(yyin); - //-DEBUG-printf("CLOSE 3\n"); - - err=load_fields(folder, gInit, tabNum); - if(err!=0){return err;} - - ////-DEBUG-printf("REVERSE t.lt\n"); - gInit->t.lt=(struct listeSimplementChainee*)rev_listeO(gInit->t.lt); - ////-DEBUG-printf("REVERSE o.lo\n"); - gInit->o.lo=(struct listeSimplementChainee*)rev_listeO(gInit->o.lo); - ////-DEBUG-printf("REVERSE s.ls\n"); - gInit->s.ls=(struct listeSimplementChainee*)rev_listeO(gInit->s.ls); - - // AFFICHAGE DES INFOS DE LA MAP - err=afficher(gInit); - if(err!=0){return err;} - - //Si tout s'est bien passé - free(terrainGif); - free(objetGif); - free(steelGif); - free(folder); - free(spriteINI); - free(lemINI); - - return 0; -} - -int afficher(struct gameInit *gInit){ - - int i; - struct listeSimplementChainee *k; - - printf("AFFICHAGE DE GAME INIT\n"); - printf("LES PARAMETRES:\n"); - for(i=0;i<IDENT_COUNT;++i){ - printf("\t%s = %d\n",tabString_eParaMap[i],gInit->mapI.paraMap[i]); - } - printf("\txPos = %d\n",gInit->mapI.xPos); - printf("\tstyle = %s\n",tabString_eMapStyle[gInit->mapI.map.style]); - - printf("LE NOM DU NIVEAU:\n"); - printf("name = %s\n",gInit->mapI.name); - printf("LES COULEURS DU NIVEAU:\n"); - printf("bgColor R = %X G = %X B = %X\n", - ((gInit->mapI.map.cmap.bgColor>>16) & 0xff), - ((gInit->mapI.map.cmap.bgColor>>8) & 0xff), - (gInit->mapI.map.cmap.bgColor & 0xff) ); - printf("debrisColor R = %X G = %X B = %X\n", - ((gInit->mapI.map.cmap.debrisColor>>16) & 0xff), - ((gInit->mapI.map.cmap.debrisColor>>8) & 0xff), - (gInit->mapI.map.cmap.debrisColor & 0xff) ); - printf("debrisColor:\n"); - for(i=0;i<PARTICULE_COUNT;++i){ - printf("\tP%d: R = %X G = %X B = %X\n",i, - ((gInit->mapI.map.cmap.particleColor[i]>>16) & 0xff), - ((gInit->mapI.map.cmap.particleColor[i]>>8) & 0xff), - (gInit->mapI.map.cmap.particleColor[i] & 0xff) ); - - } - - printf("TERRAINS:\n"); - k=gInit->t.lt; - i=0; - while ( k !=NULL ) { - if(k->data.pt != NULL){//{printf("paraTerrain non initialiser\n");return(34);} - printf("\tterrain_%d = %d, %d, %d, %d\n",i,k->ID, k->x, k->y,k->data.pt->modif); - } - ++i; - k=k->next; - } - - printf("OBJETS:\n"); - - k=gInit->o.lo; - i=0; - while ( k !=NULL ) { - if(k->data.po != NULL){ //{printf("paraObjet non initialiser\n");return(35);} - printf("\tobject_%d = %d, %d, %d, %d, %d\n",i,k->ID, k->x, k->y, k->data.po->paintMode,k->data.po->UD); - } - ++i; - k=k->next; - } - - - printf("STEELS:\n"); - k=gInit->s.ls; - i=0; - while ( k !=NULL ) { - if(k->data.ps != NULL){//{printf("paraSteel non initialiser\n");return(36);} - printf("\tsteel_%d = %d, %d, %d, %d, %d\n",i,k->ID=i, k->x, k->y, k->data.ps->w,k->data.ps->h); // FIXME big fuck here with "=" +#include "fonctions_integrees.c" +#include "fonctions_non_integrees.c" + + +int main (int argc, char **argv) +{ + int res,i,num,lt; + char *temp,*temp0; + //Uint32 ctest; + //int j,k; + //argc=3; + //argv[1]="1"; + //argv[2]="72"; + if (argc<2 || argc>3) { + fprintf(stderr, "Usage %s [Option] <Filename>\n",argv[0]); + fprintf(stderr,"\nOption:\n\t- 1: on se place dans le dossier 1_orig\n"); + fprintf(stderr,"\t- 2: on se place dans le dossier 2_ohno\n"); + fprintf(stderr,"Filename:\n\t- numero du lvl (ex: 1, 4, 13, 65b )\n"); + return(44); + } + if (argc==3) { + i=1; + num=atoi(argv[2]); + if (num >= 10) ++i; + if (num >= 100) ++i; + if (num >= 1000){ + fprintf(stderr,"Too many lvl (%d > 999)\n",num); + return(45); + } + temp0=malloc(sizeof(char)*(3-i));// => [00] [0] [] + switch(i){ + case 1 : temp0 = "00";break; + case 2 : temp0 = "0";break; + case 3 : temp0 = "";break; + default:fprintf(stderr,"BUG: i[=%d] > 3 ERREUR\n",i);return(46); + } + + lt = (strlen("../../../../trunk/level/1_orig/lvl.ini")+5);// + 4 : 4 chiffres lvl.ini, +1 : string + temp=malloc(sizeof(char)*lt); + switch(atoi(argv[1])){ + case 1 : sprintf(temp,"../../../../trunk/level/1_orig/lvl0%s%d.ini",temp0,num);break; + case 2 : sprintf(temp,"../../../../trunk/level/2_ohno/lvl1%s%d.ini",temp0,num);break; + default:fprintf(stderr,"ERREUR: dossier [%d] inconnu\n",atoi(argv[1]));return(47); } - ++i; - k=k->next; - } - - printf("DATA SPRITES OBJET:\n"); - for(i=0;i<gInit->mapI.map.tilesObjet;++i){ - printf("spr_%d:\n\tframes_%d = %d\n\tanim_%d = %d\n\ttype_%d = %d\n\tsound_%d = %d\n",i,i,gInit->mapI.map.tabDataSprO[i].state,i,gInit->mapI.map.tabDataSprO[i].anim,i,gInit->mapI.map.tabDataSprO[i].type,i,gInit->mapI.map.tabDataSprO[i].sound); - } - - printf("DATA JOB LEMMINGS:\n"); - for(i=0;i<LEM_JOB;++i){ - printf("lem_%d:\n\tlemm_%d = %d, %d, %d\n\tpos_%d = %d, %d, %d\n",i,i,gInit->mapI.lemmingDATA[i].state,gInit->mapI.lemmingDATA[i].dir,gInit->mapI.lemmingDATA[i].anim,i,gInit->mapI.lemmingDATA[i].footX,gInit->mapI.lemmingDATA[i].footY,gInit->mapI.lemmingDATA[i].footSize); - - if (gInit->mapI.lemmingDATA[i].mask != NULL){ - printf("\tmask_%d = %d, %d, %d\n",i,gInit->mapI.lemmingDATA[i].mask->stateM,gInit->mapI.lemmingDATA[i].mask->dirM,gInit->mapI.lemmingDATA[i].mask->cooldown); - } - - if (gInit->mapI.lemmingDATA[i].imask != NULL){ - printf("\timask_%d = %d , %d\n",i,gInit->mapI.lemmingDATA[i].imask->stateI,gInit->mapI.lemmingDATA[i].imask->dirI); - } - } - return 0; -} - -int clean_up() -{ - //On libère la feuille de sprites - // SDL_FreeSurface( faces ); - SDL_FreeSurface( pTerrain ); - //On quitte SDL - SDL_Quit(); - - return 0; -} - -int initGame(struct gameInit *gInit){ - - int i; - - // init liste - gInit->t.nbr=0; - gInit->o.nbr=0; - gInit->s.nbr=0; - gInit->l.nbrInput=0; - gInit->l.nbr=0; - gInit->l.nbrDied=0; - gInit->l.nbrSafe=0; - - gInit->t.lt=NULL; - gInit->o.lo=NULL; - gInit->s.ls=NULL; - gInit->l.ll=NULL; - - // init button state - for(i=0;i<NBR_CPT;++i){ - gInit->cptGame[i]=0; - } - gInit->cptGame[17]=404; - - // init InfoMap - // map name - gInit->mapI.name = NULL; - - // paraMap - for(i=0;i<IDENT_COUNT;++i){ - gInit->mapI.paraMap[i]=0; - } - // xPos - gInit->mapI.xPos=0; - - //MaxFallDistance - gInit->mapI.nbrEntry=0; - gInit->mapI.MaxFallDistance = 126; - gInit->mapI.nuke=0; - - // init MapStyle - gInit->mapI.map.style=undefined; - gInit->mapI.map.superLemming=0; - gInit->mapI.map.tiles=0; - gInit->mapI.map.tilesObjet=0; - gInit->mapI.map.tilesSteel=0; - gInit->mapI.map.tabDataSprO=NULL; - gInit->mapI.map.tabGif=NULL; - - // lemmingJobAndSprite - gInit->mapI.lemmingDATA=NULL; - - return 0; -} - -int test_O_UD(int UD){ - - switch(UD){ - case 4 : return 1;break; - case 6 : return 1;break; - case 12 : return 1;break; - case 14 : return 1;break; - default: return 0;break; - } - return 0; -} - -int creationLemming(struct gameInit *gInit){ - int nbLem; - struct listeSimplementChainee *k=NULL; - struct listeSimplementChainee *kTemp=NULL; - SDL_Surface *sf=NULL; - - int coorE[10][2]; - int i=0; - //DEBUT TEST CREATION UN LEMMING - - // DEBUT DEBUG: LEMMING COLLISION - //gInit->mapI.paraMap[1]=2; - // FIN DEBUG: LEMMING COLLISION - - nbLem=gInit->mapI.paraMap[1]; - //nbLem=1; - ////-DEBUG-printf("nbLem %d\n",nbLem); - - if(gInit->mapI.nbrEntry==0){ - //-DEBUG-printf("ERREUR ALGO: nbrEntry == 0\n"); - return(37); - } - - - // PREMIER CAS - gInit->l.nbrInput=1; - gInit->l.ll = malloc(sizeof(struct listeSimplementChainee)); - gInit->l.ll->ID=18; - - k=gInit->o.lo; - while ( k !=NULL ) { - if( k->ID == 1 ){// Entry detecte - sf = gInit->mapI.map.tabGif[gInit->mapI.map.tiles*2+k->ID*2+k->data.po->UD+ADD_OBJ]; - coorE[i][0]=k->x+(sf->w/2)-16; - coorE[i][1]=k->y; - ++i; - if(i>=gInit->mapI.nbrEntry){break;} - if(i>9){//-DEBUG-printf("ERREUR: too many entry\n"); - return(38);} - } - k=k->next; - } - if(k == NULL){ - //-DEBUG-printf("BUG: NO ENTRY IN THIS LEVEL ...\n"); - return(39); - } - gInit->l.ll->x=coorE[0][0]; - gInit->l.ll->y=coorE[0][1]; - - gInit->l.ll->data.pt=NULL; - gInit->l.ll->data.po=NULL; - gInit->l.ll->data.ps=NULL; - gInit->l.ll->data.pl=malloc(sizeof(struct paraLemming)); - - gInit->l.ll->data.pl->dir=1; - gInit->l.ll->data.pl->climber=0; - gInit->l.ll->data.pl->blocker=0; - gInit->l.ll->data.pl->bomber=404; - gInit->l.ll->data.pl->floater=0; - gInit->l.ll->data.pl->cptFall=0; - gInit->l.ll->data.pl->cptJump=0; - gInit->l.ll->data.pl->nuke=0; - gInit->l.ll->data.pl->cptState=0; - gInit->l.ll->data.pl->cptBoom=404; - gInit->l.ll->data.pl->cptFall2=0; - gInit->l.ll->data.pl->cptBrick=0; - - gInit->l.ll->next=NULL; - if(gInit->mapI.nbrEntry==1){i=0;} - else {i=1;} - while(--nbLem > 0){ - // TOUS LES AUTRES CAS - - kTemp=malloc(sizeof(struct listeSimplementChainee)); - kTemp=gInit->l.ll; - - gInit->l.ll=NULL; - gInit->l.ll=malloc(sizeof(struct listeSimplementChainee)); - - gInit->l.ll->next=NULL; - gInit->l.ll->next=malloc(sizeof(struct listeSimplementChainee)); - - gInit->l.ll->next=kTemp; - - gInit->l.nbrInput += 1; - gInit->l.ll->ID=18; - - gInit->l.ll->x=coorE[i%gInit->mapI.nbrEntry][0]; - gInit->l.ll->y=coorE[i%gInit->mapI.nbrEntry][1]; - - gInit->l.ll->data.pl=malloc(sizeof(struct paraLemming)); - gInit->l.ll->data.pl->dir=1; - gInit->l.ll->data.pl->climber=0; - gInit->l.ll->data.pl->blocker=0; - gInit->l.ll->data.pl->bomber=404; - gInit->l.ll->data.pl->floater=0; - gInit->l.ll->data.pl->cptFall=0; - gInit->l.ll->data.pl->cptJump=0; - gInit->l.ll->data.pl->nuke=0; - gInit->l.ll->data.pl->cptState=0; - gInit->l.ll->data.pl->cptBoom=404; - gInit->l.ll->data.pl->cptFall2=0; - gInit->l.ll->data.pl->cptBrick=0; - if(gInit->mapI.nbrEntry==1){i=0;} - else {++i;} - } - //FIN TEST CREATION UN LEMMING - - ////-DEBUG-printf("REVERSE l.ll\n"); - gInit->l.ll=(struct listeSimplementChainee*)rev_listeO(gInit->l.ll); - - return 0; -} - -int paint_terrain (struct gameInit *gInit, int choix){ - struct listeSimplementChainee *k=NULL; - SDL_Rect offset; - SDL_Surface *sf; - int err=0; - int overwrite=0; - int remove=0; - int paint=0; - int tx,ty=0; - int cpt=0; - int ok=0; - Uint32 color; - - // //-DEBUG-printf("LES TERRAINS : "); - k=gInit->t.lt; - while ( k !=NULL ) { - offset.x = k->x; - offset.y = k->y; - - sf = gInit->mapI.map.tabGif[test_O_UD(k->data.pt->modif)+k->ID*2+ADD_OBJ]; - - if((k->data.pt->modif != 0)&& - (k->data.pt->modif != 2)&& - (k->data.pt->modif != 4)&& - (k->data.pt->modif != 6)&& - (k->data.pt->modif != 8)&& - (k->data.pt->modif != 10)&& - (k->data.pt->modif != 12)&& - (k->data.pt->modif != 14)&& - (k->data.pt->modif != 15) - ) { - //-DEBUG-printf("WTF ... k->data.pt->modif == %d\n",k->data.pt->modif); - return(40); - } - for(ty=k->y;ty<sf->h+k->y;++ty){ - - if(ty<0) {ty=-1;continue;} - if(ty>=pTerrain->h){break;} - if(ty>=LEVEL_HEIGHT) {break;} - - for(tx=k->x;tx<sf->w+k->x;++tx){ - - if(tx<0) {tx=-1;continue;} - if(tx>=pTerrain->w){break;} - if(tx>=LEVEL_WIDTH) {break;} - - overwrite = ((k->data.pt->modif & 8) == 8)? 0 : 1; - remove = ((k->data.pt->modif & 2) == 2)? 1 : 0; - if(k->data.pt->modif == 15){ - overwrite=0; - remove=0; - } - - - color = get_pixel32(tx-k->x,ty-k->y,sf); - if(color==ccc_error){return 250;} - if (color == ccc_black) - { //putPixel(pStencil,tx,ty , ccc_tEmpty ); - continue; - } - paint=0; - if(!overwrite){ - if((get_pixel32(tx,ty,pStencil)==ccc_error)){return 67;} - // don't overwrite -> only paint if background is transparent - if(((get_pixel32(tx,ty,pStencil)>>24) & 0xff ) == 0) - {// ccc_tEmpty){ - paint=1; - } - } else if(remove){ - // if(((get_pixel32(tx,ty,pStencil)) & ccc_tFull) != 0){ - err=putPixel(pTerrain,tx,ty , gInit->mapI.map.cmap.bgColor); - if(err!=0){return err;} - err=putPixel(pStencil,tx,ty , ccc_tRemove );//ccc_tEmpty); - if(err!=0){return err;} - // } - - } else { - paint=1; - } - - if(paint==1){ - if(!overwrite){ - if(!remove){ - err=putPixel(pTerrain,tx,ty ,color); - if(err!=0){return err;} - if(k->data.pt->modif==15){ - err=putPixel(pStencil,tx,ty , ccc_tHidden );//ccc_tBrick); - if(err!=0){return err;} - } else { - err=putPixel(pStencil,tx,ty , ccc_tNoOverwrite );//ccc_tBrick); - if(err!=0){return err;} - } - } else { - err=putPixel(pTerrain,tx,ty ,color); - if(err!=0){return err;} - err=putPixel(pStencil,tx,ty , ccc_tRemoveNoOverwrite );//ccc_tBrick); - if(err!=0){return err;} - } - } else { - err=putPixel(pTerrain,tx,ty ,color); - if(err!=0){return err;} - err=putPixel(pStencil,tx,ty , ccc_tFull );//ccc_tBrick); - if(err!=0){return err;} - } - } - }//fin tx - }//fin ty - k=k->next; - cpt++; - ok=0; - //test_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x0, 0x0, 0x0 ) ); test_blit(pTerrain, &camera, screen, NULL); if( SDL_Flip( screen ) == -1 ) { return 1;} //scanf("%d",&pas); - } - // elimination des pixel remove - for(ty=0;ty<LEVEL_HEIGHT;++ty){ - for(tx=0;tx<LEVEL_WIDTH;++tx){ - color = get_pixel32(tx,ty,pStencil); - if(color==ccc_tRemove){ - err=putPixel(pStencil,tx,ty , ccc_bgStencil ); - if(err!=0){return err;} - } else { - if(color!=ccc_bgStencil){ - err=putPixel(pStencil,tx,ty , ccc_tBrick ); - if(err!=0){return err;} - } - } - } - } - - - // //-DEBUG-printf("LES STEEL : "); - k=gInit->s.ls; - while ( k !=NULL ) { - offset.x = k->x; - offset.y = k->y; - - sf = gInit->mapI.map.tabGif[ADD_OBJ+(gInit->mapI.map.tiles*2)+(gInit->mapI.map.tilesObjet*2)+k->ID]; - - offset.w = k->data.ps->w; - offset.h = k->data.ps->h; - ////-DEBUG-printf("H=)> %d\n",offset.h); - - for(ty=k->y;ty<offset.h+k->y;++ty){ - - if(ty<0) {ty=-1;continue;} - if(ty>pTerrain->h){break;} - if(ty>=LEVEL_HEIGHT) {break;} - - for(tx=k->x;tx<offset.w+k->x;++tx){ - - if(tx<0) {tx=-1;continue;} - if(tx>pTerrain->w){break;} - if(tx>=LEVEL_WIDTH) {break;} - - color = get_pixel32(tx,ty,pStencil); - if(color==ccc_error){return 219;} - if (color == ccc_black){ - continue; - } else { - err=putPixel(pStencil,tx,ty , ccc_s ); - if(err!=0){return err;} - } - } - } - // test_FillRect(pStencil, &offset, ccc_s); - k=k->next; - } - //} - - return 0; -} - - -int miniMap (SDL_Surface *s, int x0, int y0, float coefX, float coefY){ - - - int i,j,stepScreenX,stepScreenY,sizeMiniMapPixel_X,sizeMiniMapPixel_Y,w0,h0,x,y; - SDL_Rect rect; - Uint32 err; - //int cpt=0; - - x=x0; - y=y0; - - if((x0 < 0)|| - (y0 < 0)|| - (coefX < 0.)|| - (coefY < 0.)|| - (s->pixels == NULL) - ){ - //-DEBUG-printf("Bug Error x0 %d y0 %d coefX %f coefY %f s->pixels %p \n",x0,y0,coefX,coefY,s->pixels); - return 189; - } - - ////-DEBUG-printf("x0 %d y0 %d w0 %d h0 %d coefX %f coefY %f s->pixels %p \n",x0,y0,w0,h0,coefX,coefY,s->pixels); - - // if coef >= 1 then zoom++ else zoom--; - stepScreenX=((coefX>=1.)? 1 : ((int)(1./coefX))); - stepScreenY=((coefY>=1.)? 1 : ((int)(1./coefY))); - - sizeMiniMapPixel_X=((coefX>=1.)? (int)coefX : 1); - sizeMiniMapPixel_Y=((coefY>=1.)? (int)coefY : 1); - - /*if(stepX==0){stepX=1;} - if(stepY==0){stepY=1;} - if(sizeX==0){sizeX=1;} - if(sizeY==0){sizeY=1;} - */ - - rect.w=sizeMiniMapPixel_X; - rect.h=sizeMiniMapPixel_Y; - - ////-DEBUG-printf("sx %d sy %d w %d h %d \n",stepX,stepY,sizeX,sizeY); - ////-DEBUG-printf("h0 %d -> stepY %d, w0 %d -> stepX %d x0 %d y0 %d \n",stepX,stepY,sizeX,sizeY); - - w0=((coefX>=1.)? MINIMAP_MX/sizeMiniMapPixel_X : MINIMAP_MX*stepScreenX); - h0=((coefY>=1.)? MINIMAP_MY/sizeMiniMapPixel_Y : MINIMAP_MY*stepScreenY); - - //centrage camera, la position du curseur est placé au centre de la minimap - x0-= (w0/2 - 15); - y0-= (h0/2 - 10); - - if(x0<0){x0=0;} - if(y0<0){y0=0;} - - //centrage camera - ////-DEBUG-printf("x0 %d w0/2 %d\n",x0,w0/2); -// x0+=((x0+w0/3)>LEVEL_WIDTH)? 0 : ((coefX>=1.)? ((-w0/3)/(int)coefX) : (int)(((float)(-w0/3))/coefX) ); - - - if(w0 + x0 >LEVEL_WIDTH){ - if(coefX>1.) {w0=LEVEL_WIDTH-x0;} - else {x0=LEVEL_HEIGHT-w0;} - } - if(h0 + y0 > LEVEL_HEIGHT){ - if(coefY>1.) {h0=LEVEL_HEIGHT-y0;} - else {y0=LEVEL_HEIGHT-h0;} - } - - if((w0 + x0 >LEVEL_WIDTH)||(h0 + y0 > LEVEL_HEIGHT)){return 0;} - - for(j=0;j<h0;j+=stepScreenY){ - if((y0+j)<0){continue;} - rect.y=MINIMAP_Y0+(j/stepScreenY)*sizeMiniMapPixel_Y; - if(rect.y>MINIMAP_Y0+MINIMAP_MY){break;} - if(rect.h+rect.y>MINIMAP_Y0+MINIMAP_MY){break;} - - for(i=0;i<w0;i+=stepScreenX){ - //cpt++; - - if((x0+i)<0){continue;} - rect.x=MINIMAP_X0+(i/stepScreenX)*sizeMiniMapPixel_X; - if(rect.x>MINIMAP_X0+MINIMAP_MX){break;} - if(rect.w+rect.x>MINIMAP_X0+MINIMAP_MX){break;} - - err=get_pixel32(x0+i,y0+j,s); - if(err==ccc_error){return 123;} - - test_FillRect(screen, &rect, err); - - } - } - - //printf("cpt = %d\n",cpt); - return 0; -} - -int test_FillRect( SDL_Surface *t, SDL_Rect *to, Uint32 ccc){ - - SDL_FillRect(t, to, ccc); - return 0; -} - -int test_blit(SDL_Surface *spr,SDL_Rect *fr1,SDL_Surface *t,SDL_Rect *to1){ - - /* int size; - void *temp_pixels = NULL; - - - size=spr->h*spr->pitch; - temp_pixels=malloc(size); - - SDL_LockSurface(spr); - SDL_LockSurface(t); - memcpy(temp_pixels,spr->pixels,size); - - - SDL_UnlockSurface(t); - SDL_UnlockSurface(spr);*/ - SDL_BlitSurface(spr,fr1, t,to1); -/* SDL_LockSurface(spr); - memcpy(spr->pixels,temp_pixels,size); - SDL_UnlockSurface(spr); - - free(temp_pixels);*/ - - return 0; -// return SDL_BlitSurface(spr,fr1, t,to1); - -} - -int paint_manip(SDL_Surface *spr,SDL_Rect from,SDL_Surface *t,SDL_Rect to,Uint32 ccc_t, Uint32 ccc_spr, int mode){ - - int size,i,j,err; - void *temp_pixels = NULL; - Uint32 ccc_temp=0x00000000; - Uint32 ccc_temp2=0x00000000; - //FULL - if(mode==0){ - err=test_blit(spr,&from, t,&to); - return err; - } - - ////-DEBUG-printf("paint_manip(), from.x==%4i, from.y==%4i, from.h==%4i, from.w==%4i, from to.x==%4i, to.y==%4i\n", from.x, from.y, from.h, from.w, to.x, to.y); - - if(from.y<0 || from.y > spr->h) { - //-DEBUG-printf("simple y\n"); - return 345; - } - - if(from.x<0 || from.x > spr->w) { - //-DEBUG-printf("simple x\n"); - return 346; - } - - if(to.y<0 || to.y > t->h) { - //-DEBUG-printf("simple y2\n"); - return 91; - } - - if(to.x<0 || to.x > t->w) { - //-DEBUG-printf("simple x2\n"); - return 92; - } - - if(from.y+from.h > spr->h) { - from.h=spr->h - from.y; - } - - if(from.x+from.w > spr->w) { - from.w=spr->w - from.x; - } - - if(to.y+from.h > t->h) { - from.h=t->h - to.y; - } - - if(to.x + from.w > t->w) { - from.w=t->w - to.x; - } - - size=spr->h*spr->pitch; - temp_pixels=malloc(size); - - SDL_LockSurface(spr); - SDL_LockSurface(t); - memcpy(temp_pixels,spr->pixels,size); - - // NO OVERWRITE - if(mode==4){ - - for(j=0;j<from.h;j++) { - for(i=0;i<from.w;i++) { - /* if((to.y+j)*t->w+(to.x+i)>=(t->w*t->h)){ - //-DEBUG-printf("out of bound1 x=%d + i=%d, y=%d + j=%d * t->w %d == %d >= %d (t->h %d, from.h %d) \n",to.x,i,to.y,j,t->w,(to.y+j)*t->w+(to.x+i),(t->w*t->h),t->h,from.h); - return 56; - //continue; - }*/ - ccc_temp=((Uint32 *)t->pixels)[(to.y+j)*t->w+(to.x+i)]; - ////-DEBUG-printf("ccc_temp1 = %p == %p t->format->colorkey\n",ccc_temp,t->format->colorkey); - if (ccc_temp!=ccc_t) { - ((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i]=ccc_temp;//spr->format->colorkey; - } - } - } - } - // VIS ON TERRAIN - if(mode==8){ - for(j=0;j<from.h;j++) { - for(i=0;i<from.w;i++) { - /* if((to.y+j)*t->w+(to.x+i)>=(t->w*t->h)){ - //-DEBUG-printf("out of bound2 x=%d + i=%d, y=%d + j=%d * t->w %d == %d >= %d (t->h %d, from.h %d) \n",to.x,i,to.y,j,t->w,(to.y+j)*t->w+(to.x+i),(t->w*t->h),t->h,from.h); - return 57; - //continue; - }*/ - ccc_temp=((Uint32 *)t->pixels)[(to.y+j)*t->w+(to.x+i)]; - ////-DEBUG-printf("ccc_temp1 = %p == %p ccc_t\n",ccc_temp,ccc_t); - if (ccc_temp==ccc_t) { - ((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i]=ccc_t;//spr->format->colorkey; - - } - } - } - } - // erease pixel - if(mode==18){ - for(j=0;j<from.h;j++) { - for(i=0;i<from.w;i++) { - /* if((to.y+j)*t->w+(to.x+i)>=(t->w*t->h)){ - //-DEBUG-printf("out of bound2 x=%d + i=%d, y=%d + j=%d * t->w %d == %d >= %d (t->h %d, from.h %d) \n",to.x,i,to.y,j,t->w,(to.y+j)*t->w+(to.x+i),(t->w*t->h),t->h,from.h); - return 57; - //continue; - }*/ - ccc_temp=((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i];//((Uint32 *)t->pixels)[(to.y+j)*t->w+(to.x+i)]; - ccc_temp2=((Uint32 *)t->pixels)[(to.y+j)*t->w+(to.x+i)]; - ////-DEBUG-printf("ccc_temp1 = %p == %p ccc_t\n",ccc_temp,ccc_t); - if (ccc_temp!=ccc_spr) { - ((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i]=ccc_t;//spr->format->colorkey; - } else { - ((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i]=ccc_temp2; - } - } - } - } - - SDL_UnlockSurface(t); - SDL_UnlockSurface(spr); - test_blit(spr,&from, t,&to); - - SDL_LockSurface(spr); - memcpy(spr->pixels,temp_pixels,size); - SDL_UnlockSurface(spr); - - free(temp_pixels); - - return 0; - -} - -int paint_objet (struct gameInit *gInit, int cptFps){ - SDL_Rect offset,objetAnim,rStencil; - SDL_Surface *sf; - struct listeSimplementChainee *k=NULL; - int err=0; - int type=0; - //int ox,oy; - int mode=0; - //Uint32 color,color2,color3; - //int size; - //void *temp_pixels = NULL; - - //DEBUT TEST: SPR - k=gInit->o.lo; - while ( k !=NULL ) { - - sf = gInit->mapI.map.tabGif[gInit->mapI.map.tiles*2+k->ID*2+k->data.po->UD+ADD_OBJ]; - - type=gInit->mapI.map.tabDataSprO[k->ID].type; - - offset.x = k->x; - offset.y = k->y; - - objetAnim.x = 0; - objetAnim.h = sf->h/gInit->mapI.map.tabDataSprO[k->ID].state; - objetAnim.y = (anim_objet( - gInit->mapI.map.tabDataSprO[k->ID].anim, - type, - cptFps, - gInit->mapI.map.tabDataSprO[k->ID].state, - &k->data.po->cptState, - gInit) - )*(objetAnim.h); - objetAnim.w = sf->w; - - rStencil.x=k->x ; - rStencil.y=k->y ; - rStencil.h=objetAnim.h; - rStencil.w=objetAnim.w; - - mode=k->data.po->paintMode; - - // check combi impossible - if((mode!=8)&&((type==3)||(type==4))){ - //-DEBUG-printf("Construction impossible: mode!=%d(8) && type=%d(3 ou 4)\n",mode,type); - return 56; - } - if((mode==8)&&(type==5)){ - //-DEBUG-printf("Construction impossible: mode==%d(8) && type=%d(5)\n",mode,type); - return 57; - } - // fin check - rStencil.x=0 ; - rStencil.y=objetAnim.y ; - if((err=paint_manip(sf,rStencil,pSpr_Lem,offset,gInit->mapI.map.cmap.bgColor,ccc_keyGif,mode))!=0){ - return err; - } - k=k->next; - } - ///FIN TEST: SPR - return 0; -} - -int paint_objet_stencil (struct gameInit *gInit, int cptFps){ - SDL_Rect offset,objetAnim,rStencil; - SDL_Surface *sf; - struct listeSimplementChainee *k=NULL; - //int res; - int err=0; - int type=0; - int ox,oy; - int mode=0; - Uint32 color,color2;//,color3; - //int size; - //void *temp_pixels = NULL; - //DEBUT TEST: SPR - k=gInit->o.lo; - while ( k !=NULL ) { - - sf = gInit->mapI.map.tabGif[gInit->mapI.map.tiles*2+k->ID*2+k->data.po->UD+ADD_OBJ]; - - type=gInit->mapI.map.tabDataSprO[k->ID].type; - - - offset.x = k->x; - offset.y = k->y; - - objetAnim.x = 0; - objetAnim.h = sf->h/gInit->mapI.map.tabDataSprO[k->ID].state; - objetAnim.y = (anim_objet( - gInit->mapI.map.tabDataSprO[k->ID].anim, - type, - cptFps, - gInit->mapI.map.tabDataSprO[k->ID].state, - &k->data.po->cptState, - gInit) - )*(objetAnim.h); - objetAnim.w = sf->w; - - if(type==6||type==8){ - rStencil.w=objetAnim.w/3; - rStencil.x=k->x+objetAnim.w/2-rStencil.w/2 ; - rStencil.y=k->y ; - rStencil.h=objetAnim.h; - } else { - rStencil.x=k->x; - rStencil.y=k->y; - rStencil.w=objetAnim.w; - rStencil.h=objetAnim.h; - } - - mode=k->data.po->paintMode; - - // check combi impossible - if((mode!=8)&&((type==3)||(type==4))){ - //-DEBUG-printf("Construction impossible: mode!=%d(8) && type=%d(3 ou 4)\n",mode,type); - return 56; - } - if((mode==8)&&(type==5)){ - //-DEBUG-printf("Construction impossible: mode==%d(8) && type=%d(5)\n",mode,type); - return 57; - } - // fin check - if(type!=4&&type!=3){ - if(type==5){ - rStencil.y=k->y+(rStencil.h/6); - //rStencil.h=(rStencil.h/6 + (rStencil.h/6)%6)*5; - rStencil.h=4; - } - switch(type){ - case 0://passive - test_FillRect(pStencil, &rStencil, ccc_oPassive);break; - case 5://trap which makes lemmings drown (water/quick sand/mud) - test_FillRect(pStencil, &rStencil, ccc_oTrapDrown);break; - case 6://trap which replaces lemming with death animation - test_FillRect(pStencil, &rStencil, ccc_oTrapAndNoLem);break; - case 7://trap which triggers lemming death animation - test_FillRect(pStencil, &rStencil, ccc_oTrapAndLem);break; - case 8://exit - test_FillRect(pStencil, &rStencil, ccc_oExit);break; - case 32://entry - test_FillRect(pStencil, &rStencil, ccc_oEntry); - break; - default:test_FillRect(pStencil, &rStencil, ccc_oUnknow);break; - } - switch(mode){ - case 0:break;// FULL - case 4: {//No Overwrite - test_blit(pStencilFixe, &rStencil, pStencil, &rStencil); - break; - } - case 8: break;//Vis On Terrain - default:{//-DEBUG-printf("BUG: value is not 8 4 or 0 : %d \n",mode); - return(36);} - } - } - if(mode==8&&(type==3||type==4)){ - for(oy=k->y;oy<rStencil.h+k->y;oy+=1){ - - if(oy<0) {oy=-1;continue;} - if(oy>pSpr_Lem->h){break;} - if(oy>=LEVEL_HEIGHT) {break;} - - if(sf->pixels==NULL){break;} - - for(ox=k->x;ox<rStencil.w+k->x;ox+=1){ - - if(ox<0) {ox=-1;continue;} - if(ox>pSpr_Lem->w){break;} - if(ox>=LEVEL_WIDTH) {break;} - - //if(temp_pixels==NULL){break;} - //memcpy(sf->pixels,temp_pixels,size); - color2 = get_pixel32(objetAnim.x+ox-k->x,objetAnim.y+oy-k->y,sf); - if(color2==ccc_error){return 253;} - // NO DIG - color = get_pixel32(ox,oy,pStencil); - if(color==ccc_error){return 254;} - if(color==ccc_bgStencil){continue;} - - // pixel transparency in object.gif - if(type==3){ - err=putPixel(pStencil,ox,oy,ccc_oNoDigLeft); - if(err!=0){return err;} - } else { - err=putPixel(pStencil,ox,oy,ccc_oNoDigRight); - if(err!=0){return err;} - } - } - } - //} - }//FIN ELSE MODE - k=k->next; -} -///FIN TEST: SPR -return 0; -} - -int paint_lemming (struct gameInit *gInit, int cptFps){ - - //DEBUT TEST: LEM - SDL_Rect offset,objetAnim; - SDL_Surface *sf; - struct listeSimplementChainee *k=NULL; - int res; - int err=0; - int cpt=0; - int newID; - int fx,fy,fs,la,ls;//lt; - - k=gInit->l.ll; - - while ( k !=NULL ) { - // //-DEBUG-printf("ID= %d k->data.pl->cptState== %d\n",k->ID,k->data.pl->cptState); - // DIED - if(k->data.pl->cptState <= 0 ){ - ////-DEBUG-printf("id ? %d \n",k->ID); - switch(k->ID){ - case 10 : if(k->data.pl->cptBoom!=404) {break;} - case 8 : - case 7 : - case 5 : k->ID=17;break;// decret de décès - case 9 : k->ID=19;break;// decret de survivant - default:break; - - } - } - //8 + (99 - releaseRate) - if((gInit->cptGame[15]==0)&&(gInit->cptGame[16]==gInit->mapI.nbrEntry)){ - gInit->cptGame[15]=cptFps; - } - - if( (k->ID==18)&& - (gInit->cptGame[16]==gInit->mapI.nbrEntry)&& - (!gInit->mapI.nuke)&& - ((gInit->l.nbrInput-(gInit->l.nbr + gInit->l.nbrDied + gInit->l.nbrSafe))>0)&& - (cptFps==(gInit->cptGame[15]+(8 + 99 - gInit->mapI.paraMap[0]))) - ){ - k->ID=1; - ++(gInit->l.nbr); - gInit->cptGame[15]=cptFps; - } - - // lemmings => spr de sa mort ou vivant - switch(k->ID){ - case 20: newID=10;break; - case 21: newID=6;break; - case 22: newID=4;break; - default: newID=k->ID;break; - } - if((k->data.pl->cptState != 0 ) || (newID < LEM_JOB)){//FIXME il y a une faille dans la condition - //-DEBUG-printf("num = %d id=%d(%d)\n",cpt++,newID,k->ID); - - offset.x = k->x; - offset.y = k->y; - sf = gInit->mapI.map.tabGif[gInit->mapI.map.tiles*2+(gInit->mapI.map.tilesObjet*2)+gInit->mapI.map.tilesSteel+(newID)*2+k->data.pl->dir+ADD_OBJ]; - - fx=gInit->mapI.lemmingDATA[newID].footX; - fy=gInit->mapI.lemmingDATA[newID].footY; - fs=gInit->mapI.lemmingDATA[newID].footSize; - la=gInit->mapI.lemmingDATA[newID].anim; - //lt=gInit->mapI.lemmingDATA[newID].type; - ls=gInit->mapI.lemmingDATA[newID].state; - - objetAnim.x = 0; - objetAnim.h = sf->h/ls; - objetAnim.y = (anim_objet((la+90),0,cptFps,ls,&k->data.pl->cptState,gInit))*(objetAnim.h); // +3 : 0 == loop et dans anim_objet c'est 3 pas 0 (fichier de conf) FIXME - objetAnim.w = sf->w; - // offset.w = objetAnim.w; - // offset.h = objetAnim.h; - switch(newID){ - case 0: {// WALK_ON - //putPixel(pStencil,k->x+fx+fs,k->y+fy,ccc_lWalk_on); - //test_FillRect(pStencil, &offset, ccc_lWalk_on); - break; - } - case 6: {//BLOCKER - putPixel(pStencil,k->x+sf->w-fx-fs,k->y+fy,ccc_lStopperLeft); - putPixel(pStencil,k->x+fx+fs,k->y+fy,ccc_lStopperRight); - //test_FillRect(pStencil, &offset, ccc_lStopper); - break; - } - default:break; - } - if ((res=test_blit(sf, &objetAnim, pSpr_Lem, &offset)) != 0){ - //-DEBUG-printf("ERREUR [%d] test_blit : ",res); - } - } - k=k->next; - - } - ///FIN TEST: LEM - err=stateLemming(gInit); - if(err!=0){return err;} - - return 0; -} - - - -int giveSkill (struct gameInit *gInit, struct listeSimplementChainee* k, int skill ){ - /* - struct listeSimplementChainee *l=NULL; - if(skill==20){ - gInit->mapI.nuke=1; - l=gInit->l.ll; - while(l!=NULL){ - giveSkill (gInit,l,10); - l=l->next; - } - return 0; - }*/ - - switch(skill){ - //climber - case 2 : if((gInit->mapI.paraMap[4]>0)&& - (k->data.pl->climber==0)){ - --gInit->mapI.paraMap[4]; - k->data.pl->climber=1; - } - break; - //floater - case 4 : if((gInit->mapI.paraMap[5]>0)&& - (k->data.pl->floater==0)){ - --gInit->mapI.paraMap[5]; - k->data.pl->floater=1; - } - break; - //bomber - case 10 : - if((gInit->mapI.paraMap[6]>0)&& - (k->data.pl->cptBoom==404)){ - if(!gInit->mapI.nuke) - {--gInit->mapI.paraMap[6];} - k->data.pl->cptBoom=5*FPS; - if( k->data.pl->blocker==1){ - k->ID=21; - } /*else { - k->ID=10; - }*/ - } - break; - //blocker - case 6 : if(((k->ID==0)||((k->ID>9)&&(k->ID<17)))&& - (gInit->mapI.paraMap[7]>0) - /*&&(k->data.pl->blocker==0)*/ - ){ - --gInit->mapI.paraMap[7]; - k->data.pl->blocker=1; - k->ID=6; - } - break; - //builder - case 11 : if(((k->ID==0)||((k->ID>9)&&(k->ID<17)))&& - (gInit->mapI.paraMap[7]>0) - /*&&(k->data.pl->blocker==0)*/ - ){ - --gInit->mapI.paraMap[8]; - //k->data.pl->cptBrick=12; - k->ID=11; - } - break; - case 14 : ;break;//basher - case 15 : ;break;//miner - case 13 : ;break;//digger - - default: break; - } - - - return 0; - -} - -int findTerrain( struct gameInit *gInit,int x, int y, int x0){ - SDL_Rect to; - int i=0; - int cpt=0; - int err=0; - SDL_Surface *sf; - struct listeSimplementChainee *k=NULL; - k=gInit->t.lt; - to.x=5; - to.y=screen->h-10; - to.w=600; - to.h=10; - test_FillRect(screen, &to, ccc_black); - while(k != NULL){ - sf=gInit->mapI.map.tabGif[test_O_UD(k->data.pt->modif)+k->ID*2+ADD_OBJ]; - if( (x+15 <= k->x + sf->w)&& - (x+15 >= k->x)&& - (y+15 <= k->y + sf->h)&& - (y+15 > k->y)){//on the terrain - ++cpt; - print_num(screen, gInit->mapI.map.tabGif[1], 5 , screen->h-10, cpt); - print_num(screen, gInit->mapI.map.tabGif[1], 5 + cpt*40, screen->h-10, i); - to.x=k->x; - to.y=k->y; - to.w=sf->w; - to.h=1; - test_FillRect(pStencil, &to, ccc_red); - to.x=k->x; - to.y=k->y; - to.w=1; - to.h=sf->h; - test_FillRect(pStencil, &to, ccc_red); - to.x=k->x + sf->w; - to.y=k->y; - to.w=1; - to.h=sf->h; - test_FillRect(pStencil, &to, ccc_red); - to.x=k->x ; - to.y=k->y + sf->h; - to.w=sf->w; - to.h=1; - test_FillRect(pStencil, &to, ccc_red); - err=print_num(pStencil, gInit->mapI.map.tabGif[1], k->x + 2, k->y + 2, i); - if(err!=0){return err;} - - } - ++i; - k=k->next; - - } - return 0; -} - -int paint_cursor(SDL_Surface *dst1,SDL_Surface *dst2, SDL_Surface *src, int x, int y, int x0, struct gameInit *gInit) -{ - SDL_Rect from, to; - int f=0; - int err=0; - struct listeSimplementChainee *k=NULL; - - /* Render! */ - from.x = 0; - from.h = src->h/8; - from.y = gInit->cptGame[NBR_BUTTON_LEMMING] * (from.h); - from.w = src->w; - - to.x = (y>LEVEL_HEIGHT)? x0 : x; - to.y = y; - if (y>LEVEL_HEIGHT){// cursor in pSpr_Lem - test_blit(src, &from, dst2, &to); } else { - k=gInit->l.ll; - while(k != NULL){ - if( (x+15 <= k->x + 24)&&//32)&& - (x+15 >= k->x + 8)&&//)&& - (y+15 <= k->y + 20)&& - (y+15 > k->y )){//on the lemming - to.x=k->x; - to.y=k->y; - from.y = 4 * (from.h); - f=1; - //begin giveSkill - if((gInit->cptGame[18]==1)&&(gInit->cptGame[17]!=404)){ - switch(gInit->cptGame[17]){ - - case 2 : err=giveSkill(gInit,k,2);break;//climber - case 3 : err=giveSkill(gInit,k,4);break;//floater - case 4 : err=giveSkill(gInit,k,10);break;//bomber - - case 5 : err=giveSkill(gInit,k,6);break;//stopper - - case 6 : err=giveSkill(gInit,k,11);break;//builder - case 7 : err=giveSkill(gInit,k,14);break;//basher - case 8 : err=giveSkill(gInit,k,15);break;//miner - case 9 : err=giveSkill(gInit,k,13);break;//digger - - //case 11 : err=giveSkill(gInit,k,20);break;//nuke - - default: break; - } - if(err!=0){return err;} - gInit->cptGame[18]=0; - } - //end giveSkill - break; - } - k=k->next; - } - to.y -= 4; - test_blit(src, &from, dst1, &to); - if(y+from.h>LEVEL_HEIGHT){ - to.x = (f==1)? k->x-x + x0:x0; - to.y = (f==1)? k->y -4 :y -4; - test_blit(src, &from, dst2, &to); - } else { - if(f==1){ - to.x = k->x-x + x0; - to.y = k->y -4 ; - test_blit(src, &from, dst2, &to); - } - } + temp=malloc(sizeof(char)*(strlen(argv[1])+1)); + sprintf(temp,"%s",argv[1]); } - return 0; -} + yyin=fopen(temp, "r"); + //yyin=fopen("./lvlTest01.ini", "r"); + if (yyin==NULL) { + fprintf(stderr,"Filename INVALIDE: %s\n",temp); + perror("Impossible d'ouvrir le niveau"); return(48); } -int legende (struct gameInit *gInit){ - - SDL_Rect rStencil; - int decalY = 11; - int err=0; - //char* msg=NULL; - - rStencil.x= 400; - rStencil.y= LEVEL_HEIGHT + 1; - rStencil.w= 10; - rStencil.h= 10; - - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Terrain:"); - if(err!=0){return err;} - rStencil.y += decalY; - test_FillRect(screen, &rStencil, ccc_tRemove); - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y ,"Remove"); - if(err!=0){return err;} - rStencil.y += decalY; - test_FillRect(screen, &rStencil, ccc_tRemoveNoOverwrite); - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"No Overwrite Remove"); - if(err!=0){return err;} - rStencil.y += decalY; - - test_FillRect(screen, &rStencil, ccc_tFull); - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Full"); - if(err!=0){return err;} - rStencil.y += decalY; - - test_FillRect(screen, &rStencil, ccc_tNoOverwrite); - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"No Overwrite"); - if(err!=0){return err;} - rStencil.y += decalY; - - test_FillRect(screen, &rStencil, ccc_tHidden); - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Hidden"); - if(err!=0){return err;} - rStencil.y += decalY; + /* + for(i=0;i<256;++i){ + for(j=0;j<256;++j){ + for(k=0;k<256;++k){ + ctest = ((i<<16 | j<<8 | k) & 0xffffff); + //-DEBUG-printf("[i]=%d, R = %X G = %X B = %X\n", + i, ((ctest>>16) & 0xff), + ((ctest>>8) & 0xff), + (ctest & 0xff) ); + }}}*/ - // object - rStencil.x = 30; - rStencil.y = LEVEL_HEIGHT + 1; - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Objet:"); - if(err!=0){return err;} - rStencil.y += decalY; - test_FillRect(screen, &rStencil, ccc_oPassive); - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Passive"); - if(err!=0){return err;} - rStencil.y += decalY; - - test_FillRect(screen, &rStencil, ccc_oNoDigLeft); - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"No digging to the left"); - if(err!=0){return err;} - rStencil.y += decalY; - - test_FillRect(screen, &rStencil, ccc_oNoDigRight); - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"No digging to the right"); - if(err!=0){return err;} - rStencil.y += decalY; - - test_FillRect(screen, &rStencil, ccc_oExit); - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Exit"); - if(err!=0){return err;} - rStencil.y += decalY; - - test_FillRect(screen, &rStencil, ccc_oEntry); - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Entry"); - if(err!=0){return err;} - rStencil.y += decalY; - - test_FillRect(screen, &rStencil, ccc_oUnknow); - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Unknow"); - if(err!=0){return err;} - rStencil.y += decalY; - - test_FillRect(screen, &rStencil, ccc_oTrapDrown); - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Trap which makes lemmings drown (water/quick sand/mud)"); - if(err!=0){return err;} - rStencil.y += decalY; - - test_FillRect(screen, &rStencil, ccc_oTrapAndNoLem); - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Trap which replaces lemming with death animation"); - if(err!=0){return err;} - rStencil.y += decalY; - - test_FillRect(screen, &rStencil, ccc_oTrapAndLem); - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Trap which triggers lemming death animation"); - if(err!=0){return err;} - - rStencil.y += decalY + 2; - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x +15 ,rStencil.y,"Steel:"); - if(err!=0){return err;} - rStencil.y += decalY; - test_FillRect(screen, &rStencil, ccc_s); - err=print_alpha(screen, gInit->mapI.map.tabGif[9], rStencil.x + 15,rStencil.y,"Indestructible terrain"); - if(err!=0){return err;} - - return 0; -} - -int paint_interface (struct gameInit *gInit){ - int i,n; - int err=0; - SDL_Rect to,from; - SDL_Surface *sf; - - from.x = 0; - to.y = LEVEL_HEIGHT + INTER_BUTTON_Y; - - for(i=0;i<NBR_BUTTON_LEMMING;++i){ + res=lancement(); + //fclose(yyin); fait dans lancement - sf=gInit->mapI.map.tabGif[i+NBR_ADD_OBJ]; + return res; - to.x = (i*(sf->w)) + INTER_BUTTON_X; - to.w = sf->w; - to.h = sf->h/2; - - from.y = (to.h)*(gInit->cptGame[i]); - from.w = sf->w; - from.h = to.h; - - if(i+4 < 12){ - n=gInit->mapI.paraMap[i+4]; - err=print_num(screen, gInit->mapI.map.tabGif[1], (to.x+7+(((n/10)>0)?0:5))+(sf->w*(2)), to.y-12, - n); - if(err!=0){return err;} - } - test_blit(sf, &from, screen, &to); - } - n=gInit->mapI.paraMap[0]; - err=print_num(screen, gInit->mapI.map.tabGif[1],INTER_BUTTON_X + 20 + (((n/10)>0)?0:5), to.y-12, n); - if(err!=0){return err;} - return 0; } -int mouse_action (struct gameInit *gInit, int x, int y, int camX, int camY ){ - - int i,numB; - SDL_Surface *sf; - struct listeSimplementChainee *l=NULL; - // size cursor - sf=gInit->mapI.map.tabGif[3]; - x+=sf->w/2; - y+=sf->h/16;// (sf->h/8)/2; - sf=gInit->mapI.map.tabGif[NBR_ADD_OBJ]; - //-DEBUG-printf("action mouse ?\n"); - // test mouse on interface button - if( (x >= INTER_BUTTON_X)&& - (x <= sf->w*NBR_BUTTON_LEMMING)&& - (y >= INTER_BUTTON_Y + LEVEL_HEIGHT)&& - (y < INTER_BUTTON_Y + LEVEL_HEIGHT + (sf->h/2))) - {// mouse is on interface button - numB=((x-INTER_BUTTON_X)/sf->w); - if((gInit->cptGame[numB]==1)&&((numB==0)||(numB==1))){ - // incr or decr lemmingRate - if((numB==0)&&(gInit->mapI.paraMap[0]>13)){ - gInit->cptGame[15]-=5; - gInit->mapI.paraMap[0]-=5; - } else{ - if((numB==0)&&(gInit->mapI.paraMap[0]>8)){ - gInit->cptGame[15]-=1; - gInit->mapI.paraMap[0]-=1; - } } - if((numB==1)&&(gInit->mapI.paraMap[0]<91)){ - gInit->cptGame[15]+=5; - gInit->mapI.paraMap[0]+=5; - } else { - if((numB==1)&&(gInit->mapI.paraMap[0]<99)){ - gInit->cptGame[15]+=1; - gInit->mapI.paraMap[0]+=1; - } } - gInit->cptGame[numB]=0; - } else { - //NUKE - if(numB==11){ - gInit->mapI.nuke=1; - l=gInit->l.ll; - while(l!=NULL){ - if(l->ID!=17&&l->ID!=8&&l->ID!=9&&l->ID!=10&&l->ID!=21){ - giveSkill (gInit,l,10); - } - l=l->next; - } - } - - for(i=0;i<NBR_BUTTON_LEMMING;++i){ - if(i != numB){ - gInit->cptGame[i]=0; - } else { - if(gInit->cptGame[i] == 1){ - gInit->cptGame[i] = 0; - gInit->cptGame[17]=404; - } else { - gInit->cptGame[i] = 1; - gInit->cptGame[17]=i; - } - } - } - } - gInit->cptGame[18]=0; - } else { - // mouse is on pSpr_Lem - //if .. - //gInit.cptGame[NBR_BUTTON_LEMMING] - gInit->cptGame[18]=1; - } - return 0; -} - -int supprLem (struct gameInit *gInit, int cptFps){ - struct listeSimplementChainee *k=NULL; - // refresh liste all of 100 tick - if(cptFps % 100 == 0) - { - k=gInit->l.ll; - if((k != NULL)&&(gInit->l.nbr == 1)){ - if(k->ID == 17){++gInit->l.nbrDied; --gInit->l.nbr;} - if(k->ID == 19){++gInit->l.nbrSafe; --gInit->l.nbr;} - if((k->ID == 17)||(k->ID == 19)) {k=NULL;} - } else { - while(k != NULL){ - if(k->next != NULL){ - switch(k->next->ID){ - // DIED - case 17 : { - k->next=k->next->next; - ++gInit->l.nbrDied; - --gInit->l.nbr; - break; - } - // SAFE - case 19 : { - k->next=k->next->next; - ++gInit->l.nbrSafe; - --gInit->l.nbr; - break; - } - default: k=k->next; break; - } - } else { - k=k->next; - } - } - } - //-DEBUG-printf("nbrInput = %d\nnbrInGame = %d\nnbrDied = %d\nnbrSafe = %d\n",gInit->l.nbrInput,gInit->l.nbr,gInit->l.nbrDied,gInit->l.nbrSafe); - } - return 0; -} - - -// Par défaut, c'est l'analyse LEXICALE qui est lancée ! -int lancement (){ - - int res,cptFps/*,i*/; - int err=0; - //Ce qui va nous permettre de quitter - int quit = 0; - SDL_Rect camera,inter; - int mouseX,mouseY=0; - long tick,temps=0; - float fps = 0.0; - int fps_count = 0; - int fps_start = 0; - int decalFps = 0; - - //La structure d'evenement - SDL_Event event; - event.type=0; - - //paint_stencil=1; - - - /* camera.x=160; - camera.y=120; - */ - camera.x=0; - camera.y=0; - - struct gameInit gInit; - - // constructor ... - err=initGame(&gInit); - if(err!=0){return err;} - - //Initialisation - err=init(); - if(err!=0){return err;} - ////-DEBUG-printf("DONE\n"); - - ////-DEBUG-printf("DEBUG3: load_file ... \n"); - //Chargement des fichiers - err=load_files(&gInit); - if(err!=0){return err;} - - // paraMap - for(res=4;res<IDENT_COUNT;++res){ - gInit.mapI.paraMap[res]=99; - } - - // CREATION OF SOME LEMMING - err=creationLemming(&gInit); - if(err!=0){return err;} - - mouseX = gInit.mapI.xPos; - camera.x=gInit.mapI.xPos; - camera.w=screen->w; - camera.h=screen->h; - inter.x=0; - inter.y=LEVEL_HEIGHT; - inter.w=SCREEN_WIDTH; - inter.h=INTERFACE_HEIGHT; - - ////-DEBUG-printf("DEBUG ludo : camera.w==%i, camera.h==%i\n", camera.w, camera.h); - - // init level map (upper part of screen) - test_FillRect( pTerrain, &pTerrain->clip_rect, - gInit.mapI.map.cmap.bgColor ); - - - - // init interface (downner part of screen) - test_FillRect( screen, &inter, ccc_black); - cptFps=0; - gInit.cptGame[15]=0; - - test_FillRect( pStencil, NULL, ccc_bgStencil); - //paint_terrain => pTerrain - err=paint_terrain(&gInit,0); - if(err!=0){return err;} - - SDL_SetColorKey(pTerrain, SDL_SRCCOLORKEY|SDL_RLEACCEL, gInit.mapI.map.cmap.bgColor); - - err=print_num(screen, gInit.mapI.map.tabGif[1], screen->w-37, screen->h-10, FPS); - if(err!=0){return err;} - - test_blit(pStencil, NULL, pStencilFixe, NULL); - SDL_SetColorKey(pStencilFixe, SDL_SRCCOLORKEY, ccc_bgStencil); - err=paint_objet_stencil (&gInit,cptFps); - if(err!=0){return err;} - test_blit(pStencil, NULL, pStencilFixe, NULL); - - //Tant que l'utilisateur n'a pas quitter - - while( quit == 0 ) - { - /* Timing */ - tick = SDL_GetTicks(); - - //Tant qu'il y a un événement - while( SDL_PollEvent( &event ) ) - { - switch(event.key.keysym.sym){ - case SDLK_HOME : decalFps = 0; break; - case SDLK_END : decalFps = FPS-11; break; - case SDLK_PAGEUP : if(decalFps>0){decalFps -=1;}break; - case SDLK_PAGEDOWN : if(decalFps<(FPS-1)){decalFps +=1;}break; - case SDLK_w : paint_stencil = (paint_stencil==0)? 1 : 0 ; break; - case SDLK_ESCAPE : quit = 1; break; - default:break; - } - // SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE); - - switch (event.type) { - case SDL_MOUSEMOTION: { mouseX = event.motion.x; - mouseY = event.motion.y;break; } - case SDL_MOUSEBUTTONDOWN: { err=mouse_action (&gInit, mouseX, mouseY,camera.x,camera.y ); - if(err!=0){return err;} - break;} - case SDL_QUIT : { - //On quitte le programme - quit = 1; - } - default:break; - } - } - - if(mouseY <= LEVEL_HEIGHT){ - if (mouseX > (SCREEN_WIDTH - BOUND_SENSIBILITE)){ - if (camera.x < (LEVEL_WIDTH - SCREEN_WIDTH ) ) - {camera.x += CAM_VITESSE;} - } - if (mouseX < BOUND_SENSIBILITE){ - if (camera.x >= CAM_VITESSE ) - {camera.x -= CAM_VITESSE;} - } - } - - test_FillRect( pSpr_Lem, NULL, gInit.mapI.map.cmap.bgColor); - test_blit(pTerrain, NULL, pSpr_Lem, NULL); - - test_FillRect( screen, &inter, ccc_black); - - test_FillRect( pStencil, NULL, ccc_bgStencil); - test_blit(pStencilFixe, NULL, pStencil, NULL); - - // SPR => pSpr_Lem - err=paint_objet (&gInit,cptFps); - if(err!=0){return err;} - - // SUPPR LEM => gInit - err=supprLem(&gInit,cptFps); - if(err!=0){return err;} - - // LEM => pSpr_Lem - //if(paint_stencil==0){ - err=paint_lemming (&gInit,cptFps); - if(err!=0){return err;} - //} - - // INTERFACE => screen - if(paint_stencil==0){ - err=paint_interface (&gInit); - if(err!=0){return err;} - } else { - //testAlpha(screen, gInit.mapI.map.tabGif[9],gInit.mapI.map.tabGif[1],2,330); - err=legende (&gInit); - if(err!=0){return err;} - } - - // CURSOR => pSpr_Lem, screen - if(paint_stencil==0){ - err=paint_cursor (pSpr_Lem,screen, gInit.mapI.map.tabGif[3], mouseX+camera.x, mouseY+camera.y, mouseX, &gInit); - if(err!=0){return err;} - } else { - err=findTerrain(&gInit,mouseX+camera.x, mouseY+camera.y, mouseX); - if(err!=0){return err;} - err=paint_cursor (pStencil,screen, gInit.mapI.map.tabGif[3], mouseX+camera.x, mouseY+camera.y, mouseX, &gInit); - if(err!=0){return err;} - //-DEBUG-printf("x=%d y=%d\n",mouseX+camera.x,mouseY+camera.y); - } - - //get_pixel32( event.motion.x + camera.x, event.motion.y, pTerrain ); - //get_pixel32( event.motion.x + camera.x, event.motion.y, pSpr_Lem ); - //get_pixel32( event.motion.x + camera.x , event.motion.y, screen ); - - - if(paint_stencil==0){ - // printf("test %p\n",pSpr_Lem); - test_blit(pSpr_Lem, &camera, screen, NULL); - err=miniMap (pSpr_Lem,mouseX+camera.x,mouseY+camera.y,5.,5.); - if( err != 0){ return err;} - } else { - test_blit(pStencil, &camera, screen, NULL); - err=miniMap (pStencil,mouseX+camera.x,mouseY+camera.y,5.,5.); - if( err != 0){ return err;} - - } - - - /* FPS counter */ - ////-DEBUG-printf("(tick %ld > fps_start %d +500)\n",tick,fps_start); - // refresh fps all 500 ms - if(tick > fps_start + 500) - { - fps = (float)fps_count * 1000.0 / (tick - fps_start); - fps_count = 0; - fps_start = tick; - } - - ++fps_count; -/* - if (cptFps == 99999) { - //if(SDL_GetTicks()==240000){ - //decalFps = 99; - quit=1; - } -*/ - // limte fps - - if(FPS<=decalFps){//-DEBUG-printf("BUG: FPS < 0 : (FPS %d - decalFps %d) < 0",FPS,decalFps); - return(42); - } else { - while((FPS>decalFps)&&(((float)fps_count * 1000.0 / (SDL_GetTicks()-fps_start)) > FPS-decalFps) ){ - if((FPS-decalFps)<=120){// useless ? - SDL_Delay(3); - } - }} - - temps = SDL_GetTicks(); - //heure - err=print_num(screen, gInit.mapI.map.tabGif[1], screen->w-160, screen->h-40, temps/(1000*3600)); - if(err!=0){return err;} - //minute - err=print_num(screen, gInit.mapI.map.tabGif[1], screen->w-130, screen->h-40, (temps/(1000*60))%60); - if(err!=0){return err;} - //seconde - err=print_num(screen, gInit.mapI.map.tabGif[1], screen->w-100, screen->h-40, (temps/1000)%60); - if(err!=0){return err;} - - err=print_num(screen, gInit.mapI.map.tabGif[1], screen->w-150, screen->h-10, cptFps); - if(err!=0){return err;} - err=print_num(screen, gInit.mapI.map.tabGif[1], screen->w-87, screen->h-10, fps); - if(err!=0){return err;} - - //Mise à jour de l'écran - if( SDL_Flip( screen ) == -1 ) { - return 4; - } - - //SDL_Delay(1); - - if(FPS<=decalFps){//-DEBUG-printf("BUG: FPS < 0 : (FPS %d - decalFps %d) < 0",FPS,decalFps); - return(43); - } else { - temps = SDL_GetTicks(); - ////-DEBUG-printf("cptFps = %d, time: %ld:%ld:%ld \n",cptFps,temps/(1000*3600),(temps/(1000*60))%60,(temps/1000)%60); - } - cptFps++; - - //quit = 1; - - } - - //On libère les images et on quitte SDL - err=clean_up(); - if(err!=0){return err;} - - return 0; - } - - - int main (int argc, char **argv) - { - int res,i,num,lt; - char *temp,*temp0; - //Uint32 ctest; - //int j,k; - //argc=3; - //argv[1]="1"; - //argv[2]="72"; - if (argc<2 || argc>3) { - fprintf(stderr, "Usage %s [Option] <Filename>\n",argv[0]); - fprintf(stderr,"\nOption:\n\t- 1: on se place dans le dossier 1_orig\n"); - fprintf(stderr,"\t- 2: on se place dans le dossier 2_ohno\n"); - fprintf(stderr,"Filename:\n\t- numero du lvl (ex: 1, 4, 13, 65b )\n"); - return(44); - } - if (argc==3) { - i=1; - num=atoi(argv[2]); - if (num >= 10) ++i; - if (num >= 100) ++i; - if (num >= 1000){ - fprintf(stderr,"Too many lvl (%d > 999)\n",num); - return(45); - } - temp0=malloc(sizeof(char)*(3-i));// => [00] [0] [] - switch(i){ - case 1 : temp0 = "00";break; - case 2 : temp0 = "0";break; - case 3 : temp0 = "";break; - default:fprintf(stderr,"BUG: i[=%d] > 3 ERREUR\n",i);return(46); - } - - lt = (strlen("../../../../trunk/level/1_orig/lvl.ini")+5);// + 4 : 4 chiffres lvl.ini, +1 : string - temp=malloc(sizeof(char)*lt); - switch(atoi(argv[1])){ - case 1 : sprintf(temp,"../../../../trunk/level/1_orig/lvl0%s%d.ini",temp0,num);break; - case 2 : sprintf(temp,"../../../../trunk/level/2_ohno/lvl1%s%d.ini",temp0,num);break; - default:fprintf(stderr,"ERREUR: dossier [%d] inconnu\n",atoi(argv[1]));return(47); - } - } else { - temp=malloc(sizeof(char)*(strlen(argv[1])+1)); - sprintf(temp,"%s",argv[1]); - } - yyin=fopen(temp, "r"); - //yyin=fopen("./lvlTest01.ini", "r"); - if (yyin==NULL) { - fprintf(stderr,"Filename INVALIDE: %s\n",temp); - perror("Impossible d'ouvrir le niveau"); return(48); } - - /* - for(i=0;i<256;++i){ - for(j=0;j<256;++j){ - for(k=0;k<256;++k){ - ctest = ((i<<16 | j<<8 | k) & 0xffffff); - //-DEBUG-printf("[i]=%d, R = %X G = %X B = %X\n", - i, ((ctest>>16) & 0xff), - ((ctest>>8) & 0xff), - (ctest & 0xff) ); - }}}*/ - - res=lancement(); - //fclose(yyin); fait dans lancement - - return res; - - } - |