aboutsummaryrefslogtreecommitdiff
path: root/internal/game/death.go
blob: d2c1b73e3fb3c8642d3be07728564962e7c0a35a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package game

import (
	"sort"

	"thehouseoficarus/internal/player"
)

func (g *Game) dropItemsOnDeath(p *player.Player) {
	roomID := p.RoomID

	if p.Credits > 0 {
		g.World.AddGroundItem(roomID, "credits", p.Credits)
		p.Credits = 0
	}

	var items []deathDrop

	for slot, inv := range p.Inventory {
		if inv == nil || inv.Quantity <= 0 {
			continue
		}
		val := 0
		if def, err := g.ItemStore.Load(inv.ItemID); err == nil {
			val = def.Value * inv.Quantity
		}
		items = append(items, deathDrop{
			itemID:   inv.ItemID,
			quantity: inv.Quantity,
			totalVal: val,
			invSlot:  slot,
		})
	}

	for eqSlot, itemID := range p.Equipment {
		val := 0
		if def, err := g.ItemStore.Load(itemID); err == nil {
			val = def.Value
		}
		items = append(items, deathDrop{
			itemID:    itemID,
			quantity:  1,
			totalVal:  val,
			isEquip:   true,
			equipSlot: eqSlot,
		})
	}

	if len(items) <= 3 {
		return
	}

	sort.Slice(items, func(i, j int) bool {
		return items[i].totalVal > items[j].totalVal
	})

	for i := 3; i < len(items); i++ {
		it := items[i]
		if it.isEquip {
			delete(p.Equipment, it.equipSlot)
		} else {
			p.SetInvSlot(it.invSlot, nil)
		}
		g.World.AddGroundItem(roomID, it.itemID, it.quantity)
	}
}