Skip to content

Commit

Permalink
Minor code cleanup
Browse files Browse the repository at this point in the history
 On branch feature/add-trinket-swapping
 Changes to be committed:
	modified:   sim/common/shared/shared_utils.go
	modified:   sim/core/database.go
	modified:   sim/core/item_swaps.go
  • Loading branch information
NerdEgghead committed Jan 12, 2025
1 parent e16fb89 commit ea0ba0e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 47 deletions.
2 changes: 1 addition & 1 deletion sim/common/shared/shared_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func NewStackingStatBonusEffect(config StackingStatBonusEffect) {

itemSwapIsEnabled := character.ItemSwap.IsEnabled()
eligibleSlotsForItem := character.ItemSwap.EligibleSlotsForItem(config.ItemID)
itemExistsInMainEquip := !itemSwapIsEnabled || itemSwapIsEnabled && character.ItemSwap.ItemExistsInMainEquip(config.ItemID, eligibleSlotsForItem)
itemExistsInMainEquip := !itemSwapIsEnabled || character.ItemSwap.ItemExistsInMainEquip(config.ItemID, eligibleSlotsForItem)

auraID := core.ActionID{SpellID: config.AuraID}
if auraID.IsEmptyAction() {
Expand Down
37 changes: 10 additions & 27 deletions sim/core/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"fmt"
"math"
"slices"
"sync"

"github.com/wowsims/cata/sim/core/proto"
Expand Down Expand Up @@ -307,38 +308,20 @@ func (equipment *Equipment) EquipItem(item Item) {
}
}

func (equipment *Equipment) containsItem(itemID int32) bool {
for _, item := range equipment {
if item.ID == itemID {
return true
}
}
return false
}

func (equipment *Equipment) containsEnchant(effectID int32) bool {
for _, item := range equipment {
if item.Enchant.EffectID == effectID || item.TempEnchant == effectID {
return true
}
}
return false
func (equipment *Equipment) containsEnchantInSlot(effectID int32, slot proto.ItemSlot) bool {
return (equipment[slot].Enchant.EffectID == effectID) || (equipment[slot].TempEnchant == effectID)
}

func (equipment *Equipment) containsEnchantInSlot(effectID int32, slot proto.ItemSlot) bool {
if equipment[slot].Enchant.EffectID == effectID || equipment[slot].TempEnchant == effectID {
return true
}
return false
func (equipment *Equipment) containsEnchantInSlots(effectID int32, possibleSlots []proto.ItemSlot) bool {
return slices.ContainsFunc(possibleSlots, func(slot proto.ItemSlot) bool {
return equipment.containsEnchantInSlot(effectID, slot)
})
}

func (equipment *Equipment) containsItemInSlots(itemID int32, possibleSlots []proto.ItemSlot) bool {
for _, slot := range possibleSlots {
if equipment[slot].ID == itemID {
return true
}
}
return false
return slices.ContainsFunc(possibleSlots, func(slot proto.ItemSlot) bool {
return equipment[slot].ID == itemID
})
}

func (equipment *Equipment) ToEquipmentSpecProto() *proto.EquipmentSpec {
Expand Down
35 changes: 16 additions & 19 deletions sim/core/item_swaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,15 @@ type ItemSwapProcConfig struct {
func (swap *ItemSwap) registerProcInternal(config ItemSwapProcConfig) {
isItemProc := config.ItemID != 0
isEnchantEffectProc := config.EnchantId != 0
character := swap.character

swap.character.RegisterItemSwapCallback(config.Slots, func(sim *Simulation, slot proto.ItemSlot) {
var isItemSlotMatch = false
character.RegisterItemSwapCallback(config.Slots, func(sim *Simulation, _ proto.ItemSlot) {
isItemSlotMatch := false

if isItemProc {
isItemSlotMatch = swap.character.HasItemEquipped(config.ItemID)
isItemSlotMatch = character.hasItemEquipped(config.ItemID, config.Slots)
} else if isEnchantEffectProc {
isItemSlotMatch = swap.HasEnchantEquipped(config.EnchantId, config.Slots)
isItemSlotMatch = character.hasEnchantEquipped(config.EnchantId, config.Slots)
}

if isItemSlotMatch {
Expand All @@ -174,7 +175,7 @@ func (swap *ItemSwap) RegisterActive(itemID int32, slots []proto.ItemSlot) {
}

swap.character.RegisterItemSwapCallback(slots, func(sim *Simulation, slot proto.ItemSlot) {
hasItemEquipped := swap.character.HasItemEquipped(itemID)
hasItemEquipped := swap.character.hasItemEquipped(itemID, slots)

spell := swap.character.GetSpell(ActionID{ItemID: itemID})
if spell != nil {
Expand Down Expand Up @@ -214,12 +215,12 @@ func (swap *ItemSwap) IsSwapped() bool {
return swap.swapSet == proto.APLActionItemSwap_Swap1
}

func (character *Character) HasItemEquipped(itemID int32) bool {
return character.Equipment.containsItem(itemID)
func (character *Character) hasItemEquipped(itemID int32, possibleSlots []proto.ItemSlot) bool {
return character.Equipment.containsItemInSlots(itemID, possibleSlots)
}

func (swap *ItemSwap) HasEnchantEquipped(effectID int32, possibleSlots []proto.ItemSlot) bool {
return swap.character.Equipment.containsEnchant(effectID)
func (character *Character) hasEnchantEquipped(effectID int32, possibleSlots []proto.ItemSlot) bool {
return character.Equipment.containsEnchantInSlots(effectID, possibleSlots)
}

func (swap *ItemSwap) GetEquippedItemBySlot(slot proto.ItemSlot) *Item {
Expand All @@ -239,31 +240,27 @@ func (swap *ItemSwap) ItemExistsInSwapSet(itemID int32, possibleSlots []proto.It
}

func (swap *ItemSwap) EligibleSlotsForItem(itemID int32) []proto.ItemSlot {
item := GetItemByID(itemID)
eligibleSlots := eligibleSlotsForItem(item, swap.isFuryWarrior)
eligibleSlots := eligibleSlotsForItem(GetItemByID(itemID), swap.isFuryWarrior)
if !swap.IsEnabled() {
return eligibleSlots
} else {
slots := FilterSlice(eligibleSlots, func(slot proto.ItemSlot) bool {
return FilterSlice(eligibleSlots, func(slot proto.ItemSlot) bool {
return (swap.originalEquip[slot].ID == itemID) || (swap.swapEquip[slot].ID == itemID)
})
return slots
}
}

func (swap *ItemSwap) EligibleSlotsForEffect(effectID int32) []proto.ItemSlot {
var eligibleSlots = []proto.ItemSlot{}
var eligibleSlots []proto.ItemSlot

if !swap.IsEnabled() {
return []proto.ItemSlot{}
} else {
for slot := range swap.originalEquip {
itemSlot := proto.ItemSlot(slot)
if swap.IsEnabled() {
for itemSlot := proto.ItemSlot(0); itemSlot < NumItemSlots; itemSlot++ {
if swap.originalEquip.containsEnchantInSlot(effectID, itemSlot) || swap.swapEquip.containsEnchantInSlot(effectID, itemSlot) {
eligibleSlots = append(eligibleSlots, itemSlot)
}
}
}

return eligibleSlots
}

Expand Down

0 comments on commit ea0ba0e

Please sign in to comment.