| [567] | 1 | #include "Flatline.h" | 
|---|
|  | 2 | #include "Oni_Character.h" | 
|---|
|  | 3 | #include "Flatline_Server.h" | 
|---|
|  | 4 | //#include <sys/time.h> | 
|---|
|  | 5 | #include <time.h> | 
|---|
|  | 6 | uint32_t last1 = 0; uint32_t last2 = 0; | 
|---|
|  | 7 | player_info Players[MAX_PLAYERS] = {{0}, {0}, {0}, {0}}; | 
|---|
|  | 8 | player_info * PlayerList[MAX_CONNECTIONS] = {0}; | 
|---|
|  | 9 | const char * Rejection_Messages[][255] = { | 
|---|
|  | 10 | {"Server is full"}, | 
|---|
|  | 11 | {"-2"}, | 
|---|
|  | 12 | {"-3"}, | 
|---|
|  | 13 | {"-4"}, | 
|---|
|  | 14 | {"-5"}, | 
|---|
|  | 15 | }; | 
|---|
|  | 16 |  | 
|---|
|  | 17 | bool FLrServer_PacketCallback(char* data, int datalen, int from) | 
|---|
|  | 18 | { | 
|---|
|  | 19 | int i, j; | 
|---|
|  | 20 | bool found_player = 0; | 
|---|
|  | 21 | flatline_packet * packet = (flatline_packet*)data; | 
|---|
|  | 22 | static int recieved = 0; | 
|---|
|  | 23 | sockaddr_in sender; | 
|---|
|  | 24 | sender.sin_family = AF_INET; | 
|---|
|  | 25 | sender.sin_port = htons(27777); | 
|---|
|  | 26 | sender.sin_addr = *((struct in_addr*)(int*)&from); | 
|---|
|  | 27 |  | 
|---|
|  | 28 |  | 
|---|
|  | 29 | //packet->data[datalen] = '\0'; | 
|---|
|  | 30 |  | 
|---|
|  | 31 | //DDrConsole_PrintF("Packet \r%d recieved from %i",  ++recieved, from); | 
|---|
|  | 32 |  | 
|---|
|  | 33 |  | 
|---|
|  | 34 |  | 
|---|
|  | 35 | //if data[0] != CONNECT_SEND, search in playerlist for ip address | 
|---|
|  | 36 |  | 
|---|
|  | 37 |  | 
|---|
|  | 38 |  | 
|---|
|  | 39 |  | 
|---|
|  | 40 | switch(packet->id) { | 
|---|
|  | 41 | flatline_packet connect_recv; | 
|---|
|  | 42 | player_info * playah; | 
|---|
|  | 43 | //rewrite this when we get TCP support. | 
|---|
|  | 44 | //rewrite this before we get TCP support* | 
|---|
|  | 45 | //the way of seeing if there is room for players sucks. | 
|---|
|  | 46 | case CONNECT_SEND: | 
|---|
|  | 47 | ; | 
|---|
|  | 48 |  | 
|---|
|  | 49 | connect_recv.id = CONNECT_REPLY; | 
|---|
|  | 50 |  | 
|---|
|  | 51 | //if(Players[i].ip == sender.sin_addr.S_un.S_addr) break; //needs to send an error message | 
|---|
|  | 52 | sender.sin_addr.S_un.S_addr = htonl(sender.sin_addr.S_un.S_addr); | 
|---|
|  | 53 | playah = FLrServer_AddPlayer(from,packet->connect_send.name, 0); | 
|---|
|  | 54 | DDrConsole_PrintF("%s connected from %s", packet->connect_send.name, inet_ntoa(sender.sin_addr ) ); | 
|---|
|  | 55 | if(!((int)playah > -5 && (int)playah <= 0)) { | 
|---|
|  | 56 | flatline_packet new_char = {0}; | 
|---|
|  | 57 | CharacterObject* Char; | 
|---|
|  | 58 | connect_recv.connect_reply.goodtogo = 1; | 
|---|
|  | 59 | connect_recv.connect_reply.player_slot = playah->list_slot; | 
|---|
|  | 60 | DDrConsole_PrintF("Slot: %i", playah->list_slot); | 
|---|
|  | 61 |  | 
|---|
|  | 62 | //sending this several times to make sure it gets through. Really need to make up some form of packet tracking. | 
|---|
|  | 63 | NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER); | 
|---|
|  | 64 | NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER); | 
|---|
|  | 65 | NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER); | 
|---|
|  | 66 | NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER); | 
|---|
|  | 67 | NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER); | 
|---|
|  | 68 | Sleep(100); | 
|---|
|  | 69 |  | 
|---|
|  | 70 | new_char.id = NEW_PLAYER; | 
|---|
|  | 71 | Char = &new_char.new_player.Character; | 
|---|
|  | 72 | memset(Char, 0, sizeof(CharacterObject)); | 
|---|
|  | 73 | Char->Header.Type = 'CHAR'; | 
|---|
|  | 74 | Char->OSD.Options = chr_dontaim | chr_unkillable; | 
|---|
|  | 75 | for(j = 0; j < max_connections; j++) { | 
|---|
|  | 76 | if(PlayerList[j] != 0) { | 
|---|
|  | 77 | new_char.new_player.Playernumber = j; | 
|---|
|  | 78 | sprintf(Char->OSD.Name,"%s",PlayerList[j]->name); | 
|---|
|  | 79 |  | 
|---|
|  | 80 | sprintf(Char->OSD.Class, "%s", TMrInstance_GetInstanceName(PlayerList[j]->Chr->ONCC)); | 
|---|
|  | 81 | DDrConsole_PrintF("Class %s", Char->OSD.Class ); | 
|---|
|  | 82 |  | 
|---|
|  | 83 | sprintf(Char->OSD.Class, "konoko_generic"); | 
|---|
|  | 84 | NetTCPServer_Send((sockaddr *) &sender, (char*)&new_char, sizeof(new_player) + FLATLINE_HEADER ); | 
|---|
|  | 85 | } | 
|---|
|  | 86 |  | 
|---|
|  | 87 | } | 
|---|
|  | 88 | } | 
|---|
|  | 89 | else { | 
|---|
|  | 90 | //fix the error messages... | 
|---|
|  | 91 | DDrConsole_PrintF("Server is full. :("); | 
|---|
|  | 92 | connect_recv.connect_reply.goodtogo = 0; | 
|---|
|  | 93 | sender.sin_addr.S_un.S_addr = htonl(sender.sin_addr.S_un.S_addr); | 
|---|
|  | 94 | memcpy(&connect_recv.connect_reply.message,"Server is full.", sizeof("Server is full.")); | 
|---|
|  | 95 | NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(bool)*2 + FLATLINE_HEADER + sizeof("Server is full.")); | 
|---|
|  | 96 |  | 
|---|
|  | 97 | } | 
|---|
|  | 98 |  | 
|---|
|  | 99 |  | 
|---|
|  | 100 | break; | 
|---|
|  | 101 | case CONNECT_REPLY: | 
|---|
|  | 102 | break;  //do nothing...a server shouldn't recieve this type of packet. | 
|---|
|  | 103 | case MESSAGE: | 
|---|
|  | 104 | for(i = 0; i < MAX_PLAYERS; i++) { | 
|---|
|  | 105 | //DDrConsole_PrintF("%i : %i | %s : %s", from, Players[i].ip, inet_ntoa(*(struct in_addr*)&from), inet_ntoa(*(struct in_addr*)&(Players[i].ip))); | 
|---|
|  | 106 | if(Players[i].ip == sender.sin_addr.S_un.S_addr) { | 
|---|
|  | 107 | found_player = 1; | 
|---|
|  | 108 | break; | 
|---|
|  | 109 | } | 
|---|
|  | 110 | } | 
|---|
|  | 111 | if(found_player == 0) return true; | 
|---|
|  | 112 | else { | 
|---|
|  | 113 | char message_buffer[512] = {0}; | 
|---|
|  | 114 | flatline_packet message; | 
|---|
|  | 115 | int message_size; | 
|---|
|  | 116 | data[datalen] = 0; | 
|---|
|  | 117 |  | 
|---|
|  | 118 | DDrConsole_PrintF("%s: %s", Players[i].name, packet->data); | 
|---|
|  | 119 | sprintf(message_buffer, "%s: %s", Players[i].name, packet->data); | 
|---|
|  | 120 |  | 
|---|
|  | 121 | message.id = MESSAGE; | 
|---|
|  | 122 | message_size = sprintf(message.data, "%s", message_buffer); | 
|---|
|  | 123 | COrMessage_Print(message_buffer, "chat", 0); | 
|---|
|  | 124 | UDPServer_SendToAll(&message, message_size + 1 + FLATLINE_HEADER); | 
|---|
|  | 125 | break; | 
|---|
|  | 126 | } | 
|---|
|  | 127 | case CHANGE_NAME: | 
|---|
|  | 128 | ; //wtf, needed or i get an error. | 
|---|
|  | 129 | DDrConsole_PrintF("Changing Name to: %s", packet->data); | 
|---|
|  | 130 | for(i = 0; i < MAX_PLAYERS; i++) { | 
|---|
|  | 131 | if(Players[i].ip == sender.sin_addr.S_un.S_addr) { | 
|---|
|  | 132 | found_player = 1; | 
|---|
|  | 133 | break; | 
|---|
|  | 134 | } | 
|---|
|  | 135 | } | 
|---|
|  | 136 | if(found_player == 0) return true; | 
|---|
|  | 137 | else { | 
|---|
|  | 138 | bool name_exists = 0; | 
|---|
|  | 139 | for(j = 0; j < MAX_PLAYERS; j++) { | 
|---|
|  | 140 | if(!strcmp(packet->data, Players[j].name)) { | 
|---|
|  | 141 | name_exists = 1; | 
|---|
|  | 142 | break; | 
|---|
|  | 143 | } | 
|---|
|  | 144 | } | 
|---|
|  | 145 | if(!name_exists) { | 
|---|
|  | 146 | char message_buffer[1024]; | 
|---|
|  | 147 | sprintf(message_buffer,"%s changed their name to %s", Players[i].name, packet->data); | 
|---|
|  | 148 | COrMessage_Print(message_buffer, "name_change", 0); | 
|---|
|  | 149 | memcpy(Players[i].name, packet->data, 256); | 
|---|
|  | 150 |  | 
|---|
|  | 151 | } | 
|---|
|  | 152 | break; | 
|---|
|  | 153 | } | 
|---|
|  | 154 | case PLAYER_INPUT: | 
|---|
|  | 155 |  | 
|---|
|  | 156 | for(i = 0; i < max_connections; i++) { | 
|---|
|  | 157 | if(PlayerList[i] != 0 && PlayerList[i]->ip == sender.sin_addr.S_un.S_addr) { | 
|---|
|  | 158 | found_player = 1; | 
|---|
|  | 159 | break; | 
|---|
|  | 160 | } | 
|---|
|  | 161 | } | 
|---|
|  | 162 |  | 
|---|
|  | 163 | if(found_player == 0) return true; | 
|---|
|  | 164 | else { | 
|---|
|  | 165 | input_struct * packet_input = &packet->input_struct; | 
|---|
|  | 166 |  | 
|---|
|  | 167 |  | 
|---|
|  | 168 | PlayerList[i]->Actions1 = packet_input->Actions1; | 
|---|
|  | 169 | PlayerList[i]->Actions2 = packet_input->Actions2; | 
|---|
|  | 170 | PlayerList[i]->MouseDeltaX = packet_input->MouseDeltaX; | 
|---|
|  | 171 | PlayerList[i]->MouseDeltaY = packet_input->MouseDeltaY; | 
|---|
|  | 172 |  | 
|---|
|  | 173 |  | 
|---|
|  | 174 | break; | 
|---|
|  | 175 | } | 
|---|
|  | 176 | default: | 
|---|
|  | 177 | DDrConsole_PrintF("Warning, recieved badly formed packet!"); | 
|---|
|  | 178 | break; | 
|---|
|  | 179 | } | 
|---|
|  | 180 | return true; | 
|---|
|  | 181 | } | 
|---|
|  | 182 |  | 
|---|
|  | 183 | bool FLrServer_Run() | 
|---|
|  | 184 | { | 
|---|
|  | 185 | // Get the local hostname | 
|---|
|  | 186 | char szHostName[255]; | 
|---|
|  | 187 | struct hostent *host_entry; | 
|---|
|  | 188 | gethostname(szHostName, 255); | 
|---|
|  | 189 |  | 
|---|
|  | 190 | host_entry=gethostbyname(szHostName); | 
|---|
|  | 191 | DDrConsole_PrintF("Server started at %s...", inet_ntoa (*(struct in_addr *)*host_entry->h_addr_list)); | 
|---|
|  | 192 | return NetUDPServer_Listen(27777, FLrServer_PacketCallback); | 
|---|
|  | 193 | } | 
|---|
|  | 194 |  | 
|---|
|  | 195 | RGBA green = {0, 0xFF, 0, 0}; | 
|---|
|  | 196 | RGBA red = {0, 0, 0xFF, 0}; | 
|---|
|  | 197 | RGBA grey = {0x80,0x80,0x80,0x80}; | 
|---|
|  | 198 |  | 
|---|
|  | 199 | //FLrClient_Run | 
|---|
|  | 200 | //Looping function that waits for packets from the server. | 
|---|
|  | 201 | //TODO: Convert connection packet stuff to TCP | 
|---|
|  | 202 | int client_slot = 0; | 
|---|
|  | 203 | bool FLrClient_Run(flatline_packet* packet) | 
|---|
|  | 204 | { | 
|---|
|  | 205 |  | 
|---|
|  | 206 | char data[1400]; | 
|---|
|  | 207 | uint16_t len; | 
|---|
|  | 208 | int j; | 
|---|
|  | 209 | int sent_bytes; | 
|---|
|  | 210 | client_connected = 0; | 
|---|
|  | 211 |  | 
|---|
|  | 212 |  | 
|---|
|  | 213 | //starts the connection | 
|---|
|  | 214 | DDrConsole_PrintF("Connecting to server %s on socket %i",  inet_ntoa(address.sin_addr), client_sock); | 
|---|
|  | 215 | sent_bytes = NetUDPSocket_Send(client_sock, (sockaddr*)&address, (char*)packet, 255); | 
|---|
|  | 216 | if(sent_bytes == SOCKET_ERROR) { | 
|---|
|  | 217 | NetCatchError(); | 
|---|
|  | 218 | } | 
|---|
|  | 219 | //loops once per second waiting for a reply. | 
|---|
|  | 220 | for(j = 0; j < CONNECTION_TIMEOUT; j++) { | 
|---|
|  | 221 | while(NetUDPSocket_Recieve(client_sock, (sockaddr_storage *) &client_address, data, &len)){ | 
|---|
|  | 222 | packet = (flatline_packet*)data; | 
|---|
|  | 223 | if(packet->id == CONNECT_REPLY) { | 
|---|
|  | 224 | if(packet->connect_reply.goodtogo){ | 
|---|
|  | 225 | client_connected = 1; | 
|---|
|  | 226 |  | 
|---|
|  | 227 | client_slot = ((connect_reply*)packet->data)->player_slot; | 
|---|
|  | 228 |  | 
|---|
|  | 229 | PlayerList[client_slot] = Players+client_slot; | 
|---|
|  | 230 | PlayerList[client_slot]->Chr = ONgGameState->PlayerCharacter; | 
|---|
|  | 231 |  | 
|---|
|  | 232 |  | 
|---|
|  | 233 |  | 
|---|
|  | 234 | DDrConsole_PrintColored("Connection successful!",0,green, grey); | 
|---|
|  | 235 | //DDrConsole_PrintF("Slot %i",  ((connect_reply*)packet)->player_slot); | 
|---|
|  | 236 | break; | 
|---|
|  | 237 | } | 
|---|
|  | 238 | else { | 
|---|
|  | 239 | DDrConsole_PrintF("Connection rejected: %s", ((connect_reply*)packet->data)->message); | 
|---|
|  | 240 | return false; | 
|---|
|  | 241 | break; | 
|---|
|  | 242 | } | 
|---|
|  | 243 | } | 
|---|
|  | 244 | } | 
|---|
|  | 245 | if(client_connected) break; | 
|---|
|  | 246 | DDrConsole_PrintF("Connection timing out in %i seconds...", CONNECTION_TIMEOUT - j); | 
|---|
|  | 247 | Sleep(1000); | 
|---|
|  | 248 | } | 
|---|
|  | 249 | //the client timed out without recieving an error message. | 
|---|
|  | 250 | if(!client_connected) { | 
|---|
|  | 251 | DDrConsole_PrintColored("Connection timed out.",0,red, grey); | 
|---|
|  | 252 | return false; | 
|---|
|  | 253 | } | 
|---|
|  | 254 | else | 
|---|
|  | 255 | { | 
|---|
|  | 256 | ActiveCharacter * Active; | 
|---|
|  | 257 | flatline_packet packet; | 
|---|
|  | 258 | //#define SPAM_INPUT | 
|---|
|  | 259 | #ifdef SPAM_INPUT | 
|---|
|  | 260 | struct timeval lasttime; | 
|---|
|  | 261 | struct timeval thistime; | 
|---|
|  | 262 | gettimeofday(&lasttime, 0); | 
|---|
|  | 263 | #endif | 
|---|
|  | 264 | while(1) { | 
|---|
|  | 265 | #ifdef SPAM_INPUT | 
|---|
|  | 266 | gettimeofday(&thistime, 0); | 
|---|
|  | 267 |  | 
|---|
|  | 268 | //DDrConsole_PrintF("%i.%i | %i.%i | %i.%i",lasttime.tv_sec, lasttime.tv_usec, thistime.tv_sec, thistime.tv_usec, | 
|---|
|  | 269 | //      thistime.tv_sec - lasttime.tv_sec, thistime.tv_usec - lasttime.tv_usec); | 
|---|
|  | 270 | //checks to see if enough time has passed since the last input update (by default once every 10ms) | 
|---|
|  | 271 | if( | 
|---|
|  | 272 | ((thistime.tv_sec > lasttime.tv_sec) && ((thistime.tv_usec + 1000000 - lasttime.tv_usec ) > update_rate * 1000) ) | 
|---|
|  | 273 | || ((thistime.tv_sec == lasttime.tv_sec) && ((thistime.tv_usec - lasttime.tv_usec ) > update_rate * 1000)) | 
|---|
|  | 274 | ) { | 
|---|
|  | 275 |  | 
|---|
|  | 276 | flatline_packet input_packet; | 
|---|
|  | 277 | lasttime.tv_usec = ++thistime.tv_usec; //in case recieving packets takes less than 1 ms. | 
|---|
|  | 278 |  | 
|---|
|  | 279 | input_packet.id = PLAYER_INPUT; | 
|---|
|  | 280 |  | 
|---|
|  | 281 | if( ((GameState*)(ONgGameState))->Input.Current.Actions1 != last1 || ((GameState*)(ONgGameState))->Input.Current.Actions2 != last2) { | 
|---|
|  | 282 | last1 =((GameState*)(ONgGameState))->Input.Current.Actions1; | 
|---|
|  | 283 | last2 =((GameState*)(ONgGameState))->Input.Current.Actions2; | 
|---|
|  | 284 | } | 
|---|
|  | 285 |  | 
|---|
|  | 286 | ((input_struct*)(void*)(input_packet.data))->Actions1 = ((GameState*)(ONgGameState))->Input.Current.Actions1; | 
|---|
|  | 287 | ((input_struct*)(void*)(input_packet.data))->Actions2 = ((GameState*)(ONgGameState))->Input.Current.Actions2; | 
|---|
|  | 288 | ((input_struct*)(void*)(input_packet.data))->MouseDeltaX = ((GameState*)(ONgGameState))->Input.MouseDeltaX; | 
|---|
|  | 289 | ((input_struct*)(void*)(input_packet.data))->MouseDeltaY = ((GameState*)(ONgGameState))->Input.MouseDeltaY; | 
|---|
|  | 290 | sent_bytes = NetUDPSocket_Send(client_sock,(sockaddr *) &address, (char*)&input_packet, sizeof(input_struct) + FLATLINE_HEADER); | 
|---|
|  | 291 | //if(sent_bytes == SOCKET_ERROR)        NetCatchError(); | 
|---|
|  | 292 | } | 
|---|
|  | 293 | #endif | 
|---|
|  | 294 | if(NetUDPSocket_Recieve(client_sock, (sockaddr_storage *) &client_address, &packet, &len)) { | 
|---|
|  | 295 | //packet = (flatline_packet*)data; | 
|---|
|  | 296 | //DDrConsole_PrintF("Data recieved, length %i, type %i", len, ((flatline_packet*)data)->id); | 
|---|
|  | 297 | switch(packet.id) { | 
|---|
|  | 298 | case MESSAGE: | 
|---|
|  | 299 | COrMessage_Print(packet.data, "chat", 0); | 
|---|
|  | 300 | break; | 
|---|
|  | 301 | case CONNECT_SEND: | 
|---|
|  | 302 | ;if(1) { | 
|---|
|  | 303 | flatline_packet connect_recv; | 
|---|
|  | 304 | memcpy(&connect_recv.connect_reply.message,"This isn't a server!", sizeof("This isn't a server!")); | 
|---|
|  | 305 | NetUDPSocket_Send(client_sock, (sockaddr *) &address, (char*)&connect_recv, sizeof(bool) + FLATLINE_HEADER + sizeof("This isn't a server!")); | 
|---|
|  | 306 | } | 
|---|
|  | 307 | case CONNECT_REPLY: | 
|---|
|  | 308 | break; //extra packet or something. | 
|---|
|  | 309 | case NEW_PLAYER: | 
|---|
|  | 310 | ;if(1) { //haxhaxhax | 
|---|
|  | 311 | CharacterObject* Char = &(packet.new_player.Character); | 
|---|
|  | 312 | uint32_t chr_index = 0; | 
|---|
|  | 313 | Character* PC; | 
|---|
|  | 314 | DDrConsole_PrintF("%i |  %i", packet.new_player.Playernumber ,client_slot); | 
|---|
|  | 315 | //Char->OSD.Options = 0; | 
|---|
|  | 316 | if(packet.new_player.Playernumber == client_slot) { | 
|---|
|  | 317 | PlayerList[packet.new_player.Playernumber] = &Players[0]; | 
|---|
|  | 318 | PC = (ONgGameState->PlayerCharacter); | 
|---|
|  | 319 | Players[0].Chr = PC; | 
|---|
|  | 320 |  | 
|---|
|  | 321 | } | 
|---|
|  | 322 | else { | 
|---|
|  | 323 | ONrGameState_NewCharacter(Char, NULL, NULL, &chr_index); | 
|---|
|  | 324 | PlayerList[packet.new_player.Playernumber] = &Players[chr_index]; | 
|---|
|  | 325 | Players[chr_index].Chr = &(ONgGameState->CharacterStorage[chr_index]); | 
|---|
|  | 326 | Players[chr_index].Chr->Flags &= 0xFFBFFFFF; | 
|---|
|  | 327 | DDrConsole_PrintF("Spawning player %s, class %s, slot  %i", ((new_player*)(packet.data))->Character.OSD.Name, ((new_player*)(packet.data))->Character.OSD.Class,chr_index) ; | 
|---|
|  | 328 | } | 
|---|
|  | 329 | //Players[((new_player*)(packet.data))->Playernumber].spawnnumber = ONrGameState_NewCharacter(&(((new_player*)(packet.data))->Character), NULL, NULL, 0); | 
|---|
|  | 330 | break; | 
|---|
|  | 331 | } | 
|---|
|  | 332 | case PLAYER_DATA: | 
|---|
|  | 333 | if(1) { //haxhaxhax | 
|---|
|  | 334 | player_data* data = &packet.player_data; | 
|---|
|  | 335 | uint16_t i = data->PlayerNum; | 
|---|
|  | 336 | GameInput * Active_Input; | 
|---|
|  | 337 | data = (void*)packet.data; | 
|---|
|  | 338 |  | 
|---|
|  | 339 | //DDrConsole_PrintF("Got data for Player %i, %x", i, PlayerList[i]); | 
|---|
|  | 340 | if (i > max_connections) break; | 
|---|
|  | 341 | if( !PlayerList[i] ) break; | 
|---|
|  | 342 | //PlayerList[i]->Chr = ((GameState *)ONgGameState)->CharacterStorage; | 
|---|
|  | 343 |  | 
|---|
|  | 344 | PlayerList[i]->Chr->Health = data->Health; | 
|---|
|  | 345 | PlayerList[i]->Chr->MaxHealth = data->MaxHealth; | 
|---|
|  | 346 | PlayerList[i]->Chr->Position = data->Position; | 
|---|
|  | 347 | PlayerList[i]->Chr->Location = data->Location; | 
|---|
|  | 348 | PlayerList[i]->Chr->LastPosition = data->LastPosition; | 
|---|
|  | 349 | PlayerList[i]->Chr->Facing = data->Facing; | 
|---|
|  | 350 | PlayerList[i]->Chr->DesiredFacing = data->DesiredFacing; | 
|---|
|  | 351 | PlayerList[i]->Chr->CosmeticFacing = data->CosmeticFacing; | 
|---|
|  | 352 | PlayerList[i]->Actions1 = data->Inputs.Actions1; | 
|---|
|  | 353 | PlayerList[i]->Actions2 = data->Inputs.Actions2; | 
|---|
|  | 354 | PlayerList[i]->MouseDeltaX = data->Inputs.MouseDeltaX; | 
|---|
|  | 355 | PlayerList[i]->MouseDeltaY = data->Inputs.MouseDeltaY; | 
|---|
|  | 356 |  | 
|---|
|  | 357 | Active = ((ActiveCharacter*)(ONrGetActiveCharacter(PlayerList[i]->Chr))); | 
|---|
|  | 358 | if(!Active) break; | 
|---|
|  | 359 |  | 
|---|
|  | 360 | Active->PhyContext->Position = data->Position; | 
|---|
|  | 361 |  | 
|---|
|  | 362 | #if 0 | 
|---|
|  | 363 | TMrInstance_GetDataPtr('TRAM',data->Animation,Active->Animation); | 
|---|
|  | 364 | Active->AnimationFromState      = data->AnimationFromState; | 
|---|
|  | 365 | Active->AnimationToState        = data->AnimationToState; | 
|---|
|  | 366 | Active->AnimationType = data->AnimationType; | 
|---|
|  | 367 | Active->NextAnimationType = data->NextAnimationType; | 
|---|
|  | 368 | Active->Frame = data->Frame; | 
|---|
|  | 369 | Active->InterpolationCurrentFrame = data->InterpolationCurrentFrame; | 
|---|
|  | 370 | Active->InterpolationFromState = data->InterpolationFromState; | 
|---|
|  | 371 | Active->InterpolationLength = data->InterpolationLength; | 
|---|
|  | 372 | memcpy(Active->InterpolationStartRotations, data->InterpolationStartRotations, | 
|---|
|  | 373 | sizeof(Quaternion) * 19); | 
|---|
|  | 374 | memcpy(Active->BoneMatrices, data->BoneMatrices, | 
|---|
|  | 375 | sizeof(Quaternion) * 19); | 
|---|
|  | 376 | Active->Stitch = data->Stitch; | 
|---|
|  | 377 | Active->StitchHeight = data->StitchHeight; | 
|---|
|  | 378 | Active->StitchVelocity = data->StitchVelocity; | 
|---|
|  | 379 |  | 
|---|
|  | 380 | Active->Overlay = data->Overlay; | 
|---|
|  | 381 | memcpy(Active->OverlayRotations, data->OverlayRotations, | 
|---|
|  | 382 | sizeof(Quaternion) * 19); | 
|---|
|  | 383 | #endif | 
|---|
|  | 384 | //Active->animationtype2 = data->AnimationType2; | 
|---|
|  | 385 | /* | 
|---|
|  | 386 | if((int)*((char*)(Active + 0x1AF8) + 0x166) > data->Frame + 1) | 
|---|
|  | 387 | *(uint16_t *)((char*)Active + 0x1C88) = data->Frame + 1; | 
|---|
|  | 388 | */ | 
|---|
|  | 389 | break; | 
|---|
|  | 390 | } | 
|---|
|  | 391 | default: | 
|---|
|  | 392 | DDrConsole_PrintF("Warning, recieved badly formed packet!"); | 
|---|
|  | 393 | break; | 
|---|
|  | 394 | } | 
|---|
|  | 395 | } | 
|---|
|  | 396 | else { | 
|---|
|  | 397 | Sleep(1); | 
|---|
|  | 398 | } | 
|---|
|  | 399 | } | 
|---|
|  | 400 | } | 
|---|
|  | 401 | return true; | 
|---|
|  | 402 | } | 
|---|
|  | 403 |  | 
|---|
|  | 404 | //UDPServer_SendToAll | 
|---|
|  | 405 | //Sends a packet to all the clients currently connected. | 
|---|
|  | 406 | //Returns the number of players sent to. | 
|---|
|  | 407 | int UDPServer_SendToAll(void* packet, int size) { | 
|---|
|  | 408 | int j; | 
|---|
|  | 409 | int players = 0; | 
|---|
|  | 410 | sockaddr_in address; | 
|---|
|  | 411 | memset(&address, 0, sizeof(sockaddr_in)); | 
|---|
|  | 412 | address.sin_family = AF_INET; | 
|---|
|  | 413 | address.sin_addr.s_addr = htonl(INADDR_ANY); | 
|---|
|  | 414 | address.sin_port = htons(27777); | 
|---|
|  | 415 | for(j = 0; j < max_connections; j++) { | 
|---|
|  | 416 | if (PlayerList[j] != 0 && PlayerList[j]->ip && (PlayerList[j]->ip != inet_addr("127.0.0.1"))) { | 
|---|
|  | 417 | int sent_bytes; | 
|---|
|  | 418 | address.sin_addr.s_addr = htonl(PlayerList[j]->ip);//*((struct in_addr*)(int*)&(Players[j].ip)); | 
|---|
|  | 419 | sent_bytes = NetUDPServer_Send((sockaddr *) &address, (char*)packet, size); | 
|---|
|  | 420 | if(sent_bytes == SOCKET_ERROR) NetCatchError(); | 
|---|
|  | 421 | else players++; | 
|---|
|  | 422 | } | 
|---|
|  | 423 | } | 
|---|
|  | 424 | return players; | 
|---|
|  | 425 | } | 
|---|
|  | 426 |  | 
|---|
|  | 427 | player_info *FLr_FindEmptySlot() { | 
|---|
|  | 428 | int j; | 
|---|
|  | 429 | for(j = 0; j < MAX_PLAYERS; j++) { | 
|---|
|  | 430 | if (Players[j].ip == 0) { | 
|---|
|  | 431 | return &Players[j]; | 
|---|
|  | 432 | } | 
|---|
|  | 433 | } | 
|---|
|  | 434 | return 0; | 
|---|
|  | 435 | } | 
|---|
|  | 436 |  | 
|---|
|  | 437 | extern uint16_t max_connections; | 
|---|
|  | 438 | uint16_t FLr_FindEmptyListSlot() { | 
|---|
|  | 439 | int j; | 
|---|
|  | 440 | for(j = 0; j < max_connections; j++) { | 
|---|
|  | 441 | if (PlayerList[j] == 0) { | 
|---|
|  | 442 | return j; | 
|---|
|  | 443 | } | 
|---|
|  | 444 | } | 
|---|
|  | 445 | return -1; | 
|---|
|  | 446 | } | 
|---|
|  | 447 |  | 
|---|
|  | 448 | static flatline_packet cache_input = {0}; | 
|---|
|  | 449 | extern void* TSrTest; | 
|---|
|  | 450 | void * ONICALL FLrInput_Update_Keys(void) | 
|---|
|  | 451 | { | 
|---|
|  | 452 | uint16_t i; | 
|---|
|  | 453 | #ifndef SPAM_INPUT | 
|---|
|  | 454 | if(client_connected) | 
|---|
|  | 455 | { | 
|---|
|  | 456 | int sent_bytes; | 
|---|
|  | 457 | flatline_packet input_packet = {0}; | 
|---|
|  | 458 | input_packet.id = PLAYER_INPUT; | 
|---|
|  | 459 |  | 
|---|
|  | 460 | input_packet.input_struct.Actions1 = ((GameState*)(ONgGameState))->Input.Current.Actions1; | 
|---|
|  | 461 | input_packet.input_struct.Actions2 = ((GameState*)(ONgGameState))->Input.Current.Actions2; | 
|---|
|  | 462 | input_packet.input_struct.MouseDeltaX = ((GameState*)(ONgGameState))->Input.MouseDeltaX; | 
|---|
|  | 463 | input_packet.input_struct.MouseDeltaY = ((GameState*)(ONgGameState))->Input.MouseDeltaY; | 
|---|
|  | 464 |  | 
|---|
|  | 465 | sent_bytes = NetUDPSocket_Send(client_sock,(sockaddr *) &address, (char*)&input_packet, sizeof(input_struct) + FLATLINE_HEADER); | 
|---|
|  | 466 |  | 
|---|
|  | 467 | //return ONgGameState; | 
|---|
|  | 468 | } | 
|---|
|  | 469 | #endif | 
|---|
|  | 470 | //Testing drawing text to the screen...this failed. Will need to get it working eventually. | 
|---|
|  | 471 | if( TSrTest ) | 
|---|
|  | 472 | { | 
|---|
|  | 473 | OniRectangle TextRect = { 20, 20, 50, 50 }; | 
|---|
|  | 474 | TSrContext_DrawText(TSrTest, "Testing woohoo", 0xFF, 0, &TextRect); | 
|---|
|  | 475 | } | 
|---|
|  | 476 | if(!(server_started || client_connected)) return ONgGameState; | 
|---|
|  | 477 |  | 
|---|
|  | 478 | for(i = 0; i < max_connections; i++) { | 
|---|
|  | 479 | ActiveCharacter * Active_Player; | 
|---|
|  | 480 | GameInput * Active_Input; | 
|---|
|  | 481 | if(PlayerList[i] == 0) continue; | 
|---|
|  | 482 |  | 
|---|
|  | 483 | Active_Player = (void*)ONrGetActiveCharacter(PlayerList[i]->Chr); | 
|---|
|  | 484 |  | 
|---|
|  | 485 | if(Active_Player == 0) continue; | 
|---|
|  | 486 | Active_Input = &(Active_Player->Input); | 
|---|
|  | 487 | if(server_started) { | 
|---|
|  | 488 | player_data * data; | 
|---|
|  | 489 | flatline_packet data_out = {0}; | 
|---|
|  | 490 | data_out.id = PLAYER_DATA; | 
|---|
|  | 491 | data = (void*)&(data_out.data); | 
|---|
|  | 492 | data->PlayerNum = i; | 
|---|
|  | 493 | data->Health = PlayerList[i]->Chr->Health; | 
|---|
|  | 494 | data->MaxHealth = PlayerList[i]->Chr->MaxHealth; | 
|---|
|  | 495 | data->Position = PlayerList[i]->Chr->Position; | 
|---|
|  | 496 | data->Location = PlayerList[i]->Chr->Location; | 
|---|
|  | 497 | data->LastPosition = PlayerList[i]->Chr->LastPosition; | 
|---|
|  | 498 | data->Facing = PlayerList[i]->Chr->Facing; | 
|---|
|  | 499 | data->DesiredFacing = PlayerList[i]->Chr->DesiredFacing; | 
|---|
|  | 500 | data->CosmeticFacing = PlayerList[i]->Chr->CosmeticFacing; | 
|---|
|  | 501 |  | 
|---|
|  | 502 | #if 0 | 
|---|
|  | 503 | data->Frame = Active_Player->Frame; | 
|---|
|  | 504 | memcpy(data->Animation, TMrInstance_GetInstanceName(Active_Player->Animation), 32); | 
|---|
|  | 505 | data->AnimationToState = Active_Player->AnimationToState; | 
|---|
|  | 506 | data->AnimationFromState = Active_Player->AnimationFromState; | 
|---|
|  | 507 | data->AnimationType = Active_Player->AnimationType; | 
|---|
|  | 508 | data->NextAnimationType = Active_Player->NextAnimationType; | 
|---|
|  | 509 | data->InterpolationCurrentFrame = Active_Player->InterpolationCurrentFrame; | 
|---|
|  | 510 | data->InterpolationFromState = Active_Player->InterpolationFromState; | 
|---|
|  | 511 | data->InterpolationLength = Active_Player->InterpolationLength; | 
|---|
|  | 512 | memcpy(data->InterpolationStartRotations, Active_Player->InterpolationStartRotations, | 
|---|
|  | 513 | sizeof(Quaternion) * 19); | 
|---|
|  | 514 | //                      data->InterpolationStartRotations = Active_Player->InterpolationStartRotations; | 
|---|
|  | 515 | memcpy(data->BoneMatrices, Active_Player->BoneMatrices, | 
|---|
|  | 516 | sizeof(Quaternion) * 19); | 
|---|
|  | 517 | data->Stitch = Active_Player->Stitch; | 
|---|
|  | 518 | data->StitchHeight = Active_Player->StitchHeight; | 
|---|
|  | 519 | data->StitchVelocity = Active_Player->StitchVelocity; | 
|---|
|  | 520 |  | 
|---|
|  | 521 | data->Overlay = Active_Player->Overlay; | 
|---|
|  | 522 | memcpy( data->OverlayRotations, Active_Player->OverlayRotations | 
|---|
|  | 523 | ,sizeof(Quaternion) * 19); | 
|---|
|  | 524 | #endif | 
|---|
|  | 525 | if(i == 0) { | 
|---|
|  | 526 | data->Inputs.Actions1 = ((GameState*)(ONgGameState))->Input.Current.Actions1; | 
|---|
|  | 527 | data->Inputs.Actions2 = ((GameState*)(ONgGameState))->Input.Current.Actions2; | 
|---|
|  | 528 | } | 
|---|
|  | 529 | else{ | 
|---|
|  | 530 | data->Inputs.Actions1 = PlayerList[i]->Actions1; | 
|---|
|  | 531 | data->Inputs.Actions2 = PlayerList[i]->Actions2; | 
|---|
|  | 532 | } | 
|---|
|  | 533 | UDPServer_SendToAll(&data_out, sizeof(player_data) + FLATLINE_HEADER); | 
|---|
|  | 534 | } | 
|---|
|  | 535 |  | 
|---|
|  | 536 | if( (server_started && i !=0) || (!server_started && i != client_slot)) { | 
|---|
|  | 537 | Active_Input->Stop.Actions1 = ~PlayerList[i]->Actions1 & Active_Input->Current.Actions1; | 
|---|
|  | 538 | Active_Input->Stop.Actions2 = ~PlayerList[i]->Actions2 & Active_Input->Current.Actions2; | 
|---|
|  | 539 | Active_Input->Start.Actions1 = ~Active_Input->Current.Actions1 & PlayerList[i]->Actions1; | 
|---|
|  | 540 | Active_Input->Start.Actions2 = ~Active_Input->Current.Actions2 & PlayerList[i]->Actions2; | 
|---|
|  | 541 | Active_Input->Current.Actions1 = PlayerList[i]->Actions1; | 
|---|
|  | 542 | Active_Input->Current.Actions2 = PlayerList[i]->Actions2; | 
|---|
|  | 543 | Active_Input->Stopped.Actions1 = ~Active_Input->Current.Actions1; | 
|---|
|  | 544 | Active_Input->Stopped.Actions2 = ~Active_Input->Current.Actions2; | 
|---|
|  | 545 | Active_Input->MouseDeltaX = PlayerList[i]->MouseDeltaX; | 
|---|
|  | 546 | Active_Input->MouseDeltaY = PlayerList[i]->MouseDeltaY; | 
|---|
|  | 547 | } | 
|---|
|  | 548 | } | 
|---|
|  | 549 | return ONgGameState; | 
|---|
|  | 550 | } | 
|---|