Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of a view layer #844

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -2286,6 +2286,11 @@ func (p *Player) Tick(w *world.World, current int64) {
}
}

// ViewLayer ...
func (p *Player) ViewLayer() *world.ViewLayer {
return p.session().ViewLayer()
}

// tickAirSupply tick's the player's air supply, consuming it when underwater, and replenishing it when out of water.
func (p *Player) tickAirSupply(w *world.World) {
if !p.canBreathe(w) {
Expand Down
4 changes: 4 additions & 0 deletions server/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ type Session struct {

joinMessage, quitMessage string

viewLayer *world.ViewLayer

closeBackground chan struct{}
}

Expand Down Expand Up @@ -164,6 +166,7 @@ func New(conn Conn, maxChunkRadius int, log Logger, joinMessage, quitMessage str
heldSlot: atomic.NewUint32(0),
joinMessage: joinMessage,
quitMessage: quitMessage,
viewLayer: world.NewViewLayer(),
openedWindow: *atomic.NewValue(inventory.New(1, nil)),
}

Expand Down Expand Up @@ -232,6 +235,7 @@ func (s *Session) Close() error {
// close closes the session, which in turn closes the controllable and the connection that the session
// manages.
func (s *Session) close() {
_ = s.viewLayer.Close()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should one viewer leaving close the entire view layer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because they left?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if multiple sessions has the same view layer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 session = 1 view layer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then why do view layers store multiple sessions at the moment?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

every player has it's own layer, so that each player can be different, only to a certain player

_ = s.c.Close()

// Move UI inventory items to the main inventory.
Expand Down
11 changes: 11 additions & 0 deletions server/session/view_layer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package session

import "github.com/df-mc/dragonfly/server/world"

type LayerViewer interface {
ViewLayer() *world.ViewLayer
}

func (s *Session) ViewLayer() *world.ViewLayer {
return s.viewLayer
}
15 changes: 14 additions & 1 deletion server/session/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,9 +908,22 @@ func (s *Session) ViewEntityAction(e world.Entity, a world.EntityAction) {

// ViewEntityState ...
func (s *Session) ViewEntityState(e world.Entity) {
metadata := s.parseEntityMetadata(e)
if v, ok := e.(LayerViewer); ok {
if nt := s.viewLayer.NameTag(v); len(nt) > 0 {
metadata[protocol.EntityDataKeyName] = nt
}
if visibility := s.viewLayer.Visibility(v); visibility.EnforceVisibility() {
invisibleFlag := metadata.Flag(protocol.EntityDataKeyFlags, protocol.EntityDataFlagInvisible)
if (visibility == world.EnforceVisible() && invisibleFlag) ||
visibility == world.EnforceInvisible() && !invisibleFlag {
metadata.SetFlag(protocol.EntityDataKeyFlags, protocol.EntityDataFlagInvisible)
}
}
}
s.writePacket(&packet.SetActorData{
EntityRuntimeID: s.entityRuntimeID(e),
EntityMetadata: s.parseEntityMetadata(e),
EntityMetadata: metadata,
})
}

Expand Down
84 changes: 84 additions & 0 deletions server/world/view_layer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package world

import (
"sync"
)

type LayerViewer interface {
ViewLayer() *ViewLayer
}

type Layer struct {
nameTag string
visibility VisibilityLevel
}

// ViewLayer is a view layer that can be used to add a layer to the view of a player.
type ViewLayer struct {
viewerMu sync.RWMutex
viewers map[LayerViewer]Layer
}

// NewViewLayer returns a new ViewLayer.
func NewViewLayer() *ViewLayer {
return &ViewLayer{
viewers: map[LayerViewer]Layer{},
}
}

// Viewers returns all viewers of the view layer.
func (v *ViewLayer) Viewers() []LayerViewer {
v.viewerMu.Lock()
defer v.viewerMu.Unlock()
viewers := make([]LayerViewer, 0, len(v.viewers))
for viewer := range v.viewers {
viewers = append(viewers, viewer)
}
return viewers
}

// ViewNameTag overwrites the public name tag of the viewer and allows this ViewLayer to view a different name tag.
// Leaving the name tag empty reverts this behaviour.
func (v *ViewLayer) ViewNameTag(viewer LayerViewer, nameTag string) {
v.viewerMu.Lock()
defer v.viewerMu.Unlock()

l := v.viewers[viewer]
l.nameTag = nameTag
v.viewers[viewer] = l
}

// NameTag returns the overwritten name tag of the viewer.
func (v *ViewLayer) NameTag(viewer LayerViewer) string {
v.viewerMu.Lock()
defer v.viewerMu.Unlock()
return v.viewers[viewer].nameTag
}

// ViewVisibility overwrites the public visibility of the viewer and allows this ViewLayer to view
// this viewer as (in)visible depending on the VisibilityLevel.
func (v *ViewLayer) ViewVisibility(viewer LayerViewer, level VisibilityLevel) {
v.viewerMu.Lock()
defer v.viewerMu.Unlock()

l := v.viewers[viewer]
l.visibility = level
v.viewers[viewer] = l
}

// Visibility returns the visibility of the viewer.
func (v *ViewLayer) Visibility(viewer LayerViewer) VisibilityLevel {
v.viewerMu.Lock()
defer v.viewerMu.Unlock()
return v.viewers[viewer].visibility
}

// Close closes the view layer.
func (v *ViewLayer) Close() error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also remove the view layer instance from the viewers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like v = nil?
Can you be clearer?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete(viewer.viewers, v.e) or something I think, e being the owner of the view layer

v.viewerMu.Lock()
defer v.viewerMu.Unlock()
for viewer := range v.viewers {
delete(v.viewers, viewer)
}
return nil
}
28 changes: 28 additions & 0 deletions server/world/visibility_level.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package world

type VisibilityLevel struct {
visibility
}

// PublicVisibility is the default visibility level where the entity is (in)visible
// depending on how it already is publicly viewed.
func PublicVisibility() VisibilityLevel {
return VisibilityLevel{0}
}

// EnforceInvisible is the visibility level where the entity is always invisible to the viewer.
func EnforceInvisible() VisibilityLevel {
return VisibilityLevel{1}
}

// EnforceVisible is the visibility level where the entity is always visible to the viewer.
func EnforceVisible() VisibilityLevel {
return VisibilityLevel{2}
}

type visibility uint8

// EnforceVisibility returns whether metadata should be changed.
func (v visibility) EnforceVisibility() bool {
return v > 0
}
Loading