2019-12-28 23:32:53 -05:00
|
|
|
package d2player
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-07-11 11:25:34 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
|
2019-12-28 23:32:53 -05:00
|
|
|
"log"
|
2020-01-31 23:18:11 -05:00
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2ui"
|
2020-01-31 23:18:11 -05:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
2019-12-28 23:32:53 -05:00
|
|
|
)
|
|
|
|
|
2020-07-11 11:25:34 -04:00
|
|
|
// images for 1x1 grid tile items (rings and stuff) are 28x28 pixel
|
|
|
|
// however, the grid cells are 29x29 pixels, this is for padding
|
|
|
|
// for each row in inventory, we need to account for this padding
|
|
|
|
const cellPadding = 1
|
|
|
|
|
2019-12-28 23:32:53 -05:00
|
|
|
type InventoryItem interface {
|
|
|
|
InventoryGridSize() (width int, height int)
|
2020-06-13 18:32:09 -04:00
|
|
|
GetItemCode() string
|
2019-12-28 23:32:53 -05:00
|
|
|
InventoryGridSlot() (x int, y int)
|
|
|
|
SetInventoryGridSlot(x int, y int)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrorInventoryFull = errors.New("inventory full")
|
|
|
|
|
|
|
|
// Reusable grid for use with player and merchant inventory.
|
|
|
|
// Handles layout and rendering item icons based on code.
|
|
|
|
type ItemGrid struct {
|
2020-06-23 14:12:30 -04:00
|
|
|
items []InventoryItem
|
2020-07-08 19:08:07 -04:00
|
|
|
equipmentSlots map[d2enum.EquippedSlot]EquipmentSlot
|
2020-06-23 14:12:30 -04:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
originX int
|
|
|
|
originY int
|
|
|
|
sprites map[string]*d2ui.Sprite
|
|
|
|
slotSize int
|
2019-12-28 23:32:53 -05:00
|
|
|
}
|
|
|
|
|
2020-07-11 11:25:34 -04:00
|
|
|
func NewItemGrid(record *d2datadict.InventoryRecord) *ItemGrid {
|
|
|
|
grid := record.Grid
|
2019-12-28 23:32:53 -05:00
|
|
|
return &ItemGrid{
|
2020-07-11 11:25:34 -04:00
|
|
|
width: grid.Box.Width,
|
|
|
|
height: grid.Box.Height,
|
|
|
|
originX: grid.Box.Left,
|
|
|
|
originY: grid.Box.Top + (grid.Rows * cellPadding),
|
|
|
|
slotSize: grid.CellWidth,
|
2020-06-23 14:12:30 -04:00
|
|
|
sprites: make(map[string]*d2ui.Sprite),
|
2020-07-11 11:25:34 -04:00
|
|
|
equipmentSlots: genEquipmentSlotsMap(record),
|
2019-12-28 23:32:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ItemGrid) SlotToScreen(slotX int, slotY int) (screenX int, screenY int) {
|
|
|
|
screenX = g.originX + slotX*g.slotSize
|
|
|
|
screenY = g.originY + slotY*g.slotSize
|
|
|
|
return screenX, screenY
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ItemGrid) ScreenToSlot(screenX int, screenY int) (slotX int, slotY int) {
|
|
|
|
slotX = (screenX - g.originX) / g.slotSize
|
|
|
|
slotY = (screenY - g.originY) / g.slotSize
|
|
|
|
return slotX, slotY
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ItemGrid) GetSlot(x int, y int) InventoryItem {
|
|
|
|
for _, item := range g.items {
|
|
|
|
slotX, slotY := item.InventoryGridSlot()
|
|
|
|
width, height := item.InventoryGridSize()
|
|
|
|
|
|
|
|
if x >= slotX && x < slotX+width && y >= slotY && y < slotY+height {
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-08 19:08:07 -04:00
|
|
|
func (g *ItemGrid) ChangeEquippedSlot(slot d2enum.EquippedSlot, item InventoryItem) {
|
2020-06-23 14:12:30 -04:00
|
|
|
var curItem = g.equipmentSlots[slot]
|
|
|
|
curItem.item = item
|
|
|
|
g.equipmentSlots[slot] = curItem
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:53 -05:00
|
|
|
// Add places a given set of items into the first available slots.
|
|
|
|
// Returns a count of the number of items which could be inserted.
|
|
|
|
func (g *ItemGrid) Add(items ...InventoryItem) (int, error) {
|
|
|
|
added := 0
|
|
|
|
var err error
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
if g.add(item) {
|
|
|
|
added++
|
|
|
|
} else {
|
|
|
|
err = ErrorInventoryFull
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g.Load(items...)
|
|
|
|
|
|
|
|
return added, err
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:12:30 -04:00
|
|
|
func (g *ItemGrid) loadItem(item InventoryItem) {
|
|
|
|
if _, exists := g.sprites[item.GetItemCode()]; !exists {
|
|
|
|
var itemSprite *d2ui.Sprite
|
2019-12-28 23:32:53 -05:00
|
|
|
|
|
|
|
// TODO: Put the pattern into D2Shared
|
2020-02-01 18:55:56 -05:00
|
|
|
animation, err := d2asset.LoadAnimation(
|
2020-06-13 18:32:09 -04:00
|
|
|
fmt.Sprintf("/data/global/items/inv%s.dc6", item.GetItemCode()),
|
2019-12-28 23:32:53 -05:00
|
|
|
d2resource.PaletteSky,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2020-06-13 18:32:09 -04:00
|
|
|
log.Printf("failed to load sprite for item (%s): %v", item.GetItemCode(), err)
|
2020-06-23 14:12:30 -04:00
|
|
|
return
|
2019-12-28 23:32:53 -05:00
|
|
|
}
|
2020-02-01 18:55:56 -05:00
|
|
|
itemSprite, err = d2ui.LoadSprite(animation)
|
2020-06-23 14:12:30 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to load sprite, error: " + err.Error())
|
|
|
|
}
|
2020-06-13 18:32:09 -04:00
|
|
|
g.sprites[item.GetItemCode()] = itemSprite
|
2019-12-28 23:32:53 -05:00
|
|
|
}
|
2020-06-23 14:12:30 -04:00
|
|
|
}
|
2019-12-28 23:32:53 -05:00
|
|
|
|
2020-06-23 14:12:30 -04:00
|
|
|
// Load reads the inventory sprites for items into local cache for rendering.
|
|
|
|
func (g *ItemGrid) Load(items ...InventoryItem) {
|
|
|
|
for _, item := range items {
|
|
|
|
g.loadItem(item)
|
|
|
|
}
|
|
|
|
for _, eq := range g.equipmentSlots {
|
|
|
|
if eq.item != nil {
|
|
|
|
g.loadItem(eq.item)
|
|
|
|
}
|
|
|
|
}
|
2019-12-28 23:32:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Walk from top left to bottom right until a position large enough to hold the item is found.
|
|
|
|
// This is inefficient but simplifies the storage. At most a hundred or so cells will be looped, so impact is minimal.
|
|
|
|
func (g *ItemGrid) add(item InventoryItem) bool {
|
|
|
|
for y := 0; y < g.height; y++ {
|
|
|
|
for x := 0; x < g.width; x++ {
|
|
|
|
if !g.canFit(x, y, item) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
g.set(x, y, item)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// canFit loops over all items to determine if any other items would overlap the given position.
|
|
|
|
func (g *ItemGrid) canFit(x int, y int, item InventoryItem) bool {
|
|
|
|
insertWidth, insertHeight := item.InventoryGridSize()
|
|
|
|
if x+insertWidth > g.width || y+insertHeight > g.height {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, compItem := range g.items {
|
|
|
|
slotX, slotY := compItem.InventoryGridSlot()
|
|
|
|
compWidth, compHeight := compItem.InventoryGridSize()
|
|
|
|
|
|
|
|
if x+insertWidth >= slotX &&
|
|
|
|
x < slotX+compWidth &&
|
|
|
|
y+insertHeight >= slotY &&
|
|
|
|
y < slotY+compHeight {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ItemGrid) Set(x int, y int, item InventoryItem) error {
|
|
|
|
if !g.canFit(x, y, item) {
|
2020-06-13 18:32:09 -04:00
|
|
|
return fmt.Errorf("can not set item (%s) to position (%v, %v)", item.GetItemCode(), x, y)
|
2019-12-28 23:32:53 -05:00
|
|
|
}
|
|
|
|
g.set(x, y, item)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ItemGrid) set(x int, y int, item InventoryItem) {
|
|
|
|
item.SetInventoryGridSlot(x, y)
|
|
|
|
g.items = append(g.items, item)
|
|
|
|
|
|
|
|
g.Load(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove does an in place filter to remove the element from the slice of items.
|
|
|
|
func (g *ItemGrid) Remove(item InventoryItem) {
|
|
|
|
n := 0
|
|
|
|
for _, compItem := range g.items {
|
|
|
|
if compItem == item {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
g.items[n] = compItem
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
|
|
|
|
g.items = g.items[:n]
|
|
|
|
}
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
func (g *ItemGrid) renderItem(item InventoryItem, target d2interface.Surface, x int, y int) {
|
2020-06-23 14:12:30 -04:00
|
|
|
itemSprite := g.sprites[item.GetItemCode()]
|
|
|
|
if itemSprite != nil {
|
|
|
|
itemSprite.SetPosition(x, y)
|
|
|
|
itemSprite.GetCurrentFrameSize()
|
|
|
|
_ = itemSprite.Render(target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
func (g *ItemGrid) Render(target d2interface.Surface) {
|
2020-06-23 14:12:30 -04:00
|
|
|
g.renderInventoryItems(target)
|
|
|
|
g.renderEquippedItems(target)
|
|
|
|
}
|
2019-12-28 23:32:53 -05:00
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
func (g *ItemGrid) renderInventoryItems(target d2interface.Surface) {
|
2020-06-23 14:12:30 -04:00
|
|
|
for _, item := range g.items {
|
2020-06-13 18:32:09 -04:00
|
|
|
itemSprite := g.sprites[item.GetItemCode()]
|
2019-12-28 23:32:53 -05:00
|
|
|
slotX, slotY := g.SlotToScreen(item.InventoryGridSlot())
|
|
|
|
_, h := itemSprite.GetCurrentFrameSize()
|
2020-06-23 14:12:30 -04:00
|
|
|
slotY = slotY + h
|
|
|
|
g.renderItem(item, target, slotX, slotY)
|
|
|
|
}
|
|
|
|
}
|
2019-12-28 23:32:53 -05:00
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
func (g *ItemGrid) renderEquippedItems(target d2interface.Surface) {
|
2020-06-23 14:12:30 -04:00
|
|
|
for _, eq := range g.equipmentSlots {
|
|
|
|
if eq.item != nil {
|
|
|
|
itemSprite := g.sprites[eq.item.GetItemCode()]
|
|
|
|
itemWidth, itemHeight := itemSprite.GetCurrentFrameSize()
|
|
|
|
var x = eq.x + ((eq.width - itemWidth) / 2)
|
|
|
|
var y = eq.y - ((eq.height - itemHeight) / 2)
|
|
|
|
g.renderItem(eq.item, target, x, y)
|
|
|
|
}
|
2019-12-28 23:32:53 -05:00
|
|
|
}
|
|
|
|
}
|