-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
176 lines (167 loc) · 8.23 KB
/
Copy pathapp.js
File metadata and controls
176 lines (167 loc) · 8.23 KB
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
'use strict';
var _ = require('lodash');
var config = require('config');
var path = require('path');
var fs = require('fs');
var videoshow = require('videoshow');
var logger = require('./logger');
var FileReader = require("./eu/ParadoxFileReader");
var Definitions = require("./eu/Definitions");
var Map = require('./eu/Map');
var Color = require('./eu/Color');
var HistoricalDate = require('./eu/HistoricalDate');
var Country = require('./eu/Country').Country;
var CountryFactory = require('./eu/Country').CountryFactory;
var ProvinceFactory = require('./eu/ProvinceHistory').ProvinceFactory;
if(process.argv.length > 2) {
logger.info('Using resources from base path ', config.EU4_PATH);
var countries = {};
var provinces = [];
var provinceColors = {};
Promise.resolve()
.then(function () {
let tagPath = path.resolve(config.EU4_PATH, config.map.countries);
logger.info('Generating countries from ', tagPath);
return (new CountryFactory).all(tagPath);
})
.then(function (allCountries) {
logger.info('Found ' + allCountries.length + ' countries.');
_.forEach(allCountries, function (country) {
countries[country.tag] = country;
});
logger.info('Processing color overrides');
countries[ProvinceFactory.PLACEHOLDER_SEA] = new Country(undefined, 'Water', config.map.colors.sea);
countries[ProvinceFactory.PLACEHOLDER_WASTELAND] = new Country(undefined, 'Wasteland', config.map.colors.wasteland);
let overrides = _.get(config, 'map.colors.overrides', {});
_.forEach(_.keysIn(overrides), function (tag) {
countries[tag] = new Country(tag, 'user-defined', overrides[tag]);
});
logger.info('Using ' + _.keysIn(overrides).length + ' overrides.');
})
.then(function () {
let provincePath = path.resolve(config.EU4_PATH, config.map.history);
logger.info('Reading provinces from ', provincePath);
return new ProvinceFactory().all(provincePath);
})
.then(function (allProvinces) {
logger.info('Found ' + allProvinces.length + ' provinces.');
provinces = allProvinces;
})
.then(function () {
let climatePath = path.resolve(config.EU4_PATH, config.map.climate);
logger.info('Loading wastelands from', climatePath);
return (new ProvinceFactory()).allWastelands(climatePath);
})
.then(function (wastelandProvinces) {
logger.info('Found ' + wastelandProvinces.length + ' wastelands.');
provinces = provinces.concat(wastelandProvinces);
})
.then(function () {
let definitionPath = path.resolve(config.EU4_PATH, config.map.definition);
logger.info('Reading definitions from ', definitionPath);
return Definitions.fromFile(definitionPath);
})
.then(function (definitions) {
_.forEach(definitions, function (definition) {
provinceColors[_.get(definition, 'province')] = new Color(definition);
});
let mapPath = path.resolve(config.EU4_PATH, config.map.provinces);
logger.info('Loading and resizing map ', mapPath);
return new Map(config.map.width).fromFile(mapPath);
})
.then(function (map) {
logger.info('Building province mapping');
return map.buildProvinceMapping(provinceColors);
})
.then(function (map) {
let saveFilePath = process.argv[2];
logger.info('Parsing save file ', saveFilePath);
return FileReader.fromFile(saveFilePath)
.then(function (save) {
logger.info('Building history');
var historyMapping = {};
_.forEach(save.root.provinces, function (province) {
let historySection = province.history;
if (historySection && historySection) {
let history = historySection;
_.forEach(historySection.$insertionOrder, function (key) {
if (HistoricalDate.isValid(key)) {
let events = history[key];
let owner = _.get(events, 'owner');
if (owner) {
let values = _.get(historyMapping, key, []);
values.push({id: Math.abs(province.$name), owner: owner});
historyMapping[key] = values;
}
}
});
}
});
let timeline = _.keysIn(historyMapping);
timeline = timeline.sort(HistoricalDate.compare);
let frames = [];
logger.info('Generating initial map');
_.forEach(provinces, function (province) {
map.recolor([province.id], countries[province.owner].color);
});
let outPath = 'out/initial.png';
map.saveToFile(outPath);
frames.push(outPath);
logger.info('Generating frames');
_.forEach(timeline, function (date, index) {
let events = historyMapping[date];
_.forEach(events, function (event) {
map.recolor([event.id], countries[event.owner].color);
});
let framePath = 'out/frames/frame' + index + '.png';
map.saveToFile(framePath);
frames.push(framePath);
});
logger.info('Creating video. This might take a while.');
let promises = [];
let fpp = config.video.framesPerPart;
let parts = Math.ceil(frames.length / fpp);
for (var index = 1; index <= parts; index++) {
promises.push(function (index) {
return function (resolve, reject) {
logger.info('Creating part ' + index + '/' + parts);
var images = frames.slice((index - 1) * fpp, index * fpp);
var videoOptions = config.video.options(map.width);
videoshow(images, videoOptions)
.save(config.video.outputPath(index) + '.' + videoOptions.format)
.on('start', function (command) {
logger.info('ffmpeg process started:', command);
})
.on('error', function (err, stdout, stderr) {
logger.error('ffmpeg stderr:', stderr);
reject(err);
})
.on('end', function (output) {
logger.info('Video created in:', output);
resolve();
});
};
}(index));
}
if (config.video.parallelize) {
return Promise.all(promises.map(function (p) {
return new Promise(p);
}));
} else {
var promise = Promise.resolve();
promises.forEach(function (p) {
promise = promise.then(function() {return new Promise(p)});
});
return promise;
}
});
})
.then(function () {
logger.info('All done. Bye!')
})
.catch(function (err) {
logger.error(err);
});
} else {
logger.error("Provide (unzipped) .eu4 file as first parameter");
}