Skip to content

Commit

Permalink
Updates to netcode
Browse files Browse the repository at this point in the history
- Added version compatibility checks
- Removed obsolete IPX session management
- Added various error messages
- Changed default server address to localhost
- Use a different default server port
  • Loading branch information
klei1984 committed Jan 21, 2024
1 parent fee9405 commit 0317272
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 99 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ target_include_directories(${PROJECT_NAME} PRIVATE
)

set_target_properties(${PROJECT_NAME} PROPERTIES
COMPILE_FLAGS "${MAX_CFLAGS} -DGAME_VERSION=\"\\\"v${GAME_VERSION}\\\"\" -DSDL_ASSERT_LEVEL=2"
COMPILE_FLAGS "${MAX_CFLAGS} -DGAME_VERSION_STRING=\"\\\"v${GAME_VERSION_STRING}\\\"\" -DGAME_VERSION_MAJOR=\"${GAME_VERSION_MAJOR}\" -DGAME_VERSION_MINOR=\"${GAME_VERSION_MINOR}\" -DGAME_VERSION_PATCH=\"${GAME_VERSION_PATCH}\" -DSDL_ASSERT_LEVEL=2"
)

if(NOT BUILD_SHARED_LIBS)
Expand Down
4 changes: 2 additions & 2 deletions assets/settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ dialog_center_mode=0

[NETWORK_SETTINGS]
transport=udp_default
host_address=192.168.0.1
host_port=213
host_address=127.0.0.1
host_port=31554
10 changes: 5 additions & 5 deletions cmake/setup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ if(Git_FOUND)
set(GAME_VERSION_PATCH ${CMAKE_MATCH_3})
set(GAME_VERSION_BUILD ${CMAKE_MATCH_4})
set(GAME_VERSION_REVISION ${CMAKE_MATCH_5})
set(GAME_VERSION "${GAME_VERSION_MAJOR}.${GAME_VERSION_MINOR}.${GAME_VERSION_PATCH} Build ${GAME_VERSION_BUILD} Revision ${GAME_VERSION_REVISION}")
set(GAME_VERSION_STRING "${GAME_VERSION_MAJOR}.${GAME_VERSION_MINOR}.${GAME_VERSION_PATCH} Build ${GAME_VERSION_BUILD} Revision ${GAME_VERSION_REVISION}")
else()
string(REGEX MATCH "^v([0-9]+)\\.([0-9]+)\\.([0-9]+)$" GIT_MATCHES "${GIT_OUTPUT}")
if(CMAKE_MATCH_COUNT EQUAL 3)
set(GAME_VERSION_MAJOR ${CMAKE_MATCH_1})
set(GAME_VERSION_MINOR ${CMAKE_MATCH_2})
set(GAME_VERSION_PATCH ${CMAKE_MATCH_3})
set(GAME_VERSION "${GAME_VERSION_MAJOR}.${GAME_VERSION_MINOR}.${GAME_VERSION_PATCH}")
set(GAME_VERSION_STRING "${GAME_VERSION_MAJOR}.${GAME_VERSION_MINOR}.${GAME_VERSION_PATCH}")
else()
string(REGEX MATCH "^([a-fA-F0-9]+)$" GIT_MATCHES "${GIT_OUTPUT}")
if(CMAKE_MATCH_COUNT EQUAL 1)
Expand All @@ -37,14 +37,14 @@ if(Git_FOUND)
set(GAME_VERSION_MAJOR "0")
set(GAME_VERSION_MINOR "6")
set(GAME_VERSION_PATCH "1")
set(GAME_VERSION "${GAME_VERSION_MAJOR}.${GAME_VERSION_MINOR}.${GAME_VERSION_PATCH} Revision ${GAME_VERSION_REVISION}")
set(GAME_VERSION_STRING "${GAME_VERSION_MAJOR}.${GAME_VERSION_MINOR}.${GAME_VERSION_PATCH} Revision ${GAME_VERSION_REVISION}")
endif()
endif()
else()
set(GAME_VERSION_MAJOR "0")
set(GAME_VERSION_MINOR "6")
set(GAME_VERSION_PATCH "1")
set(GAME_VERSION "${GAME_VERSION_MAJOR}.${GAME_VERSION_MINOR}.${GAME_VERSION_PATCH}")
set(GAME_VERSION_STRING "${GAME_VERSION_MAJOR}.${GAME_VERSION_MINOR}.${GAME_VERSION_PATCH}")
endif()

