int scanNames(int argCnt, char **array, char **codes) { FILE *fp; fp = fopen(array[argCnt],"r"); char name[10]; while(fscanf(fp,"%s",name)!= EOF) { doRules(codes,name); } return 1; }
int main(void) { int output = 0, reference, steer, out_steer; while(1) { pitch_angle_mf->sensor = 420; pitch_rate_mf->sensor = 513; roll_angle_mf->sensor = 330; roll_rate_mf->sensor = 200; yaw_angle_mf->sensor = 200; yaw_rate_mf->sensor = 203; collective_height_mf->sensor = 320; collective_rate_mf->sensor = 500; Fuzzification( pitch_param, pitch_angle_mf); Fuzzification( tilt_rate_param, pitch_rate_mf); Fuzzification( roll_param, roll_angle_mf); Fuzzification( tilt_rate_param, roll_rate_mf); Fuzzification( yaw_param, yaw_angle_mf); Fuzzification( yaw_rate_param, yaw_rate_mf); Fuzzification( collective_param, collective_height_mf); Fuzzification( collective_rate_param, collective_rate_mf); output = doRules(pitch_mf, PitchRule); // Kyle - changed doRules output = doRules(roll_mf, RollRule); // Kyle - changed doRules output = doRules(yaw_mf, YawRule); // Kyle - changed doRules output = doRules(collective_mf, CollectiveRule); // Kyle - changed doRules } }
//进行规约的操作 bool LRGramar::doGuiYue(const int ruleNum) { switch(ruleNum) { case R1: //执行进出站等相关操作 if(!doRules(A_NTERMINAL,4)) { return false; } break; case R2: if(!doRules(B_NTERMINAL,3)) { return false; } break; case R3: if(!doRules(C_NTERMINAL,3)) { return false; } break; case R4: if(!doRules(C_NTERMINAL,0)) { return false; } break; case R5: if(!doRules(D_NTERMINAL,1)) { return false; } break; case R6: case R7: if(!doRules(D_NTERMINAL,1)) { return false; } else { } break; case R8: if(!doRules(F_NTERMINAL,3)) { return false; } else { //堆栈是先进先出 int argP = variableBufferPtr--; int resultPtr = variableBufferPtr--; emit("=",variableBuffer[argP]," ",variableBuffer[resultPtr]); } break; case R9: if(!doRules(G_NTERMINAL,2)) { return false; } else { backPatch(trueChainHead,nextq); //回填真链头 } break; case R10: if(!doRules(S_NTERMINAL,2)) { return false; } else { backPatch(falseChain[falseChainPtr--],nextq); } break; case R11: if(!doRules(T_NTERMINAL,3)) { return false; } else { emit("goto","","","0"); backPatch(falseChain[falseChainPtr--],nextq); falseChain[++falseChainPtr] = nextq-1; } break; case R12: if(!doRules(S_NTERMINAL,2)) { return false; } backPatch(falseChain[falseChainPtr--],nextq); break; case R13: if(!doRules(W_NTERMINAL,2)) { return false; } else { backPatch(trueChainHead,doTrue[doTruePtr--]); backPatch(falseChain[falseChainPtr--],nextq); } break; case R14: if(!doRules(K_NTERMINAL,1)) { return false; } doTrue[++doTruePtr] = nextq;//保存do后面的地址 break; case R15: if(!doRules(L_NTERMINAL,2)) { return false; } break; case R16: if(!doRules(N_NTERMINAL,2)) { return false; } break; case R17: if(!doRules(Q_NTERMINAL,3)) { return false; } else { trueChainHead = nextq; falseChain[++falseChainPtr]=nextq+1; char temp[10]; int arg2 = variableBufferPtr--; int arg1 = variableBufferPtr--; sprintf(temp,"%s%s%s",variableBuffer[arg1],lastOp[OpPtr--],variableBuffer[arg2]); emit("if",temp,"goto","0"); emit("goto","","","0"); } break; case R18: if(!doRules(P_NTERMINAL,2)) { return false; } else { if(strcmp(variableBuffer[variableBufferPtr],"null")!=0) { char *pPlace = newTemp(); emit(lastOp[OpPtr--],variableBuffer[variableBufferPtr--],variableBuffer[variableBufferPtr--],pPlace); strcpy(variableBuffer[++variableBufferPtr],pPlace); } else { variableBufferPtr--; } } break; case R19: if(!doRules(H_NTERMINAL,3)) { return false; } else { if(strcmp(variableBuffer[variableBufferPtr],"null")!=0) { char *hPlace = newTemp(); emit(lastOp[OpPtr--],variableBuffer[variableBufferPtr--],variableBuffer[variableBufferPtr--],hPlace); strcpy(variableBuffer[++variableBufferPtr],hPlace); } else { variableBufferPtr--; } strcpy(lastOp[++OpPtr],"+"); } break; case R20: if(!doRules(H_NTERMINAL,3)) { return false; } else { if(strcmp(variableBuffer[variableBufferPtr],"null")!=0) { char *hPlace = newTemp(); emit(lastOp[OpPtr--],variableBuffer[variableBufferPtr--],variableBuffer[variableBufferPtr--],hPlace); strcpy(variableBuffer[++variableBufferPtr],hPlace); } else { variableBufferPtr--; } strcpy(lastOp[++OpPtr],"-"); } break; case R21: if(!doRules(H_NTERMINAL,0)) { return false; } else { strcpy(variableBuffer[++variableBufferPtr],"null"); } break; case R22: if(!doRules(Z_NTERMINAL,2)) { return false; } else { if(strcmp(variableBuffer[variableBufferPtr],"null")!=0) { char *zPlace = newTemp(); emit(lastOp[OpPtr--],variableBuffer[variableBufferPtr--],variableBuffer[variableBufferPtr--],zPlace); strcpy(variableBuffer[++variableBufferPtr],zPlace); } else { variableBufferPtr--; } } break; case R23: if(!doRules(V_NTERMINAL,3)) { return false; } else { if(strcmp(variableBuffer[variableBufferPtr],"null")!=0) { char *vPlace = newTemp(); emit(lastOp[--OpPtr],variableBuffer[variableBufferPtr--],variableBuffer[variableBufferPtr--],vPlace); strcpy(variableBuffer[++variableBufferPtr],vPlace); } else { variableBufferPtr--; } strcpy(lastOp[++OpPtr] ,"*"); } break; case R24: if(!doRules(V_NTERMINAL,3)) { return false; } else { if(strcmp(variableBuffer[variableBufferPtr],"null")!=0) { char *vPlace = newTemp(); emit(lastOp[--OpPtr],variableBuffer[variableBufferPtr--],variableBuffer[variableBufferPtr--],vPlace); strcpy(variableBuffer[++variableBufferPtr],vPlace); } else { variableBufferPtr--; } strcpy(lastOp[++OpPtr] ,"/"); } break; case R25: case R26: if(!doRules(U_NTERMINAL,1)) { return false; } break; case R27: if(!doRules(U_NTERMINAL,3)) { return false; } break; case R28: strcpy(lastOp[++OpPtr],"<"); if(!doRules(Y_NTERMINAL,1)) { return false; } break; case R29: if(!doRules(Y_NTERMINAL,1)) { return false; } strcpy(lastOp[++OpPtr],"<="); break; case R30: if(!doRules(Y_NTERMINAL,1)) { return false; } strcpy(lastOp[++OpPtr],">"); break; case R31: if(!doRules(Y_NTERMINAL,1)) { return false; } strcpy(lastOp[++OpPtr],">="); break; case R32: if(!doRules(Y_NTERMINAL,1)) { return false; } strcpy(lastOp[++OpPtr],"=="); break; case R33: if(!doRules(Y_NTERMINAL,1)) { return false; } strcpy(lastOp[++OpPtr],"!="); break; case R34: if(!doRules(V_NTERMINAL,0)) { return false; } else { strcpy(variableBuffer[++variableBufferPtr],"null"); } break; default: return false; } return true; }
int runGame(SDL_Surface* screen) { if(gameState==GAMESTATEPLAYING) { getInpPointerState()->escEnable=1; //Handle input int lim=1; //Limit cursor travel... int goUp=0, goDown=0, goLeft=0, goRight=0; if( getButton( C_UP ) ) { restartConfirm=0; if( getBtnTime( C_UP ) > REPEATDELAY ) { goUp=1; } else if(getBtnTime(C_UP)==0) { goUp=1; lim=0; } } if( getButton( C_DOWN ) ) { restartConfirm=0; if( getBtnTime( C_DOWN ) > REPEATDELAY ) { goDown=1; } else if(getBtnTime(C_DOWN)==0) { goDown=1; lim=0; } } if( getButton( C_LEFT ) ) { restartConfirm=0; if( getBtnTime( C_LEFT ) > REPEATDELAY ) { goLeft=1; } else if(getBtnTime(C_LEFT)==0) { goLeft=1; lim=0; } } if( getButton( C_RIGHT ) ) { restartConfirm=0; if( getBtnTime( C_RIGHT ) > REPEATDELAY ) { goRight=1; } else if(getBtnTime(C_RIGHT)==0) { goRight=1; lim=0; } } //Pause ? if( getButton( C_BTNMENU ) || isPointerEscapeClicked() ) { resetBtn( C_BTNMENU ); gamePause(screen); return(STATEMENU); } //Retry if( getButton( C_BTNSELECT ) || (getInpPointerState()->timeSinceMoved<POINTER_SHOW_TIMEOUT && isBoxClicked(&ptrRestartRect)) ) { resetBtn( C_BTNSELECT ); resetMouseBtn(); if(!restartConfirm) { restartConfirm=1; } else if(restartConfirm) { gameRestart(screen); } } //Handle mouse input if( getInpPointerState()->timeSinceMoved==0 && !cur.lock ) { setCursor(&cur, getInpPointerState()->curX,getInpPointerState()->curY ); } if(!getInpPointerState()->isDown) { mouseGrab=0; //Allow moving the cursor around with the input device when no brick is below, just for effect } else { brickType* b=brickUnderCursor(&pf, cur.x,cur.y); //We're over a brick, tell curser it's position, it will be locked later because we grab it now if( b ) { getInpPointerState()->startX=b->dx; getInpPointerState()->startY=b->dy; if( !cur.lock ) { mouseGrab=1; getInpPointerState()->startX=getInpPointerState()->curX; getInpPointerState()->startY=getInpPointerState()->curY; } else { if( b->dx > getInpPointerState()->curX ) { //Drag Left if( b->dx == b->sx && getInpPointerState()->startX != getInpPointerState()->curX ) { goLeft=1; } } else if( b->dx < getInpPointerState()->curX ) { //Drag Right if( b->dx == b->sx && getInpPointerState()->startX != getInpPointerState()->curX ) { goRight=1; } } } } else { mouseGrab=0; } } // printf("x:%i\n", getInpPointerState()->curX ); //Drag if( getButton( C_BTNX ) || getButton( C_BTNB ) || mouseGrab || isPointerClicked() ) { //Remove "Restart" question restartConfirm=0; //Magnet to brick if it's moving brickType* b=brickUnderCursor(&pf, cur.dx, cur.dy); if( !cur.lock && b ) { //Attach cursor cur.lock=1; b->curLock=1; cur.x=cur.dx; cur.y=cur.dy; cur.px = b->pxx-4; cur.py = b->pxy-4; sndPlay( SND_BRICKGRAB, cur.px ); } int movedBrick=0; //We're holding a brick, and it's not falling if( b ) { if( (goRight && curMoveBrick(&pf,b, DIRRIGHT)) || (goLeft && curMoveBrick(&pf,b, DIRLEFT)) ) { movedBrick=1; b->curLock=1; cur.lock=1; } } //Moved brick if(movedBrick) { player()->hsEntry.moves++; sndPlay(SND_BRICKMOVE, cur.px); ps.layer=PSYS_LAYER_TOP; ps.x=b->pxx; ps.y=b->pxy+18; ps.vel=50; ps.life=500; ps.lifeVar=250; ps.gravity=1; ps.srcImg=stealGfxPtr()->tiles[b->type-1]->img; ps.srcRect=stealGfxPtr()->tiles[b->type-1]->clip; ps.srcRect.y += 18; ps.srcRect.h = 2; spawnParticleSystem(&ps); } } else { cur.lock=0; } if(!cur.lock) { if( goLeft ) moveCursor(&cur, DIRLEFT, 0, lim); if( goRight ) moveCursor(&cur, DIRRIGHT, 0, lim); if( goUp ) moveCursor(&cur, 0, DIRUP, lim); if( goDown ) moveCursor(&cur, 0, DIRDOWN, lim); } //Sim first, so moving blocks get evaluated before getting moved again simField(&pf, &cur); //Do rules int ret=doRules(&pf); //Draw scene draw(&cur,&pf, screen); //Draw a path to show where we are pulling the brick if( mouseGrab ) drawPath( screen, getInpPointerState()->startX,getInpPointerState()->startY,getInpPointerState()->curX,getInpPointerState()->startY,1 ); //If no more bricks, countdown time left. if(ret == NOBRICKSLEFT) { if( !justWon ) { sndPlay(SND_WINNER,160); } justWon++; pf.levelInfo->time -= 1000; player()->hsEntry.score +=1; if(getButton(C_BTNX) || getButton(C_BTNB) || isPointerClicked() ) { resetBtn(C_BTNX); resetBtn(C_BTNB); resetMouseBtn(); while(pf.levelInfo->time > 0) { player()->hsEntry.score +=1; pf.levelInfo->time -= 1000; } } if(justWon > 50) { sndPlayOnce(SND_SCORECOUNT, 160); } if(pf.levelInfo->time < 1) { //Completed level player()->timeouts=0; pf.levelInfo->time=0; sndPlay(SND_VICTORY, 160); if(!player()->inEditor) { //Don't submit if it was from the leveleditor statsSubmitBest(); setMenu(menuStateFinishedLevel); if(pf.levelInfo->stopImg) { gameState=GAMESTATESTOPIMAGE; return(STATEPLAY); } } else { setLevelCompletable(pf.levelInfo->file, 1); } cleanUpGame(); startTransition(screen, TRANSITION_TYPE_ROLL_IN, 700); return(STATEMENU); } } else if(ret > 0) //Player destroyed bricks. { if(ret > 2) //Check for combo's { ///TODO: Some nice text effect? How about dissolving an image into a particle system? printf("%i Combo!\n",ret); player()->hsEntry.combos++; } player()->hsEntry.score += ret*ret*11*(player()->level+1); } //if ret > -1 then ret == number of bricks destroyed if(ret>-1) { //Update time: pf.levelInfo->time -= getTicks(); player()->hsEntry.time += getTicks(); if(pf.levelInfo->time < 1 && ret!=NOBRICKSLEFT ) { countdown=4000; gameState=GAMESTATEOUTOFTIME; if( !player()->inEditor ) { player()->timeouts++; } sndPlay(SND_TIMEOUT, 160); } } //Check if level is unsolvable. if(ret==UNSOLVABLE) { countdown=2000; gameState=GAMESTATEUNSOLVABLE; if( !player()->inEditor ) { player()->timeouts++; } sndPlay(SND_LOSER, 160); } else if(ret==LIFELOST) { countdown=2000; gameState=GAMESTATELIFELOST; if( !player()->inEditor ) { player()->timeouts++; } sndPlay(SND_LOSER, 160); } //Draw question if(restartConfirm) { sprintf(buf,STR_GAME_RESTARTWARNING); txtWriteCenter(screen, GAMEFONTMEDIUM, buf, HSCREENW, HSCREENH-20); sprintf(buf,STR_GAME_RESTARTCONFIRM); txtWriteCenter(screen, GAMEFONTSMALL, buf, HSCREENW, HSCREENH); } else { //Draw text drawUi(screen); } //Show the restart icon if(getInpPointerState()->timeSinceMoved<POINTER_SHOW_TIMEOUT && getInpPointerState()->escEnable) { SDL_Rect ptrRestartRectC = ptrRestartRect; SDL_BlitSurface( ptrRestart,NULL, screen, &ptrRestartRectC ); } } else if(gameState==GAMESTATECOUNTDOWN) { draw(&cur,&pf, screen); countdown -=getTicks(); if( getButton( C_BTNMENU ) ) { resetBtn( C_BTNMENU ); countdownSeconds=0; countdown=0; } if( (getButton( C_BTNX ) || getButton( C_BTNB ) || getInpPointerState()->isDown ) && countdownSeconds ) { countdownSeconds=0; countdown=500; } drawShowCountDown(screen, countdownSeconds); drawUi(screen); if(countdown < 1) { countdown=1000; countdownSeconds--; if(countdownSeconds == -1) { gameState=GAMESTATEPLAYING; return(STATEPLAY); } if(countdownSeconds==0) { countdown=500; sndPlay(SND_START, 160); } else { sndPlay(SND_COUNTDOWNTOSTART, 160); } } } else if(gameState==GAMESTATEOUTOFTIME) //Menu was last in "Entering level" so it will return to that if timeout { draw(&cur,&pf, screen); //drawUi(screen); countdown-=getTicks(); //Offer to skip after dying twice on same level, but only if it is not the last level in the pack. if( player()->timeouts > 1 && player()->level+1 < getNumLevels() ) { int skipLevel = skipLevelDialog(screen); if( skipLevel==1 ) { gameState=GAMESTATESKIPLEVEL; countdown=500; } txtWriteCenter(screen, GAMEFONTMEDIUM, STR_GAME_OUTOFTIME, HSCREENW,HSCREENH-24-31); } else { if( lostLifeMsg(&cur, &pf, screen, STR_GAME_OUTOFTIME, "lostlife-timeout" ) ) { return(STATEMENU); } } } else if(gameState==GAMESTATEUNSOLVABLE) //The same as out-of-time, but with another graphics. { draw(&cur,&pf, screen); //drawUi(screen); countdown-=getTicks(); //Offer to skip after dying twice on same level, but only if it is not the last level in the pack. if( player()->timeouts > 1 && player()->level+1 < getNumLevels() ) { int skipLevel = skipLevelDialog(screen); if( skipLevel==1 ) { gameState=GAMESTATESKIPLEVEL; countdown=500; } txtWriteCenter(screen, GAMEFONTMEDIUM, buf, HSCREENW,HSCREENH-24-31); } else { if( lostLifeMsg(&cur, &pf, screen, STR_GAME_UNSOLVABLE, "lostlife-unsolvable" ) ) { return(STATEMENU); } } } else if(gameState==GAMESTATELIFELOST) { if( lostLifeMsg(&cur, &pf, screen, STR_GAME_LOSTLIFE, "lostlife-evilbrick" ) ) { return(STATEMENU); } } else if(gameState==GAMESTATESTARTIMAGE) { if(!startStopImg) { startStopImgCounter=0; startStopImg = loadImg( packGetFile("themes/", pf.levelInfo->startImg) ); if(!startStopImg) { printf("Couldn't load '%s'\n",packGetFile("themes/", pf.levelInfo->startImg)); } } startStopImgCounter+=getTicks(); if((startStopImgCounter > 500 && ( getButton(C_BTNB) || isPointerClicked() ) ) || !startStopImg) { if(startStopImg) SDL_FreeSurface(startStopImg); startStopImg=0; resetBtn(C_BTNB); resetMouseBtn(); gameState=GAMESTATECOUNTDOWN; } SDL_BlitSurface( startStopImg, 0, screen, &(setting()->bgPos) ); if(startStopImgCounter>4000) txtWriteCenter(screen, GAMEFONTSMALL, STR_GAME_PRESSB, HSCREENW,HSCREENH+80); } else if(gameState==GAMESTATESTOPIMAGE) { if(!startStopImg) { startStopImgCounter=0; startStopImg = loadImg( packGetFile("themes/", pf.levelInfo->stopImg) ); if(!startStopImg) { printf("Couldn't load '%s'\n",packGetFile("themes/", pf.levelInfo->stopImg)); } } startStopImgCounter+=getTicks(); if((startStopImgCounter > 500 && (getButton(C_BTNB) || isPointerClicked() ) ) || !startStopImg) { if(startStopImg) SDL_FreeSurface(startStopImg); startStopImg=0; resetBtn(C_BTNB); resetMouseBtn(); cleanUpGame(); startTransition(screen, TRANSITION_TYPE_ROLL_IN, 700); return(STATEMENU); } SDL_BlitSurface( startStopImg, 0, screen, &(setting()->bgPos) ); if(countdownSeconds>4000) txtWriteCenter(screen, GAMEFONTSMALL, STR_GAME_PRESSB, HSCREENW,HSCREENH+80); } else if(gameState==GAMESTATESKIPLEVEL) { doRules(&pf); simField(&pf, &cur); draw(&cur,&pf, screen); countdown-=getTicks(); if( countdown < 1 ) { countdown=500; if( boardDestroyNextBrick(&pf) == 0 ) { //Tell that we chose to skip level statsUpload(player()->level, player()->hsEntry.time, player()->hsEntry.moves,player()->hsEntry.combos,player()->hsEntry.score, "skip-level",0, NULL); pf.levelInfo->time=0; player()->hsEntry.score=0; cleanUpGame(); startTransition(screen, TRANSITION_TYPE_ROLL_IN, 700); clearParticles(); setMenu(menuStatePrepareNextLevel); return(STATEMENU); } } drawUi(screen); } return(STATEPLAY); }