summaryrefslogtreecommitdiff
path: root/src/openvic/dataloader/Dataloader.cpp
blob: 5c00e780a233b1461326e1490ec26ffc1dafcfd1 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "Dataloader.hpp"
#include <fstream>
#include <iostream>
#include <sstream>
#include "../Types.hpp"

using namespace std;
using namespace std::filesystem;
using namespace OpenVic;


#include <map>
//WARNING!!
//The following functions are temporary and highly fragile
//If you have a weak constitution or have a heart condition, do not look at the following functions
namespace compatibility {
   //Helper function
   void eatWhitespaceComments(wifstream& stream) {
      while (stream && (
         stream.peek() == L' ' ||
         stream.peek() == L'\t' ||
         stream.peek() == L'\r' ||
         stream.peek() == L'\n' ||
         stream.peek() == L'#')) {

         if (stream.peek() == L'#') {
            stream.ignore(numeric_limits<streamsize>::max(), L'\n');
         }
         else {
            stream.ignore(1);
         }
      }
   }

   //Helper function
   wstring getNextWord(wifstream& stream) {
      std::wstringstream ss;
      wchar_t wc;

      eatWhitespaceComments(stream);
      while (stream &&
         stream.peek() != L' ' &&
         stream.peek() != L'\t' &&
         stream.peek() != L'\r' &&
         stream.peek() != L'\n' &&
         stream.peek() != L'#') {
         stream >> wc;
         ss << wc;
      }

      eatWhitespaceComments(stream);
      return ss.str();
   }

   using MapDefinitions = map<int, pair<colour_t, wstring>>;
   MapDefinitions readMapDefinitions(path vic2folder) {
      MapDefinitions mp;
      wifstream ws = wifstream(vic2folder/path("map")/path("definition.csv"), ios::binary);
      
      ws.ignore(numeric_limits<streamsize>::max(), L'\n');
      while (ws) {
         while (ws && ws.peek() == '#' || ws.peek() == ';' || ws.peek() == '\n' || ws.peek() == '\r') {
            ws.ignore(numeric_limits<streamsize>::max(), L'\n');
         }
         if (ws.eof()) break;

         int numericId;
         int r, g, b;
         wstring name;

         ws >> numericId; ws.ignore();
         ws >> r; ws.ignore();
         ws >> g; ws.ignore();
         ws >> b; ws.ignore();
         std::getline(ws, name, L';');
         ws.ignore(numeric_limits<streamsize>::max(), '\n');

         mp[numericId] = pair(rgba_to_colour(r, g, b), name);
      }
      return mp;
   }

   void readProvinceFile(path provinceFile) {
      if (provinceFile.extension() != ".txt") { return; }
      // cout << provinceFile.filename() << endl;
   }

   void readProvinceFiles(path vic2folder) {
      path provinceHistoryPath = vic2folder/path("history")/path("provinces");
      for (directory_entry const& di : recursive_directory_iterator(provinceHistoryPath)) {
         if (di.is_directory()) { continue; }
         // cout << di << endl;
         readProvinceFile(di.path());
      }
   }

   void loadGoodsFile(wifstream& file, OpenVic::Simulation& sim) {
      while (file) {
         wstring category = getNextWord(file); //Load a good category
         if (category == L"military_goods") {
            category = L"military";
         }
         if (category == L"raw_material_goods") {
            category = L"raw";
         }
         if (category == L"industrial_goods") {
            category = L"industrial";
         }
         if (category == L"consumer_goods") {
            category = L"consumer";
         }
         getNextWord(file); //Eat =
         getNextWord(file); //Eat {
         while (file.peek() != L'}') { //Load a good
            wstring goodid = getNextWord(file);
            price_t price = 0.0;
            bool availableAtStart = true;
            bool tradeable = true;
            bool currency = true;
            bool overseasMaintenance = false;
            colour_t colour;
            getNextWord(file); //Eat =
            getNextWord(file); //Eat {
            while (file.peek() != L'}') { //Load good attributes
               wstring attribute = getNextWord(file);
               getNextWord(file); //Eat =
               if (attribute == L"cost") {
                  file >> price;
               }
               else if (attribute == L"color") {
                  getNextWord(file); //Eat {
                  int r, g, b;
                  file >> r;
                  file >> g;
                  file >> b;
                  colour = rgba_to_colour(r, g, b);
                  
                  getNextWord(file); //Eat }
               }
               else if (attribute == L"available_from_start") {
                  availableAtStart = (getNextWord(file) == L"yes");
               }
               else if (attribute == L"tradeable") {
                  tradeable = (getNextWord(file) == L"yes");
               }
               else if (attribute == L"money") {
                  currency = (getNextWord(file) == L"yes");
               }
               else if (attribute == L"overseas_penalty") {
                  overseasMaintenance = (getNextWord(file) == L"yes");
               }
            }
            getNextWord(file); //Eat }
            wcout << goodid <<" "<< category <<" 0x"<< HasColour::colour_to_hex_string(colour) <<" "<< price <<" "<< availableAtStart <<" "<< tradeable <<" "<< currency <<" "<< overseasMaintenance << endl;
            sim.goodManager.add_good(goodid, category, colour, price, availableAtStart, tradeable, currency, overseasMaintenance);
         }
         getNextWord(file); //Eat }
      }
   }
}




bool OpenVic::Dataloader::loadDir(std::filesystem::path rootDataFolder, Simulation& sim, LoadingMode loadMode) {
   if (loadMode == LoadingMode::DL_COMPATABILITY) {
      if (!is_directory(rootDataFolder)) {
         return false;
      }

      path goods = rootDataFolder/path("common")/path("goods.txt");
      if (!filesystem::exists(goods)) {
         wcerr << (goods.c_str()) << " does not exist" << endl;
         return false;
      }
      wifstream goodsf(goods);
      cout << "Start loading..." << endl;
      compatibility::loadGoodsFile(goodsf, sim);
      compatibility::readMapDefinitions(rootDataFolder);
      compatibility::readProvinceFiles(rootDataFolder);
      cout << "Done loading" << endl;


      return true;
   }
   return false;
}