mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-06 06:32:56 +01:00
map loading over network
This commit is contained in:
@@ -167,8 +167,8 @@ void update_rain_animation()
|
||||
*/
|
||||
static void draw_light_rain(int left, int top, int width, int height)
|
||||
{
|
||||
int x_start = -RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) + 8;
|
||||
int y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) * 3) + 7;
|
||||
int x_start = -(int)RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) + 8;
|
||||
int y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) * 3) + 7;
|
||||
y_start = -y_start;
|
||||
|
||||
x_start += left;
|
||||
@@ -176,8 +176,8 @@ static void draw_light_rain(int left, int top, int width, int height)
|
||||
|
||||
gfx_draw_rain(left, top, width, height, x_start, y_start);
|
||||
|
||||
x_start = -RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) + 0x18;
|
||||
y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) * 4) + 0x0D;
|
||||
x_start = -(int)RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) + 0x18;
|
||||
y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) * 4) + 0x0D;
|
||||
y_start = -y_start;
|
||||
|
||||
x_start += left;
|
||||
@@ -191,8 +191,8 @@ static void draw_light_rain(int left, int top, int width, int height)
|
||||
*/
|
||||
static void draw_heavy_rain(int left, int top, int width, int height)
|
||||
{
|
||||
int x_start = -RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int);
|
||||
int y_start = RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) * 5;
|
||||
int x_start = -(int)RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32);
|
||||
int y_start = RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) * 5;
|
||||
y_start = -y_start;
|
||||
|
||||
x_start += left;
|
||||
@@ -200,8 +200,8 @@ static void draw_heavy_rain(int left, int top, int width, int height)
|
||||
|
||||
gfx_draw_rain(left, top, width, height, x_start, y_start);
|
||||
|
||||
x_start = -RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) + 0x10;
|
||||
y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) * 6) + 5;
|
||||
x_start = -(int)RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) + 0x10;
|
||||
y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) * 6) + 5;
|
||||
y_start = -y_start;
|
||||
|
||||
x_start += left;
|
||||
@@ -209,8 +209,8 @@ static void draw_heavy_rain(int left, int top, int width, int height)
|
||||
|
||||
gfx_draw_rain(left, top, width, height, x_start, y_start);
|
||||
|
||||
x_start = -RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) + 8;
|
||||
y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) * 3) + 7;
|
||||
x_start = -(int)RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) + 8;
|
||||
y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) * 3) + 7;
|
||||
y_start = -y_start;
|
||||
|
||||
x_start += left;
|
||||
@@ -218,8 +218,8 @@ static void draw_heavy_rain(int left, int top, int width, int height)
|
||||
|
||||
gfx_draw_rain(left, top, width, height, x_start, y_start);
|
||||
|
||||
x_start = -RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) + 0x18;
|
||||
y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) * 4) + 0x0D;
|
||||
x_start = -(int)RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) + 0x18;
|
||||
y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) * 4) + 0x0D;
|
||||
y_start = -y_start;
|
||||
|
||||
x_start += left;
|
||||
|
||||
57
src/game.c
57
src/game.c
@@ -233,6 +233,13 @@ void game_update()
|
||||
numUpdates = clamp(1, numUpdates, 4);
|
||||
}
|
||||
|
||||
if (gNetworkStatus == NETWORK_CLIENT) {
|
||||
if (gNetworkServerTick - RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) >= 10) {
|
||||
// make sure client doesn't fall behind the server too much
|
||||
numUpdates += 10;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the game one or more times
|
||||
if (RCT2_GLOBAL(RCT2_ADDRESS_GAME_PAUSED, uint8) == 0) {
|
||||
for (i = 0; i < numUpdates; i++) {
|
||||
@@ -271,7 +278,7 @@ void game_update()
|
||||
// the flickering frequency is reduced by 4, compared to the original
|
||||
// it was done due to inability to reproduce original frequency
|
||||
// and decision that the original one looks too fast
|
||||
if (RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, sint32) % 4 == 0)
|
||||
if (RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) % 4 == 0)
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_WINDOW_MAP_FLASHING_FLAGS, uint16) ^= (1 << 15);
|
||||
|
||||
// Handle guest map flashing
|
||||
@@ -303,8 +310,14 @@ void game_update()
|
||||
|
||||
void game_logic_update()
|
||||
{
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, sint32)++;
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, sint32)++;
|
||||
if (gNetworkStatus == NETWORK_CLIENT) {
|
||||
if (RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) == gNetworkServerTick) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32)++;
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32)++;
|
||||
RCT2_GLOBAL(0x009DEA66, sint16)++;
|
||||
if (RCT2_GLOBAL(0x009DEA66, sint16) == 0)
|
||||
RCT2_GLOBAL(0x009DEA66, sint16)--;
|
||||
@@ -402,18 +415,18 @@ int game_do_command_p(int command, int *eax, int *ebx, int *ecx, int *edx, int *
|
||||
}
|
||||
|
||||
if (sendPacket) {
|
||||
network_packet packet;
|
||||
packet.size = 8 * 4;
|
||||
uint32 *args = (uint32*)&packet.data;
|
||||
args[0] = command;
|
||||
args[1] = *eax;
|
||||
args[2] = *ebx;
|
||||
args[3] = *ecx;
|
||||
args[4] = *edx;
|
||||
args[5] = *esi;
|
||||
args[6] = *edi;
|
||||
args[7] = *ebp;
|
||||
network_send_packet(&packet);
|
||||
network_packet* packet = network_alloc_packet(9 * 4);
|
||||
uint32 *args = (uint32*)packet->data;
|
||||
args[0] = NETWORK_COMMAND_GAMECMD;
|
||||
args[1] = command;
|
||||
args[2] = *eax;
|
||||
args[3] = *ebx;
|
||||
args[4] = *ecx;
|
||||
args[5] = *edx;
|
||||
args[6] = *esi;
|
||||
args[7] = *edi;
|
||||
args[8] = *ebp;
|
||||
network_queue_packet(packet);
|
||||
|
||||
if (gNetworkStatus == NETWORK_CLIENT)
|
||||
return MONEY32_UNDEFINED;
|
||||
@@ -741,6 +754,9 @@ int game_load_sv6(SDL_RWops* rw)
|
||||
reset_0x69EBE4();
|
||||
openrct2_reset_object_tween_locations();
|
||||
game_convert_strings_to_utf8();
|
||||
if (gNetworkStatus == NETWORK_SERVER) {
|
||||
network_send_map();
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -750,8 +766,6 @@ int game_load_sv6(SDL_RWops* rw)
|
||||
*/
|
||||
int game_load_save(const char *path)
|
||||
{
|
||||
rct_window *mainWindow;
|
||||
|
||||
log_verbose("loading saved game, %s", path);
|
||||
|
||||
strcpy((char*)0x0141EF68, path);
|
||||
@@ -776,6 +790,14 @@ int game_load_save(const char *path)
|
||||
}
|
||||
SDL_RWclose(rw);
|
||||
|
||||
game_load_init();
|
||||
return 1;
|
||||
}
|
||||
|
||||
void game_load_init()
|
||||
{
|
||||
rct_window *mainWindow;
|
||||
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_FLAGS, uint8) = SCREEN_FLAGS_PLAYING;
|
||||
viewport_init_all();
|
||||
game_create_windows();
|
||||
@@ -814,7 +836,6 @@ int game_load_save(const char *path)
|
||||
gGameSpeed = 1;
|
||||
|
||||
scenario_set_filename((char*)0x0135936C);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -112,6 +112,7 @@ void game_reduce_game_speed();
|
||||
void game_load_or_quit_no_save_prompt();
|
||||
int game_load_sv6(SDL_RWops* rw);
|
||||
int game_load_save(const char *path);
|
||||
void game_load_init();
|
||||
void game_pause_toggle(int *eax, int *ebx, int *ecx, int *edx, int *esi, int *edi, int *ebp);
|
||||
void pause_toggle();
|
||||
int save_game();
|
||||
|
||||
@@ -48,5 +48,5 @@ void date_reset()
|
||||
{
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_MONTH_YEAR, sint16) = MONTH_MARCH;
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_MONTH_TICKS, sint16) = 0;
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, sint32) = 0;
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) = 0;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "../localisation/date.h"
|
||||
#include "../localisation/localisation.h"
|
||||
#include "../management/news_item.h"
|
||||
#include "../scenario.h"
|
||||
#include "network.h"
|
||||
|
||||
#pragma comment(lib, "Ws2_32.lib")
|
||||
@@ -34,14 +35,22 @@ int gNetworkStart = NETWORK_NONE;
|
||||
char gNetworkStartHost[128];
|
||||
int gNetworkStartPort = NETWORK_DEFAULT_PORT;
|
||||
int gNetworkStatus = NETWORK_NONE;
|
||||
uint32 gNetworkServerTick = 0;
|
||||
|
||||
static network_packet* _packetQueue = NULL;
|
||||
static int _wsaInitialised = 0;
|
||||
static WSADATA _wsaData;
|
||||
static SOCKET _serverSocket;
|
||||
static SOCKET _clientSocket;
|
||||
static network_packet _inboundPacket;
|
||||
static char* _chunkBuffer = NULL;
|
||||
|
||||
static int network_get_next_packet(network_packet *outPacket);
|
||||
static int network_read_next_packet(network_packet *outPacket);
|
||||
static void network_process_packet(network_packet *packet);
|
||||
static void network_init_packet(network_packet* packet, int size);
|
||||
static void network_close_packet(network_packet *packet);
|
||||
static int network_send_packet(network_packet *packet);
|
||||
static void network_send_queued_packets();
|
||||
|
||||
int network_init()
|
||||
{
|
||||
@@ -54,6 +63,8 @@ int network_init()
|
||||
_wsaInitialised = 1;
|
||||
}
|
||||
|
||||
network_init_packet(&_inboundPacket, 0);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -93,6 +104,8 @@ int network_begin_client(const char *host, int port)
|
||||
if (connect(_serverSocket, (SOCKADDR*)&serverAddress, sizeof(SOCKADDR_IN)) != 0) {
|
||||
log_error("Unable to connect to host.");
|
||||
return 0;
|
||||
} else {
|
||||
printf("Connected to server!\n");
|
||||
}
|
||||
|
||||
iMode = 1;
|
||||
@@ -174,7 +187,7 @@ void network_update()
|
||||
{
|
||||
SOCKET socket;
|
||||
int packetStatus;
|
||||
network_packet packet;
|
||||
static uint32 lastTickUpdate = 0;
|
||||
|
||||
if (gNetworkStatus == NETWORK_NONE)
|
||||
return;
|
||||
@@ -182,8 +195,9 @@ void network_update()
|
||||
socket = gNetworkStatus == NETWORK_CLIENT ? _serverSocket : _clientSocket;
|
||||
|
||||
do {
|
||||
packetStatus = network_get_next_packet(&packet);
|
||||
if (packetStatus == NETWORK_DISCONNECTED) {
|
||||
packetStatus = network_read_next_packet(&_inboundPacket);
|
||||
switch(packetStatus) {
|
||||
case NETWORK_DISCONNECTED:
|
||||
network_print_error();
|
||||
if (gNetworkStatus == NETWORK_CLIENT) {
|
||||
network_end_client();
|
||||
@@ -194,31 +208,69 @@ void network_update()
|
||||
printf("client disconnected...\n");
|
||||
return;
|
||||
}
|
||||
} else if (packetStatus == NETWORK_SUCCESS) {
|
||||
network_process_packet(&packet);
|
||||
break;
|
||||
case NETWORK_SUCCESS:
|
||||
// done reading in packet
|
||||
network_process_packet(&_inboundPacket);
|
||||
network_close_packet(&_inboundPacket);
|
||||
network_init_packet(&_inboundPacket, 0); // initialize the packet struct to be used again
|
||||
break;
|
||||
case NETWORK_MORE_DATA:
|
||||
// more data required to be read
|
||||
break;
|
||||
case NETWORK_NO_DATA:
|
||||
break;
|
||||
}
|
||||
} while (packetStatus == NETWORK_SUCCESS);
|
||||
} while (packetStatus == NETWORK_MORE_DATA || packetStatus == NETWORK_SUCCESS);
|
||||
|
||||
if (gNetworkStatus == NETWORK_SERVER) {
|
||||
if (SDL_GetTicks() - lastTickUpdate >= 100) {
|
||||
lastTickUpdate = SDL_GetTicks();
|
||||
network_send_tick();
|
||||
}
|
||||
}
|
||||
|
||||
network_send_queued_packets();
|
||||
}
|
||||
|
||||
static int network_get_next_packet(network_packet *outPacket)
|
||||
static int network_read_next_packet(network_packet *packet)
|
||||
{
|
||||
SOCKET socket;
|
||||
int readBytes;
|
||||
|
||||
socket = gNetworkStatus == NETWORK_CLIENT ? _serverSocket : _clientSocket;
|
||||
|
||||
readBytes = recv(socket, (char*)&outPacket->size, sizeof(outPacket->size), 0);
|
||||
if (readBytes == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
|
||||
return NETWORK_DISCONNECTED;
|
||||
|
||||
if (readBytes != sizeof(outPacket->size))
|
||||
return NETWORK_NO_DATA;
|
||||
|
||||
readBytes = recv(socket, (char*)&outPacket->data, outPacket->size, 0);
|
||||
if (readBytes != outPacket->size)
|
||||
return NETWORK_NO_DATA;
|
||||
|
||||
return NETWORK_SUCCESS;
|
||||
if (packet->read < sizeof(packet->size)) {
|
||||
// read packet size
|
||||
int readBytes = recv(socket, &((char*)&packet->size)[packet->read], sizeof(packet->size) - packet->read, 0);
|
||||
if (readBytes == SOCKET_ERROR || readBytes == 0) {
|
||||
if (WSAGetLastError() != WSAEWOULDBLOCK) {
|
||||
return NETWORK_DISCONNECTED;
|
||||
} else {
|
||||
return NETWORK_NO_DATA;
|
||||
}
|
||||
}
|
||||
packet->read += readBytes;
|
||||
} else {
|
||||
// read packet data
|
||||
if (!packet->data) {
|
||||
packet->size = ntohs(packet->size);
|
||||
packet->data = malloc(packet->size);
|
||||
}
|
||||
int dataread = packet->read - sizeof(packet->size);
|
||||
int readBytes = recv(socket, &packet->data[dataread], packet->size - dataread, 0);
|
||||
if (readBytes == SOCKET_ERROR || readBytes == 0) {
|
||||
if (WSAGetLastError() != WSAEWOULDBLOCK) {
|
||||
return NETWORK_DISCONNECTED;
|
||||
} else {
|
||||
return NETWORK_NO_DATA;
|
||||
}
|
||||
}
|
||||
packet->read += readBytes;
|
||||
if (packet->read == packet->size + sizeof(packet->size)) {
|
||||
return NETWORK_SUCCESS;
|
||||
}
|
||||
}
|
||||
return NETWORK_MORE_DATA;
|
||||
}
|
||||
|
||||
static void network_process_packet(network_packet *packet)
|
||||
@@ -227,39 +279,169 @@ static void network_process_packet(network_packet *packet)
|
||||
int command;
|
||||
rct_news_item newsItem;
|
||||
|
||||
args = (uint32*)&packet->data;
|
||||
args = (uint32*)packet->data;
|
||||
command = args[0];
|
||||
|
||||
switch (command) {
|
||||
case 700:
|
||||
newsItem.type = NEWS_ITEM_BLANK;
|
||||
newsItem.flags = 1;
|
||||
newsItem.assoc = 0;
|
||||
newsItem.ticks = 0;
|
||||
newsItem.month_year = RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_MONTH_YEAR, uint16);
|
||||
newsItem.day = ((days_in_month[(newsItem.month_year & 7)] * RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_MONTH_TICKS, uint16)) >> 16) + 1;
|
||||
newsItem.colour = FORMAT_TOPAZ;
|
||||
strcpy(newsItem.text, packet->data + 4);
|
||||
news_item_add_to_queue_custom(&newsItem);
|
||||
break;
|
||||
default:
|
||||
if (gNetworkStatus == NETWORK_CLIENT)
|
||||
command |= (1 << 31);
|
||||
case NETWORK_COMMAND_AUTH:{
|
||||
|
||||
game_do_command_p(command, &args[1], &args[2], &args[3], &args[4], &args[5], &args[6], &args[7]);
|
||||
break;
|
||||
}break;
|
||||
case NETWORK_COMMAND_MAP:{
|
||||
uint32 size = args[1];
|
||||
uint32 offset = args[2];
|
||||
if (!_chunkBuffer) {
|
||||
_chunkBuffer = malloc(0x600000);
|
||||
}
|
||||
if (_chunkBuffer) {
|
||||
int chunksize = packet->size - 4 - 4 - 4;
|
||||
if (offset + chunksize <= 0x600000) {
|
||||
memcpy(&_chunkBuffer[offset], packet->data + 4 + 4 + 4, chunksize);
|
||||
}
|
||||
if (offset + chunksize == size) {
|
||||
SDL_RWops* rw = SDL_RWFromMem(_chunkBuffer, size);
|
||||
if (game_load_sv6(rw)) {
|
||||
game_load_init();
|
||||
}
|
||||
SDL_RWclose(rw);
|
||||
}
|
||||
}
|
||||
}break;
|
||||
case NETWORK_COMMAND_CHAT:{
|
||||
newsItem.type = NEWS_ITEM_BLANK;
|
||||
newsItem.flags = 1;
|
||||
newsItem.assoc = 0;
|
||||
newsItem.ticks = 0;
|
||||
newsItem.month_year = RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_MONTH_YEAR, uint16);
|
||||
newsItem.day = ((days_in_month[(newsItem.month_year & 7)] * RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_MONTH_TICKS, uint16)) >> 16) + 1;
|
||||
newsItem.colour = FORMAT_TOPAZ;
|
||||
strcpy(newsItem.text, packet->data + 4);
|
||||
news_item_add_to_queue_custom(&newsItem);
|
||||
}break;
|
||||
case NETWORK_COMMAND_GAMECMD:{
|
||||
if (gNetworkStatus == NETWORK_CLIENT)
|
||||
args[1] |= (1 << 31);
|
||||
|
||||
game_do_command_p(args[1], &args[2], &args[3], &args[4], &args[5], &args[6], &args[7], &args[8]);
|
||||
}break;
|
||||
case NETWORK_COMMAND_TICK:{
|
||||
gNetworkServerTick = args[1];
|
||||
}break;
|
||||
}
|
||||
}
|
||||
|
||||
void network_send_packet(network_packet *packet)
|
||||
network_packet* network_alloc_packet(int size)
|
||||
{
|
||||
network_packet* packet = malloc(sizeof(network_packet));
|
||||
network_init_packet(packet, size);
|
||||
return packet;
|
||||
}
|
||||
|
||||
void network_init_packet(network_packet* packet, int size)
|
||||
{
|
||||
packet->read = 0;
|
||||
packet->size = size;
|
||||
packet->data = NULL;
|
||||
packet->next = NULL;
|
||||
if (size) {
|
||||
packet->data = malloc(size);
|
||||
}
|
||||
}
|
||||
|
||||
void network_queue_packet(network_packet *packet)
|
||||
{
|
||||
if (!_packetQueue) {
|
||||
_packetQueue = packet;
|
||||
return;
|
||||
}
|
||||
network_packet* end_packet = _packetQueue;
|
||||
while (end_packet->next) {
|
||||
end_packet = end_packet->next;
|
||||
}
|
||||
end_packet->next = packet;
|
||||
}
|
||||
|
||||
void network_send_queued_packets()
|
||||
{
|
||||
while (network_send_packet(_packetQueue)) {
|
||||
network_packet* sentPacket = _packetQueue;
|
||||
_packetQueue = _packetQueue->next;
|
||||
network_close_packet(sentPacket);
|
||||
free(sentPacket);
|
||||
}
|
||||
}
|
||||
|
||||
void network_close_packet(network_packet *packet)
|
||||
{
|
||||
free(packet->data);
|
||||
}
|
||||
|
||||
int network_send_packet(network_packet *packet)
|
||||
{
|
||||
if (!packet) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
SOCKET socket;
|
||||
|
||||
if (gNetworkStatus == NETWORK_NONE)
|
||||
return;
|
||||
return 0;
|
||||
|
||||
socket = gNetworkStatus == NETWORK_CLIENT ? _serverSocket : _clientSocket;
|
||||
send(socket, (char*)packet, 2 + packet->size, 0);
|
||||
|
||||
while (1) {
|
||||
if (packet->read < sizeof(packet->size)) {
|
||||
// send packet size
|
||||
uint16 size = htons(packet->size);
|
||||
int sentBytes = send(socket, &((char*)&size)[packet->read], sizeof(size) - packet->read, 0);
|
||||
if (sentBytes == SOCKET_ERROR) {
|
||||
return 0;
|
||||
}
|
||||
packet->read += sentBytes;
|
||||
} else {
|
||||
// send packet data
|
||||
int datasent = packet->read - sizeof(packet->size);
|
||||
int sentBytes = send(socket, &packet->data[datasent], packet->size - datasent, 0);
|
||||
if (sentBytes == SOCKET_ERROR) {
|
||||
return 0;
|
||||
}
|
||||
packet->read += sentBytes;
|
||||
if (packet->read == packet->size + sizeof(packet->size)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void network_send_tick()
|
||||
{
|
||||
network_packet* packet = network_alloc_packet(4 + sizeof(RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32)));
|
||||
*((uint32*)packet->data) = NETWORK_COMMAND_TICK;
|
||||
memcpy(packet->data + 4, (char*)&RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32), sizeof(RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32)));
|
||||
network_queue_packet(packet);
|
||||
}
|
||||
|
||||
void network_send_map()
|
||||
{
|
||||
int buffersize = 0x600000;
|
||||
char* buffer = malloc(buffersize);
|
||||
if (!buffer) {
|
||||
return;
|
||||
}
|
||||
SDL_RWops* rw = SDL_RWFromMem(buffer, buffersize);
|
||||
scenario_save(rw, 0);
|
||||
int size = (int)SDL_RWtell(rw);
|
||||
int chunksize = 1000;
|
||||
for (int i = 0; i < size; i += chunksize) {
|
||||
int datasize = min(chunksize, size - i);
|
||||
network_packet* packet = network_alloc_packet(4 + 4 + 4 + datasize);
|
||||
*((uint32*)packet->data) = NETWORK_COMMAND_MAP;
|
||||
*((uint32*)packet->data + 1) = size;
|
||||
*((uint32*)packet->data + 1 + 1) = i;
|
||||
memcpy(packet->data + 4 + 4 + 4, &buffer[i], datasize);
|
||||
network_queue_packet(packet);
|
||||
}
|
||||
SDL_RWclose(rw);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
void network_print_error()
|
||||
@@ -284,12 +466,10 @@ static void window_chat_host_textinput()
|
||||
if (!result)
|
||||
return;
|
||||
|
||||
network_packet packet;
|
||||
packet.size = 4 + strlen(text) + 1;
|
||||
*((uint32*)packet.data) = 700;
|
||||
strcpy(packet.data + 4, text);
|
||||
|
||||
network_send_packet(&packet);
|
||||
network_packet* packet = network_alloc_packet(4 + strlen(text) + 1);
|
||||
*((uint32*)packet->data) = NETWORK_COMMAND_CHAT;
|
||||
strcpy(packet->data + 4, text);
|
||||
network_queue_packet(packet);
|
||||
|
||||
window_close(w);
|
||||
}
|
||||
|
||||
@@ -27,9 +27,13 @@
|
||||
|
||||
#include "../common.h"
|
||||
|
||||
typedef struct {
|
||||
typedef struct network_packet network_packet;
|
||||
|
||||
typedef struct network_packet {
|
||||
uint16 size;
|
||||
uint8 data[128];
|
||||
uint8* data;
|
||||
int read;
|
||||
network_packet* next;
|
||||
} network_packet;
|
||||
|
||||
#define NETWORK_DEFAULT_PORT 11753
|
||||
@@ -43,13 +47,23 @@ enum {
|
||||
enum {
|
||||
NETWORK_SUCCESS,
|
||||
NETWORK_NO_DATA,
|
||||
NETWORK_MORE_DATA,
|
||||
NETWORK_DISCONNECTED
|
||||
};
|
||||
|
||||
enum {
|
||||
NETWORK_COMMAND_AUTH,
|
||||
NETWORK_COMMAND_MAP,
|
||||
NETWORK_COMMAND_CHAT,
|
||||
NETWORK_COMMAND_GAMECMD,
|
||||
NETWORK_COMMAND_TICK
|
||||
};
|
||||
|
||||
extern int gNetworkStart;
|
||||
extern char gNetworkStartHost[128];
|
||||
extern int gNetworkStartPort;
|
||||
extern int gNetworkStatus;
|
||||
extern uint32 gNetworkServerTick;
|
||||
|
||||
int network_init();
|
||||
void network_close();
|
||||
@@ -59,7 +73,11 @@ int network_begin_server(int port);
|
||||
void network_end_server();
|
||||
|
||||
void network_update();
|
||||
void network_send_packet(network_packet *packet);
|
||||
network_packet* network_alloc_packet(int size);
|
||||
void network_queue_packet(network_packet *packet);
|
||||
|
||||
void network_send_tick();
|
||||
void network_send_map();
|
||||
|
||||
void network_open_chat_box();
|
||||
|
||||
|
||||
@@ -3739,7 +3739,7 @@ static void peep_update_entering_park(rct_peep* peep){
|
||||
peep_window_state_update(peep);
|
||||
|
||||
peep->var_2A = 0;
|
||||
peep->time_in_park = RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, sint32);
|
||||
peep->time_in_park = RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32);
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_GUESTS_IN_PARK, uint16)++;
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_GUESTS_HEADING_FOR_PARK, uint16)--;
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_BTM_TOOLBAR_DIRTY_FLAGS, uint16) |= BTM_TB_DIRTY_FLAG_PEEP_COUNT;
|
||||
|
||||
@@ -74,7 +74,7 @@ int rct2_init()
|
||||
{
|
||||
log_verbose("initialising game");
|
||||
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) = 0;
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) = 0;
|
||||
RCT2_GLOBAL(0x009AC310, char*) = RCT2_GLOBAL(RCT2_ADDRESS_CMDLINE, char*);
|
||||
get_system_time();
|
||||
srand((unsigned int)time(0));
|
||||
|
||||
@@ -420,7 +420,7 @@ static void window_game_bottom_toolbar_draw_park_rating(rct_drawpixelinfo *dpi,
|
||||
|
||||
bar_width = (factor * (90 + WIDTH_MOD)) / 256;
|
||||
gfx_fill_rect_inset(dpi, x, y + 1, x + (93 + WIDTH_MOD), y + 9, w->colours[1], 48);
|
||||
if (!(colour & 0x80000000) || RCT2_GLOBAL(RCT2_ADDRESS_GAME_PAUSED, uint8) != 0 || (RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint8) & 8)) {
|
||||
if (!(colour & 0x80000000) || RCT2_GLOBAL(RCT2_ADDRESS_GAME_PAUSED, uint8) != 0 || (RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) & 8)) {
|
||||
if (bar_width > 2)
|
||||
gfx_fill_rect_inset(dpi, x + 2, y + 2, x + bar_width - 1, y + 8, colour & 0x7FFFFFFF, 0);
|
||||
}
|
||||
|
||||
@@ -168,7 +168,7 @@ void climate_update()
|
||||
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_CLIMATE_UPDATE_TIMER, sint16)--;
|
||||
|
||||
} else if (!(RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, sint32) & 0x7F)) {
|
||||
} else if (!(RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) & 0x7F)) {
|
||||
|
||||
if (temperature == target_temperature) {
|
||||
if (cur_gloom == next_gloom) {
|
||||
|
||||
@@ -539,7 +539,7 @@ void park_update()
|
||||
return;
|
||||
|
||||
// Every 5 seconds approximately
|
||||
if (RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint8) % 512 == 0) {
|
||||
if (RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) % 512 == 0) {
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_PARK_RATING, uint16) = calculate_park_rating();
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_PARK_VALUE, money32) = calculate_park_value();
|
||||
RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_COMPANY_VALUE, money32) = calculate_company_value();
|
||||
|
||||
Reference in New Issue
Block a user