source: Daodan/MSVC/Flatline.c@ 580

Last change on this file since 580 was 580, checked in by gumby, 14 years ago
File size: 29.6 KB
Line 
1#include "Flatline.h"
2#include "Oni_Character.h"
3#include "Flatline_Client.h"
4#include "Flatline_Server.h"
5#include "Flatline_Events.h"
6//#include <sys/time.h>
7#include <time.h>
8uint32_t last1 = 0; uint32_t last2 = 0;
9player_info Players[MAX_PLAYERS] = {{0}, {0}, {0}, {0}};
10player_info * PlayerList[MAX_CONNECTIONS] = {0};
11
12unsigned int lastPingTime;
13
14const char * Rejection_Messages[][255] = {
15 {"Server is full"},
16 {"-2"},
17 {"-3"},
18 {"-4"},
19 {"-5"},
20};
21
22#define BETTER_SYNC
23
24void DoRareSync( short Player, sockaddr_in * sender )
25{
26 flatline_packet sync = {0};
27
28 if (Player > max_connections || !PlayerList[ Player ] ) return;
29
30
31 // DDrConsole_PrintF( "Sending sync data for player %i, new index %u", Player, PlayerList[ Player ]->rare_sync_index);
32 sender->sin_addr.S_un.S_addr = htonl(sender->sin_addr.S_un.S_addr);
33 sync.id = RARE_SYNC_DATA;
34 sprintf( sync.rare_sync_data.Class, TMrInstance_GetInstanceName( PlayerList[ Player ]->Chr->ONCC ) );
35 //using ->Inventory instead of ->Chr->Inventory to keep the index and Inventory in sync, just in case.
36 memcpy( &(sync.rare_sync_data.Inventory), &(PlayerList[ Player ]->Inventory), sizeof(Inventory) );
37 //WEAPONS ARE DISABLED. Why? Pain in the arse to sync.
38 sync.rare_sync_data.Inventory.Weapons[0] = NULL;
39 sync.rare_sync_data.Inventory.Weapons[1] = NULL;
40 sync.rare_sync_data.Inventory.Weapons[2] = NULL;
41 sync.rare_sync_data.PlayerNum = Player;
42 sync.rare_sync_data.index = PlayerList[ Player ]->rare_sync_index;
43 NetTCPServer_Send( sender, (char*)&sync, sizeof(rare_sync_data) + FLATLINE_HEADER );
44}
45
46bool FLrServer_PacketCallback(char* data, int datalen, int from)
47{
48 int i, j;
49 bool found_player = 0;
50 flatline_packet * packet = (flatline_packet*)data;
51 static int recieved = 0;
52 sockaddr_in sender;
53 sender.sin_family = AF_INET;
54 sender.sin_port = htons(27777);
55 sender.sin_addr = *((struct in_addr*)(int*)&from);
56
57
58 //packet->data[datalen] = '\0';
59
60 //DDrConsole_PrintF("Packet \r%d recieved from %i", ++recieved, from);
61
62
63
64 //if data[0] != CONNECT_SEND, search in playerlist for ip address
65
66
67
68
69 switch(packet->id) {
70 flatline_packet connect_recv;
71 player_info * playah;
72 //rewrite this when we get TCP support.
73 //rewrite this before we get TCP support*
74 //the way of seeing if there is room for players sucks.
75 case CONNECT_SEND:
76 ;
77
78 connect_recv.id = CONNECT_REPLY;
79
80 //if(Players[i].ip == sender.sin_addr.S_un.S_addr) break; //needs to send an error message
81 sender.sin_addr.S_un.S_addr = htonl(sender.sin_addr.S_un.S_addr);
82 playah = FLrServer_AddPlayer(from,packet->connect_send.name, 0, 0);
83 DDrConsole_PrintF("%s connected from %s", packet->connect_send.name, inet_ntoa(sender.sin_addr ) );
84 if(!((int)playah > -5 && (int)playah <= 0)) {
85 flatline_packet new_char = {0};
86 CharacterObject* Char;
87 connect_recv.connect_reply.goodtogo = 1;
88 connect_recv.connect_reply.player_slot = playah->list_slot;
89 DDrConsole_PrintF("Slot: %i", playah->list_slot);
90
91 //sending this several times to make sure it gets through. Really need to make up some form of packet tracking.
92 NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER);
93 NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER);
94 NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER);
95 NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER);
96 NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER);
97 Sleep(100);
98
99 new_char.id = NEW_PLAYER;
100 Char = &new_char.new_player.Character;
101 memset(Char, 0, sizeof(CharacterObject));
102 Char->Header.Type = 'CHAR';
103 Char->OSD.Options = chr_dontaim;
104 for(j = 0; j < max_connections; j++) {
105 if(PlayerList[j] != 0) {
106 new_char.new_player.Playernumber = j;
107 sprintf(Char->OSD.Name,"%s",PlayerList[j]->name);
108
109 sprintf(Char->OSD.Class, "%s", TMrInstance_GetInstanceName(PlayerList[j]->Chr->ONCC));
110 DDrConsole_PrintF("Class %s", Char->OSD.Class );
111
112 sprintf(Char->OSD.Class, "konoko_generic");
113 NetTCPServer_Send((sockaddr *) &sender, (char*)&new_char, sizeof(new_player) + FLATLINE_HEADER );
114 }
115
116 }
117 }
118 else {
119 //fix the error messages...
120 DDrConsole_PrintF("Server is full. :(");
121 connect_recv.connect_reply.goodtogo = 0;
122 sender.sin_addr.S_un.S_addr = htonl(sender.sin_addr.S_un.S_addr);
123 memcpy(&connect_recv.connect_reply.message,"Server is full.", sizeof("Server is full."));
124 NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(bool)*2 + FLATLINE_HEADER + sizeof("Server is full."));
125
126 }
127
128
129 break;
130 case CONNECT_REPLY:
131 break; //do nothing...a server shouldn't recieve this type of packet.
132 case MESSAGE:
133 for(i = 0; i < MAX_PLAYERS; i++) {
134 //DDrConsole_PrintF("%i : %i | %s : %s", from, Players[i].ip, inet_ntoa(*(struct in_addr*)&from), inet_ntoa(*(struct in_addr*)&(Players[i].ip)));
135 if(Players[i].ip == sender.sin_addr.S_un.S_addr) {
136 found_player = 1;
137 break;
138 }
139 }
140 if(found_player == 0) return true;
141 else {
142 char message_buffer[512] = {0};
143 flatline_packet message;
144 int message_size;
145 data[datalen] = 0;
146
147 DDrConsole_PrintF("%s: %s", Players[i].name, packet->data);
148 sprintf(message_buffer, "%s: %s", Players[i].name, packet->data);
149
150 message.id = MESSAGE;
151 message_size = sprintf(message.data, "%s", message_buffer);
152 COrMessage_Print(message_buffer, "chat", 0);
153 UDPServer_SendToAll(&message, message_size + 1 + FLATLINE_HEADER);
154 break;
155 }
156 case CHANGE_NAME:
157 ; //wtf, needed or i get an error.
158 DDrConsole_PrintF("Changing Name to: %s", packet->data);
159 for(i = 0; i < MAX_PLAYERS; i++) {
160 if(Players[i].ip == sender.sin_addr.S_un.S_addr) {
161 found_player = 1;
162 break;
163 }
164 }
165 if(found_player == 0) return true;
166 else {
167 bool name_exists = 0;
168 for(j = 0; j < MAX_PLAYERS; j++) {
169 if(!strcmp(packet->data, Players[j].name)) {
170 name_exists = 1;
171 break;
172 }
173 }
174 if(!name_exists) {
175 char message_buffer[1024];
176 sprintf(message_buffer,"%s changed their name to %s", Players[i].name, packet->data);
177 COrMessage_Print(message_buffer, "name_change", 0);
178 memcpy(Players[i].name, packet->data, 256);
179
180 }
181 break;
182 }
183 case PLAYER_INPUT:
184
185 for(i = 0; i < max_connections; i++) {
186 if(PlayerList[i] != 0 && PlayerList[i]->ip == sender.sin_addr.S_un.S_addr) {
187 found_player = 1;
188 break;
189 }
190 }
191
192 if(found_player == 0) return true;
193 else {
194 input_struct * packet_input = &packet->input_struct;
195
196
197 PlayerList[i]->Actions1 = packet_input->Actions1;
198 PlayerList[i]->Actions2 = packet_input->Actions2;
199 PlayerList[i]->MouseDeltaX = packet_input->MouseDeltaX;
200 PlayerList[i]->MouseDeltaY = packet_input->MouseDeltaY;
201 PlayerList[i]->LastInputTime = packet_input->Time;
202
203 break;
204 }
205 case RARE_SYNC_DATA_REQUEST:
206 DoRareSync( packet->sync_request , &sender);
207 break;
208 case PK_PONG:
209 for(i = 0; i < max_connections; i++) {
210 if(PlayerList[i] != 0 && PlayerList[i]->ip == sender.sin_addr.S_un.S_addr) {
211 found_player = 1;
212 break;
213 }
214 }
215
216 if(found_player == 0) return true;
217 if(packet->ping != lastPingTime)
218 {
219 PlayerList[i]->Ping = 999;
220 }
221 else
222 {
223 PlayerList[i]->Ping = (ONgGameState->GameTime - packet->ping) * 1000 / 60;
224 }
225 break;
226 default:
227 DDrConsole_PrintF("Warning, recieved badly formed packet!");
228 break;
229 }
230 return true;
231}
232
233bool FLrServer_Run()
234{
235 // Get the local hostname
236 char szHostName[255];
237 struct hostent *host_entry;
238 gethostname(szHostName, 255);
239
240 host_entry=gethostbyname(szHostName);
241 DDrConsole_PrintF("Server started at %s...", inet_ntoa (*(struct in_addr *)*host_entry->h_addr_list));
242 return NetUDPServer_Listen(27777, FLrServer_PacketCallback);
243}
244
245short TRrAnimation_GetType(char* anim)
246{
247 return *(short*)(anim + 0x15A);
248}
249
250void ONrCharacter_SetAnimationInternal(Character* Char, ActiveCharacter* AChar,
251 short inFromState, short inNextAnimType, const void *TRAM)
252{
253 ONCC *ONCC = Char->ONCC;
254 void *TRAC = ONCC->TRAC;
255 short index = Char->Number;
256 short animType;
257
258 if (TRAM == 0) return;
259
260 animType = TRrAnimation_GetType(TRAM);
261
262 AChar->Animation = TRAM;
263 AChar->Frame = 0;
264 AChar->AnimationFromState = inFromState;
265 AChar->AnimationType = animType;
266
267 AChar->NextAnimationType= inNextAnimType;
268 AChar->AnimationToState = TRrAnimation_GetTo(TRAM);
269
270 return;
271}
272
273
274RGBA green = {0, 0xFF, 0, 0};
275RGBA red = {0, 0, 0xFF, 0};
276RGBA grey = {0x80,0x80,0x80,0x80};
277
278//FLrClient_Run
279//Looping function that waits for packets from the server.
280
281int client_slot = 0;
282bool FLrClient_Run(flatline_packet* packet)
283{
284
285 char data[1400];
286 uint16_t len;
287 int j;
288 int sent_bytes;
289 client_connected = 0;
290
291
292 //starts the connection
293 DDrConsole_PrintF("Connecting to server %s on socket %i", inet_ntoa(address.sin_addr), client_sock);
294 sent_bytes = NetUDPSocket_Send(client_sock, (sockaddr*)&address, (char*)packet, 255);
295 if(sent_bytes == SOCKET_ERROR) {
296 NetCatchError();
297 }
298 //loops once per second waiting for a reply.
299 for(j = 0; j < CONNECTION_TIMEOUT; j++) {
300 while(NetUDPSocket_Recieve(client_sock, (sockaddr_storage *) &client_address, data, &len)){
301 packet = (flatline_packet*)data;
302 if(packet->id == CONNECT_REPLY) {
303 if(packet->connect_reply.goodtogo){
304 client_connected = 1;
305
306 client_slot = ((connect_reply*)packet->data)->player_slot;
307
308 PlayerList[client_slot] = Players+client_slot;
309 PlayerList[client_slot]->Chr = ONgGameState->PlayerCharacter;
310
311 DDrConsole_PrintColored("Connection successful!",0,green, grey);
312
313 //disable local input.
314 DDrPatch_NOOP(0x004FA929, 5 + 6 + 5);
315
316 //DDrConsole_PrintF("Slot %i", ((connect_reply*)packet)->player_slot);
317 break;
318 }
319 else {
320 DDrConsole_PrintF("Connection rejected: %s", ((connect_reply*)packet->data)->message);
321 return false;
322 break;
323 }
324 }
325 }
326 if(client_connected) break;
327 DDrConsole_PrintF("Connection timing out in %i seconds...", CONNECTION_TIMEOUT - j);
328 Sleep(1000);
329 }
330 //the client timed out without recieving an error message.
331 if(!client_connected) {
332 DDrConsole_PrintColored("Connection timed out.",0,red, grey);
333 return false;
334 }
335 else
336 {
337 ActiveCharacter * Active;
338 flatline_packet packet;
339 //#define SPAM_INPUT
340#ifdef SPAM_INPUT
341 struct timeval lasttime;
342 struct timeval thistime;
343 gettimeofday(&lasttime, 0);
344#endif
345 while(1) {
346#ifdef SPAM_INPUT
347 gettimeofday(&thistime, 0);
348
349 //DDrConsole_PrintF("%i.%i | %i.%i | %i.%i",lasttime.tv_sec, lasttime.tv_usec, thistime.tv_sec, thistime.tv_usec,
350 // thistime.tv_sec - lasttime.tv_sec, thistime.tv_usec - lasttime.tv_usec);
351 //checks to see if enough time has passed since the last input update (by default once every 10ms)
352 if(
353 ((thistime.tv_sec > lasttime.tv_sec) && ((thistime.tv_usec + 1000000 - lasttime.tv_usec ) > update_rate * 1000) )
354 || ((thistime.tv_sec == lasttime.tv_sec) && ((thistime.tv_usec - lasttime.tv_usec ) > update_rate * 1000))
355 ) {
356
357 flatline_packet input_packet;
358 lasttime.tv_usec = ++thistime.tv_usec; //in case recieving packets takes less than 1 ms.
359
360 input_packet.id = PLAYER_INPUT;
361
362 if( ((GameState*)(ONgGameState))->Input.Current.Actions1 != last1 || ((GameState*)(ONgGameState))->Input.Current.Actions2 != last2) {
363 last1 =((GameState*)(ONgGameState))->Input.Current.Actions1;
364 last2 =((GameState*)(ONgGameState))->Input.Current.Actions2;
365 }
366
367 ((input_struct*)(void*)(input_packet.data))->Actions1 = ((GameState*)(ONgGameState))->Input.Current.Actions1;
368 ((input_struct*)(void*)(input_packet.data))->Actions2 = ((GameState*)(ONgGameState))->Input.Current.Actions2;
369 ((input_struct*)(void*)(input_packet.data))->MouseDeltaX = ((GameState*)(ONgGameState))->Input.MouseDeltaX;
370 ((input_struct*)(void*)(input_packet.data))->MouseDeltaY = ((GameState*)(ONgGameState))->Input.MouseDeltaY;
371 sent_bytes = NetUDPSocket_Send(client_sock,(sockaddr *) &address, (char*)&input_packet, sizeof(input_struct) + FLATLINE_HEADER);
372 //if(sent_bytes == SOCKET_ERROR) NetCatchError();
373 }
374#endif
375 if(NetUDPSocket_Recieve(client_sock, (sockaddr_storage *) &client_address, &packet, &len)) {
376 //packet = (flatline_packet*)data;
377 //DDrConsole_PrintF("Data recieved, length %i, type %i", len, ((flatline_packet*)data)->id);
378 switch(packet.id) {
379 case MESSAGE:
380 COrMessage_Print(packet.data, "chat", 0);
381 break;
382 case CONNECT_SEND:
383 ;if(1) {
384 flatline_packet connect_recv;
385 memcpy(&connect_recv.connect_reply.message,"This isn't a server!", sizeof("This isn't a server!"));
386 NetUDPSocket_Send(client_sock, (sockaddr *) &address, (char*)&connect_recv, sizeof(bool) + FLATLINE_HEADER + sizeof("This isn't a server!"));
387 }
388 case CONNECT_REPLY:
389 break; //extra packet or something.
390 case NEW_PLAYER:
391 ;if(1) { //haxhaxhax
392 CharacterObject* Char = &(packet.new_player.Character);
393 uint32_t chr_index = 0;
394 Character* PC;
395 DDrConsole_PrintF("%i | %i", packet.new_player.Playernumber ,client_slot);
396 //Char->OSD.Options = 0;
397 if(packet.new_player.Playernumber == client_slot) {
398 PlayerList[packet.new_player.Playernumber] = &Players[0];
399 PC = (ONgGameState->PlayerCharacter);
400 Players[0].Chr = PC;
401
402 }
403 else {
404 ONrGameState_NewCharacter(Char, NULL, NULL, &chr_index);
405 ONgGameState->CharacterStorage[chr_index].charType = 0;
406 PlayerList[packet.new_player.Playernumber] = &Players[chr_index];
407 Players[chr_index].Chr = &(ONgGameState->CharacterStorage[chr_index]);
408 Players[chr_index].Chr->Flags &= 0xFFBFFFFF;
409 Players[chr_index].spawnnumber = chr_index;
410 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) ;
411 }
412 //Players[((new_player*)(packet.data))->Playernumber].spawnnumber = ONrGameState_NewCharacter(&(((new_player*)(packet.data))->Character), NULL, NULL, 0);
413 break;
414 }
415 case PLAYER_DATA:
416 if(1) { //haxhaxhax
417 player_data* pd = &packet.player_data;
418 uint16_t i = pd->PlayerNum;
419 GameInput * Active_Input;
420 pd = (void*)packet.data;
421
422 //DDrConsole_PrintF("Got data for Player %i, %x", i, PlayerList[i]);
423 if (i > max_connections) break;
424 if( !PlayerList[i] ) break;
425 //PlayerList[i]->Chr = ((GameState *)ONgGameState)->CharacterStorage;
426
427 PlayerList[i]->Actions1 = pd->Inputs.Actions1;
428 PlayerList[i]->Actions2 = pd->Inputs.Actions2;
429 PlayerList[i]->MouseDeltaX = pd->Inputs.MouseDeltaX;
430 PlayerList[i]->MouseDeltaY = pd->Inputs.MouseDeltaY;
431 memcpy( &(PlayerList[i]->player_data), pd, sizeof(player_data) );
432
433 if( !server_started && pd->rare_sync_index > PlayerList[i]->rare_sync_index )
434 {
435 int sent_bytes;
436 flatline_packet sync_request = {0};
437 sync_request.id = RARE_SYNC_DATA_REQUEST;
438 sync_request.sync_request = i;
439 DDrConsole_PrintF( "Requesting sync data for player %i, old index %u", i, PlayerList[i]->rare_sync_index);
440 sent_bytes = NetUDPSocket_Send(client_sock,(sockaddr *) &address, (char*)&sync_request, FLATLINE_HEADER + sizeof(int) );
441 }
442
443 PlayerList[i]->DataApplied = 0;
444
445
446 break;
447 }
448 case RARE_SYNC_DATA:
449 if(1) {
450 sl_arg hax[2];
451 int dontuse;
452 uint16_t i = packet.rare_sync_data.PlayerNum;
453
454 if (i > max_connections) break;
455 if( !PlayerList[i] ) break;
456
457 //WEAPONS ARE DISABLED. Why? Pain in the arse to sync.
458 packet.rare_sync_data.Inventory.Weapons[0] = NULL;
459 packet.rare_sync_data.Inventory.Weapons[1] = NULL;
460 packet.rare_sync_data.Inventory.Weapons[2] = NULL;
461 // TMrInstance_GetDataPtr( 'ONCC', packet.rare_sync_data.Class, PlayerList[ i ]->Chr->ONCC );
462
463 //add the target character
464 hax[0].type = sl_int32;
465 hax[0].value_int32 = PlayerList[ packet.rare_sync_data.PlayerNum ]->spawnnumber;
466
467 //add the new class
468 //fix this later so we cant buffer overflow :O
469 hax[1].type = sl_str32;
470 hax[1].value_str32 = packet.rare_sync_data.Class;
471
472 //we are directly calling a bsl function instead of using the normal method for two reasons
473 //1. it has all the checking built in
474 iSetCharacterClass( 0, 2, hax, &dontuse, &dontuse, hax );
475 //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);
476 memcpy( &(PlayerList[ i ]->Chr->Inventory), &(packet.rare_sync_data.Inventory), sizeof(Inventory ));
477
478 PlayerList[i]->rare_sync_index = packet.rare_sync_data.index;
479 }
480 break;
481 case FLATLINE_EVENT:
482 FLcEventHandler( packet.flatline_event.event_index, packet.flatline_event.intArray );
483 break;
484 case PK_PING:
485 packet.id = PK_PONG;
486
487 NetUDPSocket_Send(client_sock, (sockaddr *) &address, (char*)&packet, FLATLINE_HEADER + 4);
488 break;
489 default:
490 DDrConsole_PrintF("Warning, recieved badly formed packet!");
491 break;
492 }
493 }
494 else {
495 Sleep(1);
496 }
497 }
498 }
499 return true;
500}
501
502//wtf, this needs cleaned up...
503player_info *FLr_FindEmptySlot() {
504 int j;
505 for(j = 0; j < MAX_PLAYERS; j++) {
506 if (Players[j].ip == 0) {
507 return &Players[j];
508 }
509 }
510 return 0;
511}
512
513extern uint16_t max_connections;
514uint16_t FLr_FindEmptyListSlot() {
515 int j;
516 for(j = 0; j < max_connections; j++) {
517 if (PlayerList[j] == 0) {
518 return j;
519 }
520 }
521 return -1;
522}
523typedef struct
524{
525 uint16_t x;
526 uint16_t y;
527
528} IMtPoint2D;
529static flatline_packet cache_input = {0};
530extern void* TSrTest;
531void * ONICALL FLrInput_Update_Keys(void)
532{
533 uint16_t i;
534
535 ActiveCharacter * Active_Player = ONgGameState->ActiveCharacters;
536 Active_Player->PhyContext->Rotation;
537 Active_Player->PhyContext->Position;
538 ONgGameState->PlayerCharacter->Position;
539
540 if(client_connected)
541 {
542 int sent_bytes;
543 flatline_packet input_packet = {0};
544 input_packet.id = PLAYER_INPUT;
545 input_packet.input_struct.Time = ONgGameState->GameTime;
546 input_packet.input_struct.Actions1 = ((GameState*)(ONgGameState))->Input.Current.Actions1;
547 input_packet.input_struct.Actions2 = ((GameState*)(ONgGameState))->Input.Current.Actions2;
548 input_packet.input_struct.MouseDeltaX = ((GameState*)(ONgGameState))->Input.MouseDeltaX;
549 input_packet.input_struct.MouseDeltaY = ((GameState*)(ONgGameState))->Input.MouseDeltaY;
550
551 sent_bytes = NetUDPSocket_Send(client_sock,(sockaddr *) &address, (char*)&input_packet, sizeof(input_struct) + FLATLINE_HEADER);
552
553 //return ONgGameState;
554 }
555
556
557 if(!(server_started || client_connected)) return ONgGameState;
558
559 if(server_started && ONgGameState->GameTime % 120 == 0)
560 {
561 FLsPingAll();
562 }
563
564 for(i = 0; i < max_connections; i++) {
565 ActiveCharacter * Active_Player;
566 Character* Player;
567 GameInput * Active_Input;
568 if(PlayerList[i] == 0) continue;
569
570
571 Player = PlayerList[i]->Chr;
572 if( Player->Health == 0)
573 {
574 int Actions;
575 const short TicksToRespawn = 3 * 60;
576 if(PlayerList[i]->state != STATE_DEAD)
577 {
578 PlayerList[i]->state = STATE_DEAD;
579 PlayerList[i]->DeathTime = ONgGameState->GameTime;
580 if(i == client_slot)
581 {
582 ONrGameState_Timer_Start( "", TicksToRespawn );
583 }
584 }
585
586 if(server_started & i == 0)
587 {
588 Actions = ONgGameState->Input.Current.Actions1;
589 }
590 else
591 {
592 Actions = PlayerList[i]->Actions1;
593 }
594
595 if(PlayerList[i]->flags & PF_SCRIPTEDAI)
596 {
597 FLrPlayerDisconnect( i );
598 }
599 else if(ONgGameState->GameTime - PlayerList[i]->DeathTime < TicksToRespawn )
600 {
601 continue;
602 }
603 else if(Actions & (Action_Punch | Action_Kick))
604 {
605 FLrPlayerRespawn( i );
606 }
607 continue;
608
609
610 }
611
612 Active_Player = ONrGetActiveCharacter( PlayerList[i]->Chr);
613
614 if(Active_Player == 0) continue;
615 Active_Input = &(Active_Player->Input);
616 if(server_started) {
617 player_data * data;
618 flatline_packet data_out = {0};
619
620 if(!strcmp(PlayerList[i]->player_data.Animation, TMrInstance_GetInstanceName(Active_Player->Animation)))
621 continue;
622
623 data_out.id = PLAYER_DATA;
624 data = (void*)&(data_out.data);
625 data->PlayerNum = i;
626 data->Health = PlayerList[i]->Chr->Health;
627 data->MaxHealth = PlayerList[i]->Chr->MaxHealth;
628 data->Position = PlayerList[i]->Chr->Position;
629 data->Facing = PlayerList[i]->Chr->Facing;
630 data->DesiredFacing = PlayerList[i]->Chr->DesiredFacing;
631 data->Position = Active_Player->PhyContext->Position;
632 memcpy(data->Animation, TMrInstance_GetInstanceName(Active_Player->Animation), 31);
633 data->Frame = Active_Player->Frame;
634
635 if(Active_Player->targetThrow)
636 {
637 data->throw_data.throwing = Players[Active_Player->throwing].list_slot;
638 memcpy(data->throw_data.throwName, TMrInstance_GetInstanceName(Active_Player->targetThrow), 31);
639 data->throw_data.throwFrame = ONrGetActiveCharacter(Active_Player->targetThrow)->Frame;
640 }
641
642
643 if( PlayerList[i]->OldClass != PlayerList[i]->Chr->ONCC || memcmp( &(PlayerList[i]->Inventory), &(PlayerList[i]->Chr->Inventory), sizeof(Inventory) ) )
644 {
645 PlayerList[i]->OldClass = PlayerList[i]->Chr->ONCC;
646 memcpy( &(PlayerList[i]->Inventory), &(PlayerList[i]->Chr->Inventory), sizeof(Inventory) );
647 PlayerList[i]->rare_sync_index++;
648
649 }
650
651 data->rare_sync_index = PlayerList[i]->rare_sync_index;
652
653
654 if(i == 0) {
655 data->Inputs.Actions1 = ((GameState*)(ONgGameState))->Input.Current.Actions1;
656 data->Inputs.Actions2 = ((GameState*)(ONgGameState))->Input.Current.Actions2;
657 data->Inputs.MouseDeltaX = ONgGameState->Input.MouseDeltaX;
658 data->Inputs.MouseDeltaY = ONgGameState->Input.MouseDeltaY;
659 }
660 else{
661 data->Inputs.Actions1 = PlayerList[i]->Actions1;
662 data->Inputs.Actions2 = PlayerList[i]->Actions2;
663 data->Inputs.MouseDeltaX = PlayerList[i]->MouseDeltaX;
664 data->Inputs.MouseDeltaY = PlayerList[i]->MouseDeltaY;
665 }
666 data->Ping = PlayerList[i]->Ping;
667 memcpy( &(PlayerList[i]->player_data), data, sizeof(player_data) );
668
669 UDPServer_SendToAll(&data_out, sizeof(player_data) + FLATLINE_HEADER);
670 }
671
672 if( (server_started && i !=0) || (!server_started/* && i != client_slot*/) )
673 {
674 Active_Input->Stop.Actions1 = ~PlayerList[i]->Actions1 & Active_Input->Current.Actions1;
675 Active_Input->Stop.Actions2 = ~PlayerList[i]->Actions2 & Active_Input->Current.Actions2;
676 Active_Input->Start.Actions1 = ~Active_Input->Current.Actions1 & PlayerList[i]->Actions1;
677 Active_Input->Start.Actions2 = ~Active_Input->Current.Actions2 & PlayerList[i]->Actions2;
678
679 Active_Input->Current.Actions1 = PlayerList[i]->Actions1;
680 Active_Input->Current.Actions2 = PlayerList[i]->Actions2;
681 Active_Input->Stopped.Actions1 = ~Active_Input->Current.Actions1;
682 Active_Input->Stopped.Actions2 = ~Active_Input->Current.Actions2;
683 Active_Input->MouseDeltaX = PlayerList[i]->MouseDeltaX;
684 Active_Input->MouseDeltaY = PlayerList[i]->MouseDeltaY;
685
686
687 if( !server_started && PlayerList[i]->player_data.Health != 0) {
688 void* OldAnimation;
689 void* Animation;
690 player_data* pd = &PlayerList[i]->player_data;
691
692 if(PlayerList[i]->DataApplied) continue;
693
694 Player->Health = PlayerList[i]->player_data.Health;
695 PlayerList[i]->Chr->MaxHealth = PlayerList[i]->player_data.MaxHealth;
696
697 PlayerList[i]->Chr->Facing = PlayerList[i]->player_data.Facing;
698 PlayerList[i]->Chr->DesiredFacing = PlayerList[i]->player_data.DesiredFacing;
699
700 PlayerList[i]->Actions1 = PlayerList[i]->player_data.Inputs.Actions1;
701 PlayerList[i]->Actions2 = PlayerList[i]->player_data.Inputs.Actions2;
702 PlayerList[i]->MouseDeltaX = PlayerList[i]->player_data.Inputs.MouseDeltaX;
703 PlayerList[i]->MouseDeltaY = PlayerList[i]->player_data.Inputs.MouseDeltaY;
704 Active_Player->PhyContext->Position = PlayerList[i]->player_data.Position;
705 OldAnimation = Active_Player->Animation;
706
707 if (!(Player->Flags & ONcCharacterFlag_BeingThrown) &&
708 (pd->Animation != 0))
709 {
710 // get a pointer to the animation
711
712 TMrInstance_GetDataPtr(
713 'TRAM',
714 pd->Animation,
715 &Animation);
716 if (Animation != OldAnimation)
717 {
718 short num_frames;
719 bool updateAnimation = true;
720
721 // if the character is dead, make sure this animation is appropriate for death
722 /*
723 if (Player->Flags & ONcCharacterFlag_Dead)
724 {
725 short curToState = TRrAnimation_GetTo(Active_Player->Animation);
726 short newToState = TRrAnimation_GetTo(animation);
727
728 // if we are currently heading towards fallen and the new animation would not
729 // then this is a better animation to run when we are dead
730 if ((ONrAnimState_IsFallen(curToState)) &&
731 (!ONrAnimState_IsFallen(newToState)))
732 {
733 updateAnimation = false;
734 }
735 }
736 */
737 if ((updateAnimation) && (Active_Player->Animation == Animation))
738 {
739 int oldFrame = Active_Player->Frame;
740 int newFrame = pd->Frame;
741
742 if (abs(oldFrame - newFrame) < 2)
743 {
744 updateAnimation = false;
745 }
746 }
747
748 if (updateAnimation)
749 {
750 // set the characters animation
751 /* ONrCharacter_SetAnimationInternal(Player,
752 Active_Player,
753 Active_Player->AnimationToState,
754 0,
755 Animation);
756 ONrCharacter_NewAnimationHook(Player, Active_Player);*/
757 //Player->Flags |= 0x00000010;
758 ONrCharacter_SetAnimationExternal(Player, TRrAnimation_GetFrom(Animation), Animation, 0);
759 }
760
761 num_frames = TRrAnimation_GetDuration(Active_Player->Animation);
762
763 if (pd->Frame == num_frames)
764 {
765 Active_Player->Frame = num_frames - 1;
766 //Active_Player->Frame = 0;
767 }
768 else
769 {
770 Active_Player->Frame = pd->Frame;
771 }
772 }
773 }
774
775 if (PlayerList[i]->player_data.throw_data.throwName[0] != 0)
776 {
777 short throwTarget = PlayerList[pd->throw_data.throwing]->spawnnumber;
778 if ((throwTarget != Active_Player->throwing) &&
779 (pd->throw_data.throwFrame < 10))
780 {
781 void *throw_animation;
782 ActiveCharacter* Target;
783 // get the animation
784
785 TMrInstance_GetDataPtr(
786 'TRAM',
787 pd->throw_data.throwName,
788 &throw_animation);
789 //if (error) return;
790
791 // set the throw target
792 Active_Player->ThrowTargetCharacter = &ONgGameState->CharacterStorage[throwTarget];
793 Target = ONrGetActiveCharacter(Active_Player->ThrowTargetCharacter);
794 if ((Target->Animation != throw_animation) &&
795 (OldAnimation != Animation) &&
796 !(Active_Player->ThrowTargetCharacter->Flags & ONcCharacterFlag_BeingThrown))
797 {
798 // set the throw variables
799 Active_Player->targetThrow = throw_animation;
800 Active_Player->throwing = throwTarget;
801
802 // run the throw
803 ONrCharacter_NewAnimationHook(Player, Active_Player);
804
805 if (Active_Player->ThrowTargetCharacter)
806 {
807 Target->Frame += 2;
808 Target->thrownBy = Player->Number;
809 }
810 }
811 }
812 }
813 PlayerList[i]->DataApplied = 1;
814
815 }
816 }
817 }
818
819 return ONgGameState;
820}
821
822void FLrPlayerDisconnect( int Player )
823{
824 if(server_started)
825 {
826 //FLsPublic_Event(EV_DISCONNECT, &Player );
827 }
828 //Kill off the character in another function, please
829 //ONrCharacter_SetHitPoints( PlayerList[Player]->Chr, 0);
830
831 memset(PlayerList[Player], 0, sizeof(player_info));
832 PlayerList[Player] = 0;
833
834
835
836 return;
837}
838
839void FLrPlayerRespawn( int Player )
840{
841 PlayerList[Player]->state = STATE_ALIVE;
842 ONrCorpse_Create(PlayerList[Player]->Chr);
843 ONrCharacter_SetHitPoints( PlayerList[Player]->Chr, PlayerList[Player]->Chr->MaxHealth );
844}
845
846
847void* ScoreboardInstance = 0;
848void FLrRun_Scores()
849{
850 if(client_connected || server_started)
851 {
852 if(!ScoreboardInstance){
853 void* TSFFTahoma;
854 TMrInstance_GetDataPtr( 'TSFF', "Tahoma", &TSFFTahoma);
855 TSrContext_New( TSFFTahoma, 7, 1, 1, 0, &ScoreboardInstance);
856 }
857 if(ScoreboardInstance){
858 const int white = 0x00FFFFFF;
859 const int green = 0x0000FF00;
860 const int red = 0x00FF0000;
861 int i;
862 char DrawString[255];
863 const int LineHeight = 15;
864 IMtPoint2D DrawLocation = {20, 20};
865 TSrContext_SetShade(ScoreboardInstance, white);
866 TSrContext_DrawText(ScoreboardInstance, "Oni Flatline build " __DATE__ " " __TIME__, 255, 0, &DrawLocation);
867 DrawLocation.y += LineHeight;
868 TSrContext_DrawText(ScoreboardInstance, "Name", 255, 0, &DrawLocation);
869 DrawLocation.x += 150;
870 TSrContext_DrawText(ScoreboardInstance, "Score", 255, 0, &DrawLocation);
871 DrawLocation.x += 50;
872 TSrContext_DrawText(ScoreboardInstance, "Ping", 255, 0, &DrawLocation);
873 for(i = 0; i <MAX_PLAYERS; i++)
874 {
875 if(PlayerList[i] == 0 || PlayerList[i]->Chr == 0) continue;
876
877 DrawLocation.x = 20;
878 DrawLocation.y += LineHeight;
879
880 if(PlayerList[i]->Chr && PlayerList[i]->Chr->Health == 0)
881 {
882 TSrContext_SetShade(ScoreboardInstance, red);
883 }
884 else if (i == client_slot)
885 {
886 TSrContext_SetShade(ScoreboardInstance, green);
887 }
888 TSrContext_DrawText(ScoreboardInstance, PlayerList[i]->Chr->Name, 255, 0, &DrawLocation);
889 TSrContext_SetShade(ScoreboardInstance, white);
890 DrawLocation.x += 150;
891 sprintf(DrawString, "%i", PlayerList[i]->Chr->Damage);
892 TSrContext_DrawText(ScoreboardInstance, DrawString, 255, 0, &DrawLocation);
893 DrawLocation.x += 50;
894 sprintf(DrawString, "%i", PlayerList[i]->player_data.Ping);
895 TSrContext_DrawText(ScoreboardInstance, DrawString, 255, 0, &DrawLocation);
896 }
897 }
898 }
899}
Note: See TracBrowser for help on using the repository browser.