Reduce massive code duplication in setsector(), setres()
There's the same sector loop in every switch case. Loop around the switch instead.
This commit is contained in:
parent
9afc6a8e5c
commit
f4db90c849
2 changed files with 73 additions and 221 deletions
|
@ -48,110 +48,61 @@ setres(void)
|
||||||
char *p;
|
char *p;
|
||||||
struct nstr_sect nstr;
|
struct nstr_sect nstr;
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
|
char char0;
|
||||||
|
|
||||||
what = getstarg(player->argp[1],
|
what = getstarg(player->argp[1],
|
||||||
"Set what (iron, gold, oil, uranium, fertility)? ",
|
"Set what (iron, gold, oil, uranium, fertility)? ",
|
||||||
buf);
|
buf);
|
||||||
if (!what)
|
if (!what)
|
||||||
return RET_SYN;
|
return RET_SYN;
|
||||||
switch (what[0]) {
|
char0 = what[0];
|
||||||
case 'i':
|
|
||||||
if (!snxtsct(&nstr, player->argp[2]))
|
if (!snxtsct(&nstr, player->argp[2]))
|
||||||
|
return RET_SYN;
|
||||||
|
while (nxtsct(&nstr, §) > 0) {
|
||||||
|
p = getstarg(player->argp[3], "What value : ", buf);
|
||||||
|
if (!p || !*p)
|
||||||
return RET_SYN;
|
return RET_SYN;
|
||||||
while (nxtsct(&nstr, §) > 0) {
|
amt = atoi(p);
|
||||||
p = getstarg(player->argp[3], "What value : ", buf);
|
if (amt > 100)
|
||||||
if (!p || !*p)
|
amt = 100;
|
||||||
return RET_SYN;
|
if (amt < 0)
|
||||||
amt = atoi(p);
|
amt = 0;
|
||||||
if (amt > 100)
|
switch (char0) {
|
||||||
amt = 100;
|
case 'i':
|
||||||
if (amt < 0)
|
|
||||||
amt = 0;
|
|
||||||
if (sect.sct_own != 0)
|
if (sect.sct_own != 0)
|
||||||
resnoise(§, 1, "Iron ore content",
|
resnoise(§, 1, "Iron ore content",
|
||||||
(int)sect.sct_min, amt);
|
(int)sect.sct_min, amt);
|
||||||
sect.sct_min = (unsigned char)amt;
|
sect.sct_min = (unsigned char)amt;
|
||||||
putsect(§);
|
break;
|
||||||
}
|
case 'g':
|
||||||
break;
|
|
||||||
case 'g':
|
|
||||||
if (!snxtsct(&nstr, player->argp[2]))
|
|
||||||
return RET_SYN;
|
|
||||||
while (nxtsct(&nstr, §) > 0) {
|
|
||||||
p = getstarg(player->argp[3], "What value : ", buf);
|
|
||||||
if (!p || !*p)
|
|
||||||
return RET_SYN;
|
|
||||||
amt = atoi(p);
|
|
||||||
if (amt > 100)
|
|
||||||
amt = 100;
|
|
||||||
if (amt < 0)
|
|
||||||
amt = 0;
|
|
||||||
if (sect.sct_own != 0)
|
if (sect.sct_own != 0)
|
||||||
resnoise(§, 1, "Gold content",
|
resnoise(§, 1, "Gold content",
|
||||||
(int)sect.sct_gmin, amt);
|
(int)sect.sct_gmin, amt);
|
||||||
sect.sct_gmin = (unsigned char)amt;
|
sect.sct_gmin = (unsigned char)amt;
|
||||||
putsect(§);
|
break;
|
||||||
}
|
case 'o':
|
||||||
break;
|
|
||||||
case 'o':
|
|
||||||
if (!snxtsct(&nstr, player->argp[2]))
|
|
||||||
return RET_SYN;
|
|
||||||
while (nxtsct(&nstr, §) > 0) {
|
|
||||||
p = getstarg(player->argp[3], "What value : ", buf);
|
|
||||||
if (!p || !*p)
|
|
||||||
return RET_SYN;
|
|
||||||
amt = atoi(p);
|
|
||||||
if (amt > 100)
|
|
||||||
amt = 100;
|
|
||||||
if (amt < 0)
|
|
||||||
amt = 0;
|
|
||||||
if (sect.sct_own != 0)
|
if (sect.sct_own != 0)
|
||||||
resnoise(§, 1, "Oil content", (int)sect.sct_oil, amt);
|
resnoise(§, 1, "Oil content", (int)sect.sct_oil, amt);
|
||||||
sect.sct_oil = (unsigned char)amt;
|
sect.sct_oil = (unsigned char)amt;
|
||||||
putsect(§);
|
break;
|
||||||
}
|
case 'f':
|
||||||
break;
|
|
||||||
case 'f':
|
|
||||||
if (!snxtsct(&nstr, player->argp[2]))
|
|
||||||
return RET_SYN;
|
|
||||||
while (nxtsct(&nstr, §) > 0) {
|
|
||||||
p = getstarg(player->argp[3], "What value : ", buf);
|
|
||||||
if (!p || !*p)
|
|
||||||
return RET_SYN;
|
|
||||||
amt = atoi(p);
|
|
||||||
if (amt > 100)
|
|
||||||
amt = 100;
|
|
||||||
if (amt < 0)
|
|
||||||
amt = 0;
|
|
||||||
if (sect.sct_own != 0)
|
if (sect.sct_own != 0)
|
||||||
resnoise(§, 1, "Fertility content",
|
resnoise(§, 1, "Fertility content",
|
||||||
(int)sect.sct_fertil, amt);
|
(int)sect.sct_fertil, amt);
|
||||||
sect.sct_fertil = (unsigned char)amt;
|
sect.sct_fertil = (unsigned char)amt;
|
||||||
putsect(§);
|
break;
|
||||||
}
|
case 'u':
|
||||||
break;
|
|
||||||
case 'u':
|
|
||||||
if (!snxtsct(&nstr, player->argp[2]))
|
|
||||||
return RET_SYN;
|
|
||||||
while (nxtsct(&nstr, §) > 0) {
|
|
||||||
p = getstarg(player->argp[3], "What value : ", buf);
|
|
||||||
if (!p || !*p)
|
|
||||||
return RET_SYN;
|
|
||||||
amt = atoi(p);
|
|
||||||
if (amt > 100)
|
|
||||||
amt = 100;
|
|
||||||
if (amt < 0)
|
|
||||||
amt = 0;
|
|
||||||
if (sect.sct_own != 0)
|
if (sect.sct_own != 0)
|
||||||
resnoise(§, 1, "Uranium content",
|
resnoise(§, 1, "Uranium content",
|
||||||
(int)sect.sct_uran, amt);
|
(int)sect.sct_uran, amt);
|
||||||
sect.sct_uran = (unsigned char)amt;
|
sect.sct_uran = (unsigned char)amt;
|
||||||
putsect(§);
|
break;
|
||||||
|
default:
|
||||||
|
pr("huh?\n");
|
||||||
|
return RET_SYN;
|
||||||
}
|
}
|
||||||
break;
|
putsect(§);
|
||||||
default:
|
|
||||||
pr("huh?\n");
|
|
||||||
return RET_SYN;
|
|
||||||
}
|
}
|
||||||
return RET_OK;
|
return RET_OK;
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,15 +61,15 @@ setsector(void)
|
||||||
char0 = what[0];
|
char0 = what[0];
|
||||||
char1 = what[1];
|
char1 = what[1];
|
||||||
|
|
||||||
switch (char0) {
|
if (!snxtsct(&nstr, player->argp[2]))
|
||||||
case 'i':
|
return RET_SYN;
|
||||||
if (!snxtsct(&nstr, player->argp[2]))
|
while (nxtsct(&nstr, §) > 0) {
|
||||||
|
p = getstarg(player->argp[3], "What value : ", buf);
|
||||||
|
if (!p || !*p)
|
||||||
return RET_SYN;
|
return RET_SYN;
|
||||||
while (nxtsct(&nstr, §) > 0) {
|
amt = atoi(p);
|
||||||
p = getstarg(player->argp[3], "What value : ", buf);
|
switch (char0) {
|
||||||
if (!p || !*p)
|
case 'i':
|
||||||
return RET_SYN;
|
|
||||||
amt = atoi(p);
|
|
||||||
current = sect.sct_min;
|
current = sect.sct_min;
|
||||||
current += amt;
|
current += amt;
|
||||||
if (current < 0)
|
if (current < 0)
|
||||||
|
@ -80,17 +80,8 @@ setsector(void)
|
||||||
resnoise(§, 1, "Iron ore content",
|
resnoise(§, 1, "Iron ore content",
|
||||||
(int)sect.sct_min, current);
|
(int)sect.sct_min, current);
|
||||||
sect.sct_min = (unsigned char)current;
|
sect.sct_min = (unsigned char)current;
|
||||||
putsect(§);
|
break;
|
||||||
}
|
case 'g':
|
||||||
break;
|
|
||||||
case 'g':
|
|
||||||
if (!snxtsct(&nstr, player->argp[2]))
|
|
||||||
return RET_SYN;
|
|
||||||
while (nxtsct(&nstr, §) > 0) {
|
|
||||||
p = getstarg(player->argp[3], "What value : ", buf);
|
|
||||||
if (!p || !*p)
|
|
||||||
return RET_SYN;
|
|
||||||
amt = atoi(p);
|
|
||||||
current = sect.sct_gmin;
|
current = sect.sct_gmin;
|
||||||
current += amt;
|
current += amt;
|
||||||
if (current < 0)
|
if (current < 0)
|
||||||
|
@ -101,19 +92,10 @@ setsector(void)
|
||||||
resnoise(§, 1, "Gold content",
|
resnoise(§, 1, "Gold content",
|
||||||
(int)sect.sct_gmin, current);
|
(int)sect.sct_gmin, current);
|
||||||
sect.sct_gmin = (unsigned char)current;
|
sect.sct_gmin = (unsigned char)current;
|
||||||
putsect(§);
|
break;
|
||||||
}
|
case 'o':
|
||||||
break;
|
switch (char1) {
|
||||||
case 'o':
|
case 'i':
|
||||||
switch (char1) {
|
|
||||||
case 'i':
|
|
||||||
if (!snxtsct(&nstr, player->argp[2]))
|
|
||||||
return RET_SYN;
|
|
||||||
while (nxtsct(&nstr, §) > 0) {
|
|
||||||
p = getstarg(player->argp[3], "What value : ", buf);
|
|
||||||
if (!p || !*p)
|
|
||||||
return RET_SYN;
|
|
||||||
amt = atoi(p);
|
|
||||||
current = sect.sct_oil;
|
current = sect.sct_oil;
|
||||||
current += amt;
|
current += amt;
|
||||||
if (current < 0)
|
if (current < 0)
|
||||||
|
@ -124,17 +106,8 @@ setsector(void)
|
||||||
resnoise(§, 1, "Oil content",
|
resnoise(§, 1, "Oil content",
|
||||||
(int)sect.sct_oil, current);
|
(int)sect.sct_oil, current);
|
||||||
sect.sct_oil = (unsigned char)current;
|
sect.sct_oil = (unsigned char)current;
|
||||||
putsect(§);
|
break;
|
||||||
}
|
case 'w':
|
||||||
break;
|
|
||||||
case 'w':
|
|
||||||
if (!snxtsct(&nstr, player->argp[2]))
|
|
||||||
return RET_SYN;
|
|
||||||
while (nxtsct(&nstr, §) > 0) {
|
|
||||||
p = getstarg(player->argp[3], "What value : ", buf);
|
|
||||||
if (!p || !*p)
|
|
||||||
return RET_SYN;
|
|
||||||
amt = atoi(p);
|
|
||||||
if ((amt < 0) || (amt > MAXNOC - 1))
|
if ((amt < 0) || (amt > MAXNOC - 1))
|
||||||
return RET_SYN;
|
return RET_SYN;
|
||||||
pr("Owner of %s changed from %s (#%d) to %s (#%d).\n",
|
pr("Owner of %s changed from %s (#%d) to %s (#%d).\n",
|
||||||
|
@ -150,17 +123,8 @@ setsector(void)
|
||||||
"Sector %s gained from deity intervention\n",
|
"Sector %s gained from deity intervention\n",
|
||||||
xyas(sect.sct_x, sect.sct_y, amt));
|
xyas(sect.sct_x, sect.sct_y, amt));
|
||||||
sect.sct_own = (natid)amt;
|
sect.sct_own = (natid)amt;
|
||||||
putsect(§);
|
break;
|
||||||
}
|
case 'l':
|
||||||
break;
|
|
||||||
case 'l':
|
|
||||||
if (!snxtsct(&nstr, player->argp[2]))
|
|
||||||
return RET_SYN;
|
|
||||||
while (nxtsct(&nstr, §) > 0) {
|
|
||||||
p = getstarg(player->argp[3], "What value : ", buf);
|
|
||||||
if (!p || !*p)
|
|
||||||
return RET_SYN;
|
|
||||||
amt = atoi(p);
|
|
||||||
if ((amt < 0) || (amt > MAXNOC - 1))
|
if ((amt < 0) || (amt > MAXNOC - 1))
|
||||||
return RET_SYN;
|
return RET_SYN;
|
||||||
pr("Old owner of %s changed from %s (#%d) to %s (#%d).\n",
|
pr("Old owner of %s changed from %s (#%d) to %s (#%d).\n",
|
||||||
|
@ -168,22 +132,13 @@ setsector(void)
|
||||||
cname(sect.sct_oldown),
|
cname(sect.sct_oldown),
|
||||||
sect.sct_oldown, cname(amt), amt);
|
sect.sct_oldown, cname(amt), amt);
|
||||||
sect.sct_oldown = (natid)amt;
|
sect.sct_oldown = (natid)amt;
|
||||||
putsect(§);
|
break;
|
||||||
|
default:
|
||||||
|
pr("huh?\n");
|
||||||
|
return RET_SYN;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
case 'e':
|
||||||
pr("huh?\n");
|
|
||||||
return RET_SYN;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'e':
|
|
||||||
if (!snxtsct(&nstr, player->argp[2]))
|
|
||||||
return RET_SYN;
|
|
||||||
while (nxtsct(&nstr, §) > 0) {
|
|
||||||
p = getstarg(player->argp[3], "What value : ", buf);
|
|
||||||
if (!p || !*p)
|
|
||||||
return RET_SYN;
|
|
||||||
amt = atoi(p);
|
|
||||||
current = sect.sct_effic;
|
current = sect.sct_effic;
|
||||||
current += amt;
|
current += amt;
|
||||||
if (current < 0)
|
if (current < 0)
|
||||||
|
@ -193,19 +148,10 @@ setsector(void)
|
||||||
pr("Efficiency in %s changed to %d.\n",
|
pr("Efficiency in %s changed to %d.\n",
|
||||||
xyas(sect.sct_x, sect.sct_y, player->cnum), current);
|
xyas(sect.sct_x, sect.sct_y, player->cnum), current);
|
||||||
sect.sct_effic = current;
|
sect.sct_effic = current;
|
||||||
putsect(§);
|
break;
|
||||||
}
|
case 'm':
|
||||||
break;
|
switch (char1) {
|
||||||
case 'm':
|
case 'i':
|
||||||
switch (char1) {
|
|
||||||
case 'i':
|
|
||||||
if (!snxtsct(&nstr, player->argp[2]))
|
|
||||||
return RET_SYN;
|
|
||||||
while (nxtsct(&nstr, §) > 0) {
|
|
||||||
p = getstarg(player->argp[3], "What value : ", buf);
|
|
||||||
if (!p || !*p)
|
|
||||||
return RET_SYN;
|
|
||||||
amt = atoi(p);
|
|
||||||
current = sect.sct_mines;
|
current = sect.sct_mines;
|
||||||
current += amt;
|
current += amt;
|
||||||
if (current < 0)
|
if (current < 0)
|
||||||
|
@ -215,17 +161,8 @@ setsector(void)
|
||||||
if (sect.sct_own != 0)
|
if (sect.sct_own != 0)
|
||||||
resnoise(§, 1, "Mines", sect.sct_mines, current);
|
resnoise(§, 1, "Mines", sect.sct_mines, current);
|
||||||
sect.sct_mines = current;
|
sect.sct_mines = current;
|
||||||
putsect(§);
|
break;
|
||||||
}
|
case 'o':
|
||||||
break;
|
|
||||||
case 'o':
|
|
||||||
if (!snxtsct(&nstr, player->argp[2]))
|
|
||||||
return RET_SYN;
|
|
||||||
while (nxtsct(&nstr, §) > 0) {
|
|
||||||
p = getstarg(player->argp[3], "What value : ", buf);
|
|
||||||
if (!p || !*p)
|
|
||||||
return RET_SYN;
|
|
||||||
amt = atoi(p);
|
|
||||||
current = sect.sct_mobil;
|
current = sect.sct_mobil;
|
||||||
current += amt;
|
current += amt;
|
||||||
if (current < -127)
|
if (current < -127)
|
||||||
|
@ -235,22 +172,13 @@ setsector(void)
|
||||||
pr("Mobility in %s changed to %d.\n",
|
pr("Mobility in %s changed to %d.\n",
|
||||||
xyas(sect.sct_x, sect.sct_y, player->cnum), current);
|
xyas(sect.sct_x, sect.sct_y, player->cnum), current);
|
||||||
sect.sct_mobil = current;
|
sect.sct_mobil = current;
|
||||||
putsect(§);
|
break;
|
||||||
|
default:
|
||||||
|
pr("huh?\n");
|
||||||
|
return RET_SYN;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
case 'a':
|
||||||
pr("huh?\n");
|
|
||||||
return RET_SYN;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'a':
|
|
||||||
if (!snxtsct(&nstr, player->argp[2]))
|
|
||||||
return RET_SYN;
|
|
||||||
while (nxtsct(&nstr, §) > 0) {
|
|
||||||
p = getstarg(player->argp[3], "What value : ", buf);
|
|
||||||
if (!p || !*p)
|
|
||||||
return RET_SYN;
|
|
||||||
amt = atoi(p);
|
|
||||||
current = sect.sct_avail;
|
current = sect.sct_avail;
|
||||||
current += amt;
|
current += amt;
|
||||||
if (current < 0)
|
if (current < 0)
|
||||||
|
@ -260,17 +188,8 @@ setsector(void)
|
||||||
pr("Available in %s changed to %d.\n",
|
pr("Available in %s changed to %d.\n",
|
||||||
xyas(sect.sct_x, sect.sct_y, player->cnum), current);
|
xyas(sect.sct_x, sect.sct_y, player->cnum), current);
|
||||||
sect.sct_avail = (short)current;
|
sect.sct_avail = (short)current;
|
||||||
putsect(§);
|
break;
|
||||||
}
|
case 'w':
|
||||||
break;
|
|
||||||
case 'w':
|
|
||||||
if (!snxtsct(&nstr, player->argp[2]))
|
|
||||||
return RET_SYN;
|
|
||||||
while (nxtsct(&nstr, §) > 0) {
|
|
||||||
p = getstarg(player->argp[3], "What value : ", buf);
|
|
||||||
if (!p || !*p)
|
|
||||||
return RET_SYN;
|
|
||||||
amt = atoi(p);
|
|
||||||
current = sect.sct_work;
|
current = sect.sct_work;
|
||||||
current += amt;
|
current += amt;
|
||||||
if (current < 0)
|
if (current < 0)
|
||||||
|
@ -280,17 +199,8 @@ setsector(void)
|
||||||
pr("Work in %s changed to %d.\n",
|
pr("Work in %s changed to %d.\n",
|
||||||
xyas(sect.sct_x, sect.sct_y, player->cnum), current);
|
xyas(sect.sct_x, sect.sct_y, player->cnum), current);
|
||||||
sect.sct_work = (unsigned char)current;
|
sect.sct_work = (unsigned char)current;
|
||||||
putsect(§);
|
break;
|
||||||
}
|
case 'f':
|
||||||
break;
|
|
||||||
case 'f':
|
|
||||||
if (!snxtsct(&nstr, player->argp[2]))
|
|
||||||
return RET_SYN;
|
|
||||||
while (nxtsct(&nstr, §) > 0) {
|
|
||||||
p = getstarg(player->argp[3], "What value : ", buf);
|
|
||||||
if (!p || !*p)
|
|
||||||
return RET_SYN;
|
|
||||||
amt = atoi(p);
|
|
||||||
current = sect.sct_fertil;
|
current = sect.sct_fertil;
|
||||||
current += amt;
|
current += amt;
|
||||||
if (current < 0)
|
if (current < 0)
|
||||||
|
@ -301,17 +211,8 @@ setsector(void)
|
||||||
resnoise(§, 1, "Fertility content",
|
resnoise(§, 1, "Fertility content",
|
||||||
(int)sect.sct_fertil, current);
|
(int)sect.sct_fertil, current);
|
||||||
sect.sct_fertil = (unsigned char)current;
|
sect.sct_fertil = (unsigned char)current;
|
||||||
putsect(§);
|
break;
|
||||||
}
|
case 'u':
|
||||||
break;
|
|
||||||
case 'u':
|
|
||||||
if (!snxtsct(&nstr, player->argp[2]))
|
|
||||||
return RET_SYN;
|
|
||||||
while (nxtsct(&nstr, §) > 0) {
|
|
||||||
p = getstarg(player->argp[3], "What value : ", buf);
|
|
||||||
if (!p || !*p)
|
|
||||||
return RET_SYN;
|
|
||||||
amt = atoi(p);
|
|
||||||
current = sect.sct_uran;
|
current = sect.sct_uran;
|
||||||
current += amt;
|
current += amt;
|
||||||
if (current < 0)
|
if (current < 0)
|
||||||
|
@ -322,12 +223,12 @@ setsector(void)
|
||||||
resnoise(§, 1, "Uranium content",
|
resnoise(§, 1, "Uranium content",
|
||||||
(int)sect.sct_uran, current);
|
(int)sect.sct_uran, current);
|
||||||
sect.sct_uran = (unsigned char)current;
|
sect.sct_uran = (unsigned char)current;
|
||||||
putsect(§);
|
break;
|
||||||
|
default:
|
||||||
|
pr("huh?\n");
|
||||||
|
return RET_SYN;
|
||||||
}
|
}
|
||||||
break;
|
putsect(§);
|
||||||
default:
|
|
||||||
pr("huh?\n");
|
|
||||||
return RET_SYN;
|
|
||||||
}
|
}
|
||||||
return RET_OK;
|
return RET_OK;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue