Skip to content

Commit

Permalink
Include lava
Browse files Browse the repository at this point in the history
- volcanic areas now include regions of lava (well, they did anyways,
but it's now extended)
- add alpha tool to facilitate transparentifying things
  • Loading branch information
augustus committed Nov 24, 2021
1 parent c86154e commit bfde9fa
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 10 deletions.
101 changes: 101 additions & 0 deletions cmd/alpha/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package main

import (
"bytes"
"fmt"
"image"
"image/color"
_ "image/gif"
_ "image/jpeg"
"image/png"
"io/ioutil"
"os"

"github.com/alecthomas/kong"
)

const desc = `Alpha tool takes some input image & a region (x0,y0)->(x1,y1).
All colours in the given region are made fully transparent across the image.
`

var cli struct {
//
Input string `arg short:"i" type:"existingfile" help:"input image"`

AlphaX0 int `arg long:"alpha-x0" help:"x0 of region designating colours to alpha"`
AlphaY0 int `arg long:"alpha-y0" help:"y0 of region designating colours to alpha"`
AlphaX1 int `arg long:"alpha-x1" help:"x1 of region designating colours to alpha"`
AlphaY1 int `arg long:"alpha-y1" help:"y1 of region designating colours to alpha"`

//
Output string `short:"o" default:"out.png" help:"output name"`
}

func main() {
kong.Parse(
&cli,
kong.Name("alpha"),
kong.Description(desc),
)
fmt.Println("args", cli)

f, err := os.Open(cli.Input)
if err != nil {
panic(err)
}

im, _, err := image.Decode(f)
if err != nil {
panic(err)
}

toalpha := coloursInRegion(im, cli.AlphaX0, cli.AlphaY0, cli.AlphaX1, cli.AlphaY1)
fmt.Printf("colors to remove: %d\n", len(toalpha))
if len(toalpha) < 1 {
panic(fmt.Errorf("region must contain at least one colour"))
}

bnds := im.Bounds()
out := image.NewRGBA(bnds)

for dy := bnds.Min.Y; dy < bnds.Max.Y; dy++ {
for dx := bnds.Min.X; dx < bnds.Max.X; dx++ {
c := im.At(dx, dy)
_, ok := toalpha[c]
if ok {
out.Set(dx, dy, color.RGBA{255, 255, 255, 0})
} else {
out.Set(dx, dy, c)
}
}
}

err = savePng(cli.Output, out)
if err != nil {
panic(err)
}
}

// coloursInRegion returns a map of colours found in the given region
func coloursInRegion(im image.Image, x0, y0, x1, y1 int) map[color.Color]bool {
cs := map[color.Color]bool{}

for dy := y0; dy <= y1; dy++ {
for dx := x0; dx <= x1; dx++ {
cs[im.At(dx, dy)] = true
}
}

return cs
}

// savePng to disk
func savePng(fpath string, in image.Image) error {
buff := new(bytes.Buffer)
err := png.Encode(buff, in)
if err != nil {
return err
}
return ioutil.WriteFile(fpath, buff.Bytes(), 0644)
}
14 changes: 8 additions & 6 deletions pkg/landscape/area.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ type Area struct {
Temperature uint8 // in degress c, offset so 100 => 0 degrees cel
Volcanism uint8

// if the square contains fresh/salt water
Sea bool
River bool
Lake bool // lake implies river
Swamp bool // swamp water
Swampland bool // water ridden land
// if the square contains fresh/salt water/lava
Sea bool
River bool
Lake bool // lake implies river
Swamp bool // swamp water
Lava bool

BiomeSwamp bool

// if river, we set a river ID else 0
RiverID int
Expand Down
4 changes: 4 additions & 0 deletions pkg/landscape/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type volcSettings struct {

// min dist volcanoes must have from each other
OriginMinDist float64

// at or over this value we consider volanic land lava
LavaOver uint8
}

type swampSettings struct {
Expand Down Expand Up @@ -115,6 +118,7 @@ func DefaultConfig() *Config {
Radius: 30,
Variance: 0.7,
OriginMinDist: 10,
LavaOver: 180,
},
Swamp: &swampSettings{
Number: 8,
Expand Down
12 changes: 9 additions & 3 deletions pkg/landscape/geography.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,20 @@ func determineGeothermal(hmap *MapImage, vs *volcSettings) (*MapImage, []*POI) {

for _, volcano := range origins {
pois = append(pois, &POI{X: volcano.X(), Y: volcano.Y(), Type: Volcano})
vmap.SetValue(volcano.X(), volcano.Y(), 255)

lavamap := &MapImage{im: perlin.Perlin(x, y, vs.Variance*1.5)}

// describe square around origin
for dy := volcano.Y() - int(vs.Radius)/2; dy < volcano.Y()+int(vs.Radius)/2; dy++ {
for dx := volcano.X() - int(vs.Radius)/2; dx < volcano.X()+int(vs.Radius)/2; dx++ {
current := vmap.Value(dx, dy)

pv := pmap.Value(dx, dy)
if lavamap.Value(dx, dy) > vs.LavaOver {
pv = 255
hmap.SetValue(dx, dy, decrement(hmap.Value(dx, dy), 5))
}

dist := math.Sqrt(
math.Pow(float64(volcano.X())-float64(dx), 2) + math.Pow(float64(volcano.Y())-float64(dy), 2),
)
Expand All @@ -126,9 +132,9 @@ func determineGeothermal(hmap *MapImage, vs *volcSettings) (*MapImage, []*POI) {
continue
} else if dist > vs.Radius/2 {
pv /= 6
} else if dist > vs.Radius/3 {
} else if dist > vs.Radius/3 && pv != 255 {
pv /= 4
} else if dist > vs.Radius/4 {
} else if dist > vs.Radius/4 && pv != 255 {
pv /= 2
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/landscape/land.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ func (l *Landscape) At(x, y int) *Area {
River: l.rivers.Value(x, y) == 255,
Temperature: l.temperature.Value(x, y),
Swamp: l.swamp.Value(x, y) == 255,
Swampland: l.swamp.Value(x, y) == 120,
BiomeSwamp: l.swamp.Value(x, y) != 0,
Volcanism: l.volcanic.Value(x, y),
Lava: l.volcanic.Value(x, y) == 255,
}
if !a.River {
return a
Expand Down

0 comments on commit bfde9fa

Please sign in to comment.