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:
Markus Armbruster 2011-04-16 20:30:22 +02:00
parent 9afc6a8e5c
commit f4db90c849
2 changed files with 73 additions and 221 deletions

View file

@ -48,14 +48,15 @@ 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; return RET_SYN;
while (nxtsct(&nstr, &sect) > 0) { while (nxtsct(&nstr, &sect) > 0) {
@ -67,91 +68,41 @@ setres(void)
amt = 100; amt = 100;
if (amt < 0) if (amt < 0)
amt = 0; amt = 0;
switch (char0) {
case 'i':
if (sect.sct_own != 0) if (sect.sct_own != 0)
resnoise(&sect, 1, "Iron ore content", resnoise(&sect, 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(&sect);
}
break; break;
case 'g': case 'g':
if (!snxtsct(&nstr, player->argp[2]))
return RET_SYN;
while (nxtsct(&nstr, &sect) > 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(&sect, 1, "Gold content", resnoise(&sect, 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(&sect);
}
break; break;
case 'o': case 'o':
if (!snxtsct(&nstr, player->argp[2]))
return RET_SYN;
while (nxtsct(&nstr, &sect) > 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(&sect, 1, "Oil content", (int)sect.sct_oil, amt); resnoise(&sect, 1, "Oil content", (int)sect.sct_oil, amt);
sect.sct_oil = (unsigned char)amt; sect.sct_oil = (unsigned char)amt;
putsect(&sect);
}
break; break;
case 'f': case 'f':
if (!snxtsct(&nstr, player->argp[2]))
return RET_SYN;
while (nxtsct(&nstr, &sect) > 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(&sect, 1, "Fertility content", resnoise(&sect, 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(&sect);
}
break; break;
case 'u': case 'u':
if (!snxtsct(&nstr, player->argp[2]))
return RET_SYN;
while (nxtsct(&nstr, &sect) > 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(&sect, 1, "Uranium content", resnoise(&sect, 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(&sect);
}
break; break;
default: default:
pr("huh?\n"); pr("huh?\n");
return RET_SYN; return RET_SYN;
} }
putsect(&sect);
}
return RET_OK; return RET_OK;
} }

View file

@ -61,8 +61,6 @@ setsector(void)
char0 = what[0]; char0 = what[0];
char1 = what[1]; char1 = what[1];
switch (char0) {
case 'i':
if (!snxtsct(&nstr, player->argp[2])) if (!snxtsct(&nstr, player->argp[2]))
return RET_SYN; return RET_SYN;
while (nxtsct(&nstr, &sect) > 0) { while (nxtsct(&nstr, &sect) > 0) {
@ -70,6 +68,8 @@ setsector(void)
if (!p || !*p) if (!p || !*p)
return RET_SYN; return RET_SYN;
amt = atoi(p); amt = atoi(p);
switch (char0) {
case 'i':
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(&sect, 1, "Iron ore content", resnoise(&sect, 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(&sect);
}
break; break;
case 'g': case 'g':
if (!snxtsct(&nstr, player->argp[2]))
return RET_SYN;
while (nxtsct(&nstr, &sect) > 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(&sect, 1, "Gold content", resnoise(&sect, 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(&sect);
}
break; break;
case 'o': case 'o':
switch (char1) { switch (char1) {
case 'i': case 'i':
if (!snxtsct(&nstr, player->argp[2]))
return RET_SYN;
while (nxtsct(&nstr, &sect) > 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(&sect, 1, "Oil content", resnoise(&sect, 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(&sect);
}
break; break;
case 'w': case 'w':
if (!snxtsct(&nstr, player->argp[2]))
return RET_SYN;
while (nxtsct(&nstr, &sect) > 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(&sect);
}
break; break;
case 'l': case 'l':
if (!snxtsct(&nstr, player->argp[2]))
return RET_SYN;
while (nxtsct(&nstr, &sect) > 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,8 +132,6 @@ 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(&sect);
}
break; break;
default: default:
pr("huh?\n"); pr("huh?\n");
@ -177,13 +139,6 @@ setsector(void)
} }
break; break;
case 'e': case 'e':
if (!snxtsct(&nstr, player->argp[2]))
return RET_SYN;
while (nxtsct(&nstr, &sect) > 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(&sect);
}
break; break;
case 'm': case 'm':
switch (char1) { switch (char1) {
case 'i': case 'i':
if (!snxtsct(&nstr, player->argp[2]))
return RET_SYN;
while (nxtsct(&nstr, &sect) > 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(&sect, 1, "Mines", sect.sct_mines, current); resnoise(&sect, 1, "Mines", sect.sct_mines, current);
sect.sct_mines = current; sect.sct_mines = current;
putsect(&sect);
}
break; break;
case 'o': case 'o':
if (!snxtsct(&nstr, player->argp[2]))
return RET_SYN;
while (nxtsct(&nstr, &sect) > 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,8 +172,6 @@ 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(&sect);
}
break; break;
default: default:
pr("huh?\n"); pr("huh?\n");
@ -244,13 +179,6 @@ setsector(void)
} }
break; break;
case 'a': case 'a':
if (!snxtsct(&nstr, player->argp[2]))
return RET_SYN;
while (nxtsct(&nstr, &sect) > 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(&sect);
}
break; break;
case 'w': case 'w':
if (!snxtsct(&nstr, player->argp[2]))
return RET_SYN;
while (nxtsct(&nstr, &sect) > 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(&sect);
}
break; break;
case 'f': case 'f':
if (!snxtsct(&nstr, player->argp[2]))
return RET_SYN;
while (nxtsct(&nstr, &sect) > 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(&sect, 1, "Fertility content", resnoise(&sect, 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(&sect);
}
break; break;
case 'u': case 'u':
if (!snxtsct(&nstr, player->argp[2]))
return RET_SYN;
while (nxtsct(&nstr, &sect) > 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,13 +223,13 @@ setsector(void)
resnoise(&sect, 1, "Uranium content", resnoise(&sect, 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(&sect);
}
break; break;
default: default:
pr("huh?\n"); pr("huh?\n");
return RET_SYN; return RET_SYN;
} }
putsect(&sect);
}
return RET_OK; return RET_OK;
} }