void EvaluationQueue::Clear() { while(!Empty()) RemoveFirst(); m_size = 0; m_workingGroupChanged = false; }
Node *RemoveNodeByIndex(LinkedList *pLinkedList, int index) { if (index < 0) return NULL; if (index == 0) { return RemoveFirst(pLinkedList); } /* Below is the index > 0*/ Node *previousNode = pLinkedList->head; if (previousNode == NULL) return NULL; /* NULL linked list, nothing can be removed. */ Node *node = previousNode->next; if (node == NULL) return NULL; /* One node list, index should be 0, can not be great than 0. */ // if no return at this time, at least there are two nodes in the list now, remember it. while (--index) { previousNode = node; node = node->next; if (node == NULL) return NULL; /* index > size of linked list*/ } if (node->next == NULL) { previousNode->next = NULL; } else { previousNode->next = node->next; } return node; }
void RouteDetectSrc(unsigned short src){ int i; unsigned short curr,cursor_ID; fwdneigh * queue=NULL; for(i=0;i<network[src].n_customers;i++){ curr=network[src].customers[i]; network[src].FIB[curr]=1; queue=AddList(curr,queue); } while(queue!=NULL){ curr=queue->ID; queue=RemoveFirst(queue); for(i=0;i<network[curr].n_customers;i++){ cursor_ID = network[curr].customers[i]; if( network[src].FIB[cursor_ID] > 1 ){ network[src].FIB[cursor_ID]=1; queue=AddList(cursor_ID,queue); } } } for(i=0;i<network[src].n_peers;i++){ curr=network[src].peers[i]; if( network[src].FIB[curr] > 2 ){ network[src].FIB[curr]=2; queue=AddList(curr,queue); } } while(queue!=NULL){ curr=queue->ID; queue=RemoveFirst(queue); for(i=0;i<network[curr].n_customers;i++){ cursor_ID = network[curr].customers[i]; if( network[src].FIB[cursor_ID] > 2 ){ network[src].FIB[cursor_ID]=2; queue=AddList(cursor_ID,queue); } } } }
~ObjectPool() { // Acquire the lock. Will be automatically released by the destructor. SpinLock lock(&lock_); while(Count() > 0) { RemoveFirst(); DeallocateBlock(static_cast<BlockHeader*>(First())); } }
void DLList::RemoveAll(int value) { bool removed = false; int count = size; do { count = size; removed = false; RemoveFirst(value); if (count != size) { removed = true; } } while (removed); }
void Stripe::Update() { m_textureOffset += m_textureOffsetSpeed; if( m_isAnimating ) m_interpolationValue += m_animationSpeed; if( m_interpolationValue >= 1 && m_isAnimating ) { m_interpolationValue = 1; // delete the first element ( this should be the "PRESS ANY KEY" text) RemoveFirst(); m_isAnimating = false; m_hasAnimated = true; m_textureOffsetSpeed = 0; m_textureOffset = 0; RefreshMenuItemPositions(); } }
void List::RemoveAt( int index ) { if( index == 0 ) RemoveFirst(); else if( index == count - 1 ) RemoveLast(); else { int curItemIdx = 0; ListItem* curItem = first; while( curItem != 0 && curItemIdx < index ) { curItem = curItem->nextItem; curItemIdx++; } curItem->previousItem->nextItem = curItem->nextItem; curItem->nextItem->previousItem = curItem->previousItem; free( (void*)curItem ); count--; } }
/** * Return 1 means success to remove while 0 means fail to remove. */ int RemoveNode(LinkedList *pLinkedList, Node *pDeleteNode) { Node *node = pLinkedList->head; if (node == NULL) return 0; // Remove first. if (node == pDeleteNode) { RemoveFirst(pLinkedList); return 1; } while (node->next != NULL) { if (node->next == pDeleteNode) { if (node->next->next == NULL) { node->next = NULL; } else { node->next = node->next->next; } return 1; } node = node->next; } return 0; }
void tcpConnection() { if(client) { static boolean hello = false; if(client.connected()) { if(client.available()) { if(!hello) { Serial.println("client present"); client.println("Hello!"); hello = true; } else { char s = client.read(); if(!(s == 13 || s == 10)) { stringbuilder += s; } if(s == '\n' && stringbuilder != "") { Serial.println(stringbuilder); Serial.println(Contains(stringbuilder, ",")); if(Contains(stringbuilder, ",")) { int id = stringbuilder.substring(IndexOf(stringbuilder, ",")).toInt(); client.println(id); stringbuilder = RemoveFirst(stringbuilder, id + ","); client.println(stringbuilder); int cyc = stringbuilder.substring(IndexOf(stringbuilder, ",")).toInt(); client.println(id); stringbuilder = RemoveFirst(stringbuilder, id + ","); client.println(stringbuilder); int inter = stringbuilder.substring(0).toInt(); createTask(id, cyc, inter); client.print("Created task("); client.print(id); client.print(","); client.print(cyc); client.print(","); client.print(inter); client.println(")"); } else { if(stringbuilder.toInt() >= 0) { tone(SPK, stringbuilder.toInt(), 200); } } stringbuilder = ""; } } } } else { Serial.println("client disconnected"); client.stop(); hello = false; } } else { client = server.available(); } }
/* * Total Order Broadcast with good throughput. * Note that like every functions defined here, they have the type NodesFct. */ void TOBThroughputBroadcast(int id, Message m){ PipelineAckData_t data; int argSize, ackOrigin; char *event, *eventArg, *msgTxt; Message mOut; NumberedMessage NumMsg, temp; if((data=(PipelineAckData_t)getData(id))==NULL){ //Initialization if((data=malloc(sizeof(struct _PipleineAckData_t)))==NULL){ fprintf(stderr,"malloc fail at TOBThroughputBroadcast\n"); exit(EXIT_FAILURE); } data->clock=0; data->next=(id+1)%getNbNodes(); // data->pred=(id==0)?getNbNodes()-1:id-1; data->pending=newSortedList(NumberedMsgComp); setData(id,data); } if(m==NULL){ // Event Rules if((event = getNextExternalEvent(id)) != NULL){ printf("event received %i %s\n",id, event); // Read the first event if(event==strstr(event, "broadcast")){ //there is an event : increment the clock data->clock++; //event is someting like broadcast <string> if((eventArg=malloc(sizeof(char)*maxArgSize))==NULL){ fprintf(stderr,"malloc fail at TOBThroughputBroadcast\n"); exit(EXIT_FAILURE); } if(1>sscanf(event, "broadcast %s", eventArg)){ sscanf("hello","%s",eventArg); } //eventArg is either the string to broadcast, or "hello" argSize=strlen(eventArg); if((msgTxt=malloc((intToStringSize+1+argSize)*sizeof(char)))==NULL){ fprintf(stderr,"malloc fail at TOBThroughputBroadcast\n"); exit(EXIT_FAILURE); } sprintf(msgTxt, "%d ",data->clock); strncat(msgTxt, eventArg, argSize); //Now the message text have the format: clk <arg> if((NumMsg=malloc(sizeof(struct _NumberedMessage)))==NULL){ fprintf(stderr,"malloc fail at TOBThroughputBroadcast\n"); exit(EXIT_FAILURE); } NumMsg->clk=data->clock; //waiting for 1 ack //creating the actual message NumMsg->m=initMessage(msgTxt,id,id,data->next); free(msgTxt); NumMsg->origin=id; if(data->next==id){ //we are the only process of the system deliver(NumMsg->m,id); free(NumMsg->m->msg); free(NumMsg->m); free(NumMsg); }else{ //and send it Send(copyMessage(NumMsg->m)); //Finally store the numbered msg in the pending list AddSorted(NumMsg,data->pending); } } free(event); } }else{ // Message Rules if((NumMsg=malloc(sizeof(struct _NumberedMessage)))==NULL){ fprintf(stderr,"malloc fail at TOBThroughputBroadcast\n"); exit(EXIT_FAILURE); } //this is a new message at the format: //clock <text> where text is either ack or the text of the //message //We have to parse the clock of the broadcast extractInt(m->msg, &(NumMsg->clk)); NumMsg->origin=m->origin; //don't forget to update the clock data->clock=MAX(NumMsg->clk,data->clock)+1; if(strstr(m->msg,"ack")==NULL){ printf("%s received by %d from %d but not delivered yet\n", m->msg, id, m->sender); //m is an actual message NumMsg->m=m; if(m->origin!=data->next){ //We need to forward the message mOut=initMessage(m->msg,m->origin,id,data->next); Send(mOut); AddSorted(NumMsg,data->pending); }else{ AddSorted(NumMsg,data->pending); //we deliver all the older messages mOut=NULL; while((temp=getFirst(data->pending))!=NULL && NumMsg!=NULL && NumberedMsgComp(temp,NumMsg)<=0){ //we remove the message from the pending list RemoveFirst(data->pending); if(!NumberedMsgComp(temp,NumMsg)){ //to ensure we stop after that NumMsg=NULL; } //prepare an ack if(temp->origin!=id){ if(mOut!=NULL){ free(mOut); } mOut=Ack(temp->clk,temp->origin,id,data->next); } //deliver deliver(temp->m,id); //we can free the message free(temp->m->msg); free(temp->m); free(temp); } //if we have managed to deliver a message //we acknowleged the older message delvered if(mOut){ Send(mOut); } } }else{ //this is an ack printf("%s received by %d from %d origin %d\n", m->msg, id, m->sender, m->origin); //we deliver all the older messages mOut=NULL; while((temp=getFirst(data->pending))!=NULL && NumMsg!=NULL && NumberedMsgComp(temp,NumMsg)<=0){ //we remove the message from the pending list RemoveFirst(data->pending); //prepare an ack ackOrigin=((temp->origin+getNbNodes()-1)%getNbNodes()); if(ackOrigin!=data->next){ if(mOut!=NULL){ free(mOut); } mOut=Ack(temp->clk,temp->origin,id,data->next); } //deliver deliver(temp->m,id); //we can free the message free(temp->m->msg); free(temp->m); free(temp); } //if we have managed to deliver a message //we acknowleged the older message delvered ackOrigin=((NumMsg->origin+getNbNodes()-1)%getNbNodes()); if(mOut){ Send(mOut); }else if(Size(data->pending)==0 && ackOrigin!=data->next){ //we haven't be able to deliver a message because our pending //list is empty, but the message may be blocked in someon else //queue so we forward the ack Send(Ack(NumMsg->clk,NumMsg->origin,id,data->next)); } // Free the local message free(m->msg); free(m); } } }
/* LazyEval() basically just sums up the material score on the board. It also * flags and scores passed pawns. It does this by remembering where the first * and last pawns of each colour in each file were and checking for pawns without * opposing blockers / flanking attackers. */ int LazyEval(Board *B) { BITBOARD pieces; int sq,p,Pawns[16],npawns=0,x, offside, wkx, bkx, dangerw=0, dangerb=0; int LastBlackPawn[8]={64,64,64,64,64,64,64,64},LastWhitePawn[8]={-1,-1,-1,-1,-1,-1,-1,-1}; int score=0,tpts=B->BPts + B->WPts; int wmin=0,wmaj=0,bmin=0,bmaj=0; #ifdef ExchangeOff int sdiff=0; #endif #ifdef DEBUG_EVAL int oldscore = 0, material=0; #endif B->Gamestage = Gamestage[B->WPts + B->BPts]; wmat = bmat = TRUE; /* Danger level of opposing pieces (for scoring connected PPs and offside majorities). */ if (B->WhiteQueens) dangerb=2; else if (B->WhiteRooks && B->WhiteBishops) dangerb=1; if (B->BlackQueens) dangerw=2; else if (B->BlackRooks && B->BlackBishops) dangerw=1; #ifdef DEBUG_EVAL fprintf(stdout,"Lazy Eval\n---------\n"); fprintf(stdout,"Game Stage = %d [0=Opening, 5=Late Endgame]\n",B->Gamestage); #endif /* Loop through all pieces, adding up their basic scores */ /* White pieces */ pieces = B->WhitePawns; while (pieces) { sq = FirstPiece(pieces); score += PAWN_SCORE + PawnPosition[B->Gamestage][sq]; LastWhitePawn[File(sq)] = sq; Pawns[npawns++] = sq; RemoveFirst(pieces); } pieces = B->WhiteRooks; while (pieces) { sq = FirstPiece(pieces); score += ROOK_SCORE + RookPosition[sq]; wmaj++; RemoveFirst(pieces); } pieces = B->WhiteKnights; while (pieces) { sq = FirstPiece(pieces); score += KNIGHT_SCORE + KnightPosition[sq]; wmin++; RemoveFirst(pieces); } pieces = B->WhiteBishops; while (pieces) { sq = FirstPiece(pieces); score += BISHOP_SCORE + BishopPosition[sq]; /* Penalise for getting trapped on a7 or h7 */ if (sq==a7 && B->pieces[b6]==bpawn) score -= BISHOP_TRAPPED; if (sq==h7 && B->pieces[g6]==bpawn) score -= BISHOP_TRAPPED; wmin++; RemoveFirst(pieces); } pieces = B->WhiteQueens; while (pieces) { sq = FirstPiece(pieces); score += QUEEN_SCORE + QueenPosition[sq]; /* Penalty for advancing too far too early */ if (B->Gamestage <= Middle && Rank(sq)<Rank5) score -= OVERSTRETCHED * (1 + Middle - B->Gamestage); /* Penalty for advancing before minor pieces */ if (B->Gamestage==Opening && Rank(sq)!=Rank1) { score -= (EARLY_QUEEN*Count((B->WhiteBishops | B->WhiteKnights) & RankMask[Rank1])); } /* Bonus for having a queen in the endgame */ if (tpts<40) score += (40-tpts) * LATE_QUEEN_BONUS; wmaj += 2; RemoveFirst(pieces); } /* Black pieces */ pieces = B->BlackPawns; while (pieces) { sq = FirstPiece(pieces); score -= (PAWN_SCORE + PawnPosition[B->Gamestage][(Flip[sq])]); if (LastBlackPawn[File(sq)] == 64) LastBlackPawn[File(sq)] = sq; Pawns[npawns++] = sq; RemoveFirst(pieces); } pieces = B->BlackRooks; while (pieces) { sq = FirstPiece(pieces); score -= (ROOK_SCORE + RookPosition[(Flip[sq])]); bmaj++; RemoveFirst(pieces); } pieces = B->BlackKnights; while (pieces) { sq = FirstPiece(pieces); score -= (KNIGHT_SCORE + KnightPosition[(Flip[sq])]); bmin++; RemoveFirst(pieces); } pieces = B->BlackBishops; while (pieces) { sq = FirstPiece(pieces); score -= (BISHOP_SCORE + BishopPosition[(Flip[sq])]); /* Penalise for getting trapped on a2 or h2 */ if (sq==a2 && B->pieces[b3]==wpawn) score += BISHOP_TRAPPED; if (sq==h2 && B->pieces[g3]==wpawn) score += BISHOP_TRAPPED; bmin++; RemoveFirst(pieces); } pieces = B->BlackQueens; while (pieces) { sq = FirstPiece(pieces); score -= (QUEEN_SCORE + QueenPosition[(Flip[sq])]); /* Penalty for advancing too far too early */ if (B->Gamestage <= Middle && Rank(sq) > Rank4) score += OVERSTRETCHED * (1 + Middle - B->Gamestage); /* Penalty for advancing before minor pieces */ if (B->Gamestage==Opening && Rank(sq)!=Rank8) { score -= (EARLY_QUEEN*Count((B->BlackKnights | B->BlackBishops) & RankMask[Rank8])); } /* Bonus for having a queen in the endgame */ if (tpts<40) score -= (40-tpts) * LATE_QUEEN_BONUS; bmaj += 2; RemoveFirst(pieces); } /* King locations */ switch (B->Gamestage) { case (Opening) : score += CornerBoard3[B->WhiteKing] - CornerBoard3[B->BlackKing]; break; case (EarlyMid) : score += CornerBoard2[B->WhiteKing] - CornerBoard2[B->BlackKing]; break; case (Middle) : score += CornerBoard2[B->WhiteKing] - CornerBoard2[B->BlackKing]; break; case (LateMid) : score += CornerBoard[B->WhiteKing] - CornerBoard[B->BlackKing]; break; case (Endgame) : score += CentreBoard[B->WhiteKing] - CentreBoard[B->BlackKing]; break; case (LateEnd) : score += CentreBoard2[B->WhiteKing] - CentreBoard2[B->BlackKing]; break; } wkx = File(B->WhiteKing); bkx = File(B->BlackKing); // Store piece counts B->wmaj = wmaj; B->wmin = wmin; B->bmaj = bmaj; B->bmin = bmin; #ifdef DEBUG_EVAL for (sq=0;sq<64;sq++) { if (B->pieces[sq]>0) material += PieceValue100[(B->pieces[sq])]; else material -= PieceValue100[-(B->pieces[sq])]; } fprintf(stdout,"\nMaterial Eval : %d\n",material); fprintf(stdout,"Positional Eval : %d\n",score-material); #endif /* King Safety */ if ((bmaj*2+bmin)>3) { #ifdef DEBUG_EVAL fprintf(stdout,"White King Defence : "); #endif score += KingDefence(B,B->WhiteKing,WHITE); } if ((wmaj*2+wmin)>3) { #ifdef DEBUG_EVAL fprintf(stdout,"Black King Defence : "); #endif score -= KingDefence(B,B->BlackKing,BLACK); } /* Reset Passed Pawn Bitboards */ PassedMask = HiddenMask = EMPTY; /* Cycle through the pawns we found earlier. Check for passed pawns. */ if (B->Gamestage != Opening) { for (p=0;p<npawns;p++) { sq = Pawns[p]; x = File(sq); if (B->pieces[sq]==wpawn) { if (LastBlackPawn[x]>sq && (x==0 || LastBlackPawn[x-1]>=(sq-1)) && (x==7 || LastBlackPawn[x+1]>sq)) { PassedMask |= Mask[sq]; if (FileDownMask[sq] & B->WhiteRooks) score += ROOK_BEHIND_PP; } /* Check for disguised passed pawns, where only one of a pair of connected pawns * is blocked by an opposing pawn. Clearly, one of the two will be able to convert * to a passer, provided the pawn on the open file has no attackers on its other * side. Only consider pawns where the blocker is on its 3rd rank or less. */ else if (Rank(sq)<Rank4 && Mask[sq-8]&B->BlackPawns && !(FileUpMask[sq-8]&B->BlackPawns) && ((x>0 && B->pieces[sq+7]==wpawn && !(FileUpMask[sq+7]&B->BlackPawns) && (x==1 || !(FileUpMask[sq+6]&B->BlackPawns)) || (x<7 && B->pieces[sq+9]==wpawn && !(FileUpMask[sq+9]&B->BlackPawns) && (x==6 || !(FileUpMask[sq+10]&B->BlackPawns)))))) { HiddenMask |= Mask[sq]; } } else { if (LastWhitePawn[x]<sq && (x==0 || LastWhitePawn[x-1]<sq) && (x==7 || LastWhitePawn[x+1]<=(sq+1))) { PassedMask |= Mask[sq]; if (FileUpMask[sq] & B->BlackRooks) score -= ROOK_BEHIND_PP; } /* Check for disguised passed pawns, where only one of a pair of connected pawns * is blocked by a opposing pawn. Clearly, one of the two will be able to convert * to a passer, provided the pawn on the open file has no attackers on its other * side. Only consider pawns where the blocker is on its 3rd rank or less. */ else if (Rank(sq)>Rank5 && Mask[sq+8]&B->WhitePawns && !(FileDownMask[sq+8]&B->WhitePawns) && ((x>0 && B->pieces[sq-9]==bpawn && !(FileDownMask[sq-9]&B->WhitePawns) && (x==1 || !(FileDownMask[sq-10]&B->WhitePawns)) || (x<7 && B->pieces[sq-7]==bpawn && !(FileDownMask[sq-7]&B->WhitePawns) && (x==6 || !(FileDownMask[sq-6]&B->WhitePawns)))))) { HiddenMask |= Mask[sq]; } } } } /* Encourage Trade Off if Ahead */ #ifdef ExchangeOff if (B->WPts != B->BPts) { /* Calculate half the sum of the scores of pieces captured so far this game, * and subtract it from the loser's score. Hence the winner is encouraged to * trade off to lower total scores. */ sdiff = (78 - (B->WPts + B->BPts)) >> 1; if (B->WPts > B->BPts) score += sdiff; else score -= sdiff; #ifdef DEBUG_EVAL if (B->WPts > B->BPts) fprintf(stdout,"Trade Off score : %d\n",sdiff); else fprintf(stdout,"Trade Off score : %d\n",-sdiff); #endif }
/* Evaluate the current board position for the side stored in the board * structure. Score for white. (Score for black = -1* this.) We also pass the values * of alpha and beta to this routine on the expectation that we might be able to get a * cutoff if the score is particularly high or low without having to do the time consuming * part of the eval(). */ int EvalMain(Board *B, int alpha, int beta) { int sq,p,npw=0,tpts=B->WPts+B->BPts,sq2,def, score=0; int *SpDef, *SpWon, lazyscore; BITBOARD pieces = B->All; BOOL one_sided; #ifdef DEBUG_EVAL int control = 0, oldscore = 0, contestcount = 0; #endif /* Check for a drawn position */ if (IsDrawnMaterial(B)) return ((Current_Board.side == WHITE) ? (DRAW_SCORE) : -(DRAW_SCORE)); /* Check if the game is theoretically won/lost */ score = IsWonGame(B); #ifdef DEBUG_EVAL if (score != 0) fprintf(stdout,"\nPosition Theoretically Won : %d\n\n",score); #endif /* Check to see if we can just cutoff here - this score is so good that * we needn't bother working it out exactly - it's going to cause a cutoff. * We have to be very careful because the score difference gained by doing * a proper eval here might be huge, therefore we only cutoff if this * position is theoretically won, and beta isn't, or it is theoretically * lost and alpha isn't. We can't just use standard futility cutoffs like * we do below, because in theoretically won positions, the score returned * by LazyEval() will almost always be much larger than EVAL_FUTILITY. */ if (USE_EVAL_SC && score!=0 && ((score > 0 && beta<T_WIN_BOUND) || (score < 0 && alpha>-(T_WIN_BOUND)))) { EvalCuts++; #ifdef DEBUG_EVAL fprintf(stdout,"Early Cut [1] %d (A=%d,B=%d)\n",score,alpha,beta); #endif return score; } /* Get a lazy score evaluation * (material score & simple positional terms plus passed pawns) */ lazyscore = LazyEval(B) + score; /* Check to see if we can just cutoff here. The expectation is that the LazyEval * is alway within EVAL_FUTILITY of the true score. Of course this isn't always * true, but we hope that it is true most of the time. */ if (USE_EVAL_SC && (lazyscore > (beta+EVAL_FUTILITY) || lazyscore < (alpha-EVAL_FUTILITY))) { EvalCuts++; #ifdef DEBUG_EVAL fprintf(stdout,"Early Cut [2] %d (A=%d,B=%d)\n",lazyscore,alpha,beta); #endif return lazyscore; } /* We didn't get a cutoff so we have to be more careful and evaluate the board properly. * Begin with the LazyEval() score we obtained BEFORE any possible truncation due to lack * of mating material (we don't want to do this twice!). */ score += prematscore; #ifdef DEBUG_EVAL fprintf(stdout,"\nFull Eval\n---------\n"); #endif /* Generate arrays storing how many times each side attacks each square on the board. * This takes quite some time, but it is worth the effort! */ GenerateAttacks(B); /* Now loop through all the pieces on the board and sum their contributions. * Also include the contributions of the empty spaces, measuring board * domination and territorial control. * There is the specialised version for the endgame below. */ if (B->Gamestage < Endgame) { /* Work out how much square possession is worth */ SpWon = SpaceWon[B->Gamestage]; SpDef = SpaceDefended[B->Gamestage]; /* Loop through the board */ for (sq=0;sq<64;sq++) { p = B->pieces[sq]; #ifdef DEBUG_EVAL fprintf(stdout,"Square %c%d [",File(sq)+97,8-Rank(sq)); PrintPiece(p); fprintf(stdout,"] : "); #endif switch(p) { /* White Piece */ case (wpawn) : score += TactPawn(sq,B,WHITE); npw += SquareColour[sq]; break; case (wrook) : score += TactRook(sq,B,WHITE); break; case (wknight): score += TactKnight(sq,B,WHITE); break; case (wbishop): score += TactBishop(sq,B,WHITE); break; case (wqueen) : score += TactQueen(sq,B,WHITE); break; case (wking) : score += TactKing(sq,B,WHITE); break; /* Black Piece */ case (bpawn) : score -= TactPawn(sq,B,BLACK); npw += SquareColour[sq]; break; case (brook) : score -= TactRook(sq,B,BLACK); break; case (bknight): score -= TactKnight(sq,B,BLACK); break; case (bbishop): score -= TactBishop(sq,B,BLACK); break; case (bqueen) : score -= TactQueen(sq,B,BLACK); break; case (bking) : score -= TactKing(sq,B,BLACK); break; /* Empty square - reward for possession by one side */ case (empty) : sq2=ConvertSq[sq]; #ifdef DEBUG_EVAL oldscore = score; #endif /* If only one side is attacking a square, then it is won * by that side. Otherwise it can only be defended. */ one_sided = (!(B->WAttacks[sq2]) || !(B->BAttacks[sq2])); def = B->WAttacks[sq2] - B->BAttacks[sq2]; if (one_sided) { if (def>0) score += SpWon[sq]; else if (def<0) score -= SpWon[Flip[sq]]; } else { /* We have no clear winner, so evaluate the ownership of the square (slow!!) */ def = EvaluateOwnership(B,sq); if (def > 0) score += SpDef[sq]; else if (def < 0) score -= SpDef[Flip[sq]]; #ifdef DEBUG_EVAL contestcount++; #endif } #ifdef DEBUG_EVAL control += score-oldscore; fprintf(stdout,"%d", score-oldscore); #endif break; } #ifdef DEBUG_EVAL fprintf(stdout,"\n"); #endif } } /* If we're in the endgame then use this simpler version */ else { /* Loop through the pieces */ while (pieces) { sq = FirstPiece(pieces); p = B->pieces[sq]; #ifdef DEBUG_EVAL fprintf(stdout,"Square %c%d [",File(sq)+97,8-Rank(sq)); PrintPiece(p); fprintf(stdout,"] : "); #endif switch(p) { /* White Piece */ case (wpawn) : score += TactPawn(sq,B,WHITE); break; case (wrook) : score += TactRook(sq,B,WHITE); break; case (wknight): score += TactKnight(sq,B,WHITE); break; case (wbishop): score += TactBishop(sq,B,WHITE); break; case (wqueen) : score += TactQueen(sq,B,WHITE); break; case (wking) : score += TactKing(sq,B,WHITE); break; /* Black Piece */ case (bpawn) : score -= TactPawn(sq,B,BLACK); break; case (brook) : score -= TactRook(sq,B,BLACK); break; case (bknight): score -= TactKnight(sq,B,BLACK); break; case (bbishop): score -= TactBishop(sq,B,BLACK); break; case (bqueen) : score -= TactQueen(sq,B,BLACK); break; case (bking) : score -= TactKing(sq,B,BLACK); break; } RemoveFirst(pieces); #ifdef DEBUG_EVAL fprintf(stdout,"\n"); #endif } } #ifdef DEBUG_EVAL fprintf(stdout,"After Piece Tactics : %d\n",score); fprintf(stdout,"Control Balance = %d\n",control); fprintf(stdout,"Contested Empty Squares : %d\n\n",contestcount); #endif /* Add on general positional score */ if (B->Gamestage <= Middle) score += TacticsPositional(B); /* -- General score modifiers -- */ /* Modifiers for pawns blocking bishops. Your pawns should be on different squares to your * own bishops, but the same as your opponent's */ /* White Bishops */ if (B->WhiteBishops) { /* Only black square bishops */ if (!(B->WhiteBishops & WhiteMask)) score += npw*PAWN_BLOCK; /* Only white square bishops */ else if (!(B->WhiteBishops & BlackMask)) score -= npw*PAWN_BLOCK; /* Bonus for having two bishops (or more) on opposite colours */ else score += TWO_BISHOPS; #ifdef DEBUG_EVAL if (npw!=0) { if (!(B->WhiteBishops & WhiteMask)) fprintf(stdout,"White has only black square bishops : Pawn Block = %d\n",-npw*PAWN_BLOCK); else if (!(B->WhiteBishops & BlackMask)) fprintf(stdout,"White has only white square bishops : Pawn Block = %d\n",npw*PAWN_BLOCK); } if ((B->WhiteBishops & WhiteMask) && (B->WhiteBishops & BlackMask)) fprintf(stdout,"White has a bishop pair [+%d]\n",TWO_BISHOPS); #endif } /* Black Bishops */ if (B->BlackBishops) { /* Only black square bishops */ if (!(B->BlackBishops & WhiteMask)) score -= npw*PAWN_BLOCK; /* Only white square bishops */ else if (!(B->BlackBishops & BlackMask)) score += npw*PAWN_BLOCK; /* Penalty for opponent having two bishops (or more) on opposite colours */ else score -= TWO_BISHOPS; #ifdef DEBUG_EVAL if (npw!=0) { if (!(B->BlackBishops & WhiteMask)) fprintf(stdout,"Black has only black square bishops : Pawn Block = %d\n",npw*PAWN_BLOCK); else if (!(B->BlackBishops & BlackMask)) fprintf(stdout,"Black has only white square bishops : Pawn Block = %d\n",-npw*PAWN_BLOCK); } if ((B->BlackBishops & WhiteMask) && (B->BlackBishops & BlackMask)) fprintf(stdout,"Black has a bishop pair [-%d]\n",TWO_BISHOPS); #endif } /* Penalty for having no pawns */ if (!B->WhitePawns) score -= NO_PAWNS; if (!B->BlackPawns) score += NO_PAWNS; /* If there is no mating material for one side then the score cannot favour that side * (Note - we calculated this in the LazyEval and retained the results) */ if (!wmat) score = min(0,score); if (!bmat) score = max(0,score); #ifdef DEBUG_EVAL if (!wmat) fprintf(stdout,"No mating material for white\n"); if (!bmat) fprintf(stdout,"No mating material for black\n"); #endif #ifdef DRAWISH_ENDINGS /* Test for a 'drawish' ending, and reduce the score if so */ if (Drawish(B)) score = (score * (50+tpts)) / 100; #ifdef DEBUG_EVAL if (Drawish(B)) fprintf(stdout,"Drawish Position (score reduced)\n"); #endif #endif #ifdef DEBUG_EVAL fprintf(stdout,"Final Score : %d [Delta %d]\n",score,score-lazyscore); #endif // Return score, possibly altered by the skill level if (Skill == 10) return score; return score + Random(((10-Skill) * 4) + 1) + 2 * Skill - 20; }
/* * InitProcess -- initialize a per-process data structure for this backend */ void InitProcess(void) { /* use volatile pointer to prevent code rearrangement */ volatile PROC_HDR *procglobal = ProcGlobal; int i; /* * ProcGlobal should be set up already (if we are a backend, we inherit * this by fork() or EXEC_BACKEND mechanism from the postmaster). */ if (procglobal == NULL) elog(PANIC, "proc header uninitialized"); if (MyProc != NULL) elog(ERROR, "you already exist"); MyProc = RemoveFirst(); if (MyProc == NULL) { ereport(FATAL, (errcode(ERRCODE_TOO_MANY_CONNECTIONS), errmsg("sorry, too many clients already"))); } if (gp_debug_pgproc) { elog(LOG, "allocating PGPROC entry for pid %d, freeProcs (prev offset, new offset): (%ld, %ld)", MyProcPid, MAKE_OFFSET(MyProc), MyProc->links.next); } set_spins_per_delay(procglobal->spins_per_delay); int mppLocalProcessSerial = gp_atomic_add_32(&procglobal->mppLocalProcessCounter, 1); lockHolderProcPtr = MyProc; /* Set the next pointer to INVALID_OFFSET */ MyProc->links.next = INVALID_OFFSET; /* * Initialize all fields of MyProc, except for the semaphore which was * prepared for us by InitProcGlobal. */ SHMQueueElemInit(&(MyProc->links)); MyProc->waitStatus = STATUS_OK; MyProc->xid = InvalidTransactionId; MyProc->xmin = InvalidTransactionId; MyProc->pid = MyProcPid; /* databaseId and roleId will be filled in later */ MyProc->databaseId = InvalidOid; MyProc->roleId = InvalidOid; MyProc->inVacuum = false; MyProc->postmasterResetRequired = true; MyProc->lwWaiting = false; MyProc->lwExclusive = false; MyProc->lwWaitLink = NULL; MyProc->waitLock = NULL; MyProc->waitProcLock = NULL; for (i = 0; i < NUM_LOCK_PARTITIONS; i++) SHMQueueInit(&(MyProc->myProcLocks[i])); /* * mppLocalProcessSerial uniquely identifies this backend process among * all those that our parent postmaster process creates over its lifetime. * * Since we use the process serial number to decide if we should * deliver a response from a server under this spin, we need to * assign it under the spin lock. */ MyProc->mppLocalProcessSerial = mppLocalProcessSerial; /* * A nonzero gp_session_id uniquely identifies an MPP client session * over the lifetime of the entry postmaster process. A qDisp passes * its gp_session_id down to all of its qExecs. If this is a qExec, * we have already received the gp_session_id from the qDisp. */ elog(DEBUG1,"InitProcess(): gp_session_id %d", gp_session_id); if (Gp_role == GP_ROLE_DISPATCH && gp_session_id == -1) gp_session_id = mppLocalProcessSerial; MyProc->mppSessionId = gp_session_id; MyProc->mppIsWriter = Gp_is_writer; /* * We might be reusing a semaphore that belonged to a failed process. So * be careful and reinitialize its value here. (This is not strictly * necessary anymore, but seems like a good idea for cleanliness.) */ PGSemaphoreReset(&MyProc->sem); /* Set wait portal (do not check if resource scheduling is enabled) */ MyProc->waitPortalId = INVALID_PORTALID; MyProc->queryCommandId = -1; /* * Arrange to clean up at backend exit. */ on_shmem_exit(ProcKill, 0); /* * Now that we have a PGPROC, we could try to acquire locks, so initialize * the deadlock checker. */ InitDeadLockChecking(); }