-
Notifications
You must be signed in to change notification settings - Fork 68
/
GameData.cpp
879 lines (745 loc) · 26.3 KB
/
GameData.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
#include <algorithm>
#include <cassert>
#include <cmath>
#include "SDL.h"
#include "GameData.h"
#include "../Assets/ExeStrings.h"
#include "../Assets/INFFile.h"
#include "../Assets/MIFFile.h"
#include "../Entities/Animation.h"
#include "../Entities/CharacterClass.h"
#include "../Entities/Doodad.h"
#include "../Entities/Entity.h"
#include "../Entities/EntityManager.h"
#include "../Entities/GenderName.h"
#include "../Entities/NonPlayer.h"
#include "../Entities/Player.h"
#include "../Items/WeaponType.h"
#include "../Math/Constants.h"
#include "../Math/Random.h"
#include "../Media/PaletteFile.h"
#include "../Media/PaletteName.h"
#include "../Media/TextureManager.h"
#include "../Rendering/Renderer.h"
#include "../Utilities/Debug.h"
#include "../Utilities/String.h"
#include "../World/ClimateType.h"
#include "../World/LocationType.h"
#include "../World/VoxelGrid.h"
#include "../World/VoxelType.h"
#include "../World/WeatherType.h"
#include "../World/WorldType.h"
// Arbitrary value for testing. One real second = six game minutes.
// The value used in Arena is one real second = twenty game seconds.
const double GameData::TIME_SCALE = static_cast<double>(Clock::SECONDS_IN_A_DAY) / 240.0;
GameData::GameData(Player &&player, WorldData &&worldData, const Location &location,
const Date &date, const Clock &clock, double fogDistance)
: player(std::move(player)), worldData(std::move(worldData)), location(location),
date(date), clock(clock)
{
DebugMention("Initializing.");
this->fogDistance = fogDistance;
}
GameData::~GameData()
{
DebugMention("Closing.");
}
std::vector<uint32_t> GameData::makeExteriorSkyPalette(const std::string &paletteName,
TextureManager &textureManager)
{
// The palettes in the data files only cover half of the day, so some added
// darkness is needed for the other half.
const SDL_Surface *palette = textureManager.getSurface(paletteName);
const uint32_t *pixels = static_cast<const uint32_t*>(palette->pixels);
const int pixelCount = palette->w * palette->h;
std::vector<uint32_t> fullPalette(pixelCount * 2);
// Fill with darkness (the first color in the palette is the closest to night).
const uint32_t darkness = pixels[0];
std::fill(fullPalette.begin(), fullPalette.end(), darkness);
// Copy the sky palette over the center of the full palette.
std::copy(pixels, pixels + pixelCount,
fullPalette.data() + (fullPalette.size() / 4));
return fullPalette;
}
void GameData::loadFromMIF(const MIFFile &mif, const INFFile &inf, WorldType worldType,
WeatherType weatherType, Double3 &playerPosition, WorldData &worldData,
TextureManager &textureManager, Renderer &renderer)
{
// Clear all entities.
auto &entityManager = worldData.getEntityManager();
for (const auto *entity : entityManager.getAllEntities())
{
renderer.removeFlat(entity->getID());
entityManager.remove(entity->getID());
}
// Clear software renderer textures.
renderer.clearTextures();
// Set sky palette based on world type and weather.
if ((worldType == WorldType::City) || (worldType == WorldType::Wilderness))
{
// Regular sky palette, based on weather.
const std::vector<uint32_t> skyPalette = GameData::makeExteriorSkyPalette(
(weatherType == WeatherType::Clear) ? "DAYTIME.COL" : "DREARY.COL",
textureManager);
renderer.setSkyPalette(skyPalette.data(), static_cast<int>(skyPalette.size()));
}
else
{
// Interior location. See if it's an "outdoor dungeon" (i.e., one with no ceiling),
// or a typical interior.
const uint32_t skyColor = inf.getCeiling().outdoorDungeon ?
Color::Gray.toARGB() : Color::Black.toARGB();
renderer.setSkyPalette(&skyColor, 1);
}
// Load the world data from the .MIF and .INF files.
worldData = WorldData(mif, inf, worldType);
// Convert start point to new coordinate system and set player's location
// (player Y value is arbitrary for now).
const Double2 &startPoint = worldData.getStartPoints().at(0);
playerPosition = Double3(startPoint.x, playerPosition.y, startPoint.y);
// Load .INF voxel textures into the renderer. Assume all voxel textures are 64x64.
const int voxelTextureCount = static_cast<int>(inf.getVoxelTextures().size());
for (int i = 0; i < voxelTextureCount; i++)
{
const auto &textureData = inf.getVoxelTextures().at(i);
const std::string textureName = String::toUppercase(textureData.filename);
const std::string extension = String::getExtension(textureName);
const bool isIMG = extension == ".IMG";
const bool isSET = extension == ".SET";
const bool noExtension = extension.size() == 0;
if (isSET)
{
// Use the texture data's .SET index to obtain the correct surface.
const auto &surfaces = textureManager.getSurfaces(textureName);
const SDL_Surface *surface = surfaces.at(textureData.setIndex);
renderer.setVoxelTexture(i, static_cast<const uint32_t*>(surface->pixels));
}
else if (isIMG)
{
const SDL_Surface *surface = textureManager.getSurface(textureName);
renderer.setVoxelTexture(i, static_cast<const uint32_t*>(surface->pixels));
}
else if (noExtension)
{
// Ignore texture names with no extension. They appear to be lore-related names
// that were used at one point in Arena's development.
static_cast<void>(textureData);
}
else
{
DebugCrash("Unrecognized voxel texture extension \"" + extension + "\".");
}
}
// Load .INF flat textures into the renderer.
// - To do: maybe turn this into a while loop, so the index variable can be incremented
// by the size of each .DFA. It's incorrect as-is.
const int flatTextureCount = static_cast<int>(inf.getFlatTextures().size());
for (int i = 0; i < flatTextureCount; i++)
{
const auto &textureData = inf.getFlatTextures().at(i);
const std::string textureName = String::toUppercase(textureData.filename);
const std::string extension = String::getExtension(textureName);
const bool isDFA = extension == ".DFA";
const bool isIMG = extension == ".IMG";
const bool noExtension = extension.size() == 0;
if (isDFA)
{
// To do: creatures don't have .DFA files (although they're referenced in the .INF
// files), so I think the extension needs to be .CFA instead for them.
/*const auto &surfaces = textureManager.getSurfaces(textureName);
for (const auto *surface : surfaces)
{
renderer.addTexture(static_cast<const uint32_t*>(surface->pixels),
surface->w, surface->h);
}*/
}
else if (isIMG)
{
const SDL_Surface *surface = textureManager.getSurface(textureName);
renderer.setFlatTexture(i, static_cast<const uint32_t*>(surface->pixels),
surface->w, surface->h);
}
else if (noExtension)
{
// Ignore texture names with no extension. They appear to be lore-related names
// that were used at one point in Arena's development.
static_cast<void>(textureData);
}
else
{
DebugCrash("Unrecognized flat texture extension \"" + extension + "\".");
}
}
}
std::unique_ptr<GameData> GameData::createDefault(const std::string &playerName,
GenderName gender, int raceID, const CharacterClass &charClass, int portraitID,
TextureManager &textureManager, Renderer &renderer)
{
// Create some dummy data for the test world.
// Some arbitrary player values.
const Double3 position = Double3(1.50, 1.70, 12.50);
const Double3 direction = Double3(1.0, 0.0, 0.0).normalized();
const Double3 velocity = Double3(0.0, 0.0, 0.0);
const double maxWalkSpeed = 2.0;
const double maxRunSpeed = 8.0;
const WeaponType weaponType = []()
{
// Pick a random weapon type for testing.
const std::vector<WeaponType> types =
{
WeaponType::BattleAxe,
WeaponType::Broadsword,
WeaponType::Fists,
WeaponType::Flail,
WeaponType::Mace,
WeaponType::Staff,
WeaponType::Warhammer
};
Random random;
int index = random.next(static_cast<int>(types.size()));
return types.at(index);
}();
Player player(playerName, gender, raceID, charClass, portraitID,
position, direction, velocity, maxWalkSpeed, maxRunSpeed, weaponType);
// Add some wall textures.
textureManager.setPalette(PaletteFile::fromName(PaletteName::Default));
std::vector<const SDL_Surface*> surfaces = {
// Texture indices:
// 0: city wall
textureManager.getSurface("CITYWALL.IMG"),
// 1: sea wall
textureManager.getSurface("SEAWALL.IMG"),
// 2-4: grounds
textureManager.getSurfaces("NORM1.SET").at(0),
textureManager.getSurfaces("NORM1.SET").at(1),
textureManager.getSurfaces("NORM1.SET").at(2),
// 5-6: gates
textureManager.getSurface("DLGT.IMG"),
textureManager.getSurface("DRGT.IMG"),
// 7-10: tavern + door
textureManager.getSurfaces("MTAVERN.SET").at(0),
textureManager.getSurfaces("MTAVERN.SET").at(1),
textureManager.getSurfaces("MTAVERN.SET").at(2),
textureManager.getSurface("DTAV.IMG"),
// 11-16: temple + door
textureManager.getSurfaces("MTEMPLE.SET").at(0),
textureManager.getSurfaces("MTEMPLE.SET").at(1),
textureManager.getSurfaces("MTEMPLE.SET").at(2),
textureManager.getSurfaces("MTEMPLE.SET").at(3),
textureManager.getSurfaces("MTEMPLE.SET").at(4),
textureManager.getSurface("DTEP.IMG"),
// 17-22: Mage's Guild + door
textureManager.getSurfaces("MMUGUILD.SET").at(0),
textureManager.getSurfaces("MMUGUILD.SET").at(1),
textureManager.getSurfaces("MMUGUILD.SET").at(2),
textureManager.getSurfaces("MMUGUILD.SET").at(3),
textureManager.getSurfaces("MMUGUILD.SET").at(4),
textureManager.getSurface("DMU.IMG"),
// 23-26: Equipment store + door
textureManager.getSurfaces("MEQUIP.SET").at(0),
textureManager.getSurfaces("MEQUIP.SET").at(1),
textureManager.getSurfaces("MEQUIP.SET").at(2),
textureManager.getSurface("DEQ.IMG"),
// 27-31: Low house + door
textureManager.getSurfaces("MBS1.SET").at(0),
textureManager.getSurfaces("MBS1.SET").at(1),
textureManager.getSurfaces("MBS1.SET").at(2),
textureManager.getSurfaces("MBS1.SET").at(3),
textureManager.getSurface("DBS1.IMG"),
// 32-35: Medium house + door
textureManager.getSurfaces("MBS3.SET").at(0),
textureManager.getSurfaces("MBS3.SET").at(1),
textureManager.getSurfaces("MBS3.SET").at(2),
textureManager.getSurface("DBS3.IMG"),
// 36-39: Noble house + door
textureManager.getSurfaces("MNOBLE.SET").at(0),
textureManager.getSurfaces("MNOBLE.SET").at(1),
textureManager.getSurfaces("MNOBLE.SET").at(2),
textureManager.getSurface("DNB1.IMG"),
// 40: Hedge
textureManager.getSurface("HEDGE.IMG"),
// 41-42: Bridge
textureManager.getSurface("TTOWER.IMG"),
textureManager.getSurface("NBRIDGE.IMG"),
};
const int wallTextureCount = static_cast<int>(surfaces.size());
for (int i = 0; i < wallTextureCount; i++)
{
const SDL_Surface *surface = surfaces.at(i);
renderer.setVoxelTexture(i, static_cast<uint32_t*>(surface->pixels));
}
// Make an empty voxel grid with some arbitrary dimensions.
const int gridWidth = 24;
const int gridHeight = 5;
const int gridDepth = 24;
VoxelGrid voxelGrid(gridWidth, gridHeight, gridDepth);
// Add some voxel data for the voxel grid's IDs to refer to. The first voxel data
// is a placeholder for empty voxels.
const int emptyID = voxelGrid.addVoxelData(VoxelData());
auto addWall = [&voxelGrid](int id)
{
return voxelGrid.addVoxelData(
VoxelData::makeWall(id, id, id, VoxelType::Solid));
};
// To do: now that the lowest texture ID can be 0 instead of 1, reduce all IDs
// here accordingly, so the renderer doesn't need to do -1.
// City wall.
const int cityWallID = addWall(1);
// Ground.
const int gravelID = voxelGrid.addVoxelData(VoxelData::makeFloor(2));
const int roadID = voxelGrid.addVoxelData(VoxelData::makeFloor(3));
const int grassID = voxelGrid.addVoxelData(VoxelData::makeFloor(4));
// Tavern.
const int tavern1ID = addWall(8);
const int tavern2ID = addWall(9);
const int tavern3ID = addWall(10);
const int tavernDoorID = addWall(11);
// Temple.
const int temple1ID = addWall(12);
const int temple2ID = addWall(13);
const int temple3ID = addWall(14);
const int temple4ID = addWall(15);
const int temple5ID = addWall(16);
const int templeDoorID = addWall(17);
// Mage's guild.
const int mages1ID = addWall(18);
const int mages2ID = addWall(19);
const int mages3ID = addWall(20);
const int mages4ID = addWall(21);
const int mages5ID = addWall(22);
const int magesDoorID = addWall(23);
// Equipment store.
const int equip1ID = addWall(24);
const int equip2ID = addWall(25);
const int equip3ID = addWall(26);
const int equipDoorID = addWall(27);
// Low house.
const int lowHouse1ID = addWall(28);
const int lowHouse2ID = addWall(29);
const int lowHouse3ID = addWall(30);
const int lowHouse4ID = addWall(31);
const int lowHouseDoorID = addWall(32);
// Medium house.
const int medHouse1ID = addWall(33);
const int medHouse2ID = addWall(34);
const int medHouse3ID = addWall(35);
const int medHouseDoorID = addWall(36);
// Noble house.
const int noble1ID = addWall(37);
const int noble2ID = addWall(38);
const int noble3ID = addWall(39);
const int nobleDoorID = addWall(40);
// Hedge.
const int hedgeID = voxelGrid.addVoxelData(VoxelData::makeTransparentWall(41));
// Bridge.
const int bridge1ID = voxelGrid.addVoxelData(VoxelData::makeRaised(
42, 43, 43, 0.0, 0.125, 0.875, Constants::JustBelowOne));
const int bridge2ID = voxelGrid.addVoxelData(VoxelData::makeRaised(
42, 43, 43, 0.10, 0.125, 0.775, 0.90));
// Water.
const int water1ID = voxelGrid.addVoxelData(VoxelData::makeChasm(
1, false, false, true, false, VoxelData::ChasmData::Type::Wet));
const int water2ID = voxelGrid.addVoxelData(VoxelData::makeChasm(
1, true, false, false, false, VoxelData::ChasmData::Type::Wet));
// Lambda for setting a voxel at some coordinate to some ID.
auto setVoxel = [&voxelGrid](int x, int y, int z, int id)
{
uint8_t *voxels = voxelGrid.getVoxels();
voxels[x + (y * voxelGrid.getWidth()) +
(z * voxelGrid.getWidth() * voxelGrid.getHeight())] = id;
};
// Set voxel IDs with indices into the voxel data.
// City walls.
for (int j = 0; j < (gridHeight - 1); j++)
{
for (int k = 0; k < gridDepth; k++)
{
setVoxel(0, j, k, cityWallID);
setVoxel(gridWidth - 1, j, k, cityWallID);
}
}
for (int j = 0; j < (gridHeight - 1); j++)
{
for (int i = 0; i < gridWidth; i++)
{
setVoxel(i, j, 0, cityWallID);
setVoxel(i, j, gridDepth - 1, cityWallID);
}
}
// Grass fill.
for (int k = 1; k < (gridDepth - 1); k++)
{
for (int i = 1; i < (gridWidth - 1); i++)
{
setVoxel(i, 0, k, grassID);
}
}
// Road.
for (int i = 1; i < (gridWidth - 1); i++)
{
setVoxel(i, 0, 11, roadID);
setVoxel(i, 0, 12, roadID);
setVoxel(i, 0, 13, roadID);
}
// Trench.
for (int k = 1; k < (gridDepth - 1); k++)
{
setVoxel(11, 0, k, water1ID);
setVoxel(12, 0, k, water2ID);
}
// Random number generator with an arbitrary seed.
Random random(0);
// Tavern.
for (int k = 5; k < 10; k++)
{
for (int j = 1; j < 3; j++)
{
for (int i = 2; i < 6; i++)
{
setVoxel(i, j, k, tavern1ID + random.next(3));
}
}
}
// Tavern door.
setVoxel(3, 1, 9, tavernDoorID);
// Tavern gravel.
setVoxel(3, 0, 10, gravelID);
// Temple.
for (int k = 2; k < 10; k++)
{
for (int j = 1; j < 4; j++)
{
for (int i = 7; i < 10; i++)
{
setVoxel(i, j, k, temple1ID + random.next(5));
}
}
}
// Temple door.
setVoxel(8, 1, 9, templeDoorID);
// Temple gravel.
setVoxel(8, 0, 10, gravelID);
// Mages' guild.
for (int k = 15; k < 20; k++)
{
for (int j = 1; j < 3; j++)
{
for (int i = 7; i < 10; i++)
{
setVoxel(i, j, k, mages1ID + random.next(5));
}
}
}
// Mages' guild door.
setVoxel(8, 1, 15, magesDoorID);
// Mages' guild gravel.
setVoxel(8, 0, 14, gravelID);
// Equipment store.
for (int k = 15; k < 19; k++)
{
for (int j = 1; j < 2; j++)
{
for (int i = 2; i < 5; i++)
{
setVoxel(i, j, k, equip1ID + random.next(3));
}
}
}
// Equipment store door.
setVoxel(3, 1, 15, equipDoorID);
// Equipment store gravel.
setVoxel(3, 0, 14, gravelID);
// Low house.
for (int k = 15; k < 20; k++)
{
for (int j = 1; j < 2; j++)
{
for (int i = 14; i < 18; i++)
{
setVoxel(i, j, k, lowHouse1ID + random.next(4));
}
}
}
// Low house door.
setVoxel(15, 1, 15, lowHouseDoorID);
// Low house gravel.
setVoxel(15, 0, 14, gravelID);
// Medium house.
for (int k = 15; k < 19; k++)
{
for (int j = 1; j < 3; j++)
{
for (int i = 19; i < 22; i++)
{
setVoxel(i, j, k, medHouse1ID + random.next(3));
}
}
}
// Medium house door.
setVoxel(20, 1, 15, medHouseDoorID);
// Medium house gravel.
setVoxel(20, 0, 14, gravelID);
// Noble house.
for (int k = 4; k < 9; k++)
{
for (int j = 1; j < 3; j++)
{
for (int i = 16; i < 20; i++)
{
setVoxel(i, j, k, noble1ID + random.next(3));
}
}
}
// Noble house door.
setVoxel(17, 1, 8, nobleDoorID);
// Noble house gravel.
setVoxel(17, 0, 9, gravelID);
setVoxel(17, 0, 10, gravelID);
// Noble house hedges.
for (int k = 2; k < 10; k++)
{
setVoxel(14, 1, k, hedgeID);
setVoxel(21, 1, k, hedgeID);
}
for (int i = 15; i < 21; i++)
{
setVoxel(i, 1, 2, hedgeID);
}
// Bridge.
for (int k = 11; k < 14; k++)
{
setVoxel(10, 1, k, bridge1ID);
setVoxel(11, 1, k, bridge2ID);
setVoxel(12, 1, k, bridge2ID);
setVoxel(13, 1, k, bridge1ID);
}
// Lambdas for adding a new flat texture to the renderer.
auto setFlatTexture = [&textureManager, &renderer](int id, const std::string &filename)
{
const SDL_Surface *surface = textureManager.getSurface(filename);
renderer.setFlatTexture(id, static_cast<const uint32_t*>(surface->pixels),
surface->w, surface->h);
};
auto setFlatTextures = [&textureManager, &renderer](int startID, const std::string &filename)
{
const std::vector<SDL_Surface*> &surfaces = textureManager.getSurfaces(filename);
std::vector<int> textureIDs;
const int surfaceCount = static_cast<int>(surfaces.size());
for (int i = 0; i < surfaceCount; i++)
{
const SDL_Surface *surface = surfaces.at(i);
const int textureID = startID + i;
renderer.setFlatTexture(textureID, static_cast<const uint32_t*>(surface->pixels),
surface->w, surface->h);
textureIDs.push_back(textureID);
}
return textureIDs;
};
// Flat texture properties. Also add them to the renderer.
const int tree1TextureID = 0;
const int tree2TextureID = 1;
const int statueTextureID = 2;
setFlatTexture(tree1TextureID, "NPINE1.IMG");
setFlatTexture(tree2TextureID, "NPINE4.IMG");
setFlatTexture(statueTextureID, "NSTATUE1.IMG");
const std::vector<int> lampPostTextureIDs = setFlatTextures(
statueTextureID + 1, "NLAMP1.DFA");
const std::vector<int> womanTextureIDs = setFlatTextures(
lampPostTextureIDs.back() + 1, "FMGEN01.CFA");
const std::vector<int> manTextureIDs = setFlatTextures(
womanTextureIDs.back() + 1, "MLGEN01W.CFA");
const double tree1Scale = 2.0;
const double tree2Scale = 2.0;
const double statueScale = 1.0;
const double lampPostScale = 0.90;
const double womanScale = 0.80;
const double manScale = 0.80;
// Lambdas for adding entities to the entity manager and renderer (they can have
// more parameters in the future as entities grow more complex).
EntityManager entityManager;
auto addDoodad = [&entityManager, &renderer](const Double3 &position, double width,
double height, const std::vector<int> &textureIDs)
{
const double timePerFrame = 0.10;
Animation animation(textureIDs, timePerFrame, true);
std::unique_ptr<Doodad> doodad(new Doodad(animation, position, entityManager));
// Assign the entity ID with the first texture.
renderer.addFlat(doodad->getID(), position, width, height, textureIDs.at(0));
entityManager.add(std::move(doodad));
};
auto addNonPlayer = [&entityManager, &renderer](const Double3 &position,
const Double2 &direction, double width, double height,
const std::vector<int> &idleIDs, const std::vector<int> &moveIDs,
const std::vector<int> &attackIDs, const std::vector<int> &deathIDs)
{
// Eventually, "idleIDs" and "moveIDs" should be vector<vector<int>>.
std::vector<Animation> idleAnimations, moveAnimations;
const double timePerFrame = 0.33;
idleAnimations.push_back(Animation(idleIDs, timePerFrame, true));
moveAnimations.push_back(Animation(moveIDs, timePerFrame, true));
Animation attackAnimation(attackIDs, timePerFrame, false);
Animation deathAnimation(deathIDs, timePerFrame, false);
std::unique_ptr<NonPlayer> nonPlayer(new NonPlayer(
position, direction, idleAnimations, moveAnimations, attackAnimation,
deathAnimation, entityManager));
// Assign the entity ID with the first texture.
renderer.addFlat(nonPlayer->getID(), position, width, height, idleIDs.at(0));
entityManager.add(std::move(nonPlayer));
};
// Add entities.
addDoodad(Double3(2.50, 1.0, 21.50), 0.88 * tree1Scale, 1.37 * tree1Scale, { tree1TextureID });
addDoodad(Double3(9.50, 1.0, 21.50), 0.66 * tree2Scale, 1.32 * tree2Scale, { tree2TextureID });
addDoodad(Double3(2.50, 1.0, 2.50), 0.66 * tree2Scale, 1.32 * tree2Scale, { tree2TextureID });
addDoodad(Double3(20.50, 1.0, 21.50), 0.88 * tree1Scale, 1.37 * tree1Scale, { tree1TextureID });
addDoodad(Double3(6.50, 1.0, 12.50), 0.74 * statueScale, 1.38 * statueScale, { statueTextureID });
addDoodad(Double3(5.50, 1.0, 10.50), 0.64 * lampPostScale, 1.03 * lampPostScale, lampPostTextureIDs);
addDoodad(Double3(9.50, 1.0, 14.50), 0.64 * lampPostScale, 1.03 * lampPostScale, lampPostTextureIDs);
addDoodad(Double3(18.50, 1.0, 9.50), 0.64 * lampPostScale, 1.03 * lampPostScale, lampPostTextureIDs);
addDoodad(Double3(17.50, 1.0, 14.50), 0.64 * lampPostScale, 1.03 * lampPostScale, lampPostTextureIDs);
addNonPlayer(Double3(4.50, 1.0, 13.50), Double2(1.0, 0.0),
0.44 * womanScale, 1.04 * womanScale, womanTextureIDs, womanTextureIDs, {}, {});
addNonPlayer(Double3(4.50, 1.0, 11.50), Double2(1.0, 0.0),
0.52 * manScale, 0.99 * manScale, manTextureIDs, manTextureIDs, {}, {});
// Fog distance is changed infrequently, so it can go here in scene creation.
// It's not an expensive operation for the software renderer.
const double fogDistance = 18.0;
renderer.setFogDistance(fogDistance);
// The sky palette is used to color the sky and fog. The renderer chooses
// which color to use based on the time of day. Interiors should just have
// one pixel as the sky palette (usually black).
const std::vector<uint32_t> fullSkyPalette =
GameData::makeExteriorSkyPalette("DAYTIME.COL", textureManager);
renderer.setSkyPalette(fullSkyPalette.data(), static_cast<int>(fullSkyPalette.size()));
Location location("Test City", player.getRaceID(),
LocationType::CityState, ClimateType::Mountain);
// Start the date on the first day of the first month.
const int month = 0;
const int day = 0;
Date date(month, day);
// Start the clock at 5:00am.
Clock clock(5, 0, 0);
renderer.setNightLightsActive(true);
return std::unique_ptr<GameData>(new GameData(
std::move(player), WorldData(std::move(voxelGrid), std::move(entityManager)),
location, date, clock, fogDistance));
}
std::unique_ptr<GameData> GameData::createRandomPlayer(const std::vector<CharacterClass> &charClasses,
TextureManager &textureManager, Renderer &renderer)
{
Random random;
const std::string playerName = "Player";
const GenderName gender = (random.next(2) == 0) ? GenderName::Male : GenderName::Female;
const int raceID = random.next(8);
const CharacterClass &charClass = charClasses.at(
random.next() % static_cast<int>(charClasses.size()));
const int portraitID = random.next(10);
return GameData::createDefault(playerName, gender, raceID, charClass,
portraitID, textureManager, renderer);
}
Player &GameData::getPlayer()
{
return this->player;
}
WorldData &GameData::getWorldData()
{
return this->worldData;
}
Location &GameData::getLocation()
{
return this->location;
}
const Date &GameData::getDate() const
{
return this->date;
}
const Clock &GameData::getClock() const
{
return this->clock;
}
double GameData::getDaytimePercent() const
{
return this->clock.getPreciseTotalSeconds() /
static_cast<double>(Clock::SECONDS_IN_A_DAY);
}
double GameData::getFogDistance() const
{
return this->fogDistance;
}
double GameData::getAmbientPercent() const
{
if (this->worldData.getWorldType() == WorldType::Interior)
{
// Completely dark indoors (some places might be an exception to this, and those
// would be handled eventually).
return 0.0;
}
else
{
// The ambient light outside depends on the clock time.
const double clockPreciseSeconds = this->clock.getPreciseTotalSeconds();
// Time ranges where the ambient light changes. The start times are inclusive,
// and the end times are exclusive.
const double startBrighteningTime =
Clock::AmbientStartBrightening.getPreciseTotalSeconds();
const double endBrighteningTime =
Clock::AmbientEndBrightening.getPreciseTotalSeconds();
const double startDimmingTime =
Clock::AmbientStartDimming.getPreciseTotalSeconds();
const double endDimmingTime =
Clock::AmbientEndDimming.getPreciseTotalSeconds();
// In Arena, the min ambient is 0 and the max ambient is 1, but we're using
// some values here that make testing easier.
const double minAmbient = 0.30;
const double maxAmbient = 0.90;
if ((clockPreciseSeconds >= endBrighteningTime) &&
(clockPreciseSeconds < startDimmingTime))
{
// Daytime ambient.
return maxAmbient;
}
else if ((clockPreciseSeconds >= startBrighteningTime) &&
(clockPreciseSeconds < endBrighteningTime))
{
// Interpolate brightening light (in the morning).
const double timePercent = (clockPreciseSeconds - startBrighteningTime) /
(endBrighteningTime - startBrighteningTime);
return minAmbient + ((maxAmbient - minAmbient) * timePercent);
}
else if ((clockPreciseSeconds >= startDimmingTime) &&
(clockPreciseSeconds < endDimmingTime))
{
// Interpolate dimming light (in the evening).
const double timePercent = (clockPreciseSeconds - startDimmingTime) /
(endDimmingTime - startDimmingTime);
return maxAmbient + ((minAmbient - maxAmbient) * timePercent);
}
else
{
// Night ambient.
return minAmbient;
}
}
}
double GameData::getBetterAmbientPercent() const
{
const double daytimePercent = this->getDaytimePercent();
const double minAmbient = 0.20;
const double maxAmbient = 0.90;
const double diff = maxAmbient - minAmbient;
const double center = minAmbient + (diff / 2.0);
return center + ((diff / 2.0) * -std::cos(daytimePercent * (2.0 * Constants::Pi)));
}
void GameData::tickTime(double dt)
{
assert(dt >= 0.0);
// Tick the game clock.
const int oldHour = this->clock.getHours24();
this->clock.tick(dt * GameData::TIME_SCALE);
const int newHour = this->clock.getHours24();
// Check if the clock hour looped back around.
if (newHour < oldHour)
{
// Increment the day.
this->date.incrementDay();
}
}