void game_hub::areaMenu() { bool areaMenu = true; while (areaMenu) { int area_sel = getSel("1. Go to dungeons", "2. Go to random dungeon", "3. Return to town"); switch(area_sel) { case 1: { printAreas(); printf("%d. Cancel\n", active_areas+1); int sel = getSel(active_areas+1); if (sel < active_areas+1) { sel--; //Dungeons[sel]->dungeon_loop(); Dungeons[sel]->enter_area(); areaMenu = false; } else printf("Cancelling action...\n"); break; } case 2: { rand_dungeon = new areas; rand_dungeon->setArea("Random Dungeon", 4, Team); //rand_dungeon->dungeon_loop(); rand_dungeon->enter_area(); areaMenu = false; delete rand_dungeon; rand_dungeon = NULL; break; } case 3: { printf("Returning to town...\n"); areaMenu = false; break; } } } incrementAreas(); }
int main(int argv, char **argc) { robot_if_t ri; int major, minor, x_dist_diff, square_count, prev_square_area_1 = 0, prev_square_area_2 = 0; IplImage *image = NULL, *hsv = NULL, *threshold_1 = NULL, *threshold_2 = NULL, *final_threshold = NULL; squares_t *squares, *biggest_1, *biggest_2, , *pair_square_1, *pair_square_2, *sq_idx; bool same_square; bool hasPair = 0; square_count = 0; // Make sure we have a valid command line argument if(argv <= 1) { printf("Usage: robot_test <address of robot>\n"); exit(-1); } ri_api_version(&major, &minor); printf("Robot API Test: API Version v%i.%i\n", major, minor); // Setup the robot with the address passed in if(ri_setup(&ri, argc[1], 0)) { printf("Failed to setup the robot!\n"); exit(-1); } // Setup the camera if(ri_cfg_camera(&ri, RI_CAMERA_DEFAULT_BRIGHTNESS, RI_CAMERA_DEFAULT_CONTRAST, 5, RI_CAMERA_RES_640, RI_CAMERA_QUALITY_LOW)) { printf("Failed to configure the camera!\n"); exit(-1); } // Create a window to display the output //cvNamedWindow("Rovio Camera", CV_WINDOW_AUTOSIZE); cvNamedWindow("Biggest Square", CV_WINDOW_AUTOSIZE); cvNamedWindow("Thresholded", CV_WINDOW_AUTOSIZE); // Create an image to store the image from the camera image = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 3); // Create an image to store the HSV version in // We configured the camera for 640x480 above, so use that size here hsv = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 3); // And an image for each thresholded version threshold_1 = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 1); threshold_2 = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 1); final_threshold = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 1); // Move the head up to the middle position ri_move(&ri, RI_HEAD_MIDDLE, RI_FASTEST); // Action loop do { // Update the robot's sensor information if(ri_update(&ri) != RI_RESP_SUCCESS) { printf("Failed to update sensor information!\n"); continue; } // Get the current camera image and display it if(ri_get_image(&ri, image) != RI_RESP_SUCCESS) { printf("Unable to capture an image!\n"); continue; } //cvShowImage("Rovio Camera", image); // Convert the image from RGB to HSV cvCvtColor(image, hsv, CV_BGR2HSV); // Pick out the first range of pink color from the image cvInRangeS(hsv, RC_PINK_LOW_1, RC_PINK_HIGH_1, threshold_1); // Pick out the second range of pink color from the image cvInRangeS(hsv, RC_PINK_LOW_2, RC_PINK_HIGH_2, threshold_2); // compute the final threshold image by using cvOr cvOr(threshold_1, threshold_2, final_threshold, NULL); cvShowImage("Thresholded", final_threshold); // Find the squares in the image squares = ri_find_squares(final_threshold, RI_DEFAULT_SQUARE_SIZE); if( squares != NULL ) { printf("Sorting squares!\n"); sort_squares(squares); printf("Sort Complete!\n"); printAreas(squares); printf("Done printing"); //find biggest pair (if it exists) sq_idx = squares; while(sq_idx != NULL){ if(sq_idx->next == NULL) break; else if(isPair(sq_idx, sq_idx->next, 0.75)){ hasPair = 1; break; } sq_idx = sq_idx->next; } printf("Pair ID complete!\n"); if(hasPair){ printf("Pair found.\n"); //draw_green_X(sq_idx, image); //draw_green_X(sq_idx->next, image); biggest_1 = sq_idx; biggest_2 = sq_idx->next; } else { printf("Pair not found. Marking largest.\n"); draw_red_X(squares, image); //temporary: biggest_1 = squares; biggest_2 = squares; } hasPair = 0; } else { printf("No squares found.\n"); } hasPair = 0; if(biggest_1 != NULL){ draw_green_X(biggest_1, image); printf("Area 1 = %d", biggest_1->area); } //we only see the last pair of squares, go straight ahead until IR_Detect stops the robot if (square_count == 3){ ri_move(&ri, RI_MOVE_FORWARD, 1); if (ri_IR_Detected(&ri)) { square_count++; printf("Object detected, square_count = %d\n", square_count); } } //once the robot is at the intersection, rotate right until it detects a pair of squares else if(square_count == 4){ printf("Rotating\n"); if (biggest_1 != NULL && biggest_2 != NULL && (biggest_1->area - biggest_2->area) < 500){ square_count++; printf("New Path Found\n"); } ri_move(&ri, RI_TURN_RIGHT, 7); } else{ /* * If we only find a single usable largest square: * if square is on left of screen, turn right, strafe right * if square is on right of screen, turn left, strafe left */ if(biggest_1 != NULL && biggest_2 != NULL ) { draw_red_X(biggest_2, image); printf("\tArea 2 = %d\n", biggest_2->area); //get the difference in distance between the two biggest squares and the center vertical line x_dist_diff = get_square_diffence(biggest_1, biggest_2, image); get_diff_in_y(biggest_1, biggest_2); //when the camera can't detect a pair of squares, which means now the second biggest square //is much smaller than the first biggest square if ((biggest_1->area - biggest_2->area) > 500){ //if both squares are at the left side of the center line if (biggest_1->center.x < image->width/2 && biggest_2->center.x < image->width/2){ printf("rotate right at speed = 6\n"); ri_move(&ri, RI_TURN_RIGHT, 6); } //if both squares are at the right side of the center line else if (biggest_1->center.x > image->width/2 && biggest_2->center.x > image->width/2){ printf("rotate left at speed = 6\n"); ri_move(&ri, RI_TURN_LEFT, 6); } //if the center line is in the middle of the two biggest squares else if (biggest_1->center.x < image->width/2 && biggest_2->center.x > image->width/2 ){ printf("rotate right at speed = 2\n"); ri_move(&ri, RI_TURN_RIGHT, 2); } else{ printf("rotate left at speed = 2\n"); ri_move(&ri, RI_TURN_LEFT, 2); } } else{ //increment square_count whenever the robot pass by a pair of squares if (prev_square_area_1 != 0 && prev_square_area_2 != 0 && biggest_1->area < prev_square_area_1 && biggest_2->area < prev_square_area_2 ){ square_count++; printf("square count = %d\n", square_count); } //rotate to the left if (x_dist_diff < -40){ printf("rotate left at speed = 6\n"); ri_move(&ri, RI_TURN_LEFT, 6); } //rotate to the right else if (x_dist_diff > 40){ printf("rotate right at speed = 6\n"); ri_move(&ri, RI_TURN_RIGHT, 6); } prev_square_area_1 = biggest_1->area; prev_square_area_2 = biggest_2->area; } ri_move(&ri, RI_MOVE_FORWARD, 5); } //once the camera can't detect any squares, make the robot go backwards else if (biggest_1 == NULL && biggest_2 == NULL){ printf("Move Backwards\n"); ri_move(&ri, RI_MOVE_BACKWARD , 1); } } // display a straight vertical line draw_vertical_line(image); // Display the image with the drawing oon ito cvShowImage("Biggest Square", image); // Update the UI (10ms wait) cvWaitKey(10); // Release the square data while(squares != NULL) { sq_idx = squares->next; free(squares); squares = sq_idx; } biggest_1 = NULL; biggest_2 = NULL; // Move forward unless there's something in front of the robot /*if(!ri_IR_Detected(&ri)) ri_move(&ri, RI_MOVE_FORWARD, RI_SLOWEST);*/ //printf("Loop Complete\n"); //getc(stdin); } while(1); // Clean up (although we'll never get here...) //cvDestroyWindow("Rovio Camera"); cvDestroyWindow("Biggest Square"); cvDestroyWindow("Thresholded"); // Free the images cvReleaseImage(&threshold_1); cvReleaseImage(&threshold_2); cvReleaseImage(&final_threshold); cvReleaseImage(&hsv); cvReleaseImage(&image); return 0; }