world.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /* ID used for anything in a world but the world itself and the account. */
  12. type ID string
  13. type World struct {
  14. Name string
  15. MOTD string
  16. entitymap map[ID] * Entity
  17. zonemap map[ID] * Zone
  18. zones [] * Zone
  19. charactermap map[ID] * Character
  20. characters [] Character
  21. roommap map[ID] * Room
  22. rooms [] Room
  23. itemmap map[ID] * Item
  24. items [] Item
  25. mobilemap map[ID] * Mobile
  26. mobiles [] Mobile
  27. accounts [] * Account
  28. accountmap map[string] * Account
  29. }
  30. func (me * World) AddWoeDefaults() {
  31. /*
  32. me.AddSpecies(NewSpecies("sp_human" , "Human"))
  33. me.AddSpecies(NewSpecies("sp_neosa" , "Neosa"))
  34. me.AddSpecies(NewSpecies("sp_mantu" , "Mantu"))
  35. me.AddSpecies(NewSpecies("sp_cyborg" , "Cyborg"))
  36. me.AddSpecies(NewSpecies("sp_android", "Android"))
  37. */
  38. }
  39. func NewWorld(name string, motd string) (*World) {
  40. world := new(World)
  41. world.Name = name
  42. world.MOTD = motd
  43. world.accountmap = make(map[string] * Account)
  44. world.AddWoeDefaults()
  45. return world;
  46. }
  47. func HaveID(ids [] ID, id ID) bool {
  48. for index := 0 ; index < len(ids) ; index++ {
  49. if ids[index] == id { return true }
  50. }
  51. return false
  52. }
  53. func (me * World) AddEntity(entity * Entity) {
  54. me.entitymap[entity.ID] = entity;
  55. }
  56. func (me * World) AddZone(zone * Zone) {
  57. me.zones = append(me.zones, zone)
  58. me.zonemap[zone.ID] = zone;
  59. me.AddEntity(&zone.Entity);
  60. }
  61. // Save an account as a sitef file.
  62. func (me * World) Save(dirname string) (err error) {
  63. path := SavePathFor(dirname, "world", me.Name)
  64. rec := make(sitef.Record)
  65. rec.Put("name", me.Name)
  66. rec.Put("motd", me.MOTD)
  67. monolog.Debug("Saving World record: %s %v", path, rec)
  68. return sitef.SaveRecord(path, rec)
  69. }
  70. // Load a world from a sitef file.
  71. func LoadWorld(dirname string, name string) (world * World, err error) {
  72. path := SavePathFor(dirname, "world", name)
  73. records, err := sitef.ParseFilename(path)
  74. if err != nil {
  75. return nil, err
  76. }
  77. if len(records) < 1 {
  78. return nil, errors.New("No record found!")
  79. }
  80. record := records[0]
  81. monolog.Info("Loading World record: %s %v", path, record)
  82. world = NewWorld(record.Get("name"), record.Get("motd"))
  83. monolog.Info("Loaded World: %s %v", path, world)
  84. return world, nil
  85. }
  86. // Returns an acccount that has already been loaded or nil if not found
  87. func (me * World) GetAccount(name string) (account * Account) {
  88. account, ok := me.accountmap[name];
  89. if !ok {
  90. return nil
  91. }
  92. return account
  93. }
  94. // Loads an account to be used with this world. Characters will be linked.
  95. // If the account was already loaded, returns that in stead.
  96. func (me * World) LoadAccount(dirname string, name string) (account *Account, err error) {
  97. account = me.GetAccount(name)
  98. if (account != nil) {
  99. return account, nil
  100. }
  101. account, err = LoadAccount(dirname, name);
  102. if err != nil {
  103. return account, err
  104. }
  105. me.accountmap[account.Name] = account
  106. return account, nil
  107. }
  108. // Removes an account from this world by name.
  109. func (me * World) RemoveAccount(name string) {
  110. _, have := me.accountmap[name]
  111. if (!have) {
  112. return
  113. }
  114. delete(me.accountmap, name)
  115. }
  116. // Default world pointer
  117. var DefaultWorld * World