123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- package world
- import "os"
- import "encoding/xml"
- import "github.com/beoran/woe/monolog"
- /* Ekements of the WOE game world.
- * Only Zones, Rooms and their Exits, Items,
- * Mobiles & Characters are saved
- * and loaded from disk. All the rest
- * is kept statically delared in code for simplocoty.
- */
- /* ID used for anything in a world but the world itself and the account. */
- type ID string
- type World struct {
- Name string
- ZoneIDS [] ID
- zones [] * Zone
- CharacterIDS [] ID
- characters [] * Character
- MOTD string
-
- /* Skills, etc that exist in this world */
- genders map[ID] *Gender
- kins map[ID] *Kin
- professions map[ID] *Job
- skills map[ID] *Skill
- arts map[ID] *Art
- techniques map[ID] *Technique
- exploits map[ID] *Exploit
-
- /* Botha array and map are needed for serialization. */
- Genders [] Gender
- Kins [] Kin
- Jobs [] Job
- Skills [] Skill
- Arts [] Art
- Techniques [] Technique
- Exploits [] Exploit
-
- }
- func (me * World) AddKin(toadd * Kin) {
- me.kins[toadd.ID] = toadd
- me.Kins = append(me.Kins, *toadd)
- }
- func (me * World) AddJob(toadd * Job) {
- me.professions[toadd.ID] = toadd
- me.Jobs = append(me.Jobs, *toadd)
- }
- func (me * World) AddSkill(toadd * Skill) {
- me.skills[toadd.ID] = toadd
- me.Skills = append(me.Skills, *toadd)
- }
- func (me * World) AddArt(toadd * Art) {
- me.arts[toadd.ID] = toadd
- me.Arts = append(me.Arts, *toadd)
- }
- func (me * World) AddTechnique(toadd * Technique) {
- me.techniques[toadd.ID] = toadd
- me.Techniques = append(me.Techniques, *toadd)
- }
- func (me * World) AddExploit(toadd * Exploit) {
- me.exploits[toadd.ID] = toadd
- me.Exploits = append(me.Exploits, *toadd)
- }
- func (me * World) AddWoeDefaults() {
- /*
- me.AddSpecies(NewSpecies("sp_human" , "Human"))
- me.AddSpecies(NewSpecies("sp_neosa" , "Neosa"))
- me.AddSpecies(NewSpecies("sp_mantu" , "Mantu"))
- me.AddSpecies(NewSpecies("sp_cyborg" , "Cyborg"))
- me.AddSpecies(NewSpecies("sp_android", "Android"))
- */
- }
- func NewWorld(name string, motd string) (*World) {
- world := new(World)
- world.Name = name
- world.MOTD = motd
- world.kins = make(map[ID] *Kin)
-
- world.AddWoeDefaults()
- return world;
- }
- func HaveID(ids [] ID, id ID) bool {
- for index := 0 ; index < len(ids) ; index++ {
- if ids[index] == id { return true }
- }
- return false
- }
- func (me * World) AddZone(zone * Zone) {
- me.zones = append(me.zones, zone)
- if (!HaveID(me.ZoneIDS, zone.ID)) {
- me.ZoneIDS = append(me.ZoneIDS, zone.ID)
- }
- }
- func (me * World) Save(dirname string) (err error) {
- path := SavePathFor(dirname, "world", me.Name)
-
- file, err := os.Create(path)
- if err != nil {
- monolog.Error("Could not load %name: %v", err)
- return err
- }
- enc := xml.NewEncoder(file)
- enc.Indent(" ", " ")
- res := enc.Encode(me)
- if (res != nil) {
- monolog.Error("Could not save %s: %v", me.Name, err)
- }
- return res
- }
- func (me * World) onLoad() {
- for _, v := range me.Kins {me.kins[v.ID] = &v }
- }
- func LoadWorld(dirname string, name string) (result *World, err error) {
- path := SavePathFor(dirname, "world", name)
-
- file, err := os.Open(path)
- if err != nil {
- return nil, err
- }
- dec := xml.NewDecoder(file)
- result = new(World)
- err = dec.Decode(result)
- if err != nil {
- monolog.Error("Could not load %s: %v", name, err)
- panic(err)
- }
-
- result.onLoad()
- return result, nil
- }
|