1package world 2 3import ( 4 "math" 5 6 "tinyadventure/internal/model" 7) 8 9const ( 10 elevationScale = 14.0 11 climateScale = 22.0 12 riverScale = 20.0 13 forestScale = 4.0 14 deepLevel = -0.12 15 shallowLevel = -0.08 16 plainLevel = 0.1 17 hillLevel = 0.2 18 mountainLevel = 0.3 19 heightStep = 0.05 20 climateBand = 0.09 21 seamWidth = 0.03 22 coldPerRise = 0.6 23 riverWidth = 0.014 24 shore = 5.0 25 rim = 2.0 26 sink = 0.4 27 lift = 0.06 28 drift = 0.22 29 wander = 0.12 30) 31 32var forestBias = [...]float64{-0.25, 0.2, -0.1, -0.2, 0.12, -0.05, -0.4, 0.1, 0.25} 33 34type Climate struct{ Temp, Moist float64 } 35 36type Generator struct { 37 w, h int 38 toward Climate 39 elevation, temperature, moisture, river, forest, seam noise 40} 41 42func New(seed uint64, w, h int, toward Climate) Generator { 43 layer := func(i uint64) noise { return noise(mix(seed*8 + i)) } 44 return Generator{w, h, toward, layer(0), layer(1), layer(2), layer(3), layer(4), layer(5)} 45} 46 47func (g Generator) Hex(col, row int) [model.HexBytes]byte { 48 x, y := (float64(col)+0.5*float64(row&1))*math.Sqrt(3)/1.5, float64(row) 49 edge := float64(min(col, row, g.w-1-col, g.h-1-row)) 50 e := g.elevation.fbm(x/elevationScale, y/elevationScale, 4) + lift - sink*math.Pow(max(0, 1-edge/shore), 2) 51 p := smooth(float64(col) / float64(g.w-1)) 52 t := g.toward.Temp*drift*p + wander*g.temperature.fbm(x/climateScale, y/climateScale, 3) - coldPerRise*max(0, e-hillLevel) + seamWidth*g.seam.unit(col, row) 53 m := g.toward.Moist*drift*p + wander*g.moisture.fbm(x/climateScale, y/climateScale, 3) + seamWidth*g.seam.unit(row, col) 54 river := math.Abs(g.river.fbm(x/riverScale, y/riverScale, 3)) 55 biome := byte(3*band(t) + band(m)) 56 var shape byte 57 switch { 58 case e < deepLevel: 59 shape = model.ShapeDeep 60 case e < shallowLevel: 61 shape = model.ShapeShallow 62 case e < plainLevel: 63 shape = model.ShapePlain 64 case e < hillLevel: 65 shape = model.ShapeHill 66 case e < mountainLevel: 67 shape = model.ShapeMountain 68 default: 69 shape = model.ShapePeak 70 } 71 if edge < rim { 72 if shape >= model.ShapePlain { 73 shape, e = model.ShapePeak, max(e, mountainLevel) 74 } else { 75 shape = model.ShapeDeep 76 } 77 } 78 if (shape == model.ShapePlain || shape == model.ShapeHill) && river < riverWidth { 79 shape = model.ShapeShallow 80 } 81 var height, flags byte 82 if shape > model.ShapeShallow { 83 height = byte(1 + (e-shallowLevel)/heightStep) 84 } 85 open := shape == model.ShapePlain || shape == model.ShapeHill 86 if open && g.forest.fbm(x/forestScale, y/forestScale, 2)+forestBias[biome] > 0 { 87 flags |= model.FlagForest 88 } 89 return [model.HexBytes]byte{shape, biome, height, flags} 90} 91 92func (g Generator) Map() []byte { 93 out := make([]byte, 0, g.w*g.h*model.HexBytes) 94 for row := range g.h { 95 for col := range g.w { 96 h := g.Hex(col, row) 97 out = append(out, h[:]...) 98 } 99 } 100 return out 101} 102 103func smooth(v float64) float64 { return v * v * (3 - 2*v) } 104 105func band(v float64) int { 106 switch { 107 case v < -climateBand: 108 return 0 109 case v > climateBand: 110 return 2 111 } 112 return 1 113}