-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.cpp
431 lines (374 loc) · 11 KB
/
game.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include "game.h"
#include "city.h"
#include "player_city.h"
#include "stringfunc.h"
#include "world.h"
#include "kingdom.h"
#include "rng.h"
#include "globals.h"
#include "files.h"
#include <fstream>
Game::Game()
{
next_city_uid = 0;
world = new World_map;
city = new Player_city;
world_ready = false;
}
Game::~Game()
{
}
bool Game::start_new_game()
{
date = Date(1400, 5, 1);
if (!world_ready) {
if (file_exists(SAVE_DIR + "world.sav")) {
if (!query_yn("Load world from save file?")) {
return false;
}
world->load_from_file(SAVE_DIR + "world.sav");
} else {
popup("You need to generate a world first! Press G to design one.");
return false;
}
}
// Pick our race first, so we know where to start placement.
if (!city->pick_race()) {
return false;
}
// Let the city pick a location in the world
// Start from the center of the appropriate kingdom.
Point start;
Kingdom* city_kingdom = get_kingdom_for_race(city->get_race());
if (city_kingdom) {
start.x = (city_kingdom->most_west + city_kingdom->most_east ) / 2;
start.y = (city_kingdom->most_north + city_kingdom->most_south) / 2;
} else {
debugmsg("Kingdom not found for %s. %d kingdoms.",
Race_data[city->get_race()]->name.c_str(), kingdoms.size());
start = Point(world->get_size() / 2, world->get_size() / 2);
}
city->set_starting_tiles_seen();
//Point p = world->draw(start);
Point p = world->draw(start, &(city->world_seen));
if (p.x == -1) { // We canceled
return false;
}
// Put our city there.
bool placed = false;
while (!placed) {
if (!city->world_seen.is_seen(p)) {
popup("That's unexplored territory!");
} else if (!world->get_city(p)) {
city->generate_map(p);
placed = city->place_keep();
} else {
popup("There is already a city there!");
}
if (!placed) {
p = world->draw(p, &(city->world_seen)); // Try again
if (p.x == -1) { // We canceled
return false;
}
}
}
city->location = p;
// Let us see a little more.
city->mark_nearby_tiles_seen(4);
world->set_city(p, city);
// Crude race picker; TODO: replace this.
city->set_name();
city->start_new_city();
city->setup_trade_routes();
city->set_starting_tiles_seen();
return true;
}
bool Game::is_world_ready()
{
return world_ready;
}
bool Game::load_world()
{
if (!file_exists(SAVE_DIR + "world.sav")) {
return false;
}
world->load_from_file(SAVE_DIR + "world.sav");
world_ready = true;
return load_kingdoms();
}
bool Game::generate_world(World_design* design)
{
if (!design) {
debugmsg("Game::generate_world(NULL) called!");
return false;
}
if (world) {
delete world;
world = new World_map;
}
world->generate(*design);
generate_kingdoms(design);
world->save_to_file(SAVE_DIR + "world.sav");
save_kingdoms();
world_ready = true;
return true;
}
bool Game::save_game()
{
// Save the player's city.
std::ofstream fout;
std::string filename = SAVE_DIR + "cities/" + city->get_name() + ".sav";
fout << city->save_data();
fout.close();
save_kingdoms();
world->save_to_file(SAVE_DIR + "world.txt");
return true;
}
bool Game::load_game(std::string filename)
{
std::ifstream fin;
fin.open(filename.c_str());
if (!fin.is_open()) {
debugmsg("Couldn't open %s for reading.", filename.c_str());
return false;
}
if (!city->load_data(fin)) {
debugmsg("Game failed to load city.");
return false;
}
load_world();
return true;
}
void Game::advance_time(int days, City* city)
{
// days defaults to 1.
date.advance(days);
// city defaults to NULL
if (city) {
for (int i = 0; i < days; i++) {
city->do_turn();
}
}
}
Date Game::get_date()
{
return date;
}
// TODO: Allow other formats of the date.
std::string Game::get_date_str(int length)
{
if (length < 0) {
return date.get_text();
}
std::string ret = "<c=white,blue>";
ret += date.get_text();
ret += "<c=blue,blue>";
while (tagless_length(ret) < length) {
ret += 'x';
}
ret += "<c=/>";
return ret;
}
Kingdom* Game::get_kingdom_for_race(Race race)
{
for (int i = 0; i < kingdoms.size(); i++) {
if (kingdoms[i]->race == race) {
//debugmsg("Got kingdom %d/%d (uid %d) %s", i, kingdoms.size(), kingdoms[i]->uid, Point(kingdoms[i]->most_west, kingdoms[i]->most_north).str().c_str());
return kingdoms[i];
}
}
return NULL;
}
int Game::get_city_uid()
{
int ret = next_city_uid;
next_city_uid++;
return ret;
}
void Game::generate_kingdoms(World_design* design)
{
if (!world) {
debugmsg("Game::generate_kingdoms() called with NULL world!");
return;
}
if (!design) {
debugmsg("Game::generate_kingdoms(NULL) called!");
return;
}
if (!kingdoms.empty()) {
for (int i = 0; i < kingdoms.size(); i++) {
delete (kingdoms[i]);
}
kingdoms.clear();
}
bool color_free[c_null]; // c_null is the last color
for (int i = 0; i < c_null; i++) {
color_free[i] = true;
}
// Add kingdoms per the races specified in our design.
for (int i = 0; i < design->kingdoms.size(); i++) {
popup_nowait("Initializing kingdoms (%d of %d)...",
i + 1, design->kingdoms.size());
Race race = design->kingdoms[i];
Race_datum* race_dat = Race_data[race];
Kingdom* kingdom = new Kingdom;
kingdom->uid = i;
kingdom->set_game(this);
kingdom->race = race;
// Pick a color - try the official race color first
if (race_dat->color < c_dkgray && color_free[ race_dat->color ]) {
kingdom->color = race_dat->color;
color_free[ race_dat->color ] = false;
} else {
std::vector<nc_color> colors = race_dat->kingdom_colors;
// Remove any already-used colors
for (int n = 0; n < colors.size(); n++) {
if (!color_free[ colors[n] ]) {
colors.erase(colors.begin() + n);
n--;
}
}
if (colors.empty()) { // Can't use official colors; use a random one
std::vector<nc_color> free_colors;
// Start at 1 to skip c_black; stop at c_dkgray to skip bright colors
for (int n = 1; n < c_dkgray; n++) {
if (color_free[n]) {
free_colors.push_back( nc_color(n) );
}
}
if (free_colors.empty()) { // 8 kingdoms used already!
kingdom->color = nc_color( rng(1, c_dkgray - 1) );
} else {
int index = rng(0, free_colors.size() - 1);
kingdom->color = free_colors[index];
color_free[ free_colors[index] ] = false;
}
} else { // We can use an official color!
int index = rng(0, colors.size() - 1);
kingdom->color = colors[index];
color_free[ colors[index] ] = false;
}
} // if (!color_free[race_dat->color])
// Place the kingdom.
if (kingdom->place_capital(world)) {
kingdoms.push_back( kingdom ); // ...and add to our list.
} else {
popup("Kingdom %d of %d [%s] failed to place its capital!",
i + 1, design->kingdoms.size(), race_dat->plural_name.c_str());
}
} // for (int i = 0; i < design->kingdoms.size(); i++)
/* Now, expand the kingdoms by placing duchy seats. To keep things fair, each
* kingdom gets to place a single city/duchy at a time. We go through our list,
* each kingdom taking a turn, until all kingdoms are out of points.
*/
std::vector<int> points, kingdom_index;
for (int i = 0; i < kingdoms.size(); i++) {
points.push_back( KINGDOM_EXPANSION_POINTS ); // Defined in kingdom.h
kingdom_index.push_back( i );
}
int iteration = 0, max_points = points.size() * KINGDOM_EXPANSION_POINTS;
while (!kingdom_index.empty()) {
iteration++;
int total_points = 0;
for (int i = 0; i < points.size(); i++) {
total_points += points[i];
}
int percent = (100 * (max_points - total_points)) / max_points;
popup_nowait("Placing duchies... [%d%%%%%%%%]", percent);
for (int i = 0; i < kingdom_index.size(); i++) {
Kingdom* kingdom = kingdoms[ kingdom_index[i] ];
if (!kingdom->place_duchy_seat(world, points[i])) {
kingdom_index.erase( kingdom_index.begin() + i );
points.erase( points.begin() + i );
}
}
}
// Now each kingdom gets to place minor cities in its duchies.
for (int i = 0; i < kingdoms.size(); i++) {
int percent = (100 * (i + 1)) / kingdoms.size();
popup_nowait("Placing minor cities... [%d%%%%%%%%] [%d/%d]",
percent, i + 1, kingdoms.size());
kingdoms[i]->place_minor_cities(world);
}
// Build roads from the capital to each duchy. Building roads from the duchy to
// its minor cities is handled in place_minor_cities().
for (int i = 0; i < kingdoms.size(); i++) {
int percent = (100 * (i + 1)) / kingdoms.size();
popup_nowait("Connecting duchies via road... [%d%%%%%%%%]", percent);
for (int n = 0; n < kingdoms[i]->dukes.size(); n++) {
City* capital = kingdoms[i]->capital;
City* duke = kingdoms[i]->dukes[n];
kingdoms[i]->build_road( world, capital, duke );
}
}
// Finally, swell the territory claimed by each kingdom.
int expansions = 3;
for (int n = 0; n < expansions; n++) {
for (int i = 0; i < kingdoms.size(); i++) {
popup_nowait("Expanding territories... (%d/%d; %d/%d)",
n + 1, expansions, i + 1, kingdoms.size());
kingdoms[i]->expand_boundaries(world);
}
}
// And setup trade routes for all cities!
for (int i = 0; i < kingdoms.size(); i++) {
int base_percent = (100 * i) / kingdoms.size();
kingdoms[i]->setup_trade_routes(base_percent);
}
}
bool Game::save_kingdoms()
{
std::ofstream fout;
std::string filename = SAVE_DIR + "kingdoms.txt";
fout.open(filename.c_str());
if (!fout.is_open()) {
debugmsg("Couldn't open %s/kingdoms.txt for saving.", SAVE_DIR.c_str());
return false;
}
fout << date.save_data() << std::endl;
fout << next_city_uid << std::endl;
fout << kingdoms.size() << std::endl;
for (int i = 0; i < kingdoms.size(); i++) {
fout << kingdoms[i]->save_data() << std::endl;
}
fout.close();
return true;
}
bool Game::load_kingdoms()
{
if (!world_ready) {
debugmsg("Loading kingdoms before world - not okay!");
return false;
}
std::ifstream fin;
std::string filename = SAVE_DIR + "kingdoms.txt";
fin.open(filename.c_str());
if (!fin.is_open()) {
return false;
}
if (!date.load_data(fin)) {
debugmsg("Game failed to load date.");
return false;
}
fin >> next_city_uid;
int num_kingdoms;
fin >> num_kingdoms;
for (int i = 0; i < num_kingdoms; i++) {
Kingdom* tmp_kingdom = new Kingdom;
if (!tmp_kingdom->load_data(fin)) {
return false;
}
kingdoms.push_back(tmp_kingdom);
// Add the kingdom's cities to our world map
world->set_city(tmp_kingdom->capital->location, tmp_kingdom->capital);
for (int i = 0; i < tmp_kingdom->dukes.size(); i++) {
City* duke = tmp_kingdom->dukes[i];
world->set_city(duke->location, duke);
}
for (int i = 0; i < tmp_kingdom->cities.size(); i++) {
City* city = tmp_kingdom->cities[i];
world->set_city(city->location, city);
}
}
return true;
}