Skip to content

Commit

Permalink
Bunch of transactions being passed around now.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandertv committed Oct 19, 2024
1 parent a13eb38 commit f7ff3ab
Show file tree
Hide file tree
Showing 36 changed files with 379 additions and 361 deletions.
12 changes: 6 additions & 6 deletions server/entity/area_effect_cloud_behaviour.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ func (a *AreaEffectCloudBehaviour) Effects() []effect.Effect {
}

// Tick ...
func (a *AreaEffectCloudBehaviour) Tick(e *Ent) *Movement {
a.stationary.Tick(e)
func (a *AreaEffectCloudBehaviour) Tick(e *Ent, tx *world.Tx) *Movement {
a.stationary.Tick(e, tx)
if a.stationary.close || a.stationary.age < 10 {
// The cloud lives for at least half a second before it may begin
// spreading effects and growing/shrinking.
return nil
}

pos, w := e.Position(), e.World()
pos := e.Position()
if a.subtractTickRadius(e) {
for _, v := range w.Viewers(pos) {
for _, v := range tx.Viewers(pos) {
v.ViewEntityState(e)
}
}
Expand All @@ -101,13 +101,13 @@ func (a *AreaEffectCloudBehaviour) Tick(e *Ent) *Movement {
}
}

entities := w.EntitiesWithin(e.Type().BBox(e).Translate(pos), func(entity world.Entity) bool {
entities := tx.EntitiesWithin(e.Type().BBox(e).Translate(pos), func(entity world.Entity) bool {
_, target := a.targets[entity]
_, living := entity.(Living)
return !living || target || entity == e
})
if a.applyEffects(pos, e, entities) {
for _, v := range w.Viewers(pos) {
for _, v := range tx.Viewers(pos) {
v.ViewEntityState(e)
}
}
Expand Down
5 changes: 3 additions & 2 deletions server/entity/effect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package entity
import (
"fmt"
"github.com/df-mc/dragonfly/server/entity/effect"
"github.com/df-mc/dragonfly/server/world"
"reflect"
"sync"
)
Expand Down Expand Up @@ -99,7 +100,7 @@ func (m *EffectManager) Effects() []effect.Effect {

// Tick ticks the EffectManager, applying all of its effects to the Living entity passed when applicable and
// removing expired effects.
func (m *EffectManager) Tick(entity Living) {
func (m *EffectManager) Tick(entity Living, tx *world.Tx) {
m.mu.Lock()
e := make([]effect.Effect, 0, len(m.effects))
var toEnd []effect.Effect
Expand All @@ -124,7 +125,7 @@ func (m *EffectManager) Tick(entity Living) {
}

if len(toEnd) > 0 {
for _, v := range entity.World().Viewers(entity.Position()) {
for _, v := range tx.Viewers(entity.Position()) {
v.ViewEntityState(entity)
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/entity/ender_pearl.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ type teleporter interface {
}

// teleport teleports the owner of an Ent to a trace.Result's position.
func teleport(e *Ent, target trace.Result) {
func teleport(e *Ent, tx *world.Tx, target trace.Result) {
if user, ok := e.Behaviour().(*ProjectileBehaviour).Owner().(teleporter); ok {
e.World().PlaySound(user.Position(), sound.Teleport{})
tx.PlaySound(user.Position(), sound.Teleport{})
user.Teleport(target.Position())
user.Hurt(5, FallDamageSource{})
}
Expand Down
27 changes: 18 additions & 9 deletions server/entity/ent.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Behaviour interface {
// Tick ticks the Ent using the Behaviour. A Movement is returned that
// specifies the movement of the entity over the tick. Nil may be returned
// if the entity did not move.
Tick(e *Ent) *Movement
Tick(e *Ent, tx *world.Tx) *Movement
}

// Config allows specifying options that influence the way an Ent behaves.
Expand All @@ -31,6 +31,10 @@ func (conf Config) New(t world.EntityType, pos mgl64.Vec3) *Ent {
// share a lot of code. It is currently under development and is prone to
// (breaking) changes.
type Ent struct {
tx *world.Tx
handle *world.EntityHandle
data *world.EntityData

conf Config
t world.EntityType

Expand All @@ -45,6 +49,11 @@ type Ent struct {
age time.Duration
}

func (e *Ent) Handle() *world.EntityHandle {
// TODO: Move this over to world.EntityHandle.
return nil
}

// Explode propagates the explosion behaviour of the underlying Behaviour.
func (e *Ent) Explode(src mgl64.Vec3, impact float64, conf block.ExplosionConfig) {
if expl, ok := e.conf.Behaviour.(interface {
Expand Down Expand Up @@ -95,8 +104,8 @@ func (e *Ent) Rotation() cube.Rotation {

// World returns the world of the entity.
func (e *Ent) World() *world.World {
w, _ := world.OfEntity(e)
return w
// TODO: Fix this
return nil
}

// Age returns the total time lived of this entity. It increases by
Expand Down Expand Up @@ -128,7 +137,7 @@ func (e *Ent) SetOnFire(duration time.Duration) {
if before == after {
return
}
for _, v := range e.World().Viewers(pos) {
for _, v := range e.tx.Viewers(pos) {
v.ViewEntityState(e)
}
}
Expand All @@ -153,24 +162,24 @@ func (e *Ent) SetNameTag(s string) {
e.name = s
e.mu.Unlock()

for _, v := range e.World().Viewers(e.Position()) {
for _, v := range e.tx.Viewers(e.Position()) {
v.ViewEntityState(e)
}
}

// Tick ticks Ent, progressing its lifetime and closing the entity if it is
// in the void.
func (e *Ent) Tick(w *world.World, current int64) {
func (e *Ent) Tick(tx *world.Tx, current int64) {
e.mu.Lock()
y := e.pos[1]
e.mu.Unlock()
if y < float64(w.Range()[0]) && current%10 == 0 {
if y < float64(tx.Range()[0]) && current%10 == 0 {
_ = e.Close()
return
}
e.SetOnFire(e.OnFireDuration() - time.Second/20)

if m := e.conf.Behaviour.Tick(e); m != nil {
if m := e.conf.Behaviour.Tick(e, tx); m != nil {
m.Send()
}
e.mu.Lock()
Expand All @@ -180,6 +189,6 @@ func (e *Ent) Tick(w *world.World, current int64) {

// Close closes the Ent and removes the associated entity from the world.
func (e *Ent) Close() error {
e.World().RemoveEntity(e)
e.tx.RemoveEntity(e)
return nil
}
16 changes: 8 additions & 8 deletions server/entity/experience_orb_behaviour.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,32 @@ func (exp *ExperienceOrbBehaviour) Experience() int {
}

// Tick finds a target for the experience orb and moves the orb towards it.
func (exp *ExperienceOrbBehaviour) Tick(e *Ent) *Movement {
return exp.passive.Tick(e)
func (exp *ExperienceOrbBehaviour) Tick(e *Ent, tx *world.Tx) *Movement {
return exp.passive.Tick(e, tx)
}

// followBox is the bounding box used to search for collectors to follow for experience orbs.
var followBox = cube.Box(-8, -8, -8, 8, 8, 8)

// tick finds a target for the experience orb and moves the orb towards it.
func (exp *ExperienceOrbBehaviour) tick(e *Ent) {
w, pos := e.World(), e.Position()
if exp.target != nil && (exp.target.Dead() || exp.target.World() != w || pos.Sub(exp.target.Position()).Len() > 8) {
func (exp *ExperienceOrbBehaviour) tick(e *Ent, tx *world.Tx) {
pos := e.Position()
if exp.target != nil && (exp.target.Dead() || exp.target.World() != tx.World() || pos.Sub(exp.target.Position()).Len() > 8) {
exp.target = nil
}

if time.Since(exp.lastSearch) >= time.Second {
exp.findTarget(w, pos)
exp.findTarget(tx, pos)
}
if exp.target != nil {
exp.moveToTarget(e)
}
}

// findTarget attempts to find a target for an experience orb in w around pos.
func (exp *ExperienceOrbBehaviour) findTarget(w *world.World, pos mgl64.Vec3) {
func (exp *ExperienceOrbBehaviour) findTarget(tx *world.Tx, pos mgl64.Vec3) {
if exp.target == nil {
collectors := w.EntitiesWithin(followBox.Translate(pos), func(o world.Entity) bool {
collectors := tx.EntitiesWithin(followBox.Translate(pos), func(o world.Entity) bool {
_, ok := o.(experienceCollector)
return !ok
})
Expand Down
12 changes: 6 additions & 6 deletions server/entity/falling_block_behaviour.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ func (f *FallingBlockBehaviour) Block() world.Block {
}

// Tick implements the movement and solidification behaviour of falling blocks.
func (f *FallingBlockBehaviour) Tick(e *Ent) *Movement {
return f.passive.Tick(e)
func (f *FallingBlockBehaviour) Tick(e *Ent, tx *world.Tx) *Movement {
return f.passive.Tick(e, tx)
}

// tick checks if the falling block should solidify.
func (f *FallingBlockBehaviour) tick(e *Ent) {
func (f *FallingBlockBehaviour) tick(e *Ent, tx *world.Tx) {
pos := e.Position()
bpos, w := cube.PosFromVec3(pos), e.World()
if a, ok := f.block.(Solidifiable); (ok && a.Solidifies(bpos, w)) || f.passive.mc.OnGround() {
f.solidify(e, pos, w)
bpos := cube.PosFromVec3(pos)
if a, ok := f.block.(Solidifiable); (ok && a.Solidifies(bpos, tx)) || f.passive.mc.OnGround() {
f.solidify(e, pos, tx)
}
}

Expand Down
22 changes: 11 additions & 11 deletions server/entity/firework_behaviour.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ func (f *FireworkBehaviour) Owner() world.Entity {

// Tick moves the firework and makes it explode when it reaches its maximum
// duration.
func (f *FireworkBehaviour) Tick(e *Ent) *Movement {
return f.passive.Tick(e)
func (f *FireworkBehaviour) Tick(e *Ent, tx *world.Tx) *Movement {
return f.passive.Tick(e, tx)
}

// tick ticks the entity, updating its velocity either with a constant factor
// or based on the owner's position and velocity if attached.
func (f *FireworkBehaviour) tick(e *Ent) {
func (f *FireworkBehaviour) tick(e *Ent, _ *world.Tx) {
var ownerVel mgl64.Vec3
if o, ok := f.owner.(interface {
Velocity() mgl64.Vec3
Expand All @@ -98,20 +98,20 @@ func (f *FireworkBehaviour) tick(e *Ent) {

// explode causes an explosion at the position of the firework, spawning
// particles and damaging nearby entities.
func (f *FireworkBehaviour) explode(e *Ent) {
w, pos, explosions := e.World(), e.Position(), f.firework.Explosions
func (f *FireworkBehaviour) explode(e *Ent, tx *world.Tx) {
pos, explosions := e.Position(), f.firework.Explosions

for _, v := range w.Viewers(pos) {
for _, v := range tx.Viewers(pos) {
v.ViewEntityAction(e, FireworkExplosionAction{})
}
for _, explosion := range explosions {
if explosion.Shape == item.FireworkShapeHugeSphere() {
w.PlaySound(pos, sound.FireworkHugeBlast{})
tx.PlaySound(pos, sound.FireworkHugeBlast{})
} else {
w.PlaySound(pos, sound.FireworkBlast{})
tx.PlaySound(pos, sound.FireworkBlast{})
}
if explosion.Twinkle {
w.PlaySound(pos, sound.FireworkTwinkle{})
tx.PlaySound(pos, sound.FireworkTwinkle{})
}
}

Expand All @@ -120,7 +120,7 @@ func (f *FireworkBehaviour) explode(e *Ent) {
}

force := float64(len(explosions)*2) + 5.0
targets := w.EntitiesWithin(e.Type().BBox(e).Translate(pos).Grow(5.25), func(e world.Entity) bool {
targets := tx.EntitiesWithin(e.Type().BBox(e).Translate(pos).Grow(5.25), func(e world.Entity) bool {
l, living := e.(Living)
return !living || l.AttackImmune()
})
Expand All @@ -138,7 +138,7 @@ func (f *FireworkBehaviour) explode(e *Ent) {
e.(Living).Hurt(dmg, src)
continue
}
if _, ok := trace.Perform(pos, tpos, w, e.Type().BBox(e).Grow(0.3), func(world.Entity) bool {
if _, ok := trace.Perform(pos, tpos, tx, e.Type().BBox(e).Grow(0.3), func(world.Entity) bool {
return true
}); ok {
e.(Living).Hurt(dmg, src)
Expand Down
Loading

0 comments on commit f7ff3ab

Please sign in to comment.