/////////////////////////////////////////////////////////// ////// Released under the GPL ////// see gpl.txt ////// Written by and copyrighted by: ////// Michael Gibson ////// (c)2003, 2004, 2005, 2006 /////////////////////////////////////////////////////////// #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]; 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; ///graphic interface int northbutton; int northeastbutton; int eastbutton; int southeastbutton; int southbutton; int southwestbutton; int westbutton; int northwestbutton; int talkbutton; int attackbutton; int purchasebutton; int startgamebutton; int text1,text2; //functions void Rsocket(); void Rconnect(); void ReadConf(); void udp_server(); void start_game(); void report_move_to_server(); void tcp_logon(); void find_ip(); //////////////////////////////// // redraw map /////////////////////////////// void redraw_map(){ int xx,yy; int map_num; int image_num; map_num=0; image_num=0; for(yy=64;yy<360;yy=yy+64){ for(xx=256;xx<575;xx=xx+64){ if(map[map_num] == 1) gdx_imageupdate(image_num,"grass.xpm"); if(map[map_num] == 2) gdx_imageupdate(image_num,"mud.xpm"); if(map[map_num] == 3) gdx_imageupdate(image_num,"sand.xpm"); if(map[map_num] ==100) gdx_imageupdate(image_num,"tree.xpm"); if(map[map_num] ==101) gdx_imageupdate(image_num,"tree2.xpm"); if(map[map_num] ==102) gdx_imageupdate(image_num,"tree3.xpm"); if(map[map_num] ==200) gdx_imageupdate(image_num,"mount.xpm"); if(map[map_num] ==800) gdx_imageupdate(image_num,"tres.xpm"); if(map[map_num] ==801) gdx_imageupdate(image_num,"tres.xpm"); if(map[map_num] ==802) gdx_imageupdate(image_num,"tres.xpm"); map_num++; image_num++; } } } ///////////////////////////////// // move //////////////////////////////// void movenorth(){ player_move='1'; report_move_to_server(); redraw_map(); } void movenortheast(){ player_move='2'; report_move_to_server(); redraw_map(); } void moveeast(){ player_move='3'; report_move_to_server(); redraw_map(); } void movesoutheast(){ player_move='4'; report_move_to_server(); redraw_map(); } void movesouth(){ player_move='5'; report_move_to_server(); redraw_map(); } void movesouthwest(){ player_move='6'; report_move_to_server(); redraw_map(); } void movewest(){ player_move='7'; report_move_to_server(); redraw_map(); } void movenorthwest(){ player_move='8'; report_move_to_server(); redraw_map(); } void attack(){ char send_packet[20]; char recvln[7]; int near_player; //int mapnum,xx; //find nearest player //todo!!!!! //for testing purposes we will use 5 near_player=5; strcpy(send_packet,"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); //format packet sprintf(send_packet, "AT %04d %04d", player_num, near_player); printf("attack attempted by %04d\n", player_num); 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 */ //example |AT F 0| memset(buff,'\0',BUFFSIZE); alarm(15); read(server2,recvln,114); alarm(0); printf("recieved\n'%s'\n",recvln); if(recvln[3]=='F' && recvln[5]=='0'){ gdx_settextentry(text2,"Attack Failed - out of weapon's range"); } close(server2); } void talk(){ } void purchase(){ } ///////////////////////////////////////////////////// // main ///////////////////////////////////////////////////// int main(int argc, char *argv[]) { char disp_string[100]; int xx,yy; int map_num; int image_num; player_rac = atoi(argv[1]); //1 human 2 elf 3 halfling 4 dwarf player_wep = atoi(argv[2]); //1 battle axe 2 sword 3 dagger 4 Wooden staff 5 mase 6 sling player_sex = atoi(argv[3]); //1 male 2 female strcpy(server_ip,argv[4]); server_port= atoi(argv[5]); sprintf(eth_card,"eth%s",argv[6]); //format eth[0,1] strcpy(player_name,argv[7]); //format eth[0,1] //if(strlen(eth_card) <= 1){ // strcpy(eth_card,"eth0"); //} printf("incoming race=%s weapon=%s sex=%s %s %s %s %s\n",argv[1],argv[2],argv[3],argv[4],argv[5],argv[6],argv[7]); ptype=1; Rsocket(); Rconnect(); find_ip(); tcp_logon(); ptype=2; Rsocket(); Rconnect(); if(player_rac==1) { strcpy(string_rac,"Human"); } if(player_rac==2) { strcpy(string_rac,"Elf"); } if(player_rac==3) { strcpy(string_rac,"Halfling"); } if(player_rac==4) { strcpy(string_rac,"Dwarf"); } x_open(); gdx_window("GDX MASE Client", 800, 600); gdx_setwindowbg("screen.xpm",1); /// Draw Control Panel northbutton = gdx_imagebutton(660,300,"north.xpm",movenorth); northeastbutton = gdx_imagebutton(690,300,"northeast.xpm",movenortheast); eastbutton = gdx_imagebutton(690,330,"east.xpm",moveeast); southeastbutton = gdx_imagebutton(690,360,"southeast.xpm",movesoutheast); southbutton = gdx_imagebutton(660,360,"south.xpm",movesouth); southwestbutton = gdx_imagebutton(630,360,"southwest.xpm",movesouthwest); westbutton = gdx_imagebutton(630,330,"west.xpm",movewest); northwestbutton = gdx_imagebutton(630,300,"northwest.xpm",movenorthwest); attackbutton = gdx_imagebutton(740,300,"attack.xpm",attack); talkbutton = gdx_imagebutton(740,330,"talk.xpm", talk); talkbutton = gdx_imagebutton(770,330,"name.xpm", talk); purchasebutton = gdx_imagebutton(740,360,"buy.xpm",purchase); if(player_num==1){ startgamebutton=gdx_button(600,425,"Start Game",start_game); } /// Draw Map map_num=0; for(yy=64;yy<360;yy=yy+64){ for(xx=256;xx<575;xx=xx+64){ if(map[map_num] == 1) gdx_image(xx,yy,"grass.xpm"); if(map[map_num] == 2) gdx_image(xx,yy,"mud.xpm"); if(map[map_num] == 3) gdx_image(xx,yy,"sand.xpm"); if(map[map_num] ==100) gdx_image(xx,yy,"tree.xpm"); if(map[map_num] ==101) gdx_image(xx,yy,"tree2.xpm"); if(map[map_num] ==102) gdx_image(xx,yy,"tree3.xpm"); if(map[map_num] ==200) gdx_image(xx,yy,"mount.xpm"); if(map[map_num] ==800) gdx_image(xx,yy,"tres.xpm"); if(map[map_num] ==801) gdx_image(xx,yy,"tres.xpm"); if(map[map_num] ==802) gdx_image(xx,yy,"tres.xpm"); map_num++; } } /// Write character text sprintf(disp_string,"Race: %s",string_rac); gdx_label(disp_string,18, 85,0); sprintf(disp_string,"Strength: %02d",player_str); gdx_label(disp_string,18, 100,0); sprintf(disp_string,"Dexterity: %02d",player_dex); gdx_label(disp_string,18, 115, 0); sprintf(disp_string,"Dexterity Adj: -%02d",dex_adj); gdx_label(disp_string,18, 130, 0); true_dex=player_dex - dex_adj; sprintf(disp_string,"True Dexterity: %02d",true_dex); gdx_label(disp_string,18, 145, 0); sprintf(disp_string,"Experiance: %03d",player_exp); gdx_label(disp_string,18, 160, 0); sprintf(disp_string,"Gold: %03d",player_gld); gdx_label(disp_string,18, 200, 0); sprintf(disp_string,"Silver: %03d",player_slv); gdx_label(disp_string,18, 215, 0); sprintf(disp_string,"Copper: %03d",player_cpr); gdx_label(disp_string,18, 230, 0); sprintf(disp_string,"Player Number: %03d",player_num); gdx_label(disp_string,18, 300, 0); sprintf(disp_string,"Name: %s",player_name); gdx_label(disp_string,18, 320, 0); /// Display weapon if(player_wep==1){ //if battleaxe gdx_image(704,64,"battleaxe.xpm"); gdx_label("BattleAxe",645,225,0); sprintf(disp_string,"Weapon Wear: %d/300",wep_wear); gdx_label(disp_string,645,240,0); } if(player_wep==2){ //if sword gdx_image(704,64,"sword.xpm"); gdx_label("Sword",645,225,0); sprintf(disp_string,"Weapon Wear: %d/250",wep_wear); gdx_label(disp_string,645,240,0); } text1=gdx_textentry(18,458,600,0); text2=gdx_textentry(18,483,600,0); gdx_main(); x_close(); return 0; } ////////////////////////////////////////////////// // 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\n" , 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("\n-- Error finding my IP!!!\n Quiting..\n"); close(sock); ////please don't continue. ////Not going to work if the server can not findr/send to your IP exit(0); } strcpy(my_ip, inet_ntop(PF_INET, (const void *)&sin->sin_addr, address, 50)); printf("my ip is %s\n",my_ip); 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); } /////////////////////////// //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); }