int main(void) { struct room *start_room; /* store start room */ struct room *goal_room; /* store goal room */ int numRooms; int numDoors; char input_name[21]; int input_doughnuts; int intput_milkshakes; char start[21]; char goal[21]; /*scan number of rooms from input and check if valid */ scanf("\n%d", &numRooms); if(numRooms<=0){ printf("\nerror"); return -1; } base = (struct list *)malloc(sizeof(struct list)); createRooms(numRooms); /* //loop that creates new room for(int i=1; i<= numRooms;i++){ scanf("\n%20s %d %d, &input_name, &input_doughnuts); } */ /*scan number of doors from input and check if valid */ scanf("\n%d", &numDoors); if(numDoors<=0){ printf("\nerror"); return -1; } createDoors(numDoors); /* scan START and GOAL rooms */ scanf("\n%s %s",&start, &goal); start_room = find_room(start, base); goal_room = find_room(goal, base); if(start_room == NULL || goal_room == NULL){ printf("\nerror"); return -1; } pointer = start_room; while(scanf("\n%c", &move)!=EOF){ command(); if(pointer == NULL){ return -1; } else if(pointer == goal_room){ printf("\nwon"); return 0; } else{ printf("\n %s %d %d %d %d", pointer->name, pointer->num_doughnuts, pointer->num_milkshakes, player_doughnuts, player_milkshakes); } } delete_rooms(base); return 0; }
Level MapGenerator:: createLevel(int seed) { if (seed == -1) srand(time(nullptr)); else srand(seed); rows_ = 40; cols_ = 80; canvas_ = new char[rows_ * cols_]; for(int i = 0; i < rows_; i++) for (int j = 0; j < cols_; j++) canvas_[i * cols_ + j] = Wall; int prevCol, prevRow, currRow, currCol; for (int i = 0; i < 10; i++) { prevRow = currRow; prevCol = currCol; currRow = rand() % (rows_ / 2 - 1); currRow = currRow * 2 + 1; currCol = rand() % (cols_ / 2 - 1); currCol = currRow * 2 + 1; int height = rand() % 7 + 3; int width = rand() % 7 + 3; createRectangularRoom(currRow, currCol, width, height); // Brutally add corridors int dir; if (i > 0) { dir = prevRow > currRow ? -1 : 1; // Horizontal while (prevRow != currRow) { prevRow += dir; int tmp = prevRow * cols_ + prevCol; if (canvas_[tmp] == Wall) { if (canvas_[tmp - 1] != Floor && canvas_[tmp + 1] != Floor) canvas_[tmp] = Corridor; else canvas_[tmp] = Floor; } } // Vertical dir = prevCol > currCol ? -1 : 1; while (prevCol != currCol) { prevCol += dir; int tmp = prevRow * cols_ + prevCol; if (canvas_[tmp] == Wall) { if (canvas_[tmp - cols_] != Floor && canvas_[tmp + cols_] != Floor) canvas_[tmp] = Corridor; else canvas_[tmp] = Floor; } } } } createDoors(); clearCorridors(); Level result(canvas_, rows_, cols_); result.heroStartPos_ = prevRow * cols_ + prevCol; return result; }
/******************************************************************* City Modelling Program ********************************************************************/ ////////// #include <windows.h> #include <gl/gl.h> #include <gl/glu.h> //#include <gl/glut.h> #include "Dependencies\freeglut\freeglut.h" #include <stdlib.h> #include <stdio.h> #include <string.h> #include <math.h> #include <utility> #include <vector> #include "VECTOR3D.h" #include "TerrainGrid.h" #include "Mesh.h" #include "RGBpixmap.h" #include "ComplexObj.h" void initOpenGL(); void display(void); void reshape(int w, int h); void mouse(int button, int state, int x, int y); void mouseMotionHandler(int xMouse, int yMouse); void keyboard(unsigned char key, int x, int y); void functionKeys(int key, int x, int y); void timer(int value); VECTOR3D ScreenToWorld(int x, int y); void updateCameraPos(); void limitCameraAngle(); bool hasCollision(const Mesh* a, Mesh* b[], Mesh* c[], Mesh* d[], float offset, int sizeb, int sizec, int sized); bool hasObjCollision(const Mesh* a, ComplexObj *cobj[], int sizee); void createBoundaryWalls(); void createInnerWalls(); void createDoors(); static int currentButton; static unsigned char currentKey; #define M_PI 3.14159265358979323846 const float FPS = 30.0; // City Interaction State Variable enum Action {TRANSLATE, ROTATE, RAISE, SCALE, EXTRUDE, SELECT, MULTIPLESELECT, DESELECT_ALL, NAVIGATE}; enum Action currentAction = TRANSLATE; GLfloat light_position0[] = {12.0, 6.0,-12.0,1.0}; GLfloat light_position1[] = {-12.0, 6.0,12.0,1.0}; GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_ambient[] = {0.2, 0.2, 0.2, 1.0}; // Person mesh - head + body Mesh *personBody; GLUquadricObj *personHead; // City terrain mesh TerrainGrid *terrainGrid = NULL; int gridSize = 16; // Wall Meshes Mesh *boundaryWalls[4]; // boundary walls that surround the world Mesh *innerWalls[20]; // inner walls of the rooms & world Mesh *doors[20]; // inner walls of the rooms & world float wallHeight = 1.0f; // wall height of all (boundary, inner) walls int numBoundaryWalls = 0, numInnerWalls = 0, numDoors = 0; RGBpixmap floorPix[1], boundaryWallPix[1], innerWallPix[1], doorPix[2]; // Room Objects ComplexObj *cobj[12]; //int nextobj = 0; //index to objects array to store next cube ComplexObj *navobj; //selected object in NAVIGATE mode bool objCollision = false; // state to toggle avatar and object collision float trsize = 0.2, scsize = 0.2, rosize = 5.0; //translation, scaling and rotation angle deltas // Textures GLuint textureId; // Camera Control VECTOR3D lookFrom; VECTOR3D lookAt; VECTOR3D up; float radius = 8; // Camera Distance float lookFromx = 0; // Camera X Position float lookFromy = 0; // Camera Y Position float lookFromz = radius; // Camera Z Position float angleTheta = 0; // Camera X angle float anglePhi = 80; // Camera Y angle float upx = 0; // Up Vector float upy = 1; float upz = 0; float lookAtx = 0; // Camera is looking at float lookAty = 0; float lookAtz = 0; float camerax = 0; // Camera X Position float cameray = 0; // Camera Y Position float cameraz = radius; // Camera Z Position static float zoomFactor = 1.0; float xbefore; // Previous X position of tank float zbefore; // Previous Y position of tank float collision_offset = 0.4; //GLint glutWindowWidth = 750; //GLint glutWindowHeight = 500; GLint glutWindowWidth = 1200; GLint glutWindowHeight = 800; GLint viewportWidth = glutWindowWidth; GLint viewportHeight = glutWindowHeight; // Wolrd Boundaries GLdouble worldLeftBase = -8.0; GLdouble worldRightBase = 8.0; GLdouble worldBottomBase= -8.0; GLdouble worldTopBase = 8.0; // World view boundaries GLdouble wvLeftBase = worldLeftBase, wvRightBase = worldRightBase, wvBottomBase = worldBottomBase, wvTopBase = worldTopBase; int main(int argc, char **argv){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); //glutInitWindowSize(750, 500); glutInitWindowSize(glutWindowWidth, glutWindowHeight); glutInitWindowPosition(100, 100); glutCreateWindow("Room Navigator"); floorPix[0].readBMPFile("FloorWood01.bmp"); boundaryWallPix[0].readBMPFile("WallBrick02.bmp"); innerWallPix[0].readBMPFile("WallWood01.bmp"); doorPix[0].readBMPFile("Door01L.bmp"); doorPix[1].readBMPFile("Door01R.bmp"); initOpenGL(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutMotionFunc(mouseMotionHandler); glutKeyboardFunc(keyboard); glutSpecialFunc(functionKeys); glutTimerFunc(1000.0 / FPS, timer, 0); glutMainLoop(); return 0; } int width, height; VECTOR3D scale; VECTOR3D trans; VECTOR3D angles; // Setup openGL */ void initOpenGL(){ glViewport(0, 0, (GLsizei) viewportWidth, (GLsizei) viewportHeight); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0*zoomFactor,(float)viewportWidth/(float)viewportHeight,0.2,80.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // Set up and enable lighting glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position0); glLightfv(GL_LIGHT1, GL_POSITION, light_position1); glShadeModel(GL_SMOOTH); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); glClearColor(0.6, 0.6, 0.6, 0.0); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); //Nice perspective. glHint(GL_PERSPECTIVE_CORRECTION_HINT , GL_NICEST); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); updateCameraPos(); gluLookAt(lookFromx, lookFromy, lookFromz,lookAtx, lookAty, lookAtz, upx, upy, upz); //Texture glGenTextures(1, &textureId); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // store pixels by byte glBindTexture(GL_TEXTURE_2D, textureId); // select current texture (0) glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Set up Terrain Grid VECTOR3D origin = VECTOR3D(-16.0f, 0.0f, 16.0f); terrainGrid = new TerrainGrid(gridSize, 32.0); // gridsize = 16 terrainGrid->InitGrid(gridSize, origin, 32.0, 32.0); createBoundaryWalls(); // Create boundary walls createInnerWalls(); // create inner walls createDoors(); // create doors // Create a person (Head+Body) - initially oriented along z axis direction (-ve z dir (0,0,-1) & ccw => angles.y = 0) scale.x = 0.5; scale.y = 1.4; scale.z = 0.2; trans.x = -2.0; trans.y = 0; trans.z = 8.0; personBody = createMesh(scale, trans, 1.0, 1); personBody->angles.x = personBody->angles.y = personBody->angles.z = 0.0; personBody->selected = true; // Create room objects (4 kinds) for (int i = 0; i < 12; i++){ cobj[i] = createObj(); } createShield(cobj[0]); cobj[0]->translation.Set( 0.0, 2.0, 15.0); cobj[0]->angles.Set(0.0, 180.0, 0.0); createShield(cobj[1]); cobj[1]->translation.Set(-8.0, 2.0, -15.0); createShield(cobj[2]); cobj[2]->translation.Set( 8.0, 2.0, -15.0); createTable( cobj[3]); cobj[3]->translation.Set(14.0, 0.0, 2.0); createTable( cobj[4]); cobj[4]->translation.Set(14.0, 0.0,-2.0); createTable( cobj[5]); cobj[5]->translation.Set(-14.0,0.0,-2.0); createVase( cobj[6]); cobj[6]->translation.Set(14.0, 0.0, 4.0); createVase( cobj[7]); cobj[7]->translation.Set(-14.0, 0.0,-14.0); createVase( cobj[8]); cobj[8]->translation.Set(14.0, 0.0,-14.0); createPicFrame(cobj[9]); cobj[9]->translation.Set(8.0, 2.0, 15.0); cobj[9]->angles.Set(0.0, 180.0, 0.0); createPicFrame(cobj[10]); cobj[10]->translation.Set(-15.0, 2.0, -8.0); cobj[10]->angles.Set(0.0, 90.0, 0.0); createPicFrame(cobj[11]); cobj[11]->translation.Set(15.0, 2.0, -8.0); cobj[11]->angles.Set(0.0, -90.0, 0.0); }