void Blinky::ai(BricksVec bricks, Player* player, PPoint* point) { switch (getState()) { case ATTACK: findDirection(bricks, player->getPosition()); break; case DEFENCE: findDirection(bricks, DEFENCE_POINT->multiply(getWidth())); break; case DEAD: findDirection(bricks, START_POINT->multiply(getWidth())); break; } move(bricks); }
void Pinky::ai(World* world) { switch (getState()) { case ATTACK: findDirection(world, findPathFourStep(world), this); break; case DEFENCE: findDirection(world, DEFENCE_POINT->multiply(getWidth()), this); break; case DEAD: findDirection(world, START_POINT->multiply(getWidth()), this); break; } move(world); }
bool SimulatedPermissiveRuleChecker:: visit(ConcretePiece<PawnTag> const& pawn) const{ size_t pawnPos=simulatedIndex(simulated_, toIndex(pawn.x(),pawn.y())); size_t finalPos = simulatedIndex(simulated_, destination_); Move move(pawnPos, finalPos); Directions moveDirection = findDirection(move); size_t distance = traversedSquares(move); /* Refusons tous les déplacements "vers l'arrière". */ if(moveDirection * pawn.multiplier() > 63) return false; /* acceptons les déplacement en ligne droite */ if(abs(moveDirection) == 8){ /* Les déplacements d'une ou de deux cases sont autorisés * si le pion n'a pas encore bougé. */ if(! pawn.hasMoved()){ return distance == 1 || distance == 2; } else{ return distance == 1; } } /* Acceptons les déplacements d'une case en diagonale. */ if(abs(moveDirection) == frontLeft || abs(moveDirection) == frontRight){ return distance == 1; } /* Tous les autres déplacements sont interdits. */ return false; }
bool SimulatedPermissiveRuleChecker:: visit(ConcretePiece<BishopTag> const& bishop) const{ size_t bishopPos=simulatedIndex(simulated_, toIndex(bishop.x(),bishop.y())); size_t finalPos = simulatedIndex(simulated_, destination_); Move move(bishopPos, finalPos); Directions moveDirection = findDirection(move); size_t distance = traversedSquares(move); /* Seuls les déplacements en diagonale sont autorisés pour les * fous */ return ( moveDirection == frontLeft || moveDirection == backLeft || moveDirection == frontRight || moveDirection == backRight ) && distance > 0 && distance < 8; }
bool SimulatedPermissiveRuleChecker:: visit(ConcretePiece<TowerTag> const& tower) const{ size_t towerPos=simulatedIndex(simulated_, toIndex(tower.x(),tower.y())); size_t finalPos = simulatedIndex(simulated_, destination_); Move move(towerPos, finalPos); Directions moveDirection = findDirection(move); size_t distance = traversedSquares(move); /* Seuls les déplacements en droite ligne sont autorisés * pour les tours. */ return ( moveDirection == front || moveDirection == back || moveDirection == left || moveDirection == right) && distance > 0 && distance < 8; }
bool SimulatedPermissiveRuleChecker:: visit(ConcretePiece<QueenTag> const& queen) const{ size_t queenPos=simulatedIndex(simulated_, toIndex(queen.x(),queen.y())); size_t finalPos = simulatedIndex(simulated_, destination_); Move move(queenPos, finalPos); Directions moveDirection = findDirection(move); size_t distance = traversedSquares(move); /* les huit grandes directions sont autorisées pour les dames */ return ( moveDirection == front || moveDirection == back || moveDirection == left || moveDirection == right || moveDirection == frontLeft || moveDirection == backLeft || moveDirection == frontRight || moveDirection == backRight ) && distance > 0 && distance < 8; }
void Path::findPath(int x, int y) { Destination *dest = Path::findNeighbors(x, y); Destination * current = &Destination(x, y, 0, 0); if (dest == NULL) { for (int i = 0; i < signed(destinations->size()); i++) destinations->at(i)->Print(); std::cout << "Could not find location" << std::endl; return; } Direction direction; int distance = 0; for (int i = 0; i < 120; i++) { direction = findDirection(current->x, current->y, dest->x, dest->y); distance = findDistance(current->x, current->y, dest->x, dest->y); //const char *arr[] = {"Left", "Right", "Up", "Down"}; //std::cout << "Going " << arr[direction] << "distance " << distance << std::endl; roadMap.push_back(new Destination(dest->x, dest->y, direction, distance * GRANULARITY)); current = dest; int random = rand() % current->neighbors.size(); dest = current->neighbors.at(random); } }
/* * findTilt() : Angle of tilt calculated with distance * from corners (max/min_x/y) * * findDirection() : Direction of tilt calculated with * distance from side mid points (max/min_x/y, center_x/y) * * min_x, min_y center_x,min_y max_x, min_y * +---#-----+---------+ * | | | * | x2,y2 | x1,y1 # * | | | * min_x,center_y +---------+---------+ max_x,center_y * | | | * # x3,y3 | x4,y4 | * | | | * +---------+-----#---+ * min_x, max_y center_x,max_y max_x, max_y * * * # - tilted anchor corner points ( x1/2/3/4, y1/2/3/4 ) * */ int Shape::findTilt() { int x1 = center_x, y1 = center_y, p1 = grid_w * grid_h; int x2 = center_x, y2 = center_y, p2 = grid_w * grid_h; int x3 = center_x, y3 = center_y, p3 = grid_w * grid_h; int x4 = center_x, y4 = center_y, p4 = grid_w * grid_h; int angle = 0, a1=0, a2=0, a3=0, a4=0; int x=0, y=0, p=0; // p : roximity to corner int bbx = (max_x - min_x)/3, bby = (max_y - min_y)/3; //inner bounding box int direction = 1; if(debug){ cout << endl << "[W " ; int c = max_y-min_y; for(int i = 1; i < c/2; i++){ cout << widths_at_y[i] << "~" << widths_at_y[c-i] << " "; } cout << "W] " << endl; cout << endl << "[H " ; c = max_x-min_x; for(int i = 1; i < c/2; i++){ cout << heights_at_x[i] << "~" << heights_at_x[c-i] << " "; } cout << "H] " << endl; } /* * loop through all border pixels * only select pixels close to bounding box edge * (selected by outside inner bounding box) * for each quandrant measure disance from coner * closeset to corner for each quandrant is picked as * the anchor corner point */ int i = 0; while( i < mapcount ){ x = xmap[i]; y = ymap[i]; if( abs(x-center_x) > bbx || abs(y-center_y) > bby){//close to edge if( x > center_x && y <= center_y ){ //top right Q1 p = (max_x - x) + (y - min_y); if(p < p1){ p1 = p; x1 = x; y1 = y; } }else if( x <= center_x && y <= center_y ){ //top left Q2 p = (x - min_x) + (y - min_y); if(p < p2){ p2 = p; x2 = x; y2 = y; } }else if( x <= center_x && y > center_y ){ //bot left Q3 p = (x - min_x) + (max_y - y); if(p < p3){ p3 = p; x3 = x; y3 = y; } }else if( x > center_x && y > center_y ){ //bot right Q4 p = (max_x - x) + (max_y - y); if(p < p4){ p4 = p; x4 = x; y4 = y; } } } i++; } a1 = findAngle(x2, y2, x1, y1); a2 = findAngle(x1, y1, x4, y4); a3 = findAngle(x4, y4, x3, y3); a4 = findAngle(x3, y3, x2, y2); angle = isDiagonal(a1, a2, a3, a4) ? maxAngle(a1, a2, a3, a4) : (a1+a2+a3+a4)/4; if(angle != 45 ) direction = findDirection(x1, y1, x2, y2, x3, y3, x4, y4); angle*=direction; if(debug) cout << "[ " << angle << " | " << a1 << " " << a2 << " " << a3 << " " << a4 << " ]" << endl ; if( pixdebug ){ d_pixmap->clearPixmap(); d_markAnchor(); d_pixmap->setPen(0, 255, 0); d_pixmap->markPoint( x1, y1, 6); d_pixmap->setPen(0, 0, 255); d_pixmap->markPoint( x4, y4, 6); d_pixmap->setPen(0, 255, 255); d_pixmap->markPoint( x3, y3, 6); d_pixmap->setPen(0, 0, 0); d_pixmap->markPoint( x2, y2, 6); d_pixmap->writeImage("corners"); } return angle; }
/** * Main robot function that controls the behaviour of the robot. * Needs a connected socket to communicate with the simulator or HW */ void robot(int sck, FILE *sckfd) { int val1; int curX = 2; int curY = 0; int direction = NORTH; initRobot(sck, 100, curX, curY); //initGrid(); int nummoves = 0; // Challenge A/B int destX = 5, destY = 5; FILE *file; file = fopen("log.txt", "w"); // Loop until destination is reached while (1) { // system("cls"); int nextX, nextY, dir, rotation; calculateRoute(curX, curY, destX, destY); d_printGrid(curX, curY); if (!nodes[curX][curY]->next) { fprintf(stderr, "No route found\n"); sendExit(sck); return; } nextX = nodes[curX][curY]->next->pos.x; nextY = nodes[curX][curY]->next->pos.y; dir = findDirection(curX, curY, nextX, nextY); rotation = dir - direction; if (rotation > 2) rotation -= 4; if (rotation < -2) rotation += 4; fprintf(stderr, "Next (%d,%d), rotation: %d\n", nextX, nextY, dir); if (rotation < 0) { //turn left fprintf(file, "Turn left %d\r\n", abs(rotation)); setCommand(sck, TURN, 1, abs(rotation), 0); } else if (rotation > 0) {//turn right fprintf(file, "Turn right %d\r\n", abs(rotation)); setCommand(sck, TURN, 2, rotation, 0); } fflush(file); // wait until robot is ready with turning if (rotation != 0) { while (1) { getCommand(sck, sckfd, READY, 1, &val1, NULL); if (val1 == 1) break; } fprintf(file, "Ready!\r\n"); } // Update direction direction = dir; if (curY == 0 || curY == 6 || curX == 0 || curX == 6) nummoves = 1; else nummoves = 2; printf("Cur (%d,%d) Nummoves: %d\n", curX, curY, nummoves); printf("Move forward\n"); fprintf(file, "Move forward %d\r\n", nummoves); fflush(file); setCommand(sck, MOVE, 1, nummoves, 0); if (nummoves == 1) { // Wait while not ready while (1) { getCommand(sck, sckfd, READY, 1, &val1, NULL); if (val1 == 1) { break; } } curX = nextX; curY = nextY; } else if (nummoves == 2) { // Wait while not ready while (1) { getCommand(sck, sckfd, READY, 1, &val1, NULL); if (val1 == 1) { break; } } // Check for mine printf("Check for mine\n"); getCommand(sck, sckfd, MINE, 1, &val1, NULL); if (val1 == 1) { //removeConnection(x1, y1, x2, y2); // Clear mine flag setCommand(sck, MINE, 1, 0, 0); fprintf(file, "Mine found!\r\n"); printf("Mine found!\n"); // Get back!:D //return 0; } else { printf("No mine found!\n"); } } } fclose(file); sendExit(sck); }
int makeMove(int x1, int y1, int x2, int y2) { int val1; printf("Making move from (%d,%d) to (%d,%d)\n", x1, y1, x2, y2); setCommand(new_sck, MINE, 1, 0, 0); // getchar(); // Dafuq why would you call me?! if(x1==x2 && y1==y2) return 1; // (if needed) Turn and make the move int dir = findDirection(x1, y1, x2, y2); makeTurn(curDir, dir, x1, y1); // update direction curDir = dir; printf("Dir set to %d\n", dir); // getchar(); // Make 2 moves setCommand(new_sck, MOVE, 1, 2, 0); wait_till_ready(new_sck); printf("Check for mine\n"); getCommand(new_sck, sckfd, MINE, 1, &val1, NULL); // A mine was found, we're not on our destination if (val1 == 1) { //wait_till_ready(new_sck); printf("Mine found!\n"); // getchar(); // Move in reverse setCommand(new_sck, MOVE, 1, 4, 0); wait_till_ready(new_sck); setCommand(new_sck, MINE, 1, 0, 0); dir=curDir; return 0; } // We're on the destination else { printf("No mine found!\n"); // Check wether we are heading to our final destination if(isPlaceToVisit(x2, y2)) { // Turn into exit #ifdef SMART_VISIT if (x2 == 4){ if (curDir != EAST) { makeTurn(curDir, EAST, x1, y1); curDir = EAST; } }else if(x2 == 0){ if (curDir != WEST) { makeTurn(curDir, WEST, x1, y1); curDir = WEST; } }else if(y2 == 0){ if (curDir != SOUTH) { makeTurn(curDir, SOUTH, x1, y1); curDir = SOUTH; } }else if(y2 == 4){ if (curDir != NORTH) { makeTurn(curDir, NORTH, x1, y1); curDir = NORTH; } } #else if (x2 == 4){ makeTurn(curDir, EAST, x1, y1); curDir = WEST; }else if(x2 == 0){ makeTurn(curDir, WEST, x1, y1); curDir = EAST; }else if(y2 == 0){ makeTurn(curDir, SOUTH, x1, y1); curDir = NORTH; }else if(y2 == 4){ makeTurn(curDir, NORTH, x1, y1); curDir = SOUTH; } // Move until white is seen by sensor setCommand(new_sck, MOVE, 1, 3, 0); wait_till_ready(new_sck); // Draai 180 graden setCommand(new_sck, TURN, 2, 1, 0); wait_till_ready(new_sck); // Ga weer terug naar kruispunt setCommand(new_sck, MOVE, 1, 1, 0); wait_till_ready(new_sck); #endif } return 1; } return 0; }