void evolveCurve(t_List *L_in, t_List *L_out, t_RegionStats *inside, t_RegionStats *outside, char *bip1, t_jit_matrix_info* in1_minfo, char *phi_bp, t_jit_matrix_info* phi_minfo) { t_Node *nodeOut, *nodeIn, *tempNode; uchar val; nodeOut = L_out->lastNode; //evolve outside while(nodeOut) { tempNode = nodeOut->prev; val = *(uchar *)(bip1 + nodeOut->pixel[0] + (nodeOut->pixel[1])*in1_minfo->dimstride[1]); // post("swOut: %f", calcProb(inside, outside, val)); if( calcProb(inside, outside, val) > 1.0f) { switch_in(L_in, L_out, nodeOut, phi_bp, phi_minfo); outside->countTemp--; outside->sumTemp -= (double)val; outside->sumSqTemp -= (double)val*(double)val; inside->countTemp++; inside->sumTemp += (double)val; inside->sumSqTemp += (double)val*(double)val; } nodeOut = tempNode; } cleanListInterior(L_in, phi_bp, phi_minfo); nodeIn = L_in->lastNode; //evolve inside while(nodeIn) { tempNode = nodeIn->prev; val = *(uchar *)(bip1 + nodeIn->pixel[0] + (nodeIn->pixel[1])*in1_minfo->dimstride[1]); //post("swIn: %f", calcProb(inside, outside, val)); if( calcProb(inside, outside, val) < 1.0f) { switch_out(L_in, L_out, nodeIn, phi_bp, phi_minfo); outside->countTemp++; outside->sumTemp += (double)val; outside->sumSqTemp += (double)val*(double)val; inside->countTemp--; inside->sumTemp -= (double)val; inside->sumSqTemp -= (double)val*(double)val; } nodeIn = tempNode; } cleanListExterior(L_out, phi_bp, phi_minfo); }
char stoppingConditions(t_List *L_in, t_List *L_out, t_RegionStats *inside, t_RegionStats *outside, char *bip1, t_jit_matrix_info* in1_minfo, char *phi_bp, t_jit_matrix_info* phi_minfo) { t_Node *nodeOut, *nodeIn, *tempNode; uchar val; char stop = 1; nodeOut = L_out->lastNode; //evolve outside while(nodeOut) { tempNode = nodeOut->prev; val = *(uchar *)(bip1 + nodeOut->pixel[0] + (nodeOut->pixel[1])*in1_minfo->dimstride[1]); if( calcProb(inside, outside, val) > 1.0f) { stop = 0; break; } nodeOut = tempNode; } if(!stop) { return stop; } nodeIn = L_in->lastNode; //evolve inside while(nodeIn) { tempNode = nodeIn->prev; val = *(uchar *)(bip1 + nodeIn->pixel[0] + (nodeIn->pixel[1])*in1_minfo->dimstride[1]); if( calcProb(inside, outside, val) < 1.0f) { stop = 0; break; } nodeIn = tempNode; } return stop; }
void playerAction(Player player, int rank) { int recvbuf[SIZE_PLAYER_RECV]; int sendbuf[SIZE_PLAYER_SEND]; int teamId = rank / 5; int fieldProvider = teamId + 10; int ballPos[2]; int round = 1; int side = 0; int scorePost[2] = {0, 32}; if (teamId == TEAMA) { scorePost[0] = 128; scorePost[1] = 32; } do { MPI_Recv(recvbuf, SIZE_PLAYER_RECV, MPI_INT, fieldProvider, TAG_ROUND_START + round, MPI_COMM_WORLD, MPI_STATUS_IGNORE); ballPos[0] = recvbuf[0]; ballPos[1] = recvbuf[1]; sendbuf[INDEX_RANK] = rank; sendbuf[INDEX_CHALLENGE] = -1; sendbuf[INDEX_BALLX] = -1; sendbuf[INDEX_BALLY] = -1; int mov = player.speed; int diffX = ballPos[0] - player.x; int diffY = ballPos[1] - player.y; int absDiffX = abs(diffX); int absDiffY = abs(diffY); // Player movement strategy if (mov >= absDiffX + absDiffY) { player.x = ballPos[0]; player.y = ballPos[1]; mov = absDiffX + absDiffY; // generate ball challenge to shoot sendbuf[INDEX_CHALLENGE] = (rand()%10 + 1)*player.dribble; int dist = absDist(scorePost[0], scorePost[1], player.x, player.y); int prob = calcProb(player.shoot, dist); if (prob > 1) { // Chance to hoop the ball sendbuf[INDEX_BALLX] = scorePost[0]; sendbuf[INDEX_BALLY] = scorePost[1]; } else { int minDist = 10000, throwX = -1, throwY = -1; int i; for (i = 2; i < SIZE_PLAYER_RECV; i+=2) { if ((i-2)/2 == rank) continue; int pX = recvbuf[i]; int pY = recvbuf[i + 1]; int d = absDist(scorePost[0], scorePost[1], pX, pY); if (d < minDist) { minDist = d; throwX = pX; throwY = pY; } } sendbuf[INDEX_BALLX] = throwX; sendbuf[INDEX_BALLY] = throwY; } } else if (mov <= absDiffX) { player.x += mov*(diffX < 0 ? -1:1); } else if (mov <= absDiffY) { player.y += mov*(diffY < 0 ? -1:1); } else { player.x += diffX; player.y += (mov - absDiffX)*(diffY < 0 ? -1:1); } sendbuf[INDEX_NEWX] = player.x; sendbuf[INDEX_NEWY] = player.y; int fieldProcess = 10 + (player.x > 64); // Send field process data int j; MPI_Send(sendbuf, SIZE_PLAYER_SEND, MPI_INT, fieldProcess, TAG_PLAYER_SEND + round, MPI_COMM_WORLD); if (round == HALF_ROUNDS) { //swap scorePost if (teamId == TEAMA) { scorePost[0] = 0; scorePost[1] = 32; } else { scorePost[0] = 128; scorePost[1] = 32; } side = 1; } round++; } while(round <= 2*HALF_ROUNDS); }
void fieldAction(int rank, int teamPos[2][5][2], int teamSkill[2][5][3]) { int ballPos[2] = {64, 32}; int sendbuf[SIZE_PLAYER_RECV]; int recvbuf[SIZE_PLAYER_SEND*NUM_PLAYERS]; int teamId = rank - 10; int otherFieldRank = 21 - rank; int i, j; int round = 1; int teamPoints[NUM_TEAMS] = {0, 0}; int scorePost[2][2] = {{128, 32}, {0, 32}}; int side = 0; do { memset(recvbuf, 0, SIZE_PLAYER_SEND*NUM_PLAYERS*sizeof(int)); sendbuf[0] = ballPos[0]; sendbuf[1] = ballPos[1]; for (i = 2; i < 12; i+=2) { // Put pos of all players in the same team sendbuf[i] = teamPos[teamId][i/2 - 1][0]; sendbuf[i+1] = teamPos[teamId][i/2 - 1][1]; } // Send to one team for (i = 0; i < 5; i++) { MPI_Request tempReq; MPI_Isend(sendbuf, SIZE_PLAYER_RECV, MPI_INT, 5*teamId + i, TAG_ROUND_START + round, MPI_COMM_WORLD, &tempReq); MPI_Request_free(&tempReq); } MPI_Request playerReqs[NUM_PLAYERS]; MPI_Request otherField; for (i = 0; i < NUM_PLAYERS; i++) { MPI_Irecv(recvbuf + i*SIZE_PLAYER_SEND, SIZE_PLAYER_SEND, MPI_INT, i, TAG_PLAYER_SEND + round, MPI_COMM_WORLD, playerReqs + i); } int playersReceived[1] = {0}; int playersOtherReceived[1] = {0}; int flag = 0; MPI_Irecv(playersOtherReceived, 1, MPI_INT, 11 - teamId, TAG_FIELD_STAT + round, MPI_COMM_WORLD, &otherField); int outcount = 0; int outIndices[NUM_PLAYERS]; int playersReceivedArray[NUM_PLAYERS]; for (i = 0; i < NUM_PLAYERS; i++) { playersReceivedArray[i] = 0; } while (true) { MPI_Testsome(NUM_PLAYERS, playerReqs, &outcount, outIndices, MPI_STATUS_IGNORE); if (outcount == MPI_UNDEFINED || outcount > 0) { if (outcount == MPI_UNDEFINED) { outcount = 10 - playersReceived[0]; for (i = 0; i < 10; i++) { outIndices[i] = i; } } for (i = 0; i < outcount; i++) { playersReceivedArray[outIndices[i]] = 1; } playersReceived[0] += outcount; if (rank == F1) { MPI_Request tempReq; MPI_Isend(playersReceived, 1, MPI_INT, F0, TAG_FIELD_STAT + round, MPI_COMM_WORLD, &tempReq); MPI_Request_free(&tempReq); if (playersReceived[0] == NUM_PLAYERS) { MPI_Wait(&otherField, MPI_STATUS_IGNORE); } } else if (playersReceived[0] + playersOtherReceived[0] == NUM_PLAYERS) { MPI_Request tempReq; MPI_Isend(playersReceived, 1, MPI_INT, F1, TAG_FIELD_STAT + round, MPI_COMM_WORLD, &tempReq); MPI_Request_free(&tempReq); break; } } MPI_Test(&otherField, &flag, MPI_STATUS_IGNORE); if (flag) { if (rank == F1) { break; } if (playersReceived[0] + playersOtherReceived[0] == NUM_PLAYERS) { MPI_Request tempReq; MPI_Isend(playersReceived, 1, MPI_INT, F1, TAG_FIELD_STAT + round, MPI_COMM_WORLD, &tempReq); MPI_Request_free(&tempReq); break; } MPI_Irecv(playersOtherReceived, 1, MPI_INT, F1, TAG_FIELD_STAT + round, MPI_COMM_WORLD, &otherField); } } // Cancel all extra team recv requests for (i = 0; i < NUM_PLAYERS; i++) { if (playerReqs[i] != MPI_REQUEST_NULL) { MPI_Request_free(&playerReqs[i]); } } int numReached = 0; int reachedRanks[NUM_PLAYERS]; int maxBallChallenge = 1; for (i = 0; i < NUM_PLAYERS; i++) { if (playersReceivedArray[i]) { recvbuf[i*SIZE_PLAYER_SEND + INDEX_RANK] = FLAG_UPDATED; if (recvbuf[i*SIZE_PLAYER_SEND + INDEX_NEWX] == ballPos[0] && recvbuf[i*SIZE_PLAYER_SEND + INDEX_NEWY] == ballPos[1]) { int chall = recvbuf[i*SIZE_PLAYER_SEND + INDEX_CHALLENGE]; if (chall > maxBallChallenge) { maxBallChallenge = chall; numReached = 0; } reachedRanks[numReached++] = i; } } } int winnerRank = -1; if (numReached > 0) { winnerRank = reachedRanks[rand() % numReached]; int newBallPos[2]; newBallPos[0] = recvbuf[winnerRank*SIZE_PLAYER_SEND + INDEX_BALLX]; newBallPos[1] = recvbuf[winnerRank*SIZE_PLAYER_SEND + INDEX_BALLY]; int d = abs(recvbuf[winnerRank*SIZE_PLAYER_SEND + INDEX_NEWX] - newBallPos[0]) + abs(recvbuf[winnerRank*SIZE_PLAYER_SEND + INDEX_NEWY] - newBallPos[1]); int winnerShootSkill = teamSkill[winnerRank/5][winnerRank%5][2]; int prob = calcProb(winnerShootSkill, d); int hit = -1; if (prob > 0) { hit = rand() % (100 / prob); } ballPos[0] = newBallPos[0]; ballPos[1] = newBallPos[1]; if (hit != 0) { // Random 8 int x8 = rand() % 8 + 1; int y8 = rand() % 8 + 1; if (rand() % 2) x8 = -x8; if (rand() % 2) y8 = -y8; ballPos[0] = recvbuf[winnerRank*SIZE_PLAYER_SEND + INDEX_BALLX] + x8; ballPos[1] = recvbuf[winnerRank*SIZE_PLAYER_SEND + INDEX_BALLY] + y8; if (ballPos[0] < 0 || ballPos[1] < 0) { ballPos[0] = 0; ballPos[1] = 0; } else if (ballPos[0] > 128 || ballPos[1] > 64) { ballPos[0] = 128; ballPos[1] = 64; } } else { if (ballPos[0] == scorePost[winnerRank/5][0] && ballPos[1] == scorePost[winnerRank/5][1]) { ballPos[0] = 64; ballPos[1] = 32; teamPoints[winnerRank/5] += (d > 24) ? 3:2; } } } int adminDetails[SIZE_FIELD_ADMIN] = {winnerRank, ballPos[0], ballPos[1], teamPoints[0], teamPoints[1]}; int recvbuf2[NUM_PLAYERS*SIZE_PLAYER_SEND]; int adminDetails2[SIZE_FIELD_ADMIN]; memset(recvbuf2, 0, NUM_PLAYERS*SIZE_PLAYER_SEND*sizeof(int)); MPI_Request temp, temp2, roundOverRequest; MPI_Isend(adminDetails, SIZE_FIELD_ADMIN, MPI_INT, otherFieldRank, TAG_FIELD_SYNC_ADMIN + round, MPI_COMM_WORLD, &temp); MPI_Request_free(&temp); MPI_Isend(recvbuf, NUM_PLAYERS*SIZE_PLAYER_SEND, MPI_INT, otherFieldRank, TAG_FIELD_SYNC_PLAYER + round, MPI_COMM_WORLD, &temp2); MPI_Request_free(&temp2); MPI_Recv(adminDetails2, SIZE_FIELD_ADMIN, MPI_INT, otherFieldRank, TAG_FIELD_SYNC_ADMIN + round, MPI_COMM_WORLD, MPI_STATUS_IGNORE); MPI_Recv(recvbuf2, NUM_PLAYERS*SIZE_PLAYER_SEND, MPI_INT, otherFieldRank, TAG_FIELD_SYNC_PLAYER + round, MPI_COMM_WORLD, MPI_STATUS_IGNORE); if (adminDetails2[INDEX_WINNER_RANK] != -1) { winnerRank = adminDetails2[INDEX_WINNER_RANK]; ballPos[0] = adminDetails2[1]; ballPos[1] = adminDetails2[2]; teamPoints[0] = adminDetails2[3]; teamPoints[1] = adminDetails2[4]; } for (i = 0; i < NUM_PLAYERS; i++) { if (recvbuf2[i*SIZE_PLAYER_SEND + INDEX_RANK] == FLAG_UPDATED) { for (j = 1; j < SIZE_PLAYER_SEND; j++) { recvbuf[i*SIZE_PLAYER_SEND + j] = recvbuf2[i*SIZE_PLAYER_SEND + j]; } } } if (rank == F1) { int tempVal[1]; int iTeamId, iPlayerId; for (i = 0; i < NUM_PLAYERS; ++i) { iTeamId = i/5; iPlayerId = i%5; teamPos[iTeamId][iPlayerId][0] = recvbuf[i*SIZE_PLAYER_SEND + INDEX_NEWX]; teamPos[iTeamId][iPlayerId][1] = recvbuf[i*SIZE_PLAYER_SEND + INDEX_NEWY]; } MPI_Irecv(tempVal, 1, MPI_INT, F0, TAG_FIELD_ROUND_OVER + round, MPI_COMM_WORLD, &roundOverRequest); MPI_Wait(&roundOverRequest, MPI_STATUS_IGNORE); round++; continue; } // Printing function printf("%d\n", round); printf("%d %d\n", teamPoints[0], teamPoints[1]); printf("%d %d\n", ballPos[0], ballPos[1]); //New pos int newX, newY, iTeamId, iPlayerId; for (i = 0; i < NUM_PLAYERS; ++i) { iTeamId = i/5; iPlayerId = i%5; printf("%d ", i); // Player number printf("%d %d ", teamPos[iTeamId][iPlayerId][0], teamPos[iTeamId][iPlayerId][1]); teamPos[iTeamId][iPlayerId][0] = recvbuf[i*SIZE_PLAYER_SEND + INDEX_NEWX]; teamPos[iTeamId][iPlayerId][1] = recvbuf[i*SIZE_PLAYER_SEND + INDEX_NEWY]; printf("%d %d ", teamPos[iTeamId][iPlayerId][0], teamPos[iTeamId][iPlayerId][1]); printf("%d ", recvbuf[i*SIZE_PLAYER_SEND + INDEX_CHALLENGE] > -1); printf("%d ", winnerRank == i); printf("%d ", recvbuf[i*SIZE_PLAYER_SEND + INDEX_CHALLENGE]); if (winnerRank == i) { printf("%d ", recvbuf[i*SIZE_PLAYER_SEND + INDEX_BALLX]); printf("%d", recvbuf[i*SIZE_PLAYER_SEND + INDEX_BALLY]); } else { printf("-1 -1"); } printf("\n"); } if (round == HALF_ROUNDS) { scorePost[0][0] = 0; scorePost[0][1] = 32; scorePost[1][0] = 128; scorePost[1][1] = 32; } int tempVal[1] = {1}; MPI_Send(tempVal, 1, MPI_INT, F1, TAG_FIELD_ROUND_OVER + round, MPI_COMM_WORLD); round++; } while(round <= 2*HALF_ROUNDS); }