forked from OpenXcom/OpenXcom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMod.cpp
More file actions
3768 lines (3508 loc) · 105 KB
/
Copy pathMod.cpp
File metadata and controls
3768 lines (3508 loc) · 105 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
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
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2010-2016 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenXcom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Mod.h"
#include <algorithm>
#include <sstream>
#include <climits>
#include <cassert>
#include "../Engine/CrossPlatform.h"
#include "../Engine/FileMap.h"
#include "../Engine/Palette.h"
#include "../Engine/Font.h"
#include "../Engine/Surface.h"
#include "../Engine/SurfaceSet.h"
#include "../Engine/Music.h"
#include "../Engine/GMCat.h"
#include "../Engine/SoundSet.h"
#include "../Engine/Sound.h"
#include "../Interface/TextButton.h"
#include "../Interface/Window.h"
#include "MapDataSet.h"
#include "RuleMusic.h"
#include "../Engine/ShaderDraw.h"
#include "../Engine/ShaderMove.h"
#include "../Engine/Exception.h"
#include "../Engine/Logger.h"
#include "SoundDefinition.h"
#include "ExtraSprites.h"
#include "ExtraSounds.h"
#include "../Engine/AdlibMusic.h"
#include "../fmath.h"
#include "../Engine/RNG.h"
#include "../Engine/Options.h"
#include "../Battlescape/Pathfinding.h"
#include "RuleCountry.h"
#include "RuleRegion.h"
#include "RuleBaseFacility.h"
#include "RuleCraft.h"
#include "RuleCraftWeapon.h"
#include "RuleItem.h"
#include "RuleUfo.h"
#include "RuleTerrain.h"
#include "MapScript.h"
#include "RuleSoldier.h"
#include "RuleCommendations.h"
#include "AlienRace.h"
#include "AlienDeployment.h"
#include "Armor.h"
#include "ArticleDefinition.h"
#include "RuleInventory.h"
#include "RuleResearch.h"
#include "RuleManufacture.h"
#include "ExtraStrings.h"
#include "RuleInterface.h"
#include "RuleMissionScript.h"
#include "../Geoscape/Globe.h"
#include "../Savegame/SavedGame.h"
#include "../Savegame/Region.h"
#include "../Savegame/Base.h"
#include "../Savegame/Country.h"
#include "../Savegame/Soldier.h"
#include "../Savegame/Craft.h"
#include "../Savegame/Vehicle.h"
#include "../Savegame/ItemContainer.h"
#include "../Savegame/Transfer.h"
#include "../Ufopaedia/Ufopaedia.h"
#include "../Savegame/AlienStrategy.h"
#include "../Savegame/GameTime.h"
#include "../Savegame/SoldierDiary.h"
#include "UfoTrajectory.h"
#include "RuleAlienMission.h"
#include "MCDPatch.h"
#include "StatString.h"
#include "RuleGlobe.h"
#include "RuleVideo.h"
#include "RuleConverter.h"
#define ARRAYLEN(x) (sizeof(x) / sizeof(x[0]))
namespace OpenXcom
{
int Mod::DOOR_OPEN;
int Mod::SLIDING_DOOR_OPEN;
int Mod::SLIDING_DOOR_CLOSE;
int Mod::SMALL_EXPLOSION;
int Mod::LARGE_EXPLOSION;
int Mod::EXPLOSION_OFFSET;
int Mod::SMOKE_OFFSET;
int Mod::UNDERWATER_SMOKE_OFFSET;
int Mod::ITEM_DROP;
int Mod::ITEM_THROW;
int Mod::ITEM_RELOAD;
int Mod::WALK_OFFSET;
int Mod::FLYING_SOUND;
int Mod::BUTTON_PRESS;
int Mod::WINDOW_POPUP[3];
int Mod::UFO_FIRE;
int Mod::UFO_HIT;
int Mod::UFO_CRASH;
int Mod::UFO_EXPLODE;
int Mod::INTERCEPTOR_HIT;
int Mod::INTERCEPTOR_EXPLODE;
int Mod::GEOSCAPE_CURSOR;
int Mod::BASESCAPE_CURSOR;
int Mod::BATTLESCAPE_CURSOR;
int Mod::UFOPAEDIA_CURSOR;
int Mod::GRAPHS_CURSOR;
int Mod::DAMAGE_RANGE;
int Mod::EXPLOSIVE_DAMAGE_RANGE;
int Mod::FIRE_DAMAGE_RANGE[2];
std::string Mod::DEBRIEF_MUSIC_GOOD;
std::string Mod::DEBRIEF_MUSIC_BAD;
int Mod::DIFFICULTY_COEFFICIENT[5];
/// Predefined name for first loaded mod that have all original data
const std::string ModNameMaster = "master";
/// Predefined name for current mod that is loading rulesets.
const std::string ModNameCurrent = "current";
/// Reduction of size allocated for transparcey LUTs.
const size_t ModTransparceySizeReduction = 100;
void Mod::resetGlobalStatics()
{
DOOR_OPEN = 3;
SLIDING_DOOR_OPEN = 20;
SLIDING_DOOR_CLOSE = 21;
SMALL_EXPLOSION = 2;
LARGE_EXPLOSION = 5;
EXPLOSION_OFFSET = 0;
SMOKE_OFFSET = 8;
UNDERWATER_SMOKE_OFFSET = 0;
ITEM_DROP = 38;
ITEM_THROW = 39;
ITEM_RELOAD = 17;
WALK_OFFSET = 22;
FLYING_SOUND = 15;
BUTTON_PRESS = 0;
WINDOW_POPUP[0] = 1;
WINDOW_POPUP[1] = 2;
WINDOW_POPUP[2] = 3;
UFO_FIRE = 8;
UFO_HIT = 12;
UFO_CRASH = 10;
UFO_EXPLODE = 11;
INTERCEPTOR_HIT = 10;
INTERCEPTOR_EXPLODE = 13;
GEOSCAPE_CURSOR = 252;
BASESCAPE_CURSOR = 252;
BATTLESCAPE_CURSOR = 144;
UFOPAEDIA_CURSOR = 252;
GRAPHS_CURSOR = 252;
DAMAGE_RANGE = 100;
EXPLOSIVE_DAMAGE_RANGE = 50;
FIRE_DAMAGE_RANGE[0] = 5;
FIRE_DAMAGE_RANGE[1] = 10;
DEBRIEF_MUSIC_GOOD = "GMMARS";
DEBRIEF_MUSIC_BAD = "GMMARS";
Globe::OCEAN_COLOR = Palette::blockOffset(12);
Globe::OCEAN_SHADING = true;
Globe::COUNTRY_LABEL_COLOR = 239;
Globe::LINE_COLOR = 162;
Globe::CITY_LABEL_COLOR = 138;
Globe::BASE_LABEL_COLOR = 133;
TextButton::soundPress = 0;
Window::soundPopup[0] = 0;
Window::soundPopup[1] = 0;
Window::soundPopup[2] = 0;
Pathfinding::red = 3;
Pathfinding::yellow = 10;
Pathfinding::green = 4;
DIFFICULTY_COEFFICIENT[0] = 0;
DIFFICULTY_COEFFICIENT[1] = 1;
DIFFICULTY_COEFFICIENT[2] = 2;
DIFFICULTY_COEFFICIENT[3] = 3;
DIFFICULTY_COEFFICIENT[4] = 4;
}
/**
* Creates an empty mod.
*/
Mod::Mod() : _costEngineer(0), _costScientist(0), _timePersonnel(0), _initialFunding(0), _turnAIUseGrenade(3), _turnAIUseBlaster(3), _defeatScore(0), _defeatFunds(0), _difficultyDemigod(false), _startingTime(6, 1, 1, 1999, 12, 0, 0),
_facilityListOrder(0), _craftListOrder(0), _itemListOrder(0), _researchListOrder(0), _manufactureListOrder(0), _ufopaediaListOrder(0), _invListOrder(0), _modCurrent(0), _statePalette(0)
{
_muteMusic = new Music();
_muteSound = new Sound();
_globe = new RuleGlobe();
_converter = new RuleConverter();
_statAdjustment[0].aimAndArmorMultiplier = 0.5;
_statAdjustment[0].growthMultiplier = 0;
for (int i = 1; i != 5; ++i)
{
_statAdjustment[i].aimAndArmorMultiplier = 1.0;
_statAdjustment[i].growthMultiplier = i;
}
}
/**
* Deletes all the mod data from memory.
*/
Mod::~Mod()
{
delete _muteMusic;
delete _muteSound;
delete _globe;
delete _converter;
for (std::map<std::string, Font*>::iterator i = _fonts.begin(); i != _fonts.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, Surface*>::iterator i = _surfaces.begin(); i != _surfaces.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, SurfaceSet*>::iterator i = _sets.begin(); i != _sets.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, Palette*>::iterator i = _palettes.begin(); i != _palettes.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, Music*>::iterator i = _musics.begin(); i != _musics.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, SoundSet*>::iterator i = _sounds.begin(); i != _sounds.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, RuleCountry*>::iterator i = _countries.begin(); i != _countries.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, RuleRegion*>::iterator i = _regions.begin(); i != _regions.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, RuleBaseFacility*>::iterator i = _facilities.begin(); i != _facilities.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, RuleCraft*>::iterator i = _crafts.begin(); i != _crafts.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, RuleCraftWeapon*>::iterator i = _craftWeapons.begin(); i != _craftWeapons.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, RuleItem*>::iterator i = _items.begin(); i != _items.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, RuleUfo*>::iterator i = _ufos.begin(); i != _ufos.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, RuleTerrain*>::iterator i = _terrains.begin(); i != _terrains.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, MapDataSet*>::iterator i = _mapDataSets.begin(); i != _mapDataSets.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, RuleSoldier*>::iterator i = _soldiers.begin(); i != _soldiers.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, Unit*>::iterator i = _units.begin(); i != _units.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, AlienRace*>::iterator i = _alienRaces.begin(); i != _alienRaces.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, AlienDeployment*>::iterator i = _alienDeployments.begin(); i != _alienDeployments.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, Armor*>::iterator i = _armors.begin(); i != _armors.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, ArticleDefinition*>::iterator i = _ufopaediaArticles.begin(); i != _ufopaediaArticles.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, RuleInventory*>::iterator i = _invs.begin(); i != _invs.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, RuleResearch *>::const_iterator i = _research.begin(); i != _research.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, RuleManufacture *>::const_iterator i = _manufacture.begin(); i != _manufacture.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, UfoTrajectory *>::const_iterator i = _ufoTrajectories.begin(); i != _ufoTrajectories.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, RuleAlienMission *>::const_iterator i = _alienMissions.begin(); i != _alienMissions.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, MCDPatch *>::const_iterator i = _MCDPatches.begin(); i != _MCDPatches.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, std::vector<ExtraSprites*> >::iterator i = _extraSprites.begin(); i != _extraSprites.end(); ++i)
{
for (std::vector<ExtraSprites*>::iterator j = i->second.begin(); j != i->second.end(); ++j)
{
delete *j;
}
}
for (std::vector< std::pair<std::string, ExtraSounds *> >::const_iterator i = _extraSounds.begin(); i != _extraSounds.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, ExtraStrings *>::const_iterator i = _extraStrings.begin(); i != _extraStrings.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, RuleInterface *>::const_iterator i = _interfaces.begin(); i != _interfaces.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, std::vector<MapScript*> >::iterator i = _mapScripts.begin(); i != _mapScripts.end(); ++i)
{
for (std::vector<MapScript*>::iterator j = (*i).second.begin(); j != (*i).second.end(); ++j)
{
delete *j;
}
}
for (std::map<std::string, RuleVideo *>::const_iterator i = _videos.begin(); i != _videos.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, RuleMusic *>::const_iterator i = _musicDefs.begin(); i != _musicDefs.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, RuleMissionScript*>::const_iterator i = _missionScripts.begin(); i != _missionScripts.end(); ++i)
{
delete i->second;
}
for (std::map<std::string, SoundDefinition*>::const_iterator i = _soundDefs.begin(); i != _soundDefs.end(); ++i)
{
delete i->second;
}
for (std::vector<StatString*>::const_iterator i = _statStrings.begin(); i != _statStrings.end(); ++i)
{
delete (*i);
}
for (std::map<std::string, RuleCommendations *>::const_iterator i = _commendations.begin(); i != _commendations.end(); ++i)
{
delete i->second;
}
}
/**
* Gets a specific rule element by ID.
* @param id String ID of the rule element.
* @param name Human-readable name of the rule type.
* @param map Map associated to the rule type.
* @param error Throw an error if not found.
* @return Pointer to the rule element, or NULL if not found.
*/
template <typename T>
T *Mod::getRule(const std::string &id, const std::string &name, const std::map<std::string, T*> &map, bool error) const
{
if (id.empty())
{
return 0;
}
typename std::map<std::string, T*>::const_iterator i = map.find(id);
if (i != map.end() && i->second != 0)
{
return i->second;
}
else
{
if (error)
{
throw Exception(name + " " + id + " not found");
}
return 0;
}
}
/**
* Returns a specific font from the mod.
* @param name Name of the font.
* @return Pointer to the font.
*/
Font *Mod::getFont(const std::string &name, bool error) const
{
return getRule(name, "Font", _fonts, error);
}
/**
* Loads any extra sprites associated to a surface when
* it's first requested.
* @param name Surface name.
*/
void Mod::lazyLoadSurface(const std::string &name)
{
if (Options::lazyLoadResources)
{
std::map<std::string, std::vector<ExtraSprites *> >::const_iterator i = _extraSprites.find(name);
if (i != _extraSprites.end())
{
for (std::vector<ExtraSprites*>::const_iterator j = i->second.begin(); j != i->second.end(); ++j)
{
loadExtraSprite(*j);
}
}
}
}
/**
* Returns a specific surface from the mod.
* @param name Name of the surface.
* @return Pointer to the surface.
*/
Surface *Mod::getSurface(const std::string &name, bool error)
{
lazyLoadSurface(name);
return getRule(name, "Sprite", _surfaces, error);
}
/**
* Returns a specific surface set from the mod.
* @param name Name of the surface set.
* @return Pointer to the surface set.
*/
SurfaceSet *Mod::getSurfaceSet(const std::string &name, bool error)
{
lazyLoadSurface(name);
return getRule(name, "Sprite Set", _sets, error);
}
/**
* Returns a specific music from the mod.
* @param name Name of the music.
* @return Pointer to the music.
*/
Music *Mod::getMusic(const std::string &name, bool error) const
{
if (Options::mute)
{
return _muteMusic;
}
else
{
return getRule(name, "Music", _musics, error);
}
}
/**
* Returns a random music from the mod.
* @param name Name of the music to pick from.
* @return Pointer to the music.
*/
Music *Mod::getRandomMusic(const std::string &name) const
{
if (Options::mute)
{
return _muteMusic;
}
else
{
std::vector<Music*> music;
for (std::map<std::string, Music*>::const_iterator i = _musics.begin(); i != _musics.end(); ++i)
{
if (i->first.find(name) != std::string::npos)
{
music.push_back(i->second);
}
}
if (music.empty())
{
return _muteMusic;
}
else
{
return music[RNG::seedless(0, music.size() - 1)];
}
}
}
/**
* Plays the specified track if it's not already playing.
* @param name Name of the music.
* @param id Id of the music, 0 for random.
*/
void Mod::playMusic(const std::string &name, int id)
{
if (!Options::mute && _playingMusic != name)
{
int loop = -1;
// hacks
if (!Options::musicAlwaysLoop && (name == "GMSTORY" || name == "GMWIN" || name == "GMLOSE"))
{
loop = 0;
}
Music *music = 0;
if (id == 0)
{
music = getRandomMusic(name);
}
else
{
std::ostringstream ss;
ss << name << id;
music = getMusic(ss.str());
}
music->play(loop);
if (music != _muteMusic)
{
_playingMusic = name;
}
}
}
/**
* Returns a specific sound set from the mod.
* @param name Name of the sound set.
* @return Pointer to the sound set.
*/
SoundSet *Mod::getSoundSet(const std::string &name, bool error) const
{
return getRule(name, "Sound Set", _sounds, error);
}
/**
* Returns a specific sound from the mod.
* @param set Name of the sound set.
* @param sound ID of the sound.
* @return Pointer to the sound.
*/
Sound *Mod::getSound(const std::string &set, unsigned int sound, bool error) const
{
if (Options::mute)
{
return _muteSound;
}
else
{
SoundSet *ss = getSoundSet(set, error);
if (ss != 0)
{
Sound *s = ss->getSound(sound);
if (s == 0 && error)
{
std::ostringstream err;
err << "Sound " << sound << " in " << set << " not found";
throw Exception(err.str());
}
return s;
}
else
{
return 0;
}
}
}
/**
* Returns a specific palette from the mod.
* @param name Name of the palette.
* @return Pointer to the palette.
*/
Palette *Mod::getPalette(const std::string &name, bool error) const
{
return getRule(name, "Palette", _palettes, error);
}
/**
* Changes the palette of all the graphics in the mod.
* @param colors Pointer to the set of colors.
* @param firstcolor Offset of the first color to replace.
* @param ncolors Amount of colors to replace.
*/
void Mod::setPalette(SDL_Color *colors, int firstcolor, int ncolors)
{
_statePalette = colors;
for (std::map<std::string, Font*>::iterator i = _fonts.begin(); i != _fonts.end(); ++i)
{
i->second->setPalette(colors, firstcolor, ncolors);
}
for (std::map<std::string, Surface*>::iterator i = _surfaces.begin(); i != _surfaces.end(); ++i)
{
if (!CrossPlatform::compareExt(i->first, "LBM"))
i->second->setPalette(colors, firstcolor, ncolors);
}
for (std::map<std::string, SurfaceSet*>::iterator i = _sets.begin(); i != _sets.end(); ++i)
{
i->second->setPalette(colors, firstcolor, ncolors);
}
}
/**
* Returns the list of voxeldata in the mod.
* @return Pointer to the list of voxeldata.
*/
std::vector<Uint16> *Mod::getVoxelData()
{
return &_voxelData;
}
/**
* Returns a specific sound from either the land or underwater sound set.
* @param depth the depth of the battlescape.
* @param sound ID of the sound.
* @return Pointer to the sound.
*/
Sound *Mod::getSoundByDepth(unsigned int depth, unsigned int sound, bool error) const
{
if (depth == 0)
return getSound("BATTLE.CAT", sound, error);
else
return getSound("BATTLE2.CAT", sound, error);
}
/**
* Returns the list of color LUTs in the mod.
* @return Pointer to the list of LUTs.
*/
const std::vector<std::vector<Uint8> > *Mod::getLUTs() const
{
return &_transparencyLUTs;
}
/**
* Returns the current mod-based offset for resources.
* @return Mod offset.
*/
int Mod::getModOffset() const
{
return _modCurrent->offset;
}
/**
* Get offset and index for sound set or sprite set.
* @param parent Name of parent node, used for better error message
* @param offset Member to load new value.
* @param node Node with data
* @param shared Max offset limit that is shared for every mod
* @param multiplier Value used by `projectile` surface set to convert projectile offset to index offset in surface.
* @param sizeScale Value used by transparency colors, reduce total number of avaialbe space for offset.
*/
void Mod::loadOffsetNode(const std::string &parent, int& offset, const YAML::Node &node, int shared, const std::string &set, size_t multiplier, size_t sizeScale) const
{
assert(_modCurrent);
const ModData* curr = _modCurrent;
if (node.IsScalar())
{
offset = node.as<int>();
}
else if (node.IsMap())
{
offset = node["index"].as<int>();
std::string mod = node["mod"].as<std::string>();
if (mod == ModNameMaster)
{
curr = &_modData.at(0);
}
else if (mod == ModNameCurrent)
{
//nothing
}
else
{
const ModData* n = 0;
for (size_t i = 0; i < _modData.size(); ++i)
{
const ModData& d = _modData[i];
if (d.name == mod)
{
n = &d;
break;
}
}
if (n)
{
curr = n;
}
else
{
std::ostringstream err;
err << "Error for '" << parent << "': unknown mod '" << mod << "' used";
throw Exception(err.str());
}
}
}
if (offset < -1)
{
std::ostringstream err;
err << "Error for '" << parent << "': offset '" << offset << "' has incorrect value in set '" << set << "' at line " << node.Mark().line;
throw Exception(err.str());
}
else if (offset == -1)
{
//ok
}
else
{
int f = offset;
f *= multiplier;
if ((size_t)f > curr->size / sizeScale)
{
std::ostringstream err;
err << "Error for '" << parent << "': offset '" << offset << "' exceeds mod size limit " << (curr->size / multiplier / sizeScale) << " in set '" << set << "'";
throw Exception(err.str());
}
if (f >= shared)
f += curr->offset / sizeScale;
offset = f;
}
}
/**
* Returns the appropriate mod-based offset for a sprite.
* If the ID is bigger than the surfaceset contents, the mod offset is applied.
* @param parent Name of parent node, used for better error message
* @param sprite Member to load new sprite ID index.
* @param node Node with data
* @param set Name of the surfaceset to lookup.
* @param multiplier Value used by `projectile` surface set to convert projectile offset to index offset in surface.
*/
void Mod::loadSpriteOffset(const std::string &parent, int& sprite, const YAML::Node &node, const std::string &set, size_t multiplier) const
{
if (node)
{
loadOffsetNode(parent, sprite, node, getRule(set, "Sprite Set", _sets, true)->getMaxSharedFrames(), set, multiplier);
}
}
/**
* Returns the appropriate mod-based offset for a sound.
* If the ID is bigger than the soundset contents, the mod offset is applied.
* @param parent Name of parent node, used for better error message
* @param sound Member to load new sound ID index.
* @param node Node with data
* @param set Name of the soundset to lookup.
*/
void Mod::loadSoundOffset(const std::string &parent, int& sound, const YAML::Node &node, const std::string &set) const
{
if (node)
{
loadOffsetNode(parent, sound, node, getSoundSet(set)->getMaxSharedSounds(), set, 1);
}
}
/**
* Gets the mod offset array for a certain transparency index.
* @param parent Name of parent node, used for better error message.
* @param index Member to load new transparency index.
* @param node Node with data.
*/
void Mod::loadTransparencyOffset(const std::string &parent, int& index, const YAML::Node &node) const
{
if (node)
{
loadOffsetNode(parent, index, node, 0, "TransparencyLUTs", 1, ModTransparceySizeReduction);
}
}
/**
* Gets the mod offset array for a certain sound.
* @param parent Name of parent node, used for better error message
* @param sounds Member to load new list of sound ID indexes.
* @param node Node with data
* @param set Name of the soundset to lookup.
*/
void Mod::loadSoundOffset(const std::string &parent, std::vector<int>& sounds, const YAML::Node &node, const std::string &set) const
{
if (node)
{
int maxShared = getSoundSet(set)->getMaxSharedSounds();
sounds.clear();
if (node.IsSequence())
{
for (YAML::const_iterator i = node.begin(); i != node.end(); ++i)
{
sounds.push_back(-1);
loadOffsetNode(parent, sounds.back(), *i, maxShared, set, 1);
}
}
else
{
sounds.push_back(-1);
loadOffsetNode(parent, sounds.back(), node, maxShared, set, 1);
}
}
}
/**
* Returns the appropriate mod-based offset for a generic ID.
* If the ID is bigger than the max, the mod offset is applied.
* @param id Numeric ID.
* @param max Maximum vanilla value.
*/
int Mod::getOffset(int id, int max) const
{
assert(_modCurrent);
if (id > max)
return id + _modCurrent->offset;
else
return id;
}
/**
* Helper function used to disable invalid mod and throw exception to quit game
* @param modId Mod id
* @param error Error message
*/
static void throwModOnErrorHelper(const std::string& modId, const std::string& error)
{
std::ostringstream errorStream;
errorStream << "failed to load '"
<< Options::getModInfos().at(modId).getName()
<< "'";
if (!Options::debug)
{
Log(LOG_WARNING) << "disabling mod with invalid ruleset: " << modId;
std::vector<std::pair<std::string, bool> >::iterator it =
std::find(Options::mods.begin(), Options::mods.end(),
std::pair<std::string, bool>(modId, true));
if (it == Options::mods.end())
{
Log(LOG_ERROR) << "cannot find broken mod in mods list: " << modId;
Log(LOG_ERROR) << "clearing mods list";
Options::mods.clear();
}
else
{
it->second = false;
}
Options::save();
errorStream << "; mod disabled";
}
errorStream << std::endl << error;
throw Exception(errorStream.str());
}
/**
* Loads a list of mods specified in the options.
* @param mods List of <modId, rulesetFiles> pairs.
*/
void Mod::loadAll(const std::vector< std::pair< std::string, std::vector<std::string> > > &mods)
{
Log(LOG_INFO) << "Loading rulesets...";
_modData.clear();
_modData.resize(mods.size());
std::set<std::string> usedModNames;
usedModNames.insert(ModNameMaster);
usedModNames.insert(ModNameCurrent);
// calculated offsets and other things for all mods
size_t offset = 0;
for (size_t i = 0; mods.size() > i; ++i)
{
const std::string& modId = mods[i].first;
if (usedModNames.insert(modId).second == false)
{
throwModOnErrorHelper(modId, "this mod name is already used");
}
const ModInfo *modInfo = &Options::getModInfos().at(modId);
size_t size = modInfo->getReservedSpace();
_modData[i].name = modId;
_modData[i].offset = 1000 * offset;
_modData[i].info = modInfo;
_modData[i].size = 1000 * size;
offset += size;
}
// load rulesets that can affect loading vanilla resources
for (size_t i = 0; _modData.size() > i; ++i)
{
_modCurrent = &_modData.at(i);
const ModInfo *info = _modCurrent->info;
if (info->isMaster() && !info->getResourceConfigFile().empty())
{
std::string path = info->getPath() + "/" + info->getResourceConfigFile();
if (CrossPlatform::fileExists(path))
{
loadResourceConfigFile(path);
}
}
}
// vanilla resources load
_modCurrent = &_modData.at(0);
loadVanillaResources();
// load rest rulesets
for (size_t i = 0; mods.size() > i; ++i)
{
try
{
_modCurrent = &_modData.at(i);
loadMod(mods[i].second);
}
catch (Exception &e)
{
const std::string &modId = mods[i].first;
throwModOnErrorHelper(modId, e.what());
}
}
//back master
_modCurrent = &_modData.at(0);
sortLists();
loadExtraResources();
modResources();
}
/**
* Loads a list of rulesets from YAML files for the mod at the specified index. The first
* mod loaded should be the master at index 0, then 1, and so on.
* @param rulesetFiles List of rulesets to load.
*/
void Mod::loadMod(const std::vector<std::string> &rulesetFiles)
{
for (std::vector<std::string>::const_iterator i = rulesetFiles.begin(); i != rulesetFiles.end(); ++i)
{
Log(LOG_VERBOSE) << "- " << *i;
try
{
loadFile(*i);
}
catch (YAML::Exception &e)
{
throw Exception((*i) + ": " + std::string(e.what()));
}
}
// these need to be validated, otherwise we're gonna get into some serious trouble down the line.
// it may seem like a somewhat arbitrary limitation, but there is a good reason behind it.
// i'd need to know what results are going to be before they are formulated, and there's a hierarchical structure to
// the order in which variables are determined for a mission, and the order is DIFFERENT for regular missions vs
// missions that spawn a mission site. where normally we pick a region, then a mission based on the weights for that region.
// a terror-type mission picks a mission type FIRST, then a region based on the criteria defined by the mission.
// there is no way i can conceive of to reconcile this difference to allow mixing and matching,
// short of knowing the results of calls to the RNG before they're determined.
// the best solution i can come up with is to disallow it, as there are other ways to achieve what this would amount to anyway,
// and they don't require time travel. - Warboy
for (std::map<std::string, RuleMissionScript*>::iterator i = _missionScripts.begin(); i != _missionScripts.end(); ++i)
{
RuleMissionScript *rule = (*i).second;
std::set<std::string> missions = rule->getAllMissionTypes();
if (!missions.empty())
{
std::set<std::string>::const_iterator j = missions.begin();
if (!getAlienMission(*j))
{
throw Exception("Error with MissionScript: " + (*i).first + ": alien mission type: " + *j + " not defined, do not incite the judgement of Amaunator.");
}
bool isSiteType = getAlienMission(*j)->getObjective() == OBJECTIVE_SITE;
rule->setSiteType(isSiteType);
for (;j != missions.end(); ++j)
{