Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 50.88 KB | None | 0 0
  1. #include <SDL.h>
  2. #include <SDL_net.h>
  3. #include <SDL_opengl.h>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <gl\glaux.h>
  7. #include <SDL_ttf.h> //arial.ttf
  8. #include <cmath>
  9. #include <sstream>
  10. #include <al.h>
  11. #include <alc.h>
  12. #include <alut.h>
  13.  
  14. using namespace std;
  15.  
  16. SDLNet_SocketSet set = NULL;
  17. SDL_Surface* screen = NULL;
  18. TCPsocket gameconnection;
  19. char buffer[512];
  20. GLuint  texture[100];
  21. float scale = 0.5;
  22. SDL_Event event;
  23. TTF_Font *font = NULL;
  24. SDL_Color textcolor = {170,170,170};
  25.  
  26. const char* windowcaption = "Syerjchep";
  27. const int screenwidth = 500;
  28. const int screenheight = 500;
  29. string username = "DrenDran";
  30. string password = "password";
  31.  
  32. bool skup,skdown,skleft,skright,skpu,skpd;
  33. GLfloat xrot;            // x rotation
  34. GLfloat yrot;            // y rotation
  35. GLfloat xspeed;          // x rotation speed
  36. GLfloat yspeed;          // y rotation speed
  37. GLfloat walkbias = 0;
  38. GLfloat walkbiasangle = 0;
  39. GLfloat lookupdown = 0.0;
  40. const float piover180 = 0.0174532925f;
  41. float heading, xpos=0, ypos=10, zpos=0;
  42. float xvel = 0,yvel = 0,zvel = 0;
  43. GLfloat camx = 0, camy = 0, camz = 0; // camera location.
  44. GLfloat therotate;
  45. GLfloat z=0.0f;                       // depth into the screen.
  46. bool loggingin = true;
  47. bool takinginput = false;
  48. string tell = "";
  49.  
  50. #include <SEDTL.h>
  51.  
  52. void outpak(chalen in,string fname)
  53. {
  54.     fstream tt(("logs/"+fname+".bin").c_str(),ios::out|ios::binary);
  55.     char ttemp[in.length];
  56.     for(int i = 0; i<in.length; i++)
  57.         ttemp[i] = in.data[i];
  58.     tt.write(ttemp,in.length);
  59.     tt.close();
  60. }
  61.  
  62.  
  63.  
  64. chalen compileauthpacket()
  65. {
  66.     cerr<<"Logging in with username: "<<username<<"\n";
  67.     char *cusername;
  68.     char *cpassword;
  69.     cusername = new char[username.length()];
  70.     cpassword = new char[password.length()];
  71.  
  72.     strcpy(cusername,username.c_str());
  73.     strcpy(cpassword,password.c_str());
  74.  
  75.     char authstageone[username.length()+password.length()+4];
  76.     char authstagetwo[username.length()+password.length()+5];
  77.  
  78.     authstageone[0] = 0xA1;
  79.  
  80.     for(int i = 0; i<(int)username.length(); i++)
  81.         authstageone[i+1] = cusername[i];
  82.  
  83.     authstageone[username.length()+1] = 0xFE;
  84.  
  85.     for(int i = 0; i<(int)password.length(); i++)
  86.         authstageone[i+username.length()+2] = cpassword[i];
  87.  
  88.     authstageone[username.length()+password.length()+2] = 0xFE;
  89.     authstageone[username.length()+password.length()+3] = 0xFF;
  90.  
  91.     authstagetwo[0] = (username.length()+password.length()+4);
  92.  
  93.     for(int i = 0; i<(int)(username.length()+password.length()+4); i++)
  94.         authstagetwo[i+1] = authstageone[i];
  95.  
  96.     chalen ret;
  97.     ret.allocspace(username.length()+password.length()+5);
  98.     for(int i = 0; i<ret.length; i++)
  99.         ret.data[i] = authstagetwo[i];
  100.  
  101.  
  102.     cerr<<"Authentication packet formed.\n";
  103.  
  104.     return ret;
  105. }
  106.  
  107.  
  108. int authenticate()
  109. {
  110.     cerr<<"Logging in...\n";
  111.     chalen hi = compileauthpacket();
  112.  
  113.     if(SDLNet_TCP_Send(gameconnection,hi.data,hi.length) != hi.length)
  114.     {
  115.         cerr<<"Error sending authentication, probably because you had to disconnect.\n";
  116.         return 7;
  117.     }
  118.  
  119.     bool loggedin = false;
  120.  
  121.     for(int i = 0; i<10; i++)
  122.     {
  123.         int buflen = 0;
  124.         if ((buflen = SDLNet_TCP_Recv(gameconnection, buffer, 512)) > 0)
  125.         {
  126.             fstream outl("packetinlog.bin",ios::binary|ios::app|ios::out);
  127.             outl.write(buffer,buflen);
  128.             outl.write("     ",5);
  129.             outl.close();
  130.  
  131.  
  132.             unsigned char test;
  133.             test = buffer[0];
  134.             if(test == username.length()+1)
  135.             {
  136.                 char namecheck[username.length()];
  137.                 for(int a = 0; a<(int)username.length(); a++)
  138.                     namecheck[a] = buffer[a+1];
  139.                 if(string(namecheck).substr(0,username.length()) == username)
  140.                 {
  141.                     if(buffer[test] == 1)
  142.                     {
  143.                         loggedin = true;
  144.                         cerr<<"Logged in to game server!\n";
  145.                         break;
  146.                     }
  147.                     else
  148.                     {
  149.                         cerr<<"Invalid password, banned account, or bad connection.\n";
  150.                         return 10;
  151.                     }
  152.                 }
  153.                 else
  154.                 {
  155.                     cerr<<"Server client connection messed up username data.\n";
  156.                     return 9;
  157.                 }
  158.  
  159.             }
  160.             else
  161.             {
  162.                 chalen brf;
  163.                 brf.allocspace(buflen);
  164.                 for(int i = 0; i<buflen; i++)
  165.                     brf.data[i] = buffer[i];
  166.                 outpak(brf,"badloginresponse");
  167.                 cerr<<"Could not log in. Invalid packet size denomitator.\n";
  168.                 return 8;
  169.             }
  170.         }
  171.  
  172.         SDL_Delay(500);
  173.     }
  174.  
  175.     if(!loggedin)
  176.     {
  177.         cerr<<"Log in confrimation timed out.\n";
  178.         return 11;
  179.     }
  180.  
  181.     return 4525;
  182. }
  183.  
  184. int starttime = 0;
  185.  
  186. void starttimer()
  187. {
  188.     starttime = SDL_GetTicks();
  189. }
  190.  
  191. void printtime()
  192. {
  193.     int endtime = SDL_GetTicks();
  194.     int timetake = endtime-starttime;
  195.     cerr<<"Timer took "<<timetake<<"\n";
  196.     starttimer();
  197. }
  198.  
  199. int drawcube(float x,float y,float z,int tex,float sc,int id,bool ren)
  200. {
  201.  
  202. if(ren || !(xpos - 5 > x || xpos + 5 < x || ypos - 5 > y || ypos + 5 < y || zpos - 5 > z || zpos + 5 < z))
  203. {
  204.  
  205.  
  206. if(id>0)
  207.     glLoadName(id);
  208.  
  209.  
  210. float oldscale = scale;
  211.  
  212. if(sc != -1)
  213.     scale = sc;
  214.  
  215.  
  216. if(ren)
  217.     glBindTexture(GL_TEXTURE_2D, texture[tex]);                 // Select Our Texture
  218.     glBegin(GL_QUADS);
  219.  
  220.         // Front Face
  221.         glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y,  z); // Bottom Left Of The Texture and Quad
  222.         glTexCoord2f(1.0f, 0.0f); glVertex3f(x+scale, y,  z);   // Bottom Right Of The Texture and Quad
  223.         glTexCoord2f(1.0f, 1.0f); glVertex3f(x+scale,  y+scale,  z);    // Top Right Of The Texture and Quad
  224.         glTexCoord2f(0.0f, 1.0f); glVertex3f(x,  y+scale,  z);  // Top Left Of The Texture and Quad
  225.         // Back Face
  226.         glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z-scale);    // Bottom Right Of The Texture and Quad
  227.         glTexCoord2f(1.0f, 1.0f); glVertex3f(x,  y+scale, z-scale); // Top Right Of The Texture and Quad
  228.         glTexCoord2f(0.0f, 1.0f); glVertex3f(x+scale,  y+scale, z-scale);   // Top Left Of The Texture and Quad
  229.         glTexCoord2f(0.0f, 0.0f); glVertex3f(x+scale, y, z-scale);  // Bottom Left Of The Texture and Quad
  230.         // Top Face
  231.         glTexCoord2f(0.0f, 1.0f); glVertex3f(x,  y+scale, z-scale); // Top Left Of The Texture and Quad
  232.         glTexCoord2f(0.0f, 0.0f); glVertex3f(x,  y+scale,  z);  // Bottom Left Of The Texture and Quad
  233.         glTexCoord2f(1.0f, 0.0f); glVertex3f(x+scale,  y+scale,  z);    // Bottom Right Of The Texture and Quad
  234.         glTexCoord2f(1.0f, 1.0f); glVertex3f(x+scale,  y+scale, z-scale);   // Top Right Of The Texture and Quad
  235.         // Bottom Face
  236.         glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y, z-scale);    // Top Right Of The Texture and Quad
  237.         glTexCoord2f(0.0f, 1.0f); glVertex3f(x+scale, y, z-scale);  // Top Left Of The Texture and Quad
  238.         glTexCoord2f(0.0f, 0.0f); glVertex3f(x+scale, y,  z);   // Bottom Left Of The Texture and Quad
  239.         glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z);  // Bottom Right Of The Texture and Quad
  240.         // Right face
  241.         glTexCoord2f(1.0f, 0.0f); glVertex3f(x+scale, y, z-scale);  // Bottom Right Of The Texture and Quad
  242.         glTexCoord2f(1.0f, 1.0f); glVertex3f(x+scale,  y+scale, z-scale);   // Top Right Of The Texture and Quad
  243.         glTexCoord2f(0.0f, 1.0f); glVertex3f(x+scale,  y+scale,  z);    // Top Left Of The Texture and Quad
  244.         glTexCoord2f(0.0f, 0.0f); glVertex3f(x+scale, y, z);    // Bottom Left Of The Texture and Quad
  245.         // Left Face
  246.         glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z-scale);    // Bottom Left Of The Texture and Quad
  247.         glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y,  z); // Bottom Right Of The Texture and Quad
  248.         glTexCoord2f(1.0f, 1.0f); glVertex3f(x,  y+scale,  z);  // Top Right Of The Texture and Quad
  249.         glTexCoord2f(0.0f, 1.0f); glVertex3f(x,  y+scale, z-scale); // Top Left Of The Texture and Quad
  250.  
  251.     glEnd();
  252.  
  253. scale = oldscale;
  254.  
  255. }
  256.  
  257. return true;
  258. }
  259.  
  260. // Load Bitmaps And Convert To Textures
  261. bool createtexture(int position,char* filename)
  262. {
  263.     // Load Texture
  264.     SDL_Surface *image1;
  265.  
  266.     Uint8 *rowhi, *rowlo;
  267.     Uint8 *tmpbuf, tmpch;
  268.     SDL_Surface *image;
  269.     int i, j;
  270.  
  271.     image = SDL_LoadBMP(filename);
  272.     if ( image == NULL ) {
  273.         cerr<<"Unable to load file "<<filename<<" SDL_ERROR: "<<SDL_GetError()<<"\n";
  274.         return false;
  275.     }
  276.  
  277.     /* GL surfaces are upsidedown and RGB, not BGR :-) */
  278.     tmpbuf = (Uint8 *)malloc(image->pitch);
  279.     if ( tmpbuf == NULL ) {
  280.         fprintf(stderr, "Out of memory\n");
  281.         return false;
  282.     }
  283.     rowhi = (Uint8 *)image->pixels;
  284.     rowlo = rowhi + (image->h * image->pitch) - image->pitch;
  285.     for ( i=0; i<image->h/2; ++i ) {
  286.         for ( j=0; j<image->w; ++j ) {
  287.             tmpch = rowhi[j*3];
  288.             rowhi[j*3] = rowhi[j*3+2];
  289.             rowhi[j*3+2] = tmpch;
  290.             tmpch = rowlo[j*3];
  291.             rowlo[j*3] = rowlo[j*3+2];
  292.             rowlo[j*3+2] = tmpch;
  293.         }
  294.         memcpy(tmpbuf, rowhi, image->pitch);
  295.         memcpy(rowhi, rowlo, image->pitch);
  296.         memcpy(rowlo, tmpbuf, image->pitch);
  297.         rowhi += image->pitch;
  298.         rowlo -= image->pitch;
  299.     }
  300.     free(tmpbuf);
  301.     image1 = image;
  302.  
  303.     // Create Texture
  304.     glGenTextures(1, &texture[position]);
  305.     glBindTexture(GL_TEXTURE_2D, texture[position]);   // 2d texture (x and y size)
  306.  
  307.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
  308.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture
  309.  
  310.     // 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image,
  311.     // border 0 (normal), rgb color data, unsigned byte data, and finally the data itself.
  312.     glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->w, image1->h, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->pixels);
  313.  
  314.     return true;
  315. }
  316.  
  317. bool loadalltextures()
  318. {
  319.     bool status = true;
  320.     status = createtexture(0,"media/exe.bmp")&&status;
  321.     status = createtexture(1,"media/exe2.bmp")&&status;
  322.     status = createtexture(2,"media/grass.bmp")&&status;
  323.     status = createtexture(3,"media/soundbrick.bmp")&&status;
  324.     status = createtexture(4,"media/hammer.bmp")&&status;
  325.     status = createtexture(5,"media/noitem.bmp")&&status;
  326.     status = createtexture(6,"media/corn.bmp")&&status;
  327.  
  328.     return status;
  329. }
  330.  
  331. class cube
  332. {
  333.     public:
  334.     float x,y,z,sc;
  335.     int text,id;
  336.     bool used;
  337.     void show(bool ren);
  338. };
  339. void cube::show(bool ren)
  340. {
  341.     drawcube(x,y,z,text,sc,id,ren);
  342. }
  343.  
  344. cube cubes[10000];
  345. int numberofcubes = 0;
  346.  
  347. void addacube(float x,float y,float z,int text,int id,float sc,float l,float h,float w)
  348. {
  349.     cube adder;
  350.     adder.used = true;
  351.     adder.x = x;
  352.     adder.y = y;
  353.     adder.z = z;
  354.     adder.text = text;
  355.     adder.id = id;
  356.     adder.sc = sc;
  357.  
  358.     cubes[numberofcubes] = adder;
  359.     numberofcubes++;
  360. }
  361.  
  362. void cubemovebyid(int id,float x,float y,float z)
  363. {
  364.     for(int i = 0; i<numberofcubes; i++)
  365.     {
  366.         if(cubes[i].id == id)
  367.         {
  368.             cubes[i].x = x;
  369.             cubes[i].y = y;
  370.             cubes[i].z = z;
  371.         }
  372.     }
  373. }
  374.  
  375. bool idifcubeexists(int id)
  376. {
  377.     for(int i = 0; i<numberofcubes; i++)
  378.     {
  379.         if(cubes[i].id == id)
  380.             return true;
  381.     }
  382.     return false;
  383. }
  384.  
  385. void moveorcreate(int id,float x,float y,float z)
  386. {
  387.     if(idifcubeexists(id))
  388.     {
  389.         cubemovebyid(id,x,y,z);
  390.     }
  391.     else
  392.     {
  393.         addacube(x,y,z,0,id,-1,1,1,1);
  394.     }
  395. }
  396.  
  397. void showcubes(bool ren)
  398. {
  399.     glInitNames();
  400.     glPushName(0);
  401.     for(int i = 0; i<numberofcubes; i++)
  402.         cubes[i].show(ren);
  403. }
  404.  
  405. int updateposition()
  406. {
  407.     char pospak[18];
  408.     pospak[0] = 17;
  409.     pospak[1] = 0xA2;
  410.     memcpy(pospak+2,&xpos,4);
  411.     pospak[6] = 0xFE;
  412.     memcpy(pospak+7,&ypos,4);
  413.     pospak[11] = 0xFE;
  414.     memcpy(pospak+12,&zpos,4);
  415.     pospak[16] = 0xFE;
  416.     pospak[17] = 0xFF;
  417.  
  418.  
  419.     if(SDLNet_TCP_Send(gameconnection,pospak,18) != 18)
  420.     {
  421.         cerr<<"Error sending authauthentication, probably because you had to disconnect.\n";
  422.         return 7;
  423.     }
  424.  
  425.     return 4525;
  426. }
  427.  
  428. int round(double x)
  429. {
  430.     return (int)(x + 0.5);
  431. }
  432.  
  433. int nextpoweroftwo(int x)
  434. {
  435.     double logbase2 = log(x) / log(2);
  436.     return round(pow(2,ceil(logbase2)));
  437. }
  438.  
  439. int erc = 0;
  440.  
  441. void eb()
  442. {
  443.     erc++;
  444.     cerr<<erc<<"\n";
  445. }
  446.  
  447. int notdt = 0;
  448. SDL_Surface *tex2d[100];
  449.  
  450. void loadatexture( string fnam )
  451. {
  452.     tex2d[notdt] = SDL_LoadBMP(fnam.c_str());
  453.     notdt++;
  454. }
  455.  
  456. void SDL_GL_RenderPicture(int x,int y,int tex)
  457. {
  458.     int h = 32;
  459.     int w = 32;
  460.  
  461.     /* prepare to render our texture */
  462.     glEnable(GL_TEXTURE_2D);
  463.     glBindTexture(GL_TEXTURE_2D, texture[tex]);
  464.     glColor3f(1.0f, 1.0f, 1.0f);
  465.  
  466.     /* Draw a quad at location */
  467.     glBegin(GL_QUADS);
  468.         /* Recall that the origin is in the lower-left corner
  469.            That is why the TexCoords specify different corners
  470.            than the Vertex coors seem to. */
  471.         glTexCoord2f(0.0f, 1.0f);
  472.             glVertex2f(x, y);
  473.         glTexCoord2f(1.0f, 1.0f);
  474.             glVertex2f(x + w, y);
  475.         glTexCoord2f(1.0f, 0.0f);
  476.             glVertex2f(x + w, y + h);
  477.         glTexCoord2f(0.0f, 0.0f);
  478.             glVertex2f(x, y + h);
  479.     glEnd();
  480. }
  481.  
  482. void SDL_GL_RenderText(char *text,
  483.                       TTF_Font *font,
  484.                       SDL_Color color,
  485.                       SDL_Rect *location)
  486. {
  487.     SDL_Surface *initial;
  488.     SDL_Surface *intermediary;
  489.     int w,h;
  490.     GLuint textura;
  491.  
  492.     /* Use SDL_TTF to render our text */
  493.     initial = TTF_RenderText_Blended(font, text, color);
  494.  
  495.  
  496.  
  497.     /* Convert the rendered text to a known format */
  498.     w = nextpoweroftwo(initial->w);
  499.     h = nextpoweroftwo(initial->h);
  500.  
  501.     intermediary = SDL_CreateRGBSurface(0, w, h, 32,
  502.             0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  503.  
  504.     SDL_BlitSurface(initial, 0, intermediary, 0);
  505.  
  506.     /* Tell GL about our new texture */
  507.     glGenTextures(1, &textura);
  508.     glBindTexture(GL_TEXTURE_2D, textura);
  509.     glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_BGRA,
  510.             GL_UNSIGNED_BYTE, intermediary->pixels );
  511.  
  512.     /* GL_NEAREST looks horrible, if scaled... */
  513.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  514.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  515.  
  516.     /* prepare to render our texture */
  517.     glEnable(GL_TEXTURE_2D);
  518.     glBindTexture(GL_TEXTURE_2D, textura);
  519.     glColor3f(1.0f, 1.0f, 1.0f);
  520.  
  521.     /* Draw a quad at location */
  522.     glBegin(GL_QUADS);
  523.         /* Recall that the origin is in the lower-left corner
  524.            That is why the TexCoords specify different corners
  525.            than the Vertex coors seem to. */
  526.         glTexCoord2f(0.0f, 1.0f);
  527.             glVertex2f(location->x    , location->y);
  528.         glTexCoord2f(1.0f, 1.0f);
  529.             glVertex2f(location->x + w, location->y);
  530.         glTexCoord2f(1.0f, 0.0f);
  531.             glVertex2f(location->x + w, location->y + h);
  532.         glTexCoord2f(0.0f, 0.0f);
  533.             glVertex2f(location->x    , location->y + h);
  534.     glEnd();
  535.  
  536.     /* Bad things happen if we delete the texture before it finishes */
  537.     glFinish();
  538.  
  539.     /* return the deltas in the unused w,h part of the rect */
  540.     location->w = initial->w;
  541.     location->h = initial->h;
  542.  
  543.     /* Clean up */
  544.     SDL_FreeSurface(initial);
  545.     SDL_FreeSurface(intermediary);
  546.     glDeleteTextures(1, &textura);
  547. }
  548.  
  549. void glEnable2D()
  550. {
  551.     int vPort[4];
  552.  
  553.     glGetIntegerv(GL_VIEWPORT, vPort);
  554.  
  555.     glMatrixMode(GL_PROJECTION);
  556.     glPushMatrix();
  557.     glLoadIdentity();
  558.  
  559.     glOrtho(0, vPort[2], 0, vPort[3], -1, 1);
  560.     glMatrixMode(GL_MODELVIEW);
  561.     glPushMatrix();
  562.     glLoadIdentity();
  563. }
  564.  
  565. void glDisable2D()
  566. {
  567.     glMatrixMode(GL_PROJECTION);
  568.     glPopMatrix();
  569.     glMatrixMode(GL_MODELVIEW);
  570.     glPopMatrix();
  571. }
  572.  
  573. const int numlines = 9;
  574. string chats[numlines];
  575.  
  576. void newchat(string cat)
  577. {
  578.     for(int i = 1; i<numlines-2; i++)
  579.     {
  580.         chats[i-1] = chats[i];
  581.     }
  582.  
  583.     chats[numlines-3] = cat;
  584. }
  585.  
  586. void printchats()
  587. {
  588.  
  589.  
  590.     for(int i = 0; i<numlines; i++)
  591.     {
  592.         SDL_Rect bob;
  593.         bob.x = 10;
  594.         bob.y = 10+(i*15);
  595.         bob.h = 100;
  596.         bob.w = 100;
  597.         if(chats[i].length() > 1)
  598.             SDL_GL_RenderText((char*)(chats[i].c_str()), font, textcolor, &bob);
  599.     }
  600. }
  601.  
  602. void sendchat(string in)
  603. {
  604.     char out[4+in.length()];
  605.     out[0] = 3+in.length();
  606.     out[1] = 0xA3;
  607.     out[2+in.length()] = 0xFE;
  608.     out[3+in.length()] = 0xFF;
  609.  
  610.     cerr<<"Sending chat message.\n";
  611.  
  612.     int q = 0;
  613.     while(q < (int)in.length())
  614.     {
  615.         int ra = q;
  616.         char tmp[1];
  617.         ra = q;
  618.         string quick = in.substr(ra,1);
  619.         ra = q;
  620.         strcpy(tmp,quick.c_str());
  621.         ra = q;
  622.         out[ra+2] = tmp[0];
  623.         ra = q;
  624.         q++;
  625.     }
  626.  
  627.     out[1] = 0xA3;
  628.  
  629.     SDLNet_TCP_Send(gameconnection,out,4+in.length());
  630.  
  631.     cerr<<"Chat message test.\n";
  632.  
  633. }
  634.  
  635. ALuint Source,Buffer;
  636.  
  637. // Position of the source sound.
  638. ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };
  639.  
  640. // Velocity of the source sound.
  641. ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };
  642.  
  643.  
  644. // Position of the listener.
  645. ALfloat ListenerPos[] = { 0,0,0 };
  646.  
  647. // Velocity of the listener.
  648. ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };
  649.  
  650. // Orientation of the listener. (first 3 elements are "at", second 3 are "up")
  651. ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };
  652.  
  653. void loadaudiofile(string fnam)
  654. {
  655. ALenum format;
  656. ALsizei size;
  657. ALvoid* data;
  658. ALsizei freq;
  659. ALboolean loop;
  660. alGenBuffers(1, &Buffer);
  661. alutLoadWAVFile((ALbyte*)fnam.c_str(), &format, &data, &size, &freq, &loop);
  662. alBufferData(Buffer, format, data, size, freq);
  663. alutUnloadWAV(format, data, size, freq);
  664.  
  665. alGenSources(1, &Source);
  666. alSourcei (Source, AL_BUFFER,   Buffer   );
  667. alSourcef (Source, AL_PITCH,    1.0f     );
  668. alSourcef (Source, AL_GAIN,     1.0f     );
  669. alSourcefv(Source, AL_POSITION, SourcePos);
  670. alSourcefv(Source, AL_VELOCITY, SourceVel);
  671. alSourcei (Source, AL_LOOPING,  loop  );
  672.  
  673.  
  674. }
  675.  
  676. void loadaudio()
  677. {
  678.     loadaudiofile("media/ringout.wav");
  679.  
  680.     alListenerfv(AL_POSITION,    ListenerPos);
  681.     alListenerfv(AL_VELOCITY,    ListenerVel);
  682.     alListenerfv(AL_ORIENTATION, ListenerOri);
  683. }
  684.  
  685. globjfaces models[100];
  686. int modelnum = 0;
  687. void loadmodel(string fnam)
  688. {
  689.     globjfaces tmp;
  690.     tmp.readin((char*)fnam.c_str());
  691.     models[modelnum] = tmp;
  692.     modelnum++;
  693. }
  694. void showmodel(float x,float y,float z,int pos)
  695. {
  696.     models[pos].show(x,y,z,false,0,0,0);
  697. }
  698.  
  699. class item_basic
  700. {
  701.     public:
  702.     int idbitmap;
  703.     int model;
  704.     string name;
  705. };
  706.  
  707. class item_dropped
  708. {
  709.     public:
  710.     float x,y,z;
  711.     item_basic type;
  712.     int id;
  713.     bool used;
  714. };
  715.  
  716. class plant
  717. {
  718.     public:
  719.     int model,growth,id,maxgrowth;
  720.     float x,y,z,height;
  721.     bool used;
  722. };
  723.  
  724. plant plants[100];
  725.  
  726. void addplant(float x,float y,float z,int model,int growth,int id,int maxgrowth,float height)
  727. {
  728.     for(int i = 0; i<100; i++)
  729.     {
  730.         if(plants[i].used == false)
  731.         {
  732.             plants[i].used = true;
  733.             plants[i].x = x;
  734.             plants[i].y = y;
  735.             plants[i].z = z;
  736.             plants[i].model = model;
  737.             plants[i].growth = growth;
  738.             plants[i].id = id;
  739.             plants[i].maxgrowth = maxgrowth;
  740.             plants[i].height = height;
  741.             break;
  742.         }
  743.     }
  744. }
  745.  
  746. void pickmodel(float x,float y,float z,int pos,int r,int g,int b)
  747. {
  748.     models[pos].show(x,y,z,true,r,g,b);
  749. }
  750.  
  751. void showplants(bool picking)
  752. {
  753.     for(int i = 0; i<100; i++)
  754.     {
  755.         if(plants[i].used)
  756.         {
  757.             float plantmaxgrowthtime = plants[i].maxgrowth;
  758.             float currentgrowthtime = plants[i].growth;
  759.             float decimaldonegrowting = currentgrowthtime/plantmaxgrowthtime;
  760.             float plantmaxheight = plants[i].height;
  761.             float heightofplant = plantmaxheight*decimaldonegrowting;
  762.             if(!picking)
  763.                 showmodel(plants[i].x,(plants[i].y-0.75-plantmaxheight)+heightofplant,plants[i].z,plants[i].model);
  764.             else
  765.                 pickmodel(plants[i].x,(plants[i].y-0.75-plantmaxheight)+heightofplant,plants[i].z,plants[i].model,80,0,i);
  766.         }
  767.     }
  768. }
  769.  
  770. void removeplantbyid(int id)
  771. {
  772.     for(int i = 0; i<100; i++)
  773.     {
  774.         if(plants[i].id == id && plants[i].used)
  775.             plants[i].used = false;
  776.     }
  777. }
  778.  
  779. item_basic items[100];
  780. int noi = 0;
  781.  
  782. item_dropped things[100];
  783. void aaddnewitem(float x,float y,float z,int type,int id)
  784. {
  785.     for(int i = 0; i<100; i++)
  786.     {
  787.         if(!things[i].used)
  788.         {
  789.             things[i].type = items[type];
  790.             things[i].x = x;
  791.             things[i].y = y;
  792.             things[i].z = z;
  793.             things[i].used = true;
  794.             things[i].id = id;
  795.             break;
  796.         }
  797.     }
  798. }
  799.  
  800. void removeitem(int id)
  801. {
  802.     for(int i = 0; i<100; i++)
  803.     {
  804.         if(things[i].id == id)
  805.         things[i].used = false;
  806.     }
  807. }
  808.  
  809. void showgrounditems()
  810. {
  811.     for(int i = 0; i<100; i++)
  812.     {
  813.         if(things[i].used)
  814.             showmodel(things[i].x,things[i].y,things[i].z,things[i].type.model);
  815.     }
  816. }
  817.  
  818. void baddnewitem(string name,int text,int model)
  819. {
  820.         items[noi].name = name;
  821.         items[noi].idbitmap = text;
  822.         items[noi].model = model;
  823.         noi++;
  824. }
  825.  
  826. int itemnametoid(string name)
  827. {
  828.     for(int i = 0; i<noi; i++)
  829.         if(items[i].name == name)
  830.             return i;
  831.  
  832.     return -1;
  833. }
  834.  
  835. int inventory[16];
  836. int iosx = 200;
  837. int iosy = 0;
  838.  
  839. vec3 slottopos(int slot)
  840. {
  841.     vec3 tmp;
  842.     tmp.z = 0;
  843.  
  844.     tmp.x = 0;
  845.     tmp.y = 0;
  846.  
  847.     switch(slot)
  848.     {
  849.     case 0: tmp.x = 0; tmp.y = 0; break;
  850.     case 1: tmp.x = 40; tmp.y = 0; break;
  851.     case 2: tmp.x = 80; tmp.y = 0; break;
  852.     case 3: tmp.x = 120; tmp.y = 0; break;
  853.     case 4: tmp.x = 0; tmp.y = 40; break;
  854.     case 5: tmp.x = 40; tmp.y = 40; break;
  855.     case 6: tmp.x = 80; tmp.y = 40; break;
  856.     case 7: tmp.x = 120; tmp.y = 40; break;
  857.     case 8: tmp.x = 0; tmp.y = 80; break;
  858.     case 9: tmp.x = 40; tmp.y = 80; break;
  859.     case 10: tmp.x = 80; tmp.y = 80; break;
  860.     case 11: tmp.x = 120; tmp.y = 80; break;
  861.     case 12: tmp.x = 0; tmp.y = 120; break;
  862.     case 13: tmp.x = 40; tmp.y = 120; break;
  863.     case 14: tmp.x = 80; tmp.y = 120; break;
  864.     case 15: tmp.x = 120; tmp.y = 120; break;
  865.     }
  866.  
  867.     tmp.x += iosx;
  868.     tmp.y += iosy;
  869.  
  870.     return tmp;
  871. }
  872.  
  873. void showitems()
  874. {
  875.     for(int i = 0; i<16; i++)
  876.     {
  877.         vec3 tmp = slottopos(i);
  878.         int x = (int)tmp.x;
  879.         int y = (int)tmp.y;
  880.  
  881.         SDL_GL_RenderPicture(x,y,items[inventory[i]].idbitmap);
  882.     }
  883. }
  884.  
  885. bool pickupitem(unsigned char type)
  886. {
  887.     for(int i = 0; i<16; i++)
  888.     {
  889.         if(inventory[i] == itemnametoid("nil"))
  890.         {
  891.             inventory[i] = type;
  892.             return true;
  893.         }
  894.     }
  895.     return false;
  896. }
  897.  
  898. void inventorydrop(int slot)
  899. {
  900.     char outpak[5];
  901.     outpak[0] = 4;
  902.     outpak[1] = 0xA4;
  903.     outpak[2] = inventory[slot]-1;
  904.     outpak[3] = 0xFE;
  905.     outpak[4] = 0xFF;
  906.  
  907.     SDLNet_TCP_Send(gameconnection,outpak,5);
  908.  
  909.     inventory[slot] = itemnametoid("nil");
  910. }
  911.  
  912. void inventoryuse(int slot)
  913. {
  914.     char outpak[5];
  915.     outpak[0] = 4;
  916.     outpak[1] = 0xA6;
  917.     outpak[2] = inventory[slot]-1;
  918.     outpak[3] = 0xFE;
  919.     outpak[4] = 0xFF;
  920.  
  921.     SDLNet_TCP_Send(gameconnection,outpak,5);
  922.  
  923.     inventory[slot] = itemnametoid("nil");
  924. }
  925.  
  926. void pickplant(int a)
  927. {
  928.     if(plants[a].used && !(plants[a].growth < plants[a].maxgrowth))
  929.     {
  930.         char outpak[5];
  931.         outpak[0] = 4;
  932.         outpak[1] = 0xA5;
  933.         outpak[2] = plants[a].id;
  934.         outpak[3] = 0xFE;
  935.         outpak[4] = 0xFF;
  936.  
  937.         SDLNet_TCP_Send(gameconnection,outpak,5);
  938.     }
  939. }
  940.  
  941. int main ( int argc, char** argv )
  942. {
  943.  
  944.     baddnewitem("nil",5,0);
  945.     baddnewitem("ball",4,0);
  946.     baddnewitem("corn",6,2);
  947.  
  948.     for(int i = 0; i<16; i++)
  949.         inventory[i] = itemnametoid("nil");
  950.  
  951.     loadatexture("media/exe.bmp");
  952.  
  953.     loadmodel("media/hammar.obj");
  954.     loadmodel("media/test.obj");
  955.     loadmodel("media/corn.obj");
  956.  
  957.     chats[numlines-1] = "";
  958.     chats[numlines-2] = "";
  959.     for(float i = -16; i<16; i+=2)
  960.     {
  961.         for(float y = -16; y<16; y+=2)
  962.         {
  963.             addacube(i,-2.75,y,2,-1,2,1,1,1);
  964.         }
  965.     }
  966.  
  967.  
  968.     SourcePos[0] = 10;
  969.     SourcePos[1] = -.75;
  970.     SourcePos[2] = 10;
  971.     alSourcefv(Source, AL_POSITION, SourcePos);
  972.  
  973.     string inip;
  974.  
  975.     ifstream prop("settings.ini");
  976.     if(prop.is_open())
  977.     {
  978.         getline(prop,inip);
  979.         cerr<<inip<<"\n";
  980.         cerr<<"Properties loaded.\n";
  981.     }
  982.     else
  983.     {
  984.         cerr<<"Could not open properties file.\n";
  985.         return 88;
  986.     }
  987.  
  988.  
  989.     alutInit(&argc, argv);
  990.     cerr<<"Audio set up.\n";
  991.  
  992.     loadaudio();
  993.     cerr<<"Audio files loaded.\n";
  994.  
  995.  
  996.     if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
  997.     {
  998.         cerr<<"Could not start up SDL graphics and input system.\n";
  999.         return 1;
  1000.     }
  1001.     else
  1002.         cerr<<"Started up SDL.\n";
  1003.  
  1004.     if(SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ) == -1)
  1005.     {
  1006.         cerr<<"Could not bind graphics to OpenGL.\n";
  1007.         return 2;
  1008.     }
  1009.     else
  1010.         cerr<<"Graphics bound to OpenGL.\n";
  1011.  
  1012.     if(SDLNet_Init() == -1)
  1013.     {
  1014.         cerr<<"Could not start up SDL_Net.\n";
  1015.         return 3;
  1016.     }
  1017.     else
  1018.         cerr<<"Started up SDL_Net.\n";
  1019.  
  1020.     if(TTF_Init() == -1)
  1021.     {
  1022.         cerr<<"SDL_TTF didn't initalize correctly.\n";
  1023.         return 63;
  1024.     }
  1025.     font = NULL;
  1026.     font = TTF_OpenFont( "media/arial.ttf", 12 );
  1027.     if(font == NULL)
  1028.     {
  1029.         cerr<<"Couldn't load font.\n";
  1030.         return 62;
  1031.     }
  1032.  
  1033.     FreeConsole();
  1034.  
  1035.     screen = SDL_SetVideoMode(screenwidth, screenheight, 0, SDL_OPENGL);
  1036.     if(!screen || screen == NULL)
  1037.     {
  1038.         cerr<<"Failed to create screen.\n";
  1039.         return 4;
  1040.     }
  1041.     else
  1042.         cerr<<"Screen created.\n";
  1043.  
  1044.     SDL_WM_SetCaption(windowcaption, NULL );
  1045.         cerr<<"Caption set.\n";
  1046.  
  1047.     glViewport(0, 0, screenwidth, screenheight);
  1048.  
  1049.     if(!loadalltextures())
  1050.     {
  1051.         cerr<<"Had errors when trying to load textures.\n";
  1052.         return 12;
  1053.     }
  1054.     else
  1055.         cerr<<"Textures loaded.\n";
  1056.  
  1057.     glEnable(GL_TEXTURE_2D);                    // Enable texture mapping.
  1058.  
  1059.     glBlendFunc(GL_SRC_ALPHA, GL_ONE);          // Set the blending function for translucency (note off at init time)
  1060.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);   // This Will Clear The Background Color To Black
  1061.     glClearDepth(1.0);              // Enables Clearing Of The Depth Buffer
  1062.     glDepthFunc(GL_LESS);                       // type of depth test to do.
  1063.     glEnable(GL_DEPTH_TEST);                    // enables depth testing.
  1064.     glShadeModel(GL_SMOOTH);            // Enables Smooth Color Shading
  1065.  
  1066.     glMatrixMode(GL_PROJECTION);
  1067.     glLoadIdentity();               // Reset The Projection Matrix
  1068.  
  1069.     gluPerspective(45.0f,(GLfloat)screenwidth/(GLfloat)screenheight,0.1f,100.0f);   // Calculate The Aspect Ratio Of The Window
  1070.  
  1071.     glMatrixMode(GL_MODELVIEW);
  1072.  
  1073.     cerr<<"Screen properties set.\n";
  1074.  
  1075.     IPaddress ip;
  1076.  
  1077.     char *ipto = (char*)inip.c_str();
  1078.  
  1079.     if(SDLNet_ResolveHost(&ip, ipto, 4525) == -1)
  1080.     {
  1081.         cerr<<"Could not resolve host IP of server.\n";
  1082.         return 5;
  1083.     }
  1084.     else
  1085.         cerr<<"Host location found.\n";
  1086.  
  1087.     cerr<<"Using IP: "<<ipto<<"\n";
  1088.  
  1089.     if((gameconnection = SDLNet_TCP_Open(&ip)) == NULL)
  1090.     {
  1091.         cerr<<"Could not connect to server.\n";
  1092.         return 6;
  1093.     }
  1094.     else
  1095.         cerr<<"Connected to server.\n";
  1096.  
  1097.     int buflen = 0;
  1098.  
  1099.     SDL_EnableUNICODE( SDL_ENABLE );
  1100.     //SDL_ShowCursor(SDL_DISABLE);
  1101.  
  1102.     set = SDLNet_AllocSocketSet(2);
  1103.     SDLNet_TCP_AddSocket(set, gameconnection);
  1104.  
  1105.     cerr<<"Starting game loop.\n";
  1106.  
  1107.     bool done = false;
  1108.     bool firstround = true;
  1109.  
  1110.     int ticks = 0;
  1111.     int starttime = time(NULL);
  1112.     int roundcount = 0;
  1113.     int lastsecond = time(NULL);
  1114.  
  1115.     float spacebuf = 0.02;
  1116.     string usernametobe = "";
  1117.  
  1118.     int lastjumptime = 0;
  1119.     bool jumped = false;
  1120.     yvel = -0.05;
  1121.  
  1122.     int lft = 0;
  1123.  
  1124.     int ticksecond = SDL_GetTicks();
  1125.  
  1126.     while(!done)
  1127.     {
  1128.  
  1129.         if(ticksecond+1000 < (int)SDL_GetTicks())
  1130.         {
  1131.             ticksecond = SDL_GetTicks();
  1132.             for(int i = 0; i<100; i++)
  1133.             {
  1134.                 if(plants[i].used && plants[i].growth < plants[i].maxgrowth)
  1135.                     plants[i].growth++;
  1136.             }
  1137.         }
  1138.  
  1139.         if(lft < 20)
  1140.             SDL_Delay(20-lft);
  1141.  
  1142.         lft = SDL_GetTicks();
  1143.  
  1144.         ALfloat newListenerPos[] = {xpos,ypos,zpos};
  1145.         alListenerfv(AL_POSITION,    newListenerPos);
  1146.  
  1147.         if(jumped && lastjumptime+500 < (int)SDL_GetTicks())
  1148.         {
  1149.             jumped = false;
  1150.             yvel -= 0.075;
  1151.         }
  1152.  
  1153.         if(loggingin)
  1154.         {
  1155.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     // Clear The Screen And The Depth Buffer
  1156.             glLoadIdentity();               // Reset The View
  1157.  
  1158.             glEnable2D();
  1159.             SDL_Rect hi;
  1160.             hi.x = (screenwidth/2)-100;
  1161.             hi.y = screenheight/2;
  1162.             hi.h = 0;
  1163.             hi.w = 0;
  1164.  
  1165.             SDL_GL_RenderPicture(10,10,0);
  1166.  
  1167.             SDL_GL_RenderText("Type in the name you wish to use: ", font, textcolor, &hi);
  1168.  
  1169.             hi.y -= 20;
  1170.             if(usernametobe.length() > 0)
  1171.                 SDL_GL_RenderText((char*)usernametobe.c_str(),font,textcolor,&hi);
  1172.  
  1173.             glDisable2D();
  1174.  
  1175.             SDL_GL_SwapBuffers();
  1176.  
  1177.             while(SDL_PollEvent(&event))
  1178.             {
  1179.                 if(event.type == SDL_QUIT)
  1180.                 {
  1181.                     done = true;
  1182.                     break;
  1183.                 }
  1184.  
  1185.                 if(event.type == SDL_KEYDOWN)
  1186.                 {
  1187.                     if(usernametobe.length() < 20)
  1188.                     {
  1189.                         if( event.key.keysym.unicode == (Uint16)' ' )
  1190.                         {
  1191.                             usernametobe = usernametobe+" ";
  1192.                         }
  1193.                         else if( ( event.key.keysym.unicode >= (Uint16)'0' ) && ( event.key.keysym.unicode <= (Uint16)'9' ) )
  1194.                         {
  1195.                             usernametobe += (char)event.key.keysym.unicode;
  1196.                         }
  1197.                         else if( ( event.key.keysym.unicode >= (Uint16)'A' ) && ( event.key.keysym.unicode <= (Uint16)'Z' ) )
  1198.                         {
  1199.                             usernametobe += (char)event.key.keysym.unicode;
  1200.                         }
  1201.                         else if( ( event.key.keysym.unicode >= (Uint16)'a' ) && ( event.key.keysym.unicode <= (Uint16)'z' ) )
  1202.                         {
  1203.                             usernametobe += (char)event.key.keysym.unicode;
  1204.                         }
  1205.                     }
  1206.  
  1207.                         if(event.key.keysym.sym == SDLK_BACKSPACE)
  1208.                         {
  1209.                             if(usernametobe.length() > 0)
  1210.                             {
  1211.                                 usernametobe.erase(usernametobe.length()-1);
  1212.                             }
  1213.                         }
  1214.  
  1215.                         if(event.key.keysym.sym == SDLK_RETURN)
  1216.                         {
  1217.                             if(usernametobe.length() > 0)
  1218.                             {
  1219.                                 username = usernametobe;
  1220.                                 usernametobe = "";
  1221.  
  1222.                                 bool cah = true;
  1223.                                 while(cah)
  1224.                                 {
  1225.                                     if(authenticate() == 4525)
  1226.                                     {
  1227.                                         cah = false;
  1228.                                         cerr<<"Logged in as "<<username<<"\n";
  1229.                                     }
  1230.                                     else
  1231.                                         cerr<<"Failed to log in... trying again.\n";
  1232.                                 }
  1233.  
  1234.                                 loggingin = false;
  1235.                             }
  1236.                         }
  1237.                 }
  1238.             }
  1239.  
  1240.             SDL_Delay(10);
  1241.             }
  1242.         else
  1243.         {
  1244.         roundcount++;
  1245.         ticks++;
  1246.         if(roundcount > 9)
  1247.         {
  1248.             updateposition();
  1249.             roundcount = 0;
  1250.         }
  1251.  
  1252.         if(roundcount == 5)
  1253.         {
  1254.  
  1255.             SDLNet_CheckSockets(set, 10);
  1256.             if(SDLNet_SocketReady(gameconnection))
  1257.             {
  1258.                 chats[numlines-1] = tell;
  1259.  
  1260.                 buflen = SDLNet_TCP_Recv(gameconnection, buffer, 1);
  1261.                 if(buflen > 0)
  1262.                 {
  1263.                 unsigned int si = (unsigned int)buffer[0];
  1264.                 buflen = SDLNet_TCP_Recv(gameconnection,buffer,si);
  1265.  
  1266.                 fstream outl("packetinlog.bin",ios::binary|ios::app|ios::out);
  1267.                 outl.write(buffer,buflen);
  1268.                 outl.write("     ",5);
  1269.                 outl.close();
  1270.  
  1271.                     chalen newpak;
  1272.                     newpak.allocspace(buflen);
  1273.                     for(int i = 0; i<buflen; i++)
  1274.                         newpak.data[i] = buffer[i];
  1275.  
  1276.                     char tmp[newpak.length];
  1277.                     for(int i = 0; i<newpak.length; i++)
  1278.                         tmp[i] = newpak.data[i];
  1279.  
  1280.  
  1281.                     unsigned char ptype = (unsigned char)buffer[0];
  1282.  
  1283.                     newpak = cutdat(newpak,1);
  1284.  
  1285.                     if(ptype == 0xB1)
  1286.                     {
  1287.  
  1288.                         chalen movid = readarg(newpak);
  1289.                         chalen movx = readarg(newpak);
  1290.                         chalen movy = readarg(newpak);
  1291.                         chalen movz = readarg(newpak);
  1292.  
  1293.                         float txpos,typos,tzpos;
  1294.  
  1295.                         memcpy(&txpos, movx.data, 4);
  1296.                         memcpy(&typos, movy.data, 4);
  1297.                         memcpy(&tzpos, movz.data, 4);
  1298.  
  1299.                         moveorcreate((int)movid.data[0],txpos,typos,tzpos);
  1300.                     }
  1301.                     else if(ptype == 0xB2)
  1302.                     {
  1303.  
  1304.  
  1305.                         cerr<<"Got a chat message.\n";
  1306.  
  1307.                         chalen chat = readarg(newpak);
  1308.  
  1309.                         char type[chat.length];
  1310.                         for(int i = 0; i<chat.length; i++)
  1311.                             type[i] = chat.data[i];
  1312.  
  1313.                         cerr<<"Chat message is "<<chat.length<<" chars long and is: \n"<<string(type)<<"\n";
  1314.  
  1315.  
  1316.                         newchat(string(type).substr(0,chat.length));
  1317.  
  1318.                     }
  1319.                     else if(ptype == 0xB3)
  1320.                     {
  1321.                         cerr<<"Got a remove item request.\n";
  1322.                         chalen id = readarg(newpak);
  1323.                         cerr<<"REMOVE ITEM "<<(unsigned int)id.data[0]<<"\n";
  1324.                         removeitem((unsigned int)id.data[0]);
  1325.                     }
  1326.                     else if(ptype == 0xB4)
  1327.                     {
  1328.                         cerr<<"Got a add item request.\n";
  1329.  
  1330.                         chalen movx = readarg(newpak);
  1331.                         chalen movy = readarg(newpak);
  1332.                         chalen movz = readarg(newpak);
  1333.                         chalen type = readarg(newpak);
  1334.                         chalen id = readarg(newpak);
  1335.  
  1336.                         float txpos,typos,tzpos;
  1337.  
  1338.                         memcpy(&txpos, movx.data, 4);
  1339.                         memcpy(&typos, movy.data, 4);
  1340.                         memcpy(&tzpos, movz.data, 4);
  1341.  
  1342.                         aaddnewitem(txpos,typos,tzpos,(unsigned int)type.data[0]+1,(unsigned int)id.data[0]);
  1343.                         cerr<<"ADD ITEM "<<(unsigned int)id.data[0]<<" - "<<txpos<<":"<<typos<<":"<<tzpos<<" - "<<(unsigned int)type.data[0]<<"\n";
  1344.                     }
  1345.                     else if(ptype == 0xB5)
  1346.                     {
  1347.                         cerr<<"Picked up an item.\n";
  1348.                         chalen item = readarg(newpak);
  1349.                         pickupitem(item.data[0]+1);
  1350.                     }
  1351.                     else if(ptype == 0xB6)
  1352.                     {
  1353.                         cerr<<"Adding a plant.\n";
  1354.                         chalen movx = readarg(newpak);
  1355.                         chalen movy = readarg(newpak);
  1356.                         chalen movz = readarg(newpak);
  1357.                         chalen type = readarg(newpak);
  1358.                         chalen growth = readarg(newpak);
  1359.                         chalen id = readarg(newpak);
  1360.                         chalen amaxgrowth = readarg(newpak);
  1361.                         chalen aheight = readarg(newpak);
  1362.  
  1363.                         float txpos,typos,tzpos,height;
  1364.                         unsigned short maxgrowth;
  1365.  
  1366.                         memcpy(&txpos, movx.data, 4);
  1367.                         memcpy(&typos, movy.data, 4);
  1368.                         memcpy(&tzpos, movz.data, 4);
  1369.                         memcpy(&height, aheight.data, 4);
  1370.                         memcpy(&maxgrowth, amaxgrowth.data, 2);
  1371.  
  1372.                         cerr<<"PLANT: "<<txpos<<":"<<typos<<":"<<tzpos<<":"<<(int)type.data[0]<<":"<<(int)growth.data[0]<<":"<<(int)id.data[0]<<":"<<maxgrowth<<":"<<height<<"\n";
  1373.                         addplant(txpos,typos,tzpos,type.data[0],growth.data[0],id.data[0],maxgrowth,height);
  1374.  
  1375.                     //    float x,float y,float z,int model,int growth,int id,int maxgrowth,float height
  1376.                     }
  1377.                     else if(ptype == 0xB7)
  1378.                     {
  1379.                         cerr<<"Removing plant.\n";
  1380.                         chalen id = readarg(newpak);
  1381.                         removeplantbyid((unsigned char)id.data[0]);
  1382.                     }
  1383.                     else if(ptype == 0xB8)
  1384.                     {
  1385.                         chalen tx = readarg(newpak);
  1386.                         chalen ty = readarg(newpak);
  1387.                         chalen tz = readarg(newpak);
  1388.                         chalen ttext = readarg(newpak);
  1389.                         chalen tid = readarg(newpak);
  1390.                         chalen tsc = readarg(newpak);
  1391.                         chalen tl = readarg(newpak);
  1392.                         chalen th = readarg(newpak);
  1393.                         chalen tw = readarg(newpak);
  1394.  
  1395.                         float cx,cy,cz;
  1396.                         float cl,ch,cw;
  1397.  
  1398.                         memcpy(&cx, tx.data, 4);
  1399.                         memcpy(&cy, ty.data, 4);
  1400.                         memcpy(&cz, tz.data, 4);
  1401.  
  1402.                         memcpy(&cl, tl.data, 4);
  1403.                         memcpy(&ch, th.data, 4);
  1404.                         memcpy(&cw, tw.data, 4);
  1405.  
  1406.                         addacube(cx,cy,cz,ttext.data[0],tid.data[0],tsc.data[0],cl,ch,cw);
  1407.                     }
  1408.                     else
  1409.                     {
  1410.                         fstream oest("logs/unreconizendpacketidpacket.bin",ios::binary|ios::out);
  1411.                         oest.write(tmp,newpak.length);
  1412.                         oest.close();
  1413.                         cerr<<"Got a unreconized packet id.\n";
  1414.                         return 99;
  1415.                     }
  1416.  
  1417.  
  1418.                 }
  1419.             }
  1420.         }
  1421.  
  1422.         if(lastsecond+1 < time(NULL))
  1423.         {
  1424.             lastsecond = time(NULL);
  1425.         }
  1426.  
  1427.         while(SDL_PollEvent(&event))
  1428.         {
  1429.             if(event.type == SDL_QUIT)
  1430.             {
  1431.                 done = true;
  1432.                 break;
  1433.             }
  1434.  
  1435.             if(event.type == SDL_MOUSEBUTTONDOWN)
  1436.             {
  1437.  
  1438.                 int cursorX = event.button.x;
  1439.                 int cursorY = event.button.y;
  1440.  
  1441.                 int invselect = -1;
  1442.  
  1443.                 for(int i = 0; i<16; i++)
  1444.                 {
  1445.                     int tmpcursory = screenheight-cursorY;
  1446.                     vec3 test = slottopos(i);
  1447.                     //cerr<<"Inventory position "<<i<<" X:"<<(int)test.x<<" Y:"<<(int)test.y<<" Mouse cursor... X:"<<cursorX<<" Y:"<<tmpcursory<<"\n";
  1448.  
  1449.                     if(cursorX > test.x && cursorX < test.x+32)
  1450.                     {
  1451.                         if(tmpcursory > test.y && tmpcursory < test.y+32)
  1452.                         {
  1453.                       //      cerr<<"Match!\n";
  1454.                             invselect = i;
  1455.                             break;
  1456.                         }
  1457.                     }
  1458.                 }
  1459.  
  1460.                 if(invselect == -1)
  1461.                 {
  1462.  
  1463.                     GLuint selectBuf[512];
  1464.                     GLint viewport[4];
  1465.                     GLubyte pixel[3];
  1466.  
  1467.                     glSelectBuffer(512,selectBuf);
  1468.                     glRenderMode(GL_SELECT);
  1469.  
  1470.                     glGetIntegerv(GL_VIEWPORT,viewport);
  1471.  
  1472.                     glMatrixMode(GL_PROJECTION);
  1473.                     glPushMatrix();
  1474.                     glLoadIdentity();
  1475.  
  1476.                     gluPickMatrix(cursorX,500-cursorY,1,1,viewport);
  1477.                     gluPerspective(45.0f,(GLfloat)screenwidth/(GLfloat)screenheight,0.1f,100.0f);
  1478.  
  1479.                     showcubes(false);
  1480.  
  1481.                     glMatrixMode(GL_PROJECTION);
  1482.                     glPopMatrix();
  1483.  
  1484.                     GLuint hits = glRenderMode(GL_RENDER);
  1485.  
  1486.                     cerr<<"Picking.\n";
  1487.  
  1488.                     for (int qi = 0; qi < (int)hits; qi++)
  1489.                     {
  1490.                        if(selectBuf[qi*4 + 3] == 150)
  1491.                             alSourcePlay(Source);
  1492.                     }
  1493.  
  1494.  
  1495.                     glMatrixMode(GL_MODELVIEW);
  1496.  
  1497.         GLfloat xtrans, ztrans, ytrans;
  1498.         GLfloat sceneroty;
  1499.  
  1500.         // calculate translations and rotations.
  1501.         xtrans = -xpos;
  1502.         ztrans = -zpos;
  1503.         ytrans = -(walkbias+ypos)-0.25f;
  1504.         sceneroty = 360.0f - yrot;
  1505.  
  1506.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     // Clear The Screen And The Depth Buffer
  1507.         glLoadIdentity();               // Reset The View
  1508.  
  1509.         glRotatef(lookupdown, 1.0f, 0, 0);
  1510.         glRotatef(sceneroty, 0, 1.0f, 0);
  1511.         glTranslatef(xtrans, ytrans, ztrans);
  1512.  
  1513.         glPushMatrix();
  1514.         showcubes(true);
  1515.         glPopMatrix();
  1516.         glPushMatrix();
  1517.         showgrounditems();
  1518.         glPopMatrix();
  1519.         glPushMatrix();
  1520.         showplants(true);
  1521.         glPopMatrix();
  1522.  
  1523.         glEnable2D();
  1524.         printchats();
  1525.         showitems();
  1526.         glDisable2D();
  1527.  
  1528.         glReadPixels(cursorX,viewport[3]-cursorY,1,1,GL_RGB,GL_UNSIGNED_BYTE,(void*)pixel);
  1529.         cerr<<"Clicked pixel is "<<(int)pixel[0]<<":"<<(int)pixel[1]<<":"<<(int)pixel[2]<<"\n";
  1530.  
  1531.         if((int)pixel[0] == 80)
  1532.             pickplant((int)pixel[2]);
  1533.                 }
  1534.  
  1535.                 else
  1536.                 {
  1537.                     if(event.button.button == SDL_BUTTON_LEFT) inventorydrop(invselect);
  1538.                     else inventoryuse(invselect);
  1539.                 }
  1540.             }
  1541.  
  1542.             if(event.type == SDL_KEYDOWN)
  1543.             {
  1544.                 if(event.key.keysym.sym == SDLK_ESCAPE)
  1545.                 {
  1546.                     done = true;
  1547.                     break;
  1548.                 }
  1549.  
  1550.                 if(takinginput)
  1551.                 {
  1552.                     if(tell.length() < 40)
  1553.                     {
  1554.                         if( event.key.keysym.unicode == (Uint16)' ' )
  1555.                         {
  1556.                             tell = tell+" ";
  1557.                         }
  1558.                         else if( ( event.key.keysym.unicode >= (Uint16)'0' ) && ( event.key.keysym.unicode <= (Uint16)'9' ) )
  1559.                         {
  1560.                             tell += (char)event.key.keysym.unicode;
  1561.                         }
  1562.                         else if( ( event.key.keysym.unicode >= (Uint16)'A' ) && ( event.key.keysym.unicode <= (Uint16)'Z' ) )
  1563.                         {
  1564.                             tell += (char)event.key.keysym.unicode;
  1565.                         }
  1566.                         else if( ( event.key.keysym.unicode >= (Uint16)'a' ) && ( event.key.keysym.unicode <= (Uint16)'z' ) )
  1567.                         {
  1568.                             tell += (char)event.key.keysym.unicode;
  1569.                         }
  1570.                     }
  1571.  
  1572.  
  1573.                     if(event.key.keysym.sym == SDLK_BACKSPACE)
  1574.                     {
  1575.                         if(tell.length() > 0)
  1576.                         {
  1577.                             tell.erase(tell.length()-1);
  1578.                         }
  1579.                     }
  1580.  
  1581.                     if(event.key.keysym.sym == SDLK_RETURN)
  1582.                     {
  1583.                         if(tell.length() > 0)
  1584.                         {
  1585.                             sendchat(tell);
  1586.                             tell = "";
  1587.                         }
  1588.                     }
  1589.                 }
  1590.  
  1591.                 if(event.key.keysym.sym == SDLK_F1)
  1592.                 {
  1593.                     takinginput = !takinginput;
  1594.                     if(!takinginput)
  1595.                         tell = "";
  1596.                 }
  1597.  
  1598.                 if(event.key.keysym.sym == SDLK_DOWN)
  1599.                     skup = true;
  1600.                 if(event.key.keysym.sym == SDLK_UP)
  1601.                     skdown = true;
  1602.                 if(event.key.keysym.sym == SDLK_LEFT)
  1603.                     skleft = true;
  1604.                 if(event.key.keysym.sym == SDLK_RIGHT)
  1605.                     skright = true;
  1606.                 if(event.key.keysym.sym == SDLK_PAGEUP)
  1607.                     skpu = true;
  1608.                 if(event.key.keysym.sym == SDLK_PAGEDOWN)
  1609.                     skpd = true;
  1610.  
  1611.                 if(event.key.keysym.sym == SDLK_SPACE && !jumped)
  1612.                 {
  1613.                     yvel += 0.075;
  1614.                     jumped = true;
  1615.                     lastjumptime = SDL_GetTicks();
  1616.                 }
  1617.             }
  1618.  
  1619.             if(event.type == SDL_KEYUP)
  1620.             {
  1621.                  if(event.key.keysym.sym == SDLK_DOWN)
  1622.                     skup = false;
  1623.                 if(event.key.keysym.sym == SDLK_UP)
  1624.                     skdown = false;
  1625.                 if(event.key.keysym.sym == SDLK_LEFT)
  1626.                     skleft = false;
  1627.                 if(event.key.keysym.sym == SDLK_RIGHT)
  1628.                     skright = false;
  1629.                 if(event.key.keysym.sym == SDLK_PAGEUP)
  1630.                     skpu = false;
  1631.                 if(event.key.keysym.sym == SDLK_PAGEDOWN)
  1632.                     skpd = false;
  1633.             }
  1634.  
  1635.             if(event.type == SDL_MOUSEMOTION)
  1636.             {
  1637.  
  1638.             }
  1639.         }
  1640.  
  1641.         float lastx = xpos,lastz = zpos;
  1642.  
  1643.         Uint8 *keys = SDL_GetKeyState(NULL);
  1644.         if ( keys[SDLK_PAGEUP] == SDL_PRESSED ) {
  1645.         z -= 0.2f;
  1646.         lookupdown -= 0.2f;
  1647.         }
  1648.         if ( keys[SDLK_PAGEDOWN] == SDL_PRESSED ) {
  1649.         z += 0.2f;
  1650.         lookupdown += 0.2f;
  1651.         }
  1652.         if ( keys[SDLK_UP] == SDL_PRESSED ) {
  1653.         xpos -= (float)sin(yrot*piover180) * 0.05f;
  1654.         zpos -= (float)cos(yrot*piover180) * 0.05f;
  1655.         if (walkbiasangle >= 359.0f)
  1656.             walkbiasangle = 0.0f;
  1657.         else
  1658.             walkbiasangle+= 10;
  1659.         walkbias = (float)sin(walkbiasangle * piover180)/20.0f;
  1660.         }
  1661.         if ( keys[SDLK_DOWN] == SDL_PRESSED ) {
  1662.         xpos += (float)sin(yrot*piover180) * 0.05f;
  1663.         zpos += (float)cos(yrot*piover180) * 0.05f;
  1664.         if (walkbiasangle <= 1.0f)
  1665.             walkbiasangle = 359.0f;
  1666.         else
  1667.             walkbiasangle-= 10;
  1668.         walkbias = (float)sin(walkbiasangle * piover180)/20.0f;
  1669.         }
  1670.         if ( keys[SDLK_LEFT] == SDL_PRESSED ) {
  1671.         yrot += 1.5f;
  1672.         }
  1673.         if ( keys[SDLK_RIGHT] == SDL_PRESSED ) {
  1674.         yrot -= 1.5f;
  1675.         }
  1676.  
  1677.         bool doreset = false;
  1678.  
  1679.         for(int i = 0; i<numberofcubes; i++)
  1680.         {
  1681.             float oldscale = scale;
  1682.  
  1683.             if(cubes[i].sc > 0)
  1684.                 scale = cubes[i].sc;
  1685.  
  1686.             if(xpos > cubes[i].x-spacebuf && xpos < cubes[i].x+scale+spacebuf)
  1687.             {
  1688.                 if(ypos > cubes[i].y-spacebuf && ypos < cubes[i].y+spacebuf+scale)
  1689.                 {
  1690.                     if(zpos > cubes[i].z-scale-spacebuf && zpos < cubes[i].z+spacebuf)
  1691.                     {
  1692.                         ypos+=0.25;
  1693.                         bool keephigh = true;
  1694.                         if(xpos > cubes[i].x-spacebuf && xpos < cubes[i].x+scale+spacebuf)
  1695.                         {
  1696.                             if(ypos > cubes[i].y-spacebuf && ypos < cubes[i].y+spacebuf+scale)
  1697.                             {
  1698.                                 if(zpos > cubes[i].z-scale-spacebuf && zpos < cubes[i].z+spacebuf)
  1699.                                 {
  1700.                                     keephigh = false;
  1701.                                 }
  1702.                             }
  1703.                         }
  1704.                         if(!keephigh)
  1705.                         {
  1706.                             ypos-=0.25;
  1707.                             doreset = true;
  1708.                             break;
  1709.                         }
  1710.                     }
  1711.                 }
  1712.             }
  1713.  
  1714.             scale = oldscale;
  1715.         }
  1716.  
  1717.         if(doreset)
  1718.         {
  1719.             xpos = lastx;
  1720.             zpos = lastz;
  1721.         }
  1722.  
  1723.  
  1724.  
  1725.         xpos += xvel;
  1726.         ypos += yvel;
  1727.         zpos += zvel;
  1728.  
  1729.  
  1730.         doreset = false;
  1731.  
  1732.  
  1733.  
  1734.  
  1735.         for(int i = 0; i<numberofcubes; i++)
  1736.         {
  1737.             float oldscale = scale;
  1738.  
  1739.             if(cubes[i].sc > 0)
  1740.                 scale = cubes[i].sc;
  1741.  
  1742.             if(xpos > cubes[i].x-spacebuf && xpos < cubes[i].x+scale+spacebuf)
  1743.             {
  1744.                 if(ypos > cubes[i].y-spacebuf && ypos < cubes[i].y+spacebuf+scale)
  1745.                 {
  1746.                     if(zpos > cubes[i].z-scale-spacebuf && zpos < cubes[i].z+spacebuf)
  1747.                     {
  1748.                        doreset = true;
  1749.                     }
  1750.                 }
  1751.             }
  1752.  
  1753.             scale = oldscale;
  1754.  
  1755.         }
  1756.  
  1757.         if(doreset)
  1758.         {
  1759.             ypos+=0.05;
  1760.         }
  1761.  
  1762.  
  1763.  
  1764.         GLfloat xtrans, ztrans, ytrans;
  1765.         GLfloat sceneroty;
  1766.  
  1767.         // calculate translations and rotations.
  1768.         xtrans = -xpos;
  1769.         ztrans = -zpos;
  1770.         ytrans = -(walkbias+ypos)-0.25f;
  1771.         sceneroty = 360.0f - yrot;
  1772.  
  1773.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     // Clear The Screen And The Depth Buffer
  1774.         glLoadIdentity();               // Reset The View
  1775.  
  1776.         glRotatef(lookupdown, 1.0f, 0, 0);
  1777.         glRotatef(sceneroty, 0, 1.0f, 0);
  1778.         glTranslatef(xtrans, ytrans, ztrans);
  1779.  
  1780.         glPushMatrix();
  1781.         showcubes(true);
  1782.         glPopMatrix();
  1783.         glPushMatrix();
  1784.         showgrounditems();
  1785.         glPopMatrix();
  1786.         glPushMatrix();
  1787.         showplants(false);
  1788.         glPopMatrix();
  1789.  
  1790.         glEnable2D();
  1791.         printchats();
  1792.         showitems();
  1793.         glDisable2D();
  1794.  
  1795.         SDL_GL_SwapBuffers();
  1796.  
  1797.         SDL_Delay(10);
  1798.  
  1799.         if(firstround)
  1800.         firstround = false;
  1801.         }
  1802.  
  1803.         lft = SDL_GetTicks()-lft;
  1804.     }
  1805.     cerr<<"Main loop stopped.\n";
  1806.     int diff = time(NULL)-starttime;
  1807.     cerr<<"Ran for "<<diff<<" seconds.\n";
  1808.     int fps = ticks/diff;
  1809.     cerr<<"Avraged about "<<fps<<" frames per second.\n";
  1810.  
  1811.  
  1812.     cerr<<"Cleaning up and quiting.\n";
  1813.  
  1814.     cerr<<"OPENAL: "<<alGetError()<<"\n";
  1815.     cerr<<"OPENAL: "<<istr(alGetError())<<"\n";
  1816.  
  1817.     SDLNet_Quit();
  1818.     SDL_Quit();
  1819.     alDeleteBuffers(1, &Buffer);
  1820.     alDeleteSources(1, &Source);
  1821.     alutExit();
  1822.  
  1823.     cerr<<"Exited without any problems.\n";
  1824.     return 4525;
  1825. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement