Make cPlayer::give_item() return the reason for failure

This commit is contained in:
2025-03-19 18:38:25 -05:00
parent 599f1030f2
commit c63bbd96dc
8 changed files with 42 additions and 34 deletions

View File

@@ -2033,7 +2033,7 @@ void debug_give_item() {
if(i == -1) return;
bool was_ident = univ.scenario.scen_items[i].ident;
univ.scenario.scen_items[i].ident = true;
bool given = univ.current_pc().give_item(univ.scenario.scen_items[i], GIVE_DO_PRINT | GIVE_ALLOW_OVERLOAD);
bool given = univ.current_pc().give_item(univ.scenario.scen_items[i], GIVE_DO_PRINT | GIVE_ALLOW_OVERLOAD) == eBuyStatus::OK;
if(!given){
ASB("Debug: can't give to " + univ.current_pc().name);
given = univ.party.give_item(univ.scenario.scen_items[i], GIVE_DO_PRINT | GIVE_ALLOW_OVERLOAD);
@@ -2055,7 +2055,7 @@ void debug_overburden() {
cPlayer& pc = univ.current_pc();
cItem item(ITEM_DEBUG_HEAVY);
// Give the PC very heavy objects that do nothing:
while(pc.give_item(item, GIVE_ALLOW_OVERLOAD)){}
while(pc.give_item(item, GIVE_ALLOW_OVERLOAD) == eBuyStatus::OK){}
if(pc.has_space()){
// I don't know why this would ever happen, since the weight is 0, but just in case:
ASB("Debug: failed to fill " + pc.name + "'s inventory.");

View File

@@ -207,17 +207,24 @@ void give_thing(short pc_num, short item_num) {
univ.party[pc_num].items[item_num].charges -= how_many;
item_store.charges = how_many;
}
if(univ.party[who_to].give_item(item_store,0)) {
if(take_given_item)
univ.party[pc_num].take_item(item_num);
}
else {
if(!univ.party[who_to].has_space())
eBuyStatus give_status = univ.party[who_to].give_item(item_store,0);
switch(give_status){
case eBuyStatus::OK:
if(take_given_item){
univ.party[pc_num].take_item(item_num);
}
break;
case eBuyStatus::NO_SPACE:
ASB("Can't give: PC has max. # of items.");
else ASB("Can't give: PC carrying too much.");
// Can't give to the recipient. Put charges back in giver's inventory:
if(how_many > 0)
univ.party[pc_num].items[item_num].charges += how_many;
if(false) // Skip first line of fallthrough
case eBuyStatus::TOO_HEAVY:
ASB("Can't give: PC carrying too much.");
// Can't give to the recipient. Put charges back in giver's inventory:
if(how_many > 0)
univ.party[pc_num].items[item_num].charges += how_many;
break;
default:
break;
}
}
}

View File

@@ -2166,7 +2166,7 @@ void do_alchemy() {
cItem store_i(potion);
store_i.charges = info.charges(skill);
store_i.graphic_num += get_ran(1,0,2);
if(!univ.party[pc_num].give_item(store_i,false)) {
if(univ.party[pc_num].give_item(store_i,false) != eBuyStatus::OK) {
add_string_to_buf("No room in inventory. Potion placed on floor.", 2);
place_item(store_i,univ.party.town_loc);
}

View File

@@ -3275,7 +3275,7 @@ void affect_spec(const runtime_state& ctx) {
bool success = true;
for(short i = 0; i < 6; i++)
if(pc_num == 6 || pc_num == i)
success = success && univ.party[i].give_item(to_give, equip_type | GIVE_ALLOW_OVERLOAD);
success = success && univ.party[i].give_item(to_give, equip_type | GIVE_ALLOW_OVERLOAD) == eBuyStatus::OK;
if(!success)
ctx.next_spec = spec.pic;
}