From 4c0117b6a714462f3921945dce9a9313c03c4030 Mon Sep 17 00:00:00 2001 From: thefish Date: Tue, 1 Aug 2023 21:49:27 +0300 Subject: [PATCH 1/2] fix name calls on coords --- .../precomputed_shade/precomputed_shade.go | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/engine/fov/precomputed_shade/precomputed_shade.go b/engine/fov/precomputed_shade/precomputed_shade.go index 1bf15fe..2da14a3 100644 --- a/engine/fov/precomputed_shade/precomputed_shade.go +++ b/engine/fov/precomputed_shade/precomputed_shade.go @@ -124,7 +124,9 @@ func (ps *precomputedShade) FindByCoords(c types.Coords) (int, *Cell, error) { func (ps *precomputedShade) IsInFov(coords types.Coords) bool { rc := ps.fromLevelCoords(coords) - if rc.X == 0 && rc.Y ==0 {return true} + if rc.X == 0 && rc.Y == 0 { + return true + } _, cell, err := ps.FindByCoords(rc) if err != nil { return false @@ -143,15 +145,15 @@ func (ps *precomputedShade) Init() { func (ps *precomputedShade) PrecomputeFovMap() { max := ps.MaxTorchRadius minusMax := (-1) * max - zeroCoords := types.Coords{0, 0} + zeroCoords := types.Coords{X: 0, Y: 0} var x, y int //fill list for x = minusMax; x < max+1; x++ { for y = minusMax; y < max+1; y++ { if x == 0 && y == 0 { - continue; + continue } - iterCoords := types.Coords{x, y} + iterCoords := types.Coords{X: x, Y: y} distance := zeroCoords.DistanceTo(iterCoords) if distance <= float64(max) { ps.CellList = append(ps.CellList, &Cell{iterCoords, distance, nil, 0}) @@ -180,11 +182,11 @@ func (ps *precomputedShade) PrecomputeFovMap() { roundedX := int(basic.Round(lineX)) roundedY := int(basic.Round(lineY)) - _, cell, err := ps.FindByCoords(types.Coords{roundedX, roundedY}) + _, cell, err := ps.FindByCoords(types.Coords{X: roundedX, Y: roundedY}) if err != nil { //inexistent coord found - break; + break } cell.occludedAngles = unique(append(cell.occludedAngles, i)) } @@ -219,7 +221,7 @@ func (ps *precomputedShade) recalc(level *gamemap.Level, initCoords types.Coords i := 0 prevDistance := 0.0 for !bytes.Equal(currentShade, fullShade) { - if (i == len(ps.CellList)-1) { + if i == len(ps.CellList)-1 { break } cell := ps.CellList[i] @@ -242,7 +244,7 @@ func (ps *precomputedShade) recalc(level *gamemap.Level, initCoords types.Coords if level.GetTile(lc).BlocksSight && ps.LightWalls { //if (nextShade[angle] == 0 && currentShade[angle] == 0) { - if (nextShade[angle] == 0) { + if nextShade[angle] == 0 { level.GetTile(lc).Visible = true level.GetTile(lc).Explored = true } @@ -266,7 +268,7 @@ func (ps *precomputedShade) ComputeFov(level *gamemap.Level, initCoords types.Co //fmt.Printf("\n coords: %v, distance: %f, lit: %d", cell.Coords, cell.distance, cell.lit) cs, err := ps.toLevelCoords(level, initCoords, cell.Coords) if cell.lit > 0 && cell.lit > MIN_LIT_TO_BE_VISIBLE { - //if cell.lit > 0 && cell.lit / (ps.MaxTorchRadius - int(cell.distance - 0.4) - 1) > MIN_LIT_TO_BE_VISIBLE { + //if cell.lit > 0 && cell.lit / (ps.MaxTorchRadius - int(cell.distance - 0.4) - 1) > MIN_LIT_TO_BE_VISIBLE { if err != nil { continue } @@ -298,7 +300,7 @@ func (ps *precomputedShade) ComputeFov(level *gamemap.Level, initCoords types.Co } func (ps *precomputedShade) toLevelCoords(level *gamemap.Level, initCoords, relativeCoords types.Coords) (types.Coords, error) { - realCoords := types.Coords{initCoords.X + relativeCoords.X, initCoords.Y + relativeCoords.Y} + realCoords := types.Coords{X: initCoords.X + relativeCoords.X, Y: initCoords.Y + relativeCoords.Y} if !level.InBounds(realCoords) { return types.Coords{}, errOutOfBounds } @@ -306,7 +308,7 @@ func (ps *precomputedShade) toLevelCoords(level *gamemap.Level, initCoords, rela } func (ps *precomputedShade) fromLevelCoords(lc types.Coords) types.Coords { - relativeCoords := types.Coords{lc.X - ps.originCoords.X, lc.Y - ps.originCoords.Y} + relativeCoords := types.Coords{X: lc.X - ps.originCoords.X, Y: lc.Y - ps.originCoords.Y} return relativeCoords } From 5613744d6ef395a457c1adc5d923edecae391d31 Mon Sep 17 00:00:00 2001 From: thefish Date: Wed, 2 Aug 2023 00:09:54 +0300 Subject: [PATCH 2/2] more cleanup --- engine/ecs/systems/level_render_system.go | 4 ++-- util/delaunay/delaunay.go | 21 ++++++++++----------- util/delaunay/mst.go | 10 +++++----- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/engine/ecs/systems/level_render_system.go b/engine/ecs/systems/level_render_system.go index 3bccb88..c3d258a 100644 --- a/engine/ecs/systems/level_render_system.go +++ b/engine/ecs/systems/level_render_system.go @@ -46,7 +46,7 @@ func NewLevelRenderSystem( return trs } -//fixme add to screens/game Exit() +// fixme add to screens/game Exit() func (trs LevelRenderSystem) Close() { trs.animateTiles.Stop() trs.animateTiles = nil //zero pointer to ticker @@ -93,7 +93,7 @@ func (trs LevelRenderSystem) Process() { //terrain for y := 0; y < trs.Viewport.H; y++ { for x := 0; x < trs.Viewport.W; x++ { - mapCoords := types.Coords{trs.Viewport.CameraCoords.X + x, trs.Viewport.CameraCoords.Y + y} + mapCoords := types.Coords{X: trs.Viewport.CameraCoords.X + x, Y: trs.Viewport.CameraCoords.Y + y} if trs.state.Level.InBounds(mapCoords) { tile := trs.state.Level.GetTile(mapCoords) diff --git a/util/delaunay/delaunay.go b/util/delaunay/delaunay.go index 58ac466..0bb9bd4 100644 --- a/util/delaunay/delaunay.go +++ b/util/delaunay/delaunay.go @@ -6,6 +6,7 @@ import "lab.zaar.be/thefish/alchemyst-go/engine/types" var nodeId = 0 var nodeList = make(map[types.Coords]Node, 0) + // Node defines a struct having as components the node X and Y coordinate position. type Node struct { Id int @@ -129,17 +130,18 @@ func (d *Delaunay) Init(width, height int) *Delaunay { } var supertriangle1, supertriangle2 Triangle + // clear method clears the delaunay triangles slice. func (d *Delaunay) clear() { - p0 := newNode(types.Coords{0,0}) - p1 := newNode(types.Coords{d.width, 0}) - p2 := newNode(types.Coords{d.width, d.height}) - p3 := newNode(types.Coords{0, d.height}) + p0 := newNode(types.Coords{X: 0, Y: 0}) + p1 := newNode(types.Coords{X: d.width, Y: 0}) + p2 := newNode(types.Coords{X: d.width, Y: d.height}) + p3 := newNode(types.Coords{X: 0, Y: d.height}) // Create the supertriangle, an artificial triangle which encompasses all the points. // At the end of the triangulation process any triangles which share Edges with the supertriangle are deleted from the triangle list. - supertriangle1 = t.newTriangle(p0,p1,p2) - supertriangle2 = t.newTriangle(p0,p2,p3) + supertriangle1 = t.newTriangle(p0, p1, p2) + supertriangle2 = t.newTriangle(p0, p2, p3) d.triangles = []Triangle{supertriangle1, supertriangle2} } @@ -199,7 +201,7 @@ func (d *Delaunay) Insert(points []types.Coords) *Delaunay { } for i = 0; i < len(polygon); i++ { edge := polygon[i] - temps = append(temps, t.newTriangle(edge.Nodes[0], edge.Nodes[1], newNode(types.Coords{x, y}))) + temps = append(temps, t.newTriangle(edge.Nodes[0], edge.Nodes[1], newNode(types.Coords{X: x, Y: y}))) } d.triangles = temps } @@ -247,10 +249,7 @@ func (d *Delaunay) GetTriangles() []Triangle { func (d *Delaunay) GetEdges() []Edge { edges := make([]Edge, 0) for _, trs := range d.triangles { - for _, e := range trs.Edges { - edges = append(edges, e) - } - + edges = append(edges, trs.Edges...) } return edges } diff --git a/util/delaunay/mst.go b/util/delaunay/mst.go index aeb3551..72bc8ca 100644 --- a/util/delaunay/mst.go +++ b/util/delaunay/mst.go @@ -11,7 +11,7 @@ func GetTriangles(coords []types.Coords, w, h int) []types.Edge { edges := d.Init(100, 100).Insert(coords).GetEdges() output := make([]types.Edge, 0) for _, e := range edges{ - output = append(output, types.Edge{e.Nodes[0].Coords, e.Nodes[1].Coords}) + output = append(output, types.Edge{From: e.Nodes[0].Coords, To: e.Nodes[1].Coords}) } return output } @@ -46,16 +46,16 @@ func GetMst(coords []types.Coords, w, h, negativeWeight int) []types.Edge { graph = append( graph, kruskals.SimpleWeightedEdge{ - nodeMap[e.Nodes[0].Id], - nodeMap[e.Nodes[1].Id], - negativeWeight - int(e.Nodes[0].Coords.DistanceTo(e.Nodes[1].Coords))}, + F: nodeMap[e.Nodes[0].Id], + T: nodeMap[e.Nodes[1].Id], + W: negativeWeight - int(e.Nodes[0].Coords.DistanceTo(e.Nodes[1].Coords))}, ) } result := kruskals.MinimumSpanningTree(graph) output := make([]types.Edge, 0) for _, we := range result{ - output = append(output, types.Edge{nodeList[we.From()].Coords, nodeList[we.To()].Coords}) + output = append(output, types.Edge{From: nodeList[we.From()].Coords, To: nodeList[we.To()].Coords}) } return output }