OpenTyrian: merged upstream changes
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* OpenTyrian Classic: A modern cross-platform port of Tyrian
|
||||
* OpenTyrian: A modern cross-platform port of Tyrian
|
||||
* Copyright (C) 2007-2009 The OpenTyrian Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
@@ -30,9 +30,6 @@
|
||||
#include "varz.h"
|
||||
#include "video.h"
|
||||
|
||||
#include "SDL.h"
|
||||
#include "SDL_net.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
/* HERE BE DRAGONS!
|
||||
@@ -66,27 +63,29 @@ static char empty_string[] = "";
|
||||
char *network_player_name = empty_string,
|
||||
*network_opponent_name = empty_string;
|
||||
|
||||
UDPsocket socket;
|
||||
IPaddress ip;
|
||||
#ifdef WITH_NETWORK
|
||||
static UDPsocket socket;
|
||||
static IPaddress ip;
|
||||
|
||||
UDPpacket *packet_out_temp, *packet_temp;
|
||||
UDPpacket *packet_out_temp;
|
||||
static UDPpacket *packet_temp;
|
||||
|
||||
UDPpacket *packet_in[NET_PACKET_QUEUE] = { NULL },
|
||||
*packet_out[NET_PACKET_QUEUE] = { NULL };
|
||||
|
||||
Uint16 last_out_sync = 0, queue_in_sync = 0, queue_out_sync = 0, last_ack_sync = 0;
|
||||
Uint32 last_in_tick = 0, last_out_tick = 0;
|
||||
static Uint16 last_out_sync = 0, queue_in_sync = 0, queue_out_sync = 0, last_ack_sync = 0;
|
||||
static Uint32 last_in_tick = 0, last_out_tick = 0;
|
||||
|
||||
UDPpacket *packet_state_in[NET_PACKET_QUEUE] = { NULL },
|
||||
*packet_state_in_xor[NET_PACKET_QUEUE] = { NULL },
|
||||
*packet_state_out[NET_PACKET_QUEUE] = { NULL };
|
||||
UDPpacket *packet_state_in[NET_PACKET_QUEUE] = { NULL };
|
||||
static UDPpacket *packet_state_in_xor[NET_PACKET_QUEUE] = { NULL };
|
||||
UDPpacket *packet_state_out[NET_PACKET_QUEUE] = { NULL };
|
||||
|
||||
Uint16 last_state_in_sync = 0, last_state_out_sync = 0;
|
||||
Uint32 last_state_in_tick = 0;
|
||||
static Uint16 last_state_in_sync = 0, last_state_out_sync = 0;
|
||||
static Uint32 last_state_in_tick = 0;
|
||||
|
||||
bool net_initialized = false;
|
||||
static bool net_initialized = false;
|
||||
static bool connected = false, quit = false;
|
||||
|
||||
#endif
|
||||
|
||||
uint thisPlayerNum = 0; /* Player number on this PC (1 or 2) */
|
||||
|
||||
@@ -98,6 +97,41 @@ JE_boolean moveOk;
|
||||
JE_boolean pauseRequest, skipLevelRequest, helpRequest, nortShipRequest;
|
||||
JE_boolean yourInGameMenuRequest, inGameMenuRequest;
|
||||
|
||||
#ifdef WITH_NETWORK
|
||||
static void packet_copy( UDPpacket *dst, UDPpacket *src )
|
||||
{
|
||||
void *temp = dst->data;
|
||||
memcpy(dst, src, sizeof(*dst));
|
||||
dst->data = temp;
|
||||
memcpy(dst->data, src->data, src->len);
|
||||
}
|
||||
|
||||
static void packets_shift_up( UDPpacket **packet, int max_packets )
|
||||
{
|
||||
if (packet[0])
|
||||
{
|
||||
SDLNet_FreePacket(packet[0]);
|
||||
}
|
||||
for (int i = 0; i < max_packets - 1; i++)
|
||||
{
|
||||
packet[i] = packet[i + 1];
|
||||
}
|
||||
packet[max_packets - 1] = NULL;
|
||||
}
|
||||
|
||||
static void packets_shift_down( UDPpacket **packet, int max_packets )
|
||||
{
|
||||
if (packet[max_packets - 1])
|
||||
{
|
||||
SDLNet_FreePacket(packet[max_packets - 1]);
|
||||
}
|
||||
for (int i = max_packets - 1; i > 0; i--)
|
||||
{
|
||||
packet[i] = packet[i - 1];
|
||||
}
|
||||
packet[0] = NULL;
|
||||
}
|
||||
|
||||
// prepare new packet for sending
|
||||
void network_prepare( Uint16 type )
|
||||
{
|
||||
@@ -105,6 +139,20 @@ void network_prepare( Uint16 type )
|
||||
SDLNet_Write16(last_out_sync, &packet_out_temp->data[2]);
|
||||
}
|
||||
|
||||
// send packet but don't expect acknoledgment of delivery
|
||||
static bool network_send_no_ack( int len )
|
||||
{
|
||||
packet_out_temp->len = len;
|
||||
|
||||
if (!SDLNet_UDP_Send(socket, 0, packet_out_temp))
|
||||
{
|
||||
printf("SDLNet_UDP_Send: %s\n", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// send packet and place it in queue to be acknowledged
|
||||
bool network_send( int len )
|
||||
{
|
||||
@@ -129,18 +177,20 @@ bool network_send( int len )
|
||||
return temp;
|
||||
}
|
||||
|
||||
// send packet but don't expect acknoledgment of delivery
|
||||
bool network_send_no_ack( int len )
|
||||
// send acknowledgement packet
|
||||
static int network_acknowledge( Uint16 sync )
|
||||
{
|
||||
packet_out_temp->len = len;
|
||||
SDLNet_Write16(PACKET_ACKNOWLEDGE, &packet_out_temp->data[0]);
|
||||
SDLNet_Write16(sync, &packet_out_temp->data[2]);
|
||||
network_send_no_ack(4);
|
||||
|
||||
if (!SDLNet_UDP_Send(socket, 0, packet_out_temp))
|
||||
{
|
||||
printf("SDLNet_UDP_Send: %s\n", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
return true;
|
||||
// activity lately?
|
||||
static bool network_is_alive( void )
|
||||
{
|
||||
return (SDL_GetTicks() - last_in_tick < NET_TIME_OUT || SDL_GetTicks() - last_state_in_tick < NET_TIME_OUT);
|
||||
}
|
||||
|
||||
// poll for new packets received, check that connection is alive, resend queued packets if necessary
|
||||
@@ -337,16 +387,6 @@ int network_check( void )
|
||||
return 0;
|
||||
}
|
||||
|
||||
// send acknowledgement packet
|
||||
int network_acknowledge( Uint16 sync )
|
||||
{
|
||||
SDLNet_Write16(PACKET_ACKNOWLEDGE, &packet_out_temp->data[0]);
|
||||
SDLNet_Write16(sync, &packet_out_temp->data[2]);
|
||||
network_send_no_ack(4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// discard working packet, now processing next packet in queue
|
||||
bool network_update( void )
|
||||
{
|
||||
@@ -368,12 +408,6 @@ bool network_is_sync( void )
|
||||
return (queue_out_sync - last_ack_sync == 1);
|
||||
}
|
||||
|
||||
// activity lately?
|
||||
bool network_is_alive( void )
|
||||
{
|
||||
return (SDL_GetTicks() - last_in_tick < NET_TIME_OUT || SDL_GetTicks() - last_state_in_tick < NET_TIME_OUT);
|
||||
}
|
||||
|
||||
|
||||
// prepare new state for sending
|
||||
void network_state_prepare( void )
|
||||
@@ -741,40 +775,7 @@ int network_init( void )
|
||||
return 0;
|
||||
}
|
||||
|
||||
void packet_copy( UDPpacket *dst, UDPpacket *src )
|
||||
{
|
||||
void *temp = dst->data;
|
||||
memcpy(dst, src, sizeof(*dst));
|
||||
dst->data = temp;
|
||||
memcpy(dst->data, src->data, src->len);
|
||||
}
|
||||
|
||||
void packets_shift_up( UDPpacket **packet, int max_packets )
|
||||
{
|
||||
if (packet[0])
|
||||
{
|
||||
SDLNet_FreePacket(packet[0]);
|
||||
}
|
||||
for (int i = 0; i < max_packets - 1; i++)
|
||||
{
|
||||
packet[i] = packet[i + 1];
|
||||
}
|
||||
packet[max_packets - 1] = NULL;
|
||||
}
|
||||
|
||||
void packets_shift_down( UDPpacket **packet, int max_packets )
|
||||
{
|
||||
if (packet[max_packets - 1])
|
||||
{
|
||||
SDLNet_FreePacket(packet[max_packets - 1]);
|
||||
}
|
||||
for (int i = max_packets - 1; i > 0; i--)
|
||||
{
|
||||
packet[i] = packet[i - 1];
|
||||
}
|
||||
packet[0] = NULL;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void JE_clearSpecialRequests( void )
|
||||
{
|
||||
@@ -785,4 +786,3 @@ void JE_clearSpecialRequests( void )
|
||||
nortShipRequest = false;
|
||||
}
|
||||
|
||||
// kate: tab-width 4; vim: set noet:
|
||||
|
||||
Reference in New Issue
Block a user