/////////////////////////////////////////////////////////// ////// Released under the GPL ////// see gpl.txt ////// Written by and copyrighted by: ////// Michael Gibson ////// (c)2003, 2004, 2006 /////////////////////////////////////////////////////////// #include "client_X11.h" #include #include #include #include #include #include #include #include #include #include #include #define BUFF 251 #define LEN 1024 char my_ip[50]; char eth_card[50]; int sockfd, server2; struct sockaddr_in serv_addr, cli_addr; struct protoent *ppe; struct sockaddr_in sin; struct sockaddr_in cliAddr, servAddr;// UDP server structure int bind1; # define BUFFSIZE 1024 char buff[BUFFSIZE]="inet.h"; char systemtext[70]; char server_ip[30]; int server_port, ptype; int my_port=1112; char packet[1024]; char new_packet[1024]; XWMHints xwmh = { (InputHint|StateHint), // flags False, // input NormalState, // initial_state 0, // icon pixmap 0, // icon window 0, 0, // icon location 0, // icon mask 0, // Window group }; int map[25]; //display map char player_move; //direction player is moving int player_gld=0; //player starts with none int player_slv=0; //player starts with none int player_cpr=0; //player starts with none int player_x=0; //player x location int player_y=0; //player y location int player_str; int player_dex; int player_exp; int player_rac; //1=human,2=elf,3=halfling,4=dwarf int player_sex; //1=male, 2=female char player_name[50]; char string_rac[25]; int player_num; int player_wep; //1=battleaxe, 2=sword, 3=dagger int player_head=1; //not currently used int player_hair=1; //not currently used int player_body=1; //not currently used int player_armor=0; //0=none, 1=leather, 2=chain, 3=breastplate int player_helmet=0; //0=none, 1=plate int total_armor; int wep_wear; int dex_adj; int true_dex; void Rsocket(); void Rconnect(); void ReadConf(); void udp_server(); void start_game(); void report_move_to_server(); void character_setup(); void text_display(); void tcp_logon(); void find_ip(); void x_imgbox(); void x_open(); ///////////////////////////////////////////////////// // ///////////////////////////////////////////////////// int main() { character_setup(); text_display(); ptype=1; Rsocket(); Rconnect(); find_ip(); tcp_logon(); ptype=2; Rsocket(); Rconnect(); x_open(); x_imgbox(); return(0); } ////////////////////////////////////////////////////////// ////// x_open(); ////////////////////////////////////////////////////////// void x_open() { if ((display = XOpenDisplay(NULL)) == NULL) { printf("X client: can't open %s \n", XDisplayName(NULL)); exit(1); } } //////////////////////////////////////////////////////////// ////// x_close() //////////////////////////////////////////////////////////// void x_close() { XCloseDisplay(display); } /////////////////////////////////////////// // /////////////////////////////////////////// void character_setup() { int temp_int; printf("Please choose Race\n"); printf(" 1 for Human\n"); printf(" 2 for Elf \n"); printf(" 3 for Halfling\n"); printf(" 4 for Dwarf\n"); scanf("%d",&player_rac); printf("Please choose Weapon\n"); printf("1 for Battle Axe\n"); printf("2 for Sword\n"); printf("3 for Dagger\n"); printf("4 for Wooden Staff\n"); printf("5 for Mase\n"); printf("6 for Sling\n"); scanf("%d",&player_wep); printf("Please choose you sex\n"); printf("1 for Male\n"); printf("2 for Female\n"); scanf("%d",&player_sex); printf("IP of theServer you wish to connect:\n"); scanf("%s",server_ip); printf("Port of the Server you wish to connect:\n"); scanf("%d",&server_port); printf("Use 0 for eth0 or 1 for eth1 as primary interface:\n"); scanf("%d",&temp_int); sprintf(eth_card,"eth%d",temp_int); } ///////////////////////////////////////////////// // ///////////////////////////////////////////////// void text_display() { printf("Character\n"); printf("-----------\n"); printf("Race: "); if(player_rac==1) { strcpy(string_rac,"Human"); printf("Human\n"); } if(player_rac==2) { strcpy(string_rac,"Elf"); printf("Elf\n"); } if(player_rac==3) { strcpy(string_rac,"Halfling"); printf("Halfling\n"); } if(player_rac==4) { strcpy(string_rac,"Dwarf"); printf("Dwarf\n"); } printf("Strength: %02d \n",player_str); printf("Dexterity: %02d \n",player_dex); printf("Experiance: %03d \n",player_exp); printf("\n\nWeapon: "); if(player_wep==1) printf("Battle Axe\n"); } ////////////////////////////////////////////////// // open remote socket ////////////////////////////////////////////////// void Rsocket() { /* Opens a TCP socket (an Internet stream socket)*/ if(ptype==1){ if((sockfd = socket(AF_INET, SOCK_STREAM, 6)) < 0){ printf("\n-client: can't open stream socket"); exit(0); } else printf("\n+client: opened stream socket ; socket id = %d" , sockfd); setbuf(stdout, NULL); } if(ptype==2){ if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){ printf("\n-client: can't open stream socket"); exit(0); } else printf("\n+client: opened stream socket ; socket id = %d" , sockfd); setbuf(stdout, NULL); } } ////////////////////////////////////////////////// // Remote connect ////////////////////////////////////////////////// void Rconnect() { int result; /* Fill up the structure "serv_addr" with the address of the server that we want to connect with. */ bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(server_ip); printf("\nConnecting to '%s:%d'",server_ip,server_port); serv_addr.sin_port = htons(server_port); /* Connect to the server */ result = connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); if(result < 0){ printf("\n-client: can't connect to server"); exit(0); } else{ printf("\n+client: connected to server"); } } ////////////////////////////////////////////////// // ////////////////////////////////////////////////// void start_game() { char recvln[114]; char temp_string[10]; int mapnum,xx; ptype=1; Rsocket(); Rconnect(); memset(buff,'\0',BUFFSIZE); strcpy(buff,"PQ S"); printf("\nClient: I am sending '%s' to server\n", buff); setbuf(stdout, NULL); write(sockfd, buff, strlen(buff)); /* Receive's first packet from client */ memset(buff,'\0',BUFFSIZE); alarm(15); read(sockfd,recvln,114) ; alarm(0); printf("recieved\n'%s'\n",recvln); } ////////////////////////////////////////////////// // ////////////////////////////////////////////////// void tcp_logon() { char recvln[114]; char temp_string[10]; int mapnum,xx; memset(buff,'\0',BUFFSIZE); sprintf(buff,"PQ R %s ",my_ip); printf("\nClient: I am sending '%s' to server\n", buff); setbuf(stdout, NULL); write(sockfd, buff, strlen(buff)); /* Receive's first packet from client */ memset(buff,'\0',BUFFSIZE); alarm(15); read(sockfd,recvln,114) ; alarm(0); printf("recieved\n'%s'\n",recvln); sprintf(temp_string,"%c%c%c%c",recvln[ 2],recvln[ 3],recvln[ 4],recvln[ 5]); player_num=atoi(temp_string); sprintf(temp_string,"%c%c%c%c",recvln[ 6],recvln[ 7],recvln[ 8],recvln[ 9]); player_x=atoi(temp_string); sprintf(temp_string,"%c%c%c%c",recvln[10],recvln[11],recvln[12],recvln[13]); player_y=atoi(temp_string); mapnum=14; for(xx=0;xx<25;xx++){ sprintf(temp_string,"%c%c%c%c",recvln[mapnum],recvln[mapnum+1],recvln[mapnum+2],recvln[mapnum+3]); map[xx]=atoi(temp_string); mapnum=mapnum+4; } printf("Map recieved:\n"); printf("%04d %04d %04d %04d %04d\n%04d %04d %04d %04d %04d\n%04d %04d %04d %04d %04d\n%04d %04d %04d %04d %04d\n%04d %04d %04d %04d %04d\n", map[ 0],map[ 1],map[ 2],map[ 3],map[ 4], map[ 5],map[ 6],map[ 7],map[ 8],map[ 9], map[10],map[11],map[12],map[13],map[14], map[15],map[16],map[17],map[18],map[19], map[20],map[21],map[22],map[23],map[24]); close(sockfd); Rsocket(); Rconnect(); sprintf(buff,"PP %04d %03d %03d %03d %03d %03d %1d", player_num, player_rac, player_wep, player_head, player_hair, player_body, player_armor, player_sex); printf("\nClient: I am sending '%s' to server\n", buff); setbuf(stdout, NULL); write(sockfd, buff, strlen(buff)); /* Receive's first packet from client */ memset(buff,'\0',BUFFSIZE); alarm(15); read(sockfd,recvln,114) ; alarm(0); printf("recieved\n'%s'\n",recvln); sprintf(temp_string,"%c%c",recvln[ 3],recvln[ 4]); player_str=atoi(temp_string); sprintf(temp_string,"%c%c",recvln[ 6],recvln[ 7]); player_dex=atoi(temp_string); sprintf(temp_string,"%c%c",recvln[ 9],recvln[10]); player_exp=atoi(temp_string); sprintf(temp_string,"%c%c%c",recvln[12],recvln[13],recvln[14]); wep_wear=atoi(temp_string); sprintf(temp_string,"%c%c",recvln[16],recvln[17]); dex_adj=atoi(temp_string); close(sockfd); } //////////////////////////////////////// // find_ip //////////////////////////////////////// void find_ip() { int sock; char address[50]; struct ifreq ifr; struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_addr; sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); strncpy (ifr.ifr_ifrn.ifrn_name, eth_card, IFNAMSIZ - 1); if (ioctl(sock, SIOCGIFADDR, (void *)&ifr) == -1) { printf("Error finding IP!\n "); close(sock); } strcpy(my_ip, inet_ntop(PF_INET, (const void *)&sin->sin_addr, address, 50)); close(sock); } /////////////////////////////////////////// // report_move_to_server ////////////////////////////////////////// void report_move_to_server() { char send_packet[10]; char recvln[114]; char temp_string[10]; int mapnum,xx; sprintf(send_packet, "MO %04d %c", player_num, player_move); printf("report made\n"); ptype=2; Rsocket(); Rconnect(); udp_server(); printf("\nClient: I am sending '%s' to server\n", send_packet); setbuf(stdout, NULL); write(sockfd, send_packet, strlen(send_packet)); close(sockfd); /* Receive's first packet from client */ memset(buff,'\0',BUFFSIZE); alarm(15); read(server2,recvln,114) ; alarm(0); printf("recieved\n'%s'\n",recvln); alarm(15); read(server2,recvln,114) ; alarm(0); printf("recieved\n'%s'\n",recvln); alarm(15); read(server2,recvln,114) ; alarm(0); printf("recieved\n'%s'\n",recvln); mapnum=2; for(xx=0;xx<25;xx++){ sprintf(temp_string,"%c%c%c%c",recvln[mapnum],recvln[mapnum+1],recvln[mapnum+2],recvln[mapnum+3]); map[xx]=atoi(temp_string); mapnum=mapnum+4; } close(server2); } ///////////////////////////////// // ///////////////////////////////// void x_imgbox() { int xx,yy,map_num; GC pen[500]; // GC to draw with -- black pen GC gcP,pcPgrass,pcPmud,pcPsand,pcPtree,pcPtres,pcPmount,pcPbattleaxe,pcPsword,pcPpanel; GC pen5; // GC to draw with -- Grey pen XGCValues pen5_prop; // Struct for creating GC char disp_string[55]; // user set text Pixmap mask, pm, pmP,pmP2; Drawable drawpm; XpmAttributes attributes,attributes2; XpmColorSymbol xpmcolor[1]; XGCValues pen_prop; // Struct for creating GC XGCValues PM_prop,PM_prop2; XFontStruct *fontstruct; // Font descriptor -- all pens given same font unsigned long fth; // Font size parameters unsigned long fg, bg, bd; // Pixel values unsigned long bw; // Border width XEvent event; // Event received XSizeHints xsh; // Size hints for window manager char *geomSpec; // Window geometry string XSetWindowAttributes xswa; // Temporary Set Window Attribute struct int imageh=20,imagew=20; unsigned long color[500]; char c_color[500][10]; unsigned long b_color=X_GREY; char bc_color[10]; int xbutton, ybutton; if ((fontstruct = XLoadQueryFont(display, "fixed")) == NULL) { printf("client: display doesn't know the font 'fixed'\n"); exit(1); } fth = fontstruct->max_bounds.ascent + fontstruct->max_bounds.descent; xsh.flags = (PPosition | PSize); window = XCreateSimpleWindow(display,DefaultRootWindow(display),50,50,imagew,imageh,1,X_DGREY,b_color); XSetStandardProperties(display, window, "M.A.S.E", "M.A.S.E", None, NULL, 0, &xsh); xswa.colormap = DefaultColormap(display, DefaultScreen(display)); xswa.bit_gravity = CenterGravity; XChangeWindowAttributes(display, window, (CWColormap | CWBitGravity), &xswa); // set all pens to same font pen5_prop.font = fontstruct->fid; //set drawing color // set all pens to same font pen5_prop.font = fontstruct->fid; //set drawing color pen5_prop.foreground = X_WHITE; pen5_prop.background = X_BLACK; pen5 = XCreateGC(display, window, (GCFont | GCForeground | GCBackground), &pen5_prop); attributes.valuemask = XpmColormap; attributes.x_hotspot = 0; attributes.y_hotspot = 0; attributes.depth = DefaultDepth(display, DefaultScreen(display));; attributes.colormap = DefaultColormap(display, DefaultScreen(display)); PM_prop.function = GXcopy; PM_prop.plane_mask = AllPlanes; PM_prop.foreground = 0; //setup backround gcP = XCreateGC(display, window, GCForeground|GCFunction|GCPlaneMask, &PM_prop); attributes.valuemask = XpmSize; XpmReadFileToPixmap(display,window,"screen.xpm",&pmP,&mask,&attributes); XSetFillStyle(display, gcP, FillTiled); XSetTile(display, gcP, pmP); XSetClipMask(display,gcP,mask); imagew=attributes.width; imageh=attributes.height; XResizeWindow(display, window, imagew, imageh); //setup grass tile pcPgrass = XCreateGC(display, window, GCForeground|GCFunction|GCPlaneMask, &PM_prop); attributes2.valuemask = XpmSize; XpmReadFileToPixmap(display,window,"grass.xpm",&pmP2,&mask,&attributes2); XSetFillStyle(display, pcPgrass, FillTiled); XSetTile(display, pcPgrass, pmP2); //setup mud tile pcPmud = XCreateGC(display, window, GCForeground|GCFunction|GCPlaneMask, &PM_prop); attributes2.valuemask = XpmSize; XpmReadFileToPixmap(display,window,"mud.xpm",&pmP2,&mask,&attributes2); XSetFillStyle(display, pcPmud, FillTiled); XSetTile(display, pcPmud, pmP2); //setup sand tile pcPsand = XCreateGC(display, window, GCForeground|GCFunction|GCPlaneMask, &PM_prop); attributes2.valuemask = XpmSize; XpmReadFileToPixmap(display,window,"sand.xpm",&pmP2,&mask,&attributes2); XSetFillStyle(display, pcPsand, FillTiled); XSetTile(display, pcPsand, pmP2); //setup tree tile pcPtree = XCreateGC(display, window, GCForeground|GCFunction|GCPlaneMask, &PM_prop); attributes2.valuemask = XpmSize; XpmReadFileToPixmap(display,window,"tree.xpm",&pmP2,&mask,&attributes2); XSetFillStyle(display, pcPtree, FillTiled); XSetTile(display, pcPtree, pmP2); //setup treasure tile pcPtres = XCreateGC(display, window, GCForeground|GCFunction|GCPlaneMask, &PM_prop); attributes2.valuemask = XpmSize; XpmReadFileToPixmap(display,window,"tree.xpm",&pmP2,&mask,&attributes2); XSetFillStyle(display, pcPtres, FillTiled); XSetTile(display, pcPtres, pmP2); //setup mount tile pcPmount = XCreateGC(display, window, GCForeground|GCFunction|GCPlaneMask, &PM_prop); attributes2.valuemask = XpmSize; XpmReadFileToPixmap(display,window,"mount.xpm",&pmP2,&mask,&attributes2); XSetFillStyle(display, pcPmount, FillTiled); XSetTile(display, pcPmount, pmP2); //setup battleaxe tile pcPbattleaxe = XCreateGC(display, window, GCForeground|GCFunction|GCPlaneMask, &PM_prop); attributes2.valuemask = XpmSize; XpmReadFileToPixmap(display,window,"battleaxe.xpm",&pmP2,&mask,&attributes2); XSetFillStyle(display, pcPbattleaxe, FillTiled); XSetTile(display, pcPbattleaxe, pmP2); //setup sword tile pcPsword = XCreateGC(display, window, GCForeground|GCFunction|GCPlaneMask, &PM_prop); attributes2.valuemask = XpmSize; XpmReadFileToPixmap(display,window,"sword.xpm",&pmP2,&mask,&attributes2); XSetFillStyle(display, pcPsword, FillTiled); XSetTile(display, pcPsword, pmP2); //setup panel tile pcPpanel = XCreateGC(display, window, GCForeground|GCFunction|GCPlaneMask, &PM_prop); attributes2.valuemask = XpmSize; XpmReadFileToPixmap(display,window,"panel.xpm",&pmP2,&mask,&attributes2); XSetFillStyle(display, pcPpanel, FillTiled); XSetTile(display, pcPpanel, pmP2); XSelectInput(display, window, EVENT_MASK); XMapWindow(display, window); // show window while (1) { // Loop forever, examining each event. XNextEvent(display, &event); // capture event if (event.type == ButtonPress){ // if event is mouse button press xbutton=event.xbutton.x; // read x screen cord ybutton=event.xbutton.y; // read y screen cord printf("Positon %d,%d clicked\n",xbutton,ybutton); // mouse debug // if move up-left button if(xbutton >= 593 && xbutton <= 613 && ybutton >=399 && ybutton <=420 ) { player_move='8'; report_move_to_server(); } if(xbutton >= 617 && xbutton <= 637 && ybutton >=399 && ybutton <=420 ) { player_move='1'; report_move_to_server(); } if(xbutton >= 617 && xbutton <= 637 && ybutton >=448 && ybutton <=468 ) { player_move='5'; report_move_to_server(); } if(xbutton >= 641 && xbutton <= 662 && ybutton >=426 && ybutton <= 445) { player_move='3'; report_move_to_server(); } if(xbutton >= 595 && xbutton <= 613 && ybutton >=426 && ybutton <= 443) { player_move='7'; report_move_to_server(); } if(xbutton >= 640 && xbutton <= 680 && ybutton >=513 && ybutton <=532 ) { start_game(); } map_num=0; //fill in map for(yy=64;yy<360;yy=yy+64){ for(xx=256;xx<575;xx=xx+64){ if(map[map_num] == 1) XFillRectangle(display, window, pcPgrass, xx,yy,64,64); if(map[map_num] == 2) XFillRectangle(display, window, pcPmud, xx,yy,64,64); if(map[map_num] == 3) XFillRectangle(display, window, pcPsand, xx,yy,64,64); if(map[map_num] ==100) XFillRectangle(display, window, pcPtree, xx,yy,64,64); if(map[map_num] ==200) XFillRectangle(display, window, pcPmount, xx,yy,64,64); map_num++; } } }//end Button press event // if (event.type == Expose && event.xexpose.count == 0) { if (event.type == Expose) { XWindowAttributes xwa; // Temp Get Window Attribute struct while (XCheckTypedEvent(display, Expose, &event)); if (XGetWindowAttributes(display, window, &xwa) == 0) break; XClearWindow(display, window); XFlush(display); //fill in background XFillRectangle(display, window, gcP, 0, 0 ,attributes.width + 2,attributes.height +2); //Panel XFillRectangle(display, window, pcPpanel,576,384,192,192); //display map map_num=0; //fill in map for(yy=64;yy<360;yy=yy+64){ for(xx=256;xx<575;xx=xx+64){ if(map[map_num] == 1) XFillRectangle(display, window, pcPgrass, xx,yy,64,64); if(map[map_num] == 2) XFillRectangle(display, window, pcPmud, xx,yy,64,64); if(map[map_num] == 3) XFillRectangle(display, window, pcPsand, xx,yy,64,64); if(map[map_num] ==100) XFillRectangle(display, window, pcPtree, xx,yy,64,64); if(map[map_num] ==200) XFillRectangle(display, window, pcPmount, xx,yy,64,64); map_num++; } } //write character text sprintf(disp_string,"Race: %s",string_rac); XDrawString(display, window, pen5, 18, 85,disp_string, strlen(disp_string)); sprintf(disp_string,"Strength: %02d",player_str); XDrawString(display, window, pen5, 18, 100,disp_string, strlen(disp_string)); sprintf(disp_string,"Dexterity: %02d",player_dex); XDrawString(display, window, pen5, 18, 115, disp_string, strlen(disp_string)); sprintf(disp_string,"Dexterity Adj: -%02d",dex_adj); XDrawString(display, window, pen5, 18, 130, disp_string, strlen(disp_string)); true_dex=player_dex - dex_adj; sprintf(disp_string,"True Dexterity: %02d",true_dex); XDrawString(display, window, pen5, 18, 145, disp_string, strlen(disp_string)); sprintf(disp_string,"Experiance: %03d",player_exp); XDrawString(display, window, pen5, 18, 160, disp_string, strlen(disp_string)); sprintf(disp_string,"Gold: %03d",player_gld); XDrawString(display, window, pen5, 18, 200, disp_string, strlen(disp_string)); sprintf(disp_string,"Silver: %03d",player_slv); XDrawString(display, window, pen5, 18, 215, disp_string, strlen(disp_string)); sprintf(disp_string,"Copper: %03d",player_cpr); XDrawString(display, window, pen5, 18, 230, disp_string, strlen(disp_string)); sprintf(disp_string,"Player Number: %03d",player_num); XDrawString(display, window, pen5, 18, 300, disp_string, strlen(disp_string)); //display weapon if(player_wep==1){ //if battleaxe XFillRectangle(display, window, pcPbattleaxe, 704, 64 ,64,64); XDrawString(display,window,pen5,645,225,"BattleAxe",9); sprintf(disp_string,"Weapon Wear: %d/300",wep_wear); XDrawString(display,window,pen5,645,240,disp_string,strlen(disp_string)); } if(player_wep==2){ //if sword XFillRectangle(display, window, pcPsword, 704, 64 ,64,64); XDrawString(display,window,pen5,645,225,"Sword",5); sprintf(disp_string,"Weapon Wear: %d/250",wep_wear); XDrawString(display,window,pen5,645,240,disp_string,strlen(disp_string)); } } } } /////////////////////////// //udp server /////////////////////////// void udp_server(){ bzero((char *)&sin, sizeof(sin)); servAddr.sin_family = AF_INET; servAddr.sin_addr.s_addr = htonl(INADDR_ANY); //Load UDP server address servAddr.sin_port = htons(my_port); //Load and Convert UDP port to Little Ind ppe = getprotobyname("udp"); //Declare UPD setbuf(stdout, NULL); server2 = socket(PF_INET, SOCK_DGRAM, ppe->p_proto); printf("\nvalue of UDP socket %d\n",server2); bind1 = bind(server2, (struct sockaddr *) &servAddr,sizeof(servAddr)); printf("\n%d value of UDP bind\n", bind1); setbuf(stdout, NULL); }