我正在用C++编写一个灵活的菱形正方形地形生成器。我的地形高度值的瓷砖网格被定义为一个巨大的int数组。现在,我有了整个菱形正方形算法,它将这个网格作为一个单独的函数stdroutine
来处理。我不能将网格声明为全局变量,因为我需要用户能够定义它的大小(在合理的范围内,尽管还没有实现大小检查)。我不想只是把stdroutine
的内容粘贴到main
中,因为我希望能够连续多次调用它,而不会有太多麻烦(这最终将是一些预设例程运行所必需的)。因此,我试图给stdroutine
一个指向我的数组的指针,希望如果我告诉它,它能够弄清楚数组的实际位置。stdroutine
实际上调用了另一个函数mean
,它也需要网格才能工作。这是我的代码(大错特错,但哦,好吧,我不知道更好的了):
main.cpp:
#include <stdio.h>
#include <stdlib.h>
#include "mt.h"
#include "diamondsquare.h"
int main () {
unsigned long seed = 0, x = 0, y = 0, initial = 0, range = 0;
int smooth = 0, fail = 1, index1 = 0, index2 = 0;
char flagchar1 = 'n';
printf("Welcome to my diamond-square generator! This isn't full-feature yet, so I'm just gonna have you input the variables one by one. ");
do {
printf("Please input the seed (this is a positive integer):n");
fail = scanf("%lu", &seed);
while (fail == 0) {
printf("Try again, smartass.n");
fail = scanf("%lu", &seed);
}
fail = 1;
printf("Now input the x, or horizontal, size of your grid:n");
fail = scanf("%lu", &x);
while (fail == 0) {
printf("An integer. Not a string. An integer. You can do that, can't you?n");
fail = scanf("%lu", &x);
}
fail = 1;
printf("Now input the y, or vertical, size of your grid:n");
fail = scanf("%lu", &y);
while (fail == 0) {
printf("What was that supposed to be? An integer, please.n");
fail = scanf("%lu", &y);
}
fail = 1;
printf("Now input about how high you'd like the grid to be (this goes from a scale of 1 to 256):n");
fail = scanf("%lu", &initial);
while (initial == 0 || initial > 256 || fail == 0) {
printf("ahahahahaha how HIGH do you have to be just to HAVE that hieght........n");
fail = scanf("%lu", &initial);
}
fail = 1;
printf("Now input the range of the heights on your grid (this must be equal to or less than 256):n");
scanf("%lu", &range);
while (range >= 256 || fail == 0) {
printf("What did I say about being equal to or less than 256? Give me something reasonable to work with here.n");
fail = scanf("%lu", &range);
}
fail = 1;
printf("Just one more variable to go! Now, I need you to input the smoothness of your grid. Smaller numbers make spikier grids. You can make this negative, but beware!n");
fail = scanf("%d", &smooth);
while (fail == 0) {
printf("That... was not a number.n");
fail = scanf("%d", &smooth);
}
fail = 1;
printf("nOkay. Are these the values you want? Type Y/n.n Seed: %lun Width: %lun Length: %lun Height: %lun Range: %lun Smoothness: %dn", seed, x, y, initial, range, smooth);
// Ignore remaining characters on current line.
int ch;
while( (ch = getchar()) != EOF && ch != 'n')
;
// fetch first character on next line
flagchar1 = getchar();
} while (flagchar1 != 'y' && flagchar1 != 'Y' && flagchar1 != 'n');
printf("Welp, time to get started!nn");
printf("Twisting primes...n"); // Initializes the Mersenne twister.
mt_init(seed);
printf("Scrawling preliminary etchings...n"); // Presets as many values on the grid as is called for.
printf("Creating depth matrix...n"); // Declares grid.
int grid [x] [y];
printf("Nullifying grid constants...n"); // Sets all values in grid to 0.
for (index1 = 0; index1 < x; index1++) {
for (index2 = 0; index2 < y; index2++) {
grid [index1] [index2] = 0;
}
}
printf("Filling rhombus circumcenters...n"); // Actually runs the diamond-square algorithm.
stdroutine(initial, range, smooth, x, y, &grid);
printf("Inserting strategic aberrations..."); // Runs any postgenerational script a tag needs (currently only needed for "glacier" tag).
printf("Applying planetary fabrics...n"); // Sets the materials of all tiles6.
printf("Scraping irregularities into suface..."); // Simulates erosion.
printf("Discharging liquids...n"); // Inserts liquids.
printf("Populating biosphere...n"); // Inserts plants, animals.
printf("Constructing civilized edifices...n"); // Inserts structures.
}
菱形方块。h:
int mean (bool sqd, unsigned long mx, unsigned long my, unsigned long x, unsigned long y, int** grid [x] [y]) {
int x1 = mx, x2 = mx, x3 = mx, x4 = mx, y1 = my, y2 = my, y3 = my, y4 = my;
int avg;
if (sqd == false) {
do {
y1++;
} while (**grid [x1] [y1]);
do {
x2++;
} while (**grid [x2] [y2]);
do {
y3--;
} while (**grid [x3] [y3]);
do {
x4--;
} while (**grid [x4] [y4]);
avg = (**grid [x1] [y1] + **grid [x2] [y2] + **grid [x3] [y3] + **grid [x4] [y4]) / 4;
return avg;
}
else if (sqd == true) {
do {
x1--;
y1++;
} while (**grid [x1] [y1]);
do {
x2++;
y2++;
} while (**grid [x2] [y2]);
do {
x3++;
y3--;
} while (**grid [x3] [y3]);
do {
x4--;
y4--;
} while (**grid [x4] [y4]);
avg = (**grid [x1] [y1] + **grid [x2] [y2] + **grid [x3] [y3] + **grid [x4] [y4]) / 4;
return avg;
}
else
return 0;
}
void stdroutine (unsigned long i, unsigned long r, int h, unsigned long x, unsigned long y, int* grid [x] [y]) { // LADIES AND GENTLEMEN... THE DIAMOND-SQUARE ALGORITHM.
*grid [0] [0] = i + ((mt_random() % r) - (r/2)); // Set
*grid [x] [0] = i + ((mt_random() % r) - (r/2)); // the
*grid [0] [y] = i + ((mt_random() % r) - (r/2)); // four
*grid [x] [y] = i + ((mt_random() % r) - (r/2)); // corners.
int sect = 2; // This is the subdivision and iteration count of our diamond-square algorithm.
while (x / sect != x || y / sect != y) {
for (int n = 1; n < sect; n++) // The square algorithm: it finds a point in the middle of every square, and sets it to the mean of the four corners of the square, plus a little offset. In theory, anyway.
for (int m = 1; m < sect; m++)
if (*grid [x * (m/sect)] [y * (n/sect)] == 0) // If it's already been given a value, just leave it.
*grid [x * (m/sect)] [y * (n/sect)] = mean(true, (x * (m/sect)), (y * (n/sect)), x, y) + ((mt_random() % (r - h)) - ((r - h)/2)); // Randomize the location's height.
for (int n = 0; n == sect; n++) // The diamond algorithm: it finds a point in the middle of every diamond, and it sets it to the mean of the four corners of the diamond, plus a little offset. In theory, anyway.
for (int m = 0; m == sect; m++) {
if (n % 2 == 0)
if (m % 2 == 1)
if (*grid [x * (m/sect)] [y * (n/sect)] == 0) // Same deal here. We don't want to overwrite existing stuff.
*grid [x * (m/sect)] [y * (n/sect)] = mean (false, (x * (m/sect)), y * (n/sect), x, y, &grid) + ((mt_random() % (r - h)) - ((r - h)/2)); // Randomize the location's height.
if (n % 2 == 1)
if (m % 2 == 0)
if (*grid [x * (m/sect)] [y * (n/sect)] == 0) // Again, we only want to change positions that haven't been changed yet.
*grid [x * (m/sect)] [y * (n/sect)] = mean (false, (x * (m/sect)), (y * (n/sect)), x, y, &grid) + ((mt_random() % (r - h)) - ((r - h)/2)); // Randomize the location's height.
}
sect++; // Increment sect for the next iteration. I am perfectly aware that this could have been done as a for loop.
}
}
(mt.h只包含两个例程,mt_init
和mt_random
,它们分别初始化和运行Mersenne龙卷风PRNG。)
如何让我的网格指针与所有调用它们的函数一起发挥作用?目前,我从Xcode 4.3中得到了四个错误,其中三个在菱形方块.h中,"调用'mean'没有匹配函数",还有一个在main.cpp中,"对'stdroutine'的调用没有匹配函数。"。
编辑:嗯,比我想象的要简单。我仍然有一个问题,看评论。相关代码(main.cpp相同,但我在对stdroutine
的调用中省略了网格前的"与"号):
int mean (bool sqd, unsigned long mx, unsigned long my, unsigned long x, unsigned long y, const int grid [] [y]) {
int x1 = mx, x2 = mx, x3 = mx, x4 = mx, y1 = my, y2 = my, y3 = my, y4 = my;
int avg;
if (sqd) {
do {
x1--;
y1++;
} while (grid [x1] [y1]);
do {
x2++;
y2++;
} while (grid [x2] [y2]);
do {
x3++;
y3--;
} while (grid [x3] [y3]);
do {
x4--;
y4--;
} while (grid [x4] [y4]);
avg = (grid [x1] [y1] + grid [x2] [y2] + grid [x3] [y3] + grid [x4] [y4]) / 4;
return avg;
}
else {
do {
y1++;
} while (grid [x1] [y1]);
do {
x2++;
} while (grid [x2] [y2]);
do {
y3--;
} while (grid [x3] [y3]);
do {
x4--;
} while (grid [x4] [y4]);
avg = (grid [x1] [y1] + grid [x2] [y2] + grid [x3] [y3] + grid [x4] [y4]) / 4;
return avg;
}
}
void stdroutine (unsigned long i, unsigned long r, int h, unsigned long x, unsigned long y, int grid [] [y]) { // LADIES AND GENTLEMEN... THE DIAMOND-SQUARE ALGORITHM.
grid [0] [0] = i + ((mt_random() % r) - (r/2)); // Set
grid [x-1] [0] = i + ((mt_random() % r) - (r/2)); // the
grid [0] [y-1] = i + ((mt_random() % r) - (r/2)); // four
grid [x-1] [y-1] = i + ((mt_random() % r) - (r/2)); // corners.
int sect = 2; // This is the subdivision and iteration count of our diamond-square algorithm.
while (x / sect != x || y / sect != y) {
for (int n = 1; n < sect; n++) // The square algorithm: it finds a point in the middle of every square, and sets it to the mean of the four corners of the square, plus a little offset. In theory, anyway.
for (int m = 1; m < sect; m++)
if (grid [x * (m/sect)] [y * (n/sect)] == 0) // If it's already been given a value, just leave it.
grid [x * (m/sect)] [y * (n/sect)] = mean(true, (x * (m/sect)), (y * (n/sect)), x, y, grid) + ((mt_random() % (r - h)) - ((r - h)/2)); // Randomize the location's height.
for (int n = 0; n == sect; n++) // The diamond algorithm: it finds a point in the middle of every diamond, and it sets it to the mean of the four corners of the diamond, plus a little offset. In theory, anyway.
for (int m = 0; m == sect; m++) {
if (n % 2 == 0)
if (m % 2 == 1)
if (grid [x * (m/sect)] [y * (n/sect)] == 0) // Same deal here. We don't want to overwrite existing stuff.
grid [x * (m/sect)] [y * (n/sect)] = mean (false, (x * (m/sect)), y * (n/sect), x, y, grid) + ((mt_random() % (r - h)) - ((r - h)/2)); // Randomize the location's height.
if (n % 2 == 1)
if (m % 2 == 0)
if (grid [x * (m/sect)] [y * (n/sect)] == 0) // Again, we only want to change positions that haven't been changed yet.
grid [x * (m/sect)] [y * (n/sect)] = mean (false, (x * (m/sect)), (y * (n/sect)), x, y, grid) + ((mt_random() % (r - h)) - ((r - h)/2)); // Randomize the location's height.
}
sect++; // Increment sect for the next iteration. I am perfectly aware that this could have been done as a for loop.
}
return;
}
我一直在做更多的思考和实验。我不认为将参数声明为int grid [] [y]
是合法的C或C++,除非y是编译时已知的常数。您可以将参数设置为int grid[]
,然后将其索引为grid [y * ncolumns +x]
。您需要将指针作为&grid[0][0]
传递给方法。
您也可以使用一个方法模板来执行此操作,该模板将数组维度作为模板参数。