void quadtree_insert(void* element, QuadTreeNode* node){ //... Try to insert particle i at node n in quadtree // ... By construction, each leaf will contain either // ... 1 or 0 particles struct point_t middle_point = {0, 0}; //printf("middle_point: %d\n", middle_point.x); //printf("middle_point: %d\n", middle_point.y); if(quadtree_is_leaf(node)){ if(node->data != NULL){ //just_one particle //add n's four children to the Quadtree node->children = (QuadTreeNode **)malloc(NUM_CHILDREN * sizeof(QuadTreeNode*)); //printf("quadtree_insert: %d\n", ((struct cell_t*)(node->data))->x); //printf("quadtree_insert: %d\n", ((struct cell_t*)(node->data))->y); int i; for(i=0; i < NUM_CHILDREN; i++){ QuadTreeNode* child = init_child(node, i); node->children[i] = child; } //move the particle already in n into the child //in which it lies // struct point_t middle_point = get_middle_point(node->rect); middle_point = get_middle_point(*node->rect); int p = get_position_forward(node, middle_point); //printf("quadtree_insert: %d\n", ((struct cell_t*)(node->data))->x); //printf("quadtree_insert: %d\n", ((struct cell_t*)(node->data))->y); printf("quadtree_insert over leaf node (%d, %d). Recursive call...\n", ((struct cell_t*)(node->data))->x, ((struct cell_t*)(node->data))->y); node->children[p]->data = node->data; node->data = NULL; //let c be child in which particle i lies //QuadInsert(i,c) int q = get_position(element, middle_point); quadtree_insert(element, node->children[q]); }else{ //... n is a leaf //store particle i in node n node->data = element; printf("quadtree_insert over empty leaf. Element (%d, %d) finding its place.\n", ((struct cell_t*)(node->data))->x, ((struct cell_t*)(node->data))->y); //printf("quadtree_insert:ya %d\n", ((struct cell_t*)(node->data))->x); //printf("quadtree_insert:ya %d\n", ((struct cell_t*)(node->data))->y); } }else{ //more than one nodes in the subtree //determine which child c of node n particle i lies in //QuadInsert(i,c) middle_point = get_middle_point(*node->rect); int q = get_position(element, middle_point); quadtree_insert(element, node->children[q]); } }
static void mark_insert(){ int fail = 0; int success = 0; int replace = 0; int n = nodes; int val = 10; quadtree_t *tree = quadtree_new(0, 0, 1000, 1000); double x; double y; start(); while(n--){ x = (double) (rand() % 1000); y = (double) (rand() % 1000); int status = quadtree_insert(tree, x, y, &val); switch(status){ case 0: ++fail; break; case 1: ++success; break; case 2: ++replace; break; } } stop(); printf(" %18s %i\n", "length:", tree->length); printf(" %18s %d\n", "insert success:", success); printf(" %18s %d\n", "insert replace:", replace); printf(" %18s %d\n", "insert fail:", fail); quadtree_free(tree); }
int create_tree_callback(void *arg, int argc, char **argv, char **col) { uint64_t identifier, label; QUADTREE *tree = (QUADTREE *)arg; identifier = strtoul(argv[0], NULL, 10); label = strtoul(argv[1], NULL, 10); assert(quadtree_insert(tree, identifier, label)); return 0; }
QuadTree* construct_quadtree(void** elements, int num_elements, Rectangle* rect){ QuadTree* tree = (QuadTree*)malloc(sizeof(QuadTree)); tree->root = (QuadTreeNode*)malloc(sizeof(QuadTreeNode)); int i; tree->root->data = (void*) elements[0]; tree->root->rect = rect; for(i=1; i < MAXCELLS; i++){ quadtree_insert(elements[i], quadtree_root(tree)); } return construct_adaptive_quadtree(tree); }
static int lua_addmonster(lua_State *l) { int ret = -1; monster mon; LOG_DEBUG(mycat, "%s", __FUNC__); if (!lua_isnumber(l, -1)) goto done; mon.base.type = lua_tonumber(l, -1); if (!lua_isnumber(l, -2)) goto done; mon.born_x = lua_tonumber(l, -2); if (!lua_isnumber(l, -3)) goto done; mon.born_y = lua_tonumber(l, -3); mon.observered = NETRUE; //todo check valid mon.base.pos_x = mon.born_x; mon.base.pos_y = mon.born_y; quadbox_t box = {mon.base.pos_x, mon.base.pos_y, mon.base.pos_x, mon.base.pos_y}; //todo HP should load from db mon.base.maxHP = mon.base.curHP = 100; if (mon.base.type & 0xf) mon.base.type = MONSTER_1; mon.base.id = monster_index++; ne_mutex_lock(&map_lock); ne_wrlock(&monster_lock); memcpy(&monsters[monster_count], &mon, sizeof(mon)); if (quadtree_insert(mapobj_tree, &monsters[monster_count].quad_lst, &box) == NULL) { LOG_ERROR(mycat, "%s %d: quadtree_insert fail, err[%s]", __FUNC__, __LINE__, ne_last_error()); ne_rwunlock(&monster_lock); ne_mutex_unlock(&map_lock); return (-1); } ++monster_count; ne_rwunlock(&monster_lock); ne_mutex_unlock(&map_lock); ret = 0; done: lua_pushnumber(l, ret); return (1); }
static NEINT32 user_change_map(NEUINT16 sessionid, ne_usermsgbuf_t *msg , ne_handle listener) { ne_usermsgbuf_t buf; user_action_t *ack = (user_action_t *)buf.data; brd_torange_info_t *broadcast_data = (brd_torange_info_t *)(buf.data + sizeof(user_action_t)); login_ack_t *data = (login_ack_t *)msg->data; user_info_t *user = NULL; // ne_usermsgbuf_t buf = NE_USERMSG_INITILIZER; NEINT32 ret = -1; //long key; assert(NE_USERMSG_MAXID(msg) == MAXID_SERVER_SERVER); assert(NE_USERMSG_MINID(msg) == MSG_USER_CHG_MAP); assert(NE_USERMSG_DATALEN(msg) == sizeof(login_ack_t)); user = malloc(sizeof(user_info_t)); if (!user) { LOG_ERROR(mycat, "%s %d: malloc fail, err[%s]", __FUNC__, __LINE__, ne_last_error()); return (-1); } memcpy(user, &data->user_info, sizeof(user_info_t)); quadbox_t box = {user->base.pos_x, user->base.pos_y, user->base.pos_x, user->base.pos_y}; LOG_DEBUG(mycat, "%s %d: user[%s] changemap id[%d] x[%lf] y[%lf]", __FUNC__, __LINE__, user->base.rolename, user->id, user->base.pos_x, user->base.pos_y); ne_mutex_lock(&map_lock); if (quadtree_insert(mapobj_tree, &user->quad_lst, &box) == NULL) { LOG_ERROR(mycat, "%s %d: quadtree_insert fail, err[%s]", __FUNC__, __LINE__, ne_last_error()); free(user); ne_mutex_unlock(&map_lock); return (-1); } user->quad_head = user->quad_lst.prev; ne_mutex_unlock(&map_lock); //key = calc_hashnr(user->rolename, strlen(user->rolename)); ne_wrlock(&name_map_lock); username_map = rb_insert_v2(sizeof(user->base.rolename), user->base.rolename, username_map); ne_rwunlock(&name_map_lock); ne_wrlock(&id_map_lock); userid_map = rb_insert(user->id, user, userid_map); ne_rwunlock(&id_map_lock); NE_USERMSG_MAXID(msg) = MAXID_SERVER_SERVER ; NE_USERMSG_MINID(msg) = MSG_USER_CHG_MAP_RET ; NE_USERMSG_PARAM(msg) = user->id; NE_USERMSG_LEN(msg) = NE_USERMSG_HDRLEN; ret = NE_SENDEX(sessionid, (ne_usermsghdr_t *)msg, ESF_URGENCY, listener); broadcast_data->box._xmin = user->base.pos_x - SCENE_LENGTH; broadcast_data->box._ymin = user->base.pos_y - SCENE_LENGTH; broadcast_data->box._xmax = user->base.pos_x + SCENE_LENGTH; broadcast_data->box._ymax = user->base.pos_y + SCENE_LENGTH; broadcast_data->real_maxid = MAXID_USER_SERVER; broadcast_data->real_minid = MSG_USER_ACTION_ACK; NE_USERMSG_MAXID(&buf) = MAXID_SERVER_SERVER; NE_USERMSG_MINID(&buf) = MSG_BROADCAST_TORANGE; NE_USERMSG_PARAM(&buf) = sizeof(user_action_t); NE_USERMSG_LEN(&buf) = NE_USERMSG_HDRLEN + sizeof(user_action_t) + sizeof(brd_torange_info_t); NE_USERMSG_ENCRYPT(&buf) = 0; memcpy(ack->rolename, user->base.rolename, NORMAL_SHORT_STR_LEN); ack->action.act = ACT_STANDBY; ack->param = 0; ack->x = user->base.pos_x; ack->y = user->base.pos_y; send_to_all_gateserver((ne_usermsghdr_t *)&buf, listener); return (0); }