Skip to content

Commit

Permalink
Networking #1 - Lobby implemented (#335)
Browse files Browse the repository at this point in the history
* Basic networking functions implemented to get the lobby functional

* Apply suggestions from code review

* PDNetObtainSystemUserName gets local system name

---------

Co-authored-by: Anonymous Maarten <madebr@users.noreply.github.com>
  • Loading branch information
dethrace-labs and madebr committed Jul 5, 2023
1 parent 7d475fc commit 1c10b74
Show file tree
Hide file tree
Showing 34 changed files with 4,199 additions and 1,176 deletions.
8 changes: 8 additions & 0 deletions .github/scripts/build-msvc.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
$ErrorActionPreference = "Stop"

if ($($Env:PLATFORM_ARCH) -eq "x86") {
$sdl_path = "x86"
} else {
Expand All @@ -18,7 +20,13 @@ Expand-Archive $Env:TEMP\SDL2-devel.zip -DestinationPath $Env:TEMP

# build
cmake -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_TESTS=ON "-DSDL2_ROOT_DIR=$($Env:TEMP)\SDL2-$sdl2_version" -B build
if ($LASTEXITCODE -ne 0) {
Exit $LASTEXITCODE
}
cmake --build build --config RelWithDebInfo
if ($LASTEXITCODE -ne 0) {
Exit $LASTEXITCODE
}

# copy SDL2.dll to build folder, so tests can run
cp $Env:TEMP\SDL2-$sdl2_version\lib\$sdl_path\SDL2.dll build
Expand Down
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ endfunction()

test_big_endian(IS_BIGENDIAN)

find_package(SDL2 REQUIRED)
find_package(SDL2 CONFIG)
if(NOT SDL2_FOUND)
find_package(SDL2 MODULE REQUIRED)
endif()

add_subdirectory(lib/libsmacker)
add_subdirectory(lib/glad)
Expand Down
11 changes: 8 additions & 3 deletions src/DETHRACE/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ target_include_directories(dethrace_obj
pd
)

# add_compile_options(-fsanitize=address)
# add_link_options(-fsanitize=address)

target_link_libraries(dethrace_obj PUBLIC SDL2::SDL2 smackw32 harness brender s3)


Expand All @@ -35,6 +38,7 @@ else()
-Wstrict-prototypes
)
endif()
target_compile_definitions(dethrace_obj PRIVATE INSIDE_DETHRACE)
if(DETHRACE_FIX_BUGS)
target_compile_definitions(dethrace_obj PRIVATE DETHRACE_FIX_BUGS)
endif()
Expand Down Expand Up @@ -154,14 +158,15 @@ target_sources(dethrace_obj PRIVATE
common/world.h
constants.h
dr_types.h
pc-dos/dosnet.c
pd/net.h
pc-dos/dossys.c
pd/sys.h
pc-win95/win95sys.c
pc-win95/dinput.h
pc-win95/ssdx.c
pc-win95/ssdx.h
pc-win95/win95net.c
pc-dos/dosnet.c
pc-dos/dossys.c
)

# Create our main game binary.
Expand All @@ -184,7 +189,7 @@ if(NOT MSVC)
else()
target_link_libraries(dethrace PRIVATE dbghelp)
target_link_options(dethrace PRIVATE /subsystem:windows /ENTRY:mainCRTStartup)
target_compile_definitions(dethrace PRIVATE -D_CRT_SECURE_NO_WARNINGS -DSDL_MAIN_HANDLED)
target_compile_definitions(dethrace PRIVATE -D_CRT_SECURE_NO_WARNINGS -DSDL_MAIN_HANDLED -DWIN32_LEAN_AND_MEAN)
endif()

