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

Add explosion handler for affected blocks and entities #991

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 23 additions & 1 deletion server/block/explosion.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ type ExplosionConfig struct {
// the explosion. If set to 1 or higher, all items are dropped.
ItemDropChance float64

// CanAffectEntity determines if an entity can be affected by this explosion. By default,
// all entities within the size of the explosion will be affected.
CanAffectEntity func(e world.Entity, impact *float64) bool
// CanAffectBlock determines if this block position can be affected by this explosion. By default,
// all Breakable blocks within the size of the explosion will be affected.
CanAffectBlock func(tx *world.Tx, pos cube.Pos) bool

// Sound is the sound to play when the explosion is created. If set to nil, this will default to the sound of a
// regular explosion.
Sound world.Sound
Expand Down Expand Up @@ -86,6 +93,16 @@ func (c ExplosionConfig) Explode(tx *world.Tx, explosionPos mgl64.Vec3) {
if c.ItemDropChance == 0 {
c.ItemDropChance = 1.0 / c.Size
}
if c.CanAffectEntity == nil {
c.CanAffectEntity = func(world.Entity, *float64) bool {
return true
}
}
if c.CanAffectBlock == nil {
c.CanAffectBlock = func(*world.Tx, cube.Pos) bool {
return true
}
}

r, d := rand.New(c.RandSource), c.Size*2
box := cube.Box(
Expand All @@ -105,7 +122,9 @@ func (c ExplosionConfig) Explode(tx *world.Tx, explosionPos mgl64.Vec3) {
}
if explodable, ok := e.(ExplodableEntity); ok {
impact := (1 - dist/d) * exposure(tx, explosionPos, e)
explodable.Explode(explosionPos, impact, c)
if c.CanAffectEntity(e, &impact) {
explodable.Explode(explosionPos, impact, c)
}
}
}

Expand Down Expand Up @@ -133,6 +152,9 @@ func (c ExplosionConfig) Explode(tx *world.Tx, explosionPos mgl64.Vec3) {
}
}
for _, pos := range affectedBlocks {
if !c.CanAffectBlock(tx, pos) {
continue
}
bl := tx.Block(pos)
if explodable, ok := bl.(Explodable); ok {
explodable.Explode(explosionPos, pos, tx, c)
Expand Down
Loading