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

Nether portals and mechanics #426

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
646907e
Initial implementation of nether portals (WIP)
JustTalDevelops Feb 19, 2022
78f1ade
block/portal.go: Simply portal deactivation on neighbour change.
JustTalDevelops Feb 19, 2022
f777865
player/player.go: Make Travel exported.
JustTalDevelops Feb 19, 2022
694f4c2
world/world.go: Moved Place checks to the PlaceBlock function.
JustTalDevelops Feb 19, 2022
5b72308
block/portal.go: Fix destroying a frame not destroying all frames.
JustTalDevelops Feb 19, 2022
d6e58bd
player/player.go: Default the portal timeout to true.
JustTalDevelops Feb 19, 2022
50b9b73
player/player.go: Don't request a new transfer during a transfer if t…
JustTalDevelops Feb 19, 2022
bed9456
block/portal.go: Destroy portals when water flows into a frame.
JustTalDevelops Feb 19, 2022
5577268
portal/scan.go: Allow portals that don't specify requirements to be m…
JustTalDevelops Feb 19, 2022
656a992
portal/scan.go: Fix completion check.
JustTalDevelops Feb 19, 2022
69c1c07
player/player.go: Ensure the position is only translated if the sourc…
JustTalDevelops Feb 19, 2022
8711c67
portal/scan.go: Simplify logic.
JustTalDevelops Feb 20, 2022
38c3928
Revert "portal/scan.go: Simplify logic."
JustTalDevelops Feb 20, 2022
fc2cf59
Fix multi-axis scan logic
JustTalDevelops Feb 20, 2022
ade7b4d
Detect portal collision using their block models
JustTalDevelops Feb 22, 2022
cdc901c
player/player.go: Fix typo.
JustTalDevelops Feb 22, 2022
95bc01e
portal/nether.go: Fixed intersecting portals.
JustTalDevelops Feb 22, 2022
7a76d60
model/portal.go: Fix missing zero before decimal point.
JustTalDevelops Feb 22, 2022
eedec39
Allow fire blocks to be overridden by portal ignition
JustTalDevelops Feb 23, 2022
fca683d
Implement TravelComputers for other entities that can travel through …
JustTalDevelops Feb 23, 2022
8538faf
entity/travel.go: Slightly increase AABB growth.
JustTalDevelops Feb 23, 2022
51f7e36
Use the *world.World provided in Tick.
JustTalDevelops Feb 23, 2022
4431fa3
entity/travel.go: Only grow AABB after the BlocksAround call
JustTalDevelops Feb 23, 2022
479d6b6
Merge remote-tracking branch 'origin/master' into feature/nether-portals
JustTalDevelops Feb 24, 2022
d8d8f3a
block/portal.go: Fix typo.
JustTalDevelops Feb 26, 2022
b45deae
Remove world.Entity as an Instantaneous parameter
JustTalDevelops Feb 27, 2022
b431bd7
Undo unnecessary ordering
JustTalDevelops Feb 27, 2022
3bb09b0
entity/travel.go: Update Traveller doc.
JustTalDevelops Feb 27, 2022
611f4aa
Use an interface to detect a portal collision
JustTalDevelops Feb 27, 2022
8223c9c
Merge branch 'master' into feature/nether-portals
JustTalDevelops Jul 31, 2022
5e3b773
portal/nether.go: Various fixes.
JustTalDevelops Jul 31, 2022
0295a6f
Merge branch 'master' into feature/nether-portals
JustTalDevelops Jul 31, 2022
cfc74d7
entity/travel.go: Various improvements.
JustTalDevelops Jul 31, 2022
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
213 changes: 114 additions & 99 deletions server/block/fire.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/df-mc/dragonfly/server/entity"
"github.com/df-mc/dragonfly/server/entity/damage"
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/portal"
"math/rand"
"time"
)
Expand All @@ -24,40 +25,98 @@ type Fire struct {
Age int
}

