Ejemplo n.º 1
0
int32_t enter_map(struct map *m,struct aoi_object *o)
{
	struct map_block *block = get_block_by_point(m,&o->current_pos);
	if(!block)
		return -1;
	double_link_push(&block->aoi_objs,&o->block_node);
	m->all_aoi_objects[o->aoi_object_id] = o;
	uint32_t radius = STAND_RADIUS;	
	if(o->view_radius > STAND_RADIUS)
	{
		radius = o->view_radius;
		double_link_push(&m->super_aoi_objs,&o->super_node);
	}
	uint32_t x1,y1,x2,y2;
	cal_blocks(m,&o->current_pos,radius,&x1,&y1,&x2,&y2);
	uint32_t y = y1;
	uint32_t x;
	for( ; y <= y2; ++y)
	{
		for(x=x1 ; x <= x2; ++x)
		{
			block_process_enter(m,get_block(m,y,x),o);
		}		
	}
	o->last_update_tick = GetCurrentMs();
	o->is_leave_map = 0;
	
}
Ejemplo n.º 2
0
void on_write_active(socket_t s)
{
	s->writeable = 1;
	if(!s->isactived && !LINK_LIST_IS_EMPTY(s->pending_send))
	{
		s->isactived = 1;
		double_link_push(s->engine->actived,(struct double_link_node*)s);
	}
}
Ejemplo n.º 3
0
int32_t sche_spawn(sche_t s,void*(*fun)(void*),void*arg)
{
	if(s->coro_size > s->max_coro)
		return -1;
	coro_t co = coro_create(s,s->stack_size,coro_fun);
	co->arg = arg;
	co->fun = fun;
	++s->coro_size;
	double_link_push(&s->coros,&co->dblink);
	uthread_switch(s->co->ut,co->ut,co);
	return 0;
}
Ejemplo n.º 4
0
int32_t epoll_loop(engine_t n,int32_t ms)
{
	assert(n);	
	uint32_t sleep_ms;
	uint32_t timeout = GetSystemMs() + ms;
	uint32_t current_tick;
	do{		
		while(!double_link_empty(n->actived))
		{
			socket_t s = (socket_t)double_link_pop(n->actived);
			s->isactived = 0;
			if(Process(s))
			{
				s->isactived = 1;
				double_link_push(n->actived,(struct double_link_node*)s);
			}
			//if(GetCurrentMs() >= timeout)
			//	break;
		}
		
		current_tick = GetSystemMs();
		sleep_ms = timeout > current_tick ? timeout - current_tick:0;	
		int32_t nfds = TEMP_FAILURE_RETRY(epoll_wait(n->poller_fd,n->events,MAX_SOCKET,sleep_ms));
		if(nfds < 0)
			return -1;
		int32_t i;
		for(i = 0 ; i < nfds ; ++i)
		{	
			socket_t sock = (socket_t)n->events[i].data.ptr;
			if(sock)
			{
				if(n->events[i].events & EPOLLIN)
					on_read_active(sock);
				if(n->events[i].events & EPOLLOUT)
					on_write_active(sock);	
				if(n->events[i].events & EPOLLERR)
					on_read_active(sock);
			}
		}	
		current_tick = GetSystemMs();
	}while(timeout > current_tick);
	return 0;
}
Ejemplo n.º 5
0
static inline mq_sync_pop(mq_t m,struct per_thread_struct *pts,uint32_t timeout)
{
	mutex_lock(m->mtx);
	if(link_list_is_empty(m->share_list))
	{
		if(timeout)
		{	
			while(link_list_is_empty(m->share_list))
			{
				double_link_push(&m->blocks,&pts->block);
				if(0 != condition_timedwait(pts->cond,m->mtx,timeout))
				{
					double_link_remove(&pts->block);
					break;
				}
			}
		}
	}
	link_list_swap(pts->local_pop_q,m->share_list);
	mutex_unlock(m->mtx);
}
Ejemplo n.º 6
0
//将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();
}