The online racing simulator
Insim: Flags + replayspeed
(5 posts, started )
#1 - abz1
Insim: Flags + replayspeed
Ive made some pretty good progress using java. Im just creating classes for each packet received. So I am doing the STA packet at the moment.

1. so, im trying to convert the replayspeed into a float value, im using the 4 bytes which i got from the array sent from LFS (check attached file to see data) and stored them into a seperate array


import java.nio.*;
public static void main(String args[])
{
byte [] a = new byte[4];
a[0] = 0;
a[1] =0;
a[2] =-128;
a[3]=63;

ByteBuffer b= ByteBuffer.allocate(a.length);
b.put(a);

Float f = (float)b.getFloat(0);
int fe = f.intValue();

System.out.println(f);
System.out.println(fe);
}



I dont know if the conversion is correct and this is the output i get
f= 4.6006E-41
fe=0

2. and finally can someone explain how the values can be extracted from the flags


ISS_GAME 1 // in game (or MPR)
ISS_REPLAY 2 // in SPR
ISS_PAUSED 4 // paused
ISS_SHIFTU 8 // in SHIFT+U mode
ISS_SHIFTU_HIGH 16 // HIGH view
ISS_SHIFTU_FOLLOW 32 // following car
etc.

so i receive these values
FLAG
-32
12
from lfs what does it mean(what flags does it contain)
Attached files
datafile.txt - 858 B - 205 views
#2 - Stuff
The way I check if those flags are set is to simply 'and' the value to the word and if it equals the value, the flag is set. Eg.

If(flags And ISS_SHIFTU == ISS_SHIFTU)
{
//Shift+U flag is set
}

Not too familiar with Java but you get the picture
Basically the incoming variable has multiple settings symultaneously, which you need to extra using bitwise operations.

The reason for this is that the "replay speed" could be both ISS_PAUSED and ISS_SHIFTU_HIGH, and you dont want to use multiple variables to store that 1 item of data, as it is a waste of memory.

As RayOK has said, using the bitwise operator AND allows you to check if its set or not.

If you want to learn more on Bitwise Operations, check out this article at GameDev. I always found it to be the best article on the subject.
Quote from abz1 :1. so, im trying to convert the replayspeed into a float value, im using the 4 bytes which i got from the array sent from LFS (check attached file to see data) and stored them into a seperate array

So.. I finally have to admin that I don't know how to convert an array of bytes into a float value (yet), hehe. But there's 2 things confusing me.

I'm using C#, very similar to Java.

1) You have a byte value of -128. Isn't the datatype "byte" also a non-signed numeric datatype in Java? That means it cannot hold negative values? If so casting -128 (as integer) to a (byte) will make it 128 (positive).

2) In LFS.Live I'm often using the "BitConverter" class. Maybe you have this in Java as well and "float" is probably called "single" as well as in C#. I don't have the ToFloat() available on a (byte-)array for example. Maybe lets change the negative sign from your -128 and it turns out to be 1.0 as result as well.

After all, the following code gives me 1.0f as output:
byte[] value = new byte[] { 0, 0, 128, 63 };
float floatValue = BitConverter.ToSingle(value, 0);

Console.WriteLine("Float conversion: {0}", floatValue);

Method explained:
Returns a single-precision floating point number from four bytes at a specified position in a byte array.
BitConverter.ToSingle(byte[] array, int startIndex).
#5 - abz1
the float problem has been solved, just the flags left. So am i right in doing this. converting the 2 bytes received from the array into a unsigned byte and then adding them togther to carry out the bitwise operation?

say you get the flags -32 12 two bytes
I will then pass both bytes into this method

public static int getInt(byte b)
{
return b & 0xFF;
}

and get 224 and 12 add them and they equal 236 then use the bitwise operation on this value.

Insim: Flags + replayspeed
(5 posts, started )
FGED GREDG RDFGDR GSFDG