Skip to content

Commit

Permalink
Fix bug in jump code
Browse files Browse the repository at this point in the history
that always causes damage during jump.
Attempt to fix drive test.
  • Loading branch information
royfalk committed Nov 2, 2024
1 parent b7ce610 commit ef7d5c3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
1 change: 1 addition & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,7 @@ IF (USE_GTEST)
src/exit_unit_tests.cpp
src/components/tests/energy_container_tests.cpp
src/components/tests/balancing_tests.cpp
src/components/tests/jump_drive_tests.cpp
)

ADD_LIBRARY(vegastrike-testing
Expand Down
6 changes: 3 additions & 3 deletions engine/src/cmd/unit_functions_generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "universe.h"
#include "mount_size.h"
#include "damageable.h"

#include "resource/random_utils.h"

//Various functions that were used in .cpp files that are now included because of
//the temple GameUnit class
Expand Down Expand Up @@ -255,10 +255,10 @@ void DealPossibleJumpDamage(Unit *un) {
static double max_damage = XMLSupport::parse_float(vs_config->getVariable("physics", "max_jump_damage", "100"));

// Also damage multiplier
double chance_to_damage = ((double) rand() / (RAND_MAX)) + 1;
double chance_to_damage = randomDouble() - 0.01;

// If jump drive is fully operational, there's no chance for damage
if(un->jump_drive.Operational() >= chance_to_damage) {
if(un->jump_drive.Percent() >= chance_to_damage) {
return;
}

Expand Down
66 changes: 66 additions & 0 deletions engine/src/components/tests/jump_drive_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* jump_drive_tests.cpp
*
* Copyright (C) 2001-2023 Daniel Horn, Benjamen Meyer, Roy Falk, Stephen G. Tuggy,
* and other Vega Strike contributors.
*
* https://github.com/vegastrike/Vega-Strike-Engine-Source
*
* This file is part of Vega Strike.
*
* Vega Strike is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Vega Strike is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Vega Strike. If not, see <https://www.gnu.org/licenses/>.
*/

#include <gtest/gtest.h>
#include <map>

#include "jump_drive.h"
#include "resource/random_utils.h"
#include "unit_csv_factory.h"

static const std::string upgrades_suffix_string = "__upgrades";
static const std::string jump_drive_string = "jump_drive";

static const std::map<std::string,std::string> jump_drive_map = {
{"Key", "jump_drive__upgrades"},
{"Name", "Interstellar Jump Drive"},
{"Upgrade_Type", "Jump_Drive"},
{"Object_Type", "Upgrade_Replacement"},
{"Textual_Description", "\"@upgrades/jump_drive.png@Jump drive for traveling between stars\"\n"},
{"Mass", "10"},
{"Moment_Of_Inertia", "10"},
{"Jump_Drive_Present", "TRUE"},
{"Jump_Drive_Delay", "1"}
};



// Used to quickly figure out why the code wasn't working properly
TEST(JumpDrive, Damage) {
UnitCSVFactory::LoadUnit(jump_drive_string + upgrades_suffix_string, jump_drive_map);

JumpDrive jump_drive;

jump_drive.Load("", jump_drive_string + upgrades_suffix_string);

jump_drive.DamageByPercent(0.1);

// Check operational drive shouldn't get damage
double chance_to_damage = randomDouble() - 0.01;

std::cout << chance_to_damage << std::endl;
std::cout << jump_drive.Percent() << std::endl;

//EXPECT_FALSE(true);
}

0 comments on commit ef7d5c3

Please sign in to comment.