I'm working on a game modification for a third party game (a so called "mod"), and I want to add the player to an array.
I will explain the process of how it works. My code looks like this:
int numElements = 20;
int arrSize = numElements * 2 + 2;
Ped peds[arrSize];
peds[0] = numElements;
int countPeds = GET_PED_NEARBY_PEDS(PLAYER_PED_ID(), peds, -1);
The game function called GET_PED_NEARBY_PEDS will populate the peds array with IDs of the pedestrians walking around the PLAYER_PED_ID (our player).
My goal here is to also add our player to this array aswell. Right now, it only gathers the pedestrians around the player, and I want the player to be included in this array aswell.
I came to the conclusion that I should create a new array, and add the PLAYER_PED_ID to this new array, like so:
Ped newpeds[arrSize + 1];
for (int i = 0; i < arrSize; i++) newpeds[i] = peds[i];
newpeds[arrSize + 1] = PLAYER_PED_ID();
And then instead of using the peds array in my code, I will be using newpeds. But for some reason the modifications don't affect the player, but only the pedestrians around the player (like the first code example).
This is what my full code looks like:
int numElements = 20;
int arrSize = numElements * 2 + 2;
Ped peds[arrSize];
peds[0] = numElements;
int countPeds = GET_PED_NEARBY_PEDS(pedID, peds, -1);
Ped newpeds[arrSize + 1];
for (int i = 0; i < arrSize; i++) newpeds[i] = peds[i];
newpeds[arrSize + 1] = PLAYER_PED_ID();
for (int i = 0; i < countPeds; i++) {
Ped ped = newpeds[i * 2 + 2];
//...
}
How can I add the player to the array? Could it have something to do with the math? Any help is appreciated. :)
This may be because you are trying to add PLAYER_PED_ID() at wrong index.
for (int i = 0; i < arrSize; i++) newpeds[i] = peds[i];
newpeds[arrSize + 1] = PLAYER_PED_ID();
You should be adding PLAYER_PED_ID() at :
newpeds[arrSize] = PLAYER_PED_ID();
Related
I have the following code below:
typedef struct
{
float K[6][3]; //Kalman gain ([row][column])
} FilterData;
void setFilterData(const FilterData *filterdata)
{
FilterData r;
int i;
int i1;
for (i = 0; i < 3; i++) {
for (i1 = 0; i1 < 6; i1++) {
r.K[i1 + 6 * i] = filterdata->K[i + 3 * i1];
}
}
}
Compiling this code leads to the following error:
setFilterData.c: In function setFilterData :
setFilterData.c:25:23: error: assignment to expression with array type
r.K[i1 + 6 * i] = filterdata->K[i + 3 * i1];
What is the alternative here?
Using memcpy explicitly also did not help.
FilterData r;
int i;
for (i = 0; i < 6; i++) {
memcpy(r.K + (3*i),filterdata->K + i,sizeof(float) );
memcpy(r.K + (3*i + 1),filterdata->K + (i+6),sizeof(float));
memcpy(r.K + (3*i + 2),filterdata->K + (i+12),sizeof(float));
}
Your problem is the FilterData.K is defined as a 2-dimensional array, but setFilterData() is treating it as if it were a 1-dimensional array.
The minimual fix is to say
r.K[i1][i] = filterData.K[i1][i]
(this assumes the transposition in your original code is a bug, not a feature, I'll have to read more about Kalman gain to know)
But there's no reason to double for-loop and assign each member separately. You could
memcpy(&r, filterData, sizeof(r))
or better yet
r = *filterData
r.K is a two-dimensional array. This means that r.K[n] is a float array of length 3. You can't assign a value to an array. For example, ask yourself what
int A[5];
A = 6;
would mean. If you want to assign a few to a specific location inside a two-dimensional (or multi-dimensional) array, you have to specify all of the indices.
for (i = 0; i < 3; i++) {
for (i1 = 0; i1 < 6; i1++) {
r.K[i1][i] = ... // I'm not sure what you actually want here.
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
When I run this program : http://hastebin.com/asorawoluw.m
I get this error in GDB :
Program received signal SIGSEGV, Segmentation fault.
0x0000000000401f91 in resoudre (baie=...) at lineaire.c:291
291 printf("type[%d] : %d\n", i, helper_glpk.col_bounds[i]->type);
When i ask gdb to print iI get :
$1 = 1
So the first iteration is failing, but I'm sure I did write into the first case of helper_glpk.col_bounds at the line 200-204 and I did malloc so there's no way (I think ?) that my data is being overwritten or deleted.. so I don't understand why I get this error.
Edit : here's the minimal code :
My structs :
typedef struct Bounds Bounds;
struct Bounds
{
int type;
double lb;
double ub;
};
typedef struct HelperGlpk HelperGlpk;
struct HelperGlpk
{
double *matrix_coefs;
double *obj_coefs;
Bounds **row_bounds;
Bounds **col_bounds;
int *column_of_coef;
int *row_of_coef;
int cpt_coef;
int cpt_contrainte;
};
My functions to generates constraint :
void genere_contrainte_1(int i, int j, HelperGlpk *helper_glpk, Baie baie){
helper_glpk->col_bounds[index_ouverture_serveur(i)]->type = GLP_DB;
helper_glpk->col_bounds[index_ouverture_serveur(i)]->lb = 0;
helper_glpk->col_bounds[index_ouverture_serveur(i)]->ub = 1;
helper_glpk->cpt_coef++;
helper_glpk->col_bounds[index_connexion(i, j, baie.nbr_serveur)]->type = GLP_LO;
helper_glpk->col_bounds[index_connexion(i, j, baie.nbr_serveur)]->lb = 0;
helper_glpk->col_bounds[index_connexion(i, j, baie.nbr_serveur)]->ub = 0;
helper_glpk->cpt_coef++;
}
and the main program is :
void resoudre(Baie baie){
glp_prob *lp;
const int nbr_rows = baie.nbr_client + baie.nbr_serveur * baie.nbr_client; // nombre de contrainte
const int nbr_colums = baie.nbr_serveur + baie.nbr_serveur * baie.nbr_client; // nombre de variable
const int nbr_coefs = 3 * baie.nbr_serveur * baie.nbr_client;
int i, j;
HelperGlpk helper_glpk;
helper_glpk.matrix_coefs = malloc((nbr_coefs + 1) * sizeof(double));
helper_glpk.matrix_coefs[0] = 0;
helper_glpk.obj_coefs = malloc((nbr_colums + 1) * sizeof(double));
helper_glpk.obj_coefs[0] = 0;
helper_glpk.column_of_coef = malloc((nbr_colums + 1) * sizeof(int));
helper_glpk.column_of_coef[0] = 0;
helper_glpk.row_of_coef = malloc((nbr_rows + 1) * sizeof(int));
helper_glpk.row_of_coef[0] = 0;
helper_glpk.col_bounds = malloc((nbr_colums + 1) * sizeof(Bounds *));
for (int index = 0; index <= nbr_colums; index++)
{
helper_glpk.col_bounds[index] = malloc(sizeof(Bounds));
}
helper_glpk.row_bounds = malloc((nbr_rows + 1) * sizeof(Bounds *));
for (int index = 0; index <= nbr_rows; index++)
{
helper_glpk.row_bounds[index] = malloc(sizeof(Bounds));
}
helper_glpk.cpt_coef = 1;
for(i = 1; i <= baie.nbr_serveur; i++)
for(j = 1; j <= baie.nbr_client; j++)
genere_contrainte_1(i, j, &helper_glpk, baie);
for(i = 1; i <= nbr_colums; i++)
printf("type[%d] : %d\n", i, helper_glpk.col_bounds[i]->type);
for(j = 1; j <= baie.nbr_client; j++)
genere_contrainte_2(j, &helper_glpk, baie.nbr_serveur);
The error I get is while I try to printf after the call to generate_contrainte_1
This code is wrong:
helper_glpk.col_bounds = malloc((nbr_colums + 1) * sizeof(Bounds));
You need to fix it with (provided you need nbr_colums + 1 elements):
helper_glpk.col_bounds = malloc((nbr_colums + 1) * sizeof(Bounds *));
for (int index = 0; index < nbr_colums + 1; index++)
{
helper_glpk.col_bounds[index] = malloc(sizeof(Bounds));
}
I have not checked the rest of the code, there could be other errors.
Edit: maybe you don't need the for loop depending on what your genere_contrainte_1 does, but you need to correct your malloc with the right sizeof.
Edit2: I read your genere_contrainte_1, you definitely need all these mallocs. But I really doubt you need row_bounds and col_bounds to be Bounds **, it seems to me Bounds * would have been fine, and that way a single malloc for each field would be enough.
My program creates a matrix. It moves and centers it into a bigger one. Now I want to fill the border created with the edge values of the first matrix to make it look like this:
So far I have the following code, but it seems that it is not working as it should (let's consider the matrix as an image with width,height and border):
/////////////////////////Up/Down Fill////////////////////////////
for(i=border;i<(width+border);i++){
for(j=0;j<(height+2*border);j++){
if(j<border){
fmatr[i][j]=fmatr[i][border];
}
else if(j>(height+border)){
fmatr[i][j]=fmatr[i][(height+border)];
}
}
}
/////////////////////////Left/Right//////////////////////////////
for(i=0;i<(width+2*border);i++){
for(j=0;j<(height+2*border);j++){
if(i<border){
fmatr[i][j]=fmatr[border][j];
}
else if(i>(width+border)){
fmatr[i][j]=fmatr[(width+border)][j];
}
}
}
Could anyone point me in the right direction how to do that?
You are a victim of an off-one error.
Generally in C, to iterate a sequence you use the start index and the next_to_last index, in this way:
for (i = first; i < next_to_last; ++i)
And that is handy because the next_to_last index is the first plus the length:
for (i = first; i < first + length; ++i)
If you want to know if an index is out of bounds you use < and >=:
out_of_bounds = i < first || i >= next_to_last;
out_of_bounds = i < first || i >= first + length;
All this is cool because when you have two contiguous sequences you do not have to do any adjustment when you go from one to the other:
for (i = 0; i < a; ++i) ...;
for (; i < b; ++i) ...;
for (; i < c; ++i) ...;
But if you want to access the last element of a sequence you have to remember to substract one:
last = array[next_to_last - 1];
last = array[first + length - 1];
So when you say:
else if(j>(height+border)){
fmatr[i][j] = fmatr[i][(height+border)];
}
You probably mean:
else if(j >= height + border){
fmatr[i][j] = fmatr[i][height + border - 1];
}
And the same with the witdh run.
That said I would write the inner loop as two loops. IMO your intention is clearer this way:
for (j = 0; j < border; ++j)
fmatr[i][j] = fmatr[i][border];
for (j = height + border; j < height + 2 * border; ++j)
fmatr[i][j]=fmatr[i][height + border - 1];
And the same with the width run.
I have an image (which is a Sprite) that I store it in a byte array.
I would like to extract only the bytes that relate to a specific place and size within this byte array so that I can create a new image, basically a crop.
I am using C# and compact cf. I could use get pixel and save each value to a byte array and then 'read' the portion i am interested back. I know I can use LockBitmap() to make this quicker. I would normally use Aforge and/or Emgu but as I say I am using the compact cf framework 2.
I would be interested in any known ways to do this.
Thanks
Additional.
Following on the link below I would like to know whether there is an alternative (like a buffer copy) to this iterative piece of code?
//Iterate the selected area of the original image, and the full area of the new image
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width * BPP; j += BPP)
{
int origIndex = (startX * rawOriginal.Stride) + (i * rawOriginal.Stride) + (startY * BPP) + (j);
int croppedIndex = (i * width * BPP) + (j);
//copy data: once for each channel
for (int k = 0; k < BPP; k++)
{
croppedBytes[croppedIndex + k] = origBytes[origIndex + k];
}
}
}
I understand that this is an old question, but here's my take on it:
public static byte[] CropImageArray(byte[] pixels, int sourceWidth, int bitsPerPixel, Int32Rect rect)
{
var blockSize = bitsPerPixel / 8;
var outputPixels = new byte[rect.Width * rect.Height * blockSize];
//Create the array of bytes.
for (var line = 0; line <= rect.Height - 1; line++)
{
var sourceIndex = ((rect.Y + line) * sourceWidth + rect.X) * blockSize;
var destinationIndex = line * rect.Width * blockSize;
Array.Copy(pixels, sourceIndex, outputPixels, destinationIndex, rect.Width * blockSize);
}
return outputPixels;
}
You'll need to know the bits per pixel and the width. You'll be using one for instead of two.
I have some more links for you
Try out if you find you solution or it helps you in any way
1)http://www.codeproject.com/Articles/33838/Image-Processing-using-C
2)http://codenicely.blogspot.in/2012/03/how-to-crop-image-in-c.html
Okay, so this is just a small example of something similar I am doing in my own program I am creating for an online class. The only difference is the output of each array will be inputted by user in a different fields instead of having the answer predetermined by the programmer.
In this case after the user numbers are outputted, I want to create a button (which I know how to do, sorry if this is too theoretical right now) which will specific values and output them in a different field.
For example... for aryNumbers[0][0] (Which in my program would be a name) I would call out aryNumbers[0][1] to aryNumbers[0][4] ...so in this case 10, 76, 23.(these numbers would be test scores)
int[ ][ ] aryNumbers = new int[2][4];
aryNumbers[0][0] = 34;
aryNumbers[0][1] = 10;
aryNumbers[0][2] = 76;
aryNumbers[0][3] = 23;
aryNumbers[1][0] = 11;
aryNumbers[1][0] = 30;
aryNumbers[1][0] = 56;
aryNumbers[1][0] = 65;
aryNumbers[2][0] = 34;
aryNumbers[2][0] = 13;
aryNumbers[2][0] = 23;
aryNumbers[2][0] = 18;
int rows = 2;
int columns = 4;
int i, j;
for (i=0; i < rows ; i++) {
for (j=0; j < columns ; j++) {
System.out.print( aryNumbers[ i ][ j ] + " " );
}
System.out.println( "" );
}
Only it would call out all values for the entire list of names and their corresponding test scores.
Aka...
ary..[0][1]
ary..[0][2]
ary..[0][3]
ary..[1][1]
ary..[1][2]
ary..[1][3]
ary..[1][1]
ary..[1][2]
ary..[1][3]
This is my first time working with multi-dimensional arrays..
Change for(j=0... to for(j=1... to skip the zeroth element of the inner array.