| 1 |
|
|---|
| 2 |
#include "structures.h" |
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
#include <stdlib.h> |
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
#define _SERVER_IP "10.0.10.159" |
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
int recievedata() |
|---|
| 23 |
{ |
|---|
| 24 |
sockaddr_in name; |
|---|
| 25 |
WSADATA WSAData; |
|---|
| 26 |
SOCKET sock; |
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
if (WSAStartup (MAKEWORD(1,1), &WSAData) != 0) |
|---|
| 30 |
printf("Error initializing WinSocket\n"); |
|---|
| 31 |
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
|---|
| 32 |
printf("Invalid Socket!\n"); |
|---|
| 33 |
|
|---|
| 34 |
name.sin_family = AF_INET; |
|---|
| 35 |
name.sin_port = htons(PORT); |
|---|
| 36 |
name.sin_addr.S_un.S_addr = inet_addr(_SERVER_IP); |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
if (connect(sock, (LPSOCKADDR) &name, sizeof(sockaddr_in)) == SOCKET_ERROR) |
|---|
| 40 |
{ |
|---|
| 41 |
printf("Connect error!\n"); |
|---|
| 42 |
closesocket(sock); |
|---|
| 43 |
return 0; |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
char buff[512]; |
|---|
| 50 |
|
|---|
| 51 |
int recvd = recv(sock, buff, 16, 0); |
|---|
| 52 |
if (strcmp(buff,"WORLD") !=0) |
|---|
| 53 |
{ |
|---|
| 54 |
|
|---|
| 55 |
closesocket(sock); |
|---|
| 56 |
return 0; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
int numNPC=0; |
|---|
| 60 |
recvd = recv(sock, &buff[0], 4, 0); |
|---|
| 61 |
char temp[4]; |
|---|
| 62 |
strncpy(temp,&buff[0],4); |
|---|
| 63 |
numNPC = int(temp); |
|---|
| 64 |
if (numNPC<0) |
|---|
| 65 |
{ |
|---|
| 66 |
|
|---|
| 67 |
closesocket(sock); |
|---|
| 68 |
return 0; |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
for (int i=0; i<numNPC ; i++) |
|---|
| 73 |
{ |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
int recvd = recv(sock, buff, 32, 0); |
|---|
| 77 |
if (strcmp(buff,"") ==0) |
|---|
| 78 |
{ |
|---|
| 79 |
|
|---|
| 80 |
closesocket(sock); |
|---|
| 81 |
return 0; |
|---|
| 82 |
} |
|---|
| 83 |
char NPCname[32]; |
|---|
| 84 |
strncpy(&NPCname[0], &buff[0], 32); |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
int loc_x=0; |
|---|
| 88 |
recvd = recv(sock, &buff[0], 4, 0); |
|---|
| 89 |
char temp[4]; |
|---|
| 90 |
strncpy(temp,&buff[0],4); |
|---|
| 91 |
loc_x=int(temp); |
|---|
| 92 |
if (loc_x<0 || loc_x>299) |
|---|
| 93 |
{ |
|---|
| 94 |
|
|---|
| 95 |
closesocket(sock); |
|---|
| 96 |
return 0; |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
int loc_y=0; |
|---|
| 100 |
recvd = recv(sock, &buff[0], 4, 0); |
|---|
| 101 |
strncpy(temp,&buff[0],4); |
|---|
| 102 |
loc_y=int(temp); |
|---|
| 103 |
if (loc_y<0 || loc_y>299) |
|---|
| 104 |
{ |
|---|
| 105 |
|
|---|
| 106 |
closesocket(sock); |
|---|
| 107 |
return 0; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
NPC newNPC; |
|---|
| 111 |
newNPC.xcoord=loc_x; |
|---|
| 112 |
newNPC.ycoord=loc_y; |
|---|
| 113 |
newNPC.name = NPCname; |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
int numNPCattrib=0; |
|---|
| 118 |
recvd = recv(sock, &buff[0], 4, 0); |
|---|
| 119 |
strncpy(temp,&buff[0],4); |
|---|
| 120 |
numNPCattrib=int(temp); |
|---|
| 121 |
if (numNPCattrib<0) |
|---|
| 122 |
{ |
|---|
| 123 |
|
|---|
| 124 |
closesocket(sock); |
|---|
| 125 |
return 0; |
|---|
| 126 |
} |
|---|
| 127 |
for(int k=0; k<numNPCattrib ; k++) |
|---|
| 128 |
{ |
|---|
| 129 |
|
|---|
| 130 |
int recvd = recv(sock, buff, 16, 0); |
|---|
| 131 |
if (strcmp(buff,"") ==0) |
|---|
| 132 |
{ |
|---|
| 133 |
|
|---|
| 134 |
closesocket(sock); |
|---|
| 135 |
return 0; |
|---|
| 136 |
} |
|---|
| 137 |
char NPCattribNAME[16]; |
|---|
| 138 |
strncpy(&NPCattribNAME[0], &buff[0], 16); |
|---|
| 139 |
int attribute_value=0; |
|---|
| 140 |
recvd = recv(sock, &buff[0], 4, 0); |
|---|
| 141 |
strncpy(temp,&buff[0],4); |
|---|
| 142 |
attribute_value=int(temp); |
|---|
| 143 |
if (attribute_value<0) |
|---|
| 144 |
{ |
|---|
| 145 |
|
|---|
| 146 |
closesocket(sock); |
|---|
| 147 |
return 0; |
|---|
| 148 |
} |
|---|
| 149 |
string name = NPCattribNAME; |
|---|
| 150 |
|
|---|
| 151 |
newNPC.attributes.insert(std::pair<string, int>(name,attribute_value)); |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
int numNPCrelationship=0; |
|---|
| 158 |
recvd = recv(sock, &buff[0], 4, 0); |
|---|
| 159 |
strncpy(temp,&buff[0],4); |
|---|
| 160 |
numNPCrelationship=int(temp); |
|---|
| 161 |
if (numNPCrelationship<0) |
|---|
| 162 |
{ |
|---|
| 163 |
|
|---|
| 164 |
closesocket(sock); |
|---|
| 165 |
return 0; |
|---|
| 166 |
} |
|---|
| 167 |
for(k=0; k<numNPCrelationship ; k++) |
|---|
| 168 |
{ |
|---|
| 169 |
|
|---|
| 170 |
recvd = recv(sock, buff, 16, 0); |
|---|
| 171 |
if (strcmp(buff,"") ==0) |
|---|
| 172 |
{ |
|---|
| 173 |
|
|---|
| 174 |
closesocket(sock); |
|---|
| 175 |
return 0; |
|---|
| 176 |
} |
|---|
| 177 |
char NPCrelationshipNAME[16]; |
|---|
| 178 |
strncpy(&NPCrelationshipNAME[0], &buff[0], 16); |
|---|
| 179 |
int group_value=0; |
|---|
| 180 |
recvd = recv(sock, &buff[0], 4, 0); |
|---|
| 181 |
strncpy(temp,&buff[0],4); |
|---|
| 182 |
group_value=int(temp); |
|---|
| 183 |
if (group_value<0) |
|---|
| 184 |
{ |
|---|
| 185 |
|
|---|
| 186 |
closesocket(sock); |
|---|
| 187 |
return 0; |
|---|
| 188 |
} |
|---|
| 189 |
|
|---|
| 190 |
int relationship=0; |
|---|
| 191 |
recvd = recv(sock, &buff[0], 4, 0); |
|---|
| 192 |
strncpy(temp,&buff[0],4); |
|---|
| 193 |
relationship=int(temp); |
|---|
| 194 |
if (relationship<0) |
|---|
| 195 |
{ |
|---|
| 196 |
|
|---|
| 197 |
closesocket(sock); |
|---|
| 198 |
return 0; |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
std::pair <string, int> namegroupvalue(NPCrelationshipNAME, group_value); |
|---|
| 203 |
newNPC.relationships.insert(std::pair<std::pair<string,int> , int>(namegroupvalue,relationship)); |
|---|
| 204 |
} |
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
real_world->NPCs.push_back(newNPC); |
|---|
| 208 |
|
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
closesocket(sock); |
|---|
| 216 |
return -1; |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
int senddata(string query) |
|---|
| 229 |
{ |
|---|
| 230 |
SOCKET sock; |
|---|
| 231 |
sockaddr_in name; |
|---|
| 232 |
WSADATA WSAData; |
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
if (WSAStartup (MAKEWORD(1,1), &WSAData) != 0) |
|---|
| 236 |
printf("Error initializing WinSocket\n"); |
|---|
| 237 |
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
|---|
| 238 |
printf("Invalid Socket!\n"); |
|---|
| 239 |
|
|---|
| 240 |
name.sin_family = AF_INET; |
|---|
| 241 |
name.sin_port = htons(PORT); |
|---|
| 242 |
name.sin_addr.S_un.S_addr = inet_addr(_SERVER_IP); |
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
if (connect(sock, (LPSOCKADDR) &name, sizeof(sockaddr_in)) == SOCKET_ERROR) |
|---|
| 246 |
{ |
|---|
| 247 |
printf("Connect error!\n"); |
|---|
| 248 |
closesocket(sock); |
|---|
| 249 |
return 0; |
|---|
| 250 |
} |
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
if (send(sock, query.c_str(), query.length(), 0) == 0) |
|---|
| 259 |
{ |
|---|
| 260 |
printf("Error sending\n"); |
|---|
| 261 |
closesocket(sock); |
|---|
| 262 |
return 0; |
|---|
| 263 |
} |
|---|
| 264 |
closesocket(sock); |
|---|
| 265 |
return -1; |
|---|
| 266 |
} |
|---|
| 267 |
|
|---|
| 268 |
int temp; |
|---|