Skip to content

Commit 1aa8a59

Browse files
committed
tidying and fixes
1 parent c8f7e0a commit 1aa8a59

File tree

10 files changed

+63
-52
lines changed

10 files changed

+63
-52
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# VRSIM
22
Made by dark. If there's any bugs or feature requests message me on discord (genericname4) and I can fix them up. Very much a wip.
3-
4-
Event driven skill system
3+
<br>
4+
Being rewritten in c++ for better architecture with a more event driven esque combat system with skills listening to events their combatant undergoes

cpp-backend-new/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ if(APPLE)
2121
COMMAND brew --prefix
2222
OUTPUT_VARIABLE BREW_PREFIX
2323
OUTPUT_STRIP_TRAILING_WHITESPACE
24+
)
2425

26+
execute_process(
2527
COMMAND xcrun --show-sdk-path
2628
OUTPUT_VARIABLE MACOS_SDK_PATH
2729
OUTPUT_STRIP_TRAILING_WHITESPACE
2830
)
31+
2932
include_directories(${BREW_PREFIX}/include)
3033
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -isysroot ${MACOS_SDK_PATH}")
3134
endif()

cpp-backend-new/include/combatants/UnitType.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#ifndef UNIT_TYPE_HPP
22
#define UNIT_TYPE_HPP
33

4-
enum class UnitType
4+
#include <cstdint>
5+
6+
enum class UnitType : u_int8_t
57
{
68
PIKE,
79
ARCHER,

cpp-backend-new/include/orchestration/SetupPublisherLoader.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "orchestration/CombatPublisher.hpp"
66
#include "utils/SkillParser.hpp"
77
#include <string_view>
8+
#include <nlohmann/json.hpp>
89

910
#ifndef RESOURCE_DIR
1011
#define RESOURCE_DIR "resources"
@@ -30,11 +31,13 @@ class SetupPublisherLoader
3031
*/
3132
void loadPublisher(CombatPublisher& combat_publisher, const CombatantSetup& combatant_setup) const;
3233
private:
34+
3335
static constexpr std::string_view skills_path = RESOURCE_DIR "/skills.json";
3436
json commander_data;
3537
json skill_data;
3638
json mount_slot_1_data;
3739
json mount_slot_2_data;
40+
/*
3841
3942
std::vector<Skill> loadSkills(const json& skill_data, CommanderName commander_name, CombatPublisher& combat_publisher) const;
4043
std::vector<Skill> loadSkills(const json& skill_data, SkillName skill_name, CombatPublisher& combat_publisher) const;
@@ -52,6 +55,7 @@ class SetupPublisherLoader
5255
5356
// Determine skill target from JSON (FRIENDLY, ENEMY, etc.)
5457
SkillTarget determineSkillTarget(const json& skill_json) const;
58+
*/
5559
};
5660

5761
#endif
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#ifndef NUMBER_GENERATOR_HPP
22
#define NUMBER_GENERATOR_HPP
33

4+
#include <array>
45
#include <cstdint>
56

