-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCharacter.go
520 lines (418 loc) · 14.6 KB
/
Character.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package main
import (
"encoding/gob"
"encoding/xml"
"fmt"
"github.com/daviddengcn/go-colortext"
"math/rand"
"net"
"strconv"
"sync"
)
type Character struct {
Agent
Race string
Class string
Age int
Alignment int
experience int
gold int
//Equipment fields
PersonalInvetory Inventory
equipedWeapon *Weapon
equippedArmour ArmourSet
myClientConn *ClientConnection
inv_mutex sync.Mutex
}
//=================== CONSTRUCTOR ======================//
func NewCharacter(charData *CharacterXML) *Character {
char := new(Character)
char.SetAgentStats(charData)
char.gold = charData.Gold
char.Race = charData.Race
char.Class = charData.Class
char.experience = charData.Experience
char.equipedWeapon = NewWeaponFromXML(&charData.EquipedWeapon)
char.equippedArmour = *NewArmourSetFromXML(&charData.ArmSet)
char.PersonalInvetory = *NewInventoryFromXML(&charData.PersInv)
char.inv_mutex = sync.Mutex{}
return char
}
//============================== CLASS FUNCTIONS =============================//
// ===== Armour Functions
func (c *Character) EquipArmor(name string) []FormattedString {
if item, found := c.GetItem(name); found {
if item.GetType() == ARMOUR {
return c.equipArmour(item.(*Armour))
}
return newFormattedStringSplice("\nThat item is not armour.\n")
}
return newFormattedStringSplice("\nYou don't have that item. If it is on the ground try 'get'ing it first.\n")
}
func (c *Character) equipArmour(armr *Armour) []FormattedString {
if c.equippedArmour.IsArmourAt(armr.wearLocation) { // already an item present
return newFormattedStringSplice("\nYou already have a peice of armour equiped there.\n")
} else {
c.equippedArmour.EquipArmour(armr)
return newFormattedStringSplice("\nYou equiped " + armr.name + "\n")
}
}
func (c *Character) UnEquipArmour(name string) []FormattedString {
armr := c.equippedArmour.GetAndRemoveArmour(name)
if armr == nil {
return newFormattedStringSplice("\nYou are not wearing a peice of armour with that name. \n")
}
c.AddItem(armr)
return newFormattedStringSplice("\nYou took off the " + armr.name + ".\n")
}
// ===== Weapon Functions
func (c *Character) WieldWeapon(weapon interface{}) []FormattedString {
var weaponToEquip Item_I
switch weapon := weapon.(type) {
default:
fmt.Printf("Unexpected type %T in Character.WieldWeapon", weapon)
case string:
if item, found := c.GetItem(weapon); found {
weaponToEquip = item
}
case *Weapon:
weaponToEquip = weapon
}
if weaponToEquip == nil {
return newFormattedStringSplice("\nYou don't have that item. If it is on the ground try 'get'ing it first.\n")
} else if weaponToEquip.GetType() != WEAPON {
return newFormattedStringSplice("\nThat item is not a weapon.\n")
} else if c.equipedWeapon == nil {
c.equipedWeapon = weaponToEquip.(*Weapon)
return newFormattedStringSplice("\nYou equiped " + c.equipedWeapon.GetName() + ".\n")
} else {
return newFormattedStringSplice("\nYou already have a weapon equiped.\n")
}
}
func (c *Character) UnWieldWeapon() []FormattedString {
if weapon := c.equipedWeapon; weapon != nil {
c.AddItem(weapon)
c.equipedWeapon = nil
return newFormattedStringSplice("\nYou put the " + weapon.name + " back in your bag.\n")
} else {
return newFormattedStringSplice("\nYou are not wielding any weapons at this time\n")
}
}
// ===== Inventory Functions
func (c *Character) AddInventory(otherInv *Inventory) {
c.PersonalInvetory.AddInventory(otherInv)
}
func (c *Character) AddItems(items []Item_I) {
c.PersonalInvetory.AddItems(items)
}
func (c *Character) AddItem(item Item_I) {
if item.GetName() == "Gold" {
c.gold += item.GetWorth()
} else {
c.PersonalInvetory.AddItem(item)
}
}
func (c *Character) GetAndRemoveItems(names []string) (items []Item_I) {
for _, name := range names {
if item, found := c.GetAndRemoveItem(name); found {
items = append(items, item)
}
}
return items
}
func (c *Character) GetAndRemoveItem(name string) (Item_I, bool) {
c.inv_mutex.Lock()
defer c.inv_mutex.Unlock()
return c.PersonalInvetory.GetAndRemoveItem(name)
}
func (c *Character) GetItem(name string) (Item_I, bool) {
return c.PersonalInvetory.GetItem(name)
}
// ===== General Functions
func (char *Character) Move(source *Room, destination *Room) (int, []FormattedString) {
if source != nil && destination != nil {
source.RemovePlayer(char.Name)
destination.AddPlayer(char)
if destination.IsLocal() {
return GAMEPLAY, destination.GetDescription()
} else {
SendCharactersXML(char.ToXML())
return REDIRECT, newFormattedStringSplice(servers[destination.WorldID])
}
} else {
return GAMEPLAY, newFormattedStringSplice("No exit in that direction\n")
}
}
//Attack executes an attack, by the character, against the supplied target. It is
//required for the Agenter interface. Player experience is automatically handled
//here. If the target dies the correct clean up functions are called.
func (char *Character) Attack(target Agenter) []FormattedString {
if target == nil {
return newFormattedStringSplice("\nYour target does not exist, perhaps you typed the name wrong or another player killed it!\n")
}
if !target.IsDead() {
a1 := char.GetAttack()
if a1 >= target.GetDefense() {
dmg := char.GetDamage()
target.TakeDamage(dmg, 0)
target.TakeDamage(char.GetDamage(), 0)
if target.IsDead() {
char.experience += 10 * target.GetLevel()
room := eventManager.GetRoom(char)
room.KillOffMonster(target.GetName())
return newFormattedStringSplice("You hit " + target.GetName() + " for " + strconv.Itoa(dmg) + " damage and it drops over dead.\n")
} else {
target.AddTarget(char)
char.experience += dmg / 2
var output []FormattedString
output = append(output, newFormattedString2(ct.Red, "\nYou were hit by "+char.Name+" for "+strconv.Itoa(dmg)+" damage!\n"))
target.SendMessage(newServerMessageFS(output))
return newFormattedStringSplice("\nYou hit " + target.GetName() + " for " + strconv.Itoa(dmg) + " damage!\n")
}
} else {
char.experience += 2
return newFormattedStringSplice("\nYour attack missed " + target.GetName() + "!\n")
}
}
return newFormattedStringSplice("\nthe " + target.GetName() + " is already dead!\n")
}
func (c *Character) TakeDamage(amount int, typeOfDamge int) {
c.currentHP -= amount
}
func (c *Character) AddTarget(target Agenter) {
//Do nothing, required for Agenter interface
}
func (c *Character) ApplyFleePenalty() []FormattedString {
c.experience -= 100 * c.Level
return newFormattedStringSplice2(ct.Red, fmt.Sprintf("\nYou lost %d experience for fleeing.\n", 100*c.Level))
}
func (c *Character) Respawn() []FormattedString {
output := newFormattedStringCollection()
output.addMessage(ct.Red, "\nYou died!\n")
output.addMessage2("\nYou repspawned.\n")
src := eventManager.GetRoom(c)
dest := eventManager.GetRespawnRoom()
c.Move(src, dest)
c.currentHP = c.MaxHitPoints
return output.fmtedStrings
}
func (c *Character) LevelUp() []FormattedString {
if c.experience-(1000*c.GetLevel()) > 0 {
c.incrementAttack()
c.incrementDefense()
c.incrementLevel()
return newFormattedStringSplice2(ct.Green, "\nYou leveled up!\n")
} else {
return newFormattedStringSplice2(ct.Green, "\nYou don't have enough exp to level up yet.\n")
}
}
// ===== Getter Functions
func (c *Character) GetName() string {
return c.Name
}
func (c *Character) GetDescription() string {
return c.Name //TODO GetDescription
}
func (c *Character) GetRoomID() int {
return c.RoomIN
}
func (c *Character) GetAttack() int {
return (rand.Int() % 20) + c.equipedWeapon.attack + c.Strength
}
func (c *Character) GetDefense() int {
return c.equippedArmour.GetDefense()
}
func (c *Character) GetAlignment() string {
if c.Alignment > 400 {
return "Good"
} else if c.Alignment < -400 {
return "Evil"
} else {
return "Nuetral"
}
}
func (c *Character) GetAttackRoll() string {
min := c.equipedWeapon.attack + c.Strength
max := 20 + c.equipedWeapon.attack + c.Strength
return fmt.Sprintf("%d to %d", min, max)
}
func (c *Character) GetDamageRoll() string {
min := c.equipedWeapon.minDmg + c.Strength
max := c.equipedWeapon.maxDmg + c.Strength
return fmt.Sprintf("%d to %d", min, max)
}
func (c *Character) GetDamage() int {
return c.equipedWeapon.GetDamage() + c.Strength
}
func (c *Character) GetGoldAmount() int {
return c.gold
}
func (c *Character) GetItemsToTrade(inv *Inventory, wg *sync.WaitGroup) {
defer wg.Done()
for {
response := c.myClientConn.GetItemsToTrade()
if response == "timeout" || response == "done" {
break
} else {
if item, found := c.GetAndRemoveItem(response); found {
inv.AddItem(item)
c.SendMessage("\nOne " + item.GetName() + " was added to the trade pool.\n")
} else {
c.SendMessage("\nYou do not have any more of the item: " + response + ".\n")
}
}
}
}
func (c *Character) GetTradeResponse() string {
return c.myClientConn.GetResponseToTrade()
}
func (c *Character) GetEquipmentPage() []FormattedString {
output := newFormattedStringCollection()
output.addMessage(ct.Green, "\t\t\tEquipment Page for "+c.Name+"\n--------------------------------------------------------------------\n")
if c.equipedWeapon != nil {
output.addMessages2(c.equipedWeapon.GetWeaponPage())
} else {
output.addMessage(ct.Green, "\t\t\tEquipped Weapon\n")
output.addMessage2(fmt.Sprintf("\t%-15s %-15s %-15s %-15s\n", "Name", "Attack", "MinDmg", "MaxDmg"))
output.addMessage(ct.Green, "--------------------------------------------------------------------\n\n")
}
output.addMessages2(c.equippedArmour.GetArmourWornPage())
return output.fmtedStrings
}
func (c *Character) GetStatsPage() []FormattedString {
output := newFormattedStringCollection()
output.addMessage(ct.Green, "Character Page for "+c.Name+"\n-------------------------------------------------\n")
output.addMessage(ct.Green, "LEVEL:")
output.addMessage(ct.White, fmt.Sprintf("%2d %8s", c.Level, ""))
output.addMessage(ct.Green, "RACE :")
output.addMessage(ct.White, fmt.Sprintf("%8s\n", c.Race))
output.addMessage(ct.Green, "AGE :")
output.addMessage(ct.White, fmt.Sprintf("%4d %6s", c.Age, ""))
output.addMessage(ct.Green, "CLASS:")
output.addMessage(ct.White, fmt.Sprintf("%8s\n", c.Class))
output.addMessage(ct.Green, "STR :")
output.addMessage(ct.White, fmt.Sprintf("%2d %8s", c.Strength, ""))
output.addMessage(ct.Green, "HitRoll:")
output.addMessage(ct.White, fmt.Sprintf("%8s\n", c.GetAttackRoll()))
output.addMessage(ct.Green, "INT :")
output.addMessage(ct.White, fmt.Sprintf("%2d %8s", c.Inteligence, ""))
output.addMessage(ct.Green, "DmgRoll:")
output.addMessage(ct.White, fmt.Sprintf("%8s\n", c.GetDamageRoll()))
output.addMessage(ct.Green, "WIS :")
output.addMessage(ct.White, fmt.Sprintf("%2d %8s", c.Wisdom, ""))
output.addMessage(ct.Green, "Alignment:")
output.addMessage(ct.White, fmt.Sprintf("%8s\n", c.GetAlignment()))
output.addMessage(ct.Green, "DEX :")
output.addMessage(ct.White, fmt.Sprintf("%2d %8s", c.Dexterity, ""))
output.addMessage(ct.Green, "Armour:")
output.addMessage(ct.White, fmt.Sprintf("%8d\n", c.GetDefense()))
output.addMessage(ct.Green, "CON :")
output.addMessage(ct.White, fmt.Sprintf("%2d %8s", c.Constitution, ""))
output.addMessage(ct.Green, "Gold:")
output.addMessage(ct.White, fmt.Sprintf("%8d\n", c.GetGoldAmount()))
output.addMessage(ct.Green, "CHA :")
output.addMessage(ct.White, fmt.Sprintf("%2d %8s", c.Charisma, ""))
return output.fmtedStrings
}
// ===== Predicate Functions
func (c *Character) HasItems(itemNames []string) bool {
for _, name := range itemNames {
if !c.HasItem(name) {
return false
}
}
return true
}
func (c *Character) HasItem(name string) bool {
return c.PersonalInvetory.PossesItem(name)
}
func (c *Character) IsDead() bool {
return c.currentHP <= 0 || c.myClientConn.IsConnectionClosed()
}
// ===== Misc Functions
func (char *Character) ToXML() *CharacterXML {
ch := new(CharacterXML)
ch.Name = char.Name
ch.RoomIN = char.RoomIN
ch.HP = char.MaxHitPoints
ch.Gold = char.gold
ch.Class = char.Class
ch.Race = char.Race
ch.Strength = char.Strength
ch.Constitution = char.Constitution
ch.Dexterity = char.Dexterity
ch.Wisdom = char.Wisdom
ch.Charisma = char.Charisma
ch.Inteligence = char.Inteligence
ch.Level = char.Level
ch.Experience = char.experience
ch.WeaponComment = []byte("Equipped Weapon")
ch.EquipedWeapon = *char.equipedWeapon.ToXML().(*WeaponXML)
ch.ArmSet = *char.equippedArmour.ToXML()
ch.PersInv = *char.PersonalInvetory.toXML()
ch.CurrentWorld = eventManager.GetPlayersWorld(char)
return ch
}
//SendMessage is used to send a communication to a character. This communication
//is then forwarded to the client for this character. The only valid message types
//are: string, ServerMessage, and []FormattedString.
func (char *Character) SendMessage(msg interface{}) {
switch msg := msg.(type) {
default:
fmt.Printf("Unexpected type %T in Character.SendMessage\n", msg)
case string:
char.myClientConn.Write(newServerMessageS(msg))
case ServerMessage:
char.myClientConn.Write(msg)
case []FormattedString:
char.myClientConn.Write(newServerMessageFS(msg))
}
}
//==============="STATIC" FUNCTIONS===================//
//TODO add any missing fields
type CharacterXML struct {
XMLName xml.Name `xml:"Character"`
Name string `xml:"Name"`
RoomIN int `xml:"RoomIN"`
HP int `xml:"HitPoints"`
Defense int `xml:"Defense"`
Race string `xml:"Race"`
Class string `xml:"Class"`
Strength int `xml:"Strength"`
Constitution int `xml:"Constitution"`
Dexterity int `xml:"Dexterity"`
Wisdom int `xml:"Wisdom"`
Charisma int `xml:"Charisma"`
Inteligence int `xml:"Inteligence"`
Level int `xml:"Level"`
Experience int `xml:"Experience"`
Gold int `xml:"Gold"`
CurrentWorld string `xml:"CurrentWorld"`
WeaponComment xml.Comment `xml:",comment"`
EquipedWeapon WeaponXML `xml:"Weapon"`
ArmSet ArmourSetXML `xml:"ArmourSet"`
PersInv InventoryXML `xml:"Inventory"`
}
//GetCharacterFromStorage querys the characterStorage server for the characters
//data and stores it into a new character object.
func GetCharacterFromStorage(charName string) (char *Character, err error) {
conn, err := net.Dial("tcp", servers["characterStorage"])
if err != nil {
return char, err
}
defer conn.Close()
enc := gob.NewEncoder(conn)
dec := gob.NewDecoder(conn)
var queriedChar CharacterXML
//Request file from server
if err = enc.Encode(newServerMessageTypeS(GETFILE, charName)); err != nil {
return char, err
}
//Decode the character from message
if err = dec.Decode(&queriedChar); err != nil {
return char, err
}
//Decode the characters xml into a character object
char = NewCharacter(&queriedChar)
return char, err
}