if(DETHRACE_IDE_ROOT_DIR)
Expand Down
54 changes: 39 additions & 15 deletions src/DETHRACE/common/car.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,44 +494,68 @@ void SetInitialPosition(tRace_info* pThe_race, int pCar_index, int pGrid_index)
car = pThe_race->opponent_list[pCar_index].car_spec;
BrMatrix34Identity(&car_actor->t.t.mat);
place_on_grid = 1;
if (gNet_mode && !gCurrent_net_game->options.grid_start && pThe_race->number_of_net_start_points) {
TELL_ME_IF_WE_PASS_THIS_WAY();
if (gNet_mode != eNet_mode_none && !gCurrent_net_game->options.grid_start && pThe_race->number_of_net_start_points != 0) {
start_i = i = IRandomBetween(0, pThe_race->number_of_net_start_points - 1);
do {
PossibleService();
for (j = 0; j < gNumber_of_net_players; j++) {
if (j != pCar_index) {
BrVector3Copy(&real_pos, &pThe_race->opponent_list[j].car_spec->car_master_actor->t.t.translate.t);
if (real_pos.v[0] > 500.f) {
real_pos.v[0] -= 1000.f;
real_pos.v[1] -= 1000.f;
real_pos.v[2] -= 1000.f;
}
BrVector3Sub(&dist, &real_pos, &pThe_race->net_starts[i].pos);
if (BrVector3LengthSquared(&dist) < 16.f) {
break;
}
}
}
if (j == gNumber_of_net_players) {
BrVector3Copy(&car_actor->t.t.translate.t, &pThe_race->net_starts[i].pos);
initial_yaw = BrDegreeToAngle(pThe_race->net_starts[i].yaw);
place_on_grid = 0;
}
i++;
if (i == pThe_race->number_of_net_start_points) {
i = 0;
}
} while (start_i != i);
}
if (place_on_grid) {
initial_yaw = (pThe_race->initial_yaw * 182.0444444444445);
initial_yaw = BrDegreeToAngle(pThe_race->initial_yaw);
BrMatrix34RotateY(&initial_yaw_matrix, initial_yaw);
grid_offset.v[0] = 0.0 - pGrid_index % 2;
grid_offset.v[1] = 0.0;
grid_offset.v[2] = (double)(pGrid_index / 2) * 2.0 + (double)(pGrid_index % 2) * 0.40000001;
grid_offset.v[0] = 0.0f - pGrid_index % 2;
grid_offset.v[1] = 0.0f;
grid_offset.v[2] = (br_scalar)(pGrid_index / 2) * 2.0f + (br_scalar)(pGrid_index % 2) * 0.4f;
BrMatrix34ApplyV(&car_actor->t.t.translate.t, &grid_offset, &initial_yaw_matrix);
BrVector3Accumulate(&car_actor->t.t.translate.t, &pThe_race->initial_position);
}
FindBestY(
&car_actor->t.t.translate.t,
gTrack_actor,
10.0,
10.0f,
&nearest_y_above,
&nearest_y_below,
&above_model,
&below_model,
&above_face_index,
&below_face_index);
if (nearest_y_above == 30000.0) {
if (nearest_y_below == -30000.0) {
car_actor->t.t.translate.t.v[1] = 0.0;
} else {
car_actor->t.t.translate.t.v[1] = nearest_y_below;
}
} else {
if (nearest_y_above != 30000.0f) {
car_actor->t.t.translate.t.v[1] = nearest_y_above;
} else if (nearest_y_below != -30000.0f) {
car_actor->t.t.translate.t.v[1] = nearest_y_below;
} else {
car_actor->t.t.translate.t.v[1] = 0.0f;
}
BrMatrix34PreRotateY(&car_actor->t.t.mat, initial_yaw);
if (gNet_mode) {
BrMatrix34Copy(
&gNet_players[pThe_race->opponent_list[pCar_index].net_player_index].initial_position,
&car->car_master_actor->t.t.mat);
}
if (gNet_mode && car->disabled && car_actor->t.t.translate.t.v[0] < 500.0) {
if (gNet_mode != eNet_mode_none && car->disabled && car_actor->t.t.translate.t.v[0] < 500.0f) {
DisableCar(car);
}
// Enable to start all opponent cars upside down ;)
Expand Down
29 changes: 28 additions & 1 deletion src/DETHRACE/common/controls.c
Original file line number Diff line number Diff line change
Expand Up @@ -2500,7 +2500,34 @@ void InitAbuseomatic(void) {
int i;
int len;
LOG_TRACE("()");
NOT_IMPLEMENTED();

gString[20] = '\0';
PDBuildAppPath(path);
strcat(path, "ABUSE.TXT");
for (i = 0; i < COUNT_OF(gAbuse_text); i++) {
gAbuse_text[i] = NULL;
}
f = fopen(path, "rt");
if (f == NULL) {
return;
}
for (i = 0; i < COUNT_OF(gAbuse_text); i++) {
if (fgets(s, COUNT_OF(s) - 1, f) == NULL) {
break;
}
len = strlen(s);
if (len > 63) {
s[63] = '\0';
}
len = strlen(s);
while (len != 0 && s[len - 1] < ' ') {
s[len - 1] = '\0';
len--;
}
gAbuse_text[i] = BrMemAllocate(strlen(s) + 1, kMem_abuse_text);
strcpy(gAbuse_text[i], s);
}
fclose(f);
}

// IDA: void __cdecl DisposeAbuseomatic()
Expand Down
10 changes: 10 additions & 0 deletions src/DETHRACE/common/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ void dr_dprintf(char* fmt_string, ...) {
return;
}

if (gDiagnostic_file == NULL) {
return;
}

if (first_time == 0) {
first_time = GetTotalTime();
}
Expand All @@ -254,6 +258,12 @@ void dr_dprintf(char* fmt_string, ...) {
va_end(args);
fputs("\n", gDiagnostic_file);
fflush(gDiagnostic_file);

// Added by dethrace for debugging
// va_start(args, fmt_string);
// vprintf(fmt_string, args);
// va_end(args);
// printf("\n");
}

// IDA: int __usercall DoErrorInterface@<EAX>(int pMisc_text_index@<EAX>)
Expand Down
8 changes: 4 additions & 4 deletions src/DETHRACE/common/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ void InitGame(int pStart_race) {

gWaiting_for_unpause = 1;
gWait_for_it = 1;
if (gNet_mode) {
if (gNet_mode != eNet_mode_none) {
gCredit_period = gCredit_period_network[gCurrent_net_game->type];
} else {
gCredit_period = gCredit_period_single[gProgram_state.skill_level];
Expand All @@ -448,10 +448,10 @@ void InitGame(int pStart_race) {
gProgram_state.current_car.power_up_levels[i] = 0;
}
}
for (i = 0; gNumber_of_races > i; ++i) {
for (i = 0; i < gNumber_of_races; i++) {
gRace_list[i].been_there_done_that = 0;
}
for (i = 0; gNumber_of_racers > i; ++i) {
for (i = 0; i < gNumber_of_racers; i++) {
gOpponents[i].dead = 0;
}
gProgram_state.rank = gInitial_rank;
Expand All @@ -470,7 +470,7 @@ void DisposeGameIfNecessary(void) {
int i;
LOG_TRACE("()");

if (gNet_mode) {
if (gNet_mode != eNet_mode_none) {
NetLeaveGame(gCurrent_net_game);
}
if (gGame_initialized) {
Expand Down
44 changes: 20 additions & 24 deletions src/DETHRACE/common/intrface.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,33 +511,29 @@ int DoInterfaceScreen(tInterface_spec* pSpec, int pOptions, int pCurrent_choice)
} else {
result = gCurrent_choice;
}
if (!go_ahead) {
if (!escaped) {
if (pSpec->end_flic_otherwise > 0) {
DRS3StartSound(gEffects_outlet, 3007);
RunFlic(pSpec->end_flic_otherwise);
} else if (pSpec->end_flic_otherwise < 0) {
FadePaletteDown();
}
goto LABEL_230;
} else {
if (pSpec->end_flic_escaped > 0) {
DRS3StartSound(gEffects_outlet, 3007);
RunFlic(pSpec->end_flic_escaped);
} else if (pSpec->end_flic_escaped < 0) {
FadePaletteDown();
}
goto LABEL_230;
if (go_ahead) {
if (pSpec->end_flic_go_ahead > 0) {
DRS3StartSound(gIndexed_outlets[0], 3007);
RunFlic(pSpec->end_flic_go_ahead);
} else if (pSpec->end_flic_go_ahead < 0) {
FadePaletteDown();
}
} else if (escaped) {
if (pSpec->end_flic_escaped > 0) {
DRS3StartSound(gIndexed_outlets[0], 3007);
RunFlic(pSpec->end_flic_escaped);
} else if (pSpec->end_flic_escaped < 0) {
FadePaletteDown();
}
} else {
if (pSpec->end_flic_otherwise > 0) {
DRS3StartSound(gIndexed_outlets[0], 3007);
RunFlic(pSpec->end_flic_otherwise);
} else if (pSpec->end_flic_otherwise < 0) {
FadePaletteDown();
}
}
if (pSpec->end_flic_go_ahead > 0) {
DRS3StartSound(gEffects_outlet, 3007);
RunFlic(pSpec->end_flic_go_ahead);
} else if (pSpec->end_flic_go_ahead < 0) {
FadePaletteDown();
}

LABEL_230:
gProgram_state.dont_save_or_load = 0;
EndMouseCursor();
UnlockInterfaceStuff();
Expand Down
7 changes: 6 additions & 1 deletion src/DETHRACE/common/loading.c
Original file line number Diff line number Diff line change
Expand Up @@ -3411,7 +3411,12 @@ int OriginalCarmaCDinDrive(void) {
// IDA: int __cdecl CarmaCDinDriveOrFullGameInstalled()
int CarmaCDinDriveOrFullGameInstalled(void) {
LOG_TRACE("()");
NOT_IMPLEMENTED();

if (gCD_fully_installed) {
return 1;
} else {
return OriginalCarmaCDinDrive();
}
}

// IDA: void __usercall ReadNetworkSettings(FILE *pF@<EAX>, tNet_game_options *pOptions@<EDX>)
Expand Down
12 changes: 6 additions & 6 deletions src/DETHRACE/common/mainmenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,10 @@ int DoMainMenuInterface(tU32 pTime_out, int pContinue_allowed) {
if (pContinue_allowed) {
gMain_menu_spec = &interface_spec1;
result = DoInterfaceScreen(&interface_spec1, gFaded_palette | 2, 0);
if (result != 7 && result && result != 1 && result != 2) {
RunFlic(12);
} else {
if (result == 0 || result == 1 || result == 2 || result == 7) {
FadePaletteDown();
} else {
RunFlic(12);
}
switch (result) {
case 0:
Expand All @@ -352,10 +352,10 @@ int DoMainMenuInterface(tU32 pTime_out, int pContinue_allowed) {
} else {
interface_spec2.time_out = pTime_out;
result = DoInterfaceScreen(&interface_spec2, gFaded_palette, 0);
if (result != 4 && result != -1) {
RunFlic(32);
} else {
if (result == -1 || result == 4) {
FadePaletteDown();
} else {
RunFlic(32);
}
switch (result) {
case 0:
Expand Down
Loading

0 comments on commit 1c10b74

Please sign in to comment.