Skip to content

Commit ef99aac

Browse files
committed
Apply clang-tidy fixes
Move fixed point LUT generator scripts to misc/scripts
1 parent f2d1150 commit ef99aac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+592
-1383
lines changed

misc/scripts/exp_lut_generator.py

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/usr/bin/env python
2+
import os
3+
from argparse import ArgumentParser
4+
from math import e
5+
from typing import List
6+
7+
BIT_COUNT = 64
8+
MAX_VALUE = 2**BIT_COUNT
9+
10+
DEFAULT_DIVISOR_BASE = 2
11+
DEFAULT_DIVISOR_POWER = 16
12+
DEFAULT_EXP_BASE = e
13+
14+
15+
def generate_exp_lut(divisor_base: int, divisor_power: int, exp_base: float):
16+
divisor: int = divisor_base**divisor_power
17+
18+
exp_lut: List[int] = []
19+
20+
for index in range(BIT_COUNT):
21+
exponent = (2**index) / divisor
22+
value = int((exp_base**exponent) * divisor + 0.5)
23+
if value > MAX_VALUE:
24+
break
25+
exp_lut.append(value)
26+
27+
lut_identifier: str = (
28+
f"LUT_{divisor_base}_{divisor_power}_EXP_{'e' if exp_base == e else ('%g' % exp_base).replace('.', 'p')}"
29+
)
30+
lut_size: int = len(exp_lut)
31+
32+
generated_options = ""
33+
if (
34+
DEFAULT_DIVISOR_BASE is not divisor_base
35+
or DEFAULT_DIVISOR_POWER is not divisor_power
36+
or DEFAULT_EXP_BASE is not exp_base
37+
):
38+
generated_options += " with `"
39+
40+
if DEFAULT_DIVISOR_BASE is not divisor_base:
41+
generated_options += f"-b {divisor_base}"
42+
if DEFAULT_DIVISOR_POWER is not divisor_power or DEFAULT_EXP_BASE is not exp_base:
43+
generated_options += " "
44+
45+
if DEFAULT_DIVISOR_POWER is not divisor_power:
46+
generated_options += f"-p {divisor_power}"
47+
if DEFAULT_EXP_BASE is not exp_base:
48+
generated_options += " "
49+
50+
if DEFAULT_EXP_BASE is not exp_base:
51+
generated_options += f"-e {exp_base:g}"
52+
53+
generated_options += "`"
54+
55+
source: str = f"""// This file was generated using the `misc/scripts/exp_lut_generator.py` script{generated_options}.
56+
57+
#pragma once
58+
59+
#include <array>
60+
#include <cstddef>
61+
#include <cstdint>
62+
63+
static constexpr int64_t {lut_identifier}_DIVISOR = {divisor};
64+
static constexpr size_t {lut_identifier}_SIZE = {lut_size};
65+
66+
static constexpr std::array<int64_t, {lut_identifier}_SIZE> {lut_identifier} {{
67+
"""
68+
69+
for value in exp_lut[:-1]:
70+
source += f"\t{value},\n"
71+
72+
source += f"\t{exp_lut[-1]}\n"
73+
source += "};\n"
74+
75+
fixed_point_lut_path: str = os.path.join(
76+
os.path.dirname(__file__), f"../../src/openvic-simulation/types/fixed_point/FixedPoint{lut_identifier}.hpp"
77+
)
78+
with open(fixed_point_lut_path, "w", newline="\n") as file:
79+
file.write(source)
80+
81+
print(f"`FixedPoint{lut_identifier}.hpp` generated successfully.")
82+
83+
84+
if __name__ == "__main__":
85+
parser = ArgumentParser(
86+
prog="Fixed Point Exp LUT Generator", description="Fixed-Point Exponential Look-Up Table generator"
87+
)
88+
parser.add_argument(
89+
"-b",
90+
"--base",
91+
type=int,
92+
default=DEFAULT_DIVISOR_BASE,
93+
choices=range(2, 65),
94+
help="The base of the fixed point divisor",
95+
)
96+
parser.add_argument(
97+
"-p",
98+
"--power",
99+
type=int,
100+
default=DEFAULT_DIVISOR_POWER,
101+
choices=range(1, 65),
102+
help="The power of the fixed point divisor",
103+
)
104+
parser.add_argument(
105+
"-e",
106+
"--exp",
107+
type=float,
108+
default=DEFAULT_EXP_BASE,
109+
help="The base of the exponential function the look-up table represents",
110+
)
111+
args = parser.parse_args()
112+
113+
generate_exp_lut(args.base, args.power, args.exp)
114+
exit(0)

