Simplify buil(): factor "can't build" handling out of switch cases

This commit is contained in:
Markus Armbruster 2011-04-23 09:22:00 +02:00
parent 164b90760f
commit 61e00d5559

View file

@ -69,8 +69,7 @@ buil(void)
int rqtech; int rqtech;
int tlev; int tlev;
int type; int type;
char what; char *p, *what, *prompt;
char *p, *prompt;
int gotsect = 0; int gotsect = 0;
int (*build_it)(struct sctstr *, int, short[], int); int (*build_it)(struct sctstr *, int, short[], int);
int number; int number;
@ -84,20 +83,22 @@ buil(void)
buf); buf);
if (!p) if (!p)
return RET_SYN; return RET_SYN;
what = *p; switch (*p) {
switch (what) {
case 'b': case 'b':
case 't': case 't':
return build_bridge(what); return build_bridge(*p);
case 's': case 's':
what = "ship";
prompt = "Ship type? "; prompt = "Ship type? ";
build_it = build_ship; build_it = build_ship;
break; break;
case 'p': case 'p':
what = "plane";
prompt = "Plane type? "; prompt = "Plane type? ";
build_it = build_plane; build_it = build_plane;
break; break;
case 'l': case 'l':
what = "land";
prompt = "Land unit type? "; prompt = "Land unit type? ";
build_it = build_land; build_it = build_land;
break; break;
@ -109,6 +110,7 @@ buil(void)
if (drnuke_const > MIN_DRNUKE_CONST) if (drnuke_const > MIN_DRNUKE_CONST)
tlev = MIN(tlev, tlev = MIN(tlev,
(int)(natp->nat_level[NAT_RLEV] / drnuke_const)); (int)(natp->nat_level[NAT_RLEV] / drnuke_const));
what = "nuke";
prompt = "Nuke type? "; prompt = "Nuke type? ";
build_it = build_nuke; build_it = build_nuke;
break; break;
@ -124,72 +126,46 @@ buil(void)
if (!p || !*p) if (!p || !*p)
return RET_SYN; return RET_SYN;
switch (what) { rqtech = 0;
switch (*what) {
case 'p': case 'p':
type = ef_elt_byname(EF_PLANE_CHR, p); type = ef_elt_byname(EF_PLANE_CHR, p);
if (type >= 0) { if (type >= 0)
rqtech = plchr[type].pl_tech; rqtech = plchr[type].pl_tech;
if (rqtech > tlev)
type = -1;
}
if (type < 0) {
pr("You can't build that!\n");
pr("Use `show plane build %d' to show types you can build.\n",
tlev);
return RET_FAIL;
}
break; break;
case 's': case 's':
type = ef_elt_byname(EF_SHIP_CHR, p); type = ef_elt_byname(EF_SHIP_CHR, p);
if (type >= 0) { if (type >= 0) {
rqtech = mchr[type].m_tech; rqtech = mchr[type].m_tech;
if (rqtech > tlev)
type = -1;
if ((mchr[type].m_flags & M_TRADE) && !opt_TRADESHIPS) if ((mchr[type].m_flags & M_TRADE) && !opt_TRADESHIPS)
type = -1; type = -1;
} }
if (type < 0) {
pr("You can't build that!\n");
pr("Use `show ship build %d' to show types you can build.\n",
tlev);
return RET_FAIL;
}
break; break;
case 'l': case 'l':
type = ef_elt_byname(EF_LAND_CHR, p); type = ef_elt_byname(EF_LAND_CHR, p);
if (type >= 0) { if (type >= 0) {
rqtech = lchr[type].l_tech; rqtech = lchr[type].l_tech;
if (rqtech > tlev)
type = -1;
if ((lchr[type].l_flags & L_SPY) && !opt_LANDSPIES) if ((lchr[type].l_flags & L_SPY) && !opt_LANDSPIES)
type = -1; type = -1;
} }
if (type < 0) {
pr("You can't build that!\n");
pr("Use `show land build %d' to show types you can build.\n",
tlev);
return RET_FAIL;
}
break; break;
case 'n': case 'n':
type = ef_elt_byname(EF_NUKE_CHR, p); type = ef_elt_byname(EF_NUKE_CHR, p);
if (type >= 0) { if (type >= 0)
rqtech = nchr[type].n_tech; rqtech = nchr[type].n_tech;
if (rqtech > tlev)
type = -1;
}
if (type < 0) {
pr("You can't build that!\n");
pr("Use `show nuke build %d' to show types you can build.\n",
tlev);
return RET_FAIL;
}
break; break;
default: default:
CANT_REACH(); CANT_REACH();
return RET_FAIL; return RET_FAIL;
} }
if (type < 0 || tlev < rqtech) {
pr("You can't build that!\n");
pr("Use `show %s build %d' to show types you can build.\n",
what, tlev);
return RET_FAIL;
}
number = 1; number = 1;
if (player->argp[4]) { if (player->argp[4]) {
number = atoi(player->argp[4]); number = atoi(player->argp[4]);
@ -204,7 +180,7 @@ buil(void)
} }
} }
if (what != 'n') { if (*what != 'n') {
if (player->argp[5]) { if (player->argp[5]) {
tlev = atoi(player->argp[5]); tlev = atoi(player->argp[5]);
if (tlev > natp->nat_level[NAT_TLEV] && !player->god) { if (tlev > natp->nat_level[NAT_TLEV] && !player->god) {