-
Notifications
You must be signed in to change notification settings - Fork 107
/
Converter.bb
675 lines (527 loc) · 16.6 KB
/
Converter.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
Graphics3D 640,480,0,2
AppTitle "Convert SCP-CB rooms (B3D generated by 3DWS) to RMESH"
Const HIT_MAP%=1
Const HIT_PLAYER%=2
Collisions HIT_PLAYER,HIT_MAP,2,2
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 StripFilename$(file$)
;Local name$=""
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 EntityScaleX#(entity, globl=False)
If globl Then TFormVector 1,0,0,entity,0 Else TFormVector 1,0,0,entity,GetParent(entity)
Return Sqr(TFormedX()*TFormedX()+TFormedY()*TFormedY()+TFormedZ()*TFormedZ())
End Function
Function EntityScaleY#(entity, globl=False)
If globl Then TFormVector 0,1,0,entity,0 Else TFormVector 0,1,0,entity,GetParent(entity)
Return Sqr(TFormedX()*TFormedX()+TFormedY()*TFormedY()+TFormedZ()*TFormedZ())
End Function
Function EntityScaleZ#(entity, globl=False)
If globl Then TFormVector 0,0,1,entity,0 Else TFormVector 0,0,1,entity,GetParent(entity)
Return Sqr(TFormedX()*TFormedX()+TFormedY()*TFormedY()+TFormedZ()*TFormedZ())
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
Function isAlpha%(tex%) ;detect transparency in textures
Local temp1s$=StripPath(TextureName(tex))
Local temp1i%
If Instr(temp1s,".png")<>0 Or Instr(temp1s,".tga")<>0 Or Instr(temp1s,".tpic")<>0 Then ;texture is PNG or TARGA
LockBuffer(TextureBuffer(tex))
For x%=0 To TextureWidth(tex)-1
For y%=0 To TextureHeight(tex)-1
temp1i=ReadPixelFast(x,y,TextureBuffer(tex))
temp1i=temp1i Shr 24
If temp1i<255 Then
UnlockBuffer(TextureBuffer(tex))
;DebugLog temp1s + " has transparency."
Return 3 ;texture has transparency
EndIf
Next
Next
UnlockBuffer(TextureBuffer(tex))
;DebugLog temp1s + " is opaque."
Return 1 ;texture is opaque
Else If Instr(temp1s,"_lm")<>0 Then ;texture is a lightmap
;DebugLog temp1s + " is a lightmap."
Return 2
EndIf
;DebugLog temp1s + " is opaque."
Return 1 ;texture is opaque
End Function
Function SaveRoomMesh(BaseMesh%,filename$) ;base mesh should be a 3D World Studio mesh
DebugLog filename + "___" + BaseMesh
;If Right(filename, 5)="rmesh" Then filename=Left(filename, Len(filename)-5)+"b3d"
;DebugLog filename
Local node%,classname$
Local surf%,brush%,tex%,texname$
Local temp1i%
Local tempmesh% = BaseMesh
Local f% = WriteFile(filename)
Local drawnmesh% = CreateMesh()
Local hiddenmesh% = CreateMesh()
Local TriggerboxAmount% = 0
Local Triggerbox[128]
Local TriggerboxName$[128]
For c%=1 To CountChildren(tempmesh)
node=GetChild(tempmesh,c)
classname$=Lower(KeyValue(node,"classname"))
Select classname
Case "mesh"
ScaleMesh node,EntityScaleX(node),EntityScaleY(node),EntityScaleZ(node)
RotateMesh node,EntityPitch(node),EntityYaw(node),EntityRoll(node)
PositionMesh node,EntityX(node),EntityY(node),EntityZ(node)
AddMesh node,drawnmesh
Case "brush"
RotateMesh node,EntityPitch(node),EntityYaw(node),EntityRoll(node)
PositionMesh node,EntityX(node),EntityY(node),EntityZ(node)
AddMesh node,drawnmesh
Case "field_hit"
RotateMesh node,EntityPitch(node),EntityYaw(node),EntityRoll(node)
PositionMesh node,EntityX(node),EntityY(node),EntityZ(node)
AddMesh node,hiddenmesh
Case "trigger"
Triggerbox[TriggerboxAmount] = CreateMesh()
RotateMesh node,EntityPitch(node),EntityYaw(node),EntityRoll(node)
PositionMesh node,EntityX(node),EntityY(node),EntityZ(node)
AddMesh node,Triggerbox[TriggerboxAmount]
TriggerboxName[TriggerboxAmount] = String(KeyValue(node,"event","event"),1)
TriggerboxAmount=TriggerboxAmount+1
End Select
Next
If TriggerboxAmount% = 0
WriteString f,"RoomMesh"
Else
WriteString f,"RoomMesh.HasTriggerBox"
EndIf
;WriteString f,"drawn"
WriteInt f,CountSurfaces(drawnmesh) : DebugLog CountSurfaces(drawnmesh)
For i%=1 To CountSurfaces(drawnmesh)
surf=GetSurface(drawnmesh,i)
brush=GetSurfaceBrush(surf)
tex=0
tex=GetBrushTexture(brush,0)
If tex<>0 Then
WriteByte(f,isAlpha(tex))
texname=TextureName(tex)
WriteString f,StripPath(texname)
FreeTexture tex
Else
WriteByte(f,0)
EndIf
tex=0
tex=GetBrushTexture(brush,1)
If tex<>0 Then
WriteByte(f,isAlpha(tex))
texname=TextureName(tex)
WriteString f,StripPath(texname)
FreeTexture tex
Else
WriteByte(f,0)
EndIf
FreeBrush brush
WriteInt f,CountVertices(surf)
For j%=0 To CountVertices(surf)-1
;world coords
WriteFloat f,VertexX(surf,j)
WriteFloat f,VertexY(surf,j)
WriteFloat f,VertexZ(surf,j)
;texture coords
WriteFloat f,VertexU(surf,j,0)
WriteFloat f,VertexV(surf,j,0)
WriteFloat f,VertexU(surf,j,1)
WriteFloat f,VertexV(surf,j,1)
;colors
WriteByte f,VertexRed(surf,j)
WriteByte f,VertexGreen(surf,j)
WriteByte f,VertexBlue(surf,j)
Next
WriteInt f,CountTriangles(surf)
For j%=0 To CountTriangles(surf)-1
WriteInt f,TriangleVertex(surf,j,0)
WriteInt f,TriangleVertex(surf,j,1)
WriteInt f,TriangleVertex(surf,j,2)
Next
Next
;WriteString f,"hidden"
WriteInt f,CountSurfaces(hiddenmesh) : DebugLog CountSurfaces(hiddenmesh)
For i%=1 To CountSurfaces(hiddenmesh)
surf=GetSurface(hiddenmesh,i)
WriteInt f,CountVertices(surf)
For j%=0 To CountVertices(surf)-1
;world coords
WriteFloat f,VertexX(surf,j)
WriteFloat f,VertexY(surf,j)
WriteFloat f,VertexZ(surf,j)
Next
WriteInt f,CountTriangles(surf)
For j%=0 To CountTriangles(surf)-1
WriteInt f,TriangleVertex(surf,j,0)
WriteInt f,TriangleVertex(surf,j,1)
WriteInt f,TriangleVertex(surf,j,2)
Next
Next
If TriggerboxAmount > 0
WriteInt f,TriggerboxAmount
For z=0 To TriggerboxAmount-1
WriteInt f,CountSurfaces(Triggerbox[z]) : DebugLog CountSurfaces(Triggerbox[z])
For i%=1 To CountSurfaces(Triggerbox[z])
surf=GetSurface(Triggerbox[z],i)
WriteInt f,CountVertices(surf)
For j%=0 To CountVertices(surf)-1
;world coords
WriteFloat f,VertexX(surf,j)
WriteFloat f,VertexY(surf,j)
WriteFloat f,VertexZ(surf,j)
Next
WriteInt f,CountTriangles(surf)
For j%=0 To CountTriangles(surf)-1
WriteInt f,TriangleVertex(surf,j,0)
WriteInt f,TriangleVertex(surf,j,1)
WriteInt f,TriangleVertex(surf,j,2)
Next
Next
WriteString f,TriggerboxName[z]
Next
EndIf
;WriteString f,"point_ents"
temp1i=0
For c%=1 To CountChildren(tempmesh)
node=GetChild(tempmesh,c)
classname$=Lower(KeyValue(node,"classname"))
Select classname
Case "screen","waypoint","light","spotlight","soundemitter","playerstart","model"
temp1i=temp1i+1
End Select
Next
WriteInt f,temp1i
For c%=1 To CountChildren(tempmesh)
node=GetChild(tempmesh,c)
classname$=Lower(KeyValue(node,"classname"))
Select classname
Case "screen"
WriteString f,classname
WriteFloat f,EntityX(node)
WriteFloat f,EntityY(node)
WriteFloat f,EntityZ(node)
WriteString f,KeyValue(node,"imgpath","")
Case "waypoint"
WriteString f,classname
WriteFloat f,EntityX(node)
WriteFloat f,EntityY(node)
WriteFloat f,EntityZ(node)
Case "light"
WriteString f,classname
WriteFloat f,EntityX(node)
WriteFloat f,EntityY(node)
WriteFloat f,EntityZ(node)
WriteFloat f,Float(KeyValue(node,"range","1"))
WriteString f,KeyValue(node,"color","255 255 255")
WriteFloat f,Float(KeyValue(node,"intensity","1.0"))
Case "spotlight"
WriteString f,classname
WriteFloat f,EntityX(node)
WriteFloat f,EntityY(node)
WriteFloat f,EntityZ(node)
WriteFloat f,Float(KeyValue(node,"range","1"))
WriteString f,KeyValue(node,"color","255 255 255")
WriteFloat f,Float(KeyValue(node,"intensity","1.0"))
WriteString f,KeyValue(node,"angles","0 0 0")
WriteInt f,Int(KeyValue(node,"innerconeangle",""))
WriteInt f,Int(KeyValue(node,"outerconeangle",""))
Case "soundemitter"
WriteString f,classname
WriteFloat f,EntityX(node)
WriteFloat f,EntityY(node)
WriteFloat f,EntityZ(node)
WriteInt f,Int(KeyValue(node,"sound","0"))
WriteFloat f,Float(KeyValue(node,"range","1"))
Case "playerstart"
WriteString f,classname
WriteFloat f,EntityX(node)
WriteFloat f,EntityY(node)
WriteFloat f,EntityZ(node)
WriteString f,KeyValue(node,"angles","0 0 0")
Case "model"
WriteString f,classname
WriteString f,KeyValue(node,"file","")
WriteFloat f,EntityX(node)
WriteFloat f,EntityY(node)
WriteFloat f,EntityZ(node)
WriteFloat f,EntityPitch(node)
WriteFloat f,EntityYaw(node)
WriteFloat f,EntityRoll(node)
WriteFloat f,EntityScaleX(node)
WriteFloat f,EntityScaleY(node)
WriteFloat f,EntityScaleZ(node)
End Select
Next
WriteString f,"EOF"
CloseFile f
FreeEntity drawnmesh
FreeEntity hiddenmesh
End Function
;INI-funktiot:
Function GetINIString$(file$, section$, parameter$)
Local TemporaryString$ = ""
Local f = ReadFile(file)
While Not Eof(f)
If ReadLine(f) = "["+section+"]" Then
Repeat
TemporaryString = ReadLine(f)
If Trim( Left(TemporaryString, Max(Instr(TemporaryString,"=")-1,0)) ) = parameter Then
CloseFile f
Return Trim( Right(TemporaryString,Len(TemporaryString)-Instr(TemporaryString,"=")) )
EndIf
Until Left(TemporaryString,1) = "[" Or Eof(f)
CloseFile f
Return ""
EndIf
Wend
CloseFile f
End Function
Function GetINIInt%(file$, section$, parameter$)
Local strtemp$ = Lower(GetINIString(file$, section$, parameter$))
Select strtemp
Case "true"
Return 1
Case "false"
Return 0
Default
Return Int(strtemp)
End Select
Return
End Function
Function GetINIFloat#(file$, section$, parameter$)
Return GetINIString(file$, section$, parameter$)
End Function
Function PutINIValue%(INI_sAppName$, INI_sSection$, INI_sKey$, INI_sValue$)
; Returns: True (Success) or False (Failed)
INI_sSection = "[" + Trim$(INI_sSection) + "]"
INI_sUpperSection$ = Upper$(INI_sSection)
INI_sKey = Trim$(INI_sKey)
INI_sValue = Trim$(INI_sValue)
INI_sFilename$ = CurrentDir$() + "\" + INI_sAppName
; Retrieve the INI data (if it exists)
INI_sContents$= INI_FileToString(INI_sFilename)
; (Re)Create the INI file updating/adding the SECTION, KEY and VALUE
INI_bWrittenKey% = False
INI_bSectionFound% = False
INI_sCurrentSection$ = ""
INI_lFileHandle = WriteFile(INI_sFilename)
If INI_lFileHandle = 0 Then Return False ; Create file failed!
INI_lOldPos% = 1
INI_lPos% = Instr(INI_sContents, Chr$(0))
While (INI_lPos <> 0)
INI_sTemp$ =Trim$(Mid$(INI_sContents, INI_lOldPos, (INI_lPos - INI_lOldPos)))
If (INI_sTemp <> "") Then
If Left$(INI_sTemp, 1) = "[" And Right$(INI_sTemp, 1) = "]" Then
; Process SECTION
If (INI_sCurrentSection = INI_sUpperSection) And (INI_bWrittenKey = False) Then
INI_bWrittenKey = INI_CreateKey(INI_lFileHandle, INI_sKey, INI_sValue)
End If
INI_sCurrentSection = Upper$(INI_CreateSection(INI_lFileHandle, INI_sTemp))
If (INI_sCurrentSection = INI_sUpperSection) Then INI_bSectionFound = True
Else
; KEY=VALUE
lEqualsPos% = Instr(INI_sTemp, "=")
If (lEqualsPos <> 0) Then
If (INI_sCurrentSection = INI_sUpperSection) And (Upper$(Trim$(Left$(INI_sTemp, (lEqualsPos - 1)))) = Upper$(INI_sKey)) Then
If (INI_sValue <> "") Then INI_CreateKey INI_lFileHandle, INI_sKey, INI_sValue
INI_bWrittenKey = True
Else
WriteLine INI_lFileHandle, INI_sTemp
End If
End If
End If
End If
; Move through the INI file...
INI_lOldPos = INI_lPos + 1
INI_lPos% = Instr(INI_sContents, Chr$(0), INI_lOldPos)
Wend
; KEY wasn't found in the INI file - Append a new SECTION if required and create our KEY=VALUE line
If (INI_bWrittenKey = False) Then
If (INI_bSectionFound = False) Then INI_CreateSection INI_lFileHandle, INI_sSection
INI_CreateKey INI_lFileHandle, INI_sKey, INI_sValue
End If
CloseFile INI_lFileHandle
Return True ; Success
End Function
Function INI_FileToString$(INI_sFilename$)
INI_sString$ = ""
INI_lFileHandle% = ReadFile(INI_sFilename)
If INI_lFileHandle <> 0 Then
While Not(Eof(INI_lFileHandle))
INI_sString = INI_sString + ReadLine$(INI_lFileHandle) + Chr$(0)
Wend
CloseFile INI_lFileHandle
End If
Return INI_sString
End Function
Function INI_CreateSection$(INI_lFileHandle%, INI_sNewSection$)
If FilePos(INI_lFileHandle) <> 0 Then WriteLine INI_lFileHandle, "" ; Blank line between sections
WriteLine INI_lFileHandle, INI_sNewSection
Return INI_sNewSection
End Function
Function INI_CreateKey%(INI_lFileHandle%, INI_sKey$, INI_sValue$)
WriteLine INI_lFileHandle, INI_sKey + "=" + INI_sValue
Return True
End Function
; matemaattiset funktiot:
Function Min#(a#,b#)
If a < b Then Return a Else Return b
End Function
Function Max#(a#,b#)
If a > b Then Return a Else Return b
End Function
Local state%=0
SetBuffer BackBuffer()
ClsColor 0,0,0
Cls
Color 255,255,255
Text 5,5,"Press a key:"
Text 5,25,"1 - Convert B3D (generated by 3D World Studio) to RMESH"
Text 5,45,"2 - Convert a specific B3D (generated by 3D World Studio) to RMESH"
Text 5,65,"3 - Reset rooms.ini to use B3D meshes"
Text 5,85,"ESC - Close without doing anything"
Flip
While (Not KeyHit(1))
If KeyHit(2) Or KeyHit(79) Then state=1 : Exit
If KeyHit(3) Or KeyHit(80) Then state=2 : Exit
If KeyHit(4) Or KeyHit(81) Then state=3 : Exit
Wend
Local Stri$,TemporaryString$,f%
Type INIConvert
Field file$
Field section$
Field key$
Field value$
End Type
Local ic.INIConvert
If state=1 Then ;convert B3D to Rmesh
If FileSize("Data\rooms_b3d.ini")=0 Then
CopyFile "Data\rooms.ini","Data\rooms_b3d.ini"
EndIf
f%=ReadFile("Data\rooms.ini")
While Not Eof(f)
TemporaryString = Trim(ReadLine(f))
If Left(TemporaryString,1) = "[" Then
TemporaryString = Mid(TemporaryString, 2, Len(TemporaryString) - 2)
If TemporaryString <> "room ambience" Then
Stri=GetINIString("Data\rooms.ini",TemporaryString,"mesh path")
mesh=LoadAnimMesh(Stri)
SaveRoomMesh(mesh,Replace(Stri,".b3d",".rmesh"))
Cls
Text 5,5,"Converted "+Chr(34)+Stri+Chr(34)
Flip
ic.INIConvert=New INIConvert
ic\file="Data\rooms.ini"
ic\section=TemporaryString
ic\key="mesh path"
ic\value=Replace(Stri,".b3d",".rmesh")
;PutINIValue("Data\rooms.ini",TemporaryString,"mesh path",Replace(Stri,".b3d",".rmesh"))
EndIf
EndIf
Wend
For ic.INIConvert=Each INIConvert
PutINIValue(ic\file,ic\section,ic\key,ic\value)
Next
Cls
Text 5,5,"Conversion complete"
Flip
Delay 1000
CloseFile f
Else If state=2
Cls
Flip
FlushKeys()
Stri=Input("Path for the room to be converted: ")
mesh=LoadAnimMesh(Stri)
SaveRoomMesh(mesh,Replace(Stri,".b3d",".rmesh"))
Cls
Text 5,5,"Conversion of "+Stri+" complete"
Flip
Delay 1000
Else If state=3
f%=ReadFile("Data\rooms.ini")
While Not Eof(f)
TemporaryString = Trim(ReadLine(f))
If Left(TemporaryString,1) = "[" Then
TemporaryString = Mid(TemporaryString, 2, Len(TemporaryString) - 2)
If TemporaryString <> "room ambience" Then
Stri=GetINIString("Data\rooms.ini",TemporaryString,"mesh path")
ic.INIConvert=New INIConvert
ic\file="Data\rooms.ini"
ic\section=TemporaryString
ic\key="mesh path"
ic\value=Replace(Stri,".rmesh",".b3d")
;PutINIValue("Data\rooms.ini",TemporaryString,"mesh path",Replace(Stri,".rmesh",".b3d"))
EndIf
EndIf
Wend
For ic.INIConvert=Each INIConvert
PutINIValue(ic\file,ic\section,ic\key,ic\value)
Next
Cls
Text 5,5,"Reset complete"
Flip
Delay 1000
CloseFile(f)
EndIf
;~IDEal Editor Parameters:
;~F#9#19#29#2E#33#38#49#5F#18A#19F#1AD#1B1#202#210#218#220#224#23B
;~C#Blitz3D