-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoom.go
398 lines (330 loc) · 9.57 KB
/
Room.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
package main
import (
"encoding/xml"
"fmt"
"github.com/daviddengcn/go-colortext"
"io/ioutil"
"os"
"strings"
"sync"
"time"
)
// Enumeration for movement/exit directions
const (
NORTH = 0
SOUTH = 1
EAST = 2
WEST = 3
NORTH_EAST = 4
NORTH_WEST = 5
SOUTH_EAST = 6
SOUTH_WEST = 7
UP = 8
DOWN = 9
)
type Room struct {
Name string
ID int
Description string
WorldID string
// This represents each directions exit and has the room number of the connected
// room or -1 if no exit in that direction. This can probaly be combined
// with ExitLinksToRooms.
Exits [10]int
ExitLinksToRooms [10]*Room
//This represents the player characters (PCs) in the room
CharactersInRoom map[string]*Character
//This represents the attackable non-playable characters (NPCs) in the room
MonstersInRoom map[string]*Monster
//May have a third mapping to friendly NPCs like shopkeepers
//NonCharactersInRoom map[string]*NPC
//TODO change this to an Inventory object
ItemsInRoom map[string]Item_I
items_mutex sync.Mutex
//This is for the monsters native to this room
monsterTemplateNames []string
}
//This is a constructor that creates a room from xml data
func NewRoom(roomData RoomXML) *Room {
room := Room{
Name: roomData.Name,
ID: roomData.ID,
Description: roomData.Description,
WorldID: roomData.WorldID,
items_mutex: sync.Mutex{},
}
for i := 0; i < 10; i++ {
room.Exits[i] = -1
}
for _, roomExit := range roomData.Exits {
room.Exits[convertDirectionToInt(roomExit.Direction)] = roomExit.ConnectedRoomID
}
if room.IsLocal() {
room.CharactersInRoom = make(map[string]*Character)
room.MonstersInRoom = make(map[string]*Monster)
room.ItemsInRoom = make(map[string]Item_I)
for _, item := range roomData.Items.Items {
room.AddItem(NewItem(item.(*ItemXML)))
}
room.monsterTemplateNames = roomData.Monsters
room.PopulateMonsters()
go room.repopulateRoomTick(15)
}
return &room
}
//setRoomLink must be called after all rooms are created and is
// responsible for seting the exit pointers to point at the correct rooms
func (room *Room) setRoomLink(roomLink map[int]*Room) {
for i := 0; i < 10; i++ {
if room.Exits[i] != -1 {
room.ExitLinksToRooms[i] = roomLink[room.Exits[i]]
}
}
}
func (room *Room) IsAggroed(charName string) bool {
for _, monster := range room.MonstersInRoom {
if monster.IsAttackingPlayer(charName) {
return true
}
}
return false
}
func (room *Room) IsValidDirection(dir int) bool {
return dir < 10 && dir >= 0 && room.Exits[dir] >= 0
}
func (room *Room) IsLocal() bool {
return room.WorldID == os.Args[1]
}
func (room *Room) GetConnectedRoom(exit int) *Room {
if exit != -1 {
return room.ExitLinksToRooms[exit]
} else {
return nil
}
}
func (room *Room) AddItem(itm Item_I) {
if itm != nil {
room.ItemsInRoom[itm.GetName()] = itm
}
}
func (room *Room) AddPlayer(char *Character) {
if room.IsLocal() {
eventManager.SendMessageToRoom(room.ID, newServerMessageFS(newFormattedStringSplice2(ct.Blue, "\n"+char.Name+" has entered this room.")))
room.CharactersInRoom[strings.ToLower(char.Name)] = char
}
char.RoomIN = room.ID
}
func (room *Room) RemovePlayer(charName string) {
if _, found := room.GetPlayer(charName); found {
eventManager.SendMessageToRoom(room.ID, newServerMessageFS(newFormattedStringSplice2(ct.Blue, "\n"+charName+" has left the room.")))
delete(room.CharactersInRoom, strings.ToLower(charName))
} else {
fmt.Fprint(os.Stderr, "Failed to find: ", charName, " in Room: ", room.Name, ", in RemovePlayer\n")
}
}
func (room *Room) UnAggroPlayer(charName string) {
for _, monster := range room.MonstersInRoom {
monster.RemoveTarget(charName)
}
}
func (room *Room) GetPlayer(charName string) (*Character, bool) {
if room.CharactersInRoom != nil {
char, found := room.CharactersInRoom[strings.ToLower(charName)]
return char, found
} else {
fmt.Fprint(os.Stderr, "Failed to find: ", charName, " in Room: ", room.Name, ", in GetPlayer\n")
return nil, false
}
}
func (room *Room) GetItem(itemName string) (Item_I, bool) {
for name, item := range room.ItemsInRoom {
lcName := strings.ToLower(name)
if strings.Contains(lcName, itemName) {
return item, true
}
}
fmt.Fprint(os.Stderr, "Failed to find: ", itemName, " in Room: ", room.Name, ", in GetItem\n")
return nil, false
}
func (room *Room) GetAndRemoveItem(itemName string) (Item_I, bool) {
room.items_mutex.Lock()
defer room.items_mutex.Unlock()
if item, found := room.GetItem(itemName); found {
delete(room.ItemsInRoom, item.GetName())
return item, found
}
return nil, false
}
func (room *Room) GiveItemToPlayer(char *Character, itemName string) []FormattedString {
if item, found := room.GetAndRemoveItem(itemName); found {
char.AddItem(item)
return newFormattedStringSplice("You succesfully picked up the item and added it to your invenctory\n")
} else {
return newFormattedStringSplice("\nThat item was not found in the room.\n")
}
}
func (room *Room) GetMonster(monsterName string) *Monster {
for name, mosnter := range room.MonstersInRoom {
if strings.Contains(strings.ToLower(name), monsterName) {
return mosnter
}
}
return nil
}
func (room *Room) GetAgent(name string) (Agenter, bool) {
if val := room.GetMonster(name); val != nil {
return val, true
} else if val, found := room.GetPlayer(name); found {
return val, true
} else { // in case, it's already dead, return nil
return nil, false
}
}
func (room *Room) KillOffMonster(monsterName string) {
if monster := room.GetMonster(monsterName); monster != nil {
drops := room.MonstersInRoom[monsterName].GetLootAndCorpse()
for _, drop := range drops {
room.AddItem(drop)
}
delete(room.MonstersInRoom, monsterName)
}
}
func (room *Room) repopulateRoomTick(timeInMinutes time.Duration) {
for {
//TODO should also periodically remove corpses and items in room
//Repopulate the room every x minutes
time.Sleep(time.Minute * timeInMinutes)
room.PopulateMonsters()
}
}
//PopulateMonsters will add monsters to the room based on the preset types
//of monsters allowed in this room. In other words, if the rooms monsters are dead
//or have never been spawned, this will spawn them; if the monsters are all alive
//this will have no effect.
func (room *Room) PopulateMonsters() {
for _, monsterName := range room.monsterTemplateNames {
if _, found := room.MonstersInRoom[monsterName]; found == false {
room.MonstersInRoom[monsterName] = NewMonster(monsterName, room.ID)
}
}
}
func (room *Room) GetDescription() []FormattedString {
var output string
fs := newFormattedStringCollection()
fs.addMessage(ct.Green, room.Name)
fs.addMessage(ct.White, "\n-------------------------------------------------\n")
fs.addMessage2(room.Description)
output = "\nExits: "
for i := 0; i < 10; i++ {
if room.Exits[i] >= 0 {
output += convertIntToDirection(i) + " "
}
}
fs.addMessage(ct.Magenta, output)
output = ""
for _, itemPtr := range room.ItemsInRoom {
output += "\n\t" + itemPtr.GetName()
}
fs.addMessage(ct.Yellow, output)
output = ""
for key, _ := range room.MonstersInRoom {
output += "\n\t" + key
}
fs.addMessage(ct.Red, output)
output = ""
for key, _ := range room.CharactersInRoom {
output += "\n\t" + key
}
fs.addMessage(ct.Blue, output+"\n")
return fs.fmtedStrings
}
//===================="STATIC" FUNCTIONS======================//
func convertDirectionToInt(direction string) int {
switch strings.ToLower(direction) {
case "n", "n\r\n", "n\n", "north":
return NORTH
case "s", "s\r\n", "s\n", "south":
return SOUTH
case "e", "e\r\n", "e\n", "east":
return EAST
case "w", "w\r\n", "w\n", "west":
return WEST
case "nw", "nw\r\n", "nw\n", "northwest":
return NORTH_WEST
case "ne", "ne\r\n", "ne\n", "northeast":
return NORTH_EAST
case "sw", "sw\r\n", "sw\n", "southwest":
return SOUTH_WEST
case "se", "se\r\n", "se\n", "southeast":
return SOUTH_EAST
case "u", "u\r\n", "u\n", "up":
return UP
case "d", "d\r\n", "d\n", "down":
return DOWN
}
return -1
}
func convertIntToDirection(direction int) string {
switch direction {
case NORTH:
return "North"
case SOUTH:
return "South"
case EAST:
return "East"
case WEST:
return "West"
case NORTH_WEST:
return "North-West"
case NORTH_EAST:
return "North-East"
case SOUTH_WEST:
return "South-West"
case SOUTH_EAST:
return "South-East"
case UP:
return "Up"
case DOWN:
return "Down"
}
return ""
}
type ExitXML struct {
XMLName xml.Name `xml:"Exit"`
Direction string `xml:"Direction"`
ConnectedRoomID int `xml:"RoomID"`
}
type RoomXML struct {
XMLName xml.Name `xml:"Room"`
ID int `xml:"ID"`
Name string `xml:"Name"`
Description string `xml:"Description"`
WorldID string `xml:"WorldID"`
Items InventoryXML `xml:"Inventory"`
Monsters []string `xml:"Monster"`
Exits []ExitXML `xml:"Exit"`
}
type RoomsXML struct {
XMLName xml.Name `xml:"Rooms"`
Rooms []RoomXML `xml:"Room"`
respawnRoomID int `xml:"RespawnRoomID"`
}
func LoadRooms() map[int]*Room {
xmlFile, err := os.Open(serverName + ".xml")
checkErrorWithMessage(err, true, " In load rooms function.")
defer xmlFile.Close()
XMLdata, _ := ioutil.ReadAll(xmlFile)
var roomsData RoomsXML
xml.Unmarshal(XMLdata, &roomsData)
worldRespawnRoomID = roomsData.respawnRoomID
rooms := make(map[int]*Room)
for _, roomData := range roomsData.Rooms {
getRoom := NewRoom(roomData)
rooms[getRoom.ID] = getRoom
}
for _, room := range rooms {
room.setRoomLink(rooms)
}
fmt.Println("Rooms loaded.")
return rooms
}