/
def_main.cpp
2231 lines (1903 loc) · 61.6 KB
/
def_main.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
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
/** @file def_main.cpp
*
* Definitions Subsystem.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2006 Jamie Jones <jamie_jones_au@yahoo.com.au>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program 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 2 of the License, or (at your
* option) any later version. This program 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 this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include <string.h>
#include <ctype.h>
#include <de/NativePath>
#include "de_base.h"
#include "de_system.h"
#include "de_platform.h"
#include "de_console.h"
#include "de_audio.h"
#include "de_misc.h"
#include "de_graphics.h"
#include "de_ui.h"
#include "de_filesys.h"
#include "de_resource.h"
#define DENG_NO_API_MACROS_DEFINITIONS
#include "api_def.h"
#include "map/r_world.h"
// XGClass.h is actually a part of the engine.
#include "../../../plugins/common/include/xgclass.h"
#define LOOPi(n) for(i = 0; i < (n); ++i)
#define LOOPk(n) for(k = 0; k < (n); ++k)
using namespace de;
typedef struct {
char* name; // Name of the routine.
void (*func)(); // Pointer to the function.
} actionlink_t;
void Def_ReadProcessDED(const char* filename);
ded_t defs; // The main definitions database.
boolean firstDED;
sprname_t* sprNames; // Sprite name list.
ded_count_t countSprNames;
state_t* states; // State list.
ded_count_t countStates;
mobjinfo_t* mobjInfo; // Map object info database.
ded_count_t countMobjInfo;
sfxinfo_t* sounds; // Sound effect list.
ded_count_t countSounds;
ddtext_t* texts; // Text list.
ded_count_t countTexts;
mobjinfo_t** stateOwners; // A pointer for each State.
ded_count_t countStateOwners;
ded_light_t** stateLights; // A pointer for each State.
ded_count_t countStateLights;
ded_ptcgen_t** statePtcGens; // A pointer for each State.
ded_count_t countStatePtcGens;
static boolean defsInited = false;
static mobjinfo_t* gettingFor;
xgclass_t nullXgClassLinks; // Used when none defined.
xgclass_t* xgClassLinks;
static void defineFlaremap(uri_s const *_resourceUri);
static void defineLightmap(uri_s const *_resourceUri);
/**
* Retrieves the XG Class list from the Game.
* XGFunc links are provided by the Game, who owns the actual
* XG classes and their functions.
*/
int Def_GetGameClasses(void)
{
xgClassLinks = 0;
if(gx.GetVariable)
xgClassLinks = (xgclass_t*) gx.GetVariable(DD_XGFUNC_LINK);
if(!xgClassLinks)
{
memset(&nullXgClassLinks, 0, sizeof(nullXgClassLinks));
xgClassLinks = &nullXgClassLinks;
}
return 1;
}
/**
* Initializes the definition databases.
*/
void Def_Init(void)
{
sprNames = 0;
DED_ZCount(&countSprNames);
mobjInfo = 0;
DED_ZCount(&countMobjInfo);
states = 0;
DED_ZCount(&countStates);
sounds = 0;
DED_ZCount(&countSounds);
texts = 0;
DED_ZCount(&countTexts);
stateOwners = 0;
DED_ZCount(&countStateOwners);
statePtcGens = 0;
DED_ZCount(&countStatePtcGens);
stateLights = 0;
DED_ZCount(&countStateLights);
DED_Init(&defs);
}
void Def_Destroy(void)
{
int i;
// To make sure...
DED_Clear(&defs);
DED_Init(&defs);
// Destroy the databases.
DED_DelArray((void**) &sprNames, &countSprNames);
DED_DelArray((void**) &states, &countStates);
DED_DelArray((void**) &mobjInfo, &countMobjInfo);
for(i = 0; i < countSounds.num; ++i)
{
Str_Free(&sounds[i].external);
}
DED_DelArray((void**) &sounds, &countSounds);
DED_DelArray((void**) &texts, &countTexts);
DED_DelArray((void**) &stateOwners, &countStateOwners);
DED_DelArray((void**) &statePtcGens, &countStatePtcGens);
DED_DelArray((void**) &stateLights, &countStateLights);
defsInited = false;
}
/**
* @return Number of the given sprite else,
* @c -1, if it doesn't exist.
*/
int Def_GetSpriteNum(const char* name)
{
int i;
if(!name || !name[0])
return -1;
for(i = 0; i < countSprNames.num; ++i)
if(!stricmp(sprNames[i].name, name))
return i;
return -1;
}
int Def_GetMobjNum(const char* id)
{
int i;
if(!id || !id[0])
return -1;
for(i = 0; i < defs.count.mobjs.num; ++i)
if(!stricmp(defs.mobjs[i].id, id))
return i;
return -1;
}
int Def_GetMobjNumForName(const char* name)
{
int i;
if(!name || !name[0])
return -1;
for(i = defs.count.mobjs.num -1; i >= 0; --i)
if(!stricmp(defs.mobjs[i].name, name))
return i;
return -1;
}
const char* Def_GetMobjName(int num)
{
if(num < 0) return "(<0)";
if(num >= defs.count.mobjs.num) return "(>mobjtypes)";
return defs.mobjs[num].id;
}
int Def_GetStateNum(const char* id)
{
int idx = -1;
if(id && id[0] && defs.count.states.num)
{
int i = 0;
do
{
if(!stricmp(defs.states[i].id, id))
idx = i;
} while(idx == -1 && ++i < defs.count.states.num);
}
return idx;
}
int Def_GetModelNum(const char* id)
{
int idx = -1;
if(id && id[0] && defs.count.models.num)
{
int i = 0;
do
{
if(!stricmp(defs.models[i].id, id))
idx = i;
} while(idx == -1 && ++i < defs.count.models.num);
}
return idx;
}
int Def_GetSoundNum(const char* id)
{
int idx = -1;
if(id && id[0] && defs.count.sounds.num)
{
int i = 0;
do
{
if(!stricmp(defs.sounds[i].id, id))
idx = i;
} while(idx == -1 && ++i < defs.count.sounds.num);
}
return idx;
}
/**
* Looks up a sound using the Name key. If the name is not found, returns
* the NULL sound index (zero).
*/
int Def_GetSoundNumForName(const char* name)
{
int i;
if(!name || !name[0])
return -1;
for(i = 0; i < defs.count.sounds.num; ++i)
if(!stricmp(defs.sounds[i].name, name))
return i;
return 0;
}
int Def_GetMusicNum(const char* id)
{
int idx = -1;
if(id && id[0] && defs.count.music.num)
{
int i = 0;
do
{
if(!stricmp(defs.music[i].id, id))
idx = i;
} while(idx == -1 && ++i < defs.count.music.num);
}
return idx;
}
acfnptr_t Def_GetActionPtr(const char* name)
{
actionlink_t* linkIt;
if(!name || !name[0]) return 0;
if(!DD_GameLoaded()) return 0;
// Action links are provided by the game, who owns the actual action functions.
for(linkIt = (actionlink_t*) gx.GetVariable(DD_ACTION_LINK);
linkIt && linkIt->name; linkIt++)
{
actionlink_t* link = linkIt;
if(!stricmp(name, link->name))
return link->func;
}
return 0;
}
int Def_GetActionNum(const char* name)
{
if(name && name[0] && DD_GameLoaded())
{
// Action links are provided by the game, who owns the actual action functions.
actionlink_t* links = (actionlink_t*) gx.GetVariable(DD_ACTION_LINK);
actionlink_t* linkIt;
for(linkIt = links; linkIt && linkIt->name; linkIt++)
{
actionlink_t* link = linkIt;
if(!stricmp(name, link->name))
return linkIt - links;
}
}
return -1; // Not found.
}
ded_value_t* Def_GetValueById(char const* id)
{
if(!id || !id[0]) return NULL;
// Read backwards to allow patching.
for(int i = defs.count.values.num - 1; i >= 0; i--)
{
if(!stricmp(defs.values[i].id, id))
return defs.values + i;
}
return 0;
}
ded_value_t* Def_GetValueByUri(struct uri_s const *_uri)
{
if(!_uri) return 0;
de::Uri const& uri = reinterpret_cast<de::Uri const&>(*_uri);
if(uri.scheme().compareWithoutCase("Values")) return 0;
return Def_GetValueById(uri.pathCStr());
}
ded_mapinfo_t* Def_GetMapInfo(struct uri_s const *_uri)
{
if(!_uri) return 0;
de::Uri const& uri = reinterpret_cast<de::Uri const&>(*_uri);
for(int i = defs.count.mapInfo.num - 1; i >= 0; i--)
{
if(defs.mapInfo[i].uri && uri == reinterpret_cast<de::Uri const&>(*defs.mapInfo[i].uri))
return defs.mapInfo + i;
}
return 0;
}
ded_sky_t* Def_GetSky(char const* id)
{
if(!id || !id[0]) return NULL;
for(int i = defs.count.skies.num - 1; i >= 0; i--)
{
if(!stricmp(defs.skies[i].id, id))
return defs.skies + i;
}
return NULL;
}
static ded_material_t* findMaterialDef(de::Uri const& uri)
{
for(int i = defs.count.materials.num - 1; i >= 0; i--)
{
ded_material_t* def = &defs.materials[i];
if(!def->uri || uri != reinterpret_cast<de::Uri&>(*def->uri)) continue;
return def;
}
return NULL;
}
ded_material_t* Def_GetMaterial(char const* uriCString)
{
ded_material_t* def = NULL;
if(uriCString && uriCString[0])
{
de::Uri uri = de::Uri(uriCString, RC_NULL);
if(uri.scheme().isEmpty())
{
// Caller doesn't care which scheme - use a priority search order.
de::Uri temp = de::Uri(uri);
temp.setScheme("Sprites");
def = findMaterialDef(temp);
if(!def)
{
temp.setScheme("Textures");
def = findMaterialDef(temp);
}
if(!def)
{
temp.setScheme("Flats");
def = findMaterialDef(temp);
}
}
if(!def)
{
def = findMaterialDef(uri);
}
}
return def;
}
static ded_compositefont_t* findCompositeFontDef(de::Uri const& uri)
{
for(int i = defs.count.compositeFonts.num - 1; i >= 0; i--)
{
ded_compositefont_t* def = &defs.compositeFonts[i];
if(!def->uri || uri != reinterpret_cast<de::Uri&>(*def->uri)) continue;
return def;
}
return NULL;
}
ded_compositefont_t* Def_GetCompositeFont(char const* uriCString)
{
ded_compositefont_t* def = NULL;
if(uriCString && uriCString[0])
{
de::Uri uri = de::Uri(uriCString, RC_NULL);
if(uri.scheme().isEmpty())
{
// Caller doesn't care which scheme - use a priority search order.
de::Uri temp = de::Uri(uri);
temp.setScheme("Game");
def = findCompositeFontDef(temp);
if(!def)
{
temp.setScheme("System");
def = findCompositeFontDef(temp);
}
}
if(!def)
{
def = findCompositeFontDef(uri);
}
}
return def;
}
/// @todo $revise-texture-animation
ded_decor_t *Def_GetDecoration(uri_s const *uri /*, boolean hasExternal, boolean isCustom*/)
{
DENG_ASSERT(uri);
ded_decor_t *def;
int i;
for(i = defs.count.decorations.num - 1, def = defs.decorations + i; i >= 0; i--, def--)
{
if(def->material && Uri_Equality(def->material, uri))
{
// Is this suitable?
//if(Def_IsAllowedDecoration(def, hasExternal, isCustom))
return def;
}
}
return 0; // None found.
}
/// @todo $revise-texture-animation
ded_reflection_t *Def_GetReflection(uri_s const *uri /*, boolean hasExternal, boolean isCustom*/)
{
DENG_ASSERT(uri);
ded_reflection_t *def;
int i;
for(i = defs.count.reflections.num - 1, def = defs.reflections + i; i >= 0; i--, def--)
{
if(def->material && Uri_Equality(def->material, uri))
{
// Is this suitable?
//if(Def_IsAllowedReflection(def, hasExternal, isCustom))
return def;
}
}
return 0; // None found.
}
/// @todo $revise-texture-animation
ded_detailtexture_t *Def_GetDetailTex(uri_s const *uri /*, boolean hasExternal, boolean isCustom*/)
{
DENG_ASSERT(uri);
ded_detailtexture_t *def;
int i;
for(i = defs.count.details.num - 1, def = defs.details + i; i >= 0; i--, def--)
{
if(def->material1 && Uri_Equality(def->material1, uri))
{
// Is this suitable?
//if(Def_IsAllowedDetailTex(def, hasExternal, isCustom))
return def;
}
if(def->material2 && Uri_Equality(def->material2, uri))
{
// Is this suitable?
//if(Def_IsAllowedDetailTex(def, hasExternal, isCustom))
return def;
}
}
return 0; // None found.
}
ded_ptcgen_t* Def_GetGenerator(uri_s const *uri)
{
DENG_ASSERT(uri);
ded_ptcgen_t *def = defs.ptcGens;
for(int i = 0; i < defs.count.ptcGens.num; ++i, def++)
{
if(!def->material) continue;
// Is this suitable?
if(Uri_Equality(def->material, uri))
return def;
#if 0 /// @todo $revise-texture-animation
if(def->flags & PGF_GROUP)
{
/**
* Generator triggered by all materials in the (animation) group.
* A search is necessary only if we know both the used material and
* the specified material in this definition are in *a* group.
*/
if(Material_IsGroupAnimated(defMat) && Material_IsGroupAnimated(mat) &&
&Material_AnimGroup(defMat) == &Material_AnimGroup(mat))
{
// Both are in this group! This def will do.
return def;
}
}
#endif
}
return 0; // None found.
}
ded_ptcgen_t* Def_GetDamageGenerator(int mobjType)
{
// Search for a suitable definition.
ded_ptcgen_t *def = defs.ptcGens;
for(int i = 0; i < defs.count.ptcGens.num; ++i, def++)
{
// It must be for this type of mobj.
if(def->damageNum == mobjType)
return def;
}
return 0;
}
ded_flag_t *Def_GetFlag(char const *flag)
{
if(!flag || !flag[0])
{
DEBUG_Message(("Attempted Def_GetFlagValue with %s flag argument.\n",
flag? "zero-length" : "<null>"));
return 0;
}
for(int i = defs.count.flags.num - 1; i >= 0; i--)
{
if(!stricmp(defs.flags[i].id, flag))
return defs.flags + i;
}
return 0;
}
/**
* Attempts to retrieve a flag by its prefix and value.
* Returns a ptr to the text string of the first flag it
* finds that matches the criteria, else NULL.
*/
const char* Def_GetFlagTextByPrefixVal(const char* prefix, int val)
{
int i;
for(i = defs.count.flags.num - 1; i >= 0; i--)
{
if(strnicmp(defs.flags[i].id, prefix, strlen(prefix)) == 0 && defs.flags[i].value == val)
return defs.flags[i].text;
}
return NULL;
}
#undef Def_EvalFlags
int Def_EvalFlags(char *ptr)
{
LOG_AS("Def_EvalFlags");
int value = 0;
while(*ptr)
{
ptr = M_SkipWhite(ptr);
int flagNameLength = M_FindWhite(ptr) - ptr;
String flagName(ptr, flagNameLength);
ptr += flagNameLength;
if(ded_flag_t *flag = Def_GetFlag(flagName.toUtf8().constData()))
{
value |= flag->value;
}
else
{
LOG_WARNING("Flag '%s' is not defined (or used out of context).") << flagName;
}
}
return value;
}
int Def_GetTextNumForName(const char* name)
{
int idx = -1;
if(name && name[0] && defs.count.text.num)
{
int i = 0;
do
{
if(!stricmp(defs.text[i].id, name))
idx = i;
} while(idx == -1 && ++i < defs.count.text.num);
}
return idx;
}
/**
* The following escape sequences are un-escaped:
* <pre>
* \n Newline
* \r Carriage return
* \t Tab
* \_ Space
* \s Space
* </pre>
*/
static void Def_InitTextDef(ddtext_t* txt, char const* str)
{
// Handle null pointers with "".
if(!str) str = "";
txt->text = (char*) M_Calloc(strlen(str) + 1);
char const* in = str;
char* out = txt->text;
for(; *in; out++, in++)
{
if(*in == '\\')
{
in++;
if(*in == 'n') *out = '\n'; // Newline.
else if(*in == 'r') *out = '\r'; // Carriage return.
else if(*in == 't') *out = '\t'; // Tab.
else if(*in == '_'
|| *in == 's') *out = ' '; // Space.
else
{
*out = *in;
}
}
else
{
*out = *in;
}
}
// Adjust buffer to fix exactly.
txt->text = (char*) M_Realloc(txt->text, strlen(txt->text) + 1);
}
void Def_ReadProcessDED(char const* path)
{
LOG_AS("Def_ReadProcessDED");
if(!path || !path[0]) return;
de::Uri path_ = de::Uri(path, RC_NULL);
if(!App_FileSystem()->accessFile(path_))
{
LOG_WARNING("\"%s\" not found!") << NativePath(path_.asText()).pretty();
return;
}
// We use the File Ids to prevent loading the same files multiple times.
if(!App_FileSystem()->checkFileId(path_))
{
// Already handled.
LOG_DEBUG("\"%s\" has already been read.") << NativePath(path_.asText()).pretty();
return;
}
if(!DED_Read(&defs, path))
{
Con_Error("Def_ReadProcessDED: %s\n", dedReadError);
}
}
/**
* Prints a count with a 2-space indentation.
*/
void Def_CountMsg(int count, const char* label)
{
if(!verbose && !count)
return; // Don't print zeros if not verbose.
Con_Message("%5i %s\n", count, label);
}
/**
* Read all DD_DEFNS lumps in the primary lump index.
*/
void Def_ReadLumpDefs(void)
{
int numProcessedLumps = 0;
DENG2_FOR_EACH_CONST(LumpIndex::Lumps, i, App_FileSystem()->nameIndex().lumps())
{
de::File1 const& lump = **i;
if(!lump.name().beginsWith("DD_DEFNS", Qt::CaseInsensitive)) continue;
numProcessedLumps += 1;
if(!DED_ReadLump(&defs, lump.info().lumpIdx))
{
QByteArray path = NativePath(lump.container().composePath()).pretty().toUtf8();
Con_Error("DD_ReadLumpDefs: Parse error reading \"%s:DD_DEFNS\".\n", path.constData());
}
}
if(verbose && numProcessedLumps > 0)
{
Con_Message("ReadLumpDefs: %i definition lump%s read.\n", numProcessedLumps, numProcessedLumps != 1 ? "s" : "");
}
}
/**
* Uses gettingFor. Initializes the state-owners information.
*/
int Def_StateForMobj(const char* state)
{
int num = Def_GetStateNum(state);
int st, count = 16;
if(num < 0)
num = 0;
// State zero is the NULL state.
if(num > 0)
{
stateOwners[num] = gettingFor;
// Scan forward at most 'count' states, or until we hit a state with
// an owner, or the NULL state.
for(st = states[num].nextState; st > 0 && count-- && !stateOwners[st];
st = states[st].nextState)
{
stateOwners[st] = gettingFor;
}
}
return num;
}
int Def_GetIntValue(char* val, int* returned_val)
{
char* data;
// First look for a DED Value
if(Def_Get(DD_DEF_VALUE, val, &data) >= 0)
{
*returned_val = strtol(data, 0, 0);
return true;
}
// Convert the literal string
*returned_val = strtol(val, 0, 0);
return false;
}
static __inline void readDefinitionFile(const char* fileName)
{
if(!fileName || !fileName[0])
return;
Def_ReadProcessDED(fileName);
}
static void readAllDefinitions(void)
{
uint startTime = Timer_RealMilliseconds();
ddstring_t buf;
int p;
// Start with engine's own top-level definition file, it is always read first.
de::Uri searchPath = de::Uri("doomsday.ded", RC_DEFINITION);
AutoStr* foundPath = AutoStr_NewStd();
if(F_FindPath(RC_DEFINITION, reinterpret_cast<uri_s*>(&searchPath), foundPath))
{
VERBOSE2( Con_Message(" Processing '%s'...\n", F_PrettyPath(Str_Text(foundPath))) )
readDefinitionFile(Str_Text(foundPath));
}
else
{
Con_Error("readAllDefinitions: Error, failed to locate main engine definition file \"doomsday.ded\".");
}
// Now any definition files required by the game on load.
if(DD_GameLoaded())
{
de::Game::Manifests const& gameResources = reinterpret_cast<de::Game *>(App_CurrentGame())->manifests();
int packageIdx = 0;
for(de::Game::Manifests::const_iterator i = gameResources.find(RC_DEFINITION);
i != gameResources.end() && i.key() == RC_DEFINITION; ++i, ++packageIdx)
{
Manifest &record = **i;
/// Try to locate this resource now.
QString const &path = record.resolvedPath(true/*try to locate*/);
if(path.isEmpty())
{
QByteArray names = record.names().join(";").toUtf8();
Con_Error("readAllDefinitions: Error, failed to locate required game definition \"%s\".", names.constData());
}
QByteArray pathUtf8 = path.toUtf8();
LOG_VERBOSE(" Processing '%s'...") << F_PrettyPath(pathUtf8.constData());
readDefinitionFile(pathUtf8.constData());
}
}
// Next up are definition files in the Games' /auto directory.
if(!CommandLine_Exists("-noauto") && DD_GameLoaded())
{
FS1::PathList found;
if(App_FileSystem()->findAllPaths(de::Uri("$(App.DefsPath)/$(GamePlugin.Name)/auto/*.ded", RC_NULL).resolved(), 0, found))
{
DENG2_FOR_EACH_CONST(FS1::PathList, i, found)
{
// Ignore directories.
if(i->attrib & A_SUBDIR) continue;
QByteArray foundPathUtf8 = i->path.toUtf8();
readDefinitionFile(foundPathUtf8.constData());
}
}
}
// Any definition files on the command line?
Str_Init(&buf);
for(p = 0; p < CommandLine_Count(); ++p)
{
const char* arg = CommandLine_At(p);
if(!CommandLine_IsMatchingAlias("-def", arg) &&
!CommandLine_IsMatchingAlias("-defs", arg)) continue;
while(++p != CommandLine_Count() && !CommandLine_IsOption(p))
{
const char* searchPath = CommandLine_PathAt(p);
Con_Message(" Processing '%s'...\n", F_PrettyPath(searchPath));
Str_Clear(&buf); Str_Set(&buf, searchPath);
F_FixSlashes(&buf, &buf);
F_ExpandBasePath(&buf, &buf);
// We must have an absolute path. If we still do not have one then
// prepend the current working directory if necessary.
F_PrependWorkPath(&buf, &buf);
readDefinitionFile(Str_Text(&buf));
}
p--; /* For ArgIsOption(p) necessary, for p==Argc() harmless */
}
Str_Free(&buf);
// Read DD_DEFNS definition lumps.
Def_ReadLumpDefs();
VERBOSE2( Con_Message(" Done in %.2f seconds.\n", (Timer_RealMilliseconds() - startTime) / 1000.0f) );
}
static animgroup_t const *findAnimGroupForTexture(TextureManifest &manifest)
{
// Group ids are 1-based.
// Search backwards to allow patching.
for(int i = R_AnimGroupCount(); i > 0; i--)
{
animgroup_t const *anim = R_ToAnimGroup(i);
for(int j = 0; j < anim->count; ++j)
{
animframe_t const *frame = &anim->frames[j];
if(!frame->textureManifest) continue;
if(&manifest == frame->textureManifest)
{
return anim;
}
}
}
return 0; // Not found.
}
static void generateMaterialDefForTexture(TextureManifest &manifest)
{
LOG_AS("generateMaterialDefForTexture");
de::Uri texUri = manifest.composeUri();
int matIdx = DED_AddMaterial(&defs, 0);
ded_material_t *mat = &defs.materials[matIdx];
mat->autoGenerated = true;
mat->uri = reinterpret_cast<uri_s *>(new de::Uri(DD_MaterialSchemeNameForTextureScheme(texUri.scheme()), texUri.path()));
if(Texture *tex = manifest.texture())
{
mat->width = tex->width();
mat->height = tex->height();
mat->flags = (tex->flags().testFlag(Texture::NoDraw)? MATF_NO_DRAW : 0);
}
#if _DEBUG
else
{
LOG_DEBUG("Texture \"%s\" not yet defined, resultant Material will inherit dimensions.") << texUri;
}
#endif
// The first stage is implicit.
ded_material_layer_stage_t *st =
&mat->layers[0].stages[DED_AddMaterialLayerStage(&mat->layers[0])];
st->texture = reinterpret_cast<uri_s *>(new de::Uri(texUri));
// Is there an animation for this?
animgroup_t const *anim = findAnimGroupForTexture(manifest);
if(anim && anim->count > 1)
{
animframe_t const *animFrame;
// Determine the start frame.
int startFrame = 0;
while(anim->frames[startFrame].textureManifest != &manifest)
{
startFrame++;
}
// Just animate the first in the sequence?
if(startFrame && (anim->flags & AGF_FIRST_ONLY))
return;
// Complete configuration of the first stage.
animFrame = &anim->frames[startFrame];
st->tics = animFrame->tics + animFrame->randomTics;
if(animFrame->randomTics)
{
st->variance = animFrame->randomTics / float( st->tics );
}
// Add further stages according to the animation group.
startFrame++;
for(int i = 0; i < anim->count - 1; ++i)
{
int frame = startFrame + i;
// Wrap around?
if(frame >= anim->count) frame -= anim->count;
animFrame = &anim->frames[frame];
if(!animFrame->textureManifest) continue;
TextureManifest &frameManifest = *reinterpret_cast<TextureManifest *>(animFrame->textureManifest);
ded_material_layer_stage_t *st =
&mat->layers[0].stages[DED_AddMaterialLayerStage(&mat->layers[0])];