fairland: Simplify defaulting optional positional arguments

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
Markus Armbruster 2020-08-10 15:24:30 +02:00
parent 3501df487f
commit 6aa3d37d6c

View file

@ -145,13 +145,13 @@ static char *program_name;
static int nc, sc; /* number and size of continents */ static int nc, sc; /* number and size of continents */
static int ni, is; /* number and size of islands */ static int ni, is; /* number and size of islands */
#define DEFAULT_SPIKE 10 #define DEFAULT_SPIKE 10
static int sp; /* spike percentage */ static int sp = DEFAULT_SPIKE; /* spike percentage */
#define DEFAULT_MOUNTAIN 0 #define DEFAULT_MOUNTAIN 0
static int pm; /* mountain percentage */ static int pm = DEFAULT_MOUNTAIN; /* mountain percentage */
#define DEFAULT_CONTDIST 2 #define DEFAULT_CONTDIST 2
static int di; /* min. distance between continents */ static int di = DEFAULT_CONTDIST; /* min. distance between continents */
#define DEFAULT_ISLDIST 1 #define DEFAULT_ISLDIST 1
static int id; /* ... continents and islands */ static int id = DEFAULT_ISLDIST; /* ... continents and islands */
/* don't let the islands crash into each other. /* don't let the islands crash into each other.
1 = don't merge, 0 = merge. */ 1 = don't merge, 0 = merge. */
static int DISTINCT_ISLANDS = 1; static int DISTINCT_ISLANDS = 1;
@ -403,10 +403,11 @@ parse_args(int argc, char *argv[])
exit(1); exit(1);
} }
ni = nc;
is = sc / 2;
if (argc > 2) if (argc > 2)
ni = atoi(argv[2]); ni = atoi(argv[2]);
else
ni = nc;
if (ni < 0) { if (ni < 0) {
fprintf(stderr, "%s: number of islands must be >= 0\n", fprintf(stderr, "%s: number of islands must be >= 0\n",
program_name); program_name);
@ -415,8 +416,6 @@ parse_args(int argc, char *argv[])
if (argc > 3) if (argc > 3)
is = atoi(argv[3]); is = atoi(argv[3]);
else
is = sc / 2;
if (is < 1) { if (is < 1) {
fprintf(stderr, "%s: size of islands must be > 0\n", fprintf(stderr, "%s: size of islands must be > 0\n",
program_name); program_name);
@ -425,8 +424,6 @@ parse_args(int argc, char *argv[])
if (argc > 4) if (argc > 4)
sp = atoi(argv[4]); sp = atoi(argv[4]);
else
sp = DEFAULT_SPIKE;
if (sp < 0 || sp > 100) { if (sp < 0 || sp > 100) {
fprintf(stderr, fprintf(stderr,
"%s: spike percentage must be between 0 and 100\n", "%s: spike percentage must be between 0 and 100\n",
@ -436,8 +433,6 @@ parse_args(int argc, char *argv[])
if (argc > 5) if (argc > 5)
pm = atoi(argv[5]); pm = atoi(argv[5]);
else
pm = DEFAULT_MOUNTAIN;
if (pm < 0 || pm > 100) { if (pm < 0 || pm > 100) {
fprintf(stderr, fprintf(stderr,
"%s: mountain percentage must be between 0 and 100\n", "%s: mountain percentage must be between 0 and 100\n",
@ -447,9 +442,6 @@ parse_args(int argc, char *argv[])
if (argc > 6) if (argc > 6)
di = atoi(argv[6]); di = atoi(argv[6]);
else
di = DEFAULT_CONTDIST;
if (di < 0) { if (di < 0) {
fprintf(stderr, "%s: distance between continents must be >= 0\n", fprintf(stderr, "%s: distance between continents must be >= 0\n",
program_name); program_name);
@ -463,8 +455,6 @@ parse_args(int argc, char *argv[])
if (argc > 7) if (argc > 7)
id = atoi(argv[7]); id = atoi(argv[7]);
else
id = DEFAULT_ISLDIST;
if (id < 0) { if (id < 0) {
fprintf(stderr, fprintf(stderr,
"%s: distance from islands to continents must be >= 0\n", "%s: distance from islands to continents must be >= 0\n",