source: Daodan/MSVC/Flatline_PacketBuilder.c@ 754

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

stuff

File size: 4.9 KB
RevLine 
[584]1#include "Flatline.h"
2#include <assert.h>
3
4#define FLAG_AND_INCREMENT( FLAG ) PD->UpdateFlags |= (1 << FLAG ); DataPointer += FLpData_PartSize( FLAG );
5
[586]6void FLsPacketBuild( uint8_t p, PlayerData* PD, bool UpdateAll )
[584]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));
[585]34 PI->Input = PI->InputFromClient;
[584]35
36 FLAG_AND_INCREMENT( PFlag_Input );
37 }
38
39 if( PI->Facings.Facing != Player->Facing ||
[586]40 PI->Facings.DesiredFacing != Player->DesiredFacing || UpdateAll )
[584]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 ||
[586]52 PI->Health.MaxHealth != Player->MaxHealth || UpdateAll )
[584]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
[586]91 if( PI->Class != Player->ONCC || UpdateAll )
[584]92 {
93 sprintf_s( DataPointer, 32, "%s", TMrInstance_GetInstanceName( Player->ONCC ) );
[586]94 sprintf_s( PI->ClassString, 32, "%s", DataPointer );
[584]95 PI->Class = Player->ONCC;
96
97 FLAG_AND_INCREMENT( PFlag_Class );
98 }
99
100 if(APlayer)
101 {
[586]102 if( (APlayer->PhyContext) && (memcmp(&PI->Position, &APlayer->PhyContext->Position, sizeof(Vector3)) || UpdateAll ))
[584]103 {
104 Vector3* ptr = (Vector3*)DataPointer;
105 *ptr = PI->Position = APlayer->PhyContext->Position;
106
107 FLAG_AND_INCREMENT( PFlag_Position );
108 }
109
[586]110 if(APlayer->Animation != PI->Animation || !APlayer->Frame || UpdateAll )
[584]111 {
[585]112 sprintf_s( DataPointer, 32, "%s", TMrInstance_GetInstanceName( APlayer->Animation ) );
[586]113 sprintf_s( PI->AnimationString, 32, "%s", DataPointer );
[584]114 PI->Animation = APlayer->Animation;
[586]115
[584]116
117 FLAG_AND_INCREMENT( PFlag_Animation );
118 }
119
120 if(APlayer->targetThrow)
121 {
[586]122 if(!PI->HasAppliedThrow || UpdateAll )
[584]123 {
124 PlayerThrowData* ptr = (void*)DataPointer;
125 ptr->throwing = Players[APlayer->throwing].list_slot;
126 memcpy(ptr->throwName, TMrInstance_GetInstanceName(APlayer->targetThrow), 31);
127 ptr->throwFrame = ONrGetActiveCharacter(APlayer->targetThrow)->Frame;
128
129 PI->ThrowData = *ptr;
130
131 PI->HasAppliedThrow = 1;
132
133 FLAG_AND_INCREMENT( PFlag_Throws );
134 }
135 }
136 else
137 {
138 PI->HasAppliedThrow = 0;
139 }
140 }
141
[585]142 PD->Size = (uint32_t)DataPointer - (uint32_t)PD;
[584]143
144 PI->UpdateFlags = PD->UpdateFlags;
145
146}
147
148
149
150void FLsSendPlayerData()
151{
152 PlayerData BuildData[32] = {0};
153 uint8_t p = 0;
154 uint32_t PacketSize = 0;
155 flatline_packet OutputPacket = {0};
156 uint8_t* OutputPointer = OutputPacket.data;
157 //Most of the time we won't even hit this. Could probably be bumped up a few hundred bytes
158 const uint32_t max_packet_size = 1000;
159
160 //Prepare buffers
161 OutputPacket.id = PK_PLAYER_DATA;
162 memset( BuildData, 0, sizeof(PlayerData) * 32 );
163
164 //Build data
165 for(p = 0; p < MAX_PLAYERS; p++)
166 {
167 if(PlayerList[p] && PlayerList[p]->Chr)
168 {
[586]169 FLsPacketBuild(p, &BuildData[p], 0);
[584]170
171 assert( BuildData[p].Size < 255 );
172
[585]173 if( BuildData[p].Size > 0 )
[584]174 {
[585]175
176 //If we hit maximum size, send the packet and reset the buffer for a new one
177 if( BuildData[p].Size + PacketSize > max_packet_size )
178 {
179 UDPServer_SendToAll(&OutputPacket, PacketSize + FLATLINE_HEADER);
180
181 memset( OutputPacket.data, 0, PacketSize );
182 OutputPointer = OutputPacket.data;
183 PacketSize = 0;
184 }
185
186 //add to the packet
187 memcpy( OutputPointer, &BuildData[p], BuildData[p].Size );
188
189 OutputPointer += BuildData[p].Size;
190 PacketSize += BuildData[p].Size;
[584]191 }
192 }
193 }
194 //Send data
195 if( PacketSize > 0 )
196 {
197 UDPServer_SendToAll(&OutputPacket, PacketSize + FLATLINE_HEADER);
198 }
199}
Note: See TracBrowser for help on using the repository browser.