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

Implemented explosions #562

Merged
merged 37 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
3346de4
explosion/explosion.go: Third attempt at implementing explosions.
T14Raptor Jul 12, 2022
800702d
Merge remote-tracking branch 'origin/master' into feature/explosions
T14Raptor Jul 14, 2022
b172c4d
"Explodable" api
T14Raptor Jul 14, 2022
11882b2
Moved explosions to block package
T14Raptor Jul 14, 2022
61f945c
Merge branch 'master' into feature/explosions
JustTalDevelops Jul 15, 2022
4a3db7d
Merge remote-tracking branch 'origin/master' into feature/explosions
JustTalDevelops Jul 15, 2022
c8c1965
improvements
T14Raptor Jul 15, 2022
fd081cf
block: Added blast resistances for each block.
JustTalDevelops Jul 15, 2022
4da01aa
Merge remote-tracking branch 'origin/feature/explosions' into feature…
JustTalDevelops Jul 15, 2022
f0886fb
block/break_info.go: Improve doc.
JustTalDevelops Jul 15, 2022
a134491
Update server/block/explosion.go
JustTalDevelops Jul 15, 2022
489570f
block/explosion.go: Use `box` for consistency.
JustTalDevelops Jul 15, 2022
b982885
Merge remote-tracking branch 'origin/feature/explosions' into feature…
JustTalDevelops Jul 15, 2022
97b9cf6
block/break_info.go: Don't multiply hardness by 5.
T14Raptor Jul 15, 2022
83e7e71
Merge remote-tracking branch 'origin/feature/explosions' into feature…
T14Raptor Jul 15, 2022
294f673
block/break_info.go: Correct doc.
T14Raptor Jul 15, 2022
020bf23
block/explosion.go: Fix SpawnFire logic.
JustTalDevelops Jul 15, 2022
c026aa5
damage/source.go: Doc SourceExplosion.
JustTalDevelops Jul 15, 2022
011b7f4
player/player.go: Implement ExplodableEntity.
T14Raptor Jul 15, 2022
9cd90fe
Merge remote-tracking branch 'origin/feature/explosions' into feature…
T14Raptor Jul 15, 2022
7a9f47a
player/player.go: Directly set velocity.
JustTalDevelops Jul 15, 2022
d6fabcc
player/player.go: Revert "Directly set velocity".
JustTalDevelops Jul 15, 2022
659a0ca
block/explosion.go: Change Radius field back to Size.
T14Raptor Jul 16, 2022
1c01f45
Merge remote-tracking branch 'origin/feature/explosions' into feature…
T14Raptor Jul 16, 2022
e5e5fa9
Fixes
T14Raptor Jul 16, 2022
cc79531
block/explosions.go: Fixed exposure?
T14Raptor Jul 18, 2022
cb9b588
Merge remote-tracking branch 'origin/master' into feature/explosions
T14Raptor Jul 18, 2022
3a4180a
Merge branch 'master' into feature/explosions
JustTalDevelops Jul 26, 2022
c4f7ec8
block/block.go: Blame GitHub!
JustTalDevelops Jul 26, 2022
cb209b1
block/explosion.go: Divide blast resistance by 5.
T14Raptor Jul 26, 2022
c29385b
player/player.go: Fixed explosion knockback when in creative.
T14Raptor Jul 26, 2022
a2c0bfd
Merge branch 'master' into feature/explosions
JustTalDevelops Jul 26, 2022
c9e5e51
block/explosion.go: Various improvements.
JustTalDevelops Jul 29, 2022
e68e396
Merge branch 'master' into feature/explosions
JustTalDevelops Jul 29, 2022
8fbaf19
model/thin.go: Fix refactor.
JustTalDevelops Jul 29, 2022
df0f525
Merge remote-tracking branch 'origin/feature/explosions' into feature…
JustTalDevelops Jul 29, 2022
3e57c34
Merge branch 'master' into feature/explosions
JustTalDevelops Jul 29, 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
23 changes: 22 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package main
import (
"fmt"
"github.com/df-mc/dragonfly/server"
"github.com/df-mc/dragonfly/server/block"
"github.com/df-mc/dragonfly/server/event"
"github.com/df-mc/dragonfly/server/player"
"github.com/df-mc/dragonfly/server/player/chat"
"github.com/df-mc/dragonfly/server/world"
"github.com/pelletier/go-toml"
"github.com/sirupsen/logrus"
"io/ioutil"
Expand All @@ -28,10 +32,27 @@ func main() {
log.Fatalln(err)
}

for srv.Accept(nil) {
for srv.Accept(func(p *player.Player) {
p.SetGameMode(world.GameModeSurvival)
p.Handle(h{p: p})
}) {
}
}

type h struct {
player.NopHandler

p *player.Player
}

func (h h) HandleChat(*event.Context, *string) {
block.ExplosionConfig{
World: h.p.World(),
Pos: h.p.Position(),
Size: 4,
}.Do()
}

// readConfig reads the configuration from the config.toml file, or creates the file if it does not yet exist.
func readConfig() (server.Config, error) {
c := server.DefaultConfig()
Expand Down
5 changes: 5 additions & 0 deletions server/block/bedrock.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ type Bedrock struct {
InfiniteBurning bool
}

// BreakInfo ...
func (b Bedrock) BreakInfo() BreakInfo {
return newBreakInfo(-1, neverHarvestable, nothingEffective, simpleDrops()).withBlastResistance(0.6)
}

// EncodeItem ...
func (Bedrock) EncodeItem() (name string, meta int16) {
return "minecraft:bedrock", 0
Expand Down
90 changes: 87 additions & 3 deletions server/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package block

import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/entity"
"github.com/df-mc/dragonfly/server/entity/damage"
"github.com/df-mc/dragonfly/server/entity/effect"
"github.com/df-mc/dragonfly/server/entity/healing"
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/sound"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
"time"
)

// Activatable represents a block that may be activated by a viewer of the world. When activated, the block
Expand Down Expand Up @@ -82,7 +87,7 @@ func calculateFace(user item.User, placePos cube.Pos) cube.Face {
pos := cube.PosFromVec3(userPos)
if abs(pos[0]-placePos[0]) < 2 && abs(pos[2]-placePos[2]) < 2 {
y := userPos[1]
if eyed, ok := user.(entity.Eyed); ok {
if eyed, ok := user.(interface{ EyeHeight() float64 }); ok {
y += eyed.EyeHeight()
}

Expand Down Expand Up @@ -201,7 +206,17 @@ func (g gravityAffected) fall(b world.Block, pos cube.Pos, w *world.World) {
_, liquid := w.Liquid(pos.Side(cube.FaceDown))
if air || liquid {
w.SetBlock(pos, nil, nil)
w.AddEntity(entity.NewFallingBlock(b, pos.Vec3Middle()))

ent, ok := world.EntityByName("minecraft:falling_block")
if !ok {
return
}

if p, ok := ent.(interface {
New(bl world.Block, pos mgl64.Vec3) world.Entity
}); ok {
w.AddEntity(p.New(b, pos.Vec3Centre()))
}
}
}

Expand Down Expand Up @@ -230,6 +245,75 @@ func newFlammabilityInfo(encouragement, flammability int, lavaFlammable bool) Fl
}
}

type livingEntity interface {
world.Entity
// Health returns the health of the entity.
Health() float64
// MaxHealth returns the maximum health of the entity.
MaxHealth() float64
// SetMaxHealth changes the maximum health of the entity to the value passed.
SetMaxHealth(v float64)
// Dead checks if the entity is considered dead. True is returned if the health of the entity is equal to or
// lower than 0.
Dead() bool
// AttackImmune checks if the entity is currently immune to entity attacks. Entities typically turn
// immune for half a second after being attacked.
AttackImmune() bool
// Hurt hurts the entity for a given amount of damage. The source passed represents the cause of the
// damage, for example damage.SourceEntityAttack if the entity is attacked by another entity.
// If the final damage exceeds the health that the entity currently has, the entity is killed.
// Hurt returns the final amount of damage dealt to the Living entity and returns whether the Living entity
// was vulnerable to the damage at all.
Hurt(damage float64, src damage.Source) (n float64, vulnerable bool)
// Heal heals the entity for a given amount of health. The source passed represents the cause of the
// healing, for example healing.SourceFood if the entity healed by having a full food bar. If the health
// added to the original health exceeds the entity's max health, Heal may not add the full amount.
Heal(health float64, src healing.Source)
// KnockBack knocks the entity back with a given force and height. A source is passed which indicates the
// source of the velocity, typically the position of an attacking entity. The source is used to calculate
// the direction which the entity should be knocked back in.
KnockBack(src mgl64.Vec3, force, height float64)
// AddEffect adds an entity.Effect to the entity. If the effect is instant, it is applied to the entity
// immediately. If not, the effect is applied to the entity every time the Tick method is called.
// AddEffect will overwrite any effects present if the level of the effect is higher than the existing one, or
// if the effects' levels are equal and the new effect has a longer duration.
AddEffect(e effect.Effect)
// RemoveEffect removes any effect that might currently be active on the entity.
RemoveEffect(e effect.Type)
// Effects returns any effect currently applied to the entity. The returned effects are guaranteed not to have
// expired when returned.
Effects() []effect.Effect
// Speed returns the current speed of the living entity. The default value is different for each entity.
Speed() float64
// SetSpeed sets the speed of an entity to a new value.
SetSpeed(float64)
}
T14Raptor marked this conversation as resolved.
Show resolved Hide resolved

type flammableEntity interface {
// FireProof is whether the entity is currently fireproof.
FireProof() bool
// OnFireDuration returns duration of fire in ticks.
OnFireDuration() time.Duration
// SetOnFire sets the entity on fire for the specified duration.
SetOnFire(duration time.Duration)
// Extinguish extinguishes the entity.
Extinguish()
}
T14Raptor marked this conversation as resolved.
Show resolved Hide resolved

// dropItem ...
func dropItem(w *world.World, it item.Stack, pos mgl64.Vec3) {
ent, ok := world.EntityByName("minecraft:item")
if !ok {
return
}

if p, ok := ent.(interface {
New(it item.Stack, pos, vel mgl64.Vec3) world.Entity
}); ok {
w.AddEntity(p.New(it, pos, mgl64.Vec3{rand.Float64()*0.2 - 0.1, 0.2, rand.Float64()*0.2 - 0.1}))
}
}

// bass is a struct that may be embedded for blocks that create a bass sound.
type bass struct{}

Expand Down
8 changes: 8 additions & 0 deletions server/block/break_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ type BreakInfo struct {
Drops func(t item.Tool, enchantments []item.Enchantment) []item.Stack
// XPDrops is the range of XP a block can drop when broken.
XPDrops XPDropRange
// BlastResistance ...
BlastResistance float64
T14Raptor marked this conversation as resolved.
Show resolved Hide resolved
}

// newBreakInfo creates a BreakInfo struct with the properties passed. The XPDrops field is 0 by default.
Expand All @@ -103,6 +105,12 @@ func (b BreakInfo) withXPDropRange(min, max int) BreakInfo {
return b
}

// withBlastResistance sets the BlastResistance field of the BreakInfo struct to the passed value.
func (b BreakInfo) withBlastResistance(res float64) BreakInfo {
b.BlastResistance = res
return b
}

// XPDropRange holds the min & max XP drop amounts of blocks.
type XPDropRange [2]int

Expand Down
3 changes: 1 addition & 2 deletions server/block/cactus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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/entity"
"github.com/df-mc/dragonfly/server/entity/damage"
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
Expand Down Expand Up @@ -78,7 +77,7 @@ func (c Cactus) RandomTick(pos cube.Pos, w *world.World, r *rand.Rand) {

// EntityInside ...
func (c Cactus) EntityInside(_ cube.Pos, _ *world.World, e world.Entity) {
if l, ok := e.(entity.Living); ok && !l.AttackImmune() {
if l, ok := e.(livingEntity); ok && !l.AttackImmune() {
l.Hurt(0.5, damage.SourceBlock{Block: c})
}
}
Expand Down
7 changes: 1 addition & 6 deletions server/block/crop.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package block

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

// Crop is an interface for all crops that are grown on farmland. A crop has a random chance to grow during random ticks.
Expand All @@ -33,9 +30,7 @@ func (c crop) NeighbourUpdateTick(pos, _ cube.Pos, w *world.World) {
w.SetBlock(pos, nil, nil)
if breakable, ok := b.(Breakable); ok {
for _, drop := range breakable.BreakInfo().Drops(item.ToolNone{}, []item.Enchantment{}) {
itemEntity := entity.NewItem(drop, pos.Vec3Centre())
itemEntity.SetVelocity(mgl64.Vec3{rand.Float64()*0.2 - 0.1, 0.2, rand.Float64()*0.2 - 0.1})
w.AddEntity(itemEntity)
dropItem(w, drop, pos.Vec3Centre())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/block/dirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (d Dirt) SoilFor(block world.Block) bool {

// BreakInfo ...
func (d Dirt) BreakInfo() BreakInfo {
return newBreakInfo(0.5, alwaysHarvestable, shovelEffective, oneOf(d))
return newBreakInfo(0.5, alwaysHarvestable, shovelEffective, oneOf(d)).withBlastResistance(0.5)
}

// Till ...
Expand Down
6 changes: 1 addition & 5 deletions server/block/double_flower.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package block

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

// DoubleFlower is a two block high flower consisting of an upper and lower part.
Expand All @@ -28,9 +26,7 @@ func (d DoubleFlower) FlammabilityInfo() FlammabilityInfo {

// BoneMeal ...
func (d DoubleFlower) BoneMeal(pos cube.Pos, w *world.World) bool {
itemEntity := entity.NewItem(item.NewStack(d, 1), pos.Vec3Centre())
itemEntity.SetVelocity(mgl64.Vec3{rand.Float64()*0.2 - 0.1, 0.2, rand.Float64()*0.2 - 0.1})
w.AddEntity(itemEntity)
dropItem(w, item.NewStack(d, 1), pos.Vec3Centre())
return true
}

Expand Down
Loading