source: Daodan/MSVC/Flatline_PacketBuilder.c@ 584

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

Almost working rework

File size: 4.6 KB
Line 
1#include "Flatline.h"
2#include <assert.h>
3
4#define FLAG_AND_INCREMENT( FLAG ) PD->UpdateFlags |= (1 << FLAG ); DataPointer += FLpData_PartSize( FLAG );
5
6void FLsPacketBuild( uint8_t p, PlayerData* PD )
7{
8 Character* Player = PlayerList[p]->Chr;
9 ActiveCharacter* APlayer = ONrGetActiveCharacter(Player);
10 player_info * PI = PlayerList[p];
11 uint8_t * DataPointer = PD->data;
12 PD->ID = p;
13
14 //if ( data has changed )
15 //{
16 // copy it to the buffer
17 // copy it to the player info
18 // set the changed flag
19 // increment the buffer pointer
20 //}
21
22 /*
23 //Could probably send this every frame, but afk players can save a few bytes, eh?
24 if( PI->Input.Actions1 != PI->InputFromClient.Actions1 ||
25 PI->Input.Actions2 != PI->InputFromClient.Actions2 ||
26 PI->Input.MouseDeltaX != PI->InputFromClient.MouseDeltaX ||
27 PI->Input.MouseDeltaY != PI->InputFromClient.MouseDeltaY )
28 {*/
29
30 //Better in case of dropped packets to send input every frame
31 if(1)
32 {
33 memcpy( DataPointer, &PI->InputFromClient, sizeof(PlayerInput));
34 PI->InputFromClient = PI->Input;
35
36 FLAG_AND_INCREMENT( PFlag_Input );
37 }
38
39 if( PI->Facings.Facing != Player->Facing ||
40 PI->Facings.DesiredFacing != Player->DesiredFacing )
41 {
42 PlayerFacing* ptr = (void*)DataPointer;
43 ptr->Facing = Player->Facing;
44 ptr->DesiredFacing = Player->DesiredFacing;
45
46 PI->Facings = *ptr;
47
48 FLAG_AND_INCREMENT( PFlag_Facing );
49 }
50
51 if(PI->Health.Health != Player->Health ||
52 PI->Health.MaxHealth != Player->MaxHealth )
53 {
54 PlayerHealth* ptr = (void*)DataPointer;
55 ptr->Health = Player->Health;
56 ptr->MaxHealth = Player->MaxHealth;
57
58 PI->Health = *ptr;
59
60 FLAG_AND_INCREMENT( PFlag_Health );
61 }
62
63 //Score
64 //skipping for now
65
66 //Frame and ping can be sent every frame, i guess. Improve this later,
67 if( 1 )
68 {
69 PlayerFP* ptr = (void*)DataPointer;
70 if(APlayer)
71 {
72 ptr->Frame = APlayer->Frame;
73 }
74 else
75 {
76 ptr->Frame = -1;
77 }
78 ptr->Ping = PI->Ping;
79
80 PI->Frame = ptr->Frame;
81
82 FLAG_AND_INCREMENT( PFlag_FramePing );
83 }
84
85 //Skipping inventory because we dont need it right now
86 if( 0 )
87 {
88 //Do inventory
89 }
90
91 if( PI->Class != Player->ONCC )
92 {
93 sprintf_s( DataPointer, 32, "%s", TMrInstance_GetInstanceName( Player->ONCC ) );
94 PI->Class = Player->ONCC;
95
96 FLAG_AND_INCREMENT( PFlag_Class );
97 }
98
99 if(APlayer)
100 {
101 if( memcmp(&PI->Position, &APlayer->PhyContext->Position, sizeof(Vector3)) )
102 {
103 Vector3* ptr = (Vector3*)DataPointer;
104 *ptr = PI->Position = APlayer->PhyContext->Position;
105
106 FLAG_AND_INCREMENT( PFlag_Position );
107 }
108
109 if(APlayer->Animation != PI->Animation)
110 {
111 sprintf_s( DataPointer, 32, "%s", TMrInstance_GetInstanceName( PI->Animation ) );
112 PI->Animation = APlayer->Animation;
113
114 FLAG_AND_INCREMENT( PFlag_Animation );
115 }
116
117 if(APlayer->targetThrow)
118 {
119 if(!PI->HasAppliedThrow)
120 {
121 PlayerThrowData* ptr = (void*)DataPointer;
122 ptr->throwing = Players[APlayer->throwing].list_slot;
123 memcpy(ptr->throwName, TMrInstance_GetInstanceName(APlayer->targetThrow), 31);
124 ptr->throwFrame = ONrGetActiveCharacter(APlayer->targetThrow)->Frame;
125
126 PI->ThrowData = *ptr;
127
128 PI->HasAppliedThrow = 1;
129
130 FLAG_AND_INCREMENT( PFlag_Throws );
131 }
132 }
133 else
134 {
135 PI->HasAppliedThrow = 0;
136 }
137 }
138
139 PD->Size = (char)DataPointer - (char)PD;
140
141 PI->UpdateFlags = PD->UpdateFlags;
142
143}
144
145
146
147void FLsSendPlayerData()
148{
149 PlayerData BuildData[32] = {0};
150 uint8_t p = 0;
151 uint32_t PacketSize = 0;
152 flatline_packet OutputPacket = {0};
153 uint8_t* OutputPointer = OutputPacket.data;
154 //Most of the time we won't even hit this. Could probably be bumped up a few hundred bytes
155 const uint32_t max_packet_size = 1000;
156
157 //Prepare buffers
158 OutputPacket.id = PK_PLAYER_DATA;
159 memset( BuildData, 0, sizeof(PlayerData) * 32 );
160
161 //Build data
162 for(p = 0; p < MAX_PLAYERS; p++)
163 {
164 if(PlayerList[p] && PlayerList[p]->Chr)
165 {
166 FLsPacketBuild(p, &BuildData[p]);
167
168 assert( BuildData[p].Size < 255 );
169
170 //If we hit maximum size, send the packet and reset the buffer for a new one
171 if( BuildData[p].Size + PacketSize > max_packet_size )
172 {
173 UDPServer_SendToAll(&OutputPacket, PacketSize + FLATLINE_HEADER);
174
175 memset( OutputPacket.data, 0, PacketSize );
176 OutputPointer = OutputPacket.data;
177 PacketSize = 0;
178 }
179
180 //add to the packet
181 memcpy( OutputPointer, &BuildData[p], BuildData[p].Size );
182
183 OutputPointer += BuildData[p].Size;
184 PacketSize += BuildData[p].Size;
185 }
186 }
187 //Send data
188 if( PacketSize > 0 )
189 {
190 UDPServer_SendToAll(&OutputPacket, PacketSize + FLATLINE_HEADER);
191 }
192}
Note: See TracBrowser for help on using the repository browser.