misc/scripts/sin_lut_generator.py

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python
2+
import os
3+
from argparse import ArgumentParser
4+
from math import pi, sin
5+
6+
DEFAULT_PRECISION = 16
7+
DEFAULT_COUNT = 9
8+
9+
10+
def generate_sin_lut(precision: int, count_log2: int):
11+
one = 1 << precision
12+
count = 1 << count_log2
13+
14+
SinLut = []
15+
16+
for i in range(count):
17+
angle = 2 * pi * i / count
18+
19+
sin_value = sin(angle)
20+
moved_sin = sin_value * one
21+
rounded_sin = int(moved_sin + 0.5) if moved_sin > 0 else int(moved_sin - 0.5)
22+
SinLut.append(rounded_sin)
23+
24+
SinLut.append(SinLut[0])
25+
26+
generated_options = ""
27+
if DEFAULT_PRECISION is not precision or DEFAULT_COUNT is not count_log2:
28+
generated_options += " with `"
29+
30+
if DEFAULT_PRECISION is not precision:
31+
generated_options += f"-p {precision}"
32+
if DEFAULT_COUNT is not count_log2:
33+
generated_options += " "
34+
35+
if DEFAULT_COUNT is not count_log2:
36+
generated_options += f"-c {count_log2}"
37+
38+
generated_options += "`"
39+
40+
source = f"""// This file was generated using the `misc/scripts/sin_lut_generator.py` script{generated_options}.
41+
42+
#pragma once
43+
44+
#include <cstdint>
45+
46+
static constexpr int32_t SIN_LUT_PRECISION = {precision};
47+
static constexpr int32_t SIN_LUT_COUNT_LOG2 = {count_log2};
48+
static constexpr int32_t SIN_LUT_SHIFT = SIN_LUT_PRECISION - SIN_LUT_COUNT_LOG2;
49+
50+
static constexpr int64_t SIN_LUT[(1 << SIN_LUT_COUNT_LOG2) + 1] = {{
51+
"""
52+
53+
VALS_PER_LINE = 16
54+
55+
lines = [SinLut[i : i + VALS_PER_LINE] for i in range(0, len(SinLut), VALS_PER_LINE)]
56+
57+
for line in lines[:-1]:
58+
source += f"\t{', '.join(str(value) for value in line)},\n"
59+
60+
source += f"\t{', '.join(str(value) for value in lines[-1])}\n"
61+
source += "};\n"
62+
63+
fixed_point_sin_path: str = os.path.join(
64+
os.path.dirname(__file__), "../../src/openvic-simulation/types/fixed_point/FixedPointLUT_sin.hpp"
65+
)
66+
with open(fixed_point_sin_path, "w", newline="\n") as file:
67+
file.write(source)
68+
69+
print("`FixedPointLUT_sin.hpp` generated successfully.")
70+
71+
72+
if __name__ == "__main__":
73+
parser = ArgumentParser(prog="Fixed Point Sin LUT Generator", description="Fixed-Point Sin Look-Up Table generator")
74+
parser.add_argument(
75+
"-p",
76+
"--precision",
77+
type=int,
78+
default=DEFAULT_PRECISION,
79+
choices=range(1, 65),
80+
help="The number of bits after the point (fractional bits)",
81+
)
82+
parser.add_argument(
83+
"-c",
84+
"--count",
85+
type=int,
86+
default=DEFAULT_COUNT,
87+
choices=range(1, 65),
88+
help="The base 2 log of the number of values in the look-up table (must be <= precision)",
89+
)
90+
args = parser.parse_args()
91+
92+
if args.precision < args.count:
93+
print("ERROR: invalid count ", args.count, " - can't be greater than precision (", args.precision, ")")
94+
exit(-1)
95+
else:
96+
generate_sin_lut(args.precision, args.count)
97+
exit(0)

