Beispiel #1
0
void joynet_connect_to_game(JOYNET_GAME * gp, short controller, short player)
{
	char data[4] = {0};
	ENetPacket * pp;
	int assigned_player = player;
	int i;
	
	if(gp->client)
	{
		joynet_serialize(gp->client->serial_data, data);
		joynet_putw(gp->client->serial_data, controller);
		joynet_putw(gp->client->serial_data, player);
		pp = joynet_create_packet(JOYNET_GAME_MESSAGE_CONNECT, gp->client->serial_data);
		enet_peer_send(gp->client->peer, JOYNET_CHANNEL_GAME, pp);
	}
	else
	{
		if(player < 0)
		{
			for(i = 0; i < gp->players; i++)
			{
				if(!gp->player[i]->controller)
				{
					assigned_player = i;
					break;
				}
			}
		}
		if(gp->controller[controller]->port < 0 && !gp->player[assigned_player]->controller)
		{
			gp->player[assigned_player]->controller = malloc(sizeof(JOYNET_CONTROLLER));
			if(gp->player[assigned_player]->controller)
			{
				gp->player[assigned_player]->controller->port = assigned_player;
				gp->player[assigned_player]->local = 1;
				gp->player_count++;
				gp->controller[controller]->port = assigned_player;
				
				/* reset selections to prevent bugs */
				memset(gp->player[assigned_player]->selected_content, 0, sizeof(unsigned long) * JOYNET_GAME_MAX_CONTENT_LISTS);
				memset(gp->player[assigned_player]->selected_content_index, 0, sizeof(int) * JOYNET_GAME_MAX_CONTENT_LISTS);			
			}
			if(gp->callback)
			{
				JOYNET_MESSAGE message;
				
				message.type = JOYNET_GAME_MESSAGE_CONNECT;
				joynet_serialize(gp->serial_data, data);
				joynet_putw(gp->serial_data, controller);
				joynet_putw(gp->serial_data, assigned_player);
				message.data = data;
				message.data_size = 4;
				message.event = NULL;
				gp->callback(&message);
			}
		}
	}
}
Beispiel #2
0
void joynet_handle_server_chat_message(JOYNET_SERVER * sp, JOYNET_MESSAGE * mp)
{
	switch(mp->type)
	{
		case JOYNET_CHAT_MESSAGE_SET_NAME:
		{
			int client = joynet_get_client_from_peer(sp, mp->event->peer);
			ENetPacket * packet;
			
			/* send SET_NAME message back to client to finalize the change */
			strcpy(sp->client[client]->screen_name, mp->data);
			packet = joynet_build_packet(JOYNET_CHAT_MESSAGE_SET_NAME, mp->data, strlen(mp->data) + 1);
			enet_peer_send(sp->client[client]->peer, JOYNET_CHANNEL_CHAT, packet);
			break;
		}
		case JOYNET_CHAT_MESSAGE_SET_GROUP:
		{
			int client = joynet_get_client_from_peer(sp, mp->event->peer);
			ENetPacket * packet;
			
			joynet_serialize(sp->serial_data, mp->data);
			joynet_getw(sp->serial_data, &sp->client[client]->group);
			
			/* send SET_GROUP message back to client to finalize the change */
			packet = joynet_build_packet(JOYNET_CHAT_MESSAGE_SET_GROUP, mp->data, sizeof(short));
			enet_peer_send(sp->client[client]->peer, JOYNET_CHANNEL_CHAT, packet);
			break;
		}
		case JOYNET_CHAT_MESSAGE_TALK:
		case JOYNET_CHAT_MESSAGE_GROUP_TALK:
		{
			int client = joynet_get_client_from_peer(sp, mp->event->peer);
			char data[1024] = {0};
			short name_length = strlen(sp->client[client]->screen_name) + 1;
			short message_length = strlen(mp->data) + 1;
			ENetPacket * packet;
			int i;

			/* TALK message is different for client, contains user name and message */
			joynet_serialize(sp->serial_data, data);
			joynet_putw(sp->serial_data, name_length);
			joynet_putw(sp->serial_data, message_length);
			joynet_write(sp->serial_data, sp->client[client]->screen_name, name_length);
			joynet_write(sp->serial_data, mp->data, message_length);
			packet = joynet_create_packet(JOYNET_CHAT_MESSAGE_TALK, sp->serial_data);
			for(i = 0; i < sp->max_clients; i++)
			{
				if((sp->client[i]->peer) && ((mp->type == JOYNET_CHAT_MESSAGE_GROUP_TALK) || (sp->client[i]->group == sp->client[client]->group)))
				{
					enet_peer_send(sp->client[i]->peer, JOYNET_CHANNEL_CHAT, packet);
				}
			}
			break;
		}
	}
}
Beispiel #3
0
void joynet_disconnect_from_game(JOYNET_GAME * gp, short controller, short player)
{
	char data[4] = {0};
	ENetPacket * pp;
	
	if(gp->client)
	{
		joynet_serialize(gp->client->serial_data, data);
		joynet_putw(gp->client->serial_data, controller);
		if(player < 0)
		{
			joynet_putw(gp->client->serial_data, gp->controller[controller]->port);
		}
		else
		{
			joynet_putw(gp->client->serial_data, player);
		}
		pp = joynet_create_packet(JOYNET_GAME_MESSAGE_DISCONNECT, gp->client->serial_data);
		enet_peer_send(gp->client->peer, JOYNET_CHANNEL_GAME, pp);
	}
	else
	{
		JOYNET_MESSAGE message;
		
		free(gp->player[player]->controller);
		gp->player[player]->controller = NULL;
		gp->controller[controller]->port = -1;
		gp->player_count--;
		if(gp->callback)
		{
			
			/* disconnect player */
			joynet_serialize(gp->serial_data, data);
			joynet_putw(gp->serial_data, controller);
			joynet_putw(gp->serial_data, player);
			message.type = JOYNET_GAME_MESSAGE_DISCONNECT;
			message.data = data;
			message.data_size = joynet_get_serial_size(gp->serial_data);
			message.event = NULL;
			gp->callback(&message);
			
			/* remove player */
			joynet_serialize(gp->serial_data, data);
			joynet_putw(gp->serial_data, player);
			message.type = JOYNET_GAME_MESSAGE_REMOVE_PLAYER;
			message.data = data;
			message.data_size = joynet_get_serial_size(gp->serial_data);
			message.event = NULL;
			gp->callback(&message);
		}
	}
}
Beispiel #4
0
/* high level functions */
void joynet_update_game_option(JOYNET_GAME * gp, int * option)
{
	char data[1024] = {0};
	ENetPacket * pp;
	int i, c = 0;
	
	if(gp->client && gp->client->master)
	{
		for(i = 0; i < gp->options; i++)
		{
			if(gp->option[i] == option)
			{
				c = i;
				break;
			}
		}
		if(i < gp->options)
		{
			joynet_serialize(gp->client->serial_data, data);
			joynet_putw(gp->client->serial_data, c);
			joynet_putl(gp->client->serial_data, *gp->option[c]);
			pp = joynet_create_packet(JOYNET_GAME_MESSAGE_UPDATE_OPTION, gp->client->serial_data);
			enet_peer_send(gp->client->peer, JOYNET_CHANNEL_GAME, pp);
		}
	}
	else
	{
	}
}
Beispiel #5
0
void joynet_select_player(JOYNET_GAME * gp, int player)
{
	char data[2];
	ENetPacket * pp;
	if(gp->client)
	{
		if(gp->client->master && gp->state == JOYNET_GAME_STATE_PLAYING)
		{
			joynet_serialize(gp->client->serial_data, data);
			joynet_putw(gp->client->serial_data, player);
			pp = joynet_create_packet(JOYNET_GAME_MESSAGE_SELECT_PLAYER, gp->client->serial_data);
			enet_peer_send(gp->client->peer, JOYNET_CHANNEL_GAME, pp);
		}
	}
	else
	{
		JOYNET_MESSAGE message;
		
		if(gp->callback)
		{
			message.type = JOYNET_GAME_MESSAGE_SELECT_PLAYER;
			message.data = NULL;
			message.data_size = 0;
			message.event = NULL;
			gp->callback(&message);
			gp->current_player = player;
		}
	}
}
Beispiel #6
0
int joynet_get_client_from_peer(JOYNET_SERVER * sp, ENetPeer * pp)
{
	long client;
	
	joynet_serialize(sp->serial_data, pp->data);
	joynet_getl(sp->serial_data, &client);
	
	return client;
}
Beispiel #7
0
/* high level functions */
void joynet_update_player_options(JOYNET_GAME * gp, int player)
{
	char data[1024] = {0};
	ENetPacket * pp;
	int i;
	
	if(gp->client && gp->player[player]->local)
	{
		joynet_serialize(gp->client->serial_data, data);
		joynet_putw(gp->client->serial_data, player);
		joynet_putw(gp->client->serial_data, strlen(gp->player[player]->name) + 1);
		joynet_write(gp->client->serial_data, gp->player[player]->name, strlen(gp->player[player]->name) + 1);
		joynet_putw(gp->client->serial_data, gp->player[player]->options);
		for(i = 0; i < gp->player[player]->options; i++)
		{
			joynet_putl(gp->client->serial_data, *gp->player[player]->option[i]);
		}
		pp = joynet_create_packet(JOYNET_GAME_MESSAGE_UPDATE_PLAYER_OPTIONS, gp->client->serial_data);
		enet_peer_send(gp->client->peer, JOYNET_CHANNEL_GAME, pp);
	}
	else
	{
		JOYNET_MESSAGE message;
		if(gp->callback)
		{
			joynet_serialize(gp->serial_data, data);
			joynet_putw(gp->serial_data, player);
			joynet_putw(gp->serial_data, strlen(gp->player[player]->name) + 1);
			joynet_write(gp->serial_data, gp->player[player]->name, strlen(gp->player[player]->name) + 1);
			joynet_putw(gp->serial_data, gp->player[player]->options);
			for(i = 0; i < gp->player[player]->options; i++)
			{
				joynet_putl(gp->serial_data, *gp->player[player]->option[i]);
			}
			message.type = JOYNET_GAME_MESSAGE_UPDATE_PLAYER_OPTIONS;
			message.data = data;
			message.data_size = joynet_get_serial_size(gp->serial_data);
			message.event = NULL;
			gp->callback(&message);
		}
	}
}
Beispiel #8
0
void joynet_select_game_content(JOYNET_GAME * gp, int player, int list, unsigned long hash)
{
	char data[6];
	ENetPacket * pp;
	int i;
	
	if(gp->client)
	{
		joynet_serialize(gp->client->serial_data, data);
		joynet_putw(gp->client->serial_data, player);
		joynet_putw(gp->client->serial_data, list);
		joynet_putl(gp->client->serial_data, hash);
		pp = joynet_create_packet(JOYNET_GAME_MESSAGE_SELECT_PLAYER_CONTENT, gp->client->serial_data);
		enet_peer_send(gp->client->peer, JOYNET_CHANNEL_GAME, pp);
	}
	else
	{
		JOYNET_MESSAGE message;
		
		gp->player[player]->selected_content[list] = hash;
		for(i = 0; i < gp->local_content_list[list]->count; i++)
		{
			if(gp->local_content_list[list]->hash[i] == hash)
			{
				gp->player[player]->selected_content_index[list] = i;
				break;
			}
		}
		if(gp->callback)
		{
			joynet_serialize(gp->serial_data, data);
			joynet_putw(gp->serial_data, player);
			joynet_putw(gp->serial_data, list);
			joynet_putl(gp->serial_data, hash);
			message.type = JOYNET_GAME_MESSAGE_SELECT_PLAYER_CONTENT;
			message.data = data;
			message.data_size = joynet_get_serial_size(gp->serial_data);
			message.event = NULL;
			gp->callback(&message);
		}
	}
}
Beispiel #9
0
void joynet_set_client_group(JOYNET_CLIENT * cp, short group)
{
	char data[2];
	ENetPacket * pp;
	
	if(cp->peer)
	{
		joynet_serialize(cp->serial_data, data);
		joynet_putw(cp->serial_data, group);
		pp = joynet_create_packet(JOYNET_CHAT_MESSAGE_SET_GROUP, cp->serial_data);
		enet_peer_send(cp->peer, JOYNET_CHANNEL_CHAT, pp);
	}
}
Beispiel #10
0
void joynet_handle_client_chat_message(JOYNET_CLIENT * cp, JOYNET_MESSAGE * mp)
{
	switch(mp->type)
	{
		case JOYNET_CHAT_MESSAGE_SET_NAME:
		{
			strcpy(cp->screen_name, mp->data);
			break;
		}
		case JOYNET_CHAT_MESSAGE_SET_GROUP:
		{
			joynet_serialize(cp->serial_data, mp->data);
			joynet_getw(cp->serial_data, &cp->group);
			break;
		}
		case JOYNET_CHAT_MESSAGE_TALK:
		case JOYNET_CHAT_MESSAGE_GROUP_TALK:
		{
			short name_length;
			short message_length;
			char user[128] = {0};
			char message[512] = {0};
			
			/* TALK message is different for client, contains user name and message */
			joynet_serialize(cp->serial_data, mp->data);
			joynet_getw(cp->serial_data, &name_length);
			joynet_getw(cp->serial_data, &message_length);
			joynet_read(cp->serial_data, user, name_length);
			joynet_read(cp->serial_data, message, message_length);
			if(cp->chat_callback)
			{
				cp->chat_callback(user, message);
			}
			break;
		}
	}
}
Beispiel #11
0
int joynet_connect_to_game_server(JOYNET_GAME * gp, JOYNET_CLIENT * cp, char * address, int port)
{
	char data[256];
	ENetPacket * pp;
	
	if(joynet_connect_client_to_server(cp, address, port))
	{
		joynet_serialize(cp->serial_data, data);
		joynet_putw(cp->serial_data, strlen(gp->name) + 1);
		joynet_write(cp->serial_data, gp->name, strlen(gp->name) + 1);
		pp = joynet_create_packet(JOYNET_GAME_MESSAGE_CHECK_ID, cp->serial_data);
		enet_peer_send(cp->peer, JOYNET_CHANNEL_GAME, pp);
		joynet_reset_game(gp);
		joynet_current_game = gp;
		return 1;
	}
	return 0;
}
Beispiel #12
0
void joynet_update_game_options(JOYNET_GAME * gp)
{
	char data[1024] = {0};
	ENetPacket * pp;
	int i;
	
	if(gp->client && gp->client->master)
	{
		joynet_serialize(gp->client->serial_data, data);
		joynet_putw(gp->client->serial_data, gp->options);
		for(i = 0; i < gp->options; i++)
		{
			joynet_putl(gp->client->serial_data, *gp->option[i]);
		}
		pp = joynet_create_packet(JOYNET_GAME_MESSAGE_UPDATE_OPTIONS, gp->client->serial_data);
		enet_peer_send(gp->client->peer, JOYNET_CHANNEL_GAME, pp);
	}
	else
	{
	}
}
Beispiel #13
0
void joynet_connect_to_game(JOYNET_GAME * gp, short controller, short player)
{
	char data[6] = {0};
	ENetPacket * pp;
	int assigned_player = player;
	int i;

	if(gp->client)
	{
		joynet_serialize(gp->client->serial_data, data);
		joynet_putw(gp->client->serial_data, controller);
		joynet_putw(gp->client->serial_data, player);
		pp = joynet_create_packet(JOYNET_GAME_MESSAGE_CONNECT, gp->client->serial_data);
		enet_peer_send(gp->client->peer, JOYNET_CHANNEL_GAME, pp);
	}
	else
	{
		if(player < 0)
		{
			for(i = 0; i < gp->players; i++)
			{
				if(!gp->player[i]->playing)
				{
					assigned_player = i;
					break;
				}
			}
		}
		if(assigned_player >= 0)
		{
			if(!gp->player[assigned_player]->playing)
			{
				gp->player[assigned_player]->playing = 1;
				if(gp->type == JOYNET_GAME_TYPE_CONTROLLERS)
				{
					gp->player_controller[assigned_player]->port = assigned_player;
				}
				else if(gp->type == JOYNET_GAME_TYPE_MOUSE)
				{
					gp->player_mouse[assigned_player]->port = assigned_player;
				}
				gp->player[assigned_player]->local = 1;
				gp->player_count++;
				if(gp->type == JOYNET_GAME_TYPE_CONTROLLERS)
				{
					gp->controller[controller]->port = assigned_player;
				}
				else if(gp->type == JOYNET_GAME_TYPE_MOUSE)
				{
					gp->mouse[controller]->port = assigned_player;
				}

				/* reset selections to prevent bugs */
				memset(gp->player[assigned_player]->selected_content, 0, sizeof(unsigned long) * JOYNET_GAME_MAX_CONTENT_LISTS);
				memset(gp->player[assigned_player]->selected_content_index, 0, sizeof(int) * JOYNET_GAME_MAX_CONTENT_LISTS);
				if(gp->callback)
				{
					JOYNET_MESSAGE message;

					/* send connect message */
					message.type = JOYNET_GAME_MESSAGE_CONNECT;
					joynet_serialize(gp->serial_data, data);
					joynet_putw(gp->serial_data, controller);
					joynet_putw(gp->serial_data, assigned_player);
					message.data = data;
					message.data_size = 4;
					message.event = NULL;
					gp->callback(&message);

					/* send add player message */
					message.type = JOYNET_GAME_MESSAGE_ADD_PLAYER;
					joynet_serialize(gp->serial_data, data);
					joynet_putw(gp->serial_data, assigned_player);
					joynet_putw(gp->serial_data, 2);
					joynet_write(gp->serial_data, "", 2);
					message.data = data;
					message.data_size = 6;
					message.event = NULL;
					gp->callback(&message);
				}
			}
		}
	}
}
Beispiel #14
0
void joynet_poll_server(JOYNET_SERVER * sp)
{
	if(sp && sp->host)
	{
		while(enet_host_service(sp->host, &sp->event, 0))
		{
			switch(sp->event.type)
			{
				/* a connection has been received */
				case ENET_EVENT_TYPE_CONNECT:
				{
					int client = joynet_server_find_free_client(sp);
					
					if(client >= 0)
					{
						sp->client[client]->peer = sp->event.peer;
					}
					sp->client[client]->peer->data = malloc(sizeof(int));
					if(!sp->client[client]->peer->data)
					{
						/* need error system */
					}
					else
					{
						joynet_serialize(sp->serial_data, sp->client[client]->peer->data);
						joynet_putl(sp->serial_data, client);
					}
					sp->client[client]->user = client;
					strcpy(sp->client[client]->screen_name, "");
					break;
				}
				
				/* client disconnected */
				case ENET_EVENT_TYPE_DISCONNECT:
				{
					sp->client[joynet_get_client_from_peer(sp, sp->event.peer)]->peer = NULL;
					break;
				}
				
				/* packet received */
				case ENET_EVENT_TYPE_RECEIVE:
				{
					JOYNET_MESSAGE message;
					joynet_decode_message(&message, &sp->event);
					switch(sp->event.channelID)
					{
						case JOYNET_CHANNEL_SYSTEM:
						{
							break;
						}
						case JOYNET_CHANNEL_CHAT:
						{
							joynet_handle_server_chat_message(sp, &message);
							break;
						}
						case JOYNET_CHANNEL_GAME:
						{
							joynet_handle_server_game_message(sp, &message);
							break;
						}
					}
					
					if(sp->channel_callback[sp->event.channelID])
					{
						sp->channel_callback[sp->event.channelID](&message);
					}
					break;
				}
				default:
				{
					break;
				}
			}
			if(sp->internal_callback)
			{
				sp->internal_callback(&sp->event);
			}
			if(sp->global_callback)
			{
				sp->global_callback(&sp->event);
			}
		}
	}
}
Beispiel #15
0
/* decode one input frame and update player controllers to reflect this */   
void joynet_game_logic(JOYNET_GAME * gp)
{
	if(gp->input_buffer->frames > 0)
	{
		switch(gp->type)
		{
			case JOYNET_GAME_TYPE_MOUSE:
			{
				joynet_serialize(gp->serial_data, &gp->input_buffer->data[gp->input_buffer->read_pos]);
				if(gp->controller_axes > 0)
				{
					joynet_getw(gp->serial_data, &gp->player[gp->current_player]->controller->mouse_x);
				}
				if(gp->controller_axes > 1)
				{
					joynet_getw(gp->serial_data, &gp->player[gp->current_player]->controller->mouse_y);
				}
				if(gp->controller_axes > 2)
				{
					joynet_getw(gp->serial_data, &gp->player[gp->current_player]->controller->mouse_z);
				}
				if(gp->controller_buttons > 0)
				{
					joynet_getc(gp->serial_data, &gp->player[gp->current_player]->controller->mouse_b);
				}
				gp->input_buffer->read_pos += joynet_get_serial_size(gp->serial_data);
				if(gp->input_buffer->read_pos >= gp->max_buffer_frames * gp->input_buffer->frame_size)
				{
					gp->input_buffer->read_pos = 0;
				}
				break;
			}
			case JOYNET_GAME_TYPE_CONTROLLERS:
			{
				unsigned char axis_data;
				int i, j;
				
				for(i = 0; i < gp->players; i++)
				{
					if(gp->player[i]->controller)
					{
						joynet_serialize(gp->serial_data, &gp->input_buffer->data[gp->input_buffer->read_pos]);
						if(gp->controller_axes > 0)
						{
							joynet_getc(gp->serial_data, (char *)(&axis_data));
							gp->player[i]->controller->axis[0] = (float)axis_data / 127.5 - 1.0;
						}
						if(gp->controller_axes > 1)
						{
							joynet_getc(gp->serial_data, (char *)(&axis_data));
							gp->player[i]->controller->axis[1] = (float)axis_data / 127.5 - 1.0;
						}
						if(gp->controller_axes > 2)
						{
							joynet_getc(gp->serial_data, (char *)(&axis_data));
							gp->player[i]->controller->axis[2] = (float)axis_data / 127.5 - 1.0;
						}
						if(gp->controller_buttons > 0)
						{
							joynet_getc(gp->serial_data, &gp->player[i]->controller->bits[0]);
							for(j = 0; j < 8; j++)
							{
								gp->player[i]->controller->button[j] = ((gp->player[i]->controller->bits[0] >> j) & 1);
							}
//								printf("bits = %d\n", gp->player[i]->controller->bits[0]);
						}
						if(gp->controller_buttons > 8)
						{
							joynet_getc(gp->serial_data, &gp->player[i]->controller->bits[1]);
							for(j = 0; j < 8; j++)
							{
								gp->player[i]->controller->button[j + 8] = ((gp->player[i]->controller->bits[1] >> j) & 1);
							}
						}
						gp->input_buffer->read_pos += joynet_get_serial_size(gp->serial_data);
						if(gp->input_buffer->read_pos >= gp->max_buffer_frames * gp->input_buffer->frame_size)
						{
							gp->input_buffer->read_pos = 0;
						}
					}
				}
				break;
			}
Beispiel #16
0
/* if we have new input send INPUT message
 * send NO_INPUT message when no new input has been received
 * call this once per tick */
void joynet_update_game_server(JOYNET_SERVER * sp, JOYNET_GAME * gp)
{
	ENetPacket * pp;
	char data[1024];
	int i, j;
	
	if(gp->state == JOYNET_GAME_STATE_PLAYING)
	{
		switch(gp->type)
		{
			case JOYNET_GAME_TYPE_MOUSE:
			{
				if(gp->received_input)
				{
					joynet_serialize(sp->serial_data, data);
					if(joynet_current_server_game->controller_axes > 0)
					{
						joynet_putw(sp->serial_data, joynet_current_server_game->player[joynet_current_server_game->current_player]->controller->mouse_x);
					}
					if(joynet_current_server_game->controller_axes > 1)
					{
						joynet_putw(sp->serial_data, joynet_current_server_game->player[joynet_current_server_game->current_player]->controller->mouse_y);
					}
					if(joynet_current_server_game->controller_axes > 2)
					{
						joynet_putw(sp->serial_data, joynet_current_server_game->player[joynet_current_server_game->current_player]->controller->mouse_z);
					}
					if(joynet_current_server_game->controller_buttons > 0)
					{
						joynet_putc(sp->serial_data, joynet_current_server_game->player[joynet_current_server_game->current_player]->controller->mouse_b);
					}
					for(i = 0; i < sp->max_clients; i++)
					{
						if(sp->client[i]->peer && (sp->client[i]->playing || sp->client[i]->spectating == 1))
						{
							pp = joynet_create_packet(JOYNET_GAME_MESSAGE_INPUT, sp->serial_data);
							enet_peer_send(sp->client[i]->peer, JOYNET_CHANNEL_GAME, pp);
						}
					}
				}
				else
				{
					for(i = 0; i < sp->max_clients; i++)
					{
						if(sp->client[i]->peer && (sp->client[i]->playing || sp->client[i]->spectating == 1))
						{
							pp = joynet_create_packet(JOYNET_GAME_MESSAGE_NO_INPUT, NULL);
							enet_peer_send(sp->client[i]->peer, JOYNET_CHANNEL_GAME, pp);
						}
					}
				}
				break;
			}
			case JOYNET_GAME_TYPE_CONTROLLERS:
			{
				unsigned char axis_data;
				char cbits = 1;
				joynet_serialize(sp->serial_data, data);
				for(i = 0; i < joynet_current_server_game->players; i++)
				{
					if(joynet_current_server_game->player[i]->controller)
					{
						for(j = 0; j < joynet_current_server_game->controller_axes; j++)
						{
							axis_data = (unsigned char)((joynet_current_server_game->player[i]->controller->axis[j] + 1.0) * 127.5);
							joynet_putc(sp->serial_data, axis_data);
						}
						if(joynet_current_server_game->controller_buttons > 0)
						{
							joynet_putc(sp->serial_data, joynet_current_server_game->player[i]->controller->bits[0]);
						}
						if(joynet_current_server_game->controller_buttons > 8)
						{
							joynet_current_server_game->player[i]->controller->bits[1] = 0;
							for(j = 0; j < 8; j++)
							{
								if(joynet_current_server_game->player[i]->controller->button[i])
								{
									joynet_current_server_game->player[i]->controller->bits[1] |= cbits;
								}
								cbits = cbits << 1;
							}
							joynet_putc(sp->serial_data, joynet_current_server_game->player[i]->controller->bits[1]);
						}
					}
				}
				for(i = 0; i < sp->max_clients; i++)
				{
					if(sp->client[i]->peer && (sp->client[i]->playing || sp->client[i]->spectating == 1))
					{
						pp = joynet_create_packet(JOYNET_GAME_MESSAGE_INPUT, sp->serial_data);
						enet_peer_send(sp->client[i]->peer, JOYNET_CHANNEL_GAME, pp);
					}
				}
				break;
			}
		}
		gp->received_input = 0;
	}
}
Beispiel #17
0
/* construct one frame of input to be sent to the server */
int joynet_encode_game_input(JOYNET_GAME * gp, char * buffer)
{
	joynet_serialize(gp->serial_data, buffer);
	int i, j;
	
	switch(gp->type)
	{
		case JOYNET_GAME_TYPE_MOUSE:
		{
			
			/* see if any of the local players are the current player */
			for(i = 0; i < gp->controllers; i++)
			{
				if(gp->controller[i]->port == gp->current_player)
				{
					if(gp->controller_axes > 0)
					{
						joynet_putw(gp->serial_data, gp->controller[0]->mouse_x);
					}
					if(gp->controller_axes > 1)
					{
						joynet_putw(gp->serial_data, gp->controller[0]->mouse_y);
					}
					if(gp->controller_axes > 2)
					{
						joynet_putw(gp->serial_data, gp->controller[0]->mouse_z);
					}
					if(gp->controller_buttons > 0)
					{
						joynet_putc(gp->serial_data, gp->controller[0]->mouse_b);
					}
					return 1;
				}
			}
			return 0;
		}
		case JOYNET_GAME_TYPE_CONTROLLERS:
		{
			
			unsigned char axis_data;
			char cbits = 1;
			
			for(i = 0; i < gp->controllers; i++)
			{
				if(gp->controller[gp->controller_sort_data[i].index]->port >= 0)
				{
					for(j = 0; j < gp->controller_axes; j++)
					{
						axis_data = (unsigned char)((gp->controller[gp->controller_sort_data[i].index]->axis[j] + 1.0) * 127.5);
						joynet_putc(gp->serial_data, axis_data);
					}
					cbits = 1;
					if(gp->controller_buttons > 0)
					{
						gp->controller[gp->controller_sort_data[i].index]->bits[0] = 0;
						for(j = 0; j < gp->controller_buttons; j++)
						{
							if(gp->controller[gp->controller_sort_data[i].index]->button[j])
							{
								gp->controller[gp->controller_sort_data[i].index]->bits[0] |= cbits;
							}
							cbits = cbits << 1;
						}
						joynet_putc(gp->serial_data, gp->controller[gp->controller_sort_data[i].index]->bits[0]);
//						printf("bits = %d\n", gp->controller[i]->bits[0]);
					}
					cbits = 1;
					if(gp->controller_buttons > 8)
					{
						gp->controller[gp->controller_sort_data[i].index]->bits[1] = 0;
						for(j = 0; j < gp->controller_buttons; j++)
						{
							if(gp->controller[gp->controller_sort_data[i].index]->button[j])
							{
								gp->controller[gp->controller_sort_data[i].index]->bits[1] |= cbits;
							}
							cbits = cbits << 1;
						}
						joynet_putc(gp->serial_data, gp->controller[gp->controller_sort_data[i].index]->bits[1]);
					}
				}
			}
			return 1;
		}
	}
	return 0;
}
Beispiel #18
0
/* if we have new input send INPUT message
 * send NO_INPUT message when no new input has been received
 * call this once per tick */
void joynet_update_game_server(JOYNET_SERVER * sp, JOYNET_GAME * gp)
{
	ENetPacket * pp;
	char data[1024];
	int i, j;

	if(gp->state == JOYNET_GAME_STATE_PLAYING)
	{
		switch(gp->type)
		{
			case JOYNET_GAME_TYPE_MOUSE:
			{
				if(gp->input_buffer->frames)
				{
                    /* pull the next input frame from the buffer and send it to
                       everyone */
                    joynet_read_input_buffer_frame(joynet_current_server_game->input_buffer, data);
					for(i = 0; i < sp->max_clients; i++)
					{
						if(sp->client[i]->peer && (sp->client[i]->playing || sp->client[i]->spectating == 1))
						{
							pp = joynet_build_packet(JOYNET_GAME_MESSAGE_INPUT, data, joynet_current_server_game->input_buffer->frame_size);
							enet_peer_send(sp->client[i]->peer, JOYNET_CHANNEL_GAME, pp);
						}
					}

                    /* decode the input so we have a copy of the current inputs
                       on the server */
                    joynet_serialize(gp->serial_data, data);
                    if(gp->controller_axes > 0)
    				{
    					joynet_getw(gp->serial_data, &gp->player_mouse[0]->x);
    				}
    				if(gp->controller_axes > 1)
    				{
    					joynet_getw(gp->serial_data, &gp->player_mouse[0]->y);
    				}
    				if(gp->controller_axes > 2)
    				{
    					joynet_getw(gp->serial_data, &gp->player_mouse[0]->z);
    				}
    				if(gp->controller_buttons > 0)
    				{
    					joynet_getc(gp->serial_data, &gp->player_mouse[0]->b);
    				}
				}

                /* if we have no buffered input, send NO_INPUT message */
				else
				{
					for(i = 0; i < sp->max_clients; i++)
					{
						if(sp->client[i]->peer && (sp->client[i]->playing || sp->client[i]->spectating == 1))
						{
							pp = joynet_create_packet(JOYNET_GAME_MESSAGE_NO_INPUT, NULL);
							enet_peer_send(sp->client[i]->peer, JOYNET_CHANNEL_GAME, pp);
						}
					}
				}
				break;
			}
			case JOYNET_GAME_TYPE_CONTROLLERS:
			{
                /* read next input frame and send to everyone */
                if(joynet_current_server_game->input_buffer->frames)
                {
                    joynet_read_input_buffer_frame(joynet_current_server_game->input_buffer, data);
                    for(i = 0; i < sp->max_clients; i++)
    				{
    					if(sp->client[i]->peer && (sp->client[i]->playing || sp->client[i]->spectating == 1))
    					{
    						pp = joynet_build_packet(JOYNET_GAME_MESSAGE_INPUT, data, joynet_current_server_game->input_buffer->frame_size);
    						enet_peer_send(sp->client[i]->peer, JOYNET_CHANNEL_GAME, pp);
    					}
    				}
                }

                /* send NO_INPUT message if we don't have anything in the buffer */
                else
                {
                    for(i = 0; i < sp->max_clients; i++)
					{
						if(sp->client[i]->peer && (sp->client[i]->playing || sp->client[i]->spectating == 1))
						{
							pp = joynet_create_packet(JOYNET_GAME_MESSAGE_NO_INPUT, NULL);
							enet_peer_send(sp->client[i]->peer, JOYNET_CHANNEL_GAME, pp);
						}
					}
                }

				unsigned char axis_data;
				joynet_serialize(gp->serial_data, data);
                for(i = 0; i < gp->players; i++)
				{
					if(gp->player[i]->playing)
					{
						if(gp->controller_axes > 0)
						{
							joynet_getc(gp->serial_data, (char *)(&axis_data));
							gp->player_controller[i]->axis[0] = (float)axis_data / 127.5 - 1.0;
						}
						if(gp->controller_axes > 1)
						{
							joynet_getc(gp->serial_data, (char *)(&axis_data));
							gp->player_controller[i]->axis[1] = (float)axis_data / 127.5 - 1.0;
						}
						if(gp->controller_axes > 2)
						{
							joynet_getc(gp->serial_data, (char *)(&axis_data));
							gp->player_controller[i]->axis[2] = (float)axis_data / 127.5 - 1.0;
						}
						if(gp->controller_buttons > 0)
						{
							joynet_getc(gp->serial_data, &gp->player_controller[i]->bits[0]);
							for(j = 0; j < 8; j++)
							{
								gp->player_controller[i]->button[j] = ((gp->player_controller[i]->bits[0] >> j) & 1);
							}
						}
						if(gp->controller_buttons > 8)
						{
							joynet_getc(gp->serial_data, &gp->player_controller[i]->bits[1]);
							for(j = 0; j < 8; j++)
							{
								gp->player_controller[i]->button[j + 8] = ((gp->player_controller[i]->bits[1] >> j) & 1);
							}
						}
					}
				}
				break;
			}
		}