Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Item swap core #2257

Merged
merged 6 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 48 additions & 6 deletions sim/common/wotlk/enchant_effects.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ func init() {
character := agent.GetCharacter()
mh := character.Equip[proto.ItemSlot_ItemSlotMainHand].Enchant.EffectID == 3789
oh := character.Equip[proto.ItemSlot_ItemSlotOffHand].Enchant.EffectID == 3789
if !mh && !oh {
return
}

procMask := core.GetMeleeProcMaskForHands(mh, oh)
ppmm := character.AutoAttacks.NewPPMManager(1.0, procMask)

Expand All @@ -177,7 +175,7 @@ func init() {
procAuraMH := character.NewTemporaryStatsAura("Berserking MH Proc", core.ActionID{SpellID: 59620, Tag: 1}, stats.Stats{stats.AttackPower: 400, stats.RangedAttackPower: 400, stats.Armor: -fivePercentOfArmor}, time.Second*15)
procAuraOH := character.NewTemporaryStatsAura("Berserking OH Proc", core.ActionID{SpellID: 59620, Tag: 2}, stats.Stats{stats.AttackPower: 400, stats.RangedAttackPower: 400, stats.Armor: -fivePercentOfArmor}, time.Second*15)

character.GetOrRegisterAura(core.Aura{
aura := character.GetOrRegisterAura(core.Aura{
Label: "Berserking (Enchant)",
Duration: core.NeverExpires,
OnReset: func(aura *core.Aura, sim *core.Simulation) {
Expand All @@ -197,6 +195,22 @@ func init() {
}
},
})

core.RegisterOnItemSwap(3789, func(sim *core.Simulation) {
mh = character.Equip[proto.ItemSlot_ItemSlotMainHand].Enchant.EffectID == 3789
oh = character.Equip[proto.ItemSlot_ItemSlotOffHand].Enchant.EffectID == 3789

procMask = core.GetMeleeProcMaskForHands(mh, oh)
ppmm = character.AutoAttacks.NewPPMManager(1.0, procMask)
if ppmm.Chance(procMask) == 0 {
aura.Deactivate(sim)
return
Horatio27 marked this conversation as resolved.
Show resolved Hide resolved
}

if !aura.IsActive() {
Horatio27 marked this conversation as resolved.
Show resolved Hide resolved
aura.Activate(sim)
}
})
})

// TODO: These are stand-in values without any real reference.
Expand All @@ -212,7 +226,7 @@ func init() {

healthMetrics := character.NewHealthMetrics(core.ActionID{ItemID: 44494})

character.GetOrRegisterAura(core.Aura{
aura := character.GetOrRegisterAura(core.Aura{
Label: "Lifeward",
Duration: core.NeverExpires,
OnReset: func(aura *core.Aura, sim *core.Simulation) {
Expand All @@ -228,6 +242,23 @@ func init() {
}
},
})

core.RegisterOnItemSwap(3241, func(sim *core.Simulation) {
Horatio27 marked this conversation as resolved.
Show resolved Hide resolved
mh = character.Equip[proto.ItemSlot_ItemSlotMainHand].Enchant.EffectID == 3241
oh = character.Equip[proto.ItemSlot_ItemSlotOffHand].Enchant.EffectID == 3241

procMask = core.GetMeleeProcMaskForHands(mh, oh)
ppmm = character.AutoAttacks.NewPPMManager(3.0, procMask)

if ppmm.Chance(procMask) == 0 {
aura.Deactivate(sim)
return
}

if !aura.IsActive() {
aura.Activate(sim)
}
})
})

core.NewEnchantEffect(3790, func(agent core.Agent) {
Expand All @@ -239,7 +270,7 @@ func init() {
Duration: time.Second * 35,
}

character.GetOrRegisterAura(core.Aura{
aura := character.GetOrRegisterAura(core.Aura{
Label: "Black Magic",
Duration: core.NeverExpires,
OnReset: func(aura *core.Aura, sim *core.Simulation) {
Expand All @@ -256,6 +287,17 @@ func init() {
}
},
})

core.RegisterOnItemSwap(3790, func(sim *core.Simulation) {
mh := character.Equip[proto.ItemSlot_ItemSlotMainHand].Enchant.EffectID == 3790
oh := character.Equip[proto.ItemSlot_ItemSlotOffHand].Enchant.EffectID == 3790

if !mh && !oh {
aura.Deactivate(sim)
} else if !aura.IsActive() {
aura.Activate(sim)
}
})
})

core.AddWeaponEffect(3843, func(agent core.Agent, _ proto.ItemSlot) {
Expand Down
14 changes: 10 additions & 4 deletions sim/core/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,14 +682,20 @@ func (ppmm *PPMManager) Proc(sim *Simulation, procMask ProcMask, label string) b
return false
}

chance := ppmm.Chance(procMask)
return chance > 0 && sim.RandomFloat(label) < chance
}

func (ppmm *PPMManager) Chance(procMask ProcMask) float64 {
if procMask.Matches(ProcMaskMeleeMH) {
return ppmm.mhProcChance > 0 && sim.RandomFloat(label) < ppmm.mhProcChance
return ppmm.mhProcChance
} else if procMask.Matches(ProcMaskMeleeOH) {
return ppmm.ohProcChance > 0 && sim.RandomFloat(label) < ppmm.ohProcChance
return ppmm.ohProcChance
} else if procMask.Matches(ProcMaskRanged) {
return ppmm.rangedProcChance > 0 && sim.RandomFloat(label) < ppmm.rangedProcChance
return ppmm.rangedProcChance
}
return false

return 0
}

func (aa *AutoAttacks) NewPPMManager(ppm float64, procMask ProcMask) PPMManager {
Expand Down
16 changes: 16 additions & 0 deletions sim/core/character.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type Character struct {

// Current gear.
Equip Equipment
//Item Swap Handler
ItemSwap ItemSwap

// Consumables this Character will be using.
Consumes *proto.Consumes
Expand Down Expand Up @@ -241,6 +243,19 @@ func (character *Character) applyItemEffects(agent Agent) {
applyWeaponEffect(agent, proto.ItemSlot(slot))
}
}

if character.ItemSwap.IsEnabled() {
offset := int(proto.ItemSlot_ItemSlotMainHand)
for i, item := range character.ItemSwap.unEquippedItems {
if applyEnchantEffect, ok := enchantEffects[item.Enchant.EffectID]; ok {
applyEnchantEffect(agent)
}

if applyWeaponEffect, ok := weaponEffects[item.Enchant.EffectID]; ok {
applyWeaponEffect(agent, proto.ItemSlot(offset+i))
}
}
}
}

func (character *Character) AddPet(pet PetAgent) {
Expand Down Expand Up @@ -409,6 +424,7 @@ func (character *Character) reset(sim *Simulation, agent Agent) {
character.ExpectedBonusMana = 0
character.majorCooldownManager.reset(sim)
character.Unit.reset(sim, agent)
character.ItemSwap.reset(sim)
character.CurrentTarget = character.defaultTarget

if character.Type == PlayerUnit {
Expand Down
9 changes: 9 additions & 0 deletions sim/core/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ func ProtoToEquipmentSpec(es *proto.EquipmentSpec) EquipmentSpec {
return coreEquip
}

func ProtoToItem(itemSpec *proto.ItemSpec) Item {
Horatio27 marked this conversation as resolved.
Show resolved Hide resolved
spec := ItemSpec{
ID: itemSpec.Id,
}
spec.Gems = itemSpec.Gems
spec.Enchant = itemSpec.Enchant
return NewItem(spec)
}

func NewItem(itemSpec ItemSpec) Item {
item := Item{}
if foundItem, ok := ItemsByID[itemSpec.ID]; ok {
Expand Down
170 changes: 170 additions & 0 deletions sim/core/item_swaps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package core

import (
"time"

"github.com/wowsims/wotlk/sim/core/proto"
)

type OnSwapItem func(*Simulation)

var onSwapCallbacks = map[int32]OnSwapItem{}
Horatio27 marked this conversation as resolved.
Show resolved Hide resolved

const offset = proto.ItemSlot_ItemSlotMainHand

type ItemSwap struct {
character *Character

//Used for resetting
initialEquippedItems [3]Item
initialUnequippedItems [3]Item

//holds items that are currently not equipped
unEquippedItems [3]Item
}

func RegisterOnItemSwap(id int32, callback OnSwapItem) {
onSwapCallbacks[id] = callback
}

func (character *Character) EnableItemSwap(itemSwap *proto.ItemSwap) {
items := getItems(itemSwap)
character.ItemSwap = ItemSwap{
character: character,
initialEquippedItems: getInitialEquippedItems(character),
initialUnequippedItems: items,
unEquippedItems: items,
}
}

func (swap *ItemSwap) IsEnabled() bool {
return swap.character != nil
}

func getInitialEquippedItems(character *Character) [3]Item {
var items [3]Item

for i := range items {
items[i] = character.Equip[i+int(offset)]
}

return items
}

func getItems(itemSwap *proto.ItemSwap) [3]Item {
var items [3]Item

if itemSwap != nil {
items[0] = toItem(itemSwap.MhItem)
items[1] = toItem(itemSwap.OhItem)
items[2] = toItem(itemSwap.RangedItem)
}

return items
}

func toItem(itemSpecProto *proto.ItemSpec) Item {
if itemSpecProto == nil {
return Item{}
}

return ProtoToItem(itemSpecProto)
}

func (swap *ItemSwap) GetItem(slot proto.ItemSlot) Item {
if slot-offset < 0 {
panic("Not able to swap Item " + slot.String() + " not supported")
}
return swap.unEquippedItems[slot-offset]
}

func (swap *ItemSwap) setItem(slot proto.ItemSlot, item Item) {
swap.unEquippedItems[slot-offset] = item
}

func (swap *ItemSwap) SwapItems(sim *Simulation, slots []proto.ItemSlot, useGCD bool) {
if !swap.IsEnabled() {
return
}

character := swap.character

meeleWeaponSwapped := false
for _, slot := range slots {
if swap.swapItem(sim, slot) {
meeleWeaponSwapped = slot == proto.ItemSlot_ItemSlotMainHand || slot == proto.ItemSlot_ItemSlotOffHand
}
}

for _, onSwap := range onSwapCallbacks {
onSwap(sim)
}

if character.AutoAttacks.IsEnabled() && meeleWeaponSwapped {
character.AutoAttacks.StopMeleeUntil(sim, sim.CurrentTime, false)
}

if useGCD {
character.GCD.Set(1500 * time.Millisecond)
}
}

func (swap *ItemSwap) swapItem(sim *Simulation, slot proto.ItemSlot) bool {
character := swap.character
oldItem := character.Equip[slot]
newItem := swap.GetItem(slot)

// No item to swap too
if newItem.ID == 0 {
return false
}

character.Equip[slot] = newItem
stats := newItem.Stats.Add(oldItem.Stats.Multiply(-1))
character.AddStatsDynamic(sim, stats)
Horatio27 marked this conversation as resolved.
Show resolved Hide resolved

swap.setItem(slot, oldItem)
swap.swapWeapon(sim, slot)

return true
}

func (swap *ItemSwap) swapWeapon(sim *Simulation, slot proto.ItemSlot) {
character := swap.character
if !character.AutoAttacks.IsEnabled() {
return
}

switch slot {
case proto.ItemSlot_ItemSlotMainHand:
character.AutoAttacks.MH = character.WeaponFromMainHand(character.AutoAttacks.MH.CritMultiplier)
break
case proto.ItemSlot_ItemSlotOffHand:
character.AutoAttacks.OH = character.WeaponFromOffHand(character.AutoAttacks.OH.CritMultiplier)
break
case proto.ItemSlot_ItemSlotRanged:
character.AutoAttacks.Ranged = character.WeaponFromRanged(character.AutoAttacks.Ranged.CritMultiplier)
break
}

character.AutoAttacks.IsDualWielding = character.Equip[proto.ItemSlot_ItemSlotMainHand].SwingSpeed != 0 && character.Equip[proto.ItemSlot_ItemSlotOffHand].SwingSpeed != 0
Horatio27 marked this conversation as resolved.
Show resolved Hide resolved
}

func (swap *ItemSwap) reset(sim *Simulation) {
if !swap.IsEnabled() {
return
}

character := swap.character
slots := [3]proto.ItemSlot{proto.ItemSlot_ItemSlotMainHand, proto.ItemSlot_ItemSlotOffHand, proto.ItemSlot_ItemSlotRanged}
for i, slot := range slots {
character.Equip[slot] = swap.initialEquippedItems[i]
swap.swapWeapon(sim, slot)
}

swap.unEquippedItems = swap.initialUnequippedItems

for _, onSwap := range onSwapCallbacks {
onSwap(sim)
}
}