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 | #define BETTER_SYNC
|
---|
18 |
|
---|
19 | void DoRareSync( short Player, sockaddr_in * sender )
|
---|
20 | {
|
---|
21 | flatline_packet sync = {0};
|
---|
22 |
|
---|
23 | if (Player > max_connections || !PlayerList[ Player ] ) return;
|
---|
24 |
|
---|
25 |
|
---|
26 | // DDrConsole_PrintF( "Sending sync data for player %i, new index %u", Player, PlayerList[ Player ]->rare_sync_index);
|
---|
27 | sender->sin_addr.S_un.S_addr = htonl(sender->sin_addr.S_un.S_addr);
|
---|
28 | sync.id = RARE_SYNC_DATA;
|
---|
29 | sprintf( sync.rare_sync_data.Class, TMrInstance_GetInstanceName( PlayerList[ Player ]->Chr->ONCC ) );
|
---|
30 | //using ->Inventory instead of ->Chr->Inventory to keep the index and Inventory in sync, just in case.
|
---|
31 | memcpy( &(sync.rare_sync_data.Inventory), &(PlayerList[ Player ]->Inventory), sizeof(Inventory) );
|
---|
32 | //WEAPONS ARE DISABLED. Why? Pain in the arse to sync.
|
---|
33 | sync.rare_sync_data.Inventory.Weapons[0] = NULL;
|
---|
34 | sync.rare_sync_data.Inventory.Weapons[1] = NULL;
|
---|
35 | sync.rare_sync_data.Inventory.Weapons[2] = NULL;
|
---|
36 | sync.rare_sync_data.PlayerNum = Player;
|
---|
37 | sync.rare_sync_data.index = PlayerList[ Player ]->rare_sync_index;
|
---|
38 | NetTCPServer_Send( sender, (char*)&sync, sizeof(rare_sync_data) + FLATLINE_HEADER );
|
---|
39 | }
|
---|
40 |
|
---|
41 | bool FLrServer_PacketCallback(char* data, int datalen, int from)
|
---|
42 | {
|
---|
43 | int i, j;
|
---|
44 | bool found_player = 0;
|
---|
45 | flatline_packet * packet = (flatline_packet*)data;
|
---|
46 | static int recieved = 0;
|
---|
47 | sockaddr_in sender;
|
---|
48 | sender.sin_family = AF_INET;
|
---|
49 | sender.sin_port = htons(27777);
|
---|
50 | sender.sin_addr = *((struct in_addr*)(int*)&from);
|
---|
51 |
|
---|
52 |
|
---|
53 | //packet->data[datalen] = '\0';
|
---|
54 |
|
---|
55 | //DDrConsole_PrintF("Packet \r%d recieved from %i", ++recieved, from);
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 | //if data[0] != CONNECT_SEND, search in playerlist for ip address
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 | switch(packet->id) {
|
---|
65 | flatline_packet connect_recv;
|
---|
66 | player_info * playah;
|
---|
67 | //rewrite this when we get TCP support.
|
---|
68 | //rewrite this before we get TCP support*
|
---|
69 | //the way of seeing if there is room for players sucks.
|
---|
70 | case CONNECT_SEND:
|
---|
71 | ;
|
---|
72 |
|
---|
73 | connect_recv.id = CONNECT_REPLY;
|
---|
74 |
|
---|
75 | //if(Players[i].ip == sender.sin_addr.S_un.S_addr) break; //needs to send an error message
|
---|
76 | sender.sin_addr.S_un.S_addr = htonl(sender.sin_addr.S_un.S_addr);
|
---|
77 | playah = FLrServer_AddPlayer(from,packet->connect_send.name, 0);
|
---|
78 | DDrConsole_PrintF("%s connected from %s", packet->connect_send.name, inet_ntoa(sender.sin_addr ) );
|
---|
79 | if(!((int)playah > -5 && (int)playah <= 0)) {
|
---|
80 | flatline_packet new_char = {0};
|
---|
81 | CharacterObject* Char;
|
---|
82 | connect_recv.connect_reply.goodtogo = 1;
|
---|
83 | connect_recv.connect_reply.player_slot = playah->list_slot;
|
---|
84 | DDrConsole_PrintF("Slot: %i", playah->list_slot);
|
---|
85 |
|
---|
86 | //sending this several times to make sure it gets through. Really need to make up some form of packet tracking.
|
---|
87 | NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER);
|
---|
88 | NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER);
|
---|
89 | NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER);
|
---|
90 | NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER);
|
---|
91 | NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER);
|
---|
92 | Sleep(100);
|
---|
93 |
|
---|
94 | new_char.id = NEW_PLAYER;
|
---|
95 | Char = &new_char.new_player.Character;
|
---|
96 | memset(Char, 0, sizeof(CharacterObject));
|
---|
97 | Char->Header.Type = 'CHAR';
|
---|
98 | Char->OSD.Options = chr_dontaim;
|
---|
99 | for(j = 0; j < max_connections; j++) {
|
---|
100 | if(PlayerList[j] != 0) {
|
---|
101 | new_char.new_player.Playernumber = j;
|
---|
102 | sprintf(Char->OSD.Name,"%s",PlayerList[j]->name);
|
---|
103 |
|
---|
104 | sprintf(Char->OSD.Class, "%s", TMrInstance_GetInstanceName(PlayerList[j]->Chr->ONCC));
|
---|
105 | DDrConsole_PrintF("Class %s", Char->OSD.Class );
|
---|
106 |
|
---|
107 | sprintf(Char->OSD.Class, "konoko_generic");
|
---|
108 | NetTCPServer_Send((sockaddr *) &sender, (char*)&new_char, sizeof(new_player) + FLATLINE_HEADER );
|
---|
109 | }
|
---|
110 |
|
---|
111 | }
|
---|
112 | }
|
---|
113 | else {
|
---|
114 | //fix the error messages...
|
---|
115 | DDrConsole_PrintF("Server is full. :(");
|
---|
116 | connect_recv.connect_reply.goodtogo = 0;
|
---|
117 | sender.sin_addr.S_un.S_addr = htonl(sender.sin_addr.S_un.S_addr);
|
---|
118 | memcpy(&connect_recv.connect_reply.message,"Server is full.", sizeof("Server is full."));
|
---|
119 | NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(bool)*2 + FLATLINE_HEADER + sizeof("Server is full."));
|
---|
120 |
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | break;
|
---|
125 | case CONNECT_REPLY:
|
---|
126 | break; //do nothing...a server shouldn't recieve this type of packet.
|
---|
127 | case MESSAGE:
|
---|
128 | for(i = 0; i < MAX_PLAYERS; i++) {
|
---|
129 | //DDrConsole_PrintF("%i : %i | %s : %s", from, Players[i].ip, inet_ntoa(*(struct in_addr*)&from), inet_ntoa(*(struct in_addr*)&(Players[i].ip)));
|
---|
130 | if(Players[i].ip == sender.sin_addr.S_un.S_addr) {
|
---|
131 | found_player = 1;
|
---|
132 | break;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | if(found_player == 0) return true;
|
---|
136 | else {
|
---|
137 | char message_buffer[512] = {0};
|
---|
138 | flatline_packet message;
|
---|
139 | int message_size;
|
---|
140 | data[datalen] = 0;
|
---|
141 |
|
---|
142 | DDrConsole_PrintF("%s: %s", Players[i].name, packet->data);
|
---|
143 | sprintf(message_buffer, "%s: %s", Players[i].name, packet->data);
|
---|
144 |
|
---|
145 | message.id = MESSAGE;
|
---|
146 | message_size = sprintf(message.data, "%s", message_buffer);
|
---|
147 | COrMessage_Print(message_buffer, "chat", 0);
|
---|
148 | UDPServer_SendToAll(&message, message_size + 1 + FLATLINE_HEADER);
|
---|
149 | break;
|
---|
150 | }
|
---|
151 | case CHANGE_NAME:
|
---|
152 | ; //wtf, needed or i get an error.
|
---|
153 | DDrConsole_PrintF("Changing Name to: %s", packet->data);
|
---|
154 | for(i = 0; i < MAX_PLAYERS; i++) {
|
---|
155 | if(Players[i].ip == sender.sin_addr.S_un.S_addr) {
|
---|
156 | found_player = 1;
|
---|
157 | break;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | if(found_player == 0) return true;
|
---|
161 | else {
|
---|
162 | bool name_exists = 0;
|
---|
163 | for(j = 0; j < MAX_PLAYERS; j++) {
|
---|
164 | if(!strcmp(packet->data, Players[j].name)) {
|
---|
165 | name_exists = 1;
|
---|
166 | break;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | if(!name_exists) {
|
---|
170 | char message_buffer[1024];
|
---|
171 | sprintf(message_buffer,"%s changed their name to %s", Players[i].name, packet->data);
|
---|
172 | COrMessage_Print(message_buffer, "name_change", 0);
|
---|
173 | memcpy(Players[i].name, packet->data, 256);
|
---|
174 |
|
---|
175 | }
|
---|
176 | break;
|
---|
177 | }
|
---|
178 | case PLAYER_INPUT:
|
---|
179 |
|
---|
180 | for(i = 0; i < max_connections; i++) {
|
---|
181 | if(PlayerList[i] != 0 && PlayerList[i]->ip == sender.sin_addr.S_un.S_addr) {
|
---|
182 | found_player = 1;
|
---|
183 | break;
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | if(found_player == 0) return true;
|
---|
188 | else {
|
---|
189 | input_struct * packet_input = &packet->input_struct;
|
---|
190 |
|
---|
191 |
|
---|
192 | PlayerList[i]->Actions1 = packet_input->Actions1;
|
---|
193 | PlayerList[i]->Actions2 = packet_input->Actions2;
|
---|
194 | PlayerList[i]->MouseDeltaX = packet_input->MouseDeltaX;
|
---|
195 | PlayerList[i]->MouseDeltaY = packet_input->MouseDeltaY;
|
---|
196 | PlayerList[i]->LastInputTime = packet_input->Time;
|
---|
197 |
|
---|
198 | break;
|
---|
199 | }
|
---|
200 | case RARE_SYNC_DATA_REQUEST:
|
---|
201 | DoRareSync( packet->sync_request , &sender);
|
---|
202 | break;
|
---|
203 | default:
|
---|
204 | DDrConsole_PrintF("Warning, recieved badly formed packet!");
|
---|
205 | break;
|
---|
206 | }
|
---|
207 | return true;
|
---|
208 | }
|
---|
209 |
|
---|
210 | bool FLrServer_Run()
|
---|
211 | {
|
---|
212 | // Get the local hostname
|
---|
213 | char szHostName[255];
|
---|
214 | struct hostent *host_entry;
|
---|
215 | gethostname(szHostName, 255);
|
---|
216 |
|
---|
217 | host_entry=gethostbyname(szHostName);
|
---|
218 | DDrConsole_PrintF("Server started at %s...", inet_ntoa (*(struct in_addr *)*host_entry->h_addr_list));
|
---|
219 | return NetUDPServer_Listen(27777, FLrServer_PacketCallback);
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | RGBA green = {0, 0xFF, 0, 0};
|
---|
224 | RGBA red = {0, 0, 0xFF, 0};
|
---|
225 | RGBA grey = {0x80,0x80,0x80,0x80};
|
---|
226 |
|
---|
227 | //FLrClient_Run
|
---|
228 | //Looping function that waits for packets from the server.
|
---|
229 | //TODO: Convert connection packet stuff to TCP
|
---|
230 | int client_slot = 0;
|
---|
231 | bool FLrClient_Run(flatline_packet* packet)
|
---|
232 | {
|
---|
233 |
|
---|
234 | char data[1400];
|
---|
235 | uint16_t len;
|
---|
236 | int j;
|
---|
237 | int sent_bytes;
|
---|
238 | client_connected = 0;
|
---|
239 |
|
---|
240 |
|
---|
241 | //starts the connection
|
---|
242 | DDrConsole_PrintF("Connecting to server %s on socket %i", inet_ntoa(address.sin_addr), client_sock);
|
---|
243 | sent_bytes = NetUDPSocket_Send(client_sock, (sockaddr*)&address, (char*)packet, 255);
|
---|
244 | if(sent_bytes == SOCKET_ERROR) {
|
---|
245 | NetCatchError();
|
---|
246 | }
|
---|
247 | //loops once per second waiting for a reply.
|
---|
248 | for(j = 0; j < CONNECTION_TIMEOUT; j++) {
|
---|
249 | while(NetUDPSocket_Recieve(client_sock, (sockaddr_storage *) &client_address, data, &len)){
|
---|
250 | packet = (flatline_packet*)data;
|
---|
251 | if(packet->id == CONNECT_REPLY) {
|
---|
252 | if(packet->connect_reply.goodtogo){
|
---|
253 | client_connected = 1;
|
---|
254 |
|
---|
255 | client_slot = ((connect_reply*)packet->data)->player_slot;
|
---|
256 |
|
---|
257 | PlayerList[client_slot] = Players+client_slot;
|
---|
258 | PlayerList[client_slot]->Chr = ONgGameState->PlayerCharacter;
|
---|
259 |
|
---|
260 | DDrConsole_PrintColored("Connection successful!",0,green, grey);
|
---|
261 |
|
---|
262 | //disable local input.
|
---|
263 | DDrPatch_NOOP(0x004FA929, 5 + 6 + 5);
|
---|
264 |
|
---|
265 | //DDrConsole_PrintF("Slot %i", ((connect_reply*)packet)->player_slot);
|
---|
266 | break;
|
---|
267 | }
|
---|
268 | else {
|
---|
269 | DDrConsole_PrintF("Connection rejected: %s", ((connect_reply*)packet->data)->message);
|
---|
270 | return false;
|
---|
271 | break;
|
---|
272 | }
|
---|
273 | }
|
---|
274 | }
|
---|
275 | if(client_connected) break;
|
---|
276 | DDrConsole_PrintF("Connection timing out in %i seconds...", CONNECTION_TIMEOUT - j);
|
---|
277 | Sleep(1000);
|
---|
278 | }
|
---|
279 | //the client timed out without recieving an error message.
|
---|
280 | if(!client_connected) {
|
---|
281 | DDrConsole_PrintColored("Connection timed out.",0,red, grey);
|
---|
282 | return false;
|
---|
283 | }
|
---|
284 | else
|
---|
285 | {
|
---|
286 | ActiveCharacter * Active;
|
---|
287 | flatline_packet packet;
|
---|
288 | //#define SPAM_INPUT
|
---|
289 | #ifdef SPAM_INPUT
|
---|
290 | struct timeval lasttime;
|
---|
291 | struct timeval thistime;
|
---|
292 | gettimeofday(&lasttime, 0);
|
---|
293 | #endif
|
---|
294 | while(1) {
|
---|
295 | #ifdef SPAM_INPUT
|
---|
296 | gettimeofday(&thistime, 0);
|
---|
297 |
|
---|
298 | //DDrConsole_PrintF("%i.%i | %i.%i | %i.%i",lasttime.tv_sec, lasttime.tv_usec, thistime.tv_sec, thistime.tv_usec,
|
---|
299 | // thistime.tv_sec - lasttime.tv_sec, thistime.tv_usec - lasttime.tv_usec);
|
---|
300 | //checks to see if enough time has passed since the last input update (by default once every 10ms)
|
---|
301 | if(
|
---|
302 | ((thistime.tv_sec > lasttime.tv_sec) && ((thistime.tv_usec + 1000000 - lasttime.tv_usec ) > update_rate * 1000) )
|
---|
303 | || ((thistime.tv_sec == lasttime.tv_sec) && ((thistime.tv_usec - lasttime.tv_usec ) > update_rate * 1000))
|
---|
304 | ) {
|
---|
305 |
|
---|
306 | flatline_packet input_packet;
|
---|
307 | lasttime.tv_usec = ++thistime.tv_usec; //in case recieving packets takes less than 1 ms.
|
---|
308 |
|
---|
309 | input_packet.id = PLAYER_INPUT;
|
---|
310 |
|
---|
311 | if( ((GameState*)(ONgGameState))->Input.Current.Actions1 != last1 || ((GameState*)(ONgGameState))->Input.Current.Actions2 != last2) {
|
---|
312 | last1 =((GameState*)(ONgGameState))->Input.Current.Actions1;
|
---|
313 | last2 =((GameState*)(ONgGameState))->Input.Current.Actions2;
|
---|
314 | }
|
---|
315 |
|
---|
316 | ((input_struct*)(void*)(input_packet.data))->Actions1 = ((GameState*)(ONgGameState))->Input.Current.Actions1;
|
---|
317 | ((input_struct*)(void*)(input_packet.data))->Actions2 = ((GameState*)(ONgGameState))->Input.Current.Actions2;
|
---|
318 | ((input_struct*)(void*)(input_packet.data))->MouseDeltaX = ((GameState*)(ONgGameState))->Input.MouseDeltaX;
|
---|
319 | ((input_struct*)(void*)(input_packet.data))->MouseDeltaY = ((GameState*)(ONgGameState))->Input.MouseDeltaY;
|
---|
320 | sent_bytes = NetUDPSocket_Send(client_sock,(sockaddr *) &address, (char*)&input_packet, sizeof(input_struct) + FLATLINE_HEADER);
|
---|
321 | //if(sent_bytes == SOCKET_ERROR) NetCatchError();
|
---|
322 | }
|
---|
323 | #endif
|
---|
324 | if(NetUDPSocket_Recieve(client_sock, (sockaddr_storage *) &client_address, &packet, &len)) {
|
---|
325 | //packet = (flatline_packet*)data;
|
---|
326 | //DDrConsole_PrintF("Data recieved, length %i, type %i", len, ((flatline_packet*)data)->id);
|
---|
327 | switch(packet.id) {
|
---|
328 | case MESSAGE:
|
---|
329 | COrMessage_Print(packet.data, "chat", 0);
|
---|
330 | break;
|
---|
331 | case CONNECT_SEND:
|
---|
332 | ;if(1) {
|
---|
333 | flatline_packet connect_recv;
|
---|
334 | memcpy(&connect_recv.connect_reply.message,"This isn't a server!", sizeof("This isn't a server!"));
|
---|
335 | NetUDPSocket_Send(client_sock, (sockaddr *) &address, (char*)&connect_recv, sizeof(bool) + FLATLINE_HEADER + sizeof("This isn't a server!"));
|
---|
336 | }
|
---|
337 | case CONNECT_REPLY:
|
---|
338 | break; //extra packet or something.
|
---|
339 | case NEW_PLAYER:
|
---|
340 | ;if(1) { //haxhaxhax
|
---|
341 | CharacterObject* Char = &(packet.new_player.Character);
|
---|
342 | uint32_t chr_index = 0;
|
---|
343 | Character* PC;
|
---|
344 | DDrConsole_PrintF("%i | %i", packet.new_player.Playernumber ,client_slot);
|
---|
345 | //Char->OSD.Options = 0;
|
---|
346 | if(packet.new_player.Playernumber == client_slot) {
|
---|
347 | PlayerList[packet.new_player.Playernumber] = &Players[0];
|
---|
348 | PC = (ONgGameState->PlayerCharacter);
|
---|
349 | Players[0].Chr = PC;
|
---|
350 |
|
---|
351 | }
|
---|
352 | else {
|
---|
353 | ONrGameState_NewCharacter(Char, NULL, NULL, &chr_index);
|
---|
354 | ONgGameState->CharacterStorage[chr_index].field_1E8 = 0;
|
---|
355 | PlayerList[packet.new_player.Playernumber] = &Players[chr_index];
|
---|
356 | Players[chr_index].Chr = &(ONgGameState->CharacterStorage[chr_index]);
|
---|
357 | Players[chr_index].Chr->Flags &= 0xFFBFFFFF;
|
---|
358 | Players[chr_index].spawnnumber = chr_index;
|
---|
359 | 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) ;
|
---|
360 | }
|
---|
361 | //Players[((new_player*)(packet.data))->Playernumber].spawnnumber = ONrGameState_NewCharacter(&(((new_player*)(packet.data))->Character), NULL, NULL, 0);
|
---|
362 | break;
|
---|
363 | }
|
---|
364 | case PLAYER_DATA:
|
---|
365 | if(1) { //haxhaxhax
|
---|
366 | player_data* data = &packet.player_data;
|
---|
367 | uint16_t i = data->PlayerNum;
|
---|
368 | GameInput * Active_Input;
|
---|
369 | data = (void*)packet.data;
|
---|
370 |
|
---|
371 | //DDrConsole_PrintF("Got data for Player %i, %x", i, PlayerList[i]);
|
---|
372 | if (i > max_connections) break;
|
---|
373 | if( !PlayerList[i] ) break;
|
---|
374 | //PlayerList[i]->Chr = ((GameState *)ONgGameState)->CharacterStorage;
|
---|
375 | #ifndef BETTER_SYNC
|
---|
376 | PlayerList[i]->Chr->Health = data->Health;
|
---|
377 | PlayerList[i]->Chr->MaxHealth = data->MaxHealth;
|
---|
378 | PlayerList[i]->Chr->Position = data->Position;
|
---|
379 | PlayerList[i]->Chr->Location = data->Location;
|
---|
380 | PlayerList[i]->Chr->LastPosition = data->LastPosition;
|
---|
381 | PlayerList[i]->Chr->Facing = data->Facing;
|
---|
382 | PlayerList[i]->Chr->DesiredFacing = data->DesiredFacing;
|
---|
383 | PlayerList[i]->Chr->CosmeticFacing = data->CosmeticFacing;
|
---|
384 | PlayerList[i]->Actions1 = data->Inputs.Actions1;
|
---|
385 | PlayerList[i]->Actions2 = data->Inputs.Actions2;
|
---|
386 | PlayerList[i]->MouseDeltaX = data->Inputs.MouseDeltaX;
|
---|
387 | PlayerList[i]->MouseDeltaY = data->Inputs.MouseDeltaY;
|
---|
388 |
|
---|
389 | Active = ((ActiveCharacter*)(ONrGetActiveCharacter(PlayerList[i]->Chr)));
|
---|
390 | if(!Active) break;
|
---|
391 |
|
---|
392 | Active->PhyContext->Position = data->Position;
|
---|
393 | #else
|
---|
394 | PlayerList[i]->Actions1 = data->Inputs.Actions1;
|
---|
395 | PlayerList[i]->Actions2 = data->Inputs.Actions2;
|
---|
396 | PlayerList[i]->MouseDeltaX = data->Inputs.MouseDeltaX;
|
---|
397 | PlayerList[i]->MouseDeltaY = data->Inputs.MouseDeltaY;
|
---|
398 | memcpy( &(PlayerList[i]->player_data), data, sizeof(player_data) );
|
---|
399 | #endif
|
---|
400 | if( !server_started && data->rare_sync_index > PlayerList[i]->rare_sync_index )
|
---|
401 | {
|
---|
402 | int sent_bytes;
|
---|
403 | flatline_packet sync_request = {0};
|
---|
404 | sync_request.id = RARE_SYNC_DATA_REQUEST;
|
---|
405 | sync_request.sync_request = i;
|
---|
406 | DDrConsole_PrintF( "Requesting sync data for player %i, old index %u", i, PlayerList[i]->rare_sync_index);
|
---|
407 | sent_bytes = NetUDPSocket_Send(client_sock,(sockaddr *) &address, (char*)&sync_request, FLATLINE_HEADER + sizeof(int) );
|
---|
408 | }
|
---|
409 |
|
---|
410 |
|
---|
411 | #if 0
|
---|
412 | TMrInstance_GetDataPtr('TRAM',data->Animation,Active->Animation);
|
---|
413 | Active->AnimationFromState = data->AnimationFromState;
|
---|
414 | Active->AnimationToState = data->AnimationToState;
|
---|
415 | Active->AnimationType = data->AnimationType;
|
---|
416 | Active->NextAnimationType = data->NextAnimationType;
|
---|
417 | Active->Frame = data->Frame;
|
---|
418 | Active->InterpolationCurrentFrame = data->InterpolationCurrentFrame;
|
---|
419 | Active->InterpolationFromState = data->InterpolationFromState;
|
---|
420 | Active->InterpolationLength = data->InterpolationLength;
|
---|
421 | memcpy(Active->InterpolationStartRotations, data->InterpolationStartRotations,
|
---|
422 | sizeof(Quaternion) * 19);
|
---|
423 | memcpy(Active->BoneMatrices, data->BoneMatrices,
|
---|
424 | sizeof(Quaternion) * 19);
|
---|
425 | Active->Stitch = data->Stitch;
|
---|
426 | Active->StitchHeight = data->StitchHeight;
|
---|
427 | Active->StitchVelocity = data->StitchVelocity;
|
---|
428 |
|
---|
429 | Active->Overlay = data->Overlay;
|
---|
430 | memcpy(Active->OverlayRotations, data->OverlayRotations,
|
---|
431 | sizeof(Quaternion) * 19);
|
---|
432 | #endif
|
---|
433 | //Active->animationtype2 = data->AnimationType2;
|
---|
434 | /*
|
---|
435 | if((int)*((char*)(Active + 0x1AF8) + 0x166) > data->Frame + 1)
|
---|
436 | *(uint16_t *)((char*)Active + 0x1C88) = data->Frame + 1;
|
---|
437 | */
|
---|
438 | break;
|
---|
439 | }
|
---|
440 | case RARE_SYNC_DATA:
|
---|
441 | if(1) {
|
---|
442 | sl_arg hax[2];
|
---|
443 | int dontuse;
|
---|
444 | uint16_t i = packet.rare_sync_data.PlayerNum;
|
---|
445 |
|
---|
446 | if (i > max_connections) break;
|
---|
447 | if( !PlayerList[i] ) break;
|
---|
448 |
|
---|
449 | //WEAPONS ARE DISABLED. Why? Pain in the arse to sync.
|
---|
450 | packet.rare_sync_data.Inventory.Weapons[0] = NULL;
|
---|
451 | packet.rare_sync_data.Inventory.Weapons[1] = NULL;
|
---|
452 | packet.rare_sync_data.Inventory.Weapons[2] = NULL;
|
---|
453 | // TMrInstance_GetDataPtr( 'ONCC', packet.rare_sync_data.Class, PlayerList[ i ]->Chr->ONCC );
|
---|
454 |
|
---|
455 | //add the target character
|
---|
456 | hax[0].type = sl_int32;
|
---|
457 | hax[0].value_int32 = PlayerList[ packet.rare_sync_data.PlayerNum ]->spawnnumber;
|
---|
458 |
|
---|
459 | //add the new class
|
---|
460 | //fix this later so we cant buffer overflow :O
|
---|
461 | hax[1].type = sl_str32;
|
---|
462 | hax[1].value_str32 = packet.rare_sync_data.Class;
|
---|
463 |
|
---|
464 | //we are directly calling a bsl function instead of using the normal method for two reasons
|
---|
465 | //1. it has all the checking built in
|
---|
466 | iSetCharacterClass( 0, 2, hax, &dontuse, &dontuse, hax );
|
---|
467 | //DDrConsole_PrintF( "Recieved sync data for player %i, class %s, old index %u, new index %u", i, packet.rare_sync_data.Class, PlayerList[i]->rare_sync_index, packet.rare_sync_data.index);
|
---|
468 | memcpy( &(PlayerList[ i ]->Chr->Inventory), &(packet.rare_sync_data.Inventory), sizeof(Inventory ));
|
---|
469 |
|
---|
470 | PlayerList[i]->rare_sync_index = packet.rare_sync_data.index;
|
---|
471 | }
|
---|
472 | break;
|
---|
473 |
|
---|
474 | default:
|
---|
475 | DDrConsole_PrintF("Warning, recieved badly formed packet!");
|
---|
476 | break;
|
---|
477 | }
|
---|
478 | }
|
---|
479 | else {
|
---|
480 | Sleep(1);
|
---|
481 | }
|
---|
482 | }
|
---|
483 | }
|
---|
484 | return true;
|
---|
485 | }
|
---|
486 |
|
---|
487 | //wtf, this needs cleaned up...
|
---|
488 | player_info *FLr_FindEmptySlot() {
|
---|
489 | int j;
|
---|
490 | for(j = 0; j < MAX_PLAYERS; j++) {
|
---|
491 | if (Players[j].ip == 0) {
|
---|
492 | return &Players[j];
|
---|
493 | }
|
---|
494 | }
|
---|
495 | return 0;
|
---|
496 | }
|
---|
497 |
|
---|
498 | extern uint16_t max_connections;
|
---|
499 | uint16_t FLr_FindEmptyListSlot() {
|
---|
500 | int j;
|
---|
501 | for(j = 0; j < max_connections; j++) {
|
---|
502 | if (PlayerList[j] == 0) {
|
---|
503 | return j;
|
---|
504 | }
|
---|
505 | }
|
---|
506 | return -1;
|
---|
507 | }
|
---|
508 |
|
---|
509 | static flatline_packet cache_input = {0};
|
---|
510 | extern void* TSrTest;
|
---|
511 | void * ONICALL FLrInput_Update_Keys(void)
|
---|
512 | {
|
---|
513 | uint16_t i;
|
---|
514 | /*DDrConsole_PrintF("Current: %x %x | Start: %x %x | Stop: %x %x | Stopped %x %x",
|
---|
515 | ONgGameState->Input.Current.Actions1, ONgGameState->Input.Current.Actions2,
|
---|
516 | ONgGameState->Input.Start.Actions1,ONgGameState->Input.Start.Actions2,
|
---|
517 | ONgGameState->Input.Stop.Actions1, ONgGameState->Input.Stop.Actions2,
|
---|
518 | ONgGameState->Input.Stopped.Actions2, ONgGameState->Input.Stopped.Actions2
|
---|
519 | );
|
---|
520 | */
|
---|
521 | ActiveCharacter * Active_Player = ONgGameState->ActiveCharacters;
|
---|
522 | Active_Player->PhyContext->Rotation;
|
---|
523 | Active_Player->PhyContext->Position;
|
---|
524 | ONgGameState->PlayerCharacter->Position;
|
---|
525 |
|
---|
526 |
|
---|
527 |
|
---|
528 | /*
|
---|
529 | if( ONgGameState->Input.MouseDeltaX != 0 || ONgGameState->Input.MouseDeltaY != 0 ||
|
---|
530 | ONgGameState->Input.field_8 != 0 || ONgGameState->Input.field_C != 0 )
|
---|
531 |
|
---|
532 | DDrConsole_PrintF("%f %f | %f %f",
|
---|
533 | ONgGameState->Input.MouseDeltaX , ONgGameState->Input.MouseDeltaY,
|
---|
534 | ONgGameState->Input.field_8, ONgGameState->Input.field_C);
|
---|
535 |
|
---|
536 | */
|
---|
537 | //if sprint timer is equal to 0, display 0
|
---|
538 | //else if sprint timer is equal to -1, display -1
|
---|
539 | //else display difference
|
---|
540 | //good thing this is just quick an dirty debug stuff :)
|
---|
541 |
|
---|
542 | //basically it seems that if the difference is bigger than 15 frames, you cant dash. : /
|
---|
543 |
|
---|
544 | #ifndef SPAM_INPUT
|
---|
545 | if(client_connected)
|
---|
546 | {
|
---|
547 | int sent_bytes;
|
---|
548 | flatline_packet input_packet = {0};
|
---|
549 | input_packet.id = PLAYER_INPUT;
|
---|
550 | input_packet.input_struct.Time = ONgGameState->GameTime;
|
---|
551 | input_packet.input_struct.Actions1 = ((GameState*)(ONgGameState))->Input.Current.Actions1;
|
---|
552 | input_packet.input_struct.Actions2 = ((GameState*)(ONgGameState))->Input.Current.Actions2;
|
---|
553 | input_packet.input_struct.MouseDeltaX = ((GameState*)(ONgGameState))->Input.MouseDeltaX;
|
---|
554 | input_packet.input_struct.MouseDeltaY = ((GameState*)(ONgGameState))->Input.MouseDeltaY;
|
---|
555 |
|
---|
556 | sent_bytes = NetUDPSocket_Send(client_sock,(sockaddr *) &address, (char*)&input_packet, sizeof(input_struct) + FLATLINE_HEADER);
|
---|
557 |
|
---|
558 | //return ONgGameState;
|
---|
559 | }
|
---|
560 | #endif
|
---|
561 | //Testing drawing text to the screen...this failed. Will need to get it working eventually.
|
---|
562 | if( TSrTest )
|
---|
563 | {
|
---|
564 | OniRectangle TextRect = { 20, 20, 50, 50 };
|
---|
565 | TSrContext_DrawText(TSrTest, "Testing woohoo", 0xFF, 0, &TextRect);
|
---|
566 | }
|
---|
567 | if(!(server_started || client_connected)) return ONgGameState;
|
---|
568 |
|
---|
569 | for(i = 0; i < max_connections; i++) {
|
---|
570 | ActiveCharacter * Active_Player;
|
---|
571 | GameInput * Active_Input;
|
---|
572 | if(PlayerList[i] == 0) continue;
|
---|
573 |
|
---|
574 | //is this right?
|
---|
575 | Active_Player = (void*)ONrGetActiveCharacter( PlayerList[i]->Chr);
|
---|
576 |
|
---|
577 | if(Active_Player == 0) continue;
|
---|
578 | Active_Input = &(Active_Player->Input);
|
---|
579 | if(server_started) {
|
---|
580 | player_data * data;
|
---|
581 | flatline_packet data_out = {0};
|
---|
582 |
|
---|
583 | //if( PlayerList[i]->Chr->Health == 0) PlayerList[i]->Chr->Health = PlayerList[i]->Chr->MaxHealth;
|
---|
584 |
|
---|
585 | data_out.id = PLAYER_DATA;
|
---|
586 | data = (void*)&(data_out.data);
|
---|
587 | data->PlayerNum = i;
|
---|
588 | data->Health = PlayerList[i]->Chr->Health;
|
---|
589 | data->MaxHealth = PlayerList[i]->Chr->MaxHealth;
|
---|
590 | data->Position = PlayerList[i]->Chr->Position;
|
---|
591 | data->Location = PlayerList[i]->Chr->Location;
|
---|
592 | data->LastPosition = PlayerList[i]->Chr->LastPosition;
|
---|
593 | data->Facing = PlayerList[i]->Chr->Facing;
|
---|
594 | data->DesiredFacing = PlayerList[i]->Chr->DesiredFacing;
|
---|
595 | data->CosmeticFacing = PlayerList[i]->Chr->CosmeticFacing;
|
---|
596 |
|
---|
597 |
|
---|
598 | data->Position = Active_Player->PhyContext->Position;
|
---|
599 |
|
---|
600 | if( PlayerList[i]->OldClass != PlayerList[i]->Chr->ONCC || memcmp( &(PlayerList[i]->Inventory), &(PlayerList[i]->Chr->Inventory), sizeof(Inventory) ) )
|
---|
601 | {
|
---|
602 | PlayerList[i]->OldClass = PlayerList[i]->Chr->ONCC;
|
---|
603 | memcpy( &(PlayerList[i]->Inventory), &(PlayerList[i]->Chr->Inventory), sizeof(Inventory) );
|
---|
604 | PlayerList[i]->rare_sync_index++;
|
---|
605 |
|
---|
606 | }
|
---|
607 |
|
---|
608 | data->rare_sync_index = PlayerList[i]->rare_sync_index;
|
---|
609 |
|
---|
610 | #if 0
|
---|
611 | data->Frame = Active_Player->Frame;
|
---|
612 | memcpy(data->Animation, TMrInstance_GetInstanceName(Active_Player->Animation), 32);
|
---|
613 | data->AnimationToState = Active_Player->AnimationToState;
|
---|
614 | data->AnimationFromState = Active_Player->AnimationFromState;
|
---|
615 | data->AnimationType = Active_Player->AnimationType;
|
---|
616 | data->NextAnimationType = Active_Player->NextAnimationType;
|
---|
617 | data->InterpolationCurrentFrame = Active_Player->InterpolationCurrentFrame;
|
---|
618 | data->InterpolationFromState = Active_Player->InterpolationFromState;
|
---|
619 | data->InterpolationLength = Active_Player->InterpolationLength;
|
---|
620 | memcpy(data->InterpolationStartRotations, Active_Player->InterpolationStartRotations,
|
---|
621 | sizeof(Quaternion) * 19);
|
---|
622 | // data->InterpolationStartRotations = Active_Player->InterpolationStartRotations;
|
---|
623 | memcpy(data->BoneMatrices, Active_Player->BoneMatrices,
|
---|
624 | sizeof(Quaternion) * 19);
|
---|
625 | data->Stitch = Active_Player->Stitch;
|
---|
626 | data->StitchHeight = Active_Player->StitchHeight;
|
---|
627 | data->StitchVelocity = Active_Player->StitchVelocity;
|
---|
628 |
|
---|
629 | data->Overlay = Active_Player->Overlay;
|
---|
630 | memcpy( data->OverlayRotations, Active_Player->OverlayRotations
|
---|
631 | ,sizeof(Quaternion) * 19);
|
---|
632 | #endif
|
---|
633 | if(i == 0) {
|
---|
634 | data->Inputs.Actions1 = ((GameState*)(ONgGameState))->Input.Current.Actions1;
|
---|
635 | data->Inputs.Actions2 = ((GameState*)(ONgGameState))->Input.Current.Actions2;
|
---|
636 | data->Inputs.MouseDeltaX = ONgGameState->Input.MouseDeltaX;
|
---|
637 | data->Inputs.MouseDeltaY = ONgGameState->Input.MouseDeltaY;
|
---|
638 | }
|
---|
639 | else{
|
---|
640 | data->Inputs.Actions1 = PlayerList[i]->Actions1;
|
---|
641 | data->Inputs.Actions2 = PlayerList[i]->Actions2;
|
---|
642 | data->Inputs.MouseDeltaX = PlayerList[i]->MouseDeltaX;
|
---|
643 | data->Inputs.MouseDeltaY = PlayerList[i]->MouseDeltaY;
|
---|
644 | }
|
---|
645 | memcpy( &(PlayerList[i]->player_data), data, sizeof(player_data) );
|
---|
646 |
|
---|
647 | UDPServer_SendToAll(&data_out, sizeof(player_data) + FLATLINE_HEADER);
|
---|
648 | }
|
---|
649 |
|
---|
650 | if( (server_started && i !=0) || (!server_started/* && i != client_slot*/) )
|
---|
651 | {
|
---|
652 | //this just made sync even worse...
|
---|
653 | #ifdef JITTER_FIX
|
---|
654 | input_struct * New_Input = &PlayerList[i]->MouseDeltaX;
|
---|
655 | input_struct * Cache_Input = &PlayerList[i]->CacheInput;
|
---|
656 | Active_Input->Stop.Actions1 = ~Cache_Input->Actions1 & Active_Input->Current.Actions1;
|
---|
657 | Active_Input->Stop.Actions2 = ~Cache_Input->Actions2 & Active_Input->Current.Actions2;
|
---|
658 | Active_Input->Start.Actions1 = ~Active_Input->Current.Actions1 & Cache_Input->Actions1;
|
---|
659 | Active_Input->Start.Actions2 = ~Active_Input->Current.Actions2 & Cache_Input->Actions2;
|
---|
660 | Active_Input->Current.Actions1 = Cache_Input->Actions1;
|
---|
661 | Active_Input->Current.Actions2 = Cache_Input->Actions2;
|
---|
662 | Active_Input->Stopped.Actions1 = ~Active_Input->Current.Actions1;
|
---|
663 | Active_Input->Stopped.Actions2 = ~Active_Input->Current.Actions2;
|
---|
664 | Active_Input->MouseDeltaX = Cache_Input->MouseDeltaX;
|
---|
665 | Active_Input->MouseDeltaY = Cache_Input->MouseDeltaY;
|
---|
666 | memcpy( Cache_Input, New_Input, sizeof(input_struct));
|
---|
667 | #else
|
---|
668 |
|
---|
669 | Active_Input->Stop.Actions1 = ~PlayerList[i]->Actions1 & Active_Input->Current.Actions1;
|
---|
670 | Active_Input->Stop.Actions2 = ~PlayerList[i]->Actions2 & Active_Input->Current.Actions2;
|
---|
671 | Active_Input->Start.Actions1 = ~Active_Input->Current.Actions1 & PlayerList[i]->Actions1;
|
---|
672 | Active_Input->Start.Actions2 = ~Active_Input->Current.Actions2 & PlayerList[i]->Actions2;
|
---|
673 |
|
---|
674 | Active_Input->Current.Actions1 = PlayerList[i]->Actions1;
|
---|
675 | Active_Input->Current.Actions2 = PlayerList[i]->Actions2;
|
---|
676 | Active_Input->Stopped.Actions1 = ~Active_Input->Current.Actions1;
|
---|
677 | Active_Input->Stopped.Actions2 = ~Active_Input->Current.Actions2;
|
---|
678 | Active_Input->MouseDeltaX = PlayerList[i]->MouseDeltaX;
|
---|
679 | Active_Input->MouseDeltaY = PlayerList[i]->MouseDeltaY;
|
---|
680 | //DDrConsole_PrintF("Timer: %i", (Active_Player->SprintTimer != 0) ? (Active_Player->SprintTimer == -1) ? -1 : (ONgGameState->GameTime - Active_Player->SprintTimer) : 0);
|
---|
681 |
|
---|
682 | //DDrConsole_PrintF("1E8 %u", ONgGameState->PlayerCharacter->field_1E8);
|
---|
683 | /*
|
---|
684 | DDrConsole_PrintF("T %u | Current: %x %x | Start: %x %x | Stop: %x %x | Stopped %x %x",
|
---|
685 | PlayerList[i]->LastInputTime,
|
---|
686 | Active_Input->Current.Actions1, Active_Input->Current.Actions2,
|
---|
687 | Active_Input->Start.Actions1,Active_Input->Start.Actions2,
|
---|
688 | Active_Input->Stop.Actions1, Active_Input->Stop.Actions2,
|
---|
689 | Active_Input->Stopped.Actions1, Active_Input->Stopped.Actions2
|
---|
690 | );
|
---|
691 | */
|
---|
692 |
|
---|
693 |
|
---|
694 | #endif
|
---|
695 |
|
---|
696 | #ifdef BETTER_SYNC
|
---|
697 | if( !server_started && PlayerList[i]->player_data.Health != 0) {
|
---|
698 | PlayerList[i]->Chr->Health = PlayerList[i]->player_data.Health;
|
---|
699 | PlayerList[i]->Chr->MaxHealth = PlayerList[i]->player_data.MaxHealth;
|
---|
700 | PlayerList[i]->Chr->Position = PlayerList[i]->player_data.Position;
|
---|
701 | PlayerList[i]->Chr->Location = PlayerList[i]->player_data.Location;
|
---|
702 | PlayerList[i]->Chr->LastPosition = PlayerList[i]->player_data.LastPosition;
|
---|
703 | PlayerList[i]->Chr->Facing = PlayerList[i]->player_data.Facing;
|
---|
704 | PlayerList[i]->Chr->DesiredFacing = PlayerList[i]->player_data.DesiredFacing;
|
---|
705 | PlayerList[i]->Chr->CosmeticFacing = PlayerList[i]->player_data.CosmeticFacing;
|
---|
706 | PlayerList[i]->Actions1 = PlayerList[i]->player_data.Inputs.Actions1;
|
---|
707 | PlayerList[i]->Actions2 = PlayerList[i]->player_data.Inputs.Actions2;
|
---|
708 | PlayerList[i]->MouseDeltaX = PlayerList[i]->player_data.Inputs.MouseDeltaX;
|
---|
709 | PlayerList[i]->MouseDeltaY = PlayerList[i]->player_data.Inputs.MouseDeltaY;
|
---|
710 | Active_Player->PhyContext->Position = PlayerList[i]->player_data.Position;
|
---|
711 | }
|
---|
712 | #endif
|
---|
713 |
|
---|
714 | }
|
---|
715 | }
|
---|
716 |
|
---|
717 | return ONgGameState;
|
---|
718 | }
|
---|