// flammableBlock returns true if a block is flammable.
func flammableBlock(block world.Block) bool {
flammable, ok := block.(Flammable)
return ok && flammable.FlammabilityInfo().Encouragement > 0
// Place ...
func (Fire) Place(pos cube.Pos, w *world.World) bool {
for _, f := range cube.Faces() {
if o, ok := w.Block(pos.Side(f)).(Obsidian); ok && !o.Crying {
if p, ok := portal.NetherPortalFromPos(w, pos); ok && p.Framed() && !p.Activated() {
p.Activate()
return false
}
return true
}
}
return true
}

// neighboursFlammable returns true if one a block adjacent to the passed position is flammable.
func neighboursFlammable(pos cube.Pos, w *world.World) bool {
for _, i := range cube.Faces() {
if flammableBlock(w.Block(pos.Side(i))) {
return true
// EntityInside ...
func (f Fire) EntityInside(_ cube.Pos, _ *world.World, e world.Entity) {
if flammable, ok := e.(entity.Flammable); ok {
if l, ok := e.(entity.Living); ok && !l.AttackImmune() {
l.Hurt(1, damage.SourceFire{})
}
if flammable.OnFireDuration() < time.Second*8 {
flammable.SetOnFire(8 * time.Second)
}
}
}

// ScheduledTick ...
func (f Fire) ScheduledTick(pos cube.Pos, w *world.World, r *rand.Rand) {
f.tick(pos, w, r)
}

// RandomTick ...
func (f Fire) RandomTick(pos cube.Pos, w *world.World, r *rand.Rand) {
f.tick(pos, w, r)
}

// NeighbourUpdateTick ...
func (f Fire) NeighbourUpdateTick(pos, neighbour cube.Pos, w *world.World) {
below := w.Block(pos.Side(cube.FaceDown))
if diffuser, ok := below.(LightDiffuser); (ok && diffuser.LightDiffusionLevel() != 15) && (!neighboursFlammable(pos, w) || f.Type == SoulFire()) {
w.BreakBlockWithoutParticles(pos)
return
}
switch below.(type) {
case SoulSand, SoulSoil:
f.Type = SoulFire()
w.PlaceBlock(pos, f)
case Water:
if neighbour == pos {
w.BreakBlockWithoutParticles(pos)
}
default:
if f.Type == SoulFire() {
w.BreakBlockWithoutParticles(pos)
return
}
}
}

// HasLiquidDrops ...
func (f Fire) HasLiquidDrops() bool {
return false
}

// max ...
func max(a, b int) int {
if a > b {
return a
// LightEmissionLevel ...
func (f Fire) LightEmissionLevel() uint8 {
return f.Type.LightLevel()
}

// EncodeBlock ...
func (f Fire) EncodeBlock() (name string, properties map[string]interface{}) {
switch f.Type {
case NormalFire():
return "minecraft:fire", map[string]interface{}{"age": int32(f.Age)}
case SoulFire():
return "minecraft:soul_fire", map[string]interface{}{"age": int32(f.Age)}
}
return b
panic("unknown fire type")
}

// infinitelyBurning returns true if fire can infinitely burn at the specified position.
func infinitelyBurning(pos cube.Pos, w *world.World) bool {
switch block := w.Block(pos.Side(cube.FaceDown)).(type) {
//TODO: Magma Block
case Netherrack:
return true
case Bedrock:
return block.InfiniteBurning
// Start starts a fire at a position in the world. The position passed must be either air or tall grass and conditions
// for a fire to be present must be present.
func (f Fire) Start(w *world.World, pos cube.Pos) {
b := w.Block(pos)
_, isAir := b.(Air)
_, isTallGrass := b.(TallGrass)
if isAir || isTallGrass {
below := w.Block(pos.Side(cube.FaceDown))
if below.Model().FaceSolid(pos, cube.FaceUp, w) || neighboursFlammable(pos, w) {
w.PlaceBlock(pos, Fire{})
}
}
return false
}

// burn attempts to burn a block.
Expand All @@ -75,18 +134,6 @@ func (f Fire) burn(pos cube.Pos, w *world.World, r *rand.Rand, chanceBound int)
}
}

// rainingAround checks if it is raining either at the cube.Pos passed or at any of its horizontal neighbours.
func rainingAround(pos cube.Pos, w *world.World) bool {
raining := w.RainingAt(pos)
for _, face := range cube.HorizontalFaces() {
if raining {
break
}
raining = w.RainingAt(pos.Side(face))
}
return raining
}

// tick ...
func (f Fire) tick(pos cube.Pos, w *world.World, r *rand.Rand) {
if f.Type == SoulFire() {
Expand Down Expand Up @@ -174,84 +221,52 @@ func (f Fire) tick(pos cube.Pos, w *world.World, r *rand.Rand) {
}
}

// EntityInside ...
func (f Fire) EntityInside(_ cube.Pos, _ *world.World, e world.Entity) {
if flammable, ok := e.(entity.Flammable); ok {
if l, ok := e.(entity.Living); ok && !l.AttackImmune() {
l.Hurt(1, damage.SourceFire{})
}
if flammable.OnFireDuration() < time.Second*8 {
flammable.SetOnFire(8 * time.Second)
}
}
}

// ScheduledTick ...
func (f Fire) ScheduledTick(pos cube.Pos, w *world.World, r *rand.Rand) {
f.tick(pos, w, r)
}

// RandomTick ...
func (f Fire) RandomTick(pos cube.Pos, w *world.World, r *rand.Rand) {
f.tick(pos, w, r)
// flammableBlock returns true if a block is flammable.
func flammableBlock(block world.Block) bool {
flammable, ok := block.(Flammable)
return ok && flammable.FlammabilityInfo().Encouragement > 0
}

// NeighbourUpdateTick ...
func (f Fire) NeighbourUpdateTick(pos, neighbour cube.Pos, w *world.World) {
below := w.Block(pos.Side(cube.FaceDown))
if diffuser, ok := below.(LightDiffuser); (ok && diffuser.LightDiffusionLevel() != 15) && (!neighboursFlammable(pos, w) || f.Type == SoulFire()) {
w.BreakBlockWithoutParticles(pos)
return
}
switch below.(type) {
case SoulSand, SoulSoil:
f.Type = SoulFire()
w.PlaceBlock(pos, f)
case Water:
if neighbour == pos {
w.BreakBlockWithoutParticles(pos)
}
default:
if f.Type == SoulFire() {
w.BreakBlockWithoutParticles(pos)
return
// neighboursFlammable returns true if one a block adjacent to the passed position is flammable.
func neighboursFlammable(pos cube.Pos, w *world.World) bool {
for _, i := range cube.Faces() {
if flammableBlock(w.Block(pos.Side(i))) {
return true
}
}
}

// HasLiquidDrops ...
func (f Fire) HasLiquidDrops() bool {
return false
}

// LightEmissionLevel ...
func (f Fire) LightEmissionLevel() uint8 {
return f.Type.LightLevel()
// max ...
func max(a, b int) int {
if a > b {
return a
}
return b
}

// EncodeBlock ...
func (f Fire) EncodeBlock() (name string, properties map[string]interface{}) {
switch f.Type {
case NormalFire():
return "minecraft:fire", map[string]interface{}{"age": int32(f.Age)}
case SoulFire():
return "minecraft:soul_fire", map[string]interface{}{"age": int32(f.Age)}
// infinitelyBurning returns true if fire can infinitely burn at the specified position.
func infinitelyBurning(pos cube.Pos, w *world.World) bool {
switch block := w.Block(pos.Side(cube.FaceDown)).(type) {
//TODO: Magma Block
case Netherrack:
return true
case Bedrock:
return block.InfiniteBurning
}
panic("unknown fire type")
return false
}

// Start starts a fire at a position in the world. The position passed must be either air or tall grass and conditions
// for a fire to be present must be present.
func (f Fire) Start(w *world.World, pos cube.Pos) {
b := w.Block(pos)
_, isAir := b.(Air)
_, isTallGrass := b.(TallGrass)
if isAir || isTallGrass {
below := w.Block(pos.Side(cube.FaceDown))
if below.Model().FaceSolid(pos, cube.FaceUp, w) || neighboursFlammable(pos, w) {
w.PlaceBlock(pos, Fire{})
// rainingAround checks if it is raining either at the cube.Pos passed or at any of its horizontal neighbours.
func rainingAround(pos cube.Pos, w *world.World) bool {
raining := w.RainingAt(pos)
for _, face := range cube.HorizontalFaces() {
if raining {
break
}
raining = w.RainingAt(pos.Side(face))
}
return raining
}

// allFire ...
Expand Down
5 changes: 5 additions & 0 deletions server/block/hash.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions server/block/model/portal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package model

import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/entity/physics"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
)

// Portal is a model used by portal blocks.
type Portal struct {
// Axis is the axis which the portal faces.
Axis cube.Axis
}

// AABB ...
func (p Portal) AABB(cube.Pos, *world.World) []physics.AABB {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if this should return a non-empty AABB. Once we implement suffocation I imagine it'll use the AABB of the block to decide if an entity should suffocate, but this wouldn't work properly with that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, this would cause issues with the logic to check if the player is intersecting with a portal block though, no?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it would. We might need to put the AABB for that somewhere else. Blocks like water have a zero AABB as well at the moment.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe models should have a function to tell whether the block is "solid" or not.

Copy link
Member Author

Choose a reason for hiding this comment

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

Any idea if/how I should change this? I am a bit stuck here.

Copy link
Member

@T14Raptor T14Raptor Mar 15, 2022

Choose a reason for hiding this comment

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

Maybe models should have a function to tell whether the block is "solid" or not.

@Sandertv

Copy link
Member Author

Choose a reason for hiding this comment

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

Is this still a big issue, given suffocation is now implemented? Although I do think a function to tell if blocks are solid or not would be helpful, especially for MovementComputer-based entities.

min, max := mgl64.Vec3{0, 0, 0.375}, mgl64.Vec3{1, 1, 0.25}
if p.Axis == cube.Z {
min[0], min[2], max[0], max[2] = 0.375, 0, 0.25, 1
}
return []physics.AABB{physics.NewAABB(min, max)}
}

// FaceSolid ...
func (Portal) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
return false
}
38 changes: 38 additions & 0 deletions server/block/portal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package block

import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/block/model"
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/portal"
)

// Portal is the translucent part of the nether portal that teleports the player to and from the Nether.
type Portal struct {
transparent

// Axis is the axis which the portal faces.
Axis cube.Axis
}

// Model ...
func (p Portal) Model() world.BlockModel {
return model.Portal{Axis: p.Axis}
}

// EncodeBlock ...
func (p Portal) EncodeBlock() (string, map[string]interface{}) {
return "minecraft:portal", map[string]interface{}{"portal_axis": p.Axis.String()}
}

// HasLiquidDrops ...
func (p Portal) HasLiquidDrops() bool {
return false
}

// NeighbourUpdateTick ...
func (p Portal) NeighbourUpdateTick(pos, _ cube.Pos, w *world.World) {
if n, ok := portal.NetherPortalFromPos(w, pos); ok && (!n.Framed() || !n.Activated()) {
n.Deactivate()
}
}
3 changes: 3 additions & 0 deletions server/block/register.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package block

import (
"github.com/df-mc/dragonfly/server/block/cube"
_ "github.com/df-mc/dragonfly/server/internal/block_internal"
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
Expand Down Expand Up @@ -29,6 +30,8 @@ func init() {
world.RegisterBlock(Bedrock{InfiniteBurning: true})
world.RegisterBlock(Obsidian{})
world.RegisterBlock(Obsidian{Crying: true})
world.RegisterBlock(Portal{Axis: cube.X})
world.RegisterBlock(Portal{Axis: cube.Z})
world.RegisterBlock(DiamondBlock{})
world.RegisterBlock(Glass{})
world.RegisterBlock(Glowstone{})
Expand Down
2 changes: 1 addition & 1 deletion server/entity/falling_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (f *FallingBlock) Block() world.Block {
// Tick ...
func (f *FallingBlock) Tick(w *world.World, _ int64) {
f.mu.Lock()
m := f.c.TickMovement(f, f.pos, f.vel, 0, 0)
m := f.c.TickMovement(w, f, f.pos, f.vel, 0, 0)
f.pos, f.vel = m.pos, m.vel
f.mu.Unlock()

Expand Down
Loading