aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Gone2Daly <71726742+Gone2Daly@users.noreply.github.com>2023-09-20 14:34:08 +0200
committer Gone2Daly <71726742+Gone2Daly@users.noreply.github.com>2023-09-20 14:34:08 +0200
commit68f887726fe02c4ff3cfc35f6e6fad561e0d0a88 (patch)
tree66c7cafd56056cfe6f9a1f49d82f397ebdd73679
parentd56941e60016bf73587c63a36ac5ca9da3f6023e (diff)
-rw-r--r--extension/src/openvic-extension/GameSingleton.cpp1
-rw-r--r--extension/src/openvic-extension/GameSingleton.hpp3
-rw-r--r--extension/src/openvic-extension/LoadGameCompatibility.cpp77
-rw-r--r--game/src/Game/GameSession/GameSession.gd1
4 files changed, 74 insertions, 8 deletions
diff --git a/extension/src/openvic-extension/GameSingleton.cpp b/extension/src/openvic-extension/GameSingleton.cpp
index a164b23..175c44d 100644
--- a/extension/src/openvic-extension/GameSingleton.cpp
+++ b/extension/src/openvic-extension/GameSingleton.cpp
@@ -23,6 +23,7 @@ GameSingleton* GameSingleton::singleton = nullptr;
void GameSingleton::_bind_methods() {
ClassDB::bind_static_method("GameSingleton", D_METHOD("setup_logger"), &GameSingleton::setup_logger);
ClassDB::bind_method(D_METHOD("load_defines_compatibility_mode", "file_paths"), &GameSingleton::load_defines_compatibility_mode);
+ ClassDB::bind_method(D_METHOD("quazz_map_convert", "json_path", "csv_path"), &GameSingleton::quazz_map_convert);
ClassDB::bind_method(D_METHOD("lookup_file", "path"), &GameSingleton::lookup_file);
ClassDB::bind_method(D_METHOD("setup_game"), &GameSingleton::setup_game);
diff --git a/extension/src/openvic-extension/GameSingleton.hpp b/extension/src/openvic-extension/GameSingleton.hpp
index bd6b73c..abae90a 100644
--- a/extension/src/openvic-extension/GameSingleton.hpp
+++ b/extension/src/openvic-extension/GameSingleton.hpp
@@ -43,6 +43,7 @@ namespace OpenVic {
godot::Ref<godot::Texture2DArray> terrain_texture;
godot::Dictionary good_icons;
+
godot::Error _generate_terrain_texture_array();
godot::Error _load_map_images(godot::String const& province_image_path, godot::String const& terrain_image_path, bool flip_vertical = false);
@@ -76,7 +77,7 @@ namespace OpenVic {
* pointing to the defines folder.
*/
godot::Error load_defines_compatibility_mode(godot::PackedStringArray const& file_paths);
-
+ godot::Error quazz_map_convert(godot::String const& json_file_path, godot::String const& csv_file_path);
godot::String lookup_file(godot::String const& path) const;
/* Post-load/restart game setup - reset the game to post-load state
diff --git a/extension/src/openvic-extension/LoadGameCompatibility.cpp b/extension/src/openvic-extension/LoadGameCompatibility.cpp
index e8e3314..d003ee6 100644
--- a/extension/src/openvic-extension/LoadGameCompatibility.cpp
+++ b/extension/src/openvic-extension/LoadGameCompatibility.cpp
@@ -1,15 +1,77 @@
-#include "GameSingleton.hpp"
-
+#include <godot_cpp/classes/dir_access.hpp>
#include <godot_cpp/classes/file_access.hpp>
+#include <godot_cpp/classes/json.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
#include <openvic-simulation/utility/BMP.hpp>
#include "openvic-extension/Utilities.hpp"
+#include "GameSingleton.hpp"
+
using namespace godot;
using namespace OpenVic;
+Error GameSingleton::quazz_map_convert(String const& json_file_path, String const& csv_file_path) {
+ Ref<FileAccess> json_file = FileAccess::open(json_file_path, FileAccess::READ);
+
+ Variant data_received;
+ if (!json_file.is_valid() && FileAccess::get_open_error() == FAILED) {
+ UtilityFunctions::push_error("Error opening JSON file: ", json_file);
+ return FAILED;
+ }
+
+ String json_content = json_file->get_as_text();
+ json_file->close();
+ godot::JSON json_data;
+ Error error = json_data.parse(json_content);
+
+ if (error == OK) {
+ data_received = json_data.get_data();
+ } else {
+ UtilityFunctions::push_error("JSON Parse Error: ", json_data.get_error_message());
+ return FAILED;
+ }
+ Ref<FileAccess> csv_file = FileAccess::open(csv_file_path, FileAccess::WRITE);
+
+ if (!csv_file.is_valid() && FileAccess::get_open_error() == FAILED) {
+ UtilityFunctions::push_error("Error creating CSV file: ", csv_file);
+ return FAILED;
+ }
+
+ Dictionary json_dict = data_received;
+ Array keys = json_dict.keys();
+ String csv_header = "province_count;red;green;blue;province_name;x\n";
+ csv_file->store_string(csv_header);
+
+ for (int i = 0; i < keys.size(); i++) {
+ String province_name = keys[i];
+ Variant color_data = json_dict[province_name];
+ String color_string;
+
+ if (color_data.get_type() == Variant::ARRAY) {
+ Array color_array = color_data;
+ int red = color_array[0];
+ int green = color_array[1];
+ int blue = color_array[2];
+ color_string = String::num(red) + ";" + String::num(green) + ";" + String::num(blue);
+ } else if (color_data.get_type() == Variant::STRING) {
+ String hex_color = color_data;
+ int red = hex_color.substr(1, 2).hex_to_int();
+ int green = hex_color.substr(3, 2).hex_to_int();
+ int blue = hex_color.substr(5, 2).hex_to_int();
+ color_string = String::num(red) + ";" + String::num(green) + ";" + String::num(blue);
+ }
+
+ String csv_line = String::num(i + 1) + ";" + color_string + ";" + province_name + ";x\n";
+ csv_file->store_string(csv_line);
+ }
+ csv_file->close();
+
+ Error err = OK;
+ return err;
+}
+
Error GameSingleton::_load_terrain_variants_compatibility_mode(String const& terrain_image_path, String const& terrain_texturesheet_path) {
// Read BMP's palette to determine terrain variant colours which texture they're associated with
BMP bmp;
@@ -85,16 +147,17 @@ Error GameSingleton::load_defines_compatibility_mode(PackedStringArray const& fi
game_manager.map.lock_regions();
if (_load_terrain_variants_compatibility_mode(
- std_to_godot_string(dataloader.lookup_file(terrain_image_file).string()),
- std_to_godot_string(dataloader.lookup_file(terrain_texture_file).string())
+ std_to_godot_string(dataloader.lookup_file(terrain_image_file).string()),
+ std_to_godot_string(dataloader.lookup_file(terrain_texture_file).string())
) != OK) {
UtilityFunctions::push_error("Failed to load terrain variants!");
err = FAILED;
}
if (_load_map_images(
- std_to_godot_string(dataloader.lookup_file(province_image_file).string()),
- std_to_godot_string(dataloader.lookup_file(terrain_image_file).string()),
- true) != OK) {
+ std_to_godot_string(dataloader.lookup_file(province_image_file).string()),
+ std_to_godot_string(dataloader.lookup_file(terrain_image_file).string()),
+ true
+ ) != OK) {
UtilityFunctions::push_error("Failed to load map images!");
err = FAILED;
}
diff --git a/game/src/Game/GameSession/GameSession.gd b/game/src/Game/GameSession/GameSession.gd
index 2a27e3d..efc98ac 100644
--- a/game/src/Game/GameSession/GameSession.gd
+++ b/game/src/Game/GameSession/GameSession.gd
@@ -3,6 +3,7 @@ extends Control
@export var _game_session_menu : Control
func _ready():
+ GameSingleton.quazz_map_convert("res://common/map/provinces.json", "res://common/map/provinces.csv")
Events.Options.load_settings_from_file()
if GameSingleton.setup_game() != OK:
push_error("Failed to setup game")