I'm trying to set up a 320x240 "room" in a game. I want to use a list of 16x16 tiles. Each tile is a predefined list, consisting of the each pixel's color.
extern char blue27tile[] ={
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55};
extern char grey10tile[] ={
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17};
So above would be my tile definitions. Ignore that they're all just blocks of one color, that would change in the future.
My question is how to set up the same array/list for the room? I was thinking a room like:
extern int testroom1[] ={
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
where "1" refers to the grey10tile and "0" refers to the blue27tile. I would correspond numbers to the variables with something like:
extern int *tilelist[2];
tilelist[0] = &blue27tile;
tilelist[1] = &grey10tile;
but this gives me a "typemismatch in redeclaration of tilelist" error.
I know this is a simple question, I don't know C and am trying to learn with this project. I can't describe the problem well enough to find a solution by googling.
If this was python, I would just do something like
tilelist[0] = [17,17,17,...] //all of my grey10tile 17s
tilelist[1] = [55,55,55,...] //all of my blue27tile 55s
tilelist[0][1] -> 17 //the first of my grey10tile 17s
How can I set this up to do something similar in C?
Thanks a ton.
You cannot place statements outside of functions in C. At global scope only declarations are allowed. tilelist[0] = &blue27tile; is a statement, for example.
Try:
char *tilelist[] = { blue27tile, grey10tile };
Related
I'm getting a different result trying to replace numbers in an array with the number's cosine depending what I do.
I have a 10 x 10 Numpy array and I'm trying to apply a cosine to the even columns.
This is my array
tens
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18],
[ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27],
[ 0, 4, 8, 12, 16, 20, 24, 28, 32, 36],
[ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45],
[ 0, 6, 12, 18, 24, 30, 36, 42, 48, 54],
[ 0, 7, 14, 21, 28, 35, 42, 49, 56, 63],
[ 0, 8, 16, 24, 32, 40, 48, 56, 64, 72],
[ 0, 9, 18, 27, 36, 45, 54, 63, 72, 81]])
I rotate and slice the array to apply the cosine to the even columns and use the same slicing and rotating to store the cosine in the right position.
tens[:, 0::2] = np.cos(tens[:, 0::2])
Here is the array after that
tens
array([[ 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[ 1, 1, 0, 3, 0, 5, 0, 7, 0, 9],
[ 1, 2, 0, 6, 0, 10, 0, 14, 0, 18],
[ 1, 3, 0, 9, 0, 15, 0, 21, 0, 27],
[ 1, 4, 0, 12, 0, 20, 0, 28, 0, 36],
[ 1, 5, 0, 15, 0, 25, 0, 35, 0, 45],
[ 1, 6, 0, 18, 0, 30, 0, 42, 0, 54],
[ 1, 7, 0, 21, 0, 35, 0, 49, 0, 63],
[ 1, 8, 0, 24, 0, 40, 0, 56, 0, 72],
[ 1, 9, 0, 27, 0, 45, 0, 63, 0, 81]])
I'm not sure why I get integers here instead of floats. I don't get this if I apply a cosine to the original array. I get this.
np.cos(tens[:, 1::2])
array([[ 1. , 1. , 1. , 1. , 1. ],
[ 0.54030231, -0.9899925 , 0.28366219, 0.75390225, -0.91113026],
[-0.41614684, 0.96017029, -0.83907153, 0.13673722, 0.66031671],
[-0.9899925 , -0.91113026, -0.75968791, -0.54772926, -0.29213881],
[-0.65364362, 0.84385396, 0.40808206, -0.96260587, -0.12796369],
[ 0.28366219, -0.75968791, 0.99120281, -0.90369221, 0.52532199],
[ 0.96017029, 0.66031671, 0.15425145, -0.39998531, -0.82930983],
[ 0.75390225, -0.54772926, -0.90369221, 0.30059254, 0.98589658],
[-0.14550003, 0.42417901, -0.66693806, 0.85322011, -0.96725059],
[-0.91113026, -0.29213881, 0.52532199, 0.98589658, 0.77668598]])
Is there something happening with floats and integers when I try to store these numbers in their correct positions in the original array?
In Scala:
Is there a way to directly split a string that contains 72 numeric values separated by ; into a 2-dimensional array of 9 rows and 8 columns with those numeric values -in numeric data type-?
val input = List.tabulate(72)(_.toString).mkString(";")
input.split(";").map(_.toInt).grouped(9).toArray
transforms
0;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;57;58;59;60;61;62;63;64;65;66;67;68;69;70;71
into
Array(
Array(0, 1, 2, 3, 4, 5, 6, 7, 8),
Array(9, 10, 11, 12, 13, 14, 15, 16, 17),
Array(18, 19, 20, 21, 22, 23, 24, 25, 26),
Array(27, 28, 29, 30, 31, 32, 33, 34, 35),
Array(36, 37, 38, 39, 40, 41, 42, 43, 44),
Array(45, 46, 47, 48, 49, 50, 51, 52, 53),
Array(54, 55, 56, 57, 58, 59, 60, 61, 62),
Array(63, 64, 65, 66, 67, 68, 69, 70, 71)
)
If you want to swap the dimensions of rows/columns, replace 9 by 8.
using Range and grouped functions
scala> val a = (0 to 71).map(_.toString).toArray.mkString(";")
a: String = 0;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;57;58;59;60;61;62;63;64;65;66;67;68;69;70;71
scala> a.split(";").map(_.toInt).sliding(9,9).toArray
res269: Array[Array[Int]] = Array(Array(0, 1, 2, 3, 4, 5, 6, 7, 8), Array(9, 10, 11, 12, 13, 14, 15, 16, 17), Array(18, 19, 20, 21, 22, 23, 24, 25, 26), Array(27, 28, 29, 30, 31, 32, 33, 34, 35), Array(36, 37, 38, 39, 40, 41, 42, 43, 44), Array(45, 46, 47, 48, 49, 50, 51, 52, 53), Array(54, 55, 56, 57, 58, 59, 60, 61, 62), Array(63, 64, 65, 66, 67, 68, 69, 70, 71))
scala>
I would like to know how to reshape a 2-D linearized Array in Scala without loops, i.e. by using functional programming.
Assume a 2-D Array of 6x12 = 72 elements. What I need is to resize this Array so that it has 8x16 = 128 elements, assuming that the new elements are initialized to zero.
This is how I do it with loops:
val a = (1 to 72).toArray
a: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72)
scala> val h = 6
h: Int = 6
scala> val w = 12
w: Int = 12
scala> val H = 8
H: Int = 8
scala> val W = 16
W: Int = 16
scala> val b = Array.ofDim[Int](H * W)
b: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
for (i <- 0 until h)
for (j <- 0 until w)
b(i * W + j) = a(i * w + j)
b: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 0, 0, 0, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 0, 0, 0, 0, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 0, 0, 0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 0, 0, 0, 0, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
The question is: how to do this by using functional programming?
Thank you very much!
a.grouped(w).map(_.padTo(W, 0)).padTo(H, Array.fill(W)(0)).flatten.toArray
Result (and string copied from your code, for comparison):
result : Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 0, 0, 0, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 0, 0, 0, 0, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 0, 0, 0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 0, 0, 0, 0, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
b: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 0, 0, 0, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 0, 0, 0, 0, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 0, 0, 0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 0, 0, 0, 0, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
I am currently developing a display application for F1 2017 on my iPhone using swift. I am able to read the UDP packets from my PS4 to my phone by using SwiftSockets.
I received the following data:
[25, 79, 3, 64, 176, 153, 15, 61, 244, 133, 165, 69, 0, 20, 145, 192, 223, 145, 218, 194, 165, 106, 64, 64, 15, 88, 234, 67, 220, 222, 109, 59, 168, 219, 67, 187, 42, 245, 4, 59, 200, 163, 186, 57, 65, 126, 46, 63, 22, 207, 77, 59, 13, 81, 59, 191, 127, 79, 59, 191, 0, 73, 203, 187, 141, 126, 46, 191, 180, 81, 37, 190, 5, 254, 41, 189, 251, 167, 151, 62, 145, 185, 39, 62, 7, 210, 92, 63, 203, 186, 128, 192, 218, 25, 99, 64, 112, 21, 0, 191, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 24, 106, 72, 61, 239, 89, 49, 187, 0, 0, 192, 64, 191, 199, 133, 69, 0, 0, 0, 0, 0, 0, 144, 65, 0, 80, 195, 72, 0, 80, 195, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 75, 253, 209, 66, 0, 0, 210, 66, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 80, 182, 232, 65, 80, 182, 232, 65, 80, 182, 232, 65, 80, 182, 232, 65, 0, 0, 172, 65, 0, 0, 172, 65, 0, 0, 184, 65, 0, 0, 184, 65, 0, 0, 0, 64, 0, 0, 198, 66, 57, 170, 165, 69, 128, 93, 205, 66, 0, 128, 84, 70, 0, 96, 134, 69, 0, 0, 16, 65, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 252, 68, 240, 253, 176, 66, 131, 119, 113, 188, 93, 249, 95, 187, 28, 146, 43, 59, 116, 163, 23, 188, 87, 87, 88, 88, 0, 0, 0, 0, 5, 54, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 200, 88, 12, 67, 0, 0, 255, 20, 0, 146, 115, 234, 194, 88, 249, 59, 64, 95, 94, 231, 67, 128, 93, 205, 66, 176, 153, 15, 61, 24, 134, 204, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 122, 64, 100, 2, 18, 7, 5, 0, 0, 0, 0, 245, 73, 129, 193, 65, 222, 85, 64, 134, 169, 0, 68, 32, 234, 205, 66, 224, 188, 92, 67, 94, 87, 203, 66, 0, 0, 0, 0, 0, 0, 0, 0, 62, 112, 162, 69, 34, 2, 15, 3, 5, 1, 2, 0, 0, 134, 253, 181, 65, 69, 178, 83, 64, 5, 6, 10, 68, 136, 6, 200, 66, 70, 194, 141, 67, 180, 236, 199, 66, 0, 0, 0, 0, 0, 0, 0, 0, 17, 208, 160, 69, 16, 0, 5, 3, 5, 1, 2, 0, 0, 16, 137, 185, 195, 194, 6, 44, 64, 211, 20, 153, 66, 64, 15, 198, 66, 238, 232, 27, 61, 64, 15, 198, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 208, 253, 67, 0, 1, 1, 4, 5, 0, 0, 0, 0, 83, 13, 149, 65, 96, 78, 84, 64, 101, 6, 9, 68, 170, 16, 200, 66, 82, 86, 44, 68, 212, 184, 199, 66, 0, 0, 0, 0, 0, 0, 0, 0, 75, 252, 160, 69, 6, 1, 3, 3, 5, 1, 2, 0, 0, 247, 239, 27, 68, 227, 16, 124, 64, 33, 229, 220, 67, 171, 9, 202, 66, 0, 0, 0, 0, 171, 9, 202, 66, 0, 0, 0, 0, 0, 0, 0, 0, 86, 90, 110, 69, 7, 11, 11, 2, 5, 0, 2, 0, 0, 91, 113, 167, 67, 92, 188, 60, 64, 223, 10, 42, 68, 58, 73, 206, 66, 0, 0, 0, 0, 58, 73, 206, 66, 0, 0, 0, 0, 0, 0, 0, 0, 214, 218, 146, 69, 18, 5, 19, 3, 5, 0, 2, 0, 0, 235, 33, 44, 68, 26, 50, 118, 64, 22, 232, 74, 68, 64, 189, 200, 66, 0, 0, 0, 0, 64, 189, 200, 66, 0, 0, 0, 0, 0, 0, 0, 0, 190, 18, 132, 69, 5, 6, 7, 3, 5, 0, 2, 0, 0, 63, 156, 38, 68, 27, 127, 136, 64, 55, 132, 5, 68, 48, 100, 203, 66, 0, 0, 0, 0, 48, 100, 203, 66, 0, 0, 0, 0, 0, 0, 0, 0, 12, 190, 116, 69, 35, 7, 16, 3, 5, 0, 2, 0, 0, 214, 118, 54, 196, 239, 49, 70, 64, 111, 224, 185, 195, 176, 46, 202, 66, 200, 232, 13, 61, 176, 46, 202, 66, 0, 0, 0, 0, 0, 0, 0, 0, 128, 229, 135, 68, 3, 7, 13, 4, 5, 0, 0, 0, 0, 122, 220, 252, 67, 157, 82, 60, 64, 88, 205, 89, 68, 208, 106, 202, 66, 0, 0, 0, 0, 208, 106, 202, 66, 0, 0, 0, 0, 0, 0, 0, 0, 213, 35, 138, 69, 14, 11, 14, 2, 5, 0, 2, 0, 0, 43, 225, 15, 68, 97, 160, 103, 64, 244, 178, 187, 67, 216, 10, 202, 66, 0, 0, 0, 0, 216, 10, 202, 66, 0, 0, 0, 0, 0, 0, 0, 0, 64, 42, 105, 69, 1, 8, 12, 3, 5, 0, 2, 0, 0, 12, 73, 197, 67, 40, 119, 53, 64, 115, 141, 56, 68, 199, 157, 201, 66, 0, 0, 0, 0, 199, 157, 201, 66, 0, 0, 0, 0, 0, 0, 0, 0, 5, 231, 143, 69, 10, 3, 8, 2, 5, 0, 2, 0, 0, 66, 178, 177, 67, 10, 45, 66, 64, 88, 176, 42, 68, 168, 124, 203, 66, 0, 0, 0, 0, 168, 124, 203, 66, 0, 0, 0, 0, 0, 0, 0, 0, 230, 9, 146, 69, 20, 3, 17, 3, 5, 0, 2, 0, 0, 140, 235, 54, 68, 99, 244, 101, 64, 198, 155, 62, 68, 16, 171, 201, 66, 0, 0, 0, 0, 16, 171, 201, 66, 0, 0, 0, 0, 0, 0, 0, 0, 109, 212, 129, 69, 33, 6, 9, 2, 5, 0, 2, 0, 0, 21, 102, 86, 66, 112, 68, 96, 64, 60, 2, 135, 65, 128, 194, 199, 66, 164, 206, 103, 66, 128, 194, 199, 66, 96, 174, 4, 66, 0, 0, 0, 0, 236, 0, 63, 69, 22, 0, 4, 3, 5, 0, 1, 0, 0, 47, 20, 201, 67, 201, 159, 49, 64, 18, 130, 60, 68, 8, 44, 199, 66, 86, 200, 170, 66, 8, 44, 199, 66, 64, 16, 4, 66, 64, 253, 217, 65, 94, 90, 143, 69, 9, 4, 2, 3, 5, 0, 2, 0, 0, 32, 154, 227, 194, 27, 193, 93, 64, 243, 43, 77, 196, 251, 231, 201, 66, 0, 0, 0, 0, 251, 231, 201, 66, 0, 0, 0, 0, 0, 0, 0, 0, 112, 189, 255, 68, 23, 8, 10, 3, 5, 0, 1, 0, 0, 157, 12, 27, 66, 80, 210, 84, 64, 234, 220, 13, 68, 240, 20, 200, 66, 204, 189, 15, 67, 240, 20, 200, 66, 0, 0, 0, 0, 0, 0, 0, 0, 113, 36, 160, 69, 15, 4, 6, 2, 5, 1, 2, 0, 0, 70, 217, 139, 67, 81, 9, 51, 64, 70, 253, 57, 68, 81, 176, 206, 66, 204, 63, 149, 60, 81, 176, 206, 66, 0, 0, 0, 0, 0, 0, 0, 0, 71, 132, 149, 69, 31, 5, 20, 2, 5, 0, 2, 0, 0, 182, 135, 20, 192, 86, 227, 218, 187, 41, 207, 77, 187, 23, 39, 22, 59, 188, 55, 6, 59, 86, 40, 253, 58, 203, 2, 123, 66, 62, 192, 141, 194, 179, 242, 115, 66, 2, 233, 141, 194, 207, 30, 49, 191, 192, 105, 55, 62, 231, 125, 246, 190]
How would I be able to decode the data into a UDPPacket? In order to do this, I think I need to convert the data into four bytes and then somehow convert it to a float.
Here is my swift code:
import UIKit
import SwiftSocket
class ViewController: UIViewController {
var server: UDPServer!
var timer: Timer!
override func viewDidLoad() {
super.viewDidLoad()
server = UDPServer(address: "192.168.1.158", port: 20777)
timer = Timer.scheduledTimer(withTimeInterval: 0, repeats: true) { _ in
print("a")
let data = self.server.recv(1289).0
if let data = data {
print(data)
self.timer.invalidate()
}
}
timer.fire()
}
}
This is what the UDPPacket should look like:
struct UDPPacket
{
float m_time;
float m_lapTime;
float m_lapDistance;
float m_totalDistance;
float m_x; // World space position
float m_y; // World space position
float m_z; // World space position
float m_speed; // Speed of car in MPH
float m_xv; // Velocity in world space
float m_yv; // Velocity in world space
float m_zv; // Velocity in world space
float m_xr; // World space right direction
float m_yr; // World space right direction
float m_zr; // World space right direction
float m_xd; // World space forward direction
float m_yd; // World space forward direction
float m_zd; // World space forward direction
float m_susp_pos[4]; // Note: All wheel arrays have the order:
float m_susp_vel[4]; // RL, RR, FL, FR
float m_wheel_speed[4];
float m_throttle;
float m_steer;
float m_brake;
float m_clutch;
float m_gear;
float m_gforce_lat;
float m_gforce_lon;
float m_lap;
float m_engineRate;
float m_sli_pro_native_support; // SLI Pro support
float m_car_position; // car race position
float m_kers_level; // kers energy left
float m_kers_max_level; // kers maximum energy
float m_drs; // 0 = off, 1 = on
float m_traction_control; // 0 (off) - 2 (high)
float m_anti_lock_brakes; // 0 (off) - 1 (on)
float m_fuel_in_tank; // current fuel mass
float m_fuel_capacity; // fuel capacity
float m_in_pits; // 0 = none, 1 = pitting, 2 = in pit area
float m_sector; // 0 = sector1, 1 = sector2, 2 = sector3
float m_sector1_time; // time of sector1 (or 0)
float m_sector2_time; // time of sector2 (or 0)
float m_brakes_temp[4]; // brakes temperature (centigrade)
float m_tyres_pressure[4]; // tyres pressure PSI
float m_team_info; // team ID
float m_total_laps; // total number of laps in this race
float m_track_size; // track size meters
float m_last_lap_time; // last lap time
float m_max_rpm; // cars max RPM, at which point the rev limiter will kick in
float m_idle_rpm; // cars idle RPM
float m_max_gears; // maximum number of gears
float m_sessionType; // 0 = unknown, 1 = practice, 2 = qualifying, 3 = race
float m_drsAllowed; // 0 = not allowed, 1 = allowed, -1 = invalid / unknown
float m_track_number; // -1 for unknown, 0-21 for tracks
float m_vehicleFIAFlags; // -1 = invalid/unknown, 0 = none, 1 = green, 2 = blue, 3 = yellow, 4 = red
float m_era; // era, 2017 (modern) or 1980 (classic)
float m_engine_temperature; // engine temperature (centigrade)
float m_gforce_vert; // vertical g-force component
float m_ang_vel_x; // angular velocity x-component
float m_ang_vel_y; // angular velocity y-component
float m_ang_vel_z; // angular velocity z-component
byte m_tyres_temperature[4]; // tyres temperature (centigrade)
byte m_tyres_wear[4]; // tyre wear percentage
byte m_tyre_compound; // compound of tyre – 0 = ultra soft, 1 = super soft, 2 = soft, 3 = medium, 4 = hard, 5 = inter, 6 = wet
byte m_front_brake_bias; // front brake bias (percentage)
byte m_fuel_mix; // fuel mix - 0 = lean, 1 = standard, 2 = rich, 3 = max
byte m_currentLapInvalid; // current lap invalid - 0 = valid, 1 = invalid
byte m_tyres_damage[4]; // tyre damage (percentage)
byte m_front_left_wing_damage; // front left wing damage (percentage)
byte m_front_right_wing_damage; // front right wing damage (percentage)
byte m_rear_wing_damage; // rear wing damage (percentage)
byte m_engine_damage; // engine damage (percentage)
byte m_gear_box_damage; // gear box damage (percentage)
byte m_exhaust_damage; // exhaust damage (percentage)
byte m_pit_limiter_status; // pit limiter status – 0 = off, 1 = on
byte m_pit_speed_limit; // pit speed limit in mph
float m_session_time_left; // NEW: time left in session in seconds
byte m_rev_lights_percent; // NEW: rev lights indicator (percentage)
byte m_is_spectating; // NEW: whether the player is spectating
byte m_spectator_car_index; // NEW: index of the car being spectated
// Car data
byte m_num_cars; // number of cars in data
byte m_player_car_index; // index of player's car in the array
CarUDPData m_car_data[20]; // data for all cars on track
float m_yaw; // NEW (v1.8)
float m_pitch; // NEW (v1.8)
float m_roll; // NEW (v1.8)
float m_x_local_velocity; // NEW (v1.8) Velocity in local space
float m_y_local_velocity; // NEW (v1.8) Velocity in local space
float m_z_local_velocity; // NEW (v1.8) Velocity in local space
float m_susp_acceleration[4]; // NEW (v1.8) RL, RR, FL, FR
float m_ang_acc_x; // NEW (v1.8) angular acceleration x-component
float m_ang_acc_y; // NEW (v1.8) angular acceleration y-component
float m_ang_acc_z; // NEW (v1.8) angular acceleration z-component
};
CarUDPData:
struct CarUDPData
{
float m_worldPosition[3]; // world co-ordinates of vehicle
float m_lastLapTime;
float m_currentLapTime;
float m_bestLapTime;
float m_sector1Time;
float m_sector2Time;
float m_lapDistance;
byte m_driverId;
byte m_teamId;
byte m_carPosition; // UPDATED: track positions of vehicle
byte m_currentLapNum;
byte m_tyreCompound; // compound of tyre – 0 = ultra soft, 1 = super soft, 2 = soft, 3 = medium, 4 = hard, 5 = inter, 6 = wet
byte m_inPits; // 0 = none, 1 = pitting, 2 = in pit area
byte m_sector; // 0 = sector1, 1 = sector2, 2 = sector3
byte m_currentLapInvalid; // current lap invalid - 0 = valid, 1 = invalid
byte m_penalties; // NEW: accumulated time penalties in seconds to be added
};
If it helps, I am at the start of a Grand Prix (not career), racing as Lewis Hamilton at Australia and the starting lights have not gone off yet.
Here is a link to the UDP data specifications for F1 2017.
i know this sounds silly, but can someone please post the arrays described by rfc2612:
Cm = 0x5A827999
Mm = 0x6ED9EBA1
Cr = 19
Mr = 17
for (i=0; i<24; i++)
{
for (j=0; j<8; j++)
{
Tmj_(i) = Cm
Cm = (Cm + Mm) mod 2**32
Trj_(i) = Cr
Cr = (Cr + Mr) mod 32
}
}
i think im doing is wrong for some reason
i get this for Tr
[[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2]]
Tr0 = { 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11 }
Tr1 = { 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28 }
Tr2 = { 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13 }
Tr3 = { 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30 }
Tr4 = { 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15 }
Tr5 = { 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0 }
Tr6 = { 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17 }
Tr7 = { 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2 }
..and Tm (in hex):
Tm0 = { 5a827999, d151d6a1, 482133a9, bef090b1, 35bfedb9, ac8f4ac1, 235ea7c9, 9a2e04d1, 10fd61d9, 87ccbee1, fe9c1be9, 756b78f1, ec3ad5f9, 630a3301, d9d99009, 50a8ed11, c7784a19, 3e47a721, b5170429, 2be66131, a2b5be39, 19851b41, 90547849, 723d551 }
Tm1 = { c95c653a, 402bc242, b6fb1f4a, 2dca7c52, a499d95a, 1b693662, 9238936a, 907f072, 7fd74d7a, f6a6aa82, 6d76078a, e4456492, 5b14c19a, d1e41ea2, 48b37baa, bf82d8b2, 365235ba, ad2192c2, 23f0efca, 9ac04cd2, 118fa9da, 885f06e2, ff2e63ea, 75fdc0f2 }
Tm2 = { 383650db, af05ade3, 25d50aeb, 9ca467f3, 1373c4fb, 8a432203, 1127f0b, 77e1dc13, eeb1391b, 65809623, dc4ff32b, 531f5033, c9eead3b, 40be0a43, b78d674b, 2e5cc453, a52c215b, 1bfb7e63, 92cadb6b, 99a3873, 8069957b, f738f283, 6e084f8b, e4d7ac93 }
Tm3 = { a7103c7c, 1ddf9984, 94aef68c, b7e5394, 824db09c, f91d0da4, 6fec6aac, e6bbc7b4, 5d8b24bc, d45a81c4, 4b29decc, c1f93bd4, 38c898dc, af97f5e4, 266752ec, 9d36aff4, 14060cfc, 8ad56a04, 1a4c70c, 78742414, ef43811c, 6612de24, dce23b2c, 53b19834 }
Tm4 = { 15ea281d, 8cb98525, 388e22d, 7a583f35, f1279c3d, 67f6f945, dec6564d, 5595b355, cc65105d, 43346d65, ba03ca6d, 30d32775, a7a2847d, 1e71e185, 95413e8d, c109b95, 82dff89d, f9af55a5, 707eb2ad, e74e0fb5, 5e1d6cbd, d4ecc9c5, 4bbc26cd, c28b83d5 }
Tm5 = { 84c413be, fb9370c6, 7262cdce, e9322ad6, 600187de, d6d0e4e6, 4da041ee, c46f9ef6, 3b3efbfe, b20e5906, 28ddb60e, 9fad1316, 167c701e, 8d4bcd26, 41b2a2e, 7aea8736, f1b9e43e, 68894146, df589e4e, 5627fb56, ccf7585e, 43c6b566, ba96126e, 31656f76 }
Tm6 = { f39dff5f, 6a6d5c67, e13cb96f, 580c1677, cedb737f, 45aad087, bc7a2d8f, 33498a97, aa18e79f, 20e844a7, 97b7a1af, e86feb7, 85565bbf, fc25b8c7, 72f515cf, e9c472d7, 6093cfdf, d7632ce7, 4e3289ef, c501e6f7, 3bd143ff, b2a0a107, 296ffe0f, a03f5b17 }
Tm7 = { 6277eb00, d9474808, 5016a510, c6e60218, 3db55f20, b484bc28, 2b541930, a2237638, 18f2d340, 8fc23048, 6918d50, 7d60ea58, f4304760, 6affa468, e1cf0170, 589e5e78, cf6dbb80, 463d1888, bd0c7590, 33dbd298, aaab2fa0, 217a8ca8, 9849e9b0, f1946b8 }
(I'm not sure why they didn't just include these as tables in the spec).