///////////////////////////////////////////////////////////// // // Source file for BulletinBoard // // This file is generated by RapidApp 1.2 // // This class is derived from BulletinBoardUI which // implements the user interface created in // RapidApp. This class contains virtual // functions that are called from the user interface. // // When you modify this source, limit your changes to // modifying the sections between the // "//---- Start/End editable code block" markers // // This will allow RapidApp to integrate changes more easily // // This class is a ViewKit user interface "component". // For more information on how components are used, see the // "ViewKit Programmers' Manual", and the RapidApp // User's Guide. ///////////////////////////////////////////////////////////// #include "BulletinBoard.h" #include #include #include #include #include #include #include #include extern void VkUnimplemented ( Widget, const char * ); /////////////////////////////////////////////////////////////////////////////// // The following non-container elements are created by BulletinBoardUI and are // available as protected data members inherited by this class // // XmToggleButton (LED style) _start // XmPushButton _level // XmPushButton _reset // XmPushButton _exit // GLwMDrawingArea _glwidget // /////////////////////////////////////////////////////////////////////////////// //---- Start editable code block: headers and declarations #include #include #include #include "XwindowSupport.h" #include #include #include #include //#include #define XWINDOWSIZE 620 #define YWINDOWSIZE 620 #define num_apples 2 #define num_obstacles 13 //---- End editable code block: headers and declarations int num_worms; void DrawGame(); void DrawHarderGame(); void HarderCollide(); void DrawApples(); void DrawWorm(); int Collide(); int WormCollide(); void Anim(void); void Path(); void VisibleGraph(); int Dist_2_Obst(float, float); struct p{ float x[15000]; float y[15000]; int steps; }; struct p path[num_apples]; struct polygons{ float x[5]; float y[5]; int size; }; struct polygons po[num_obstacles]; float firstx, firsty, secondx, secondy, thirdx, thirdy, fourthx, fourthy; float wormx, wormy, wormx_init, wormy_init; float applex[3], appley[3]; int ij; int id, anim; int paths; int num_for; int path_step; int minindx, minindy, indy, minind, minindo, minindpt, minobst, obst_distx, obst_disty; GLUquadricObj *qobj; float minx, miny; int ignore_theta; class MyPeriodic : public VkPeriodic{ public: MyPeriodic(int interval) : VkPeriodic(interval) // Initialize base class {;} virtual void tick(void){ Anim(); } }; MyPeriodic* mp = 0; //---- BulletinBoard Constructor BulletinBoard::BulletinBoard(const char *name, Widget parent) : BulletinBoardUI(name, parent) { // This constructor calls BulletinBoardUI(parent, name) // which calls BulletinBoardUI::create() to create // the widgets for this component. Any code added here // is called after the component's interface has been built //---- Start editable code block: BulletinBoard constructor //---- End editable code block: BulletinBoard constructor } // End Constructor BulletinBoard::BulletinBoard(const char *name) : BulletinBoardUI(name) { // This constructor calls BulletinBoardUI(name) // which does not create any widgets. Usually, this // constructor is not used //---- Start editable code block: BulletinBoard constructor 2 //---- End editable code block: BulletinBoard constructor 2 } // End Constructor BulletinBoard::~BulletinBoard() { // The base class destructors are responsible for // destroying all widgets and objects used in this component. // Only additional items created directly in this class // need to be freed here. //---- Start editable code block: BulletinBoard destructor //---- End editable code block: BulletinBoard destructor } // End Destructor const char * BulletinBoard::className() // classname { return ("BulletinBoard"); } // End className() void Anim(){ glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); //cout << "in animate" << endl; DrawHarderGame(); //DrawGame(); // draw apples ***************************************************** for(int i = 0; i< num_apples; i++){ glPushMatrix(); glTranslatef(applex[i], appley[i], 0); glColor3f(1,0,0); gluQuadricDrawStyle(qobj, GLU_FILL); gluQuadricNormals(qobj, GLU_SMOOTH); gluDisk(qobj, 0, 6.0, 20, 20); glPopMatrix(); } glColor3f(.5, .3, 0); qobj = gluNewQuadric(); glPushMatrix(); glTranslatef(path[paths].x[ij], path[paths].y[ij], 0); gluQuadricDrawStyle(qobj, GLU_FILL); gluQuadricNormals(qobj, GLU_SMOOTH); gluDisk(qobj, 0, 6.0, 20, 20); glPopMatrix(); glColor3f(1, .3, 0); qobj = gluNewQuadric(); glPushMatrix(); glTranslatef(path[paths].x[ij - 1], path[paths].y[ij - 1], 0); gluQuadricDrawStyle(qobj, GLU_FILL); gluQuadricNormals(qobj, GLU_SMOOTH); gluDisk(qobj, 0, 6.0, 20, 20); glPopMatrix(); glColor3f(1, 0, 0); qobj = gluNewQuadric(); glPushMatrix(); glTranslatef(path[paths].x[ij - 2], path[paths].y[ij - 2], 0); gluQuadricDrawStyle(qobj, GLU_FILL); gluQuadricNormals(qobj, GLU_SMOOTH); gluDisk(qobj, 0, 6.0, 20, 20); glPopMatrix(); glColor3f(1, 1, 0); qobj = gluNewQuadric(); glPushMatrix(); glTranslatef(path[paths].x[ij - 3], path[paths].y[ij - 3], 0); gluQuadricDrawStyle(qobj, GLU_FILL); gluQuadricNormals(qobj, GLU_SMOOTH); gluDisk(qobj, 0, 6.0, 20, 20); glPopMatrix(); if(ij == (path[paths].steps)){ ij = path[paths].steps; } else{ ij++; } char str[100]; sprintf(str, "scrsave /home/users/lthreadg/temp/movie/%02d_%03d.rgb 28 828 155 955", anim, id); system(str); fprintf(stderr, "saving %02d_%03d.rgb\n", anim, id++); GLSwapBuffers(); } //end anim int Collide(float px, float py){ int flag = 1; if(((px > firstx - 5) && (px < (firstx + 50) + 5))&& ((py > firsty - 5) && (py < (firsty + 55)))){ flag = 0; cout << "in the first one" << endl; } if(((px > secondx -5) && (px < (secondx + 105))) && ((py > secondy -5) && (py < (secondy + 55))) && flag != 0){ cout << "in the second one" << endl; flag = 0; } if(((px > thirdx -5) && (px < thirdx + 105)) && ((py > thirdy -5) && (py < (thirdy + 155))) && flag != 0){ cout << "in the third one" << endl; flag = 0; } if(((px > fourthx -5) && (px < fourthx + 180)) && ((py > fourthy-5) && (py < (fourthy + 105))) && flag != 0){ cout << "in the fourth one" << endl; flag = 0; } if((px < 0 || px > XWINDOWSIZE || py < 0 || py > YWINDOWSIZE)){ cout << "Inside the Boundary" << endl; flag = 0; } return flag; } int HarderCollide(float x, float y){ int flag = 1; for(int i = 0; i < num_obstacles; i++){ if(flag != 0 && (x > po[i].x[0] - 5) && (x < po[i].x[2]+5) && (y < po[i].y[0]+5) && (y > po[i].y[2]-5)){ cout << "in" << i << "obstacle" << endl; flag = 0; } // end of if statement }// end of for loop if((x < 5 || x > XWINDOWSIZE || y < 5 || y > YWINDOWSIZE)){ cout << "Outside the Boundary" << endl; flag = 0; } // check to make sure the worm doesn't go ontop of each other. int bottom; if(path_step -10 < 0) bottom = 0; else bottom = path_step - 10; for(i = bottom; i < path_step; i++){ if(flag !=0 && (x == path[paths].x[i] || y == path[paths].y[i])) flag = 0; } return flag; } int WormCollide(float x, float y){ return; } void DrawHarderGame(){ //K po[0].x[3] = 30.0; po[0].y[3] = 330.0; po[0].x[2] = 100.0; po[0].y[2] = 330.0; po[0].x[1] = 100.0; po[0].y[1] = 500.0; po[0].x[0] = 30.0; po[0].y[0] = 500.0; //new K bottom po[11].x[0] = 5.0; po[11].y[0] = 180.0; po[11].x[1] = 100.0; po[11].y[1] = 180.0; po[11].x[2] = 100.0; po[11].y[2] = 150.0; po[11].x[3] = 5.0; po[11].y[3] = 150.0; // left u po[1].x[0] = 150.0; po[1].y[0] = 200.0; po[1].x[1] = 180.0; po[1].y[1] = 200.0; po[1].x[2] = 180.0; po[1].y[2] = 60.0; po[1].x[3] = 150.0; po[1].y[3] = 60.0; //bottom u po[2].x[0] = 150.0; po[2].y[0] = 60.0; po[2].x[3] = 150.0; po[2].y[3] = 20.0; po[2].x[2] = 300.0; po[2].y[2] = 20.0; po[2].x[1] = 300.0; po[2].y[1] = 60.0; //right u po[3].x[3] = 260.0; po[3].y[3] = 60.0; po[3].x[0] = 260.0; po[3].y[0] = 200.0; po[3].x[1] = 300.0; po[3].y[1] = 200.0; po[3].x[2] = 300.0; po[3].y[2] = 60.0; //top t po[4].x[3] = 180.0; po[4].y[3] = 500.0; po[4].x[2] = 430.0; po[4].y[2] = 500.0; po[4].x[1] = 430.0; po[4].y[1] = 540.0; po[4].x[0] = 180.0; po[4].y[0] = 540.0; po[5].x[3] = 220.0; po[5].y[3] = 330.0; po[5].x[0] = 220.0; po[5].y[0] = 450.0; po[5].x[1] = 230.0; po[5].y[1] = 450.0; po[5].x[2] = 230.0; po[5].y[2] = 330.0; // bottom of stem po[12].x[0] = 220.0; po[12].y[0] = 300.0; po[12].x[1] = 230.0; po[12].y[1] = 300.0; po[12].x[2] = 230.0; po[12].y[2] = 100.0; po[12].x[3] = 220.0; po[12].y[3] = 100.0; //growth T po[7].x[0] = 160.0; po[7].y[0] = 350.0; po[7].x[1] = 220.0; po[7].y[1] = 350.0; po[7].x[2] = 220.0; po[7].y[2] = 330.0; po[7].x[3] = 160.0; po[7].y[3] = 330.0; po[6].x[0] = 350.0; po[6].y[0] = 400.0; po[6].x[1] = 525.0; po[6].y[1] = 400.0; po[6].x[2] = 525.0; po[6].y[2] = 360.0; po[6].x[3] = 350.0; po[6].y[3] = 360.0; po[8].x[0] = 375.0; po[8].y[0] = 255.0; po[8].x[3] = 375.0; po[8].y[3] = 210.0; po[8].x[2] = 580.0; po[8].y[2] = 210.0; po[8].x[1] = 580.0; po[8].y[1] = 255.0; po[9].x[0] = 350.0; po[9].y[0] = 275.0; po[9].x[1] = 375.0; po[9].y[1] = 275.0; po[9].x[2] = 375.0; po[9].y[2] = 90.0; po[9].x[3] = 350.0; po[9].y[3] = 90.0; //new t stem po[10].x[0] = 290.0; po[10].y[0] = 500.0; po[10].x[1] = 320.0; po[10].y[1] = 500.0; po[10].x[2] = 320.0; po[10].y[2] = 430.0; po[10].x[3] = 290.0; po[10].y[3] = 430.0; for(int i = 0; i < num_obstacles; i++){ glColor3f(0,0,1); glBegin(GL_POLYGON); glVertex2f(po[i].x[0], po[i].y[0]); glVertex2f(po[i].x[1], po[i].y[1]); glVertex2f(po[i].x[2], po[i].y[2]); glVertex2f(po[i].x[3], po[i].y[3]); glEnd(); } } void DrawGame(){ // *************************** Draw Obstacles **************************** // /* first[0] = rand() % (100); first[1] = rand() % (150); first[2] = rand() % (100) + 200; first[3] = rand() % (150) + 200; first[4] = rand() % (100) + 350; first[5] = rand() % (150); first[6] = rand() % (100) + 150; first[7] = rand() % (100) + 375;*/ firstx = 75; firsty = 75; secondx = 200; secondy = 300; thirdx = 350; thirdy = 150; fourthx = 250; fourthy = 375; glColor3f(0, 0, 1); glBegin(GL_POLYGON); glVertex2f(firstx, firsty); glVertex2f((firstx + 50), firsty); glVertex2f((firstx + 50), (firsty + 50)); glVertex2f(firstx, (firsty + 50)); glEnd(); glColor3f(0, 1, 0); glBegin(GL_POLYGON); glVertex2f(secondx, secondy); glVertex2f((secondx + 100), secondy); glVertex2f((secondx + 100), (secondy + 50)); glVertex2f(secondx, (secondy + 50)); glEnd(); glColor3f(0, 1, 1); // Cyan glBegin(GL_POLYGON); glVertex2f(thirdx, thirdy); glVertex2f((thirdx + 100), thirdy); glVertex2f((thirdx + 100), (thirdy + 150)); glVertex2f(thirdx, (thirdy + 150)); glEnd(); glColor3f(1, 1, 0); glBegin(GL_POLYGON); glVertex2f(fourthx, fourthy); glVertex2f((fourthx + 175), fourthy); glVertex2f((fourthx + 175), (fourthy + 100)); glVertex2f(fourthx, (fourthy + 100)); glEnd(); } // end of DrawGame function // ********************************* Draw apples ******************** void DrawApples(){ int check = 0; for (int i = 0; i < num_apples; i++){ do{ applex[i] = rand() % (550)+ 80; appley[i] = rand() % (550) + 80; cout << "do loop" << endl; cout<< "applex" << applex[i] << "appley" << appley[i] << endl; check = HarderCollide(applex[i], appley[i]); cout << "check: " << check << endl; }while(check == 0); qobj = gluNewQuadric(); glPushMatrix(); glTranslatef(applex[i], appley[i], 0); glColor3f(1,0,0); gluQuadricDrawStyle(qobj, GLU_FILL); gluQuadricNormals(qobj, GLU_SMOOTH); gluDisk(qobj, 0, 6.0, 20, 20); glPopMatrix(); }// end of apple for loop }// end of draw apples // ***************************** Draw the worm ******************* void DrawWorm(){ glColor3f(.5, .3, 0); qobj = gluNewQuadric(); glPushMatrix(); glTranslatef(wormx_init, wormy_init, 0); gluQuadricDrawStyle(qobj, GLU_FILL); gluQuadricNormals(qobj, GLU_SMOOTH); gluDisk(qobj, 0, 6.0, 20, 20); glPopMatrix(); glColor3f(1, .3, 0); qobj = gluNewQuadric(); glPushMatrix(); glTranslatef(wormx_init - 9, wormy_init, 0); gluQuadricDrawStyle(qobj, GLU_FILL); gluQuadricNormals(qobj, GLU_SMOOTH); gluDisk(qobj, 0, 6.0, 20, 20); glPopMatrix(); glColor3f(1, 0.0, 0); qobj = gluNewQuadric(); glPushMatrix(); glTranslatef(wormx_init - 18, wormy_init, 0); gluQuadricDrawStyle(qobj, GLU_FILL); gluQuadricNormals(qobj, GLU_SMOOTH); gluDisk(qobj, 0, 6.0, 20, 20); glPopMatrix(); glColor3f(1, 1.0, 0); qobj = gluNewQuadric(); glPushMatrix(); glTranslatef(wormx_init - 27, wormy_init, 0); gluQuadricDrawStyle(qobj, GLU_FILL); gluQuadricNormals(qobj, GLU_SMOOTH); gluDisk(qobj, 0, 6.0, 20, 20); glPopMatrix(); } // end of DrawWorm // ************************************ Path **************************** void Path(){ for(int l = 0; l < 1500; l++){ path[paths].x[l]=0; path[paths].y[l]=0; } if(paths == 0){ path[paths].x[0] = wormx_init - 27; path[paths].x[1] = wormx_init - 18; path[paths].x[2] = wormx_init - 9; for(int w = 0; w < num_worms; w++) path[paths].y[w] = wormy_init; } else{ path[paths].x[0] = path[paths-1].x[path[paths-1].steps -3]; path[paths].x[1] = path[paths-1].x[path[paths-1].steps -2]; path[paths].x[2] = path[paths-1].x[path[paths-1].steps -1]; path[paths].y[0] = path[paths-1].y[path[paths-1].steps -3]; path[paths].y[1] = path[paths-1].y[path[paths-1].steps -2]; path[paths].y[2] = path[paths-1].y[path[paths-1].steps -1]; } float dist; int check = 0; int ptx[10], pty[10]; float min; int times = 0; int min_number; float theta; ignore_theta = 0; path_step = num_worms - 1; do{ path_step++; min = 1000; for(int i = 0; i < num_for; i++){ do{ if(paths == 0 || wormy < appley[paths] || times == 0) theta = rand() % 180; if(wormy > appley[paths]){ theta = rand() % (180) + 180; } cout << "times" << times << endl; if(times == 5) theta = 90; if(times == 6) theta = 0; if(times == 7) theta = 180; /*else theta = rand() % 360;*/ cout << "theta: " << theta << endl; theta = theta*3.14/180.0; ptx[i] = 9*cos(theta); pty[i] = 9*sin(theta); cout << "ptx" << ptx[i] << "pty" << pty[i] << endl; Dist_2_Obst((wormx + ptx[i]), (wormy + pty[i])); if(( (obst_distx < 15 || obst_disty < 15) && min > 20)){ cout << "min < 5" << flush << endl; cout << "Are you in here" << endl; //i = 4; if(obst_disty < obst_distx){ times = 6; } else if(obst_distx < obst_disty){ times = 5; } } check = HarderCollide((ptx[i] + wormx), (pty[i] + wormy)); cout << "Check" << check<< endl; //times++; }while(check == 0); times = 0; dist = sqrt(pow(((wormx + ptx[i]) - applex[paths]),2) + pow(((wormy + pty[i]) - appley[paths]),2)); cout << "dist: " << dist << endl; if((dist < min)){ min = dist; min_number = i; cout << "min " << min << endl; } Dist_2_Obst((wormx + ptx[i]), (wormy + pty[i])); if(( (obst_distx < 15 || obst_disty < 15) && min > 20)){ cout << "min < 5" << flush << endl; cout << "Are you in here" << endl; //i = 5; if(obst_disty < obst_distx){ times = 6; } else if(obst_distx < obst_disty){ times = 5; } } check = 0; } // end for i loop wormx = wormx + ptx[min_number]; wormy = wormy + pty[min_number]; glPointSize(3); glColor3f(1, 1, 0); glBegin(GL_POINTS); glVertex2f(wormx, wormy); glEnd(); path[paths].x[path_step] = wormx; path[paths].y[path_step] = wormy; cout << "new wormx: " << wormx << " new wormy: " << wormy << endl; cout << "min: " << min << endl; }while(min > 10.0); path[paths].steps = path_step; //} end for j loop } // end of path int Dist_2_Obst(float pointx, float pointy){ float obst_dist, obst_distx, obst_disty; minx = 1000; miny = 1000; minobst = 1000; for(int i =0; i < num_obstacles; i++){ for(int j = 0; j< 4; j++){ /* obst_dist = sqrt(pow((pointx - po[i].x[j]),2) + pow((pointy - po[i].y[j]),2)); if(obst_dist < minobst){ minobst = obst_dist; minindo = i; minindpt = j; }*/ obst_distx = sqrt(pow(po[i].x[j] - pointx, 2)); if(obst_distx < minx){ minx = obst_distx; minindx = i; minind = j; } obst_disty = sqrt(pow(po[i].y[j] - pointy, 2)); if(obst_disty < miny){ miny = obst_disty; minindy = i; minind = j;} cout << "miny: " << miny << "minx: " << minx << flush << endl; }// end for j /* obst_distx = sqrt(pow(XWINDOWSIZE - pointx, 2)); if(obst_distx < minx){ minx = obst_distx; cout << "minx: " << minx << flush << endl; } obst_disty = sqrt(pow(YWINDOWSIZE - pointy, 2)); if(obst_disty < miny){ miny = obst_disty; cout << "miny: " << miny << flush << endl; } */ }// end for i /* obst_distx = sqrt(pow(po[minindo].x[minindpt] - pointx, 2)); minx = po[minindo].x[minindpt] - pointx; obst_disty = sqrt(pow(po[minindo].y[minindpt] - pointy, 2)); miny = po[minindo].y[minindpt] - pointy;*/ return; }// end Dist_2_Obst function void BulletinBoard::MyDraw ( Widget w, XtPointer callData ) { makeGLCurrent(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 800.0, 0.0, 800.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0,0,800,800); glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); DrawHarderGame(); //DrawGame(); DrawApples(); DrawWorm(); GLSwapBuffers(); } // End BulletinBoard::MyDraw() void BulletinBoard::MyInit ( Widget w, XtPointer callData ) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(0.0, 0.0, 0.0, 0.0); initGLWindow(w, callData); makeGLCurrent(); mp = new MyPeriodic (100); srand(time(NULL)); wormx = 35; wormx_init = 35; wormy = 20; wormy_init = 20; num_worms = 3; ij = num_worms; anim = 5; id = 0; paths = 0; num_for = 5; ignore_theta = 0; } // End BulletinBoard::MyInit() void BulletinBoard::MyInteract ( Widget w, XtPointer callData ) { if(getMouseButton(LEFTBUTTON)) { int x, y; getMouseLocation(&x, &y); cout << "x: " << x << "y: " << y << endl; } } // End BulletinBoard::MyInteract() void BulletinBoard::Reshape ( Widget w, XtPointer callData ) { /* glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 800.0, 0.0, 800.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity();*/ } // End BulletinBoard::Reshape() void BulletinBoard::doExit ( Widget w, XtPointer callData ) { exit(1); } // End BulletinBoard::doExit() void BulletinBoard::doLevel ( Widget w, XtPointer callData ) { } // End BulletinBoard::doLevel() void BulletinBoard::doReset ( Widget w, XtPointer callData ) { if(paths < num_apples){ paths++; ij = num_worms; mp->stop(); Path(); mp->start(); } } // End BulletinBoard::doReset() void BulletinBoard::setStart ( Widget w, XtPointer callData ) { if(EZ(_start) == 0){ mp->stop(); } if(EZ(_start) == 1){ Path(); mp->start(); } } // End BulletinBoard::setStart() /////////////////////////////////////////////////////////////////// // static creation function, for importing class into rapidapp // or dynamically loading, using VkComponent::loadComponent /////////////////////////////////////////////////////////////////// VkComponent *BulletinBoard::CreateBulletinBoard( const char *name, Widget parent ) { VkComponent *obj = new BulletinBoard ( name, parent ); return ( obj ); } // End CreateBulletinBoard /////////////////////////////////////////////////////////////////// // Function for accessing a description of the dynamic interface // to this class. /////////////////////////////////////////////////////////////////// // WARNING: This structure is different than that used with 1.1 RapidApp. // See the RapidApp release notes for details struct InterfaceMap { char *resourceName; char *methodName; char *argType; char *definingClass; // Optional, if not this class void (VkCallbackObject::*method)(...); // Reserved, do not set }; void *BulletinBoard::RegisterBulletinBoardInterface() { // This structure registers information about this class // that allows RapidApp to create and manipulate an instance. // Each entry provides a resource name that will appear in the // resource manager palette when an instance of this class is // selected, the name of the member function as a string, // the type of the single argument to this function, and an. // optional argument indicating the class that defines this function. // All member functions must have the form // // void memberFunction ( Type ); // // where "Type" is one of: // const char * (Use XmRString) // Boolean (Use XmRBoolean) // int (Use XmRInt) // float (Use XmRFloat) // No argument (Use VkRNoArg or "NoArg" // A filename (Use VkRFilename or "Filename") // An enumeration (Use "Enumeration:ClassName:Type: VALUE1, VALUE2, VALUE3") // A callback (Use XmRCallback) static InterfaceMap map[] = { //---- Start editable code block: BulletinBoardUI resource table // { "resourceName", "setAttribute", XmRString}, //---- End editable code block: BulletinBoardUI resource table { NULL }, // MUST be NULL terminated }; return map; } // End RegisterBulletinBoardInterface() //---- End of generated code //---- Start editable code block: End of generated code //---- End editable code block: End of generated code