Exemplo n.º 1
0
func DoTestQuadtree_WLq() {
	t := quadtree.MakeQuadtree(twof.TwoF{0.0, 0.0}, twof.TwoF{2.0, 2.0}, 1)
	var objList []*testObj
	const (
		testNumber = 20
	)
	for i := 0; i < testNumber; i++ {
		objList = append(objList, &testObj{float64(i) / 10, 0.1})
	}
	for _, o := range objList {
		// fmt.Printf("Add object %p %v\n", o, o)
		t.Add_WLq(o, o.GetPreviousPos())
	}
	near := t.FindNearObjects_RLq(&twof.TwoF{0.4, 0.1}, 0.201) // Find 0.2, 0.3, 0.4, 0.5 and 0.6
	DoTestCheck("FindNearObjects", len(near) == 5)
	// fmt.Printf("Tree after adding:\n%v\n", t)
	for _, o := range objList {
		// fmt.Printf("Remove object %p %v\n", o, o)
		t.Remove_WLq(o)
	}
	// fmt.Printf("Tree after removing again\n%v\n", t)
	DoTestCheck("Quadtree empty", t.Empty())
}
Exemplo n.º 2
0
}

var (
	allPlayersSem    sync.RWMutex             // Used to synchronize access to all data structures in this var group
	allPlayers       [MAX_PLAYERS]*user       // This array contains all players
	lastPlayerSlot   int                      // The last slot in use in allPlayers
	numPlayers       int                      // The total number of players currently
	allPlayerNameMap = make(map[string]*user) // Map from player name to user
	allPlayerIdMap   = make(map[uint32]*user) // Map from player id to user
)

var (
	// All players and monsters are stored in an Quadtree, to make it quick to find distances.
	lowerLeftNearCorner = TwoF{-QuadtreeInitSize, -QuadtreeInitSize}
	upperLeftFarCorner  = TwoF{QuadtreeInitSize, QuadtreeInitSize}
	playerQuadtree      = quadtree.MakeQuadtree(lowerLeftNearCorner, upperLeftFarCorner, 1)
)

// A new connection
func NewClientConnection_WLa(conn net.Conn) (ok bool, index int) {
	allPlayersSem.Lock()
	defer allPlayersSem.Unlock()
	// Find an empty slot for the new player. TODO: More efficient algorithm could be used.
	var i int
	for i = 0; i < MAX_PLAYERS; i++ {
		if allPlayers[i] == nil {
			break
		}
	}
	if i == MAX_PLAYERS {
		// TODO: Handle the case with too many players