diff --git a/src/code_8045A00.c b/src/code_8045A00.c index 497396ad6..cc9756af5 100644 --- a/src/code_8045A00.c +++ b/src/code_8045A00.c @@ -21,21 +21,9 @@ #include "dungeon_util.h" #include "structs/str_item_text.h" -extern u8 *gUnknown_80FE6F4[]; -extern u8 *gPickedUpItemToolbox[]; -extern u8 *gMonTerrifiedCouldntPickUpItem[]; -extern u8 *gPickedUpItem2[]; -extern u8 *gMonCouldntPickUpItem[]; -extern u8 *gPickedUpItem[]; -extern u8 *gMonSteppedOnItem[]; -extern struct unkStruct_8090F58 gUnknown_80F699C; -extern struct unkStruct_8090F58 gUnknown_80F69A8; -extern struct unkStruct_8090F58 gUnknown_80F6990; - -bool8 sub_80461C8(DungeonPos *, bool8); void sub_8045BF8(u8 *, Item *); -extern u32 sub_803D73C(u32); -bool8 sub_80460F8(DungeonPos *, Item *, bool8); + +extern u8 *gUnknown_80FE6F4[]; void sub_8045ACC(void) { @@ -102,679 +90,5 @@ void SubstitutePlaceholderStringTags(u8 *buffer, Entity *entity, u32 param_3) // FILE SPLIT HERE -void sub_8045BF8(u8 *buffer, Item *item) -{ - sub_8090E14(buffer, item, &gUnknown_80F699C); -} - -void sub_8045C08(u8 *buffer, Item *item) -{ - sub_8090E14(buffer, item, &gUnknown_80F69A8); -} - -void sub_8045C18(u8 *buffer, Item *item) -{ - sub_8090E14(buffer, item, &gUnknown_80F6990); -} - -void sub_8045C28(Item *item, u8 itemID, u32 param_3) -{ - bool8 stickyFlag; - - xxx_init_itemslot_8090A8C(item,itemID,0); - stickyFlag = FALSE; - if (IsNotSpecialItem(itemID)) { - if (param_3 == 0) { - if (DungeonRandInt(100) < gDungeon->unk1C574.unkA) - stickyFlag = TRUE; - else - stickyFlag = FALSE; - } - else if (param_3 == 1) { - stickyFlag = TRUE; - } - } - if (stickyFlag) { - item->flags |= ITEM_FLAG_STICKY; - } - - if (GetItemCategory(itemID) == CATEGORY_POKE) { - sub_8046CE4(item, gDungeon->unk1C574.unk17 * 40); - } -} - -void sub_8045CB0(void) -{ - u8 itemID; - s32 yCounter, xCounter; - const Tile *tile; - u32 uVar5; - Item item; - u32 flag; - s32 x = DungeonRandInt(DUNGEON_MAX_SIZE_X); - s32 y = DungeonRandInt(DUNGEON_MAX_SIZE_Y); - - gDungeon->numItems = 0; - for (yCounter = 0; yCounter < DUNGEON_MAX_SIZE_Y; yCounter++) { - y++; - if (y == DUNGEON_MAX_SIZE_Y) { - y = 0; - } - - for (xCounter = 0; xCounter < DUNGEON_MAX_SIZE_X; xCounter++) { - x++; - flag = ITEM_FLAG_IN_SHOP; - if (x == DUNGEON_MAX_SIZE_X) { - x = 0; - } - tile = GetTile(x,y); - - if (!(tile->terrainType & TERRAIN_TYPE_STAIRS) && (tile->spawnOrVisibilityFlags & 2)) { - DungeonPos pos; - bool8 shopFlag = FALSE; - pos.x = x; - pos.y = y; - - if (tile->terrainType & TERRAIN_TYPE_SHOP) { - shopFlag = TRUE; - uVar5 = 1; - } - else - { - if (GetTerrainType(tile) == TERRAIN_TYPE_WALL) { - uVar5 = 3; - } - else { - uVar5 = (tile->terrainType & TERRAIN_TYPE_IN_MONSTER_HOUSE) ? 2 : 0; - } - } - itemID = sub_803D73C(uVar5); - if (!CanSellItem(itemID)) { - shopFlag = 0; - } - sub_8045C28(&item,itemID,0); - if (shopFlag) { - item.flags |= flag; - } - sub_80460F8(&pos,&item,TRUE); - } - } - } -} - -void sub_8045DB4(struct DungeonPos *pos, bool8 printMsg) -{ - Item *tileItem; - int inventoryIds[INVENTORY_SIZE + 1]; - Item *inventoryItems[INVENTORY_SIZE + 1]; - Entity *leader = GetLeader(); - EntityInfo *leaderInfo = GetEntInfo(leader); - const Tile *tile = GetTile(pos->x,pos->y); - Entity *tileObject = tile->object; - if (tileObject == NULL) { - return; - } - if (GetEntityType(tileObject) != ENTITY_ITEM) { - return; - } - tileItem = GetItemData(tileObject); - if (leaderInfo->action.actionParameters[0].actionUseIndex == 0) { - if (!printMsg) { - return; - } - sub_8045BF8(gFormatBuffer_Items[0],tileItem); - SubstitutePlaceholderStringTags(gFormatBuffer_Monsters[0],leader,0); - LogMessageByIdWithPopupCheckUser(leader,*gMonSteppedOnItem); - } - else if (ShouldMonsterRunAwayAndShowEffect(leader,1)) { - if (!printMsg) { - return; - } - sub_8045BF8(gFormatBuffer_Items[0],tileItem); - SubstitutePlaceholderStringTags(gFormatBuffer_Monsters[0],leader,0); - LogMessageByIdWithPopupCheckUser(leader,*gMonTerrifiedCouldntPickUpItem); - } - else if (GetItemCategory(tileItem->id) == CATEGORY_POKE) { - PlaySoundEffect(0x14c); - AddToTeamMoney(GetMoneyValue(tileItem)); - sub_8045BF8(gFormatBuffer_Items[0],tileItem); - sub_80461C8(pos,1); - LogMessageByIdWithPopupCheckUser(leader,*gPickedUpItem); - TryDisplayItemPickupTutorialMessage(tileItem->id); - } - else { - s32 i, maxItems; - if (gDungeon->unk644.unk17 != 0) { - for (i = 0; i < INVENTORY_SIZE; i++) { - inventoryItems[i] = &gTeamInventoryRef->teamItems[i]; - inventoryIds[i] = i; - } - maxItems = INVENTORY_SIZE; - } - else { - inventoryItems[0] = &leaderInfo->heldItem; - inventoryIds[0] = -1; - maxItems = 1; - } - - if (tileItem->id <= ITEM_GEO_PEBBLE && !(tileItem->flags & ITEM_FLAG_IN_SHOP)) { - s32 index = -1; - s32 numberItems = -1; - s32 i; - - for (i = 0; i < maxItems; i++) { - if (ItemExists(inventoryItems[i]) - && !(inventoryItems[i]->flags & ITEM_FLAG_IN_SHOP) - && (tileItem->id == inventoryItems[i]->id) - && (tileItem->flags & ITEM_FLAG_STICKY) == (inventoryItems[i]->flags & ITEM_FLAG_STICKY) - && inventoryItems[i]->quantity != 99 - && numberItems < inventoryItems[i]->quantity) - { - numberItems = inventoryItems[i]->quantity; - index = i; - } - } - - if (index == -1) { - s32 i; - - numberItems = -1; - for (i = 0; i < maxItems; i++) { - if (ItemExists(inventoryItems[i]) - && !(inventoryItems[i]->flags & ITEM_FLAG_IN_SHOP) - && tileItem->id == inventoryItems[i]->id - && inventoryItems[i]->quantity != 99 - && numberItems < inventoryItems[i]->quantity) - { - numberItems = inventoryItems[i]->quantity; - index = i; - } - } - } - - if (index != -1) { - struct Item *addedItem = inventoryItems[index]; - s32 newQuantity = addedItem->quantity + tileItem->quantity; - - if (newQuantity >= 99) { - newQuantity = 99; - } - addedItem->quantity = newQuantity; - - if (tileItem->flags & ITEM_FLAG_STICKY) { - inventoryItems[index]->flags |= ITEM_FLAG_STICKY; - } - sub_8045BF8(gFormatBuffer_Items[0],tileItem); - sub_80461C8(pos,1); - PlaySoundEffect(0x14a); - if (inventoryIds[index] < 0) { - LogMessageByIdWithPopupCheckUser(leader,*gPickedUpItem2); - } - else { - LogMessageByIdWithPopupCheckUser(leader,*gPickedUpItemToolbox); - } - TryDisplayItemPickupTutorialMessage(tileItem->id); - return; - } - } - - for (i = 0; i < maxItems; i++) { - if (!ItemExists(inventoryItems[i])) - break; - } - - if (i == maxItems) { - if (!printMsg) { - return; - } - sub_8045BF8(gFormatBuffer_Items[0],tileItem); - SubstitutePlaceholderStringTags(gFormatBuffer_Monsters[0],leader,0); - LogMessageByIdWithPopupCheckUser(leader,*gMonSteppedOnItem); - } - else { - PlaySoundEffect(0x14a); - if (inventoryIds[i] < 0) { - leaderInfo->heldItem = *tileItem; - sub_8045BF8(gFormatBuffer_Items[0],tileItem); - sub_80461C8(pos,1); - LogMessageByIdWithPopupCheckUser(leader,*gPickedUpItem2); - } - else if (AddItemToInventory(tileItem) != 0) { - SubstitutePlaceholderStringTags(gFormatBuffer_Monsters[0],leader,0); - LogMessageByIdWithPopupCheckUser(leader,*gMonCouldntPickUpItem); - } - else { - sub_8045BF8(gFormatBuffer_Items[0],tileItem); - sub_80461C8(pos,1); - LogMessageByIdWithPopupCheckUser(leader,*gPickedUpItemToolbox); - } - TryDisplayItemPickupTutorialMessage(tileItem->id); - } - } -} - -bool8 sub_80460F8(DungeonPos *pos, Item *item, bool8 a2) -{ - s32 i, count; - Tile *tile; - Entity *entity = sub_8045708(pos); - - if (entity == NULL) - return FALSE; - *entity->axObj.info.item = *item; - entity->axObj.info.item->flags &= ~(ITEM_FLAG_SET); - entity->isVisible = TRUE; - tile = GetTileMut(pos->x, pos->y); - tile->object = entity; - if ((tile->terrainType & TERRAIN_TYPE_SHOP) && CanSellItem(item->id)) { - if (item->flags & ITEM_FLAG_IN_SHOP) { - if (a2) { - gDungeon->unk644.unk48 += GetStackBuyPrice(item); - } - } - else { - gDungeon->unk644.unk4C += GetStackSellPrice(item); - } - } - - count = 0; - for (i = 0; i < DUNGEON_MAX_ITEMS; i++) { - if (EntityIsValid(gDungeon->items[i])) { - count = i + 1; - } - } - gDungeon->numItems = count; - return TRUE; -} - -bool8 sub_80461C8(DungeonPos *pos, bool8 a2) -{ - s32 i, count; - Item *item; - Tile *tile = GetTileMut(pos->x, pos->y); - Entity *tileObject = tile->object; - if (tileObject == NULL || GetEntityType(tileObject) != ENTITY_ITEM) - return FALSE; - - item = GetItemData(tileObject); - if ((tile->terrainType & TERRAIN_TYPE_SHOP) && CanSellItem(item->id)) { - if (item->flags & ITEM_FLAG_IN_SHOP) { - if (a2) { - gDungeon->unk644.unk48 -= GetStackBuyPrice(item); - } - } - else { - gDungeon->unk644.unk4C -= GetStackSellPrice(item); - } - } - - tile->object = NULL; - tileObject->type = ENTITY_NOTHING; - - count = 0; - for (i = 0; i < DUNGEON_MAX_ITEMS; i++) { - if (EntityIsValid(gDungeon->items[i])) { - count = i + 1; - } - } - gDungeon->numItems = count; - return TRUE; -} - -s32 sub_8046298(Item *item) -{ - return gItemParametersData[item->id].icon; -} - -extern bool8 sub_8045888(Entity *); - -extern SpriteOAM gUnknown_202EDC0; -extern u8 gUnknown_203B420[]; -extern u8 gUnknown_203B428[]; - -bool8 sub_80462AC(Entity * entity, u8 a1, u8 a2, u8 a3, u8 a4) -{ - s32 x, y, y2; - s32 objMode = 0; - - if (!EntityIsValid(entity)) - return FALSE; - - if (a2) { - s32 terrainType = GetTerrainType(GetTile(entity->pos.x, entity->pos.y)); - - if (terrainType == TERRAIN_TYPE_WALL) - return FALSE; - if (terrainType == TERRAIN_TYPE_SECONDARY) { - objMode = 1; - } - - if (!sub_8045888(entity)) - return FALSE; - } - - x = (entity->pixelPos.x / 256) - gDungeon->unk181e8.cameraPixelPos.x; - y = ((entity->pixelPos.y - entity->unk1C.raw) / 256) - gDungeon->unk181e8.cameraPixelPos.y; - y2 = ((entity->pixelPos.y / 256) - gDungeon->unk181e8.cameraPixelPos.y) + 8; - y2 /= 2; - - if (x >= -32 && y >= -32 && x <= 272 && y <= 192) { - s32 tileNum; - - SpriteSetMatrixNum(&gUnknown_202EDC0, 0); - if (a1) { - tileNum = 0x17; - SpriteSetPalNum(&gUnknown_202EDC0, 10); - } - else { - tileNum = sub_8046298(entity->axObj.info.item); - if (a3 != 0xFF && tileNum == 0) { - tileNum = gUnknown_203B420[a3]; - SpriteSetMatrixNum(&gUnknown_202EDC0, gUnknown_203B428[a3] * 8); - } - SpriteSetPalNum(&gUnknown_202EDC0, GetItemPalette(entity->axObj.info.item->id)); - } - - SpriteSetY(&gUnknown_202EDC0, y); - SpriteSetX(&gUnknown_202EDC0, x); - SpriteSetObjMode(&gUnknown_202EDC0, objMode); - SpriteSetPriority(&gUnknown_202EDC0, 3); - SpriteSetTileNum(&gUnknown_202EDC0, 0x1A0 + tileNum * 4); - if (!a4) { - y2 = 1; - } - AddSprite(&gUnknown_202EDC0, y2, NULL, NULL); - return TRUE; - } - - return FALSE; -} - -const u8 *sub_80464AC(Item *item) -{ - return gActions[GetItemActionType(item->id)].desc; -} - -void sub_804652C(Entity *entity1, Entity *entity2, Item *item, bool8 a3, DungeonPos *pos); - -void sub_80464C8(Entity *entity, DungeonPos *pos, Item *item) -{ - s32 x, y; - Entity itemEntity; - - itemEntity.type = ENTITY_ITEM; - itemEntity.unk24 = 0; - itemEntity.isVisible = TRUE; - itemEntity.unk22 = 0; - itemEntity.axObj.info.item = item; - itemEntity.pos = *pos; - - x = ((pos->x * 24) + 4) << 8; - y = ((pos->y * 24) + 4) << 8; - SetEntityPixelPos(&itemEntity, x, y); - itemEntity.spawnGenID = 0; - sub_804652C(entity, &itemEntity, item, TRUE, NULL); -} - -void sub_8046734(Entity *entity, DungeonPos *pos); - -extern void sub_804219C(PixelPos *pos); - -extern const u8 *const gUnknown_80FED30; -extern const DungeonPos gUnknown_80F4468[]; -extern const u8 *const gItemFellOnGround; -extern const u8 *const gItemLost; -extern const u8 *const gItemFellOutOfSight; -extern const u8 *const gItemFellInWater; -extern const u8 *const gItemBuried; - -void sub_804652C(Entity *entity1, Entity *entity2, Item *item, bool8 a3, DungeonPos *pos) -{ - DungeonPos localPos; - DungeonPos localPos2; - s32 i; - const Tile *tile = GetTile(entity2->pos.x, entity2->pos.y); - Entity *tileObject = tile->object; - bool8 var_24 = FALSE; - - if (tileObject != NULL && GetEntityType(tileObject) == ENTITY_TRAP) { - sub_8046734(entity2, &entity2->pos); - sub_807FE44(&entity2->pos, 1); - LogMessageByIdWithPopupCheckUser(entity1, gUnknown_80FED30); - if (!ItemExists(item)) - return; - } - - localPos.x = entity2->pos.x; - localPos.y = entity2->pos.y; - localPos2 = localPos; - if (a3) { - i = 0; - } - else { - i = 1; - } - - while (1) { - if (gUnknown_80F4468[i].x == 99) { - var_24 = FALSE; - break; - } - localPos.x = entity2->pos.x + gUnknown_80F4468[i].x; - localPos.y = entity2->pos.y + gUnknown_80F4468[i].y; - tile = GetTile(localPos.x, localPos.y); - if (GetTerrainType(tile) != TERRAIN_TYPE_WALL && !(tile->terrainType & TERRAIN_TYPE_STAIRS) && tile->object == NULL) { - sub_8046734(entity2, &localPos); - localPos2 = localPos; - if (GetTerrainType(tile) == (TERRAIN_TYPE_SECONDARY | TERRAIN_TYPE_NORMAL) || (sub_80460F8(&localPos, item, TRUE))) { - var_24 = TRUE; - } - break; - } - i++; - } - - sub_8045BF8(gFormatBuffer_Items[0], item); - if (var_24) { - ShowDungeonMapAtPos(localPos.x, localPos.y); - switch (GetTerrainType(GetTile(localPos.x, localPos.y))) { - case TERRAIN_TYPE_NORMAL: - TryDisplayDungeonLoggableMessage5(entity1, &localPos, gItemFellOnGround); - break; - case TERRAIN_TYPE_SECONDARY: - TryDisplayDungeonLoggableMessage5(entity1, &localPos, gItemFellInWater); - break; - case TERRAIN_TYPE_WALL: - TryDisplayDungeonLoggableMessage5(entity1, &localPos, gItemBuried); - break; - case TERRAIN_TYPE_SECONDARY | TERRAIN_TYPE_NORMAL: - TryDisplayDungeonLoggableMessage5(entity1, &localPos, gItemFellOutOfSight); - break; - } - } - else { - PixelPos pixelPos; - - pixelPos.x = X_POS_TO_PIXELPOS(localPos2.x); - pixelPos.y = X_POS_TO_PIXELPOS(localPos2.y); // Intentional or a typo(X vs Y)? - if (pos != NULL) { - pixelPos.x += pos->x << 8; - pixelPos.y += pos->y << 8; - } - - sub_804219C(&pixelPos); - TryDisplayDungeonLoggableMessage5(entity1, &localPos, gItemLost); - } -} - -extern void DungeonRunFrameActions(u32); - -void sub_8046734(Entity *entity, DungeonPos *pos) -{ - PixelPos posPixel; - PixelPos calcPixelPos; - bool8 hallucinating = gDungeon->unk181e8.hallucinating; - - posPixel.x = ((pos->x * 24) + 4) << 8; - posPixel.y = ((pos->y * 24) + 4) << 8; - if (abs(entity->pixelPos.x - posPixel.x) > 11 || abs(entity->pixelPos.y - posPixel.y) > 11) { - s32 i; - PixelPos add; - s32 unk = 0; // Direction? - s32 sinVal = 0; - - calcPixelPos.x = entity->pixelPos.x; - calcPixelPos.y = entity->pixelPos.y; - - add.x = (posPixel.x - calcPixelPos.x) / 24; - add.y = (posPixel.y - calcPixelPos.y) / 24; - for (i = 0; i < 24; i++) { - calcPixelPos.x += add.x; - calcPixelPos.y += add.y; - entity->unk1C.raw = sin_4096(sinVal) * 12; - sub_804535C(entity, &calcPixelPos); - sub_80462AC(entity, hallucinating, 0, unk, 0); - DungeonRunFrameActions(0x13); - sinVal += 85; - if ((i & 3) == 0) { - unk++; - } - unk &= 7; - } - } - - SetEntityPixelPos(entity, posPixel.x, posPixel.y); - entity->unk1C.raw = 0; - sub_80462AC(entity, hallucinating, 1, 0xFF, 0); - DungeonRunFrameActions(0x13); -} - -extern void sub_804687C(Entity *entity, DungeonPos *pos1, DungeonPos *pos2, Item *item, s32 a4); - -void sub_8046860(Entity *entity, DungeonPos *pos, Item *item, s32 a4) -{ - sub_804687C(entity, pos, pos, item, a4); -} - -extern const u8 *gSeveralItemsLost; -extern const u8 *gItemLost; -extern const u8 *gAllItemsLost; - -void sub_804687C(Entity *entity, DungeonPos *pos1, DungeonPos *pos2, Item *item, s32 count) -{ - Entity itemEntities[10]; - DungeonPos targetTilePos[10]; - PixelPos itemVelocity[10]; - u8 targetTileUsed[30]; - s32 sinVal; - s32 i; - bool8 r1, r9; - - for (i = 0; i < 30; i++) { - targetTileUsed[i] = 0; - } - - for (i = 0; i < count; i++) { - s32 j; - bool8 foundTile = FALSE; - DungeonPos pos; - - pos.x = pos2->x; - pos.y = pos2->y; - - for (j = 0; j < 30; j++) { - if (gUnknown_80F4468[j].x == 99) - break; - if (!targetTileUsed[j]) { - const Tile *tile; - - pos.x = pos2->x + gUnknown_80F4468[j].x; - pos.y = pos2->y + gUnknown_80F4468[j].y; - - tile = GetTile(pos.x, pos.y); - if (GetTerrainType(tile) != TERRAIN_TYPE_WALL && !(tile->terrainType & TERRAIN_TYPE_STAIRS) && tile->object == NULL) { - targetTilePos[i] = pos; - targetTileUsed[j] = TRUE; - foundTile = TRUE; - break; - } - } - } - - if (!foundTile) { - targetTilePos[i].x = -1; - targetTilePos[i].y = -1; - } - } - - r1 = FALSE; - r9 = FALSE; - for (i = 0; i < count; i++) { - if (targetTilePos[i].x < 0) { - itemEntities[i].type = ENTITY_NOTHING; - r9 = TRUE; - } - else { - s32 x, y; - - itemEntities[i].type = ENTITY_ITEM; - itemEntities[i].unk24 = 0; - itemEntities[i].isVisible = TRUE; - itemEntities[i].unk22 = 0; - itemEntities[i].axObj.info.item = &item[i]; - itemEntities[i].pos = targetTilePos[i]; - x = ((pos1->x * 24) + 4) << 8; - y = ((pos1->y * 24) + 4) << 8; - SetEntityPixelPos(&itemEntities[i], x, y); - itemEntities[i].spawnGenID = 0; - itemVelocity[i].x = ((((targetTilePos[i].x * 24) + 4) << 8) - (itemEntities[i].pixelPos.x)) / 24; - itemVelocity[i].y = ((((targetTilePos[i].y * 24) + 4) << 8) - (itemEntities[i].pixelPos.y)) / 24; - r1 = TRUE; - } - } - - if (!r1) { - if (count > 1) { - LogMessageByIdWithPopupCheckUser(entity, gAllItemsLost); - } - else { - LogMessageByIdWithPopupCheckUser(entity, gItemLost); - } - } - else { - s32 counter; - s32 dirMaybe; - bool8 hallucinating; - - if (r9) { - LogMessageByIdWithPopupCheckUser(entity, gSeveralItemsLost); - } - - dirMaybe = 0; - hallucinating = gDungeon->unk181e8.hallucinating; - sinVal = 0; - for (counter = 0; counter < 24; counter++) { - for (i = 0; i < count; i++) { - if (EntityIsValid(&itemEntities[i])) { - IncreaseEntityPixelPos(&itemEntities[i], itemVelocity[i].x, itemVelocity[i].y); - itemEntities[i].unk1C.raw = sin_4096(sinVal) * 12; - sub_80462AC(&itemEntities[i], hallucinating, 1, dirMaybe, 0); - } - } - DungeonRunFrameActions(0x13); - sinVal += 85; - if ((counter & 3) == 0) { - dirMaybe++; - } - dirMaybe &= 7; - } - - for (i = 0; i < count; i++) { - if (targetTilePos[i].x >= 0) { - sub_80460F8(&targetTilePos[i], &item[i], TRUE); - } - } - } -} // diff --git a/src/dungeon_items.c b/src/dungeon_items.c index d9bcbddf8..19df08df7 100644 --- a/src/dungeon_items.c +++ b/src/dungeon_items.c @@ -1,6 +1,5 @@ #include "global.h" #include "dungeon_items.h" - #include "code_803E668.h" #include "code_8041AD0.h" #include "code_8045A00.h" @@ -12,6 +11,20 @@ #include "dungeon_util.h" #include "pokemon.h" #include "structs/str_dungeon.h" +#include "constants/item.h" +#include "dungeon_map_access.h" +#include "dungeon_message.h" +#include "dungeon_music.h" +#include "dungeon_map.h" +#include "dungeon_ai_targeting.h" +#include "string_format.h" +#include "items.h" +#include "status_checks_1.h" +#include "structs/map.h" +#include "trap.h" +#include "math.h" +#include "code_80450F8.h" +#include "structs/str_item_text.h" extern s32 gUnknown_810A3F0[]; extern u8 *gUnknown_80FA408[]; @@ -19,30 +32,706 @@ extern u8 *gUnknown_810531C[]; extern u8 *gUnknown_8105360[]; extern u8 *gUnknown_81053A8[]; extern u8 *gUnknown_8105434[]; +extern u8 *gPickedUpItemToolbox[]; +extern u8 *gMonTerrifiedCouldntPickUpItem[]; +extern u8 *gPickedUpItem2[]; +extern u8 *gMonCouldntPickUpItem[]; +extern u8 *gPickedUpItem[]; +extern u8 *gMonSteppedOnItem[]; +extern struct unkStruct_8090F58 gUnknown_80F699C; +extern struct unkStruct_8090F58 gUnknown_80F69A8; +extern struct unkStruct_8090F58 gUnknown_80F6990; +extern const u8 *const gUnknown_80FED30; +extern const DungeonPos gUnknown_80F4468[]; +extern const u8 *const gItemFellOnGround; +extern const u8 *const gItemLost; +extern const u8 *const gItemFellOutOfSight; +extern const u8 *const gItemFellInWater; +extern const u8 *const gItemBuried; +extern const u8 *gSeveralItemsLost; +extern const u8 *gItemLost; +extern const u8 *gAllItemsLost; extern void sub_80416E0(PixelPos *r0, u32, bool8); extern void sub_80855E4(void *); extern void PlaySoundEffect(u32); extern void sub_804178C(u32); -extern void ShowWholeRevealedDungeonMap(void); +extern void sub_804219C(PixelPos *pos); +extern void DungeonRunFrameActions(u32); +extern bool8 sub_8045888(Entity *); static void MusicBoxCreation(void); static u8 sub_8046D70(void); +extern SpriteOAM gUnknown_202EDC0; +extern u8 gUnknown_203B420[]; +extern u8 gUnknown_203B428[]; + +void sub_8046734(Entity *entity, DungeonPos *pos); +void sub_804687C(Entity *entity, DungeonPos *pos1, DungeonPos *pos2, Item *item, s32 a4); +void sub_804652C(Entity *entity1, Entity *entity2, Item *item, bool8 a3, DungeonPos *pos); +bool8 sub_80461C8(DungeonPos *, bool8); +extern u32 sub_803D73C(u32); +bool8 sub_80460F8(DungeonPos *, Item *, bool8); + +void sub_8045BF8(u8 *buffer, Item *item) +{ + sub_8090E14(buffer, item, &gUnknown_80F699C); +} + +void sub_8045C08(u8 *buffer, Item *item) +{ + sub_8090E14(buffer, item, &gUnknown_80F69A8); +} + +void sub_8045C18(u8 *buffer, Item *item) +{ + sub_8090E14(buffer, item, &gUnknown_80F6990); +} + +void sub_8045C28(Item *item, u8 itemID, u32 param_3) +{ + bool8 stickyFlag; + + xxx_init_itemslot_8090A8C(item,itemID,0); + stickyFlag = FALSE; + if (IsNotSpecialItem(itemID)) { + if (param_3 == 0) { + if (DungeonRandInt(100) < gDungeon->unk1C574.unkA) + stickyFlag = TRUE; + else + stickyFlag = FALSE; + } + else if (param_3 == 1) { + stickyFlag = TRUE; + } + } + if (stickyFlag) { + item->flags |= ITEM_FLAG_STICKY; + } + + if (GetItemCategory(itemID) == CATEGORY_POKE) { + sub_8046CE4(item, gDungeon->unk1C574.unk17 * 40); + } +} + +void sub_8045CB0(void) +{ + u8 itemID; + s32 yCounter, xCounter; + const Tile *tile; + u32 uVar5; + Item item; + u32 flag; + s32 x = DungeonRandInt(DUNGEON_MAX_SIZE_X); + s32 y = DungeonRandInt(DUNGEON_MAX_SIZE_Y); + + gDungeon->numItems = 0; + for (yCounter = 0; yCounter < DUNGEON_MAX_SIZE_Y; yCounter++) { + y++; + if (y == DUNGEON_MAX_SIZE_Y) { + y = 0; + } + + for (xCounter = 0; xCounter < DUNGEON_MAX_SIZE_X; xCounter++) { + x++; + flag = ITEM_FLAG_IN_SHOP; + if (x == DUNGEON_MAX_SIZE_X) { + x = 0; + } + tile = GetTile(x,y); + + if (!(tile->terrainType & TERRAIN_TYPE_STAIRS) && (tile->spawnOrVisibilityFlags & 2)) { + DungeonPos pos; + bool8 shopFlag = FALSE; + pos.x = x; + pos.y = y; + + if (tile->terrainType & TERRAIN_TYPE_SHOP) { + shopFlag = TRUE; + uVar5 = 1; + } + else + { + if (GetTerrainType(tile) == TERRAIN_TYPE_WALL) { + uVar5 = 3; + } + else { + uVar5 = (tile->terrainType & TERRAIN_TYPE_IN_MONSTER_HOUSE) ? 2 : 0; + } + } + itemID = sub_803D73C(uVar5); + if (!CanSellItem(itemID)) { + shopFlag = 0; + } + sub_8045C28(&item,itemID,0); + if (shopFlag) { + item.flags |= flag; + } + sub_80460F8(&pos,&item,TRUE); + } + } + } +} + +void sub_8045DB4(struct DungeonPos *pos, bool8 printMsg) +{ + Item *tileItem; + int inventoryIds[INVENTORY_SIZE + 1]; + Item *inventoryItems[INVENTORY_SIZE + 1]; + Entity *leader = GetLeader(); + EntityInfo *leaderInfo = GetEntInfo(leader); + const Tile *tile = GetTile(pos->x,pos->y); + Entity *tileObject = tile->object; + if (tileObject == NULL) { + return; + } + if (GetEntityType(tileObject) != ENTITY_ITEM) { + return; + } + tileItem = GetItemData(tileObject); + if (leaderInfo->action.actionParameters[0].actionUseIndex == 0) { + if (!printMsg) { + return; + } + sub_8045BF8(gFormatBuffer_Items[0],tileItem); + SubstitutePlaceholderStringTags(gFormatBuffer_Monsters[0],leader,0); + LogMessageByIdWithPopupCheckUser(leader,*gMonSteppedOnItem); + } + else if (ShouldMonsterRunAwayAndShowEffect(leader,1)) { + if (!printMsg) { + return; + } + sub_8045BF8(gFormatBuffer_Items[0],tileItem); + SubstitutePlaceholderStringTags(gFormatBuffer_Monsters[0],leader,0); + LogMessageByIdWithPopupCheckUser(leader,*gMonTerrifiedCouldntPickUpItem); + } + else if (GetItemCategory(tileItem->id) == CATEGORY_POKE) { + PlaySoundEffect(0x14c); + AddToTeamMoney(GetMoneyValue(tileItem)); + sub_8045BF8(gFormatBuffer_Items[0],tileItem); + sub_80461C8(pos,1); + LogMessageByIdWithPopupCheckUser(leader,*gPickedUpItem); + TryDisplayItemPickupTutorialMessage(tileItem->id); + } + else { + s32 i, maxItems; + if (gDungeon->unk644.unk17 != 0) { + for (i = 0; i < INVENTORY_SIZE; i++) { + inventoryItems[i] = &gTeamInventoryRef->teamItems[i]; + inventoryIds[i] = i; + } + maxItems = INVENTORY_SIZE; + } + else { + inventoryItems[0] = &leaderInfo->heldItem; + inventoryIds[0] = -1; + maxItems = 1; + } + + if (tileItem->id <= ITEM_GEO_PEBBLE && !(tileItem->flags & ITEM_FLAG_IN_SHOP)) { + s32 index = -1; + s32 numberItems = -1; + s32 i; + + for (i = 0; i < maxItems; i++) { + if (ItemExists(inventoryItems[i]) + && !(inventoryItems[i]->flags & ITEM_FLAG_IN_SHOP) + && (tileItem->id == inventoryItems[i]->id) + && (tileItem->flags & ITEM_FLAG_STICKY) == (inventoryItems[i]->flags & ITEM_FLAG_STICKY) + && inventoryItems[i]->quantity != 99 + && numberItems < inventoryItems[i]->quantity) + { + numberItems = inventoryItems[i]->quantity; + index = i; + } + } + + if (index == -1) { + s32 i; + + numberItems = -1; + for (i = 0; i < maxItems; i++) { + if (ItemExists(inventoryItems[i]) + && !(inventoryItems[i]->flags & ITEM_FLAG_IN_SHOP) + && tileItem->id == inventoryItems[i]->id + && inventoryItems[i]->quantity != 99 + && numberItems < inventoryItems[i]->quantity) + { + numberItems = inventoryItems[i]->quantity; + index = i; + } + } + } + + if (index != -1) { + struct Item *addedItem = inventoryItems[index]; + s32 newQuantity = addedItem->quantity + tileItem->quantity; + + if (newQuantity >= 99) { + newQuantity = 99; + } + addedItem->quantity = newQuantity; + + if (tileItem->flags & ITEM_FLAG_STICKY) { + inventoryItems[index]->flags |= ITEM_FLAG_STICKY; + } + sub_8045BF8(gFormatBuffer_Items[0],tileItem); + sub_80461C8(pos,1); + PlaySoundEffect(0x14a); + if (inventoryIds[index] < 0) { + LogMessageByIdWithPopupCheckUser(leader,*gPickedUpItem2); + } + else { + LogMessageByIdWithPopupCheckUser(leader,*gPickedUpItemToolbox); + } + TryDisplayItemPickupTutorialMessage(tileItem->id); + return; + } + } + + for (i = 0; i < maxItems; i++) { + if (!ItemExists(inventoryItems[i])) + break; + } + + if (i == maxItems) { + if (!printMsg) { + return; + } + sub_8045BF8(gFormatBuffer_Items[0],tileItem); + SubstitutePlaceholderStringTags(gFormatBuffer_Monsters[0],leader,0); + LogMessageByIdWithPopupCheckUser(leader,*gMonSteppedOnItem); + } + else { + PlaySoundEffect(0x14a); + if (inventoryIds[i] < 0) { + leaderInfo->heldItem = *tileItem; + sub_8045BF8(gFormatBuffer_Items[0],tileItem); + sub_80461C8(pos,1); + LogMessageByIdWithPopupCheckUser(leader,*gPickedUpItem2); + } + else if (AddItemToInventory(tileItem) != 0) { + SubstitutePlaceholderStringTags(gFormatBuffer_Monsters[0],leader,0); + LogMessageByIdWithPopupCheckUser(leader,*gMonCouldntPickUpItem); + } + else { + sub_8045BF8(gFormatBuffer_Items[0],tileItem); + sub_80461C8(pos,1); + LogMessageByIdWithPopupCheckUser(leader,*gPickedUpItemToolbox); + } + TryDisplayItemPickupTutorialMessage(tileItem->id); + } + } +} + +bool8 sub_80460F8(DungeonPos *pos, Item *item, bool8 a2) +{ + s32 i, count; + Tile *tile; + Entity *entity = sub_8045708(pos); + + if (entity == NULL) + return FALSE; + *entity->axObj.info.item = *item; + entity->axObj.info.item->flags &= ~(ITEM_FLAG_SET); + entity->isVisible = TRUE; + tile = GetTileMut(pos->x, pos->y); + tile->object = entity; + if ((tile->terrainType & TERRAIN_TYPE_SHOP) && CanSellItem(item->id)) { + if (item->flags & ITEM_FLAG_IN_SHOP) { + if (a2) { + gDungeon->unk644.unk48 += GetStackBuyPrice(item); + } + } + else { + gDungeon->unk644.unk4C += GetStackSellPrice(item); + } + } + + count = 0; + for (i = 0; i < DUNGEON_MAX_ITEMS; i++) { + if (EntityIsValid(gDungeon->items[i])) { + count = i + 1; + } + } + gDungeon->numItems = count; + return TRUE; +} + +bool8 sub_80461C8(DungeonPos *pos, bool8 a2) +{ + s32 i, count; + Item *item; + Tile *tile = GetTileMut(pos->x, pos->y); + Entity *tileObject = tile->object; + if (tileObject == NULL || GetEntityType(tileObject) != ENTITY_ITEM) + return FALSE; + + item = GetItemData(tileObject); + if ((tile->terrainType & TERRAIN_TYPE_SHOP) && CanSellItem(item->id)) { + if (item->flags & ITEM_FLAG_IN_SHOP) { + if (a2) { + gDungeon->unk644.unk48 -= GetStackBuyPrice(item); + } + } + else { + gDungeon->unk644.unk4C -= GetStackSellPrice(item); + } + } + + tile->object = NULL; + tileObject->type = ENTITY_NOTHING; + + count = 0; + for (i = 0; i < DUNGEON_MAX_ITEMS; i++) { + if (EntityIsValid(gDungeon->items[i])) { + count = i + 1; + } + } + gDungeon->numItems = count; + return TRUE; +} + +s32 sub_8046298(Item *item) +{ + return gItemParametersData[item->id].icon; +} + +bool8 sub_80462AC(Entity * entity, u8 a1, u8 a2, u8 a3, u8 a4) +{ + s32 x, y, y2; + s32 objMode = 0; + + if (!EntityIsValid(entity)) + return FALSE; + + if (a2) { + s32 terrainType = GetTerrainType(GetTile(entity->pos.x, entity->pos.y)); + + if (terrainType == TERRAIN_TYPE_WALL) + return FALSE; + if (terrainType == TERRAIN_TYPE_SECONDARY) { + objMode = 1; + } + + if (!sub_8045888(entity)) + return FALSE; + } + + x = (entity->pixelPos.x / 256) - gDungeon->unk181e8.cameraPixelPos.x; + y = ((entity->pixelPos.y - entity->unk1C.raw) / 256) - gDungeon->unk181e8.cameraPixelPos.y; + y2 = ((entity->pixelPos.y / 256) - gDungeon->unk181e8.cameraPixelPos.y) + 8; + y2 /= 2; + + if (x >= -32 && y >= -32 && x <= 272 && y <= 192) { + s32 tileNum; + + SpriteSetMatrixNum(&gUnknown_202EDC0, 0); + if (a1) { + tileNum = 0x17; + SpriteSetPalNum(&gUnknown_202EDC0, 10); + } + else { + tileNum = sub_8046298(entity->axObj.info.item); + if (a3 != 0xFF && tileNum == 0) { + tileNum = gUnknown_203B420[a3]; + SpriteSetMatrixNum(&gUnknown_202EDC0, gUnknown_203B428[a3] * 8); + } + SpriteSetPalNum(&gUnknown_202EDC0, GetItemPalette(entity->axObj.info.item->id)); + } + + SpriteSetY(&gUnknown_202EDC0, y); + SpriteSetX(&gUnknown_202EDC0, x); + SpriteSetObjMode(&gUnknown_202EDC0, objMode); + SpriteSetPriority(&gUnknown_202EDC0, 3); + SpriteSetTileNum(&gUnknown_202EDC0, 0x1A0 + tileNum * 4); + if (!a4) { + y2 = 1; + } + AddSprite(&gUnknown_202EDC0, y2, NULL, NULL); + return TRUE; + } + + return FALSE; +} + +const u8 *sub_80464AC(Item *item) +{ + return gActions[GetItemActionType(item->id)].desc; +} + +void sub_80464C8(Entity *entity, DungeonPos *pos, Item *item) +{ + + s32 x, y; + Entity itemEntity; + + itemEntity.type = ENTITY_ITEM; + itemEntity.unk24 = 0; + itemEntity.isVisible = TRUE; + itemEntity.unk22 = 0; + itemEntity.axObj.info.item = item; + itemEntity.pos = *pos; + + x = ((pos->x * 24) + 4) << 8; + y = ((pos->y * 24) + 4) << 8; + SetEntityPixelPos(&itemEntity, x, y); + itemEntity.spawnGenID = 0; + sub_804652C(entity, &itemEntity, item, TRUE, NULL); +} + +void sub_804652C(Entity *entity1, Entity *entity2, Item *item, bool8 a3, DungeonPos *pos) +{ + DungeonPos localPos; + DungeonPos localPos2; + s32 i; + const Tile *tile = GetTile(entity2->pos.x, entity2->pos.y); + Entity *tileObject = tile->object; + bool8 var_24 = FALSE; + + if (tileObject != NULL && GetEntityType(tileObject) == ENTITY_TRAP) { + sub_8046734(entity2, &entity2->pos); + sub_807FE44(&entity2->pos, 1); + LogMessageByIdWithPopupCheckUser(entity1, gUnknown_80FED30); + if (!ItemExists(item)) + return; + } + + localPos.x = entity2->pos.x; + localPos.y = entity2->pos.y; + localPos2 = localPos; + if (a3) { + i = 0; + } + else { + i = 1; + } + + while (1) { + if (gUnknown_80F4468[i].x == 99) { + var_24 = FALSE; + break; + } + localPos.x = entity2->pos.x + gUnknown_80F4468[i].x; + localPos.y = entity2->pos.y + gUnknown_80F4468[i].y; + tile = GetTile(localPos.x, localPos.y); + if (GetTerrainType(tile) != TERRAIN_TYPE_WALL && !(tile->terrainType & TERRAIN_TYPE_STAIRS) && tile->object == NULL) { + sub_8046734(entity2, &localPos); + localPos2 = localPos; + if (GetTerrainType(tile) == (TERRAIN_TYPE_SECONDARY | TERRAIN_TYPE_NORMAL) || (sub_80460F8(&localPos, item, TRUE))) { + var_24 = TRUE; + } + break; + } + i++; + } + + sub_8045BF8(gFormatBuffer_Items[0], item); + if (var_24) { + ShowDungeonMapAtPos(localPos.x, localPos.y); + switch (GetTerrainType(GetTile(localPos.x, localPos.y))) { + case TERRAIN_TYPE_NORMAL: + TryDisplayDungeonLoggableMessage5(entity1, &localPos, gItemFellOnGround); + break; + case TERRAIN_TYPE_SECONDARY: + TryDisplayDungeonLoggableMessage5(entity1, &localPos, gItemFellInWater); + break; + case TERRAIN_TYPE_WALL: + TryDisplayDungeonLoggableMessage5(entity1, &localPos, gItemBuried); + break; + case TERRAIN_TYPE_SECONDARY | TERRAIN_TYPE_NORMAL: + TryDisplayDungeonLoggableMessage5(entity1, &localPos, gItemFellOutOfSight); + break; + } + } + else { + PixelPos pixelPos; + + pixelPos.x = X_POS_TO_PIXELPOS(localPos2.x); + pixelPos.y = X_POS_TO_PIXELPOS(localPos2.y); // Intentional or a typo(X vs Y)? + if (pos != NULL) { + pixelPos.x += pos->x << 8; + pixelPos.y += pos->y << 8; + } + + sub_804219C(&pixelPos); + TryDisplayDungeonLoggableMessage5(entity1, &localPos, gItemLost); + } +} + +void sub_8046734(Entity *entity, DungeonPos *pos) +{ + PixelPos posPixel; + PixelPos calcPixelPos; + bool8 hallucinating = gDungeon->unk181e8.hallucinating; + + posPixel.x = ((pos->x * 24) + 4) << 8; + posPixel.y = ((pos->y * 24) + 4) << 8; + if (abs(entity->pixelPos.x - posPixel.x) > 11 || abs(entity->pixelPos.y - posPixel.y) > 11) { + s32 i; + PixelPos add; + s32 unk = 0; // Direction? + s32 sinVal = 0; + + calcPixelPos.x = entity->pixelPos.x; + calcPixelPos.y = entity->pixelPos.y; + + add.x = (posPixel.x - calcPixelPos.x) / 24; + add.y = (posPixel.y - calcPixelPos.y) / 24; + for (i = 0; i < 24; i++) { + calcPixelPos.x += add.x; + calcPixelPos.y += add.y; + entity->unk1C.raw = sin_4096(sinVal) * 12; + sub_804535C(entity, &calcPixelPos); + sub_80462AC(entity, hallucinating, 0, unk, 0); + DungeonRunFrameActions(0x13); + sinVal += 85; + if ((i & 3) == 0) { + unk++; + } + unk &= 7; + } + } + + SetEntityPixelPos(entity, posPixel.x, posPixel.y); + entity->unk1C.raw = 0; + sub_80462AC(entity, hallucinating, 1, 0xFF, 0); + DungeonRunFrameActions(0x13); +} + +void sub_8046860(Entity *entity, DungeonPos *pos, Item *item, s32 a4) +{ + sub_804687C(entity, pos, pos, item, a4); +} + +void sub_804687C(Entity *entity, DungeonPos *pos1, DungeonPos *pos2, Item *item, s32 count) +{ + Entity itemEntities[10]; + DungeonPos targetTilePos[10]; + PixelPos itemVelocity[10]; + u8 targetTileUsed[30]; + s32 sinVal; + s32 i; + bool8 r1, r9; + + for (i = 0; i < 30; i++) { + targetTileUsed[i] = 0; + } + + for (i = 0; i < count; i++) { + s32 j; + bool8 foundTile = FALSE; + DungeonPos pos; + + pos.x = pos2->x; + pos.y = pos2->y; + + for (j = 0; j < 30; j++) { + if (gUnknown_80F4468[j].x == 99) + break; + if (!targetTileUsed[j]) { + const Tile *tile; + + pos.x = pos2->x + gUnknown_80F4468[j].x; + pos.y = pos2->y + gUnknown_80F4468[j].y; + + tile = GetTile(pos.x, pos.y); + if (GetTerrainType(tile) != TERRAIN_TYPE_WALL && !(tile->terrainType & TERRAIN_TYPE_STAIRS) && tile->object == NULL) { + targetTilePos[i] = pos; + targetTileUsed[j] = TRUE; + foundTile = TRUE; + break; + } + } + } + + if (!foundTile) { + targetTilePos[i].x = -1; + targetTilePos[i].y = -1; + } + } + + r1 = FALSE; + r9 = FALSE; + for (i = 0; i < count; i++) { + if (targetTilePos[i].x < 0) { + itemEntities[i].type = ENTITY_NOTHING; + r9 = TRUE; + } + else { + s32 x, y; + + itemEntities[i].type = ENTITY_ITEM; + itemEntities[i].unk24 = 0; + itemEntities[i].isVisible = TRUE; + itemEntities[i].unk22 = 0; + itemEntities[i].axObj.info.item = &item[i]; + itemEntities[i].pos = targetTilePos[i]; + x = ((pos1->x * 24) + 4) << 8; + y = ((pos1->y * 24) + 4) << 8; + SetEntityPixelPos(&itemEntities[i], x, y); + itemEntities[i].spawnGenID = 0; + itemVelocity[i].x = ((((targetTilePos[i].x * 24) + 4) << 8) - (itemEntities[i].pixelPos.x)) / 24; + itemVelocity[i].y = ((((targetTilePos[i].y * 24) + 4) << 8) - (itemEntities[i].pixelPos.y)) / 24; + r1 = TRUE; + } + } + + if (!r1) { + if (count > 1) { + LogMessageByIdWithPopupCheckUser(entity, gAllItemsLost); + } + else { + LogMessageByIdWithPopupCheckUser(entity, gItemLost); + } + } + else { + s32 counter; + s32 dirMaybe; + bool8 hallucinating; + + if (r9) { + LogMessageByIdWithPopupCheckUser(entity, gSeveralItemsLost); + } + + dirMaybe = 0; + hallucinating = gDungeon->unk181e8.hallucinating; + sinVal = 0; + for (counter = 0; counter < 24; counter++) { + for (i = 0; i < count; i++) { + if (EntityIsValid(&itemEntities[i])) { + IncreaseEntityPixelPos(&itemEntities[i], itemVelocity[i].x, itemVelocity[i].y); + itemEntities[i].unk1C.raw = sin_4096(sinVal) * 12; + sub_80462AC(&itemEntities[i], hallucinating, 1, dirMaybe, 0); + } + } + DungeonRunFrameActions(0x13); + sinVal += 85; + if ((counter & 3) == 0) { + dirMaybe++; + } + dirMaybe &= 7; + } + + for (i = 0; i < count; i++) { + if (targetTilePos[i].x >= 0) { + sub_80460F8(&targetTilePos[i], &item[i], TRUE); + } + } + } +} + bool8 HasHeldItem(Entity *pokemon, u8 id) { - // Weird assignment to fix a regswap. - EntityInfo *pokemonInfo = pokemonInfo = GetEntInfo(pokemon); - if (!(pokemonInfo->heldItem.flags & ITEM_FLAG_EXISTS)) - { + EntityInfo *pokemonInfo = GetEntInfo(pokemon); + if (!ItemExists(&pokemonInfo->heldItem)) { return FALSE; } - if (pokemonInfo->heldItem.flags & ITEM_FLAG_STICKY) - { + if (pokemonInfo->heldItem.flags & ITEM_FLAG_STICKY) { return FALSE; } - if (pokemonInfo->heldItem.id != id) - { + if (pokemonInfo->heldItem.id != id) { return FALSE; } return TRUE; @@ -50,40 +739,38 @@ bool8 HasHeldItem(Entity *pokemon, u8 id) void sub_8046CE4(Item *item,s32 param_2) { - s32 iVar1; - s32 iVar2; + s32 i; + s32 rand = DungeonRandInt(100); - iVar1 = DungeonRandInt(100); - for(iVar2 = 0; iVar2 < 200; iVar2++) - { - if (gUnknown_810A3F0[iVar1] <= param_2) { - item->quantity = iVar1; - return; + for (i = 0; i < 200; i++) { + if (gUnknown_810A3F0[rand] <= param_2) { + item->quantity = rand; + return; + } + rand /= 2; } - iVar1 = iVar1 / 2; - } - item->quantity = 1; + item->quantity = 1; } void sub_8046D20(void) { - u8 cVar1; + u8 cVar1; - cVar1 = gDungeon->unk8; - gDungeon->unk8 = sub_8046D70(); - if (((gDungeon->unk8 == 1) && (cVar1 == 0)) && - (DisplayDungeonYesNoMessage(0, *gUnknown_80FA408, 1) == 1)) { - gDungeon->unk4 = 1; - gDungeon->unk11 = 2; - } - else { - MusicBoxCreation(); - } + cVar1 = gDungeon->unk8; + gDungeon->unk8 = sub_8046D70(); + if (((gDungeon->unk8 == 1) && (cVar1 == 0)) && + (DisplayDungeonYesNoMessage(0, *gUnknown_80FA408, 1) == 1)) { + gDungeon->unk4 = 1; + gDungeon->unk11 = 2; + } + else { + MusicBoxCreation(); + } } u8 sub_8046D70(void) { - if(gDungeon->unk644.unk46) + if (gDungeon->unk644.unk46) return 0; else return 0; @@ -91,166 +778,146 @@ u8 sub_8046D70(void) void MusicBoxCreation(void) { - bool8 musicBoxOnce; - bool8 createMusicBox; - Item *pbVar3; // r2 - s32 iVar4; - Entity *entity; + bool8 musicBoxOnce; + bool8 createMusicBox; + s32 i; + s32 indexes[3]; - s32 indexes[3]; + createMusicBox = FALSE; + do { + musicBoxOnce = FALSE; + indexes[0] = -1; + indexes[1] = -1; + indexes[2] = -1; - createMusicBox = FALSE; - do { - musicBoxOnce = FALSE; - indexes[0] = -1; - indexes[1] = -1; - indexes[2] = -1; + for (i = 0; i < INVENTORY_SIZE; i++) { + Item *item = &gTeamInventoryRef->teamItems[i]; + if (ItemExists(item)) { + if (item->id == ITEM_ROCK_PART) { + indexes[0] = i; + } + if (item->id == ITEM_ICE_PART) { + indexes[1] = i; + } + if (item->id == ITEM_STEEL_PART) { + indexes[2] = i; + } + } + } - for(iVar4 = 0, pbVar3 = &gTeamInventoryRef->teamItems[iVar4]; iVar4 < 20; pbVar3++, iVar4++) - { - if (pbVar3->flags & ITEM_FLAG_EXISTS) { - if (pbVar3->id == ITEM_ROCK_PART) { - indexes[0] = iVar4; + if ((indexes[0] >= 0) && (indexes[1] >= 0) && (indexes[2] >= 0)) { + + musicBoxOnce = TRUE; + createMusicBox = TRUE; + + // clear out each of the parts + ZeroOutItem(&gTeamInventoryRef->teamItems[indexes[0]]); + ZeroOutItem(&gTeamInventoryRef->teamItems[indexes[1]]); + ZeroOutItem(&gTeamInventoryRef->teamItems[indexes[2]]); + + // init the music box + xxx_init_itemslot_8090A8C(&gTeamInventoryRef->teamItems[indexes[0]], ITEM_MUSIC_BOX, 0); } - if (pbVar3->id == ITEM_ICE_PART) { - indexes[1] = iVar4; - } - if (pbVar3->id == ITEM_STEEL_PART) { - indexes[2] = iVar4; - } - } + } while (musicBoxOnce); + + FillInventoryGaps(); + + if (createMusicBox) { + Entity *leader = GetLeader(); + sub_80855E4(sub_80861A8); + gDungeon->unk1356C = 1; + DisplayDungeonMessage(0,*gUnknown_810531C,1); + sub_803E708(0x3c,0x41); + DisplayDungeonMessage(0,*gUnknown_8105360,1); + sub_80869E4(leader,4,10,0); + sub_80416E0(&leader->pixelPos,0x10c,FALSE); + sub_80421C0(leader,0xd7); + sub_803E708(0x3c,0x41); + SubstitutePlaceholderStringTags(gFormatBuffer_Monsters[0],leader,0); + DisplayDungeonMessage(0,*gUnknown_81053A8,1); + sub_803E708(10,0x41); + PlaySoundEffect(0xd4); + DisplayDungeonMessage(0,*gUnknown_8105434,1); + sub_803E708(10,0x41); + sub_804178C(1); + gDungeon->unk1356C = 0; + ShowWholeRevealedDungeonMap(); } - - if ((indexes[0] >= 0) && (indexes[1] >= 0) && (indexes[2] >= 0)) { - - musicBoxOnce = TRUE; - createMusicBox = TRUE; - - // clear out each of the parts - ZeroOutItem(&gTeamInventoryRef->teamItems[indexes[0]]); - ZeroOutItem(&gTeamInventoryRef->teamItems[indexes[1]]); - ZeroOutItem(&gTeamInventoryRef->teamItems[indexes[2]]); - - // init the music box - xxx_init_itemslot_8090A8C(&gTeamInventoryRef->teamItems[indexes[0]], ITEM_MUSIC_BOX, 0); - } - } while (musicBoxOnce); - - FillInventoryGaps(); - - if (createMusicBox) { - entity = GetLeader(); - sub_80855E4(sub_80861A8); - gDungeon->unk1356C = 1; - DisplayDungeonMessage(0,*gUnknown_810531C,1); - sub_803E708(0x3c,0x41); - DisplayDungeonMessage(0,*gUnknown_8105360,1); - sub_80869E4(entity,4,10,0); - sub_80416E0(&entity->pixelPos,0x10c,FALSE); - sub_80421C0(entity,0xd7); - sub_803E708(0x3c,0x41); - SubstitutePlaceholderStringTags(gFormatBuffer_Monsters[0],entity,0); - DisplayDungeonMessage(0,*gUnknown_81053A8,1); - sub_803E708(10,0x41); - PlaySoundEffect(0xd4); - DisplayDungeonMessage(0,*gUnknown_8105434,1); - sub_803E708(10,0x41); - sub_804178C(1); - gDungeon->unk1356C = 0; - ShowWholeRevealedDungeonMap(); - } } bool8 sub_8046F00(Item *item) { - bool8 canLearnMove; - bool8 cannotMove; - u16 moveID; - Entity *entity; - s32 index; + s32 i; - if (GetItemCategory(item->id) == CATEGORY_TMS_HMS) { - moveID = GetItemMoveID(item->id); - if (!IsHMItem(item->id)) { - for(index = 0; index < MAX_TEAM_MEMBERS; index++) - { - entity = gDungeon->teamPokemon[index]; - if (EntityIsValid(entity)) { - canLearnMove = CanMonLearnMove(moveID, GetEntInfo(entity)->id); - cannotMove = CheckVariousStatuses2(entity, FALSE); - if (cannotMove) { - canLearnMove = FALSE; - } - if (canLearnMove) return TRUE; + if (GetItemCategory(item->id) == CATEGORY_TMS_HMS) { + u16 moveID = GetItemMoveID(item->id); + if (!IsHMItem(item->id)) { + for (i = 0; i < MAX_TEAM_MEMBERS; i++) { + Entity *entity = gDungeon->teamPokemon[i]; + if (EntityIsValid(entity)) { + bool8 canLearnMove = CanMonLearnMove(moveID, GetEntInfo(entity)->id); + bool8 cannotMove = CheckVariousStatuses2(entity, FALSE); + if (cannotMove) { + canLearnMove = FALSE; + } + if (canLearnMove) + return TRUE; + } + } } - } + return FALSE; } - return FALSE; - } - else + return TRUE; } void sub_8046F84(s32 itemFlag) { - Item *item; - Entity *entity; - EntityInfo *entityInfo; - s32 index; + s32 i; - for(index = 0; index < INVENTORY_SIZE; index++) - { - item = &gTeamInventoryRef->teamItems[index]; - if ((item->flags & ITEM_FLAG_EXISTS) && (item->flags & itemFlag)) { - item->id = 0; - item->quantity = 0; - item->flags = 0; + for (i = 0; i < INVENTORY_SIZE; i++) { + Item *item = &gTeamInventoryRef->teamItems[i]; + if ((item->flags & ITEM_FLAG_EXISTS) && (item->flags & itemFlag)) { + ZeroOutItem(item); + } } - } - FillInventoryGaps(); + FillInventoryGaps(); - for(index = 0; index < 4; index++) - { - entity = gDungeon->teamPokemon[index]; - if (EntityIsValid(entity)) { - entityInfo = GetEntInfo(entity); - item = &entityInfo->heldItem; - if ((item->flags & ITEM_FLAG_EXISTS) && (item->flags & itemFlag)) { - item->id = 0; - item->quantity = 0; - item->flags = 0; - } + for (i = 0; i < MAX_TEAM_MEMBERS; i++) { + Entity *entity = gDungeon->teamPokemon[i]; + if (EntityIsValid(entity)) { + EntityInfo *entityInfo = GetEntInfo(entity); + Item *item = &entityInfo->heldItem; + if ((item->flags & ITEM_FLAG_EXISTS) && (item->flags & itemFlag)) { + ZeroOutItem(item); + } + } } - } } void sub_804700C(void) { - Item *item; - Entity *entity; - EntityInfo *entityInfo; - s32 index; + s32 i; - for(index = 0; index < INVENTORY_SIZE; index++) - { - item = &gTeamInventoryRef->teamItems[index]; - if ((item->flags & ITEM_FLAG_EXISTS)) { - xxx_init_itemslot_8090A8C(item, ITEM_PLAIN_SEED, 0); + for (i = 0; i < INVENTORY_SIZE; i++) + { + Item *item = &gTeamInventoryRef->teamItems[i]; + if ((item->flags & ITEM_FLAG_EXISTS)) { + xxx_init_itemslot_8090A8C(item, ITEM_PLAIN_SEED, 0); + } } - } - FillInventoryGaps(); + FillInventoryGaps(); - for(index = 0; index < MAX_TEAM_MEMBERS; index++) - { - entity = gDungeon->teamPokemon[index]; - if (EntityIsValid(entity)) { - entityInfo = GetEntInfo(entity); - item = &entityInfo->heldItem; - if ((item->flags & ITEM_FLAG_EXISTS)) { - xxx_init_itemslot_8090A8C(item, ITEM_PLAIN_SEED, 0); - } + for (i = 0; i < MAX_TEAM_MEMBERS; i++) { + Entity *entity = gDungeon->teamPokemon[i]; + if (EntityIsValid(entity)) { + EntityInfo *entityInfo = GetEntInfo(entity); + Item *item = &entityInfo->heldItem; + if ((item->flags & ITEM_FLAG_EXISTS)) { + xxx_init_itemslot_8090A8C(item, ITEM_PLAIN_SEED, 0); + } + } } - } } bool8 sub_8047084(s32 itemFlag) @@ -281,35 +948,29 @@ bool8 sub_8047084(s32 itemFlag) void sub_8047104(void) { - Item *item; - s32 index; + s32 i; - for(index = 0; index < INVENTORY_SIZE; index++) - { - item = &gTeamInventoryRef->teamItems[index]; - if (item->flags & ITEM_FLAG_EXISTS) - { - if(item->id == ITEM_POKE) - { - AddToTeamMoney(GetMoneyValue(item)); - ZeroOutItem(item); - } - if(item->flags & ITEM_FLAG_IN_SHOP) - { - ZeroOutItem(item); + for (i = 0; i < INVENTORY_SIZE; i++) { + Item *item = &gTeamInventoryRef->teamItems[i]; + if (item->flags & ITEM_FLAG_EXISTS) { + if (item->id == ITEM_POKE) { + AddToTeamMoney(GetMoneyValue(item)); + ZeroOutItem(item); + } + if (item->flags & ITEM_FLAG_IN_SHOP) { + ZeroOutItem(item); + } } } - } - FillInventoryGaps(); + FillInventoryGaps(); } void sub_8047158(void) { - s32 index; + s32 i; - index = 0; - for(index = 0; index < INVENTORY_SIZE; index++) { - Item *item = &gTeamInventoryRef->teamItems[index]; + for (i = 0; i < INVENTORY_SIZE; i++) { + Item *item = &gTeamInventoryRef->teamItems[i]; if (ItemExists(item) && item->flags & ITEM_FLAG_UNPAID) { item->flags &= ~(ITEM_FLAG_UNPAID); }