rendered paste body902a894,1106> *State_t communication> > =============================================================================> */> > // if (int)f == f and (int)f + ( 1<<(FLOAT_INT_BITS-1) ) < ( 1 << FLOAT_INT_BITS )> // the float will be sent with FLOAT_INT_BITS, otherwise all 32 bits will be sent> #define FLOAT_INT_BITS 13> #define FLOAT_INT_BIAS (1<<(FLOAT_INT_BITS-1))> > typedef struct> {> char *name;> int offset;> int bits; // 0 = float> } netField_t;> > /*> ============= > MSG_WriteDeltaField> =============> */> void MSG_WriteDeltaField(msg_t *msg, int *from, int *to, netField_t *field) {> int trunc;> float fullFloat;> > if (*from == *to)> {> MSG_WriteBits (msg, 0, 1); // no change> return;> }> > MSG_WriteBits (msg, 1, 1); // changed> > if (field->bits == 0)> {> // float> fullFloat = *(float *) to;> trunc = (int) fullFloat;> > if (fullFloat == 0.0f)> {> MSG_WriteBits (msg, 0, 1);> }> else> {> MSG_WriteBits (msg, 1, 1);> if (trunc == fullFloat && trunc + FLOAT_INT_BIAS >= 0 &&> trunc + FLOAT_INT_BIAS < (1 << FLOAT_INT_BITS))> {> // send as small integer> MSG_WriteBits (msg, 0, 1);> MSG_WriteBits (msg, trunc + FLOAT_INT_BIAS, FLOAT_INT_BITS);> }> else> {> // send as full floating point value> MSG_WriteBits (msg, 1, 1);> MSG_WriteBits (msg, *to, 32);> }> }> }> else> {> if (*to == 0)> {> MSG_WriteBits (msg, 0, 1);> }> else> {> MSG_WriteBits (msg, 1, 1);> // integer> MSG_WriteBits (msg, *to, field->bits);> }> }> }> > /*> ============= > MSG_ReadDeltaField> =============> */> void MSG_ReadDeltaField(msg_t *msg, int *from, int *to, netField_t *field, int print) {> int trunc;> > if (!MSG_ReadBits (msg, 1))> {> // no change> *to = *from;> }> else> {> if (field->bits == 0)> {> // float> if (MSG_ReadBits (msg, 1) == 0)> {> *(float *) to = 0.0f;> }> else> {> if (MSG_ReadBits (msg, 1) == 0)> {> // integral float> trunc = MSG_ReadBits (msg, FLOAT_INT_BITS);> // bias to allow equal parts positive and negative> trunc -= FLOAT_INT_BIAS;> *(float *) to = trunc;> if (print)> {> Com_Printf ("%s:%i ", field->name, trunc);> }> }> else> {> // full floating point value> *to = MSG_ReadBits (msg, 32);> if (print)> {> Com_Printf ("%s:%f ", field->name, *(float *) to);> }> }> }> }> else> {> if (MSG_ReadBits (msg, 1) == 0)> {> *to = 0;> }> else> {> // integer> *to = MSG_ReadBits (msg, field->bits);> if (print)> {> Com_Printf ("%s:%i ", field->name, *to);> }> }> }> }> }> > /*> ============= > MSG_WriteDeltaArray> =============> */> void MSG_WriteDeltaArray(msg_t *msg, int *from, int *to, int length) {> int i;> int *fromF, *toF;> netField_t intField = { NULL, 0, 32 };> //look for changes> for (i = 0; i < length; i++)> {> fromF = &from[i];> toF = &to[i];> if (*toF != *fromF)> {> break;> }> }> if (i == length)> {> //none changed> MSG_WriteBits(msg,0,1);> }> else> {> //something changed, delta update array> MSG_WriteBits(msg,1,1);> for (i = 0; i < length; i++)> {> fromF = &from[i];> toF = &to[i];> MSG_WriteDeltaField(msg, fromF, toF, &intField);> }> }> }> /*> ============= > MSG_ReadDeltaArray> =============> */> void MSG_ReadDeltaArray(msg_t *msg, int *from, int *to, int length, int print) {> int i;> netField_t intField = { "arrayField" , 0, 32 };> int *fromF, *toF;> if (!MSG_ReadBits(msg,1))> {> //None changed, copy array.> for (i = 0; i < length; i++)> {> fromF = &from[i];> toF = &to[i];> *toF = *fromF;> }> }> else> {> //Something changed, delta update array> for (i = 0; i < length; i++)> {> fromF = &from[i];> toF = &to[i];> MSG_ReadDeltaField(msg,fromF,toF,&intField,print);> }> }> }> > /*> =============================================================================> 928,933d1130< typedef struct< {< char *name;< int offset;< int bits; // 0 = float< } netField_t;992,997d1188< < // if (int)f == f and (int)f + ( 1<<(FLOAT_INT_BITS-1) ) < ( 1 << FLOAT_INT_BITS )< // the float will be sent with FLOAT_INT_BITS, otherwise all 32 bits will be sent< #define FLOAT_INT_BITS 13< #define FLOAT_INT_BIAS (1<<(FLOAT_INT_BITS-1))< 1014,1015d1204< int trunc;< float fullFloat;1081,1130c1268< < if(*fromF == *toF)< {< MSG_WriteBits(msg, 0, 1); // no change< continue;< }< < MSG_WriteBits(msg, 1, 1); // changed< < if(field->bits == 0)< {< // float< fullFloat = *(float *)toF;< trunc = (int)fullFloat;< < if(fullFloat == 0.0f)< {< MSG_WriteBits(msg, 0, 1);< oldsize += FLOAT_INT_BITS;< }< else< {< MSG_WriteBits(msg, 1, 1);< if(trunc == fullFloat && trunc + FLOAT_INT_BIAS >= 0 && trunc + FLOAT_INT_BIAS < (1 << FLOAT_INT_BITS))< {< // send as small integer< MSG_WriteBits(msg, 0, 1);< MSG_WriteBits(msg, trunc + FLOAT_INT_BIAS, FLOAT_INT_BITS);< }< else< {< // send as full floating point value< MSG_WriteBits(msg, 1, 1);< MSG_WriteBits(msg, *toF, 32);< }< }< }< else< {< if(*toF == 0)< {< MSG_WriteBits(msg, 0, 1);< }< else< {< MSG_WriteBits(msg, 1, 1);< // integer< MSG_WriteBits(msg, *toF, field->bits);< }< }---> MSG_WriteDeltaField(msg,fromF,toF,field);1155d1292< int trunc;1213,1270c1350< < if(!MSG_ReadBits(msg, 1))< {< // no change< *toF = *fromF;< }< else< {< if(field->bits == 0)< {< // float< if(MSG_ReadBits(msg, 1) == 0)< {< *(float *)toF = 0.0f;< }< else< {< if(MSG_ReadBits(msg, 1) == 0)< {< // integral float< trunc = MSG_ReadBits(msg, FLOAT_INT_BITS);< // bias to allow equal parts positive and negative< trunc -= FLOAT_INT_BIAS;< *(float *)toF = trunc;< if(print)< {< Com_Printf("%s:%i ", field->name, trunc);< }< }< else< {< // full floating point value< *toF = MSG_ReadBits(msg, 32);< if(print)< {< Com_Printf("%s:%f ", field->name, *(float *)toF);< }< }< }< }< else< {< if(MSG_ReadBits(msg, 1) == 0)< {< *toF = 0;< }< else< {< // integer< *toF = MSG_ReadBits(msg, field->bits);< if(print)< {< Com_Printf("%s:%i ", field->name, *toF);< }< }< }< // pcount[i]++;< }---> MSG_ReadDeltaField(msg,fromF,toF,field,print);1414,1417d1494< int statsbits;< int persistantbits;< int ammobits;< int powerupbits;1422,1423c1499< float fullFloat;< int trunc, lc;---> int lc;1454,1487c1528< < if(*fromF == *toF)< {< MSG_WriteBits(msg, 0, 1); // no change< continue;< }< < MSG_WriteBits(msg, 1, 1); // changed< // pcount[i]++;< < if(field->bits == 0)< {< // float< fullFloat = *(float *)toF;< trunc = (int)fullFloat;< < if(trunc == fullFloat && trunc + FLOAT_INT_BIAS >= 0 && trunc + FLOAT_INT_BIAS < (1 << FLOAT_INT_BITS))< {< // send as small integer< MSG_WriteBits(msg, 0, 1);< MSG_WriteBits(msg, trunc + FLOAT_INT_BIAS, FLOAT_INT_BITS);< }< else< {< // send as full floating point value< MSG_WriteBits(msg, 1, 1);< MSG_WriteBits(msg, *toF, 32);< }< }< else< {< // integer< MSG_WriteBits(msg, *toF, field->bits);< }---> MSG_WriteDeltaField(msg,fromF,toF,field);1495,1589c1535,1538< statsbits = 0;< for(i = 0; i < 16; i++)< {< if(to->stats[i] != from->stats[i])< {< statsbits |= 1 << i;< }< }< persistantbits = 0;< for(i = 0; i < 16; i++)< {< if(to->persistant[i] != from->persistant[i])< {< persistantbits |= 1 << i;< }< }< ammobits = 0;< for(i = 0; i < 16; i++)< {< if(to->ammo[i] != from->ammo[i])< {< ammobits |= 1 << i;< }< }< powerupbits = 0;< for(i = 0; i < 16; i++)< {< if(to->powerups[i] != from->powerups[i])< {< powerupbits |= 1 << i;< }< }< < if(!statsbits && !persistantbits && !ammobits && !powerupbits)< {< MSG_WriteBits(msg, 0, 1); // no change< oldsize += 4;< return;< }< MSG_WriteBits(msg, 1, 1); // changed< < if(statsbits)< {< MSG_WriteBits(msg, 1, 1); // changed< MSG_WriteShort(msg, statsbits);< for(i = 0; i < 16; i++)< if(statsbits & (1 << i))< MSG_WriteShort(msg, to->stats[i]);< }< else< {< MSG_WriteBits(msg, 0, 1); // no change< }< < < if(persistantbits)< {< MSG_WriteBits(msg, 1, 1); // changed< MSG_WriteShort(msg, persistantbits);< for(i = 0; i < 16; i++)< if(persistantbits & (1 << i))< MSG_WriteShort(msg, to->persistant[i]);< }< else< {< MSG_WriteBits(msg, 0, 1); // no change< }< < < if(ammobits)< {< MSG_WriteBits(msg, 1, 1); // changed< MSG_WriteShort(msg, ammobits);< for(i = 0; i < 16; i++)< if(ammobits & (1 << i))< MSG_WriteShort(msg, to->ammo[i]);< }< else< {< MSG_WriteBits(msg, 0, 1); // no change< }< < < if(powerupbits)< {< MSG_WriteBits(msg, 1, 1); // changed< MSG_WriteShort(msg, powerupbits);< for(i = 0; i < 16; i++)< if(powerupbits & (1 << i))< MSG_WriteLong(msg, to->powerups[i]);< }< else< {< MSG_WriteBits(msg, 0, 1); // no change< }---> MSG_WriteDeltaArray(msg,from->stats,to->stats,MAX_STATS);> MSG_WriteDeltaArray(msg,from->persistant,to->persistant,MAX_PERSISTANT);> MSG_WriteDeltaArray(msg,from->powerups,to->powerups,MAX_POWERUPS);> MSG_WriteDeltaArray(msg,from->ammo,to->ammo,MAX_WEAPONS);1601d1549< int bits;1607d1554< int trunc;1645,1687c1592< < if(!MSG_ReadBits(msg, 1))< {< // no change< *toF = *fromF;< }< else< {< if(field->bits == 0)< {< // float< if(MSG_ReadBits(msg, 1) == 0)< {< // integral float< trunc = MSG_ReadBits(msg, FLOAT_INT_BITS);< // bias to allow equal parts positive and negative< trunc -= FLOAT_INT_BIAS;< *(float *)toF = trunc;< if(print)< {< Com_Printf("%s:%i ", field->name, trunc);< }< }< else< {< // full floating point value< *toF = MSG_ReadBits(msg, 32);< if(print)< {< Com_Printf("%s:%f ", field->name, *(float *)toF);< }< }< }< else< {< // integer< *toF = MSG_ReadBits(msg, field->bits);< if(print)< {< Com_Printf("%s:%i ", field->name, *toF);< }< }< }---> MSG_ReadDeltaField(msg,fromF,toF,field,print);1699,1756c1603,1606< if(MSG_ReadBits(msg, 1))< {< // parse stats< if(MSG_ReadBits(msg, 1))< {< LOG("PS_STATS");< bits = MSG_ReadShort(msg);< for(i = 0; i < 16; i++)< {< if(bits & (1 << i))< {< to->stats[i] = MSG_ReadShort(msg);< }< }< }< < // parse persistant stats< if(MSG_ReadBits(msg, 1))< {< LOG("PS_PERSISTANT");< bits = MSG_ReadShort(msg);< for(i = 0; i < 16; i++)< {< if(bits & (1 << i))< {< to->persistant[i] = MSG_ReadShort(msg);< }< }< }< < // parse ammo< if(MSG_ReadBits(msg, 1))< {< LOG("PS_AMMO");< bits = MSG_ReadShort(msg);< for(i = 0; i < 16; i++)< {< if(bits & (1 << i))< {< to->ammo[i] = MSG_ReadShort(msg);< }< }< }< < // parse powerups< if(MSG_ReadBits(msg, 1))< {< LOG("PS_POWERUPS");< bits = MSG_ReadShort(msg);< for(i = 0; i < 16; i++)< {< if(bits & (1 << i))< {< to->powerups[i] = MSG_ReadLong(msg);< }< }< }< }---> MSG_ReadDeltaArray(msg,from->stats,to->stats,MAX_STATS,print);> MSG_ReadDeltaArray(msg,from->persistant,to->persistant,MAX_PERSISTANT,print);> MSG_ReadDeltaArray(msg,from->powerups,to->powerups,MAX_POWERUPS,print);> MSG_ReadDeltaArray(msg,from->ammo,to->ammo,MAX_WEAPONS,print);