1package gen 2 3import ( 4 "sort" 5 6 "tinyadventure/internal/model" 7 "tinyadventure/internal/world" 8) 9 10const ( 11 mapW = 68 12 mapH = 46 13) 14 15var themeClimate = map[string]world.Climate{ 16 "blood": {Temp: -0.6, Moist: 1}, "reptile": {Temp: 1, Moist: 1}, "outlaw": {Temp: 1, Moist: -1}, "grave": {Temp: -1, Moist: -1}, 17 "occult": {Temp: 1, Moist: 0}, "greenskin": {Temp: 0.6, Moist: -0.6}, "wild": {Temp: 0, Moist: 0.8}, "hex": {Temp: -0.2, Moist: 1}, 18} 19 20func band(v float64) int { 21 return min(2, max(0, int(v+1.5))) 22} 23 24func harshest(c world.Climate) byte { return byte(3*band(c.Temp) + band(c.Moist)) } 25 26func buildMap(seed model.Seed, toward world.Climate) model.Map { 27 return model.Map{Width: mapW, Height: mapH, Hexes: world.New(uint64(seed), mapW, mapH, toward).Map()} 28} 29 30func land(m *model.Map, c [2]int) bool { 31 return m.Walkable(c[0], c[1]) && m.Hex(c[0], c[1])[0] >= model.ShapePlain 32} 33 34func mainland(m *model.Map) map[[2]int]bool { 35 var best map[[2]int]bool 36 seen := map[[2]int]bool{} 37 for y := range m.Height { 38 for x := range m.Width { 39 c := [2]int{x, y} 40 if seen[c] || !land(m, c) { 41 continue 42 } 43 r := world.Reach(m, c) 44 for k := range r { 45 seen[k] = true 46 } 47 if len(r) > len(best) { 48 best = r 49 } 50 } 51 } 52 return best 53} 54 55func hexes(m *model.Map, keep func(c [2]int) bool) [][2]int { 56 var out [][2]int 57 for y := range m.Height { 58 for x := range m.Width { 59 if c := [2]int{x, y}; keep(c) { 60 out = append(out, c) 61 } 62 } 63 } 64 return out 65} 66 67func layRoads(m *model.Map, pts [][2]int) { 68 sort.SliceStable(pts, func(i, j int) bool { return pts[i][0] < pts[j][0] }) 69 mark := func(c [2]int) { 70 h := m.Hex(c[0], c[1]) 71 h[3] = h[3]&^model.FlagForest | model.FlagRoad 72 } 73 for i := range pts { 74 mark(pts[i]) 75 if i == 0 { 76 continue 77 } 78 near := pts[0] 79 for _, p := range pts[:i] { 80 if world.Distance(p[0], p[1], pts[i][0], pts[i][1]) < world.Distance(near[0], near[1], pts[i][0], pts[i][1]) { 81 near = p 82 } 83 } 84 for _, s := range world.Path(m, pts[i], near, nil) { 85 mark(s) 86 } 87 } 88}