world.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package world
  2. import "os"
  3. import "encoding/xml"
  4. import "github.com/beoran/woe/monolog"
  5. /* Ekements 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 simplocoty.
  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. ZoneIDS [] ID
  16. zones [] * Zone
  17. CharacterIDS [] ID
  18. characters [] * Character
  19. MOTD string
  20. /* Skills, etc that exist in this world */
  21. genders map[ID] *Gender
  22. kins map[ID] *Kin
  23. professions map[ID] *Job
  24. skills map[ID] *Skill
  25. arts map[ID] *Art
  26. techniques map[ID] *Technique
  27. exploits map[ID] *Exploit
  28. /* Botha array and map are needed for serialization. */
  29. Genders [] Gender
  30. Kins [] Kin
  31. Jobs [] Job
  32. Skills [] Skill
  33. Arts [] Art
  34. Techniques [] Technique
  35. Exploits [] Exploit
  36. }
  37. func (me * World) AddKin(toadd * Kin) {
  38. me.kins[toadd.ID] = toadd
  39. me.Kins = append(me.Kins, *toadd)
  40. }
  41. func (me * World) AddJob(toadd * Job) {
  42. me.professions[toadd.ID] = toadd
  43. me.Jobs = append(me.Jobs, *toadd)
  44. }
  45. func (me * World) AddSkill(toadd * Skill) {
  46. me.skills[toadd.ID] = toadd
  47. me.Skills = append(me.Skills, *toadd)
  48. }
  49. func (me * World) AddArt(toadd * Art) {
  50. me.arts[toadd.ID] = toadd
  51. me.Arts = append(me.Arts, *toadd)
  52. }
  53. func (me * World) AddTechnique(toadd * Technique) {
  54. me.techniques[toadd.ID] = toadd
  55. me.Techniques = append(me.Techniques, *toadd)
  56. }
  57. func (me * World) AddExploit(toadd * Exploit) {
  58. me.exploits[toadd.ID] = toadd
  59. me.Exploits = append(me.Exploits, *toadd)
  60. }
  61. func (me * World) AddWoeDefaults() {
  62. /*
  63. me.AddSpecies(NewSpecies("sp_human" , "Human"))
  64. me.AddSpecies(NewSpecies("sp_neosa" , "Neosa"))
  65. me.AddSpecies(NewSpecies("sp_mantu" , "Mantu"))
  66. me.AddSpecies(NewSpecies("sp_cyborg" , "Cyborg"))
  67. me.AddSpecies(NewSpecies("sp_android", "Android"))
  68. */
  69. }
  70. func NewWorld(name string, motd string) (*World) {
  71. world := new(World)
  72. world.Name = name
  73. world.MOTD = motd
  74. world.kins = make(map[ID] *Kin)
  75. world.AddWoeDefaults()
  76. return world;
  77. }
  78. func HaveID(ids [] ID, id ID) bool {
  79. for index := 0 ; index < len(ids) ; index++ {
  80. if ids[index] == id { return true }
  81. }
  82. return false
  83. }
  84. func (me * World) AddZone(zone * Zone) {
  85. me.zones = append(me.zones, zone)
  86. if (!HaveID(me.ZoneIDS, zone.ID)) {
  87. me.ZoneIDS = append(me.ZoneIDS, zone.ID)
  88. }
  89. }
  90. func (me * World) Save(dirname string) (err error) {
  91. path := SavePathFor(dirname, "world", me.Name)
  92. file, err := os.Create(path)
  93. if err != nil {
  94. monolog.Error("Could not load %name: %v", err)
  95. return err
  96. }
  97. enc := xml.NewEncoder(file)
  98. enc.Indent(" ", " ")
  99. res := enc.Encode(me)
  100. if (res != nil) {
  101. monolog.Error("Could not save %s: %v", me.Name, err)
  102. }
  103. return res
  104. }
  105. func (me * World) onLoad() {
  106. for _, v := range me.Kins {me.kins[v.ID] = &v }
  107. }
  108. func LoadWorld(dirname string, name string) (result *World, err error) {
  109. path := SavePathFor(dirname, "world", name)
  110. file, err := os.Open(path)
  111. if err != nil {
  112. return nil, err
  113. }
  114. dec := xml.NewDecoder(file)
  115. result = new(World)
  116. err = dec.Decode(result)
  117. if err != nil {
  118. monolog.Error("Could not load %s: %v", name, err)
  119. panic(err)
  120. }
  121. result.onLoad()
  122. return result, nil
  123. }