source: Daodan/MSVC/Flatline_Client.c@ 587

Last change on this file since 587 was 586, checked in by gumby, 14 years ago

stuff

File size: 5.8 KB
RevLine 
[584]1#include "Flatline.h"
2#include "Flatline_Client.h"
3
4int client_slot = 0;
5RGBA green = {0, 0xFF, 0, 0};
6RGBA red = {0, 0, 0xFF, 0};
7RGBA grey = {0x80,0x80,0x80,0x80};
8
9
10int FLcEventHandler( int eventIndex, int args[] )
11{
12 switch(eventIndex)
13 {
14 case(EV_DISCONNECT):
15 FLrPlayerDisconnect( args[0] );
16 break;
17 case(EV_KILLED):
18 ONrCharacter_SetHitPoints( PlayerList[args[0]]->Chr, 0);
19 case(EV_DOOR_OPEN):
20 OBJrDoor_ForceOpen( args[0] );
21 break;
22 case(EV_CONSOLE_USE):
23 OBJrConsole_OnActivate( OBJrConsole_GetByID(args[0]), PlayerList[args[1]]->Chr );
24 break;
[586]25 case(EV_RESPAWN):
26 ONrCorpse_Create(PlayerList[args[0]]->Chr);
27 break;
[584]28 default:
29 break;
30 }
31 return 0;
32}
33
34
35bool FLrClient_Run(flatline_packet* packet)
36{
37
38 char data[1400];
39 uint16_t len;
40 int j;
41 int sent_bytes;
42 client_connected = 0;
43
44
45 //starts the connection
46 DDrConsole_PrintF("Connecting to server %s on socket %i", inet_ntoa(address.sin_addr), client_sock);
[586]47 sent_bytes = NetUDPSocket_Send(client_sock, (sockaddr*)&address, (char*)packet, FLATLINE_HEADER + sizeof(connect_send) );
[584]48 if(sent_bytes == SOCKET_ERROR) {
49 NetCatchError();
50 }
51 //loops once per second waiting for a reply.
52 for(j = 0; j < CONNECTION_TIMEOUT; j++) {
53 while(NetUDPSocket_Recieve(client_sock, (sockaddr_storage *) &client_address, data, &len)){
54 packet = (flatline_packet*)data;
55 if(packet->id == CONNECT_REPLY) {
56 if(packet->connect_reply.goodtogo){
57
58 client_connected = 1;
59
60 client_slot = ((connect_reply*)packet->data)->player_slot;
61
62 PlayerList[client_slot] = Players+client_slot;
63 PlayerList[client_slot]->Chr = ONgGameState->PlayerCharacter;
64
65 DDrConsole_PrintColored("Connection successful!",0,green, grey);
66
67 sprintf_s( PlayerList[client_slot]->name, 32, "%s", player_name );
68
69 //disable local input.
70 DDrPatch_NOOP(0x004FA929, 5 + 6 + 5);
71
72 //Disable local turning
73 //DDrPatch_NOOP(0x004F7EA8, 2);
74 //DDrPatch_Byte( 0x004F7EB1 , 0xE9);
75 //DDrPatch_MakeJump( 0x004F7EB1, 0x004F8030 );
76
77
[586]78 //DDrPatch_Byte(0x04ED6FB, 0xEB);
[584]79
80 //DDrConsole_PrintF("Slot %i", ((connect_reply*)packet)->player_slot);
81 //DDrPatch_NOOP(0x43B23,0x10);
82 //DDrPatch_NOOP(0x4EC248,(0x5A-0x48));
83 //DDrPatch_NOOP(0x4EC861, 6);
84 break;
85 }
86 else {
87 DDrConsole_PrintF("Connection rejected: %s", ((connect_reply*)packet->data)->message);
88 return false;
89 break;
90 }
91 }
92 }
93 if(client_connected) break;
94 DDrConsole_PrintF("Connection timing out in %i seconds...", CONNECTION_TIMEOUT - j);
95 Sleep(1000);
96 }
97 //the client timed out without recieving an error message.
98 if(!client_connected) {
99 DDrConsole_PrintColored("Connection timed out.",0,red, grey);
100 return false;
101 }
102
103 return true;
104}
105
106
107void FLrClient_GetPackets()
108{
109 flatline_packet packet;
110 uint16_t len;
111 //#define SPAM_INPUT
112#ifdef SPAM_INPUT
113 struct timeval lasttime;
114 struct timeval thistime;
115 gettimeofday(&lasttime, 0);
116#endif
117
118
119 while(NetUDPSocket_Recieve(client_sock, (sockaddr_storage *) &client_address, &packet, &len)) {
120 //packet = (flatline_packet*)data;
121 //DDrConsole_PrintF("Data recieved, length %i, type %i", len, ((flatline_packet*)data)->id);
122 switch(packet.id) {
123 case MESSAGE:
124 COrMessage_Print(packet.data, "chat", 0);
125 break;
126 case CHANGE_NAME:
127 if(PlayerList[(char)packet.data[0]])
128 {
129 char message_buffer[1024];
130 sprintf(message_buffer,"%s changed their name to %s", PlayerList[(char)packet.data[0]]->name, packet.data + 1);
131 COrMessage_Print(message_buffer, "name_change", 0);
132
133 sprintf_s(PlayerList[packet.data[0]]->name, 32, "%s", packet.data + 1);
134
135 }
136 break;
137 case CONNECT_SEND:
138 ;if(1) {
139 flatline_packet connect_recv;
140 memcpy(&connect_recv.connect_reply.message,"This isn't a server!", sizeof("This isn't a server!"));
141 NetUDPSocket_Send(client_sock, (sockaddr *) &address, (char*)&connect_recv, sizeof(bool) + FLATLINE_HEADER + sizeof("This isn't a server!"));
142 }
143 case CONNECT_REPLY:
144 break; //extra packet or something.
145 case NEW_PLAYER:
146 ;if(1) { //haxhaxhax
147 CharacterObject* Char = &(packet.new_player.Character);
148 uint32_t chr_index = 0;
149 Character* PC;
150 DDrConsole_PrintF("%i | %i", packet.new_player.Playernumber ,client_slot);
151 //Char->OSD.Options = 0;
152 if(packet.new_player.Playernumber == client_slot) {
153 PlayerList[packet.new_player.Playernumber] = &Players[0];
154 PC = (ONgGameState->PlayerCharacter);
155 Players[0].Chr = PC;
156
157 }
158 else {
159 ONrGameState_NewCharacter(Char, NULL, NULL, &chr_index);
160 ONgGameState->CharacterStorage[chr_index].charType = 0;
161 PlayerList[packet.new_player.Playernumber] = &Players[chr_index];
162 Players[chr_index].Chr = &(ONgGameState->CharacterStorage[chr_index]);
163 Players[chr_index].Chr->Flags &= 0xFFBFFFFF;
164 Players[chr_index].spawnnumber = chr_index;
165 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) ;
166 sprintf_s(Players[chr_index].name, 32, "%s", ((new_player*)(packet.data))->Character.OSD.Name);
167 }
168 //Players[((new_player*)(packet.data))->Playernumber].spawnnumber = ONrGameState_NewCharacter(&(((new_player*)(packet.data))->Character), NULL, NULL, 0);
169 break;
170 }
171
172 case FLATLINE_EVENT:
173 FLcEventHandler( packet.flatline_event.event_index, packet.flatline_event.intArray );
174 break;
175 case PK_PING:
176 packet.id = PK_PONG;
177 NetUDPSocket_Send(client_sock, (sockaddr *) &address, (char*)&packet, FLATLINE_HEADER + 4);
178 break;
179 case PK_PLAYER_DATA:
180 FLcReadPlayerData( &packet, len );
181 break;
182 default:
183 DDrConsole_PrintF("Warning, recieved badly formed packet!");
184 break;
185 }
186 }
187}
188
Note: See TracBrowser for help on using the repository browser.