-
Notifications
You must be signed in to change notification settings - Fork 1
/
Monster.go
282 lines (223 loc) · 6.21 KB
/
Monster.go
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Monster
package main
import (
"encoding/xml"
"fmt"
"github.com/daviddengcn/go-colortext"
"io/ioutil"
"math/rand"
"os"
"strings"
"sync"
"time"
)
type Monster struct {
Agent
Defense int
description string
em EventManager
weapon *Weapon
lootDrops []loot
targets map[string]*target
targets_mutex sync.Mutex
fightingPlayers bool
fightPlyrs_Mutex sync.Mutex
}
type target struct {
aggro int
attackTarget *Character
}
type loot struct {
item Item_I
dropRate int
}
var monsterTemplatesG map[string]*Monster
//------------------MONSTER CONSTRUCTORS------------------------------
func NewMonsterFromXML(monsterData MonsterXML) *Monster {
m := new(Monster)
m.Name = monsterData.Name
m.currentHP = monsterData.HP
m.Defense = monsterData.Defense
m.description = monsterData.Description
m.targets = make(map[string]*target)
m.weapon = monsterData.EquipedWeapon.ToItem().(*Weapon)
for index, itm := range monsterData.Loot.LootItem.Items {
drop := loot{item: (itm).(ItemXML_I).ToItem(), dropRate: monsterData.Loot.DropRates[index]}
m.lootDrops = append(m.lootDrops, drop)
}
return m
}
func NewMonster(name string, roomID int) *Monster {
m := new(Monster)
*m = *monsterTemplatesG[name]
m.targets = make(map[string]*target)
m.RoomIN = roomID
for _, lootItem := range monsterTemplatesG[name].lootDrops {
m.lootDrops = append(m.lootDrops, lootItem)
}
return m
}
//------------------MONSTER COMBAT FUNCTIONS------------------------------
func (m *Monster) fightPlayers() {
for {
time.Sleep(2 * time.Second)
m.fightPlyrs_Mutex.Lock()
if m.currentHP <= 0 || len(m.targets) <= 0 {
m.fightingPlayers = false
m.fightPlyrs_Mutex.Unlock()
break
}
//Find target with highest aggro
var attackTarget *Character
maxAggro := 0
for _, targ := range m.targets {
if targ.attackTarget.IsDead() {
delete(m.targets, targ.attackTarget.GetName())
continue
}
if targ.aggro > maxAggro {
attackTarget = targ.attackTarget
}
}
if attackTarget != nil {
event := NewEvent(m, "attack", attackTarget.Name)
eventManager.AddEvent(event)
}
m.fightPlyrs_Mutex.Unlock()
}
}
func (m *Monster) AddTarget(targetChar Agenter) {
m.targets_mutex.Lock()
defer m.targets_mutex.Unlock()
if _, exist := m.targets[targetChar.GetName()]; exist {
m.targets[targetChar.GetName()].aggro += 5
} else {
targ := target{aggro: 5, attackTarget: targetChar.(*Character)}
m.targets[targetChar.GetName()] = &targ
m.fightPlyrs_Mutex.Lock()
if len(m.targets) == 1 && m.fightingPlayers == false {
m.fightingPlayers = true
go m.fightPlayers()
}
m.fightPlyrs_Mutex.Unlock()
}
}
func (m *Monster) RemoveTarget(name string) {
m.targets_mutex.Lock()
defer m.targets_mutex.Unlock()
if _, found := m.targets[name]; found {
delete(m.targets, name)
}
}
func (m *Monster) Attack(target Agenter) []FormattedString {
m.targets_mutex.Lock()
defer m.targets_mutex.Unlock()
//If the player died or fled then the monster cant attack
if !m.IsAttackingPlayer(target.GetName()) || target.GetRoomID() != m.RoomIN {
return nil
}
output := newFormattedStringCollection()
if m.GetAttackRoll() >= target.GetDefense() {
dmg := m.GetDamage()
output.addMessage(ct.Red, fmt.Sprintf("The %s hit you for %d damage\n", m.Name, dmg))
target.TakeDamage(dmg, 0)
if target.IsDead() {
output.addMessages2(target.Respawn())
delete(m.targets, target.GetName())
}
target.SendMessage(newServerMessageFS(output.fmtedStrings))
return output.fmtedStrings
}
output.addMessage(ct.Red, fmt.Sprintf("The %s's attack missed you.\n", m.Name))
target.SendMessage(newServerMessageFS(output.fmtedStrings))
return output.fmtedStrings
}
func (m *Monster) TakeDamage(amount int, typeOfDamge int) {
m.currentHP -= amount
}
func (m *Monster) Respawn() []FormattedString {
return nil
}
//------------------MONSTER GETTERS------------------------------
func (m *Monster) GetAttackRoll() int {
return (rand.Int() % 20) + m.weapon.attack + m.Strength
}
func (c *Monster) GetRoomID() int {
return c.RoomIN
}
func (m *Monster) GetDefense() int {
return m.Defense
}
func (m *Monster) GetName() string {
return m.Name
}
func (m *Monster) GetCorpse() *Item {
return &Item{name: m.Name + " corpse", description: "A freshly kill " + m.Name + " corpse."}
}
func (m *Monster) GetLootAndCorpse() []Item_I {
return append(m.GetLoot(), m.GetCorpse())
}
func (m *Monster) GetLoot() []Item_I {
lootItems := make([]Item_I, 0)
if len(m.lootDrops) > 0 {
roll := rand.Intn(1000)
for _, itm := range m.lootDrops {
if roll <= itm.dropRate {
lootItems = append(lootItems, itm.item.GetCopy())
}
}
}
return lootItems
}
func (m *Monster) GetDamage() int {
return m.weapon.GetDamage() + m.Strength
}
func (m *Monster) GetDescription() string {
return m.description
}
func (m *Monster) SendMessage(msg interface{}) {
//Do nothing, required for agenter interface
}
func (m *Monster) IsDead() bool {
return m.currentHP < 0
}
func (m *Monster) IsAttackingPlayer(name string) bool {
for _, targets := range m.targets {
if strings.EqualFold(targets.attackTarget.Name, name) {
return true
}
}
return false
}
//------------------Loading Stuff------------------------------
type MonsterXML struct {
Name string `xml:"Name"`
HP int `xml:"HP"`
Defense int `xml:"Defense"`
Description string `xml:"Description"`
EquipedWeapon WeaponXML `xml:"Weapon"`
Loot LootXML `xml:"Loot"`
}
type MonstersXML struct {
XMLName xml.Name `xml:"Monsters"`
Monsters []MonsterXML `xml:"Monster"`
}
type LootXML struct {
XMLName xml.Name `xml:"Loot"`
LootItem InventoryXML `xml:"Inventory"`
DropRates []int `xml:"DropRate"`
}
func LoadMonsterData() {
monsterTemplatesG = make(map[string]*Monster)
xmlFile, err := os.Open("monsterData.xml")
checkErrorWithMessage(err, true, " In Load monster data.")
defer xmlFile.Close()
XMLdata, _ := ioutil.ReadAll(xmlFile)
var monstersData MonstersXML
err = xml.Unmarshal(XMLdata, &monstersData)
checkErrorWithMessage(err, true, " In load Monster Data function.")
for _, element := range monstersData.Monsters {
monsterTemplatesG[element.Name] = NewMonsterFromXML(element)
}
fmt.Println("Monster Data Loaded.")
}