world.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package world
  2. import "github.com/beoran/woe/monolog"
  3. import "github.com/beoran/woe/sitef"
  4. import "errors"
  5. /* Elements of the WOE game world.
  6. * Only Zones, Rooms and their Exits, Items,
  7. * Mobiles & Characters are saved
  8. * and loaded from disk. All the rest
  9. * is kept statically delared in code for simplicity.
  10. */
  11. type World struct {
  12. Name string
  13. MOTD string
  14. dirname string
  15. entitymap map[string] * Entity
  16. zonemap map[string] * Zone
  17. zones [] * Zone
  18. charactermap map[string] * Character
  19. characters [] Character
  20. roommap map[string] * Room
  21. rooms [] Room
  22. itemmap map[string] * Item
  23. items [] Item
  24. mobilemap map[string] * Mobile
  25. mobiles [] Mobile
  26. accounts [] * Account
  27. accountmap map[string] * Account
  28. }
  29. func (me * World) AddWoeDefaults() {
  30. /*
  31. me.AddSpecies(NewSpecies("sp_human" , "Human"))
  32. me.AddSpecies(NewSpecies("sp_neosa" , "Neosa"))
  33. me.AddSpecies(NewSpecies("sp_mantu" , "Mantu"))
  34. me.AddSpecies(NewSpecies("sp_cyborg" , "Cyborg"))
  35. me.AddSpecies(NewSpecies("sp_android", "Android"))
  36. */
  37. }
  38. func NewWorld(name string, motd string, dirname string) (*World) {
  39. world := new(World)
  40. world.Name = name
  41. world.MOTD = motd
  42. world.dirname = dirname
  43. world.accountmap = make(map[string] * Account)
  44. world.itemmap = make(map[string] * Item)
  45. world.roommap = make(map[string] * Room)
  46. world.charactermap = make(map[string] * Character)
  47. world.AddWoeDefaults()
  48. return world;
  49. }
  50. func HaveID(ids [] string, id string) bool {
  51. for index := 0 ; index < len(ids) ; index++ {
  52. if ids[index] == id { return true }
  53. }
  54. return false
  55. }
  56. func (me * World) AddEntity(entity * Entity) {
  57. me.entitymap[entity.ID] = entity;
  58. }
  59. func (me * World) AddZone(zone * Zone) {
  60. me.zones = append(me.zones, zone)
  61. me.zonemap[zone.ID] = zone;
  62. me.AddEntity(&zone.Entity);
  63. }
  64. // Save an account as a sitef file.
  65. func (me * World) Save(dirname string) (err error) {
  66. path := SavePathFor(dirname, "world", me.Name)
  67. rec := sitef.NewRecord()
  68. rec.Put("name", me.Name)
  69. rec.Put("motd", me.MOTD)
  70. monolog.Debug("Saving World record: %s %v", path, rec)
  71. return sitef.SaveRecord(path, *rec)
  72. }
  73. // Load a world from a sitef file.
  74. func LoadWorld(dirname string, name string) (world * World, err error) {
  75. path := SavePathFor(dirname, "world", name)
  76. records, err := sitef.ParseFilename(path)
  77. if err != nil {
  78. return nil, err
  79. }
  80. if len(records) < 1 {
  81. return nil, errors.New("No record found!")
  82. }
  83. record := records[0]
  84. monolog.Info("Loading World record: %s %v", path, record)
  85. world = NewWorld(record.Get("name"), record.Get("motd"), dirname)
  86. monolog.Info("Loaded World: %s %v", path, world)
  87. return world, nil
  88. }
  89. // Returns an acccount that has already been loaded or nil if not found
  90. func (me * World) GetAccount(name string) (account * Account) {
  91. account, ok := me.accountmap[name];
  92. if !ok {
  93. return nil
  94. }
  95. return account
  96. }
  97. // Loads an account to be used with this world. Characters will be linked.
  98. // If the account was already loaded, returns that in stead.
  99. func (me * World) LoadAccount(name string) (account *Account, err error) {
  100. account = me.GetAccount(name)
  101. if (account != nil) {
  102. return account, nil
  103. }
  104. account, err = LoadAccount(me.dirname, name);
  105. if err != nil {
  106. return account, err
  107. }
  108. me.accountmap[account.Name] = account
  109. return account, nil
  110. }
  111. // Removes an account from this world by name.
  112. func (me * World) RemoveAccount(name string) {
  113. _, have := me.accountmap[name]
  114. if (!have) {
  115. return
  116. }
  117. delete(me.accountmap, name)
  118. }
  119. // Default world pointer
  120. var DefaultWorld * World
  121. // Returns an item that has already been loaded or nil if not found
  122. func (me * World) GetItem(id string) (item * Item) {
  123. item, ok := me.itemmap[id]
  124. if !ok {
  125. return nil
  126. }
  127. return item
  128. }
  129. // Loads an item to be used with this world.
  130. // If the item was already loaded, returns that in stead.
  131. func (me * World) LoadItem(id string) (item *Item, err error) {
  132. item = me.GetItem(id)
  133. if (item != nil) {
  134. return item, nil
  135. }
  136. item, err = LoadItem(me.dirname, id);
  137. if err != nil {
  138. return item, err
  139. }
  140. me.itemmap[item.ID] = item
  141. return item, nil
  142. }
  143. // Removes an item from this world by ID.
  144. func (me * World) RemoveItem(id string) {
  145. _, have := me.itemmap[id]
  146. if (!have) {
  147. return
  148. }
  149. delete(me.itemmap, id)
  150. }
  151. // Returns a Room that has already been loaded or nil if not found
  152. func (me * World) GetRoom(id string) (room * Room) {
  153. room, ok := me.roommap[id]
  154. if !ok {
  155. return nil
  156. }
  157. return room
  158. }
  159. // Loads a Room to be used with this world.
  160. // If the room was already loaded, returns that in stead.
  161. func (me * World) LoadRoom(id string) (room *Room, err error) {
  162. room = me.GetRoom(id)
  163. if (room != nil) {
  164. return room, nil
  165. }
  166. room, err = LoadRoom(me.dirname, id);
  167. if err != nil {
  168. return room, err
  169. }
  170. me.roommap[room.ID] = room
  171. return room, nil
  172. }
  173. // Removes an item from this world by ID.
  174. func (me * World) RemoveRoom(id string) {
  175. _, have := me.roommap[id]
  176. if (!have) {
  177. return
  178. }
  179. delete(me.roommap, id)
  180. }