-
Notifications
You must be signed in to change notification settings - Fork 107
/
MapSystem.bb
8751 lines (7279 loc) · 304 KB
/
MapSystem.bb
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
Type Materials
Field name$
Field Diff
Field Bump
Field StepSound%
End Type
Function LoadMaterials(file$)
CatchErrors("Uncaught (LoadMaterials)")
;If Not BumpEnabled Then Return
Local TemporaryString$
Local mat.Materials = Null
Local StrTemp$ = ""
Local f = OpenFile(file)
While Not Eof(f)
TemporaryString = Trim(ReadLine(f))
If Left(TemporaryString,1) = "[" Then
TemporaryString = Mid(TemporaryString, 2, Len(TemporaryString) - 2)
mat.Materials = New Materials
mat\name = Lower(TemporaryString)
If BumpEnabled Then
StrTemp = GetINIString(file, TemporaryString, "bump")
If StrTemp <> "" Then
mat\Bump = LoadTexture_Strict(StrTemp)
TextureBlend mat\Bump, 6
TextureBumpEnvMat mat\Bump,0,0,-0.012
TextureBumpEnvMat mat\Bump,0,1,-0.012
TextureBumpEnvMat mat\Bump,1,0,0.012
TextureBumpEnvMat mat\Bump,1,1,0.012
TextureBumpEnvOffset mat\Bump,0.5
TextureBumpEnvScale mat\Bump,1.0
EndIf
EndIf
mat\StepSound = (GetINIInt(file, TemporaryString, "stepsound")+1)
EndIf
Wend
CloseFile f
CatchErrors("LoadMaterials")
End Function
Function LoadWorld(file$, rt.RoomTemplates)
Local map=LoadAnimMesh_Strict(file)
If Not map Then Return
Local x#,y#,z#,i%,c%
Local mat.Materials
Local world=CreatePivot()
Local meshes=CreatePivot(world)
Local renderbrushes=CreateMesh(world)
Local collisionbrushes=CreatePivot(world)
;Local pointentities=CreatePivot(world)
;Local solidentities=CreatePivot(world)
EntityType collisionbrushes,HIT_MAP
For c=1 To CountChildren(map)
Local node=GetChild(map,c)
Local classname$=Lower(KeyValue(node,"classname"))
Select classname
;===============================================================================
;Map Geometry
;===============================================================================
Case "mesh"
EntityParent node,meshes
If KeyValue(node,"disablecollisions")<>1 Then
EntityType node,HIT_MAP
EntityPickMode node, 2
EndIf
c=c-1
;EntityType node,HIT_MAP
Case "brush"
RotateMesh node,EntityPitch(node),EntityYaw(node),EntityRoll(node)
PositionMesh node,EntityX(node),EntityY(node),EntityZ(node)
AddMesh node,renderbrushes
EntityAlpha node,0
EntityType node,HIT_MAP
EntityAlpha node,0
EntityParent node,collisionbrushes
EntityPickMode node, 2
c=c-1
;===============================================================================
;Solid Entities
;===============================================================================
Case "item"
;name$ = KeyValue(node,"name","")
;tempname$ = KeyValue(node,"tempname","")
;CreateItem(name,tempname,EntityX(node)*RoomScale,EntityY(node)*RoomScale,EntityZ(node)*RoomScale)
Case "screen"
x# = EntityX(node)*RoomScale
y# = EntityY(node)*RoomScale
z# = EntityZ(node)*RoomScale
If x<>0 Or y<>0 Or z<>0 Then
Local ts.TempScreens = New TempScreens
ts\x = x
ts\y = y
ts\z = z
ts\imgpath = KeyValue(node,"imgpath","")
ts\roomtemplate = rt
EndIf
Case "waypoint"
x# = EntityX(node)*RoomScale
y# = EntityY(node)*RoomScale
z# = EntityZ(node)*RoomScale
Local w.TempWayPoints = New TempWayPoints
w\roomtemplate = rt
w\x = x
w\y = y
w\z = z
;EntityParent (w\obj, collisionbrushes)
Case "light"
x# = EntityX(node)*RoomScale
y# = EntityY(node)*RoomScale
z# = EntityZ(node)*RoomScale
If x<>0 Or y<>0 Or z<>0 Then
range# = Float(KeyValue(node,"range","1"))/2000.0
lcolor$=KeyValue(node,"color","255 255 255")
intensity# = Min(Float(KeyValue(node,"intensity","1.0"))*0.8,1.0)
r=Int(Piece(lcolor,1," "))*intensity
g=Int(Piece(lcolor,2," "))*intensity
b=Int(Piece(lcolor,3," "))*intensity
AddTempLight(rt, x,y,z, 2, range, r,g,b)
EndIf
Case "spotlight"
x# = EntityX(node)*RoomScale
y# = EntityY(node)*RoomScale
z# = EntityZ(node)*RoomScale
If x<>0 Or y<>0 Or z<>0 Then
range# = Float(KeyValue(node,"range","1"))/700.0
lcolor$=KeyValue(node,"color","255 255 255")
intensity# = Min(Float(KeyValue(node,"intensity","1.0"))*0.8,1.0)
r=Int(Piece(lcolor,1," "))*intensity
g=Int(Piece(lcolor,2," "))*intensity
b=Int(Piece(lcolor,3," "))*intensity
Local lt.LightTemplates = AddTempLight(rt, x,y,z, 3, range, r,g,b)
angles$=KeyValue(node,"angles","0 0 0")
pitch#=Piece(angles,1," ")
yaw#=Piece(angles,2," ")
lt\pitch = pitch
lt\yaw = yaw
lt\innerconeangle = Int(KeyValue(node,"innerconeangle",""))
lt\outerconeangle = Int(KeyValue(node,"outerconeangle",""))
EndIf
Case "soundemitter"
For i = 0 To 3
If rt\TempSoundEmitter[i]=0 Then
rt\TempSoundEmitterX[i]=EntityX(node)*RoomScale
rt\TempSoundEmitterY[i]=EntityY(node)*RoomScale
rt\TempSoundEmitterZ[i]=EntityZ(node)*RoomScale
rt\TempSoundEmitter[i]=Int(KeyValue(node,"sound","0"))
rt\TempSoundEmitterRange[i]=Float(KeyValue(node,"range","1"))
Exit
EndIf
Next
;Invisible collision brush
Case "field_hit"
EntityParent node,collisionbrushes
EntityType node,HIT_MAP
EntityAlpha node,0
c=c-1
;===============================================================================
;Point Entities
;===============================================================================
;Camera start position point entity
Case "playerstart"
angles$=KeyValue(node,"angles","0 0 0")
pitch#=Piece(angles,1," ")
yaw#=Piece(angles,2," ")
roll#=Piece(angles,3," ")
If cam Then
PositionEntity cam,EntityX(node),EntityY(node),EntityZ(node)
RotateEntity cam,pitch,yaw,roll
EndIf
End Select
Next
;If BumpEnabled Then
;
; For i = 1 To CountSurfaces(renderbrushes)
; sf = GetSurface(renderbrushes,i)
; b = GetSurfaceBrush( sf )
; t = GetBrushTexture(b, 1)
; texname$ = StripPath(TextureName(t))
;
; For mat.Materials = Each Materials
; If texname = mat\name Then
; If mat\Bump <> 0 Then
; t1 = GetBrushTexture(b,0)
;
; BrushTexture b, t1, 0, 0 ;light map
; BrushTexture b, mat\Bump, 0, 1 ;bump
; BrushTexture b, t, 0, 2 ;diff
;
; PaintSurface sf,b
;
; If StripPath(TextureName(t1)) <> "" Then FreeTexture t1
;
; ;If t1<>0 Then FreeTexture t1
; ;If t2 <> 0 Then FreeTexture t2
; EndIf
; Exit
; EndIf
; Next
;
; FreeTexture t
; FreeBrush b
;
; Next
;
;EndIf
EntityFX renderbrushes, 1
FreeEntity map
Return world
End Function
;RMESH STUFF;;;;
Function StripFilename$(file$)
Local mi$=""
Local lastSlash%=0
If Len(file)>0
For i%=1 To Len(file)
mi=Mid(file$,i,1)
If mi="\" Or mi="/" Then
lastSlash=i
EndIf
Next
EndIf
Return Left(file,lastSlash)
End Function
Function GetTextureFromCache%(name$)
For tc.Materials=Each Materials
If tc\name = name Then Return tc\Diff
Next
Return 0
End Function
Function GetBumpFromCache%(name$)
For tc.Materials=Each Materials
If tc\name = name Then Return tc\Bump
Next
Return 0
End Function
Function GetCache.Materials(name$)
For tc.Materials=Each Materials
If tc\name = name Then Return tc
Next
Return Null
End Function
Function AddTextureToCache(texture%)
Local tc.Materials=GetCache(StripPath(TextureName(texture)))
If tc.Materials=Null Then
tc.Materials=New Materials
tc\name=StripPath(TextureName(texture))
If BumpEnabled Then
Local temp$=GetINIString("Data\materials.ini",tc\name,"bump")
If temp<>"" Then
tc\Bump=LoadTexture_Strict(temp)
TextureBlend tc\Bump,6
TextureBumpEnvMat tc\Bump,0,0,-0.012
TextureBumpEnvMat tc\Bump,0,1,-0.012
TextureBumpEnvMat tc\Bump,1,0,0.012
TextureBumpEnvMat tc\Bump,1,1,0.012
TextureBumpEnvOffset tc\Bump,0.5
TextureBumpEnvScale tc\Bump,1.0
Else
tc\Bump=0
EndIf
EndIf
tc\Diff=0
EndIf
If tc\Diff=0 Then tc\Diff=texture
End Function
Function ClearTextureCache()
For tc.Materials=Each Materials
If tc\Diff<>0 Then FreeTexture tc\Diff
If tc\Bump<>0 Then FreeTexture tc\Bump
Delete tc
Next
End Function
Function FreeTextureCache()
For tc.Materials=Each Materials
If tc\Diff<>0 Then FreeTexture tc\Diff
If tc\Bump<>0 Then FreeTexture tc\Bump
tc\Diff = 0 : tc\Bump = 0
Next
End Function
Function LoadRMesh(file$,rt.RoomTemplates)
CatchErrors("Uncaught (LoadRMesh)")
;generate a texture made of white
Local blankTexture%
blankTexture=CreateTexture(4,4,1,1)
ClsColor 255,255,255
SetBuffer TextureBuffer(blankTexture)
Cls
SetBuffer BackBuffer()
Local pinkTexture%
pinkTexture=CreateTexture(4,4,1,1)
ClsColor 255,255,255
SetBuffer TextureBuffer(pinkTexture)
Cls
SetBuffer BackBuffer()
ClsColor 0,0,0
;read the file
Local f%=ReadFile(file)
Local i%,j%,k%,x#,y#,z#,yaw#
Local vertex%
Local temp1i%,temp2i%,temp3i%
Local temp1#,temp2#,temp3#
Local temp1s$, temp2s$
Local collisionMeshes% = CreatePivot()
Local hasTriggerBox% = False
For i=0 To 3 ;reattempt up to 3 times
If f=0 Then
f=ReadFile(file)
Else
Exit
EndIf
Next
If f=0 Then RuntimeError "Error reading file "+Chr(34)+file+Chr(34)
Local isRMesh$ = ReadString(f)
If isRMesh$="RoomMesh"
;Continue
ElseIf isRMesh$="RoomMesh.HasTriggerBox"
hasTriggerBox% = True
Else
RuntimeError Chr(34)+file+Chr(34)+" is Not RMESH ("+isRMesh+")"
EndIf
file=StripFilename(file)
Local count%,count2%
;drawn meshes
Local Opaque%,Alpha%
Opaque=CreateMesh()
Alpha=CreateMesh()
count = ReadInt(f)
Local childMesh%
Local surf%,tex%[2],brush%
Local isAlpha%
Local u#,v#
For i=1 To count ;drawn mesh
childMesh=CreateMesh()
surf=CreateSurface(childMesh)
brush=CreateBrush()
tex[0]=0 : tex[1]=0
isAlpha=0
For j=0 To 1
temp1i=ReadByte(f)
If temp1i<>0 Then
temp1s=ReadString(f)
tex[j]=GetTextureFromCache(temp1s)
If tex[j]=0 Then ;texture is not in cache
Select True
Case temp1i<3
tex[j]=LoadTexture(file+temp1s,1)
Default
tex[j]=LoadTexture(file+temp1s,3)
End Select
If tex[j]<>0 Then
If temp1i=1 Then TextureBlend tex[j],5
If Instr(Lower(temp1s),"_lm")<>0 Then
TextureBlend tex[j],3
EndIf
AddTextureToCache(tex[j])
EndIf
EndIf
If tex[j]<>0 Then
isAlpha=2
If temp1i=3 Then isAlpha=1
TextureCoords tex[j],1-j
EndIf
EndIf
Next
If isAlpha=1 Then
If tex[1]<>0 Then
TextureBlend tex[1],2
BrushTexture brush,tex[1],0,0
Else
BrushTexture brush,blankTexture,0,0
EndIf
Else
If tex[0]<>0 And tex[1]<>0 Then
bumptex% = GetBumpFromCache(StripPath(TextureName(tex[1])))
;If bumptex<>0 Then
; DebugLog StripPath(TextureName(bumptex))
; Stop
;EndIf
For j=0 To 1
BrushTexture brush,tex[j],0,j+1+(bumptex<>0)
Next
BrushTexture brush,AmbientLightRoomTex,0
If (bumptex<>0) Then
BrushTexture brush,bumptex,0,1
EndIf
Else
For j=0 To 1
If tex[j]<>0 Then
BrushTexture brush,tex[j],0,j
Else
BrushTexture brush,blankTexture,0,j
EndIf
Next
EndIf
EndIf
surf=CreateSurface(childMesh)
If isAlpha>0 Then PaintSurface surf,brush
FreeBrush brush : brush = 0
count2=ReadInt(f) ;vertices
For j%=1 To count2
;world coords
x=ReadFloat(f) : y=ReadFloat(f) : z=ReadFloat(f)
vertex=AddVertex(surf,x,y,z)
;texture coords
For k%=0 To 1
u=ReadFloat(f) : v=ReadFloat(f)
VertexTexCoords surf,vertex,u,v,0.0,k
Next
;colors
temp1i=ReadByte(f)
temp2i=ReadByte(f)
temp3i=ReadByte(f)
VertexColor surf,vertex,temp1i,temp2i,temp3i,1.0
Next
count2=ReadInt(f) ;polys
For j%=1 To count2
temp1i = ReadInt(f) : temp2i = ReadInt(f) : temp3i = ReadInt(f)
AddTriangle(surf,temp1i,temp2i,temp3i)
Next
If isAlpha=1 Then
AddMesh childMesh,Alpha
EntityAlpha childMesh,0.0
Else
AddMesh childMesh,Opaque
EntityParent childMesh,collisionMeshes
EntityAlpha childMesh,0.0
EntityType childMesh,HIT_MAP
EntityPickMode childMesh,2
;make collision double-sided
Local flipChild% = CopyMesh(childMesh)
FlipMesh(flipChild)
AddMesh flipChild,childMesh
FreeEntity flipChild
EndIf
Next
Local hiddenMesh%
hiddenMesh=CreateMesh()
count=ReadInt(f) ;invisible collision mesh
For i%=1 To count
surf=CreateSurface(hiddenMesh)
count2=ReadInt(f) ;vertices
For j%=1 To count2
;world coords
x=ReadFloat(f) : y=ReadFloat(f) : z=ReadFloat(f)
vertex=AddVertex(surf,x,y,z)
Next
count2=ReadInt(f) ;polys
For j%=1 To count2
temp1i = ReadInt(f) : temp2i = ReadInt(f) : temp3i = ReadInt(f)
AddTriangle(surf,temp1i,temp2i,temp3i)
AddTriangle(surf,temp1i,temp3i,temp2i)
Next
Next
;trigger boxes
If hasTriggerBox
DebugLog "TriggerBoxEnable"
rt\TempTriggerboxAmount = ReadInt(f)
For tb = 0 To rt\TempTriggerboxAmount-1
rt\TempTriggerbox[tb] = CreateMesh(rt\obj)
count = ReadInt(f)
For i%=1 To count
surf=CreateSurface(rt\TempTriggerbox[tb])
count2=ReadInt(f)
For j%=1 To count2
x=ReadFloat(f) : y=ReadFloat(f) : z=ReadFloat(f)
vertex=AddVertex(surf,x,y,z)
Next
count2=ReadInt(f)
For j%=1 To count2
temp1i = ReadInt(f) : temp2i = ReadInt(f) : temp3i = ReadInt(f)
AddTriangle(surf,temp1i,temp2i,temp3i)
AddTriangle(surf,temp1i,temp3i,temp2i)
Next
Next
rt\TempTriggerboxName[tb] = ReadString(f)
Next
EndIf
count=ReadInt(f) ;point entities
For i%=1 To count
temp1s=ReadString(f)
Select temp1s
Case "screen"
temp1=ReadFloat(f)*RoomScale
temp2=ReadFloat(f)*RoomScale
temp3=ReadFloat(f)*RoomScale
temp2s$ =ReadString(f)
If temp1<>0 Or temp2<>0 Or temp3<>0 Then
Local ts.TempScreens = New TempScreens
ts\x = temp1
ts\y = temp2
ts\z = temp3
ts\imgpath = temp2s
ts\roomtemplate = rt
EndIf
Case "waypoint"
temp1=ReadFloat(f)*RoomScale
temp2=ReadFloat(f)*RoomScale
temp3=ReadFloat(f)*RoomScale
Local w.TempWayPoints = New TempWayPoints
w\roomtemplate = rt
w\x = temp1
w\y = temp2
w\z = temp3
Case "light"
temp1=ReadFloat(f)*RoomScale
temp2=ReadFloat(f)*RoomScale
temp3=ReadFloat(f)*RoomScale
If temp1<>0 Or temp2<>0 Or temp3<>0 Then
range# = ReadFloat(f)/2000.0
lcolor$=ReadString(f)
intensity# = Min(ReadFloat(f)*0.8,1.0)
r%=Int(Piece(lcolor,1," "))*intensity
g%=Int(Piece(lcolor,2," "))*intensity
b%=Int(Piece(lcolor,3," "))*intensity
AddTempLight(rt, temp1,temp2,temp3, 2, range, r,g,b)
Else
ReadFloat(f) : ReadString(f) : ReadFloat(f)
EndIf
Case "spotlight"
temp1=ReadFloat(f)*RoomScale
temp2=ReadFloat(f)*RoomScale
temp3=ReadFloat(f)*RoomScale
If temp1<>0 Or temp2<>0 Or temp3<>0 Then
range# = ReadFloat(f)/2000.0
lcolor$=ReadString(f)
intensity# = Min(ReadFloat(f)*0.8,1.0)
r%=Int(Piece(lcolor,1," "))*intensity
g%=Int(Piece(lcolor,2," "))*intensity
b%=Int(Piece(lcolor,3," "))*intensity
Local lt.LightTemplates = AddTempLight(rt, temp1,temp2,temp3, 2, range, r,g,b)
angles$=ReadString(f)
pitch#=Piece(angles,1," ")
yaw#=Piece(angles,2," ")
lt\pitch = pitch
lt\yaw = yaw
lt\innerconeangle = ReadInt(f)
lt\outerconeangle = ReadInt(f)
Else
ReadFloat(f) : ReadString(f) : ReadFloat(f) : ReadString(f) : ReadInt(f) : ReadInt(f)
EndIf
Case "soundemitter"
temp1i=0
For j = 0 To MaxRoomEmitters-1
If rt\TempSoundEmitter[j]=0 Then
rt\TempSoundEmitterX[j]=ReadFloat(f)*RoomScale
rt\TempSoundEmitterY[j]=ReadFloat(f)*RoomScale
rt\TempSoundEmitterZ[j]=ReadFloat(f)*RoomScale
rt\TempSoundEmitter[j]=ReadInt(f)
rt\TempSoundEmitterRange[j]=ReadFloat(f)
temp1i=1
Exit
EndIf
Next
If temp1i=0 Then
ReadFloat(f)
ReadFloat(f)
ReadFloat(f)
ReadInt(f)
ReadFloat(f)
EndIf
Case "playerstart"
temp1=ReadFloat(f) : temp2=ReadFloat(f) : temp3=ReadFloat(f)
angles$=ReadString(f)
pitch#=Piece(angles,1," ")
yaw#=Piece(angles,2," ")
roll#=Piece(angles,3," ")
If cam Then
PositionEntity cam,temp1,temp2,temp3
RotateEntity cam,pitch,yaw,roll
EndIf
Case "model"
file = ReadString(f)
If file<>""
Local model = CreatePropObj("GFX\Map\Props\"+file);LoadMesh("GFX\Map\Props\"+file)
temp1=ReadFloat(f) : temp2=ReadFloat(f) : temp3=ReadFloat(f)
PositionEntity model,temp1,temp2,temp3
temp1=ReadFloat(f) : temp2=ReadFloat(f) : temp3=ReadFloat(f)
RotateEntity model,temp1,temp2,temp3
temp1=ReadFloat(f) : temp2=ReadFloat(f) : temp3=ReadFloat(f)
ScaleEntity model,temp1,temp2,temp3
EntityParent model,Opaque
EntityType model,HIT_MAP
EntityPickMode model,2
Else
DebugLog "file = 0"
temp1=ReadFloat(f) : temp2=ReadFloat(f) : temp3=ReadFloat(f)
DebugLog temp1+", "+temp2+", "+temp3
;Stop
EndIf
End Select
Next
Local obj%
temp1i=CopyMesh(Alpha)
FlipMesh temp1i
AddMesh temp1i,Alpha
FreeEntity temp1i
If brush <> 0 Then FreeBrush brush
AddMesh Alpha,Opaque
FreeEntity Alpha
EntityFX Opaque,3
EntityAlpha hiddenMesh,0.0
EntityAlpha Opaque,1.0
;EntityType Opaque,HIT_MAP
EntityType hiddenMesh,HIT_MAP
FreeTexture blankTexture
;AddMesh hiddenMesh,BigRoomMesh
obj=CreatePivot()
CreatePivot(obj) ;skip "meshes" object
EntityParent Opaque,obj
EntityParent hiddenMesh,obj
CreatePivot(obj) ;skip "pointentites" object
CreatePivot(obj) ;skip "solidentites" object
EntityParent collisionMeshes,obj
CloseFile f
CatchErrors("LoadRMesh")
Return obj%
End Function
;-----------;;;;
Function StripPath$(file$)
Local name$=""
If Len(file$)>0
For i=Len(file$) To 1 Step -1
mi$=Mid$(file$,i,1)
If mi$="\" Or mi$="/" Then Return name$
name$=mi$+name$
Next
EndIf
Return name$
End Function
Function Piece$(s$,entry,char$=" ")
While Instr(s,char+char)
s=Replace(s,char+char,char)
Wend
For n=1 To entry-1
p=Instr(s,char)
s=Right(s,Len(s)-p)
Next
p=Instr(s,char)
If p<1
a$=s
Else
a=Left(s,p-1)
EndIf
Return a
End Function
Function KeyValue$(entity,key$,defaultvalue$="")
properties$=EntityName(entity)
properties$=Replace(properties$,Chr(13),"")
key$=Lower(key)
Repeat
p=Instr(properties,Chr(10))
If p Then test$=(Left(properties,p-1)) Else test=properties
testkey$=Piece(test,1,"=")
testkey=Trim(testkey)
testkey=Replace(testkey,Chr(34),"")
testkey=Lower(testkey)
If testkey=key Then
value$=Piece(test,2,"=")
value$=Trim(value$)
value$=Replace(value$,Chr(34),"")
Return value
EndIf
If Not p Then Return defaultvalue$
properties=Right(properties,Len(properties)-p)
Forever
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Forest gen consts
Const gridsize% = 10
Const deviation_chance% = 40 ;out of 100
Const branch_chance% = 65
Const branch_max_life% = 4
Const branch_die_chance% = 18
Const max_deviation_distance% = 3
Const return_chance% = 27
Const center = 5 ;(gridsize-1) / 2
Include "Drawportals.bb"
Type Forest
Field TileMesh%[6]
Field DetailMesh%[6]
Field TileTexture%[10]
Field grid%[(gridsize*gridsize)+11]
Field TileEntities%[(gridsize*gridsize)+1]
Field Forest_Pivot%
Field Door%[2]
Field DetailEntities%[2]
Field ID%
End Type
Function move_forward%(dir%,pathx%,pathy%,retval%=0)
;move 1 unit along the grid in the designated direction
If dir = 1 Then
If retval=0 Then
Return pathx
Else
Return pathy+1
EndIf
EndIf
If retval=0 Then
Return pathx-1+dir
Else
Return pathy
EndIf
End Function
Function chance%(chanc%)
;perform a chance given a probability
Return (Rand(0,100)<=chanc)
End Function
Function turn_if_deviating%(max_deviation_distance_%,pathx%,center_%,dir%,retval%=0)
;check if deviating and return the answer. if deviating, turn around
Local current_deviation% = center_ - pathx
Local deviated% = False
If (dir = 0 And current_deviation >= max_deviation_distance_) Or (dir = 2 And current_deviation <= -max_deviation_distance_) Then
dir = (dir + 2) Mod 4
deviated = True
EndIf
If retval=0 Then Return dir Else Return deviated
End Function
Function GenForestGrid(fr.Forest)
CatchErrors("Uncaught (GenForestGrid)")
fr\ID=LastForestID+1
LastForestID=LastForestID+1
Local door1_pos%,door2_pos%
Local i%,j%
door1_pos=Rand(3,7)
door2_pos=Rand(3,7)
;clear the grid
For i=0 To gridsize-1
For j=0 To gridsize-1
fr\grid[(j*gridsize)+i]=0
Next
Next
;set the position of the concrete and doors
;For i=0 To gridsize-1
; fr\grid[i]=2
; fr\grid[((gridsize-1)*gridsize)+i]=2
;Next
fr\grid[door1_pos]=3
fr\grid[((gridsize-1)*gridsize)+door2_pos]=3
;generate the path
Local pathx = door2_pos
Local pathy = 1
Local dir = 1 ;0 = left, 1 = up, 2 = right
fr\grid[((gridsize-1-pathy)*gridsize)+pathx] = 1
Local deviated%
While pathy < gridsize -4
If dir = 1 Then ;determine whether to go forward or to the side
If chance(deviation_chance) Then
;pick a branch direction
dir = 2 * Rand(0,1)
;make sure you have not passed max side distance
dir = turn_if_deviating(max_deviation_distance,pathx,center,dir)
deviated = turn_if_deviating(max_deviation_distance,pathx,center,dir,1)
If deviated Then fr\grid[((gridsize-1-pathy)*gridsize)+pathx]=1
pathx=move_forward(dir,pathx,pathy)
pathy=move_forward(dir,pathx,pathy,1)
EndIf
Else
;we are going to the side, so determine whether to keep going or go forward again
dir = turn_if_deviating(max_deviation_distance,pathx,center,dir)
deviated = turn_if_deviating(max_deviation_distance,pathx,center,dir,1)
If deviated Or chance(return_chance) Then dir = 1
pathx=move_forward(dir,pathx,pathy)
pathy=move_forward(dir,pathx,pathy,1)
;if we just started going forward go twice so as to avoid creating a potential 2x2 line
If dir=1 Then
fr\grid[((gridsize-1-pathy)*gridsize)+pathx]=1
pathx=move_forward(dir,pathx,pathy)
pathy=move_forward(dir,pathx,pathy,1)
EndIf
EndIf
;add our position to the grid
fr\grid[((gridsize-1-pathy)*gridsize)+pathx]=1
Wend
;finally, bring the path back to the door now that we have reached the end
dir = 1
While pathy < gridsize-2
pathx=move_forward(dir,pathx,pathy)
pathy=move_forward(dir,pathx,pathy,1)
fr\grid[((gridsize-1-pathy)*gridsize)+pathx]=1
Wend
If pathx<>door1_pos Then
dir=0
If door1_pos>pathx Then dir=2
While pathx<>door1_pos
pathx=move_forward(dir,pathx,pathy)
pathy=move_forward(dir,pathx,pathy,1)
fr\grid[((gridsize-1-pathy)*gridsize)+pathx]=1
Wend
EndIf
;attempt to create new branches
Local new_y%,temp_y%,new_x%
Local branch_type%,branch_pos%
new_y=-3 ;used for counting off; branches will only be considered once every 4 units so as to avoid potentially too many branches
While new_y<gridsize-6
new_y=new_y+4
temp_y=new_y
new_x=0
If chance(branch_chance) Then
branch_type=-1
If chance(cobble_chance) Then
branch_type=-2
EndIf
;create a branch at this spot
;determine if on left or on right
branch_pos=2*Rand(0,1)
;get leftmost or rightmost path in this row
leftmost=gridsize
rightmost=0
For i=0 To gridsize
If fr\grid[((gridsize-1-new_y)*gridsize)+i]=1 Then
If i<leftmost Then leftmost=i
If i>rightmost Then rightmost=i
EndIf
Next
If branch_pos=0 Then new_x=leftmost-1 Else new_x=rightmost+1
;before creating a branch make sure there are no 1's above or below
If (temp_y<>0 And fr\grid[((gridsize-1-temp_y+1)*gridsize)+new_x]=1) Or fr\grid[((gridsize-1-temp_y-1)*gridsize)+new_x]=1 Then
Exit ;break simply to stop creating the branch
EndIf
fr\grid[((gridsize-1-temp_y)*gridsize)+new_x]=branch_type ;make 4s so you don't confuse your branch for a path; will be changed later
If branch_pos=0 Then new_x=leftmost-2 Else new_x=rightmost+2
fr\grid[((gridsize-1-temp_y)*gridsize)+new_x]=branch_type ;branch out twice to avoid creating an unwanted 2x2 path with the real path
i = 2
While i<branch_max_life
i=i+1
If chance(branch_die_chance) Then
Exit
EndIf
If Rand(0,3)=0 Then ;have a higher chance to go up to confuse the player