This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathresource.cpp
More file actions
1858 lines (1640 loc) · 76 KB
/
resource.cpp
File metadata and controls
1858 lines (1640 loc) · 76 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
#include <algorithm>
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include "core/lua.h"
#include "core/system.h"
#include "core/vmath.h"
#include "core/gl_util.h"
#include "core/gl_text.h"
#include "core/console.h"
#include "core/polygon.h"
#include "core/obb.h"
#include "render/camera.h"
#include "render/render.h"
#include "render/frustum.h"
#include "render/bordered_texture_atlas.h"
#include "render/shader_description.h"
#include "audio/audio.h"
#include "script/script.h"
#include "physics/physics.h"
#include "vt/vt_level.h"
#include "room.h"
#include "trigger.h"
#include "mesh.h"
#include "skeletal_model.h"
#include "character_controller.h"
#include "engine.h"
#include "entity.h"
#include "inventory.h"
#include "resource.h"
typedef struct fd_command_s
{
uint16_t function : 5; // 0b00000000 00011111
uint16_t function_value : 3; // 0b00000000 11100000 TR_III+
uint16_t sub_function : 7; // 0b01111111 00000000
uint16_t end_bit : 1; // 0b10000000 00000000
}fd_command_t, *fd_command_p;
typedef struct fd_trigger_head_s
{
uint16_t timer : 8; // 0b00000000 11111111 Used as common parameter for some commands.
uint16_t once : 1; // 0b00000001 00000000
uint16_t mask : 5; // 0b00111110 00000000
uint16_t uncnown : 2;
}fd_trigger_head_t, *fd_trigger_head_p;
typedef struct fd_trigger_function_s
{
uint16_t operands : 10; // 0b00000011 11111111
uint16_t function : 5; // 0b01111100 00000000
uint16_t cont_bit : 1; // 0b10000000 00000000
}fd_trigger_function_t, *fd_trigger_function_p;
typedef struct fd_slope_s
{
uint16_t slope_t10 : 4; // 0b00000000 00001111
uint16_t slope_t11 : 4; // 0b00000000 11110000
uint16_t slope_t12 : 4; // 0b00001111 00000000
uint16_t slope_t13 : 4; // 0b11110000 00000000
}fd_slope_t, *fd_slope_p;
/*
* Helper functions to convert legacy TR structs to native OpenTomb structs.
*/
__inline void TR_vertex_to_arr(float v[3], tr5_vertex_t *tr_v)
{
v[0] = tr_v->x;
v[1] =-tr_v->z;
v[2] = tr_v->y;
}
__inline void TR_color_to_arr(float v[4], tr5_colour_t *tr_c)
{
v[0] = tr_c->r * 2;
v[1] = tr_c->g * 2;
v[2] = tr_c->b * 2;
v[3] = tr_c->a * 2;
}
/*
* Functions for getting various parameters from legacy TR structs.
*/
int32_t TR_GetNumAnimationsForMoveable(class VT_Level *tr, size_t moveable_ind);
int TR_GetNumFramesForAnimation(class VT_Level *tr, size_t animation_ind);
void TR_SkeletalModelInterpolateFrames(skeletal_model_p models);
// Main functions which are used to translate legacy TR floor data
// to native OpenTomb structs.
uint32_t Res_Sector_BiggestCorner(uint32_t v1, uint32_t v2, uint32_t v3, uint32_t v4);
void Res_Sector_SetTweenFloorConfig(struct sector_tween_s *tween);
void Res_Sector_SetTweenCeilingConfig(struct sector_tween_s *tween);
struct room_sector_s *Res_Sector_GetPortalSectorTarget(struct room_sector_s *s);
bool Res_Sector_Is2SidePortals(struct room_sector_s *s1, struct room_sector_s *s2);
bool Res_Sector_IsWall(struct room_sector_s *wall_sector, struct room_sector_s *near_sector);
/*
* BASIC SECTOR COLLISION LAYOUT
*
*
* OY OZ
* ^ 0 ___________ 1 ^ 1 ___________ 2
* | | | | | |
* | | | | | tween |
* | | SECTOR | | | corners |
* | | | | | |
* | 3|___________|2 | 0 |___________| 3
* |-------------------> OX |--------------------> OXY
*/
/// is_static - rooms flipping do not change collision geometry
uint32_t Res_Sector_BiggestCorner(uint32_t v1, uint32_t v2, uint32_t v3, uint32_t v4)
{
v1 = (v1 > v2) ? (v1) : (v2);
v2 = (v3 > v4) ? (v3) : (v4);
return (v1 > v2) ? (v1) : (v2);
}
void Res_Sector_SetTweenFloorConfig(struct sector_tween_s *tween)
{
tween->floor_tween_inverted = 0x00;
if(((tween->floor_corners[1][2] > tween->floor_corners[0][2]) && (tween->floor_corners[3][2] > tween->floor_corners[2][2])) ||
((tween->floor_corners[1][2] < tween->floor_corners[0][2]) && (tween->floor_corners[3][2] < tween->floor_corners[2][2])))
{
tween->floor_tween_type = TR_SECTOR_TWEEN_TYPE_2TRIANGLES; // like a butterfly
}
else if((tween->floor_corners[0][2] != tween->floor_corners[1][2]) &&
(tween->floor_corners[2][2] != tween->floor_corners[3][2]))
{
tween->floor_tween_type = TR_SECTOR_TWEEN_TYPE_QUAD;
}
else if(tween->floor_corners[0][2] != tween->floor_corners[1][2])
{
tween->floor_tween_type = TR_SECTOR_TWEEN_TYPE_TRIANGLE_LEFT;
}
else if(tween->floor_corners[2][2] != tween->floor_corners[3][2])
{
tween->floor_tween_type = TR_SECTOR_TWEEN_TYPE_TRIANGLE_RIGHT;
}
else
{
tween->floor_tween_type = TR_SECTOR_TWEEN_TYPE_NONE;
}
}
void Res_Sector_SetTweenCeilingConfig(struct sector_tween_s *tween)
{
tween->ceiling_tween_inverted = 0x00;
if(((tween->ceiling_corners[1][2] > tween->ceiling_corners[0][2]) && (tween->ceiling_corners[3][2] > tween->ceiling_corners[2][2])) ||
((tween->ceiling_corners[1][2] < tween->ceiling_corners[0][2]) && (tween->ceiling_corners[3][2] < tween->ceiling_corners[2][2])))
{
tween->ceiling_tween_type = TR_SECTOR_TWEEN_TYPE_2TRIANGLES; // like a butterfly
}
else if((tween->ceiling_corners[0][2] != tween->ceiling_corners[1][2]) &&
(tween->ceiling_corners[2][2] != tween->ceiling_corners[3][2]))
{
tween->ceiling_tween_type = TR_SECTOR_TWEEN_TYPE_QUAD;
}
else if(tween->ceiling_corners[0][2] != tween->ceiling_corners[1][2])
{
tween->ceiling_tween_type = TR_SECTOR_TWEEN_TYPE_TRIANGLE_LEFT;
}
else if(tween->ceiling_corners[2][2] != tween->ceiling_corners[3][2])
{
tween->ceiling_tween_type = TR_SECTOR_TWEEN_TYPE_TRIANGLE_RIGHT;
}
else
{
tween->ceiling_tween_type = TR_SECTOR_TWEEN_TYPE_NONE;
}
}
struct room_sector_s *Res_Sector_GetPortalSectorTarget(struct room_sector_s *s)
{
if(s->portal_to_room)
{
s = Room_GetSectorRaw(s->portal_to_room->real_room, s->pos);
}
return s;
}
bool Res_Sector_Is2SidePortals(struct room_sector_s *s1, struct room_sector_s *s2)
{
s1 = Res_Sector_GetPortalSectorTarget(s1);
s2 = Res_Sector_GetPortalSectorTarget(s2);
room_sector_p s1p = Room_GetSectorRaw(s2->owner_room, s1->pos);
room_sector_p s2p = Room_GetSectorRaw(s1->owner_room, s2->pos);
return (s1p->portal_to_room && s2p->portal_to_room &&
(s1p->portal_to_room->real_room == s1->owner_room->real_room) &&
(s2p->portal_to_room->real_room == s2->owner_room->real_room));
}
bool Res_Sector_IsWall(struct room_sector_s *wall_sector, struct room_sector_s *near_sector)
{
if(!wall_sector->portal_to_room && !near_sector->portal_to_room && (wall_sector->floor_penetration_config == TR_PENETRATION_CONFIG_WALL))
{
return true;
}
if(!near_sector->portal_to_room && wall_sector->portal_to_room && (near_sector->floor_penetration_config != TR_PENETRATION_CONFIG_WALL))
{
wall_sector = Room_GetSectorRaw(wall_sector->portal_to_room->real_room, wall_sector->pos);
if((wall_sector->floor_penetration_config == TR_PENETRATION_CONFIG_WALL) || (!Res_Sector_Is2SidePortals(near_sector, wall_sector)))
{
return true;
}
}
return false;
}
bool Res_Sector_IsTweenAlterable(struct room_sector_s *s1, struct room_sector_s *s2)
{
if(s1->portal_to_room)
{
return s2->owner_room->alternate_room_next || s2->owner_room->alternate_room_prev ||
s1->portal_to_room->alternate_room_next || s1->portal_to_room->alternate_room_prev;
}
else if(s2->portal_to_room)
{
return s1->owner_room->alternate_room_next || s1->owner_room->alternate_room_prev ||
s2->portal_to_room->alternate_room_next || s2->portal_to_room->alternate_room_prev;
}
return false;
}
void Res_Sector_GenXTween(sector_tween_s *room_tween, room_sector_p current_heightmap, room_sector_p next_heightmap)
{
/* XY corners coordinates must be calculated from native room sector */
room_tween->floor_corners[0][1] = current_heightmap->floor_corners[0][1];
room_tween->floor_corners[1][1] = room_tween->floor_corners[0][1];
room_tween->floor_corners[2][1] = room_tween->floor_corners[0][1];
room_tween->floor_corners[3][1] = room_tween->floor_corners[0][1];
room_tween->floor_corners[0][0] = current_heightmap->floor_corners[0][0];
room_tween->floor_corners[1][0] = room_tween->floor_corners[0][0];
room_tween->floor_corners[2][0] = current_heightmap->floor_corners[1][0];
room_tween->floor_corners[3][0] = room_tween->floor_corners[2][0];
room_tween->ceiling_corners[0][1] = current_heightmap->ceiling_corners[0][1];
room_tween->ceiling_corners[1][1] = room_tween->ceiling_corners[0][1];
room_tween->ceiling_corners[2][1] = room_tween->ceiling_corners[0][1];
room_tween->ceiling_corners[3][1] = room_tween->ceiling_corners[0][1];
room_tween->ceiling_corners[0][0] = current_heightmap->ceiling_corners[0][0];
room_tween->ceiling_corners[1][0] = room_tween->ceiling_corners[0][0];
room_tween->ceiling_corners[2][0] = current_heightmap->ceiling_corners[1][0];
room_tween->ceiling_corners[3][0] = room_tween->ceiling_corners[2][0];
if((next_heightmap->floor_penetration_config != TR_PENETRATION_CONFIG_WALL) || (current_heightmap->floor_penetration_config != TR_PENETRATION_CONFIG_WALL)) // Init X-plane tween [ | ]
{
if(Res_Sector_IsWall(next_heightmap, current_heightmap))
{
room_tween->floor_corners[0][2] = current_heightmap->floor_corners[0][2];
room_tween->floor_corners[1][2] = current_heightmap->ceiling_corners[0][2];
room_tween->floor_corners[2][2] = current_heightmap->ceiling_corners[1][2];
room_tween->floor_corners[3][2] = current_heightmap->floor_corners[1][2];
Res_Sector_SetTweenFloorConfig(room_tween);
room_tween->floor_tween_inverted = 0x01;
room_tween->ceiling_tween_type = TR_SECTOR_TWEEN_TYPE_NONE;
}
else if(Res_Sector_IsWall(current_heightmap, next_heightmap))
{
room_tween->floor_corners[0][2] = next_heightmap->floor_corners[3][2];
room_tween->floor_corners[1][2] = next_heightmap->ceiling_corners[3][2];
room_tween->floor_corners[2][2] = next_heightmap->ceiling_corners[2][2];
room_tween->floor_corners[3][2] = next_heightmap->floor_corners[2][2];
Res_Sector_SetTweenFloorConfig(room_tween);
room_tween->ceiling_tween_type = TR_SECTOR_TWEEN_TYPE_NONE;
}
else
{
/************************** SECTION WITH DROPS CALCULATIONS **********************/
if((!current_heightmap->portal_to_room && !next_heightmap->portal_to_room) || Res_Sector_Is2SidePortals(current_heightmap, next_heightmap))
{
current_heightmap = Res_Sector_GetPortalSectorTarget(current_heightmap);
next_heightmap = Res_Sector_GetPortalSectorTarget(next_heightmap);
if(!current_heightmap->portal_to_room && !next_heightmap->portal_to_room && (current_heightmap->floor_penetration_config != TR_PENETRATION_CONFIG_WALL) && (next_heightmap->floor_penetration_config != TR_PENETRATION_CONFIG_WALL))
{
if((current_heightmap->floor_penetration_config == TR_PENETRATION_CONFIG_SOLID) || (next_heightmap->floor_penetration_config == TR_PENETRATION_CONFIG_SOLID))
{
room_tween->floor_corners[0][2] = current_heightmap->floor_corners[0][2];
room_tween->floor_corners[1][2] = next_heightmap->floor_corners[3][2];
room_tween->floor_corners[2][2] = next_heightmap->floor_corners[2][2];
room_tween->floor_corners[3][2] = current_heightmap->floor_corners[1][2];
Res_Sector_SetTweenFloorConfig(room_tween);
room_tween->floor_tween_inverted = 0x01;
}
if((current_heightmap->ceiling_penetration_config == TR_PENETRATION_CONFIG_SOLID) || (next_heightmap->ceiling_penetration_config == TR_PENETRATION_CONFIG_SOLID))
{
room_tween->ceiling_corners[0][2] = current_heightmap->ceiling_corners[0][2];
room_tween->ceiling_corners[1][2] = next_heightmap->ceiling_corners[3][2];
room_tween->ceiling_corners[2][2] = next_heightmap->ceiling_corners[2][2];
room_tween->ceiling_corners[3][2] = current_heightmap->ceiling_corners[1][2];
Res_Sector_SetTweenCeilingConfig(room_tween);
}
}
}
}
}
}
void Res_Sector_GenYTween(sector_tween_s *room_tween, room_sector_p current_heightmap, room_sector_p next_heightmap)
{
room_tween->floor_corners[0][0] = current_heightmap->floor_corners[1][0];
room_tween->floor_corners[1][0] = room_tween->floor_corners[0][0];
room_tween->floor_corners[2][0] = room_tween->floor_corners[0][0];
room_tween->floor_corners[3][0] = room_tween->floor_corners[0][0];
room_tween->floor_corners[0][1] = current_heightmap->floor_corners[1][1];
room_tween->floor_corners[1][1] = room_tween->floor_corners[0][1];
room_tween->floor_corners[2][1] = current_heightmap->floor_corners[2][1];
room_tween->floor_corners[3][1] = room_tween->floor_corners[2][1];
room_tween->ceiling_corners[0][0] = current_heightmap->ceiling_corners[1][0];
room_tween->ceiling_corners[1][0] = room_tween->ceiling_corners[0][0];
room_tween->ceiling_corners[2][0] = room_tween->ceiling_corners[0][0];
room_tween->ceiling_corners[3][0] = room_tween->ceiling_corners[0][0];
room_tween->ceiling_corners[0][1] = current_heightmap->ceiling_corners[1][1];
room_tween->ceiling_corners[1][1] = room_tween->ceiling_corners[0][1];
room_tween->ceiling_corners[2][1] = current_heightmap->ceiling_corners[2][1];
room_tween->ceiling_corners[3][1] = room_tween->ceiling_corners[2][1];
if((next_heightmap->floor_penetration_config != TR_PENETRATION_CONFIG_WALL) || (current_heightmap->floor_penetration_config != TR_PENETRATION_CONFIG_WALL))
{
// Init Y-plane tween [ - ]
if(Res_Sector_IsWall(next_heightmap, current_heightmap))
{
room_tween->floor_corners[0][2] = current_heightmap->floor_corners[1][2];
room_tween->floor_corners[1][2] = current_heightmap->ceiling_corners[1][2];
room_tween->floor_corners[2][2] = current_heightmap->ceiling_corners[2][2];
room_tween->floor_corners[3][2] = current_heightmap->floor_corners[2][2];
Res_Sector_SetTweenFloorConfig(room_tween);
room_tween->floor_tween_inverted = 0x01;
room_tween->ceiling_tween_type = TR_SECTOR_TWEEN_TYPE_NONE;
}
else if(Res_Sector_IsWall(current_heightmap, next_heightmap))
{
room_tween->floor_corners[0][2] = next_heightmap->floor_corners[0][2];
room_tween->floor_corners[1][2] = next_heightmap->ceiling_corners[0][2];
room_tween->floor_corners[2][2] = next_heightmap->ceiling_corners[3][2];
room_tween->floor_corners[3][2] = next_heightmap->floor_corners[3][2];
Res_Sector_SetTweenFloorConfig(room_tween);
room_tween->ceiling_tween_type = TR_SECTOR_TWEEN_TYPE_NONE;
}
else
{
/************************** BIG SECTION WITH DROPS CALCULATIONS **********************/
if((!current_heightmap->portal_to_room && !next_heightmap->portal_to_room) || Res_Sector_Is2SidePortals(current_heightmap, next_heightmap))
{
current_heightmap = Res_Sector_GetPortalSectorTarget(current_heightmap);
next_heightmap = Res_Sector_GetPortalSectorTarget(next_heightmap);
if(!current_heightmap->portal_to_room && !next_heightmap->portal_to_room && (current_heightmap->floor_penetration_config != TR_PENETRATION_CONFIG_WALL) && (next_heightmap->floor_penetration_config != TR_PENETRATION_CONFIG_WALL))
{
if((current_heightmap->floor_penetration_config == TR_PENETRATION_CONFIG_SOLID) || (next_heightmap->floor_penetration_config == TR_PENETRATION_CONFIG_SOLID))
{
room_tween->floor_corners[0][2] = current_heightmap->floor_corners[1][2];
room_tween->floor_corners[1][2] = next_heightmap->floor_corners[0][2];
room_tween->floor_corners[2][2] = next_heightmap->floor_corners[3][2];
room_tween->floor_corners[3][2] = current_heightmap->floor_corners[2][2];
Res_Sector_SetTweenFloorConfig(room_tween);
room_tween->floor_tween_inverted = 0x01;
}
if((current_heightmap->ceiling_penetration_config == TR_PENETRATION_CONFIG_SOLID) || (next_heightmap->ceiling_penetration_config == TR_PENETRATION_CONFIG_SOLID))
{
room_tween->ceiling_corners[0][2] = current_heightmap->ceiling_corners[1][2];
room_tween->ceiling_corners[1][2] = next_heightmap->ceiling_corners[0][2];
room_tween->ceiling_corners[2][2] = next_heightmap->ceiling_corners[3][2];
room_tween->ceiling_corners[3][2] = current_heightmap->ceiling_corners[2][2];
Res_Sector_SetTweenCeilingConfig(room_tween);
}
}
}
}
}
}
///@TODO: resolve floor >> ceiling case
int Res_Sector_GenStaticTweens(struct room_s *room, struct sector_tween_s *room_tween)
{
int ret = 0;
for(uint16_t h = 0; h < room->sectors_y - 1; h++)
{
for(uint16_t w = 0; w < room->sectors_x - 1; w++)
{
// Init X-plane tween [ | ]
room_sector_p current_heightmap = room->content->sectors + (w * room->sectors_y + h);
room_sector_p next_heightmap = current_heightmap + 1;
if((w > 0) && !Res_Sector_IsTweenAlterable(current_heightmap, next_heightmap))
{
Res_Sector_GenXTween(room_tween, current_heightmap, next_heightmap);
if((room_tween->floor_tween_type != TR_SECTOR_TWEEN_TYPE_NONE) ||
(room_tween->ceiling_tween_type != TR_SECTOR_TWEEN_TYPE_NONE))
{
room_tween++;
ret++;
}
}
current_heightmap = room->content->sectors + (w * room->sectors_y + h);
next_heightmap = room->content->sectors + ((w + 1) * room->sectors_y + h);
if((h > 0) && !Res_Sector_IsTweenAlterable(current_heightmap, next_heightmap))
{
Res_Sector_GenYTween(room_tween, current_heightmap, next_heightmap);
if((room_tween->floor_tween_type != TR_SECTOR_TWEEN_TYPE_NONE) ||
(room_tween->ceiling_tween_type != TR_SECTOR_TWEEN_TYPE_NONE))
{
room_tween++;
ret++;
}
}
}
}
return ret;
}
int Res_Sector_GenDynamicTweens(struct room_s *room, struct sector_tween_s *room_tween)
{
int ret = 0;
for(uint16_t h = 0; h < room->sectors_y - 1; h++)
{
for(uint16_t w = 0; w < room->sectors_x - 1; w++)
{
// Init X-plane tween [ | ]
room_sector_p current_heightmap = room->content->sectors + (w * room->sectors_y + h);
room_sector_p next_heightmap = current_heightmap + 1;
if((w > 0) && Res_Sector_IsTweenAlterable(current_heightmap, next_heightmap))
{
Res_Sector_GenXTween(room_tween, current_heightmap, next_heightmap);
if((room_tween->floor_tween_type != TR_SECTOR_TWEEN_TYPE_NONE) ||
(room_tween->ceiling_tween_type != TR_SECTOR_TWEEN_TYPE_NONE))
{
room_tween++;
ret++;
}
}
current_heightmap = room->content->sectors + (w * room->sectors_y + h);
next_heightmap = room->content->sectors + ((w + 1) * room->sectors_y + h);
if((h > 0) && Res_Sector_IsTweenAlterable(current_heightmap, next_heightmap))
{
Res_Sector_GenYTween(room_tween, current_heightmap, next_heightmap);
if((room_tween->floor_tween_type != TR_SECTOR_TWEEN_TYPE_NONE) ||
(room_tween->ceiling_tween_type != TR_SECTOR_TWEEN_TYPE_NONE))
{
room_tween++;
ret++;
}
}
}
}
return ret;
}
int Res_Sector_In2SideOfPortal(struct room_sector_s *s1, struct room_sector_s *s2, struct portal_s *p)
{
if((s1->pos[0] == s2->pos[0]) && (s1->pos[1] != s2->pos[1]) && (fabs(p->norm[1]) > 0.99))
{
float min_x, max_x, min_y, max_y, x;
max_x = min_x = p->vertex[0];
for(uint16_t i = 1; i < p->vertex_count; i++)
{
x = p->vertex[3 * i + 0];
if(x > max_x)
{
max_x = x;
}
if(x < min_x)
{
min_x = x;
}
}
if(s1->pos[1] > s2->pos[1])
{
min_y = s2->pos[1];
max_y = s1->pos[1];
}
else
{
min_y = s1->pos[1];
max_y = s2->pos[1];
}
if((s1->pos[0] < max_x) && (s1->pos[0] > min_x) && (p->centre[1] < max_y) && (p->centre[1] > min_y))
{
return 1;
}
}
else if((s1->pos[0] != s2->pos[0]) && (s1->pos[1] == s2->pos[1]) && (fabs(p->norm[0]) > 0.99))
{
float min_x, max_x, min_y, max_y, y;
max_y = min_y = p->vertex[1];
for(uint16_t i = 1; i < p->vertex_count; i++)
{
y = p->vertex[3 * i + 1];
if(y > max_y)
{
max_y = y;
}
if(y < min_y)
{
min_y = y;
}
}
if(s1->pos[0] > s2->pos[0])
{
min_x = s2->pos[0];
max_x = s1->pos[0];
}
else
{
min_x = s1->pos[0];
max_x = s2->pos[0];
}
if((p->centre[0] < max_x) && (p->centre[0] > min_x) && (s1->pos[1] < max_y) && (s1->pos[1] > min_y))
{
return 1;
}
}
return 0;
}
void Res_RoomLightCalculate(struct light_s *light, struct tr5_room_light_s *tr_light)
{
switch(tr_light->light_type)
{
case 0:
light->light_type = LT_SUN;
break;
case 1:
light->light_type = LT_POINT;
break;
case 2:
light->light_type = LT_SPOTLIGHT;
break;
case 3:
light->light_type = LT_SHADOW;
break;
default:
light->light_type = LT_NULL;
break;
}
TR_vertex_to_arr(light->pos, &tr_light->pos);
light->pos[3] = 1.0f;
if(light->light_type == LT_SHADOW)
{
light->colour[0] = -(tr_light->color.r / 255.0f) * tr_light->intensity;
light->colour[1] = -(tr_light->color.g / 255.0f) * tr_light->intensity;
light->colour[2] = -(tr_light->color.b / 255.0f) * tr_light->intensity;
light->colour[3] = 1.0f;
}
else
{
light->colour[0] = (tr_light->color.r / 255.0f) * tr_light->intensity;
light->colour[1] = (tr_light->color.g / 255.0f) * tr_light->intensity;
light->colour[2] = (tr_light->color.b / 255.0f) * tr_light->intensity;
light->colour[3] = 1.0f;
}
light->inner = tr_light->r_inner;
light->outer = tr_light->r_outer;
light->length = tr_light->length;
light->cutoff = tr_light->cutoff;
light->falloff = 0.001f / light->outer;
}
void Res_RoomSectorsCalculate(struct room_s *rooms, uint32_t rooms_count, uint32_t room_index, class VT_Level *tr)
{
room_sector_p sector;
room_p room = rooms + room_index;
tr5_room_t *tr_room = &tr->rooms[room_index];
/*
* Sectors loading
*/
sector = room->content->sectors;
for(uint32_t i = 0; i < room->sectors_count; i++, sector++)
{
/*
* Let us fill pointers to sectors above and sectors below
*/
uint8_t rp = tr_room->sector_list[i].room_below;
sector->room_below = NULL;
if(rp < rooms_count && rp != 255)
{
sector->room_below = rooms + rp;
}
rp = tr_room->sector_list[i].room_above;
sector->room_above = NULL;
if(rp < rooms_count && rp != 255)
{
sector->room_above = rooms + rp;
}
room_sector_p near_sector = NULL;
/**** OX *****/
if((sector->index_y > 0) && (sector->index_y < room->sectors_y - 1) && (sector->index_x == 0))
{
near_sector = sector + room->sectors_y;
}
if((sector->index_y > 0) && (sector->index_y < room->sectors_y - 1) && (sector->index_x == room->sectors_x - 1))
{
near_sector = sector - room->sectors_y;
}
/**** OY *****/
if((sector->index_x > 0) && (sector->index_x < room->sectors_x - 1) && (sector->index_y == 0))
{
near_sector = sector + 1;
}
if((sector->index_x > 0) && (sector->index_x < room->sectors_x - 1) && (sector->index_y == room->sectors_y - 1))
{
near_sector = sector - 1;
}
if(near_sector && sector->portal_to_room)
{
portal_p p = room->content->portals;
for(uint16_t j = 0; j < room->content->portals_count; j++, p++)
{
if((p->norm[2] < 0.01) && ((p->norm[2] > -0.01)))
{
room_sector_p dst = Room_GetSectorRaw(p->dest_room, sector->pos);
room_sector_p orig_dst = Room_GetSectorRaw(sector->portal_to_room, sector->pos);
if(dst && !dst->portal_to_room && (dst->floor != TR_METERING_WALLHEIGHT) && (dst->ceiling != TR_METERING_WALLHEIGHT) && (sector->portal_to_room != p->dest_room) && (dst->floor < orig_dst->floor) && Res_Sector_In2SideOfPortal(near_sector, dst, p))
{
sector->portal_to_room = p->dest_room;
orig_dst = dst;
}
}
}
}
}
}
int Res_Sector_TranslateFloorData(struct room_s *rooms, uint32_t rooms_count, struct room_sector_s *sector, class VT_Level *tr)
{
int ret = 0;
if(!sector || (sector->trig_index <= 0) || (sector->trig_index >= tr->floor_data_size))
{
return ret;
}
uint16_t *entry = tr->floor_data + sector->trig_index;
size_t max_offset = tr->floor_data_size;
size_t current_offset = sector->trig_index;
fd_command_t fd_command;
sector->flags = 0; // Clear sector flags before parsing.
do
{
fd_command = *((fd_command_p)entry);
entry++;
current_offset++;
switch(fd_command.function)
{
case TR_FD_FUNC_PORTALSECTOR: // PORTAL DATA
if(fd_command.sub_function == 0x00)
{
if(*entry < rooms_count)
{
sector->portal_to_room = rooms + *entry;
sector->floor_penetration_config = TR_PENETRATION_CONFIG_GHOST;
sector->ceiling_penetration_config = TR_PENETRATION_CONFIG_GHOST;
}
entry ++;
current_offset++;
}
break;
case TR_FD_FUNC_FLOORSLANT: // FLOOR SLANT
if(fd_command.sub_function == 0x00)
{
int8_t raw_y_slant = (*entry & 0x00FF);
int8_t raw_x_slant = ((*entry & 0xFF00) >> 8);
sector->floor_diagonal_type = TR_SECTOR_DIAGONAL_TYPE_NONE;
sector->floor_penetration_config = TR_PENETRATION_CONFIG_SOLID;
if(raw_x_slant > 0)
{
sector->floor_corners[2][2] -= ((float)raw_x_slant * TR_METERING_STEP);
sector->floor_corners[3][2] -= ((float)raw_x_slant * TR_METERING_STEP);
}
else if(raw_x_slant < 0)
{
sector->floor_corners[0][2] -= (fabs((float)raw_x_slant) * TR_METERING_STEP);
sector->floor_corners[1][2] -= (fabs((float)raw_x_slant) * TR_METERING_STEP);
}
if(raw_y_slant > 0)
{
sector->floor_corners[0][2] -= ((float)raw_y_slant * TR_METERING_STEP);
sector->floor_corners[3][2] -= ((float)raw_y_slant * TR_METERING_STEP);
}
else if(raw_y_slant < 0)
{
sector->floor_corners[1][2] -= (fabs((float)raw_y_slant) * TR_METERING_STEP);
sector->floor_corners[2][2] -= (fabs((float)raw_y_slant) * TR_METERING_STEP);
}
entry++;
current_offset++;
}
break;
case TR_FD_FUNC_CEILINGSLANT: // CEILING SLANT
if(fd_command.sub_function == 0x00)
{
int8_t raw_y_slant = (*entry & 0x00FF);
int8_t raw_x_slant = ((*entry & 0xFF00) >> 8);
sector->ceiling_diagonal_type = TR_SECTOR_DIAGONAL_TYPE_NONE;
sector->ceiling_penetration_config = TR_PENETRATION_CONFIG_SOLID;
if(raw_x_slant > 0)
{
sector->ceiling_corners[3][2] += ((float)raw_x_slant * TR_METERING_STEP);
sector->ceiling_corners[2][2] += ((float)raw_x_slant * TR_METERING_STEP);
}
else if(raw_x_slant < 0)
{
sector->ceiling_corners[1][2] += (fabs((float)raw_x_slant) * TR_METERING_STEP);
sector->ceiling_corners[0][2] += (fabs((float)raw_x_slant) * TR_METERING_STEP);
}
if(raw_y_slant > 0)
{
sector->ceiling_corners[1][2] += ((float)raw_y_slant * TR_METERING_STEP);
sector->ceiling_corners[2][2] += ((float)raw_y_slant * TR_METERING_STEP);
}
else if(raw_y_slant < 0)
{
sector->ceiling_corners[0][2] += (fabs((float)raw_y_slant) * TR_METERING_STEP);
sector->ceiling_corners[3][2] += (fabs((float)raw_y_slant) * TR_METERING_STEP);
}
entry++;
current_offset++;
}
break;
case TR_FD_FUNC_TRIGGER: // TRIGGERS
{
fd_trigger_head_t fd_trigger_head = *((fd_trigger_head_p)entry);
if(sector->trigger == NULL)
{
sector->trigger = (trigger_header_p)malloc(sizeof(trigger_header_t));
}
else
{
Con_AddLine("SECTOR HAS TWO OR MORE TRIGGERS!!!", FONTSTYLE_CONSOLE_WARNING);
}
sector->trigger->commands = NULL;
sector->trigger->function_value = fd_command.function_value;
sector->trigger->sub_function = fd_command.sub_function;
sector->trigger->mask = fd_trigger_head.mask;
sector->trigger->timer = fd_trigger_head.timer;
sector->trigger->once = fd_trigger_head.once;
// Now parse operand chain for trigger function!
fd_trigger_function_t fd_trigger_function;
do
{
trigger_command_p command = (trigger_command_p)malloc(sizeof(trigger_command_t));
trigger_command_p *last_command_ptr = §or->trigger->commands;
command->next = NULL;
while(*last_command_ptr)
{
last_command_ptr = &((*last_command_ptr)->next);
}
*last_command_ptr = command;
entry++;
current_offset++;
fd_trigger_function = *((fd_trigger_function_p)entry);
command->function = fd_trigger_function.function;
command->operands = fd_trigger_function.operands;
command->unused = 0;
command->once = 0;
command->camera.index = 0;
command->camera.timer = 0;
command->camera.move = 0;
command->camera.unused = 0;
switch(command->function)
{
case TR_FD_TRIGFUNC_SET_CAMERA:
{
command->camera.index = (*entry) & 0x007F;
entry++;
current_offset++;
command->camera.timer = ((*entry) & 0x00FF);
command->once = ((*entry) & 0x0100) >> 8;
command->camera.move = ((*entry) & 0x1000) >> 12;
fd_trigger_function.cont_bit = ((*entry) & 0x8000) >> 15;
}
break;
case TR_FD_TRIGFUNC_FLYBY:
{
entry++;
current_offset++;
command->once = ((*entry) & 0x0100) >> 8;
fd_trigger_function.cont_bit = ((*entry) & 0x8000) >> 15;
}
break;
case TR_FD_TRIGFUNC_OBJECT:
case TR_FD_TRIGFUNC_UWCURRENT:
case TR_FD_TRIGFUNC_FLIPMAP:
case TR_FD_TRIGFUNC_FLIPON:
case TR_FD_TRIGFUNC_FLIPOFF:
case TR_FD_TRIGFUNC_SET_TARGET:
case TR_FD_TRIGFUNC_ENDLEVEL:
case TR_FD_TRIGFUNC_PLAYTRACK:
case TR_FD_TRIGFUNC_FLIPEFFECT:
case TR_FD_TRIGFUNC_SECRET:
case TR_FD_TRIGFUNC_CLEARBODIES:
case TR_FD_TRIGFUNC_CUTSCENE:
break;
default: // UNKNOWN!
break;
};
}
while(!fd_trigger_function.cont_bit && (current_offset < max_offset));
}
break;
case TR_FD_FUNC_DEATH:
sector->flags |= SECTOR_FLAG_DEATH;
break;
case TR_FD_FUNC_CLIMB:
// First 4 sector flags are similar to subfunction layout.
sector->flags |= (uint32_t)fd_command.sub_function;
break;
case TR_FD_FUNC_MONKEY:
sector->flags |= SECTOR_FLAG_CLIMB_CEILING;
break;
case TR_FD_FUNC_MINECART_LEFT:
// Minecart left (TR3) and trigger triggerer mark (TR4-5) has the same flag value.
// We re-parse them properly here.
if(tr->game_version < TR_IV)
{
sector->flags |= SECTOR_FLAG_MINECART_LEFT;
}
else
{
sector->flags |= SECTOR_FLAG_TRIGGERER_MARK;
}
break;
case TR_FD_FUNC_MINECART_RIGHT:
// Minecart right (TR3) and beetle mark (TR4-5) has the same flag value.
// We re-parse them properly here.
if(tr->game_version < TR_IV)
{
sector->flags |= SECTOR_FLAG_MINECART_RIGHT;
}
else
{
sector->flags |= SECTOR_FLAG_BEETLE_MARK;
}
break;
default:
// Other functions are TR3+ collisional triangle functions.
if( (fd_command.function >= TR_FD_FUNC_FLOORTRIANGLE_NW) &&
(fd_command.function <= TR_FD_FUNC_CEILINGTRIANGLE_NE_PORTAL_SE) )
{
fd_slope_t fd_slope = *((fd_slope_p)entry);
float overall_adjustment = (float)Res_Sector_BiggestCorner(fd_slope.slope_t10, fd_slope.slope_t11, fd_slope.slope_t12, fd_slope.slope_t13) * TR_METERING_STEP;
entry++;
current_offset++;
if( (fd_command.function == TR_FD_FUNC_FLOORTRIANGLE_NW) ||
(fd_command.function == TR_FD_FUNC_FLOORTRIANGLE_NW_PORTAL_SW) ||
(fd_command.function == TR_FD_FUNC_FLOORTRIANGLE_NW_PORTAL_NE) )
{
sector->floor_diagonal_type = TR_SECTOR_DIAGONAL_TYPE_NW;
sector->floor_corners[0][2] -= overall_adjustment - ((float)fd_slope.slope_t12 * TR_METERING_STEP);
sector->floor_corners[1][2] -= overall_adjustment - ((float)fd_slope.slope_t13 * TR_METERING_STEP);
sector->floor_corners[2][2] -= overall_adjustment - ((float)fd_slope.slope_t10 * TR_METERING_STEP);
sector->floor_corners[3][2] -= overall_adjustment - ((float)fd_slope.slope_t11 * TR_METERING_STEP);
if(fd_command.function == TR_FD_FUNC_FLOORTRIANGLE_NW_PORTAL_SW)
{
sector->floor_penetration_config = TR_PENETRATION_CONFIG_DOOR_VERTICAL_A;
}
else if(fd_command.function == TR_FD_FUNC_FLOORTRIANGLE_NW_PORTAL_NE)
{
sector->floor_penetration_config = TR_PENETRATION_CONFIG_DOOR_VERTICAL_B;
}
else
{
sector->floor_penetration_config = TR_PENETRATION_CONFIG_SOLID;
}
}
else if( (fd_command.function == TR_FD_FUNC_FLOORTRIANGLE_NE) ||
(fd_command.function == TR_FD_FUNC_FLOORTRIANGLE_NE_PORTAL_NW) ||
(fd_command.function == TR_FD_FUNC_FLOORTRIANGLE_NE_PORTAL_SE) )
{
sector->floor_diagonal_type = TR_SECTOR_DIAGONAL_TYPE_NE;
sector->floor_corners[0][2] -= overall_adjustment - ((float)fd_slope.slope_t12 * TR_METERING_STEP);
sector->floor_corners[1][2] -= overall_adjustment - ((float)fd_slope.slope_t13 * TR_METERING_STEP);
sector->floor_corners[2][2] -= overall_adjustment - ((float)fd_slope.slope_t10 * TR_METERING_STEP);
sector->floor_corners[3][2] -= overall_adjustment - ((float)fd_slope.slope_t11 * TR_METERING_STEP);
if(fd_command.function == TR_FD_FUNC_FLOORTRIANGLE_NE_PORTAL_NW)
{
sector->floor_penetration_config = TR_PENETRATION_CONFIG_DOOR_VERTICAL_A;
}
else if(fd_command.function == TR_FD_FUNC_FLOORTRIANGLE_NE_PORTAL_SE)
{
sector->floor_penetration_config = TR_PENETRATION_CONFIG_DOOR_VERTICAL_B;
}
else
{
sector->floor_penetration_config = TR_PENETRATION_CONFIG_SOLID;
}
}
else if( (fd_command.function == TR_FD_FUNC_CEILINGTRIANGLE_NW) ||
(fd_command.function == TR_FD_FUNC_CEILINGTRIANGLE_NW_PORTAL_SW) ||
(fd_command.function == TR_FD_FUNC_CEILINGTRIANGLE_NW_PORTAL_NE) )
{
sector->ceiling_diagonal_type = TR_SECTOR_DIAGONAL_TYPE_NW;
sector->ceiling_corners[0][2] += overall_adjustment - (float)(fd_slope.slope_t11 * TR_METERING_STEP);
sector->ceiling_corners[1][2] += overall_adjustment - (float)(fd_slope.slope_t10 * TR_METERING_STEP);
sector->ceiling_corners[2][2] += overall_adjustment - (float)(fd_slope.slope_t13 * TR_METERING_STEP);
sector->ceiling_corners[3][2] += overall_adjustment - (float)(fd_slope.slope_t12 * TR_METERING_STEP);
if(fd_command.function == TR_FD_FUNC_CEILINGTRIANGLE_NW_PORTAL_SW)
{
sector->ceiling_penetration_config = TR_PENETRATION_CONFIG_DOOR_VERTICAL_A;
}
else if(fd_command.function == TR_FD_FUNC_CEILINGTRIANGLE_NW_PORTAL_NE)
{
sector->ceiling_penetration_config = TR_PENETRATION_CONFIG_DOOR_VERTICAL_B;
}
else
{
sector->ceiling_penetration_config = TR_PENETRATION_CONFIG_SOLID;
}
}
else if( (fd_command.function == TR_FD_FUNC_CEILINGTRIANGLE_NE) ||
(fd_command.function == TR_FD_FUNC_CEILINGTRIANGLE_NE_PORTAL_NW) ||
(fd_command.function == TR_FD_FUNC_CEILINGTRIANGLE_NE_PORTAL_SE) )
{
sector->ceiling_diagonal_type = TR_SECTOR_DIAGONAL_TYPE_NE;
sector->ceiling_corners[0][2] += overall_adjustment - (float)(fd_slope.slope_t11 * TR_METERING_STEP);
sector->ceiling_corners[1][2] += overall_adjustment - (float)(fd_slope.slope_t10 * TR_METERING_STEP);
sector->ceiling_corners[2][2] += overall_adjustment - (float)(fd_slope.slope_t13 * TR_METERING_STEP);
sector->ceiling_corners[3][2] += overall_adjustment - (float)(fd_slope.slope_t12 * TR_METERING_STEP);
if(fd_command.function == TR_FD_FUNC_CEILINGTRIANGLE_NE_PORTAL_NW)
{
sector->ceiling_penetration_config = TR_PENETRATION_CONFIG_DOOR_VERTICAL_A;