src/openvic-simulation/dataloader/Dataloader.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Dataloader::path_vector_t Dataloader::_lookup_files_in_dir(
108108
path_vector_t ret;
109109
struct file_entry_t {
110110
fs::path file;
111-
fs::path const* root;
111+
fs::path const* root = nullptr;
112112
};
113113
string_map_t<file_entry_t> found_files;
114114
for (fs::path const& root : roots) {

src/openvic-simulation/dataloader/NodeTools.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,10 @@ using namespace std::string_view_literals;
236236
ONE_OR_MORE = _MUST_APPEAR | _CAN_REPEAT
237237
} expected_count;
238238
node_callback_t callback;
239-
size_t count;
239+
size_t count = 0;
240240

241241
dictionary_entry_t(expected_count_t new_expected_count, node_callback_t&& new_callback)
242-
: expected_count { new_expected_count }, callback { MOV(new_callback) }, count { 0 } {}
242+
: expected_count { new_expected_count }, callback { MOV(new_callback) } {}
243243

244244
constexpr bool must_appear() const {
245245
return static_cast<uint8_t>(expected_count) & static_cast<uint8_t>(expected_count_t::_MUST_APPEAR);

src/openvic-simulation/dataloader/Vic2PathSearch_Windows.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#ifdef _WIN32
23

34
#include <concepts>
45
#pragma comment(lib, "advapi32.lib")
@@ -149,7 +150,7 @@ namespace OpenVic::Windows {
149150
};
150151

151152
template<either_char_type RCHAR_T, either_char_type CHAR_T, either_char_type CHAR_T2>
152-
std::basic_string<RCHAR_T> ReadRegValue(
153+
std::basic_string<RCHAR_T> ReadRegValue( //
153154
HKEY root, std::basic_string_view<CHAR_T> key, std::basic_string_view<CHAR_T2> name
154155
) {
155156
RegistryKey registry_key(root, key, name);
@@ -168,3 +169,4 @@ namespace OpenVic::Windows {
168169
return ReadRegValue<RCHAR_T>(root, key_sv, name_sv);
169170
}
170171
}
172+
#endif

src/openvic-simulation/defines/AIDefines.cpp

+1-40
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,7 @@
33
using namespace OpenVic;
44
using namespace OpenVic::NodeTools;
55

6-
AIDefines::AIDefines()
7-
: colony_weight {},
8-
administrator_weight {},
9-
industryworker_weight {},
10-
educator_weight {},
11-
soldier_weight {},
12-
soldier_fraction {},
13-
capitalist_fraction {},
14-
production_weight {},
15-
spam_penalty {},
16-
one_side_max_warscore {},
17-
pop_project_investment_max_budget_factor {},
18-
relation_limit_no_alliance_offer {},
19-
naval_supply_penalty_limit {},
20-
chance_build_railroad {},
21-
chance_build_naval_base {},
22-
chance_build_fort {},
23-
chance_invest_pop_proj {},
24-
chance_foreign_invest {},
25-
tws_awareness_score_low_cap {},
26-
tws_awareness_score_aspect {},
27-
peace_base_reluctance {},
28-
peace_time_duration {},
29-
peace_time_factor {},
30-
peace_time_factor_no_goals {},
31-
peace_war_exhaustion_factor {},
32-
peace_war_direction_factor {},
33-
peace_war_direction_winning_mult {},
34-
peace_force_balance_factor {},
35-
peace_ally_base_reluctance_mult {},
36-
peace_ally_time_mult {},
37-
peace_ally_war_exhaustion_mult {},
38-
peace_ally_war_direction_mult {},
39-
peace_ally_force_balance_mult {},
40-
aggression_base {},
41-
aggression_unciv_bonus {},
42-
fleet_size {},
43-
min_fleets {},
44-
max_fleets {},
45-
time_before_disband {} {}
6+
AIDefines::AIDefines() : fleet_size {}, min_fleets {}, max_fleets {} {}
467

478
std::string_view AIDefines::get_name() const {
489
return "ai";

src/openvic-simulation/defines/CountryDefines.cpp

+1-87
Original file line numberDiff line numberDiff line change
@@ -4,93 +4,7 @@ using namespace OpenVic;
44
using namespace OpenVic::NodeTools;
55

66
CountryDefines::CountryDefines()
7-
: nationalism_duration {},
8-
rebels_hold_capital_success_duration {}, // NOT USED
9-
rebel_success_duration {},
10-
base_country_tax_efficiency {},
11-
base_country_admin_efficiency {},
12-
gold_to_cash_rate {},
13-
gold_to_worker_pay_rate {},
14-
great_power_rank {},
15-
lose_great_power_grace_days {},
16-
infamy_containment_limit {},
17-
max_bureaucracy_percentage {},
18-
bureaucracy_percentage_increment {},
19-
min_crimefight_percent {},
20-
max_crimefight_percent {},
21-
admin_efficiency_crimefight_percent {},
22-
conservative_increase_after_reform {},
23-
campaign_event_base_duration {},
24-
campaign_event_min_duration {}, // NOT USED
25-
campaign_event_state_duration_modifier {}, // NOT USED
26-
campaign_duration {},
27-
secondary_power_rank {},
28-
colony_to_state_prestige_gain {},
29-
colonial_liferating {},
30-
base_greatpower_daily_influence {},
31-
ai_support_reform {},
32-
base_monthly_diplopoints {},
33-
diplomat_travel_duration {},
34-
province_overseas_penalty {},
35-
noncore_tax_penalty {},
36-
base_tariff_efficiency {},
37-
colony_formed_prestige {},
38-
created_cb_valid_time {},
39-
loyalty_boost_on_party_win {},
40-
movement_radicalism_base {},
41-
movement_radicalism_passed_reform_effect {},
42-
movement_radicalism_nationalism_factor {},
43-
suppression_points_gain_base {},
44-
suppress_bureaucrat_factor {},
45-
wrong_reform_militancy_impact {},
46-
suppression_radicalisation_hit {},
47-
country_investment_industrial_score_factor {},
48-
unciv_tech_spread_max {},
49-
unciv_tech_spread_min {},
50-
min_delay_duration_between_reforms {},
51-
economic_reform_uh_factor {},
52-
military_reform_uh_factor {},
53-
wrong_reform_radical_impact {},
54-
tech_year_span {},
55-
tech_factor_vassal {},
56-
max_suppression {},
57-
prestige_hit_on_break_country {},
58-
min_mobilize_limit {},
59-
pop_growth_country_cache_days {},
60-
newspaper_printing_frequency {},
61-
newspaper_timeout_period {},
62-
newspaper_max_tension {},
63-
naval_base_supply_score_base {},
64-
naval_base_supply_score_empty {},
65-
naval_base_non_core_supply_score {},
66-
colonial_points_from_supply_factor {},
67-
colonial_points_for_non_core_base {},
68-
mobilization_speed_base {},
69-
mobilization_speed_rails_mult {},
70-
colonization_interest_lead {},
71-
colonization_influence_lead {},
72-
colonization_duration {},
73-
colonization_days_between_investment {},
74-
colonization_days_for_initial_investment {},
75-
colonization_protectorate_province_maintenance {},
76-
colonization_colony_province_maintenance {},
77-
colonization_colony_industry_maintenance {},
78-
colonization_colony_railway_maintenance {},
79-
colonization_interest_cost_initial {},
80-
colonization_interest_cost_neighbor_modifier {},
81-
colonization_interest_cost {},
82-
colonization_influence_cost {},
83-
colonization_extra_guard_cost {},
84-
colonization_release_dominion_cost {},
85-
colonization_create_state_cost {},
86-
colonization_create_protectorate_cost {},
87-
colonization_create_colony_cost {},
88-
colonization_colony_state_distance {},
89-
colonization_influence_temperature_per_day {},
90-
colonization_influence_temperature_per_level {},
91-
party_loyalty_hit_on_war_loss {},
92-
research_points_on_conquer_mult {},
93-
max_research_points {} {}
7+
: great_power_rank {}, secondary_power_rank {}, colonial_liferating {}, min_mobilize_limit {} {}
948

959
std::string_view CountryDefines::get_name() const {
9610
return "country";

0 commit comments

Comments
 (0)