bedrocktool/subcommands/world/map_item.go

281 lines
6.5 KiB
Go
Raw Normal View History

2022-09-04 14:26:32 +00:00
package world
2022-07-29 16:12:06 +00:00
import (
2022-08-20 12:13:56 +00:00
"bytes"
2022-07-29 16:12:06 +00:00
"image"
2022-08-20 12:13:56 +00:00
"image/draw"
2022-08-12 02:53:43 +00:00
"math"
2022-08-20 12:13:56 +00:00
"os"
2022-09-04 11:13:45 +00:00
"time"
2022-07-29 16:12:06 +00:00
2023-02-12 21:22:44 +00:00
"github.com/bedrock-tool/bedrocktool/locale"
2022-09-04 14:53:21 +00:00
"github.com/bedrock-tool/bedrocktool/utils"
2022-09-05 19:08:57 +00:00
"golang.design/x/lockfree"
2022-09-04 14:26:32 +00:00
2022-08-12 16:54:29 +00:00
"github.com/df-mc/dragonfly/server/world/chunk"
2022-07-29 16:12:06 +00:00
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
2022-09-04 11:13:45 +00:00
"github.com/sirupsen/logrus"
2022-08-20 12:13:56 +00:00
"golang.org/x/image/bmp"
2022-07-29 16:12:06 +00:00
)
2023-01-29 21:20:13 +00:00
const ViewMapID = 0x424242
2022-07-29 16:12:06 +00:00
2023-01-29 21:20:13 +00:00
// MapItemPacket tells the client that it has a map with id 0x424242 in the offhand
var MapItemPacket packet.InventoryContent = packet.InventoryContent{
2022-07-29 16:12:06 +00:00
WindowID: 119,
Content: []protocol.ItemInstance{
{
2023-01-24 21:41:37 +00:00
StackNetworkID: 1, // random if auth inv
2022-07-29 16:12:06 +00:00
Stack: protocol.ItemStack{
ItemType: protocol.ItemType{
2023-01-24 21:41:37 +00:00
NetworkID: 420, // overwritten in onconnect
2022-07-29 16:12:06 +00:00
MetadataValue: 0,
},
BlockRuntimeID: 0,
Count: 1,
NBTData: map[string]interface{}{
2023-02-05 16:41:06 +00:00
"map_name_index": int64(1),
"map_uuid": int64(ViewMapID),
2022-07-29 16:12:06 +00:00
},
},
},
},
}
2023-01-29 21:20:13 +00:00
func (m *MapUI) getBounds() (min, max protocol.ChunkPos) {
2022-09-09 12:58:02 +00:00
// get the chunk coord bounds
for _ch := range m.renderedChunks {
if _ch.X() < min.X() {
min[0] = _ch.X()
}
if _ch.Z() < min.Z() {
min[1] = _ch.Z()
}
if _ch.X() > max.X() {
max[0] = _ch.X()
}
if _ch.Z() > max.Z() {
max[1] = _ch.Z()
}
}
return
}
2022-09-05 15:40:03 +00:00
type RenderElem struct {
pos protocol.ChunkPos
ch *chunk.Chunk
}
2022-08-12 16:54:29 +00:00
type MapUI struct {
2022-09-05 19:08:57 +00:00
img *image.RGBA // rendered image
zoomLevel int // pixels per chunk
renderQueue *lockfree.Queue
renderedChunks map[protocol.ChunkPos]*image.RGBA // prerendered chunks
needRedraw bool // when the map has updated this is true
2022-09-04 11:13:45 +00:00
ticker *time.Ticker
w *WorldState
2022-08-12 16:54:29 +00:00
}
2022-09-05 15:40:03 +00:00
func NewMapUI(w *WorldState) *MapUI {
m := &MapUI{
2022-09-05 19:08:57 +00:00
img: image.NewRGBA(image.Rect(0, 0, 128, 128)),
zoomLevel: 16,
renderQueue: lockfree.NewQueue(),
renderedChunks: make(map[protocol.ChunkPos]*image.RGBA),
needRedraw: true,
w: w,
2022-09-04 11:13:45 +00:00
}
2022-09-05 15:40:03 +00:00
return m
2022-09-04 11:13:45 +00:00
}
func (m *MapUI) Start() {
2023-02-05 16:41:06 +00:00
// init map
2023-02-08 11:00:36 +00:00
if m.w.proxy.Client != nil {
if err := m.w.proxy.Client.WritePacket(&packet.ClientBoundMapItemData{
MapID: ViewMapID,
Scale: 4,
MapsIncludedIn: []int64{ViewMapID},
Width: 0,
Height: 0,
Pixels: nil,
UpdateFlags: packet.MapUpdateFlagInitialisation,
}); err != nil {
logrus.Error(err)
return
}
2023-02-05 16:41:06 +00:00
}
2022-09-07 12:07:33 +00:00
m.ticker = time.NewTicker(33 * time.Millisecond)
2022-09-04 11:13:45 +00:00
go func() {
for range m.ticker.C {
if m.needRedraw {
m.needRedraw = false
m.Redraw()
2022-09-04 14:26:32 +00:00
if m.w.proxy.Client != nil {
if err := m.w.proxy.Client.WritePacket(&packet.ClientBoundMapItemData{
2023-01-29 21:20:13 +00:00
MapID: ViewMapID,
2023-02-05 16:41:06 +00:00
Scale: 4,
2022-09-04 11:13:45 +00:00
Width: 128,
Height: 128,
2022-09-04 14:26:32 +00:00
Pixels: utils.Img2rgba(m.img),
2023-01-24 21:41:37 +00:00
UpdateFlags: packet.MapUpdateFlagTexture,
2022-09-04 11:13:45 +00:00
}); err != nil {
logrus.Error(err)
return
}
}
}
}
}()
go func() { // send map item
t := time.NewTicker(1 * time.Second)
for range t.C {
if m.w.ctx.Err() != nil {
return
}
if m.w.proxy.Client != nil {
2023-01-29 21:20:13 +00:00
err := m.w.proxy.Client.WritePacket(&MapItemPacket)
if err != nil {
logrus.Error(err)
return
}
}
}
}()
2022-09-04 11:13:45 +00:00
}
func (m *MapUI) Stop() {
if m.ticker != nil {
m.ticker.Stop()
2022-08-12 16:54:29 +00:00
}
}
2022-08-17 18:04:13 +00:00
// Reset resets the map to inital state
2022-08-12 16:54:29 +00:00
func (m *MapUI) Reset() {
2022-09-05 19:08:57 +00:00
m.renderedChunks = make(map[protocol.ChunkPos]*image.RGBA)
m.SchedRedraw()
2022-08-12 16:54:29 +00:00
}
2022-08-17 18:04:13 +00:00
// ChangeZoom adds to the zoom value and goes around to 32 once it hits 128
2022-08-12 16:54:29 +00:00
func (m *MapUI) ChangeZoom() {
2022-09-04 11:13:45 +00:00
m.zoomLevel /= 2
if m.zoomLevel == 0 {
m.zoomLevel = 16
}
2022-08-12 16:54:29 +00:00
m.SchedRedraw()
}
2022-08-17 18:04:13 +00:00
// SchedRedraw tells the map to redraw the next time its sent
2022-08-12 16:54:29 +00:00
func (m *MapUI) SchedRedraw() {
m.needRedraw = true
}
2023-01-29 21:20:13 +00:00
// Redraw draws chunk images to the map image
2022-09-04 11:13:45 +00:00
func (m *MapUI) Redraw() {
2022-09-05 15:40:03 +00:00
for {
2022-09-05 19:08:57 +00:00
r, ok := m.renderQueue.Dequeue().(*RenderElem)
if !ok {
2022-09-05 15:40:03 +00:00
break
}
if r.ch != nil {
2022-09-05 19:08:57 +00:00
m.renderedChunks[r.pos] = Chunk2Img(r.ch)
2022-09-05 15:40:03 +00:00
} else {
2023-01-29 21:20:13 +00:00
m.renderedChunks[r.pos] = black16x16
2022-09-05 15:40:03 +00:00
}
}
2022-08-18 13:08:44 +00:00
middle := protocol.ChunkPos{
2022-09-04 11:13:45 +00:00
int32(m.w.PlayerPos.Position.X()),
int32(m.w.PlayerPos.Position.Z()),
2022-08-18 13:08:44 +00:00
}
2022-09-07 12:07:33 +00:00
// total_width := 32 * math.Ceil(float64(chunks_x)/32)
2023-01-29 21:20:13 +00:00
chunksPerLine := float64(128 / m.zoomLevel)
pxPerBlock := 128 / chunksPerLine / 16 // how many pixels per block
pxSizeChunk := int(math.Floor(pxPerBlock * 16))
2022-07-29 16:12:06 +00:00
2022-08-12 16:54:29 +00:00
for i := 0; i < len(m.img.Pix); i++ { // clear canvas
m.img.Pix[i] = 0
2022-08-11 20:22:39 +00:00
}
2022-07-29 16:12:06 +00:00
2022-09-05 19:08:57 +00:00
for _ch := range m.renderedChunks {
2023-01-29 21:20:13 +00:00
relativeMiddleX := float64(_ch.X()*16 - middle.X())
relativeMiddleZ := float64(_ch.Z()*16 - middle.Z())
px := image.Point{ // bottom left corner of the chunk on the map
X: int(math.Floor(relativeMiddleX*pxPerBlock)) + 64,
Y: int(math.Floor(relativeMiddleZ*pxPerBlock)) + 64,
2022-08-12 02:53:43 +00:00
}
2023-01-29 21:20:13 +00:00
if !m.img.Rect.Intersect(image.Rect(px.X, px.Y, px.X+pxSizeChunk, px.Y+pxSizeChunk)).Empty() {
utils.DrawImgScaledPos(m.img, m.renderedChunks[_ch], px, pxSizeChunk)
2022-08-12 02:53:43 +00:00
}
}
2022-08-18 16:18:16 +00:00
2023-01-29 21:20:13 +00:00
drawFull := false
2022-08-20 12:13:56 +00:00
2023-01-29 21:20:13 +00:00
if drawFull {
2022-09-09 12:58:02 +00:00
img2 := m.ToImage()
2022-08-20 12:13:56 +00:00
buf := bytes.NewBuffer(nil)
bmp.Encode(buf, img2)
os.WriteFile("test.bmp", buf.Bytes(), 0o777)
2022-08-20 12:13:56 +00:00
}
2022-07-29 16:12:06 +00:00
}
2022-09-09 12:58:02 +00:00
func (m *MapUI) ToImage() *image.RGBA {
// get the chunk coord bounds
2023-01-29 21:20:13 +00:00
min, max := m.getBounds()
chunksX := int(max[0] - min[0] + 1) // how many chunk lengths is x coordinate
chunksY := int(max[1] - min[1] + 1)
2022-09-09 12:58:02 +00:00
2023-01-29 21:20:13 +00:00
img2 := image.NewRGBA(image.Rect(0, 0, chunksX*16, chunksY*16))
2022-09-09 12:58:02 +00:00
2023-01-29 21:20:13 +00:00
middleBlockX := chunksX / 2 * 16
middleBlockY := chunksY / 2 * 16
2022-09-09 12:58:02 +00:00
for pos := range m.renderedChunks {
2023-01-29 21:20:13 +00:00
px := image.Point{
X: int(pos.X()*16) - middleBlockX + img2.Rect.Dx(),
Y: int(pos.Z()*16) - middleBlockY + img2.Rect.Dy(),
2022-09-09 12:58:02 +00:00
}
draw.Draw(img2, image.Rect(
2023-01-29 21:20:13 +00:00
px.X,
px.Y,
px.X+16,
px.Y+16,
2022-09-09 12:58:02 +00:00
), m.renderedChunks[pos], image.Point{}, draw.Src)
}
return img2
}
2022-08-12 16:54:29 +00:00
func (m *MapUI) SetChunk(pos protocol.ChunkPos, ch *chunk.Chunk) {
2022-09-05 19:08:57 +00:00
m.renderQueue.Enqueue(&RenderElem{pos, ch})
2022-08-12 16:54:29 +00:00
m.SchedRedraw()
}
2023-02-12 21:22:44 +00:00
func (w *WorldState) ProcessAnimate(pk *packet.Animate) {
if pk.ActionType == packet.AnimateActionSwingArm {
w.ui.ChangeZoom()
w.proxy.SendPopup(locale.Loc("zoom_level", locale.Strmap{"Level": w.ui.zoomLevel}))
}
}
func (w *WorldState) processMapPacketsClient(pk packet.Packet, forward *bool) packet.Packet {
switch pk := pk.(type) {
case *packet.MovePlayer:
w.SetPlayerPos(pk.Position, pk.Pitch, pk.Yaw, pk.HeadYaw)
case *packet.PlayerAuthInput:
w.SetPlayerPos(pk.Position, pk.Pitch, pk.Yaw, pk.HeadYaw)
case *packet.MapInfoRequest:
2023-01-29 21:20:13 +00:00
if pk.MapID == ViewMapID {
w.ui.SchedRedraw()
*forward = false
}
case *packet.Animate:
w.ProcessAnimate(pk)
}
return pk
}