aboutsummaryrefslogtreecommitdiff
path: root/extension/src/GameSingleton.hpp
blob: 23eb334a048d519a186437595920320c0c3a8f5b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#pragma once

#include <godot_cpp/classes/image_texture.hpp>
#include <godot_cpp/classes/texture2d_array.hpp>

#include "openvic/GameManager.hpp"

namespace OpenVic {
   struct TerrainVariant : HasIdentifier, HasColour {
      friend class GameSingleton;

   private:
      const godot::Ref<godot::Image> image;

      TerrainVariant(std::string const& 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));

      TerrainVariant(TerrainVariant&&) = default;

      godot::Ref<godot::Image> get_image() const;
   };
   class GameSingleton : public godot::Object {
      GDCLASS(GameSingleton, godot::Object)

      static GameSingleton* singleton;

      GameManager game_manager;

      godot::Vector2i image_subdivisions;
      godot::Ref<godot::Texture2DArray> province_shape_texture;
      godot::Ref<godot::Image> province_colour_image;
      godot::Ref<godot::ImageTexture> province_colour_texture;
      Mapmode::index_t mapmode_index = 0;
      IdentifierRegistry<TerrainVariant> terrain_variants;
      Map::terrain_variant_map_t terrain_variant_map;
      godot::Ref<godot::Texture2DArray> terrain_texture;
      godot::Dictionary good_icons;

      godot::Error _parse_province_identifier_entry(godot::String const& identifier, godot::Variant const& entry);
      godot::Error _parse_region_entry(godot::String const& identifier, godot::Variant const& entry);
      godot::Error _parse_terrain_entry(godot::String const& identifier, godot::Variant const& entry, godot::String const& terrain_texture_dir_path);
      godot::Error _parse_good_entry(godot::String const& identifier, godot::Variant const& entry);

      godot::Error _load_province_identifier_file(godot::String const& file_path);
      godot::Error _load_water_province_file(godot::String const& file_path);
      godot::Error _load_region_file(godot::String const& file_path);
      godot::Error _load_terrain_variants(godot::String const& terrain_identifiers_path, godot::String const& terrain_texture_dir_path);
      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);
      godot::Error _load_goods(godot::String const& defines_path, godot::String const& icons_dir_path);

      godot::Error _load_province_identifier_file_compatibility_mode(godot::String const& file_path);
      godot::Error _load_terrain_variants_compatibility_mode(godot::String const& terrain_image_path, godot::String const& terrain_texturesheet_path);

      /* Hardcoded data for defining things for which parsing from files has
       * not been implemented, currently mapmodes and building types.
       */
      godot::Error _load_hardcoded_defines();

      /* Generate the province_colour_texture from the current mapmode.
       */
      godot::Error _update_colour_image();
      void _on_state_updated();

   protected:
      static void _bind_methods();

   public:
      static GameSingleton* get_singleton();

      GameSingleton();
      ~GameSingleton();

      static void setup_logger();

      static godot::StringName const& get_province_identifier_file_key();
      static godot::StringName const& get_water_province_file_key();
      static godot::StringName const& get_region_file_key();
      static godot::StringName const& get_terrain_variant_file_key();
      static godot::StringName const& get_terrain_texture_dir_key();
      static godot::StringName const& get_province_image_file_key();
      static godot::StringName const& get_terrain_image_file_key();
      static godot::StringName const& get_goods_file_key();
      static godot::StringName const& get_good_icons_dir_key();

      /* Load the game's defines from the filepaths listed as Strings
       * in a Dictionary, using the StringNames above as keys.
       */
      godot::Error load_defines(godot::Dictionary const& file_dict);

      /* Load the game's defines in compatiblity mode from the filepath
       * pointing to the defines folder.
       */
      godot::Error load_defines_compatibility_mode(godot::String const& file_path);

      /* Post-load/restart game setup - reset the game to post-load state
       * and (re)generate starting data, e.g. buildings.
       */
      godot::Error setup_game();

      int32_t get_province_index_from_uv_coords(godot::Vector2 const& coords) const;

      static godot::StringName const& get_province_info_province_key();
      static godot::StringName const& get_province_info_region_key();
      static godot::StringName const& get_province_info_life_rating_key();
      static godot::StringName const& get_province_info_total_population_key();
      static godot::StringName const& get_province_info_rgo_key();
      static godot::StringName const& get_province_info_buildings_key();

      static godot::StringName const& get_building_info_building_key();
      static godot::StringName const& get_building_info_level_key();
      static godot::StringName const& get_building_info_expansion_state_key();
      static godot::StringName const& get_building_info_start_date_key();
      static godot::StringName const& get_building_info_end_date_key();
      static godot::StringName const& get_building_info_expansion_progress_key();

      /* Get info to display in Province Overview Panel, packaged in
       * a Dictionary using the StringNames above as keys.
       */
      godot::Dictionary get_province_info_from_index(int32_t index) const;

      int32_t get_width() const;
      int32_t get_height() const;
      float get_aspect_ratio() const;

      /* The cosmetic terrain textures stored in a Texture2DArray.
       */
      godot::Ref<godot::Texture> get_terrain_texture() const;

      /* Number of (vertical, horizontal) subdivisions the province shape image
       * was split into when making the province_shape_texture to ensure no
       * piece had a dimension greater than 16383.
       */
      godot::Vector2i get_province_shape_image_subdivisions() const;

      /* The map, encoded in RGB8 with RG representing province index and B representing terrain texture.
       * To support a wider range of GPUs, the image is divided so that no piece has a dimension
       * greater than 16383 and the pieces are stored in a Texture2DArray.
       */
      godot::Ref<godot::Texture> get_province_shape_texture() const;

      /* The colour each province should be tinted, arranged in
       * index order into a 256x256 RGB8 texture.
       */
      godot::Ref<godot::Texture> get_province_colour_texture() const;

      int32_t get_mapmode_count() const;
      godot::String get_mapmode_identifier(int32_t index) const;
      godot::Error set_mapmode(godot::String const& identifier);
      int32_t get_selected_province_index() const;
      void set_selected_province(int32_t index);

      godot::Error expand_building(int32_t province_index, godot::String const& building_type_identifier);
      godot::Ref<godot::Texture> get_good_icon_texture(godot::String const& identifier) const;

      void set_paused(bool paused);
      void toggle_paused();
      bool is_paused() const;
      void increase_speed();
      void decrease_speed();
      bool can_increase_speed() const;
      bool can_decrease_speed() const;
      godot::String get_longform_date() const;
      void try_tick();
   };
}