aboutsummaryrefslogtreecommitdiff
path: root/internal/player/player.go
blob: cccf979c091d948483edc38245d0670138590dfe (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package player

import "thirdcollapse/internal/action"
import "thirdcollapse/internal/object"

type SkillName string

const (
	Attack       SkillName = "attack"
	Strength     SkillName = "strength"
	Defense      SkillName = "defense"
	Hitpoints    SkillName = "hitpoints"
	Ranged       SkillName = "ranged"
	Science      SkillName = "science"
	Technology   SkillName = "technology"
	Fishing      SkillName = "fishing"
	Cooking      SkillName = "cooking"
	Woodcutting  SkillName = "woodcutting"
	Firemaking   SkillName = "firemaking"
	Mining       SkillName = "mining"
	Smithing     SkillName = "smithing"
	Crafting     SkillName = "crafting"
	Fletching    SkillName = "fletching"
	Alchemy      SkillName = "alchemy"
	Thieving     SkillName = "thieving"
	Agility      SkillName = "agility"
	Construction SkillName = "construction"
	Scavenging   SkillName = "scavenging"
	Hacking      SkillName = "hacking"
	Assassin     SkillName = "assassin"
	Farming      SkillName = "farming"
)

var AllSkills = []SkillName{
	Attack, Strength, Defense, Hitpoints, Ranged, Science, Technology,
	Fishing, Cooking, Woodcutting, Firemaking, Mining, Smithing, Crafting, Fletching,
	Alchemy, Thieving, Agility, Construction, Scavenging, Hacking, Assassin, Farming,
}

var SkillAbbr = map[SkillName]string{
	Attack:       "atk",
	Strength:     "str",
	Defense:      "def",
	Hitpoints:    "hp",
	Ranged:       "rng",
	Science:      "sci",
	Technology:   "tec",
	Fishing:      "fis",
	Cooking:      "cok",
	Woodcutting:  "wct",
	Firemaking:   "fmk",
	Mining:       "min",
	Smithing:     "smt",
	Crafting:     "cft",
	Fletching:    "flt",
	Alchemy:      "alc",
	Thieving:     "thv",
	Agility:      "agl",
	Construction: "con",
	Scavenging:   "scv",
	Hacking:      "hck",
	Assassin:     "asm",
	Farming:      "frm",
}

type AttackStyle string

const (
	Accurate   AttackStyle = "accurate"
	Aggressive AttackStyle = "aggressive"
	Defensive  AttackStyle = "defensive"
	Balanced   AttackStyle = "balanced"
)

type Skill struct {
	Name SkillName
	XP   int
}

func (s Skill) Level() int {
	return LevelForXP(s.XP)
}

type InventorySlot struct {
	ItemID   string `yaml:"item_id"`
	Quantity int    `yaml:"quantity"`
}

type Player struct {
	Name           string                       `yaml:"name"`
	Skills         map[SkillName]int            `yaml:"skills"`
	Inventory      map[int]*InventorySlot       `yaml:"inventory"`
	Equipment      map[object.EquipSlot]string  `yaml:"equipment"`
	RoomID         int                          `yaml:"room_id"`
	HP             int                          `yaml:"hp"`
	Credits        int                          `yaml:"credits"`
	Description    string                       `yaml:"description"`
	AttackStyle    AttackStyle                  `yaml:"attack_style"`
	Toggles        map[string]bool              `yaml:"toggles"`
	Flags          map[string]any               `yaml:"flags"`
	RegenerateTick int
	Action         *action.Action               `yaml:"-"`
}

func (p *Player) InvSlot(i int) *InventorySlot {
	if p.Inventory == nil {
		return nil
	}
	return p.Inventory[i]
}

func (p *Player) SetInvSlot(i int, slot *InventorySlot) {
	if p.Inventory == nil {
		p.Inventory = make(map[int]*InventorySlot)
	}
	if slot == nil {
		delete(p.Inventory, i)
	} else {
		p.Inventory[i] = slot
	}
}

func (p *Player) FirstFreeSlot() int {
	for i := 0; i < 28; i++ {
		if p.Inventory[i] == nil {
			return i
		}
	}
	return -1
}

func (p *Player) FreeSlots() int {
	used := len(p.Inventory)
	return 28 - used
}

func New(name string) *Player {
	p := &Player{
		Name:        name,
		Skills:      make(map[SkillName]int),
		Equipment:   make(map[object.EquipSlot]string),
		AttackStyle: Accurate,
		Toggles: map[string]bool{
			"description": true,
			"tinymap":     true,
			"xpdrops":     true,
			"exits":       true,
			"mobenter":    true,
			"mobleave":    true,
			"mobspawn":    true,
			"reserve":     true,
		},
		RoomID: 0,
	}
	for _, s := range AllSkills {
		p.Skills[s] = 0
	}
	p.Skills[Hitpoints] = XPForLevel(10)
	p.HP = p.MaxHP()
	return p
}

func (p *Player) Level(s SkillName) int {
	return LevelForXP(p.Skills[s])
}

func (p *Player) AddXP(s SkillName, amount int) {
	if p.Skills == nil {
		p.Skills = make(map[SkillName]int)
	}
	p.Skills[s] += amount
}

func (p *Player) CombatLevel() int {
	base := 0.25 * float64(p.Level(Defense)+p.Level(Hitpoints)+p.Level(Science))
	att := float64(p.Level(Attack))
	str := float64(p.Level(Strength))

	if float64(p.Level(Ranged))*1.5 > att+str {
		base += 0.375 * float64(p.Level(Ranged))
	} else {
		base += 0.25 * (att + str)
	}

	base += 0.125 * float64(p.Level(Technology))
	return int(base)
}

func (p *Player) MaxHP() int {
	return p.Level(Hitpoints)
}

func (p *Player) StartRegen() {
	if p.RegenerateTick == 0 {
		p.RegenerateTick = 100
	}
}

func (p *Player) HasItem(itemID string) bool {
	for _, slot := range p.Inventory {
		if slot != nil && slot.ItemID == itemID && slot.Quantity > 0 {
			return true
		}
	}
	return false
}

func (p *Player) RemoveItem(itemID string, qty int) bool {
	remaining := qty
	for i, slot := range p.Inventory {
		if slot == nil || slot.ItemID != itemID {
			continue
		}
		if slot.Quantity <= remaining {
			remaining -= slot.Quantity
			delete(p.Inventory, i)
		} else {
			slot.Quantity -= remaining
			remaining = 0
		}
		if remaining <= 0 {
			return true
		}
	}
	return remaining <= 0
}