Skip to content

Commit

Permalink
WIP: towns, crashing
Browse files Browse the repository at this point in the history
  • Loading branch information
silverbucket committed Dec 15, 2024
1 parent f1bf6be commit cbd6878
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 81 deletions.
20 changes: 12 additions & 8 deletions cmd/avatar/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ package avatar
import (
"fmt"
"github.com/charmbracelet/lipgloss"
"pirate-wars/cmd/common"
)

type Coordinates struct {
X int
Y int
}

type Type struct {
pos Coordinates
pos common.Coordinates
char rune
}

func (a *Type) GetX() int {
return a.pos.X
}

func (a *Type) GetMiniMapX() int {
return a.pos.X / common.MiniMapFactor
}

func (a *Type) SetX(x int) {
a.pos.X = x
}
Expand All @@ -27,11 +27,15 @@ func (a *Type) GetY() int {
return a.pos.Y
}

func (a *Type) GetMiniMapY() int {
return a.pos.Y / common.MiniMapFactor
}

func (a *Type) SetY(y int) {
a.pos.Y = y
}

func (a *Type) SetXY(c Coordinates) {
func (a *Type) SetXY(c common.Coordinates) {
a.pos.X = c.X
a.pos.Y = c.Y
}
Expand All @@ -46,6 +50,6 @@ func (a *Type) Render() string {
Render("%c"), a.char)
}

func Create(coordinates Coordinates, c rune) Type {
func Create(coordinates common.Coordinates, c rune) Type {
return Type{pos: coordinates, char: c}
}
30 changes: 30 additions & 0 deletions cmd/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package common

type Coordinates struct {
X int
Y int
}

const (
WorldWidth = 600
WorldHeight = 600
TotalTowns = 30
ViewWidth = 75
ViewHeight = 50
MiniMapFactor = 11
TypeDeepWater = 0
TypeOpenWater = 1
TypeShallowWater = 2
TypeBeach = 3
TypeLowland = 4
TypeHighland = 5
TypeRock = 6
TypePeak = 7
TypeTown = 8
)

type ViewPort struct {
width int
height int
topLeft int
}
131 changes: 68 additions & 63 deletions cmd/terrain/terrain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,17 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"github.com/ojrac/opensimplex-go"
"math"
"math/rand"
"pirate-wars/cmd/avatar"
"pirate-wars/cmd/common"
"pirate-wars/cmd/town"
)

// Icon ideas
// Towns: ⩎
// Boats: ⏅ ⏏ ⏚ ⏛ ⏡ ⪮ ⩯ ⩠ ⩟ ⅏
// People: 옷

const (
WorldWidth = 600
WorldHeight = 600
ViewWidth = 75
ViewHeight = 50
BufferWidth = 21
BufferHeight = 26
MiniMapFactor = 11
TypeDeepWater = 0
TypeOpenWater = 1
TypeShallowWater = 2
TypeBeach = 3
TypeLowland = 4
TypeHighland = 5
TypeRock = 6
TypePeak = 7
)

type ViewPort struct {
width int
height int
topLeft int
}

