static inline tick_super_object(struct map *m,struct aoi_object *o) { uint32_t now = GetCurrentMs(); if(now - o->last_update_tick >= UPDATE_INTERVAL) { //remove out of view object first uint32_t i = 0; for( ; i < MAX_BITS; ++i) { if(o->self_view_objs.bits[i] > 0) { uint32_t j = 0; for( ; j < sizeof(uint32_t); ++j) { if(o->self_view_objs.bits[i] & (1 << j)) { uint32_t aoi_object_id = i*sizeof(uint32_t) + j; if(aoi_object_id != o->aoi_object_id) { struct aoi_object *other = m->all_aoi_objects[aoi_object_id]; if(other->is_leave_map) leave_me(m,o,other); else { uint64_t distance = cal_distance_2D(&o->current_pos,&other->current_pos); if(distance > o->view_radius) leave_me(m,o,other); } } } } } } //process enter view uint32_t x1,y1,x2,y2; cal_blocks(m,&o->current_pos,o->view_radius,&x1,&y1,&x2,&y2); uint32_t y = y1; uint32_t x; for( ; y <= y2; ++y) { for( x=x1; x <= x2; ++x) { struct map_block *bl = get_block(m,y,x); struct aoi_object *cur = (struct aoi_object*)bl->aoi_objs.head.next; while(cur != (struct aoi_object*)&bl->aoi_objs.tail) { if(is_set(&o->self_view_objs,cur->aoi_object_id) == 0) { uint64_t distance = cal_distance_2D(&o->current_pos,&cur->current_pos); if(o->view_radius >= distance) enter_me(m,o,cur); } cur = (struct aoi_object *)cur->block_node.next; } } } o->last_update_tick = now; } }
static inline block_process_enter(struct map *m,struct map_block *bl,struct aoi_object *o) { struct aoi_object *cur = (struct aoi_object*)bl->aoi_objs.head.next; while(cur != (struct aoi_object*)&bl->aoi_objs.tail) { uint64_t distance = cal_distance_2D(&o->current_pos,&cur->current_pos); if(o->view_radius >= distance) enter_me(m,o,cur); if(o!= cur && cur->view_radius >= distance) enter_me(m,cur,o); cur = (struct aoi_object *)cur->block_node.next; } }
static inline block_process_leave(struct map *m,struct map_block *bl,struct aoi_object *o,uint8_t isleave_map) { struct aoi_object *cur = (struct aoi_object*)bl->aoi_objs.head.next; while(cur != (struct aoi_object*)&bl->aoi_objs.tail) { if(isleave_map) { if(is_set(&cur->self_view_objs,o->aoi_object_id)) leave_me(m,cur,o); } else { uint64_t distance = cal_distance_2D(&o->current_pos,&cur->current_pos); if(is_set(&o->self_view_objs,cur->aoi_object_id)) leave_me(m,o,cur); if(o != cur && distance > cur->view_radius && is_set(&cur->self_view_objs,o->aoi_object_id)) leave_me(m,cur,o); } cur = (struct aoi_object *)cur->block_node.next; } }
//将o移动到new_pos,并计算视野变化 void move_to(struct map *m,struct aoi_object *o,struct point2D *new_pos) { struct point2D old_pos = o->current_pos; o->current_pos = *new_pos; struct map_block *old_block = get_block_by_point(m,&old_pos); struct map_block *new_block = get_block_by_point(m,new_pos); if(old_block != new_block) double_link_remove(&o->block_node); uint32_t radius = STAND_RADIUS; if(o->view_radius > STAND_RADIUS) radius = o->view_radius; //计算新旧管理区域 uint32_t n_x1,n_y1,n_x2,n_y2; uint32_t o_x1,o_y1,o_x2,o_y2; cal_blocks(m,&old_pos,radius,&o_x1,&o_y1,&o_x2,&o_y2); cal_blocks(m,new_pos,radius,&n_x1,&n_y1,&n_x2,&n_y2); uint32_t y = n_y1; uint32_t x; for( ; y <= n_y2; ++y) { for( x = n_x1; x <= n_x2; ++x) { if(x >= o_x1 && x <= o_x2 && y >= o_y1 && y <= o_y2) { //无变化区域 struct map_block *bl = get_block(m,y,x); struct aoi_object *cur = (struct aoi_object*)bl->aoi_objs.head.next; while(cur != (struct aoi_object*)&bl->aoi_objs.tail) { uint64_t distance = cal_distance_2D(new_pos,&cur->current_pos); if(o != cur) { if(o->view_radius >= distance && !is_set(&o->self_view_objs,cur->aoi_object_id)) enter_me(m,o,cur); if(o->view_radius < distance && is_set(&o->self_view_objs,cur->aoi_object_id)) leave_me(m,o,cur); if(cur->view_radius >= distance && !is_set(&cur->self_view_objs,o->aoi_object_id)) enter_me(m,cur,o); if(cur->view_radius < distance && is_set(&cur->self_view_objs,o->aoi_object_id)) leave_me(m,cur,o); } cur = (struct aoi_object *)cur->block_node.next; } } else { //新进入的区域 block_process_enter(m,get_block(m,y,x),o); } } } if(old_block != new_block) double_link_push(&new_block->aoi_objs,&o->block_node); y = o_y1; for( ; y <= o_y2; ++y) { for(x = o_x1; x <= o_x2; ++x) { if(x >= n_x1 && x <= n_x2 && y >= n_y1 && y <= n_y2) continue;//这里不处理无变化区域 block_process_leave(m,get_block(m,y,x),o,0); } } o->last_update_tick = GetCurrentMs(); }
//��o�ƶ���new_pos,��������Ұ�仯 void move_to(struct map *m,struct aoi_object *o,int32_t _x,int32_t _y) { struct point2D new_pos = {_x,_y}; struct point2D old_pos = o->current_pos; o->current_pos = new_pos; struct map_block *old_block = get_block_by_point(m,&old_pos); struct map_block *new_block = get_block_by_point(m,&new_pos); if(old_block != new_block) dlist_remove(&o->block_node); uint32_t radius = STAND_RADIUS; if(o->view_radius > STAND_RADIUS) radius = o->view_radius; //�����¾ɹ������� uint32_t n_x1,n_y1,n_x2,n_y2; uint32_t o_x1,o_y1,o_x2,o_y2; n_x1 = n_y1 = n_x2 = n_y2 = 0; o_x1 = o_y1 = o_x2 = o_y2 = 0; cal_blocks(m,&old_pos,radius,&o_x1,&o_y1,&o_x2,&o_y2); cal_blocks(m,&new_pos,radius,&n_x1,&n_y1,&n_x2,&n_y2); uint32_t y = n_y1; uint32_t x = 0; for( ; y <= n_y2; ++y) { for( x = n_x1; x <= n_x2; ++x) { if(x >= o_x1 && x <= o_x2 && y >= o_y1 && y <= o_y2) { //�ޱ仯���� struct map_block *bl = get_block(m,y,x); struct aoi_object *cur = (struct aoi_object*)bl->aoi_objs.head.next; while(cur != (struct aoi_object*)&bl->aoi_objs.tail) { uint64_t distance = cal_distance_2D(&new_pos,&cur->current_pos); if(o != cur) { if(o->view_radius >= distance && !is_set(&o->self_view_objs,cur->aoi_object_id)) enter_me(m,o,cur); else if(o->view_radius < distance && is_set(&o->self_view_objs,cur->aoi_object_id)) leave_me(m,o,cur); if(cur->view_radius >= distance && !is_set(&cur->self_view_objs,o->aoi_object_id)) enter_me(m,cur,o); else if(cur->view_radius < distance && is_set(&cur->self_view_objs,o->aoi_object_id)) leave_me(m,cur,o); } cur = (struct aoi_object *)cur->block_node.next; } } else { //�½�������� block_process_enter(m,get_block(m,y,x),o); } } } if(old_block != new_block) dlist_push(&new_block->aoi_objs,&o->block_node); y = o_y1; for( ; y <= o_y2; ++y) { for(x = o_x1; x <= o_x2; ++x) { if(x >= n_x1 && x <= n_x2 && y >= n_y1 && y <= n_y2) continue;//���ﲻ�����ޱ仯���� block_process_leave(m,get_block(m,y,x),o,0); } } o->last_update_tick = GetSystemMs64(); }