| 647 | |
| 648 | |
| 649 | cCraftingRecipes::cRecipe * cCraftingRecipes::MatchRecipe(const cItem * a_CraftingGrid, int a_GridWidth, int a_GridHeight, int a_GridStride, const cRecipe * a_Recipe, int a_OffsetX, int a_OffsetY) |
| 650 | { |
| 651 | // Check the regular items first: |
| 652 | bool HasMatched[MAX_GRID_WIDTH][MAX_GRID_HEIGHT]; |
| 653 | memset(HasMatched, 0, sizeof(HasMatched)); |
| 654 | for (cRecipeSlots::const_iterator itrS = a_Recipe->m_Ingredients.begin(); itrS != a_Recipe->m_Ingredients.end(); ++itrS) |
| 655 | { |
| 656 | if ((itrS->x < 0) || (itrS->y < 0)) |
| 657 | { |
| 658 | // "Anywhere" item, process later |
| 659 | continue; |
| 660 | } |
| 661 | ASSERT(itrS->x + a_OffsetX < a_GridWidth); |
| 662 | ASSERT(itrS->y + a_OffsetY < a_GridHeight); |
| 663 | int GridID = (itrS->x + a_OffsetX) + a_GridStride * (itrS->y + a_OffsetY); |
| 664 | if ( |
| 665 | (itrS->x >= a_GridWidth) || |
| 666 | (itrS->y >= a_GridHeight) || |
| 667 | (itrS->m_Item.m_ItemType != a_CraftingGrid[GridID].m_ItemType) || // same item type? |
| 668 | (itrS->m_Item.m_ItemCount > a_CraftingGrid[GridID].m_ItemCount) || // not enough items |
| 669 | ( |
| 670 | (itrS->m_Item.m_ItemDamage > 0) && // should compare damage values? |
| 671 | (itrS->m_Item.m_ItemDamage != a_CraftingGrid[GridID].m_ItemDamage) |
| 672 | ) |
| 673 | ) |
| 674 | { |
| 675 | // Doesn't match |
| 676 | return NULL; |
| 677 | } |
| 678 | HasMatched[itrS->x + a_OffsetX][itrS->y + a_OffsetY] = true; |
| 679 | } // for itrS - Recipe->m_Ingredients[] |
| 680 | |
| 681 | // Process the "Anywhere" items now, and only in the cells that haven't matched yet |
| 682 | // The "anywhere" items are processed on a first-come-first-served basis. |
| 683 | // Do not use a recipe with one horizontal and one vertical "anywhere" ("*:1, 1:*") as it may not match properly! |
| 684 | cRecipeSlots MatchedSlots; // Stores the slots of "anywhere" items that have matched, with the match coords |
| 685 | for (cRecipeSlots::const_iterator itrS = a_Recipe->m_Ingredients.begin(); itrS != a_Recipe->m_Ingredients.end(); ++itrS) |
| 686 | { |
| 687 | if ((itrS->x >= 0) && (itrS->y >= 0)) |
| 688 | { |
| 689 | // Regular item, already processed |
| 690 | continue; |
| 691 | } |
| 692 | int StartX = 0, EndX = a_GridWidth - 1; |
| 693 | int StartY = 0, EndY = a_GridHeight - 1; |
| 694 | if (itrS->x >= 0) |
| 695 | { |
| 696 | StartX = itrS->x; |
| 697 | EndX = itrS->x; |
| 698 | } |
| 699 | else if (itrS->y >= 0) |
| 700 | { |
| 701 | StartY = itrS->y; |
| 702 | EndY = itrS->y; |
| 703 | } |
| 704 | bool Found = false; |
| 705 | for (int x = StartX; x <= EndX; x++) |
| 706 | { |