move error handling for use_cmd into a single location

This commit is contained in:
Enno Rehling 2012-05-19 21:01:25 -07:00
parent bcb01742ae
commit c9902a1d25
2 changed files with 22 additions and 17 deletions

View File

@ -2473,9 +2473,8 @@ static void reshow(unit * u, struct order *ord, const char *s, param_t p)
} }
/* last, check if it's a race. */ /* last, check if it's a race. */
rc = findrace(s, u->faction->locale); rc = findrace(s, u->faction->locale);
if (rc != NULL) { if (rc != NULL && display_race(u->faction, u, rc)) {
if (display_race(u->faction, u, rc)) break;
break;
} }
cmistake(u, ord, 21, MSG_EVENT); cmistake(u, ord, 21, MSG_EVENT);
break; break;
@ -3529,19 +3528,16 @@ use_item(unit * u, const item_type * itype, int amount, struct order *ord)
amount = i; amount = i;
} }
if (amount == 0) { if (amount == 0) {
cmistake(u, ord, 43, MSG_PRODUCE);
return ENOITEM; return ENOITEM;
} }
if (target == -1) { if (target == -1) {
if (itype->use == NULL) { if (itype->use == NULL) {
cmistake(u, ord, 76, MSG_PRODUCE);
return EUNUSABLE; return EUNUSABLE;
} }
return itype->use(u, itype, amount, ord); return itype->use(u, itype, amount, ord);
} else { } else {
if (itype->useonother == NULL) { if (itype->useonother == NULL) {
cmistake(u, ord, 76, MSG_PRODUCE);
return EUNUSABLE; return EUNUSABLE;
} }
return itype->useonother(u, target, itype, amount, ord); return itype->useonother(u, target, itype, amount, ord);
@ -3746,7 +3742,7 @@ static void age_factions(void)
static int use_cmd(unit * u, struct order *ord) static int use_cmd(unit * u, struct order *ord)
{ {
const char *t; const char *t;
int n; int n, err = ENOITEM;
const item_type *itype; const item_type *itype;
init_tokens(ord); init_tokens(ord);
@ -3770,15 +3766,24 @@ static int use_cmd(unit * u, struct order *ord)
itype = finditemtype(t, u->faction->locale); itype = finditemtype(t, u->faction->locale);
if (itype != NULL) { if (itype != NULL) {
int i = use_item(u, itype, n, ord); err = use_item(u, itype, n, ord);
assert(i <= 0 || !"use_item should not return positive values."); assert(err <= 0 || !"use_item should not return positive values.");
if (i > 0) { if (err > 0) {
log_error("use_item returned a value>0 for %s\n", resourcename(itype->rtype, 0)); log_error("use_item returned a value>0 for %s\n", resourcename(itype->rtype, 0));
} }
} else {
cmistake(u, ord, 43, MSG_PRODUCE);
} }
return 0; switch (err) {
case ENOITEM:
cmistake(u, ord, 43, MSG_PRODUCE);
break;
case EUNUSABLE:
cmistake(u, ord, 76, MSG_PRODUCE);
break;
case ENOSKILL:
cmistake(u, ord, 50, MSG_PRODUCE);
break;
}
return err;
} }
static int pay_cmd(unit * u, struct order *ord) static int pay_cmd(unit * u, struct order *ord)

View File

@ -119,10 +119,10 @@ extern "C" {
#define ITF_VEHICLE 0x0100 /* a vehicle, drawn by two animals */ #define ITF_VEHICLE 0x0100 /* a vehicle, drawn by two animals */
/* error codes for item_type::use */ /* error codes for item_type::use */
#define ECUSTOM -1; #define ECUSTOM -1
#define ENOITEM -2; #define ENOITEM -2
#define ENOSKILL -3; #define ENOSKILL -3
#define EUNUSABLE -4; #define EUNUSABLE -4
typedef struct item_type { typedef struct item_type {
resource_type *rtype; resource_type *rtype;