Skip to content

Commit

Permalink
string_view changes + general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Hop311 committed Aug 12, 2023
1 parent 4c43951 commit fe74604
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 79 deletions.
2 changes: 1 addition & 1 deletion extension/src/GameSingleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using namespace godot;
using namespace OpenVic;

TerrainVariant::TerrainVariant(std::string const& new_identfier,
TerrainVariant::TerrainVariant(const std::string_view new_identfier,
colour_t new_colour, Ref<Image> const& new_image)
: HasIdentifierAndColour { new_identfier, new_colour, true },
image { new_image } {}
Expand Down
2 changes: 1 addition & 1 deletion extension/src/GameSingleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace OpenVic {
private:
const godot::Ref<godot::Image> image;

TerrainVariant(std::string const& new_identfier, colour_t new_colour,
TerrainVariant(const std::string_view new_identfier, colour_t new_colour,
godot::Ref<godot::Image> const& new_image);
public:
static constexpr size_t MAX_INDEX = 1 << (8 * sizeof(Map::terrain_t));
Expand Down
80 changes: 40 additions & 40 deletions extension/src/LoadGameCompatibility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,57 @@ Error GameSingleton::_load_province_identifier_file_compatibility_mode(String co
Error err = FileAccess::get_open_error();
if (err != OK || file.is_null()) {
UtilityFunctions::push_error("Failed to load compatibility mode province identifier file: ", file_path);
return err == OK ? FAILED : err;
}

int line_number = 0;
while (!file->eof_reached()) {
const PackedStringArray line = file->get_csv_line(";");
line_number++;
if (err == OK) err = FAILED;
} else {
int line_number = 0;
while (!file->eof_reached()) {
const PackedStringArray line = file->get_csv_line(";");
line_number++;

if (line.is_empty() || (line.size() == 1 && line[0].is_empty()))
continue;
if (line.is_empty() || (line.size() == 1 && line[0].is_empty()))
continue;

if (line_number < 2) continue; // skip header line
index_t id = NULL_INDEX;
colour_t colour = NULL_COLOUR;
if (line.size() > 0) {
if (line[0].is_empty()) {
id = game_manager.map.get_province_count() + 1;
} else if (line[0].is_valid_int()) {
const int64_t val = line[0].to_int();
if (val > NULL_INDEX && val <= MAX_INDEX) id = val;
}
for (int i = 1; i < 4; ++i) {
if (line.size() > i) {
if (line[i].is_valid_int()) {
const int64_t int_val = line[i].to_int();
if (int_val >= NULL_COLOUR && int_val <= FULL_COLOUR) {
colour = (colour << 8) | int_val;
continue;
}
} else if (line[i].is_valid_float()) {
const double double_val = line[i].to_float();
if (std::trunc(double_val) == double_val) {
const int64_t int_val = double_val;
if (line_number < 2) continue; // skip header line
index_t id = NULL_INDEX;
colour_t colour = NULL_COLOUR;
if (line.size() > 0) {
if (line[0].is_empty()) {
id = game_manager.map.get_province_count() + 1;
} else if (line[0].is_valid_int()) {
const int64_t val = line[0].to_int();
if (val > NULL_INDEX && val <= MAX_INDEX) id = val;
}
for (int i = 1; i < 4; ++i) {
if (line.size() > i) {
if (line[i].is_valid_int()) {
const int64_t int_val = line[i].to_int();
if (int_val >= NULL_COLOUR && int_val <= FULL_COLOUR) {
colour = (colour << 8) | int_val;
continue;
}
} else if (line[i].is_valid_float()) {
const double double_val = line[i].to_float();
if (std::trunc(double_val) == double_val) {
const int64_t int_val = double_val;
if (int_val >= NULL_COLOUR && int_val <= FULL_COLOUR) {
colour = (colour << 8) | int_val;
continue;
}
}
}
}
colour = NULL_COLOUR;
break;
}
colour = NULL_COLOUR;
break;
}
if (id == NULL_INDEX || colour == NULL_COLOUR) {
UtilityFunctions::push_error("Invalid province ID-colour entry \"", line, "\" on line ", line_number, " in file: ", file_path);
err = FAILED;
continue;
}
static const std::string province_prefix = "PROV";
if (game_manager.map.add_province(province_prefix + std::to_string(id), colour) != SUCCESS) err = FAILED;
}
if (id == NULL_INDEX || colour == NULL_COLOUR) {
UtilityFunctions::push_error("Invalid province ID-colour entry \"", line, "\" on line ", line_number, " in file: ", file_path);
err = FAILED;
continue;
}
static const std::string province_prefix = "PROV";
if (game_manager.map.add_province(province_prefix + std::to_string(id), colour) != SUCCESS) err = FAILED;
}
game_manager.map.lock_provinces();
return err;
Expand Down
12 changes: 8 additions & 4 deletions extension/src/LoadGameOpenVic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ Error GameSingleton::_parse_region_entry(String const& identifier, Variant const
UtilityFunctions::push_error("Invalid province list for region \"", identifier, "\": ", entry);
return FAILED;
}
return ERR(game_manager.map.add_region(godot_to_std_string(identifier), province_identifiers));
std::vector<std::string_view> province_identifier_views;
for (std::string const& str : province_identifiers) {
province_identifier_views.push_back(str);
}
return ERR(game_manager.map.add_region(godot_to_std_string(identifier), province_identifier_views));
}

Error GameSingleton::_load_region_file(String const& file_path) {
Expand Down Expand Up @@ -271,7 +275,7 @@ Error GameSingleton::_load_map_images(String const& province_image_path, String

// Generate interleaved province and terrain ID image
if (game_manager.map.generate_province_shape_image(province_dims.x, province_dims.y, province_image->get_data().ptr(),
terrain_image->get_data().ptr(), terrain_variant_map) != SUCCESS) err = FAILED;
terrain_image->get_data().ptr(), terrain_variant_map, true) != SUCCESS) err = FAILED;

static constexpr int32_t GPU_DIM_LIMIT = 0x3FFF;
// For each dimension of the image, this finds the small number of equal subdivisions required get the individual texture dims under GPU_DIM_LIMIT
Expand Down Expand Up @@ -364,8 +368,8 @@ Error GameSingleton::_parse_good_entry(String const& identifier, Variant const&
if (var_overseas_maintenance.get_type() == Variant::BOOL) overseas_maintenance = var_overseas_maintenance;
else UtilityFunctions::push_error("Invalid good overseas maintenance bool value for ", identifier, ": ", var_overseas_maintenance);

return ERR(game_manager.good_manager.add_good(godot_to_std_string(identifier), godot_to_std_string(category),
colour, base_price, default_available, tradeable, currency, overseas_maintenance));
return ERR(game_manager.good_manager.add_good(godot_to_std_string(identifier), colour, godot_to_std_string(category),
base_price, default_available, tradeable, currency, overseas_maintenance));
}

Error GameSingleton::_load_goods(String const& defines_path, String const& icons_dir_path) {
Expand Down
10 changes: 5 additions & 5 deletions game/src/Game/Autoload/GuiScale.gd
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func _ready():

func has_guiscale(guiscale_value : float) -> bool:
return guiscale_value in _guiscales

func add_guiscale(guiscale_value: float, guiscale_name: StringName=&"") -> bool:
if has_guiscale(guiscale_value): return true
var scale_dict := { value = guiscale_value }
Expand All @@ -39,24 +39,24 @@ func add_guiscale(guiscale_value: float, guiscale_name: StringName=&"") -> bool:
return false
_guiscales[guiscale_value] = scale_dict
return true

#returns floats
func get_guiscale_value_list() -> Array:
var list := _guiscales.keys()
list.sort_custom(func(a, b): return a > b)
return list

func get_guiscale_display_name(guiscale_value : float) -> StringName:
return _guiscales.get(guiscale_value, {display_name = &"unknown gui scale"}).display_name

func get_current_guiscale() -> float:
return get_tree().root.content_scale_factor

func set_guiscale(guiscale:float) -> void:
print("New GUI scale: %f" % guiscale)
if not has_guiscale(guiscale):
push_warning("Setting GUI Scale to non-standard value %sx" % [guiscale])
get_tree().root.content_scale_factor = guiscale

func reset_guiscale() -> void:
set_guiscale(get_current_guiscale())
9 changes: 0 additions & 9 deletions game/src/Game/GameStart.gd
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,3 @@ func _initialize_game() -> void:

func _on_splash_container_splash_end():
loading_screen.show()

func _on_loading_screen_load_started():
Events.Loader.startup_load_begun.emit()

func _on_loading_screen_load_changed(percentage : float) -> void:
Events.Loader.startup_load_changed.emit(percentage)

func _on_loading_screen_load_ended():
Events.Loader.startup_load_ended.emit()
3 changes: 0 additions & 3 deletions game/src/Game/GameStart.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,5 @@ stream = ExtResource("5_8euyy")
autoplay = true
expand = true

[connection signal="load_changed" from="LoadingScreen" to="." method="_on_loading_screen_load_changed"]
[connection signal="load_ended" from="LoadingScreen" to="." method="_on_loading_screen_load_ended"]
[connection signal="load_started" from="LoadingScreen" to="." method="_on_loading_screen_load_started"]
[connection signal="splash_end" from="SplashContainer" to="." method="_on_splash_container_splash_end"]
[connection signal="finished" from="SplashContainer/SplashVideo" to="SplashContainer" method="_on_splash_startup_finished"]
10 changes: 3 additions & 7 deletions game/src/Game/LoadingScreen.gd
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
extends Control

signal load_started()
signal load_changed(percentage : float)
signal load_ended()

@export var quote_file_path : String = "res://common/quotes.txt"

@export_subgroup("Nodes")
Expand All @@ -24,7 +20,7 @@ func start_loading_screen(thread_safe_function : Callable) -> void:
thread.wait_to_finish()

thread.start(thread_safe_function)
load_started.emit()
Events.Loader.startup_load_begun.emit()

func try_update_loading_screen(percent_complete: float, quote_should_change = false):
# forces the function to behave as if deferred
Expand All @@ -34,9 +30,9 @@ func try_update_loading_screen(percent_complete: float, quote_should_change = fa
quote_label.text = quotes[randi() % quotes.size()]
if is_equal_approx(percent_complete, 100):
thread.wait_to_finish()
load_ended.emit()
Events.Loader.startup_load_ended.emit()
else:
load_changed.emit(percent_complete)
Events.Loader.startup_load_changed.emit(percent_complete)

func _ready():
if Engine.is_editor_hint(): return
Expand Down
2 changes: 1 addition & 1 deletion game/src/Game/Menu/OptionMenu/GuiScaleSelector.gd
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func _sync_guiscales(to_select : float = GuiScale.get_current_guiscale()) -> voi

if selected == -1:
selected = default_selected

func _setup_button():
if default_value <= 0:
default_value = ProjectSettings.get_setting("display/window/stretch/scale")
Expand Down
14 changes: 7 additions & 7 deletions game/src/Game/Theme/PieChart/PieChart.gd
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func sort_slices() -> void:

func _ready():
if not Engine.is_editor_hint():
const size : int = 256
_pie_chart_image = Image.create(size, size, false, Image.FORMAT_RGBA8)
const image_size : int = 256
_pie_chart_image = Image.create(image_size, image_size, false, Image.FORMAT_RGBA8)
texture = ImageTexture.create_from_image(_pie_chart_image)
_recalculate()

Expand Down Expand Up @@ -160,11 +160,11 @@ func _on_mouse_exited():
# is hovered over. Returns a boolean on whether the tooltip was handled.
func _handle_tooltip(pos : Vector2) -> bool:
# Is it within the circle?
var radius := size.x / 2.0
var center := Vector2(radius, radius)
var real_radius := size.x / 2.0
var center := Vector2(real_radius, real_radius)
var distance := center.distance_to(pos)
var real_donut_inner_radius : float = radius * donut_inner_radius
if distance <= radius and (not donut or distance >= real_donut_inner_radius):
var real_donut_inner_radius : float = real_radius * donut_inner_radius
if distance <= real_radius and (not donut or distance >= real_donut_inner_radius):
if _slice_order.is_empty():
_rich_tooltip.text = "PIECHART_TOOLTIP_NO_DATA"
else:
Expand All @@ -189,7 +189,7 @@ func _handle_tooltip(pos : Vector2) -> bool:
# Create a list of all the values and percentages
# with the hovered one highlighted
func _create_tooltip(labelHovered : String) -> String:
var slice_tooltips : PackedStringArray
var slice_tooltips : PackedStringArray = []
for label in _slice_order:
var slice : SliceData = _slices.get(label)
var percent := _format_percent(slice.percentage)
Expand Down

0 comments on commit fe74604

Please sign in to comment.