67
class NumberGenerator
78
{
89
public:
9-
NumberGenerator(uint64_t seed);
10+
explicit NumberGenerator(uint64_t seed);
1011
/**
1112
* @brief creates a random uniform double between [0,1)
1213
*
@@ -15,11 +16,13 @@ class NumberGenerator
1516
[[nodiscard]] double getRandomDouble();
1617
private:
1718
// used internally for bitshifting an int
18-
static uint64_t rotl(const uint64_t x, int k);
19+
static uint64_t rotl(const uint64_t value, int shift);
20+
21+
static uint64_t splitmix64(uint64_t& seed);
1922

2023
void jump();
21-
// used to store internal state
22-
uint64_t s[2];
24+
// used to store internal state of RNG
25+
std::array<uint64_t, 2> state;
2326
};
2427

2528
#endif

cpp-backend-new/include/utils/ScalarUtils.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,15 @@ namespace ScalarUtils
3636
// troop scalar
3737
if (troops >= 100000)
3838
{
39-
return (troops - 100000) * GRADIENT_100K_RISE + GRADIENT_100K_CONSTANT;
39+
return ((troops - 100000) * GRADIENT_100K_RISE) + GRADIENT_100K_CONSTANT;
4040
}
41-
else if (troops > 10000)
41+
42+
if (troops > 10000)
4243
{
43-
return (troops - 10000) * GRADIENT_10K_100K_RISE + GRADIENT_10K_100K_CONSTANT;
44-
}
45-
else
46-
{
47-
return LINEAR_FACTOR_0K_10K * std::pow(troops, POWER_FACTOR_0K_10K);
44+
return ((troops - 10000) * GRADIENT_10K_100K_RISE) + GRADIENT_10K_100K_CONSTANT;
4845
}
46+
47+
return LINEAR_FACTOR_0K_10K * std::pow(troops, POWER_FACTOR_0K_10K);
4948
}
5049
}
5150

cpp-backend-new/include/utils/SkillParser.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class SkillParser
2727
public:
2828

2929
// turns a skill name into a vector of actual skill objects
30-
[[nodiscard]] std::vector<std::unique_ptr<Skill>> loadSkills(const json& skill_data, CommanderName commander_name, bool isPrimary) const;
30+
[[nodiscard]] std::vector<std::unique_ptr<Skill>> loadSkills(const json& skill_data, CommanderName commander_name, bool is_primary) const;
3131
[[nodiscard]] std::vector<std::unique_ptr<Skill>> loadSkills(const json& skill_data, SkillName skill_name) const;
3232
[[nodiscard]] std::vector<std::unique_ptr<Skill>> loadSkills(const json& skill_data, MountSlot1Names mount_slot_1_names) const;
3333
[[nodiscard]] std::vector<std::unique_ptr<Skill>> loadSkills(const json& skill_data, MountSlot2Names mount_slot_2_names) const;

cpp-backend-new/src/orchestration/SetupPublisherLoader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ void SetupPublisherLoader::loadPublisher(CombatPublisher& combat_publisher, cons
4040
// Load skills for each commander
4141
for (int i = 0; i < CombatantSetup::NUM_COMMANDERS; ++i) {
4242

43-
const bool isPrimary { i == 0 };
43+
const bool is_primary { i == 0 };
4444

45-
auto skills = parser.loadSkills(skill_data, combatant_setup.commanders[i], isPrimary);
45+
auto skills = parser.loadSkills(skill_data, combatant_setup.commanders[i], is_primary);
4646
for (auto& skill : skills)
4747
{
4848
combat_publisher.addSkill(std::move(skill));

cpp-backend-new/src/skills/NumberGenerator.cpp

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
NumberGenerator::NumberGenerator(uint64_t seed)
66
{
7-
s[0] = splitmix64(seed);
8-
s[1] = splitmix64(seed);
7+
state[0] = splitmix64(seed);
8+
state[1] = splitmix64(seed);
99
// suppress the warning with jump calling it
1010
std::ignore = getRandomDouble();
1111
}
@@ -17,31 +17,31 @@ NumberGenerator::NumberGenerator(uint64_t seed)
1717
*/
1818
double NumberGenerator::getRandomDouble()
1919
{
20-
const uint64_t s0 = s[0];
21-
uint64_t s1 = s[1];
20+
const uint64_t state0 = state[0];
21+
uint64_t state1 = state[1];
2222

2323
// create random number using the internal state
24-
const uint64_t result = s0 + s1;
24+
const uint64_t result = state0 + state1;
2525

2626
// mix up the internal state again
27-
s1 ^= s0;
28-
s[0] = rotl(s0, 24) ^ s1 ^ (s1 << 16); // a, b
29-
s[1] = rotl(s1, 37); // c
27+
state1 ^= state0;
28+
state[0] = rotl(state0, 24) ^ state1 ^ (state1 << 16); // a, b
29+
state[1] = rotl(state1, 37); // c
3030

3131
// return, shifting result into [0,1)
32-
return (result >> 11) * (1.0 / (1ULL << 53));;
32+
return (result >> 11) * (1.0 / (1ULL << 53));
3333
}
3434

3535
/**
3636
* @brief bit shifts x
3737
*
38-
* @param x int to shift
39-
* @param k bits to shift by
38+
* @param value int to shift
39+
* @param shift bits to shift by
4040
* @return uint64_t
4141
*/
42-
inline uint64_t NumberGenerator::rotl(const uint64_t x, int k)
42+
uint64_t NumberGenerator::rotl(uint64_t value, int shift)
4343
{
44-
return (x << k) | (x >> (64 - k));
44+
return (value << shift) | (value >> (64 - shift));
4545
}
4646

4747

@@ -51,29 +51,29 @@ inline uint64_t NumberGenerator::rotl(const uint64_t x, int k)
5151

5252
void NumberGenerator::jump()
5353
{
54-
static const uint64_t JUMP[] = { 0xdf900294d8f554a5, 0x170865df4b3201fc };
54+
static const std::array<uint64_t, 2> JUMP = { 0xdf900294d8f554a5, 0x170865df4b3201fc };
5555

56-
uint64_t s0 = 0;
57-
uint64_t s1 = 0;
58-
for(int i = 0; i < sizeof JUMP / sizeof *JUMP; i++)
56+
uint64_t state0 = 0;
57+
uint64_t state1 = 0;
58+
for(const auto& jump_val : JUMP)
5959
{
60-
for(int b = 0; b < 64; b++) {
61-
if (JUMP[i] & UINT64_C(1) << b) {
62-
s0 ^= s[0];
63-
s1 ^= s[1];
60+
for(int bit_idx = 0; bit_idx < 64; bit_idx++) {
61+
if ((jump_val & (UINT64_C(1) << bit_idx)) != 0) {
62+
state0 ^= state[0];
63+
state1 ^= state[1];
6464
}
65-
getRandomDouble();
65+
std::ignore = getRandomDouble();
6666
}
6767
}
6868

69-
s[0] = s0;
70-
s[1] = s1;
69+
state[0] = state0;
70+
state[1] = state1;
7171
}
7272

73-
uint64_t splitmix64(uint64_t& seed)
73+
uint64_t NumberGenerator::splitmix64(uint64_t& seed)
7474
{
75-
uint64_t z = (seed += 0x9E3779B97F4A7C15ULL);
76-
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
77-
z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
78-
return z ^ (z >> 31);
75+
uint64_t result = (seed += 0x9E3779B97F4A7C15ULL);
76+
result = (result ^ (result >> 30)) * 0xBF58476D1CE4E5B9ULL;
77+
result = (result ^ (result >> 27)) * 0x94D049BB133111EBULL;
78+
return result ^ (result >> 31);
7979
}

cpp-backend-new/tests/TestSkillParser.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ TEST(SkillParserTest, LoadSkillsForFakeSigrid) {
4545
}
4646
)"_json;
4747

48-
bool isPrimary { true };
49-
auto skills = parser.loadSkills(skillJson, CommanderName::Sigrid, isPrimary);
48+
bool is_primary { true };
49+
auto skills = parser.loadSkills(skillJson, CommanderName::Sigrid, is_primary);
5050

5151
ASSERT_EQ(skills.size(), 2);
5252

@@ -71,14 +71,14 @@ TEST(SkillParserTest, LoadSkillsThrowsOnInvalidSkill) {
7171
}
7272
)"_json;
7373

74-
bool isPrimary { true };
75-
EXPECT_THROW(static_cast<void>(parser.loadSkills(skillJson, CommanderName::Sigrid, isPrimary)), std::runtime_error);
74+
bool is_primary { true };
75+
EXPECT_THROW(static_cast<void>(parser.loadSkills(skillJson, CommanderName::Sigrid, is_primary)), std::runtime_error);
7676
}
7777

7878
TEST(SkillParserTest, LoadSkillsThrowsOnUnknownCommander) {
7979
SkillParser parser;
8080
json skillJson = R"({"Commanders": {}})"_json;
8181

82-
bool isPrimary { true };
83-
EXPECT_THROW(static_cast<void>(parser.loadSkills(skillJson, CommanderName::Sigrid, isPrimary)), std::runtime_error);
82+
bool is_primary { true };
83+
EXPECT_THROW(static_cast<void>(parser.loadSkills(skillJson, CommanderName::Sigrid, is_primary)), std::runtime_error);
8484
}

0 commit comments

Comments
 (0)