static NEINT32 user_scene_req(NEUINT16 sessionid, ne_usermsgbuf_t *msg , ne_handle listener) { NEUINT32 id; user_info_t *user_info = NULL; monster *mon = NULL; rb_node_t *usernode; struct list_head *retlist[MAX_QUAD_SEARCH]; struct list_head *pos; ne_usermsgbuf_t buf, buf_monster;// = NE_USERMSG_INITILIZER; user_scene_ack_t *ack = (user_scene_ack_t *)buf.data; monster_scene_ack_t *ack_monster = (monster_scene_ack_t *)buf_monster.data; user_scene_req_t *req = (user_scene_req_t *)msg->data; NEINT32 ret; NEINT32 index; NEINT32 i; assert(NE_USERMSG_MAXID(msg) == MAXID_SERVER_SERVER); assert(NE_USERMSG_MINID(msg) == MSG_USER_SCENE_REQ); id = NE_USERMSG_PARAM(msg); ne_rdlock(&id_map_lock); usernode = rb_search(id, userid_map); if (!usernode || !usernode->data) { ne_rwunlock(&id_map_lock); LOG_ERROR(mycat, "%s %d: can not find usernode[%d]", __FUNC__, __LINE__, id); return (-1); } user_info = usernode->data; ne_rwunlock(&id_map_lock); user_info->rollcall = 0; quadbox_t box = {user_info->base.pos_x - 100, user_info->base.pos_y - 100, user_info->base.pos_x + 100, user_info->base.pos_y + 100}; index = 0; ne_mutex_lock(&map_lock); ne_rdlock(&monster_lock); quadtree_search(mapobj_tree, &box, retlist, &index, MAX_QUAD_SEARCH); ack->num = 0; for (i = 0; i < index; ++i) { list_for_each(pos, retlist[i]) { user_info = list_entry(pos, user_info_t, quad_lst); if (user_info->base.type & 0xf) { memcpy(&ack->player[ack->num], &user_info->base, sizeof(player_base_t)); ++ack->num; } else { mon = list_entry(pos, monster, quad_lst); memcpy(&ack_monster->monster[ack->num], &mon->base, sizeof(monster_base_t)); mon->observered = NETRUE; ++ack_monster->num; } } }
int quadtree_search (Node *key, Node *root, Node ** key_hit) { if (!root) return 0; if (root->tl.x == key->tl.x && root->tl.y == key->tl.y && root->br.x == key->br.x && root->br.y == key->br.y) { *key_hit = root; return 1; } else if (quadtree_search (key, root->one, key_hit)) return 1; else if (quadtree_search (key, root->two, key_hit)) return 1; else if (quadtree_search (key, root->three,key_hit)) return 1; else if (quadtree_search (key, root->four, key_hit)) return 1; return 0; }