Skip to content

Commit

Permalink
Follow-up to b294026
Browse files Browse the repository at this point in the history
- Added documentation for the pc->search_item function and formalized
  its return values. If the searched item is not found, now it returns
  the newly introduced constant INDEX_NOT_FOUND.
- Updated pc->search_item checks to make use of INDEX_NOT_FOUND.
- Fixed an issue with anvils not detected by the weapon forginc code,
  if they were in the first position of the inventory.
- Added ITEMID constants for the four anvil types.

Signed-off-by: Haru <haru@dotalux.com>
  • Loading branch information
MishimaHaruna committed Aug 20, 2014
1 parent b294026 commit 769b1d0
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 89 deletions.
1 change: 1 addition & 0 deletions src/common/mmo.h
Expand Up @@ -208,6 +208,7 @@ enum item_types {
IT_MAX
};

#define INDEX_NOT_FOUND (-1) ///< Used as invalid/failure value in various functions that return an index

// Questlog states
enum quest_state {
Expand Down
3 changes: 1 addition & 2 deletions src/map/atcommand.c
Expand Up @@ -8214,8 +8214,7 @@ ACMD(delitem) {
total = amount;

// delete items
while( amount && ( idx = pc->search_inventory(sd, nameid) ) != -1 )
{
while (amount && (idx = pc->search_inventory(sd, nameid)) != INDEX_NOT_FOUND) {
int delamount = ( amount < sd->status.inventory[idx].amount ) ? amount : sd->status.inventory[idx].amount;

if( sd->inventory_data[idx]->type == IT_PETEGG && sd->status.inventory[idx].card[0] == CARD0_PET )
Expand Down
5 changes: 3 additions & 2 deletions src/map/buyingstore.c
Expand Up @@ -126,8 +126,9 @@ void buyingstore_create(struct map_session_data* sd, int zenylimit, unsigned cha
break;
}

if( !id->flag.buyingstore || !itemdb->cantrade_sub(id, pc_get_group_level(sd), pc_get_group_level(sd)) || ( idx = pc->search_inventory(sd, nameid) ) == -1 )
{// restrictions: allowed, no character-bound items and at least one must be owned
if (!id->flag.buyingstore || !itemdb->cantrade_sub(id, pc_get_group_level(sd), pc_get_group_level(sd))
|| (idx = pc->search_inventory(sd, nameid)) == INDEX_NOT_FOUND
) { // restrictions: allowed, no character-bound items and at least one must be owned
break;
}

Expand Down
12 changes: 6 additions & 6 deletions src/map/clif.c
Expand Up @@ -3486,10 +3486,10 @@ void clif_arrow_create_list(struct map_session_data *sd)
WFIFOW(fd,0) = 0x1ad;

for (i = 0, c = 0; i < MAX_SKILL_ARROW_DB; i++) {
if (skill->arrow_db[i].nameid > 0 &&
(j = pc->search_inventory(sd, skill->arrow_db[i].nameid)) >= 0 &&
!sd->status.inventory[j].equip && sd->status.inventory[j].identify)
{
if (skill->arrow_db[i].nameid > 0
&& (j = pc->search_inventory(sd, skill->arrow_db[i].nameid)) != INDEX_NOT_FOUND
&& !sd->status.inventory[j].equip && sd->status.inventory[j].identify
) {
if ((j = itemdb_viewid(skill->arrow_db[i].nameid)) > 0)
WFIFOW(fd,c*2+4) = j;
else
Expand Down Expand Up @@ -14615,7 +14615,7 @@ void clif_parse_AutoRevive(int fd, struct map_session_data *sd) {
int item_position = pc->search_inventory(sd, ITEMID_TOKEN_OF_SIEGFRIED);
int hpsp = 100;

if (item_position < 0) {
if (item_position == INDEX_NOT_FOUND) {
if (sd->sc.data[SC_LIGHT_OF_REGENE])
hpsp = 20 * sd->sc.data[SC_LIGHT_OF_REGENE]->val1;
else
Expand All @@ -14628,7 +14628,7 @@ void clif_parse_AutoRevive(int fd, struct map_session_data *sd) {
if (!status->revive(&sd->bl, hpsp, hpsp))
return;

if ( item_position < 0)
if (item_position == INDEX_NOT_FOUND)
status_change_end(&sd->bl,SC_LIGHT_OF_REGENE,INVALID_TIMER);
else
pc->delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
Expand Down
4 changes: 2 additions & 2 deletions src/map/guild.c
Expand Up @@ -347,8 +347,8 @@ int guild_create(struct map_session_data *sd, const char *name)
clif->guild_created(sd,1);
return 0;
}
if( battle_config.guild_emperium_check && pc->search_inventory(sd,714) == -1 )
{// item required
if (battle_config.guild_emperium_check && pc->search_inventory(sd, ITEMID_EMPERIUM) == INDEX_NOT_FOUND) {
// item required
clif->guild_created(sd,3);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/map/homunculus.c
Expand Up @@ -578,7 +578,7 @@ bool homunculus_feed(struct map_session_data *sd, struct homun_data *hd) {

foodID = hd->homunculusDB->foodID;
i = pc->search_inventory(sd,foodID);
if(i < 0) {
if (i == INDEX_NOT_FOUND) {
clif->hom_food(sd,foodID,0);
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/map/itemdb.h
Expand Up @@ -58,6 +58,10 @@ enum item_itemid {
ITEMID_ORIDECON_STONE = 756,
ITEMID_ALCHOL = 970,
ITEMID_ORIDECON = 984,
ITEMID_ANVIL = 986,
ITEMID_ORIDECON_ANVIL = 987,
ITEMID_GOLDEN_ANVIL = 988,
ITEMID_EMPERIUM_ANVIL = 989,
ITEMID_BOODY_RED = 990,
ITEMID_CRYSTAL_BLUE = 991,
ITEMID_WIND_OF_VERDURE = 992,
Expand Down
32 changes: 19 additions & 13 deletions src/map/pc.c
Expand Up @@ -3922,16 +3922,23 @@ int pc_getzeny(struct map_session_data *sd,int zeny, enum e_log_pick_type type,
return 0;
}

/*==========================================
* Searching a specified itemid in inventory and return his stored index
*------------------------------------------*/
int pc_search_inventory(struct map_session_data *sd,int item_id)
{
/**
* Searches for the specified item ID in inventory and return its inventory index.
*
* If the item is found, the returned value is guaranteed to be a valid index
* (non-negative, smaller than MAX_INVENTORY).
*
* @param sd Character to search on.
* @param item_id The item ID to search.
* @return the inventory index of the first instance of the requested item.
* @retval INDEX_NOT_FOUND if the item wasn't found.
*/
int pc_search_inventory(struct map_session_data *sd, int item_id) {
int i;
nullpo_retr(-1, sd);
nullpo_retr(INDEX_NOT_FOUND, sd);

ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == item_id && (sd->status.inventory[i].amount > 0 || item_id == 0) );
return ( i < MAX_INVENTORY ) ? i : -1;
return ( i < MAX_INVENTORY ) ? i : INDEX_NOT_FOUND;
}

/*==========================================
Expand All @@ -3940,11 +3947,11 @@ int pc_search_inventory(struct map_session_data *sd,int item_id)
0 = success
1 = invalid itemid not found or negative amount
2 = overweight
3 = ?
3 = ?
4 = no free place found
5 = max amount reached
6 = ?
7 = stack limitation
6 = ?
7 = stack limitation
*------------------------------------------*/
int pc_additem(struct map_session_data *sd,struct item *item_data,int amount,e_log_pick_type log_type)
{
Expand Down Expand Up @@ -4011,10 +4018,9 @@ int pc_additem(struct map_session_data *sd,struct item *item_data,int amount,e_l
}
}

if( i >= MAX_INVENTORY )
{
if ( i >= MAX_INVENTORY ) {
i = pc->search_inventory(sd,0);
if( i < 0 )
if (i == INDEX_NOT_FOUND)
return 4;

memcpy(&sd->status.inventory[i], item_data, sizeof(sd->status.inventory[0]));
Expand Down
43 changes: 20 additions & 23 deletions src/map/pet.c
Expand Up @@ -743,42 +743,39 @@ int pet_unequipitem(struct map_session_data *sd, struct pet_data *pd) {
return 0;
}

int pet_food(struct map_session_data *sd, struct pet_data *pd)
{
int i,k;
int pet_food(struct map_session_data *sd, struct pet_data *pd) {
int i, food_id;

k=pd->petDB->FoodID;
i=pc->search_inventory(sd,k);
if(i < 0) {
clif->pet_food(sd,k,0);
food_id = pd->petDB->FoodID;
i = pc->search_inventory(sd, food_id);
if(i == INDEX_NOT_FOUND) {
clif->pet_food(sd, food_id, 0);
return 1;
}
pc->delitem(sd,i,1,0,0,LOG_TYPE_CONSUME);

if( pd->pet.hungry > 90 )
if (pd->pet.hungry > 90) {
pet->set_intimate(pd, pd->pet.intimate - pd->petDB->r_full);
else
{
if( battle_config.pet_friendly_rate != 100 )
k = (pd->petDB->r_hungry * battle_config.pet_friendly_rate)/100;
} else {
int add_intimate = 0;
if (battle_config.pet_friendly_rate != 100)
add_intimate = (pd->petDB->r_hungry * battle_config.pet_friendly_rate)/100;
else
k = pd->petDB->r_hungry;
if( pd->pet.hungry > 75 )
{
k = k >> 1;
if( k <= 0 )
k = 1;
add_intimate = pd->petDB->r_hungry;
if (pd->pet.hungry > 75) {
add_intimate = add_intimate >> 1;
if (add_intimate <= 0)
add_intimate = 1;
}
pet->set_intimate(pd, pd->pet.intimate + k);
pet->set_intimate(pd, pd->pet.intimate + add_intimate);
}
if( pd->pet.intimate <= 0 )
{
if (pd->pet.intimate <= 0) {
pd->pet.intimate = 0;
pet_stop_attack(pd);
pd->status.speed = pd->db->status.speed;
}
else if( pd->pet.intimate > 1000 )
} else if (pd->pet.intimate > 1000) {
pd->pet.intimate = 1000;
}
status_calc_pet(pd, SCO_NONE);
pd->pet.hungry += pd->petDB->fullness;
if( pd->pet.hungry > 100 )
Expand Down
2 changes: 1 addition & 1 deletion src/map/script.c
Expand Up @@ -10343,7 +10343,7 @@ BUILDIN(homunculus_mutate) {
m_id = homun->class2type(homun_id);

if( m_class == HT_EVO && m_id == HT_S &&
sd->hd->homunculus.level >= 99 && i >= 0 &&
sd->hd->homunculus.level >= 99 && i != INDEX_NOT_FOUND &&
!pc->delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT) ) {
sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
homun->call(sd); // Respawn homunculus.
Expand Down

0 comments on commit 769b1d0

Please sign in to comment.