fix bitset bugs

This commit is contained in:
Tnze
2022-05-28 01:48:36 +08:00
parent 29ce03be31
commit 02dd436014
6 changed files with 30 additions and 25 deletions

View File

@ -38,7 +38,7 @@ func (c *ClientInformation) Init(g *server.Game) {
return
}
}
}), "ClientInfoSystem", nil)
}), "go-mc:ClientInfoSystem", nil)
g.AddHandler(&server.PacketHandler{
ID: packetid.ServerboundClientInformation,
F: func(client *server.Client, player *server.Player, p server.Packet758) error {

View File

@ -59,9 +59,13 @@ func (b *BitSet) And(other *BitSet) *BitSet {
}
func (b *BitSet) AndNot(other BitSet) *BitSet {
result := BitSet{values: make([]uint, max(len(b.values), len(other.values)))}
result := BitSet{values: make([]uint, len(b.values))}
for i := range b.values {
if i < len(other.values) {
result.values[i] = b.values[i] & ^other.values[i]
} else {
result.values[i] = b.values[i]
}
}
return &result
}

View File

@ -45,15 +45,12 @@ func (NullStorage[T]) SetValue(eid Index, v T) {}
func (NullStorage[T]) DelValue(eid Index) {}
type MaskedStorage[T any] struct {
BitSetLike
BitSet
Storage[T]
Len int
}
func (m *MaskedStorage[T]) Init() {
if m.BitSetLike == nil {
m.BitSetLike = BitSet{make(map[Index]struct{})}
}
m.Storage.Init()
}
func (m *MaskedStorage[T]) GetValue(eid Index) *T {
@ -64,14 +61,14 @@ func (m *MaskedStorage[T]) GetValue(eid Index) *T {
}
func (m *MaskedStorage[T]) GetValueAny(eid Index) any { return m.GetValue(eid) }
func (m *MaskedStorage[T]) SetValue(eid Index, v T) {
if !m.BitSetLike.Set(eid) {
if !m.BitSet.Set(eid) {
m.Len++
}
m.Storage.SetValue(eid, v)
}
func (m *MaskedStorage[T]) SetAny(eid Index, v any) { m.SetValue(eid, v.(T)) }
func (m *MaskedStorage[T]) DelValue(eid Index) {
if m.BitSetLike.Unset(eid) {
if m.BitSet.Unset(eid) {
m.Len--
}
m.Storage.DelValue(eid)

View File

@ -14,8 +14,9 @@ type funcsystem struct {
func FuncSystem(F any) System {
type Storage interface {
BitSetLike
GetValueAny(eid Index) any
And(*BitSet) *BitSet
Range(f func(eid Index))
}
f := reflect.ValueOf(F)
in := f.Type().NumIn()
@ -37,11 +38,13 @@ func FuncSystem(F any) System {
}
args := make([]reflect.Value, len(storages))
if len(storages) > 0 {
set := BitSetLike(storages[0])
set := reflect.ValueOf(storages[0]).FieldByName("BitSet").Addr()
for _, v := range storages[1:] {
set = set.And(v)
p := reflect.ValueOf(v).FieldByName("BitSet").Addr()
set = set.MethodByName("And").Call([]reflect.Value{p})[0]
}
set.Range(func(eid Index) {
set.MethodByName("Range").Call([]reflect.Value{
reflect.ValueOf(func(eid Index) {
for i := range args {
arg := storages[i].GetValueAny(eid)
if arg == nil {
@ -53,6 +56,7 @@ func FuncSystem(F any) System {
}
}
f.Call(args)
}),
})
} else {
f.Call(args)

View File

@ -23,7 +23,7 @@ func (p playerSpawnSystem) Update(w *ecs.World) {
pos, rot := ecs.GetComponent[server.Pos](w), ecs.GetComponent[server.Rot](w)
profiles := ecs.GetComponent[PlayerProfile](w)
dimensionRes := ecs.GetResource[world.DimensionList](w)
players.AndNot(profiles.BitSetLike).Range(func(eid ecs.Index) {
players.AndNot(profiles.BitSet).Range(func(eid ecs.Index) {
player := players.GetValue(eid)
client := clients.GetValue(eid)
profile, err := p.GetPlayer(player.UUID)

View File

@ -2,12 +2,12 @@ package world
import (
_ "embed"
"github.com/Tnze/go-mc/server"
"io"
"unsafe"
"github.com/Tnze/go-mc/nbt"
pk "github.com/Tnze/go-mc/net/packet"
"github.com/Tnze/go-mc/server"
"github.com/Tnze/go-mc/server/ecs"
)