Пример #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);
			}
		}
	}
}
Пример #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;
		}
	}
}
Пример #3
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;
		}
	}
}
Пример #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
	{
	}
}
Пример #5
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);
		}
	}
}
Пример #6
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);
	}
}
Пример #7
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);
		}
	}
}
Пример #8
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;
}
Пример #9
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
	{
	}
}
Пример #10
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);
		}
	}
}
Пример #11
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;
	}
}
Пример #12
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;
}
Пример #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);
				}
			}
		}
	}
}