world.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package world
  2. import "os"
  3. import "encoding/xml"
  4. import "github.com/beoran/woe/monolog"
  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. }
  28. func (me * World) AddWoeDefaults() {
  29. /*
  30. me.AddSpecies(NewSpecies("sp_human" , "Human"))
  31. me.AddSpecies(NewSpecies("sp_neosa" , "Neosa"))
  32. me.AddSpecies(NewSpecies("sp_mantu" , "Mantu"))
  33. me.AddSpecies(NewSpecies("sp_cyborg" , "Cyborg"))
  34. me.AddSpecies(NewSpecies("sp_android", "Android"))
  35. */
  36. }
  37. func NewWorld(name string, motd string) (*World) {
  38. world := new(World)
  39. world.Name = name
  40. world.MOTD = motd
  41. world.AddWoeDefaults()
  42. return world;
  43. }
  44. func HaveID(ids [] ID, id ID) bool {
  45. for index := 0 ; index < len(ids) ; index++ {
  46. if ids[index] == id { return true }
  47. }
  48. return false
  49. }
  50. func (me * World) AddEntity(entity * Entity) {
  51. me.entitymap[entity.ID] = entity;
  52. }
  53. func (me * World) AddZone(zone * Zone) {
  54. me.zones = append(me.zones, zone)
  55. me.zonemap[zone.ID] = zone;
  56. me.AddEntity(&zone.Entity);
  57. }
  58. func (me * World) Save(dirname string) (err error) {
  59. path := SavePathFor(dirname, "world", me.Name)
  60. file, err := os.Create(path)
  61. if err != nil {
  62. monolog.Error("Could not load %name: %v", err)
  63. return err
  64. }
  65. enc := xml.NewEncoder(file)
  66. enc.Indent(" ", " ")
  67. res := enc.Encode(me)
  68. if (res != nil) {
  69. monolog.Error("Could not save %s: %v", me.Name, err)
  70. }
  71. return res
  72. }
  73. func (me * World) onLoad() {
  74. }
  75. func LoadWorld(dirname string, name string) (result *World, err error) {
  76. path := SavePathFor(dirname, "world", name)
  77. file, err := os.Open(path)
  78. if err != nil {
  79. return nil, err
  80. }
  81. dec := xml.NewDecoder(file)
  82. result = new(World)
  83. err = dec.Decode(result)
  84. if err != nil {
  85. monolog.Error("Could not load %s: %v", name, err)
  86. panic(err)
  87. }
  88. result.onLoad()
  89. return result, nil
  90. }