message(STATUS "Set build version to v${GAME_VERSION}")
message(STATUS "Set build version to v${GAME_VERSION_STRING}")
2 changes: 1 addition & 1 deletion src/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ void draw_copyright_label(WindowInfo* window) {

Text_TextBox(window->buffer, window->width,
"Copyright 1996 Interplay Productions. v1.04"
" (M.A.X. Port " GAME_VERSION ")",
" (M.A.X. Port " GAME_VERSION_STRING ")",
bounds.ulx, bounds.uly, bounds.lrx - bounds.ulx, bounds.lry - bounds.uly, COLOR_BLACK, true, false);
}

Expand Down
6 changes: 3 additions & 3 deletions src/message_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ void MessageManager_WrapText(const char* text, int16_t width) {
MessageManager_MessageBox_Height += 10;
}

void MessageManager_DrawMessageBoxText(uint8_t* buffer, int32_t width, int32_t left_margin, int32_t top_margin, char* text,
int32_t color, bool monospace) {
void MessageManager_DrawMessageBoxText(uint8_t* buffer, int32_t width, int32_t left_margin, int32_t top_margin,
char* text, int32_t color, bool monospace) {
int32_t flags;
int32_t offset;

Expand Down Expand Up @@ -156,7 +156,7 @@ void MessageManager_DrawMessage(const char* text, char type, UnitInfo* unit, Poi
}

void MessageManager_DrawMessage(const char* text, char type, int32_t mode, bool flag1, bool save_to_log) {
if (*text != '\0') {
if (text && *text != '\0') {
if (mode) {
DialogMenu dialog(text, flag1);
dialog.Run();
Expand Down
37 changes: 20 additions & 17 deletions src/remote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,10 +633,19 @@ int32_t Remote_Lobby(bool is_host_mode) {
Remote_Init();

if (ini_config.GetStringValue(INI_NETWORK_TRANSPORT, ini_transport, sizeof(ini_transport))) {
if (!strcmp(ini_transport, "udp_default")) {
if (!strcmp(ini_transport, TRANSPORT_DEFAULT_TYPE)) {
transort_type = TRANSPORT_DEFAULT_UDP;

} else {
SDL_Log("Remote: Unknown transport type (%s).\n", ini_transport);
SmartString error_message;

ini_config.SetStringValue(INI_NETWORK_TRANSPORT, TRANSPORT_DEFAULT_TYPE);

error_message.Sprintf(
100, "Unknown network transport layer type (%s), using default (" TRANSPORT_DEFAULT_TYPE ").\n",
ini_transport);
MessageManager_DrawMessage(error_message.GetCStr(), 2, 1, true);

transort_type = TRANSPORT_DEFAULT_UDP;
}

Expand All @@ -662,26 +671,25 @@ int32_t Remote_Lobby(bool is_host_mode) {
}

} else {
SDL_Log("Remote: Unable to initialize network transport layer.\n");
WindowManager_LoadBigImage(MAINPIC, WindowManager_GetWindow(WINDOW_MAIN_WINDOW),
WindowManager_GetWindow(WINDOW_MAIN_WINDOW)->width, false, true, -1, -1, true);
MessageManager_DrawMessage("Unable to initialize network transport layer.\n", 2, 1);

result = false;
}

return result;
}

void Remote_SetupConnection() {
uint32_t rng_number;
int32_t remote_player_count;

rng_number = dos_rand();
remote_player_count = Remote_SetupPlayers();

Remote_GameState = 2;
Remote_NetworkMenu->remote_player_count = 0;

Remote_SendNetPacket_32(rng_number, REMOTE_BROADCAST);

Remote_Transport->SetSessionId(rng_number);
Remote_SendNetPacket_32(REMOTE_BROADCAST);

while (Remote_NetworkMenu->remote_player_count != remote_player_count) {
Remote_ProcessNetPackets();
Expand Down Expand Up @@ -2457,14 +2465,13 @@ void Remote_ReceiveNetPacket_31(NetPacket& packet) {
}
}

void Remote_SendNetPacket_32(uint16_t random_number, int32_t transmit_mode) {
void Remote_SendNetPacket_32(int32_t transmit_mode) {
NetPacket packet;

packet << static_cast<uint8_t>(REMOTE_PACKET_32);
packet << static_cast<uint16_t>(Remote_NetworkMenu->host_node);

packet << Remote_NetworkMenu->player_team;
packet << random_number;

Remote_TransmitPacket(packet, transmit_mode);
}
Expand All @@ -2485,16 +2492,12 @@ void Remote_ReceiveNetPacket_32(NetPacket& packet) {

} else {
char team;
uint16_t random_number;

Remote_SetupPlayers();

Remote_GameState = 2;

packet >> team;
packet >> random_number;

Remote_Transport->SetSessionId(random_number);

Remote_SendNetPacket_05(0, REMOTE_UNICAST);
}
Expand Down Expand Up @@ -2858,7 +2861,7 @@ void Remote_SendNetPacket_44(NetAddress& address) {

packet << static_cast<uint8_t>(REMOTE_PACKET_44);
packet << static_cast<uint16_t>(Remote_NetworkMenu->player_node);
packet << SmartString(GAME_VERSION);
packet << static_cast<uint32_t>(GAME_VERSION);
packet << SmartString(Remote_NetworkMenu->player_name);

Remote_TransmitPacket(packet, REMOTE_UNICAST);
Expand All @@ -2871,10 +2874,10 @@ void Remote_ReceiveNetPacket_44(NetPacket& packet) {
packet >> entity_id;

if (!Remote_Hosts.Find(entity_id)) {
SmartString game_version;
uint32_t game_version;

packet >> game_version;
if (!strcmp(GAME_VERSION, game_version.GetCStr())) {
if (game_version == GAME_VERSION) {
NetNode host_node;
SmartString host_name;

Expand Down
2 changes: 1 addition & 1 deletion src/remote.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void Remote_SendNetPacket_28(int32_t node);
void Remote_SendNetPacket_29(int32_t node);
void Remote_SendNetPacket_30(NetAddress& address);
void Remote_SendNetPacket_31(int32_t node);
void Remote_SendNetPacket_32(uint16_t random_number, int32_t team);
void Remote_SendNetPacket_32(int32_t team);
void Remote_SendNetPacket_33();
void Remote_SendNetPacket_34();
void Remote_SendNetPacket_35();
Expand Down
2 changes: 1 addition & 1 deletion src/transport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#define TRANSPORT_MAX_TEAM_COUNT 4
#define TRANSPORT_MAX_PACKET_SIZE 1440
#define TRANSPORT_DEFAULT_TYPE "udp_default"

enum {
TRANSPORT_DEFAULT_UDP,
Expand All @@ -48,7 +49,6 @@ class Transport {
virtual bool Disconnect() = 0;
virtual bool TransmitPacket(NetPacket& packet) = 0;
virtual bool ReceivePacket(NetPacket& packet) = 0;
virtual void SetSessionId(uint16_t session_id) = 0;
};

Transport* Transport_Create(int32_t type);
Expand Down
Loading

0 comments on commit 0317272

Please sign in to comment.