bigbang.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "log"
  4. "math/rand"
  5. )
  6. func BigBang(config YTConfig) ([]YTSector, error) {
  7. var result []YTSector = make([]YTSector, config.Sectors)[0:0]
  8. log.Printf("Config.Sector: %d, Result: len=%d cap=%d", config.Sectors, len(result), cap(result))
  9. var err error
  10. _ = err
  11. // Fill the universe
  12. for sec_num := range make([]byte, config.Sectors) {
  13. result = append(result, YTSector{
  14. Sector: YTSectorType(sec_num + 1),
  15. })
  16. }
  17. // Warps
  18. var num_sectors int // Number of sectors/warps from this sector
  19. var sectors []YTSectorType // Collection of sectors (to be added to the current sector)
  20. var sec YTSectorType // Current sector being added
  21. var already bool // is the sector already there
  22. var added bool // have we added a sector?
  23. for sec_num, _ := range result {
  24. sectors = []YTSectorType{}
  25. num_sectors = rand.Intn(5) + 1
  26. for _ = range make([]byte, num_sectors) {
  27. added = false
  28. for !added {
  29. sec = YTSectorType(rand.Intn(config.Sectors-1) + 1)
  30. already = false
  31. for _, s := range sectors {
  32. if s == sec {
  33. already = true
  34. break
  35. }
  36. }
  37. if !already {
  38. sectors = append(sectors, sec)
  39. //log.Printf("%d > %d", sector.Sector, sec)
  40. added = true
  41. break
  42. }
  43. }
  44. }
  45. // Cross link
  46. for _, s := range sectors {
  47. result[sec_num].Sectors = append(result[sec_num].Sectors, s)
  48. result[s-1].Sectors = append(result[s-1].Sectors, YTSectorType(sec_num))
  49. //log.Printf("%d > %v", sector.Sector, sector.Sectors)
  50. //log.Printf("%d < %v", s, result[s].Sectors)
  51. }
  52. }
  53. return result, nil
  54. }