Beispiel #1
0
func TestChunksUpdate(t *testing.T) {
	tick := message.Tick(42)

	blk1 := message.NewBlock()
	for i := 0; i < 10; i++ {
		blk1.Points[10][5+i] = 1
	}
	for i := 0; i < 7; i++ {
		blk1.Points[20+i][10] = 2
	}

	blk2 := message.NewBlock()
	blk2.Fill(1)
	blk2.Points[10][5] = 0
	blk2.Points[7][21] = 2

	blks := []*message.Block{blk1, blk2}

	testMessage(t, message.Types["chunks_update"], func(w io.Writer) error {
		return builder.SendChunksUpdate(w, tick, blks)
	}, func(conn *message.Conn, t *testing.T) {
		rt, rblks := handler.ReadChunksUpdate(conn)
		if rt != tick {
			t.Fatal("Sent tick", tick, "but received", rt)
		}

		if len(rblks) != len(blks) {
			t.Fatal("Sent", len(blks), "blocks, but received", len(rblks))
		}
		for k, blk := range blks {
			rblk := rblks[k]
			for i := range blk.Points {
				for j := range blk.Points[i] {
					pt := blk.Points[i][j]
					rpt := rblk.Points[i][j]
					if rpt != pt {
						t.Fatal("Sent point", pt, " at ", i, j, "in block", k, "but received", rpt)
					}
				}
			}
		}
	})
}
Beispiel #2
0
		pos := session.Entity.Position
		radius := message.BlockCoord(8)
		blks := []*message.Block{}
		for i := pos.BX - radius; i <= pos.BX+radius; i++ {
			for j := pos.BY - radius; j <= pos.BY+radius; j++ {
				blk, err := ctx.Terrain.GetBlockAt(i, j)
				if err != nil {
					log.Println("Cannot send initial terrain to client", err)
					continue
				}

				blks = append(blks, blk)
			}
		}
		err = conn.Write(func(w io.Writer) error {
			return builder.SendChunksUpdate(w, ctx.Clock.GetRelativeTick(), blks)
		})
		if err != nil {
			return err
		}

		// Send initial entities
		log.Println("Sending initial entities")
		for _, e := range ctx.Entity.List() {
			if e.Id == session.Entity.Id {
				continue
			}

			err := conn.Write(func(w io.Writer) error {
				return builder.SendEntityCreate(w, ctx.Clock.GetRelativeTick(), e)
			})