type Terrain struct {
width int
height int
Expand All @@ -59,14 +36,15 @@ type TypeQualities struct {
}

var TypeLookup = map[Type]TypeQualities{
TypeDeepWater: {symbol: '⏖', style: createTerrainItem("18"), Passable: true, RequiresBoat: true},
TypeOpenWater: {symbol: '⏝', style: createTerrainItem("20"), Passable: true, RequiresBoat: true},
TypeShallowWater: {symbol: '⏑', style: createTerrainItem("26"), Passable: true, RequiresBoat: true},
TypeBeach: {symbol: '~', style: createTerrainItem("#dad1ad"), Passable: true, RequiresBoat: false},
TypeLowland: {symbol: ':', style: createTerrainItem("113"), Passable: true, RequiresBoat: false},
TypeHighland: {symbol: ':', style: createTerrainItem("142"), Passable: true, RequiresBoat: false},
TypeRock: {symbol: '%', style: createTerrainItem("244"), Passable: true, RequiresBoat: false},
TypePeak: {symbol: '^', style: createTerrainItem("15"), Passable: false, RequiresBoat: false},
common.TypeDeepWater: {symbol: '⏖', style: createTerrainItem("18"), Passable: true, RequiresBoat: true},
common.TypeOpenWater: {symbol: '⏝', style: createTerrainItem("20"), Passable: true, RequiresBoat: true},
common.TypeShallowWater: {symbol: '⏑', style: createTerrainItem("26"), Passable: true, RequiresBoat: true},
common.TypeBeach: {symbol: '~', style: createTerrainItem("#dad1ad"), Passable: true, RequiresBoat: false},
common.TypeLowland: {symbol: ':', style: createTerrainItem("113"), Passable: true, RequiresBoat: false},
common.TypeHighland: {symbol: ':', style: createTerrainItem("142"), Passable: true, RequiresBoat: false},
common.TypeRock: {symbol: '%', style: createTerrainItem("244"), Passable: true, RequiresBoat: false},
common.TypePeak: {symbol: '^', style: createTerrainItem("15"), Passable: false, RequiresBoat: false},
common.TypeTown: {symbol: '⩎', style: createTerrainItem("0"), Passable: true, RequiresBoat: false},
}

type World [][]Type
Expand All @@ -78,8 +56,8 @@ func createTerrainItem(color lipgloss.Color) lipgloss.Style {
func Init() *Terrain {
//default values for terrain map generation
t := Terrain{
width: WorldWidth,
height: WorldHeight,
width: common.WorldWidth,
height: common.WorldHeight,
scale: 60,
lacunarity: 2.0,
persistence: 0.5,
Expand All @@ -99,11 +77,29 @@ func (tt Type) IsPassableByBoat() bool {
return false
}

func (t *Terrain) GenerateWorld() World {
func GenerateTowns(world World, count int) {
for i := 0; i <= count; i++ {
for {
coords := common.Coordinates{X: min(rand.Intn(common.WorldWidth-5), 5), Y: min(rand.Intn(common.WorldHeight-5), 5)}
if coords.X > 1 && coords.Y > 1 &&
coords.X < common.WorldWidth-1 && coords.Y < common.WorldHeight &&
world[coords.X][coords.Y] == common.TypeBeach &&
(world[coords.X+1][coords.Y] == common.TypeShallowWater ||
world[coords.X][coords.Y+1] == common.TypeShallowWater ||
world[coords.X-1][coords.Y] == common.TypeShallowWater ||
world[coords.X][coords.Y-1] == common.TypeShallowWater) {
town.Create(coords, '⩎')
break
}
}
}
}

func (t *Terrain) Generate() World {
//var world [WorldWidth][WorldHeight]Type
world := make([][]Type, WorldHeight)
world := make([][]Type, common.WorldHeight)
for i := range world {
world[i] = make([]Type, WorldHeight)
world[i] = make([]Type, common.WorldHeight)
}

noise := opensimplex.New(rand.Int63())
Expand Down Expand Up @@ -131,25 +127,30 @@ func (t *Terrain) GenerateWorld() World {
//normalize to -1 to 1, and then from 0 to 1 (this is for the ability to use grayscale, if using colors could keep from -1 to 1)
var s = (total/normalizeOctaves + 1) / 2
if s > 0.60 {
world[x][y] = TypeDeepWater
world[x][y] = common.TypeDeepWater
} else if s > 0.46 {
world[x][y] = TypeOpenWater
world[x][y] = common.TypeOpenWater
} else if s > 0.42 {
world[x][y] = TypeShallowWater
world[x][y] = common.TypeShallowWater
} else if s > 0.40 {
world[x][y] = TypeBeach
world[x][y] = common.TypeBeach
} else if s > 0.31 {
world[x][y] = TypeLowland
world[x][y] = common.TypeLowland
} else if s > 0.26 {
world[x][y] = TypeHighland
world[x][y] = common.TypeHighland
} else if s > 0.21 {
world[x][y] = TypeRock
world[x][y] = common.TypeRock
} else {
world[x][y] = TypePeak
world[x][y] = common.TypePeak
}
}
}

GenerateTowns(world, common.TotalTowns)
for _, o := range town.List {
world[o.GetX()][o.GetY()] = common.TypeTown
}

return world
}

Expand All @@ -164,8 +165,8 @@ func GetType(i int) (Type, error) {

func (world World) RenderMiniMap() World {
// Calculate new dimensions
height := len(world) / MiniMapFactor
width := len(world[0]) / MiniMapFactor
height := len(world) / common.MiniMapFactor
width := len(world[0]) / common.MiniMapFactor

// Create new 2D slice
newArr := make([][]Type, height+1)
Expand All @@ -177,8 +178,8 @@ func (world World) RenderMiniMap() World {
for i, row := range world {
for j, val := range row {
// Calculate corresponding index in new slice
newI := i / MiniMapFactor
newJ := j / MiniMapFactor
newI := i / common.MiniMapFactor
newJ := j / common.MiniMapFactor

// Assign original value
newArr[newI][newJ] = val
Expand All @@ -196,29 +197,33 @@ func (world World) Paint(avatar avatar.Type, isMiniMap bool) string {
viewHeight := worldHeight
viewWidth := worldWidth
rowWidth := worldWidth
avatarCoordsX := avatar.GetX()
avatarCoordsY := avatar.GetY()

if !isMiniMap {
left = int(math.Max(float64(avatar.GetX()-ViewWidth+BufferWidth), 0))
top = int(math.Max(float64(avatar.GetY()-ViewHeight+BufferHeight), 0))
viewHeight = ViewHeight + top
viewWidth = ViewWidth + left
rowWidth = ViewWidth
avatarX := avatar.GetX()
avatarY := avatar.GetY()

if isMiniMap {
avatarX = avatar.GetMiniMapX()
avatarY = avatar.GetMiniMapY()
for _, o := range town.List {
world[o.GetMiniMapX()][o.GetMiniMapY()] = common.TypeTown
}
} else {
avatarCoordsX = avatarCoordsX / MiniMapFactor
avatarCoordsY = avatarCoordsY / MiniMapFactor
// center viewport on avatar
left = avatar.GetX() - (common.ViewWidth / 2)
top = avatar.GetY() - (common.ViewHeight / 2)
viewHeight = common.ViewHeight + top
viewWidth = common.ViewWidth + left
rowWidth = common.ViewWidth
}

viewport := table.New().BorderBottom(false).BorderTop(false).BorderLeft(false).BorderRight(false)

for y := top; y < worldHeight && y < viewHeight; y++ {
var row = make([]string, rowWidth)
for x := left; x < worldWidth && x < viewWidth; x++ {
//fmt.Println("[%v %v == %v %v]", x, y, avatarCoordsX, avatarCoordsY)
if x == avatarCoordsX && y == avatarCoordsY {
if x == avatarX && y == avatarY {
row[x-left] = avatar.Render()
} else {
//fmt.Printf("[%v , %v , %v ]\n", x, y, x-left)
row[x-left] = world[x][y].Render()
}
}
Expand Down
32 changes: 32 additions & 0 deletions cmd/town/town.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package town

import (
"pirate-wars/cmd/common"
)

type Type struct {
pos common.Coordinates
}

var List []Type

func (t *Type) GetX() int {
return t.pos.X
}
func (t *Type) GetMiniMapX() int {
return t.pos.X / common.MiniMapFactor
}

func (t *Type) GetY() int {
return t.pos.Y
}

func (t *Type) GetMiniMapY() int {
return t.pos.Y / common.MiniMapFactor
}

func Create(coords common.Coordinates, c rune) Type {
t := Type{pos: coords}
List = append(List, t)
return t
}
Loading

0 comments on commit cbd6878

Please sign in to comment.