-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTTDXeditProcs.old.bas
1147 lines (1062 loc) · 44.1 KB
/
TTDXeditProcs.old.bas
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
Attribute VB_Name = "TTDXeditProcs"
Option Explicit
Option Compare Text
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function GetUNIXTime Lib "TTDXHelp.dll" () As Long
Declare Function CheckMenuRadioItem Lib "user32" (ByVal hMenu As Long, ByVal un1 As Long, ByVal un2 As Long, ByVal un3 As Long, ByVal un4 As Long) As Boolean
Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, ByVal NPos As Long) As Long
Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal NPos As Long) As Long
Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Global Const MF_BYPOSITION = &H400&
Private Const Poff As Long = &H52A62
Public Type TTDXgeneral
GameName As String
ClimType As Byte
ClimName As String
CityNameSet As Byte
CityNames As String
VehSize As Byte
End Type
Public Type TTDXlandscape
X As Integer
Y As Integer
Owner As Byte
Object As Byte
Height As Byte
L1 As Byte
L2 As Byte
L3 As Long
L4 As Byte
L5 As Byte
End Type
Public Type FinanceInfo
Construction As Long
NewVehicles As Long
TrainRunningCosts As Long
RoadVehRunningCosts As Long
AircraftRunningCosts As Long
ShipRunningCosts As Long
PropertyMaintenance As Long
TrainIncome As Long
RoadVehIncome As Long
AircraftIncome As Long
ShipIncome As Long
LoanInterest As Long
Other As Long
End Type
Public Type TTDXplayer
Number As Integer
Id As Integer
Money As Long
Debt As Long
HQx As Integer
HQy As Integer
Offset As Long
FinancesThisYear As FinanceInfo
FinancesLastYear As FinanceInfo
FinancesTwoYearsAgo As FinanceInfo
End Type
Public Type TTDXIndInfo
Number As Integer
X As Byte
Y As Byte
W As Byte
H As Byte
Type As Byte
HomeTown As Byte
Prod(1) As Byte
ProR(1) As Byte
del(2) As Byte
Name As String
Offset As Long
End Type
Public Type TTDXCitInfo
Number As Integer
X As Byte
Y As Byte
Population As Long
CRate(7) As Long
CRateE(7) As Boolean
Name As String
Offset As Long
End Type
Public Type TTDXStation
Number As Integer
BaseX As Byte
BaseY As Byte
Parts As Byte
BusX As Byte
BusY As Byte
BusStatus As Byte
TruckX As Byte
TruckY As Byte
TruckStatus As Byte
RailX As Byte
RailY As Byte
RailDir As Boolean
RailTracks As Byte
RailTrackLen As Byte
AirX As Byte
AirY As Byte
AirportType As Byte
DockX As Byte
DockY As Byte
Cargo(11) As Integer
CRate(11) As Byte
CAcc(11) As Byte
CEnrout(11) As Byte
CEnroutOrg(11) As Byte
Cgood2(11) As Byte
Cgood5(11) As Byte
Cgood6(11) As Byte
Cgood7(11) As Byte
HomeTown As Byte
Owner As Byte
Name As String
Offset As Long
End Type
Public Type TTDXVehicle
Number As Long
Class As Byte
SubClass As Byte
Owner As Byte
CargoT As Byte
CargoMax As Long
CargoCur As Long
SpeedMax As Long
Value As Long
Age As Long
AgeMax As Long
Rel As Byte
RelDropRate As Long
Type As Byte
Next As Long
Name As String
Offset As Long
End Type
Public CurFile As String, FileChanged As Boolean
Public CargoTypes(25) As String, IndustryTypes(50) As String
Public Cities(70) As String, Stations(250) As String
Private F As New FileSystemObject
'
' The importaint internal values
'
Private wData() As Byte, wHeadData(46) As Byte
Private wTTDPext As Integer, wClimate As Integer
Private wError As Integer, wExtraChunks
Global CurrencyMultiplier As Double
Global CurrencyLabel As String
Global CurrencySeparator As String
Global CurrencySymbolBefore As Boolean
Function FlipSign(ByVal Value As Long) As Long
FlipSign = -Value
'If Value > 0 Then
'FlipSign = 0 - Value
'Else
'FlipSign = 0 - ((2 * Value))
'End If
End Function
Function FormatMoney(ByVal Money As Long) As String
On Error GoTo Error
Dim MoneyStr As String
Dim NewMoneyStr As String
Dim SetOf3 As Byte
Dim i As Integer
Dim Negative As Byte
MoneyStr = CStr(CCur(Money * CurrencyMultiplier))
If Left$(MoneyStr, 1) = "-" Then
MoneyStr = Right$(MoneyStr, Len(MoneyStr) - 1)
Negative = True
Else
Negative = False
End If
NewMoneyStr = ""
SetOf3 = 0
For i = Len(MoneyStr) To 1 Step -1
SetOf3 = SetOf3 + 1
NewMoneyStr = MID$(MoneyStr, i, 1) & NewMoneyStr
If SetOf3 = 3 And i <> 1 Then
NewMoneyStr = CurrencySeparator & NewMoneyStr
SetOf3 = 0
End If
Next i
If CurrencySymbolBefore = True Then
FormatMoney = CurrencyLabel & NewMoneyStr
' FormatMoney = CurrencyLabel & Format(Money * CurrencyMultiplier, "###" & CurrencySeparator & "###" & CurrencySeparator & "###" & CurrencySeparator & "###" & CurrencySeparator & "###")
Else
FormatMoney = NewMoneyStr & CurrencyLabel
'FormatMoney = Format(Money * CurrencyMultiplier, "###,###,###,###,###") & CurrencyLabel
End If
If Negative = True Then
FormatMoney = "-" & FormatMoney
End If
Exit Function
Error:
Select Case ErrorProc(Err, "Function: TTDXeditProcs.FormatMoney(" & Money & ")")
Case 3:
End
Case 2:
Resume Next
Case 1:
Resume
End Select
End Function
Function ErrorProc(Code As Long, Optional Extra)
Dim ErrDesc As String
ErrDesc = Err.Description
On Error GoTo Error
Dim mb As Integer
Dim ExtraText As String
If IsMissing(Extra) = False Then
ExtraText = vbCrLf & vbCrLf & Extra
Else
ExtraText = ""
End If
mb = MsgBox("The following error occurred:" & vbCrLf & vbCrLf & ErrDesc & vbCrLf & "Error Number " & CStr(Code) & ExtraText, vbCritical Or vbAbortRetryIgnore, "TTDX Editor")
If mb = vbAbort Then
ErrorProc = 3
ElseIf mb = vbRetry Then
ErrorProc = 1
ElseIf mb = vbIgnore Then
ErrorProc = 2
End If
' ErrorProc = mb
Exit Function
Error:
Select Case ErrorProc(Err, "WARNING! This error occurred during the error procedure. Something is very wrong!")
Case 3:
End
Case 2:
Resume Next
Case 1:
Resume
End Select
End Function
Public Function TTDXLoadFile(ByVal vPath As String) As Integer
Dim Wa As Long, Wb As Long, Wc As Integer, Wd As Integer, wWork() As Byte
Dim Wsa As String, NoMore As Boolean, TmpStr As String, TmpPos As Long
Dim j As Integer
TTDXLoadFile = 0: Wa = 0: Wb = 0
If Not F.FileExists(vPath) Then TTDXLoadFile = 1: Exit Function
If (vPath Like "*.s??hdr") Or (vPath Like "*.s??dta") Then
'
' Loading an uncompressed file
'
Wsa = Left(vPath, Len(vPath) - 3)
If F.FileExists(Wsa + "hdr") And F.FileExists(Wsa + "dta") Then
Erase wData: CurFile = "": wTTDPext = 0
Wb = F.GetFile(Wsa + "dta").Size
End If
If Wb < 49 Then TTDXLoadFile = 1: Exit Function
If (Wb Mod 108800) <> 74873 Then TTDXLoadFile = 2: Exit Function
'
' Reserve Mem and Load Data
'
ReDim wData(Wb - 1)
Open Wsa + "hdr" For Binary As 1
Get 1, , wHeadData()
Close 1
Open Wsa + "dta" For Binary As 1
Get 1, , wData()
Close 1
vPath = Wsa
ElseIf InStr(".sv0.sv1.sv2.ss0.ss1.", "." + F.GetExtensionName(vPath) + ".") > 0 Then
'
' Load a normal savegame
'
Erase wData: CurFile = "": wTTDPext = 0
Wa = F.GetFile(vPath).Size
'
' Load The Data
'
ReDim wWork(Wa - 1)
Open vPath For Binary As 1
Get 1, , wWork()
Close 1
'
' Copy Headerinformation
'
For Wa = 0 To UBound(wHeadData)
wHeadData(Wa) = wWork(Wa)
Next Wa
'
' Decompression
'
ReDim wData(618873 + 256)
Wa = 49: Wb = 0
While Wa < UBound(wWork) - 4
'
' Increase dataarea if needed
'
If Wb > UBound(wData) - 256 Then ReDim Preserve wData(UBound(wData) + 108800)
Wc = wWork(Wa)
If Wc > 127 Then
Wc = Abs(Wc - 256): Wa = Wa + 1
For Wd = 0 To Wc
wData(Wb) = wWork(Wa): Wb = Wb + 1
Next Wd
Wa = Wa + 1
Else
Wa = Wa + 1
For Wd = 0 To Wc
wData(Wb) = wWork(Wa): Wb = Wb + 1: Wa = Wa + 1
Next Wd
End If
If Not fFastMode Then DoEvents
Wend
'
' Check result and reduce memory array size to minimum
'
If Wa <> UBound(wWork) - 3 Then TTDXLoadFile = 4: Exit Function
'OCR'If (Wb Mod 108800) <> 74873 Then TTDXLoadFile = 2: Exit Function
ReDim Preserve wData(Wb - 1)
Else
TTDXLoadFile = 5: Exit Function
End If
'
' Check filesize, check for extended vehicles
'
If Wb < 618873 Then
'
' Not enough data
'
TTDXLoadFile = 2
ElseIf Wb = 618873 Then
'
' No extended vehicles
'
CurFile = vPath: wTTDPext = 1
wClimate = wData(&H77131)
Else
If GetLong(&H44CB4) = &H70445454 Then
wExtraChunks = wData(&H44CB8)
Else
wExtraChunks = wData(&H24CBC)
End If
Dim ExtraChunkPos As Long, i As Integer, ExtraChunkLength As Long
Dim ExtraChunkSingleLength As Long
Dim MoreVehicles As Long
Dim Chunk6Broken As Boolean
MoreVehicles = wData(&H24CBA)
If MoreVehicles = 0 Then
MoreVehicles = 1
ElseIf MoreVehicles = 1 Then
MoreVehicles = 2
End If
'ExtraChunkPos = ((wData(&H24CBA) - 1) * &H1A900) + &H97179
ExtraChunkPos = ((MoreVehicles - 1) * &H1A900) + &H97179
'If wData(&H24CBA) = 0 Then
'ExtraChunkPos = &H97179
'End If
For i = 1 To wExtraChunks
If wData(ExtraChunkPos) = 1 And wData(ExtraChunkPos + 1) = 128 Then
If GetLong(ExtraChunkPos + 6) And 1073741824 Then '31 Then ' and 2147483648
Chunk6Broken = False
Else
Chunk6Broken = True
End If
End If
ExtraChunkSingleLength = GetLong(ExtraChunkPos + 2)
ExtraChunkLength = ExtraChunkLength + ExtraChunkSingleLength + 6
ExtraChunkPos = ExtraChunkPos + ExtraChunkSingleLength + 6
If Chunk6Broken = True Then
ExtraChunkPos = ExtraChunkPos + 4
ExtraChunkLength = ExtraChunkLength + 4
Chunk6Broken = False
End If
Next i
' If Wb = (618873 + ExtraChunkLength) + ((wData(&H24CBA) - 1) * 108800) Then
If Wb = (618873 + ExtraChunkLength) + ((MoreVehicles - 1) * 108800) Then
'wTTDPext = wData(&H24CBA)
wTTDPext = MoreVehicles
CurFile = vPath
wClimate = wData(&H77131 + (wTTDPext - 1) * 108800)
Else
If Wb = (618873 + ExtraChunkLength) Then
wTTDPext = 1
CurFile = vPath
wClimate = wData(&H77131)
Else
TTDXLoadFile = 3
End If
End If
End If
#If MarkGame = 1 Then
'If wData(&H24CCB) >= 1 Then
'MsgBox "This game has been modified by TTDX Editor " & wData(&H24CCB) & _
'"." & wData(&H24CCC) & "." & wData(&H24CCD) & "!", vbExclamation, "TTDX Editor"
'End If
If GetLong(&H44CB4) = &H70445454 Then
wData(&H44BBA) = App.Major
wData(&H44BBB) = App.Minor
wData(&H44BBC) = App.Revision
Else
wData(&H24CCB) = App.Major
wData(&H24CCC) = App.Minor
wData(&H24CCD) = App.Revision
End If
#End If
#If MarkGameOldCode = 1 Then
wExtraChunks = wData(&H24CBC)
NoMore = False
ExtraChunkPos = ((wData(&H24CBA) - 1) * &H1A900) + &H97179
If wData(&H24CBA) > 0 Then
For i = 1 To wExtraChunks
If wData(ExtraChunkPos) = 99 Then
TmpStr = ""
TmpPos = 16
For j = 1 To wData(ExtraChunkPos + 6)
TmpStr = TmpStr & vbCrLf
TmpStr = TmpStr & _
"v" & wData(ExtraChunkPos + TmpPos) & "." _
& wData(ExtraChunkPos + TmpPos + 2) & "." _
& wData(ExtraChunkPos + TmpPos + 4)
TmpStr = TmpStr & vbTab & GetLong(ExtraChunkPos + TmpPos + 6)
TmpPos = TmpPos + 16
Next j
MsgBox "This game has been edited using TTDX Editor as follows:" & TmpStr, vbInformation, "TTDX Editor"
NoMore = True
End If
ExtraChunkSingleLength = GetLong(ExtraChunkPos + 2)
ExtraChunkLength = ExtraChunkLength + ExtraChunkSingleLength + 6
ExtraChunkPos = ExtraChunkPos + ExtraChunkSingleLength + 6
Next i
If NoMore = False Then
wData(&H24CBC) = wData(&H24CBC) + 1
ReDim Preserve wData(UBound(wData) + 32)
wData(ExtraChunkPos) = 99
PutLong wData, ExtraChunkPos + 2, 26
wData(ExtraChunkPos + 6) = 1
' padding space (8 bytes)
wData(ExtraChunkPos + 16) = App.Major
wData(ExtraChunkPos + 18) = App.Minor
wData(ExtraChunkPos + 20) = App.Revision
PutLong wData, ExtraChunkPos + 22, GetUNIXTime()
' padding space -> 32 (6 bytes)
Else
ReDim Preserve wData(UBound(wData) + 16)
'ExtraChunkPos = (ExtraChunkPos - ExtraChunkSingleLength) - 6
PutLong wData, ExtraChunkPos + 2, GetLong(ExtraChunkPos + 2) + 16
wData(ExtraChunkPos + 6) = wData(ExtraChunkPos + 6) + 1
wData((ExtraChunkPos + 6) - 1 * 16) = App.Major
wData(((ExtraChunkPos + 6) - 1 * 16) + 2) = App.Minor
wData(((ExtraChunkPos + 6) - 1 * 16) + 4) = App.Revision
PutLong wData, ((ExtraChunkPos + 6) - 1 * 16) + 4, GetUNIXTime()
End If
End If
#End If
SetVars
End Function
Public Function TTDXLoadError(vNo As Integer) As String
If vNo = 0 Then TTDXLoadError = "No Error."
If vNo = 1 Then TTDXLoadError = "File Not Found."
If vNo = 2 Then TTDXLoadError = "Unexpected Filesize."
If vNo = 3 Then TTDXLoadError = "Unexpected Extended Vehicle Array Size."
If vNo = 4 Then TTDXLoadError = "Decompression Error, Unexpected EOF."
If vNo = 5 Then TTDXLoadError = "Unknown fileformat."
End Function
Public Function TTDXSaveFile(ByVal vPath As String) As Integer
Dim Wa As Long, Wb As Long, Wc As Long, Wd As Byte, We As Byte, wWork() As Byte
Dim wHeadCheck(1) As Byte
TTDXSaveFile = 0
On Error GoTo TTDXsaveFileErr
'On Error GoTo 0
If vPath > " " Then
If F.FileExists(vPath) Then F.DeleteFile vPath
'
' Reserve workarea, calculate header checksum and copy the header
'
ReDim wWork(UBound(wData))
Wc = TTDXCalcHdCheck(wHeadData)
wWork(47) = Wc Mod 256
wWork(48) = Fix(Wc / 256)
For Wa = 0 To 46: wWork(Wa) = wHeadData(Wa): Next Wa
Wc = 49: Wb = 0
'
' Compress into RLE data
'
While Wb <= UBound(wData)
If wData(Wb) = wData(Wb + 1) Then
We = 124
For Wd = 2 To 124
If Wb + Wd > UBound(wData) Then We = Wd - 1: Exit For
If wData(Wb) <> wData(Wb + Wd) Then We = Wd - 1: Exit For
Next Wd
wWork(Wc) = 256 - We: wWork(Wc + 1) = wData(Wb): Wc = Wc + 2: Wb = Wb + We + 1
Else
We = 124
For Wd = 0 To 124
If Wb + Wd > UBound(wData) Then We = Wd - 1: Exit For
If wData(Wb + Wd) = wData(Wb + Wd + 1) Then We = Wd - 1: Exit For
Next Wd
wWork(Wc) = We: Wc = Wc + 1
For Wd = 0 To We: wWork(Wc) = wData(Wb): Wc = Wc + 1: Wb = Wb + 1: Next Wd
End If
If Not fFastMode Then DoEvents ' Give some room to other programs running
Wend
'
' Set proper size and calculate checksum
'
ReDim Preserve wWork(Wc - 1)
Wc = 0
For Wa = 0 To UBound(wWork)
Wc = (Wc And &HFFFFFF00) Or ((Wc And &HFF) + wWork(Wa) And &HFF)
If Wc And &H10000000 Then
Wc = (((Wc And &HFFFFFFF) * 8) Or ((Wc And &HE0000000) / 2 ^ 29) And &H7) Or &H80000000
Else
Wc = ((Wc And &HFFFFFFF) * 8) Or ((Wc And &HE0000000) / 2 ^ 29) And &H7
End If
Next Wa
'
' Puttin´ on the disc
'
Open vPath For Binary As 1
Put 1, , wWork()
Put 1, , CLng(Wc + 201100)
Close 1
FileChanged = False
Else
TTDXSaveFile = 1
End If
Exit Function
TTDXsaveFileErr:
TTDXSaveFile = 1
End Function
Public Function TTDXCalcHdCheck(vHd() As Byte) As Long
Dim Wc As Long, Wa As Integer
Wc = 0
For Wa = 0 To 46
Wc = (Wc + vHd(Wa)) * 2
If Wc And &H10000 Then Wc = (Wc Or 1) And &HFFFF&
Next Wa
TTDXCalcHdCheck = (Wc Xor &HAAAA&)
End Function
Public Function TTDXSaveUncom(ByVal vPath As String) As Integer
Dim Wsa As String, Wsb As String, Wc As Long
TTDXSaveUncom = 0
If vPath > " " Then
Wsa = F.GetExtensionName(vPath)
If InStr(".sv0.sv1.sv2.ss0.ss1.", "." + Wsa + ".") Then
Else
vPath = vPath + ".sv1"
End If
If F.FileExists(vPath + "hdr") Then F.DeleteFile (vPath + "hdr")
Wc = TTDXCalcHdCheck(wHeadData)
Open vPath + "hdr" For Binary As 1
Put 1, , wHeadData()
Put 1, , CByte(Wc Mod 256)
Put 1, , CByte(Fix(Wc / 256))
Close 1
If F.FileExists(vPath + "dta") Then F.DeleteFile (vPath + "dta")
Open vPath + "dta" For Binary As 1
Put 1, , wData()
Close 1
End If
Exit Function
TTDXsaveFileErr:
TTDXSaveUncom = 1
End Function
'****************************************************************************************************
'**** Landscape Procedures ****
'****************************************************************************************************
Public Function TTDXgetLandscape(Wx As Integer, Wy As Integer) As TTDXlandscape
Dim Wa As Integer, Woff As Long
If Wx < 0 Or Wy < 0 Then TTDXgetLandscape.Object = 7: Exit Function
If Wx > 255 Or Wy > 255 Then TTDXgetLandscape.Object = 7: Exit Function
Woff = Wx + 256& * Wy
If CurFile > " " Then
With TTDXgetLandscape
.X = Wx
.Y = Wy
.Owner = wData(&H4CBA + Woff)
.Object = (wData(&H77179 + (wTTDPext - 1) * 108800 + Woff) And &HF0) / 16
.Height = wData(&H77179 + (wTTDPext - 1) * 108800 + Woff) And &HF
.L1 = wData(&H4CBA + Woff)
.L2 = wData(&H14CBA + Woff)
.L3 = wData(&H24CBA + Woff * 2) + wData(&H24CBB + Woff * 2) * 256&
.L4 = wData(&H77179 + (wTTDPext - 1) * 108800 + Woff)
.L5 = wData(&H87179 + (wTTDPext - 1) * 108800 + Woff)
End With
End If
End Function
Public Sub TTDXputLandscape(wDta As TTDXlandscape)
Dim Wa As Byte, Woff As Long
With wDta
Woff = .X + 256& * .Y
If .Owner <> wData(&H4CBA + Woff) Then FileChanged = True: wData(&H4CBA + Woff) = .Owner
Wa = (.Object And &HF) * 16 + (.Height And &HF)
If Wa <> wData(&H77179 + (wTTDPext - 1) * 108800 + Woff) Then FileChanged = True: wData(&H77179 + (wTTDPext - 1) * 108800 + Woff) = Wa
If .L2 <> wData(&H14CBA + Woff) Then FileChanged = True: wData(&H14CBA + Woff) = .L2
If .L3 <> wData(&H24CBA + Woff * 2) + wData(&H24CBB + Woff * 2) * 256& Then
wData(&H24CBA + Woff * 2) = CByte(.L3 Mod 256)
wData(&H24CBB + Woff * 2) = CByte(Fix(.L3 / 256) And &HFF&)
FileChanged = True
End If
If .L5 <> wData(&H87179 + (wTTDPext - 1) * 108800 + Woff) Then FileChanged = True: wData(&H87179 + (wTTDPext - 1) * 108800 + Woff) = .L5
End With
End Sub
Public Function TTDXGetLandMap(Wx As Integer, Wy As Integer) As Integer
TTDXGetLandMap = (wData(&H77179 + (wTTDPext - 1) * 108800 + Wx + Wy * 256&) And &HF0&) / 16
End Function
'****************************************************************************************************
'**** City procedures ****
'****************************************************************************************************
Public Function CityData(vCityNo As Integer, vDataoff As Integer) As Byte
Dim Wa As Long
CityData = wData(&H264 + &H5E * vCityNo + vDataoff)
End Function
Public Function CityInfo(vCityNo As Integer) As TTDXCitInfo
Dim Wa As Integer
'
' Return information about a city
'
With CityInfo
.Number = vCityNo
.Offset = &H264 + &H5E * vCityNo
.X = CityData(vCityNo, 0)
.Y = CityData(vCityNo, 1)
.Population = CityData(vCityNo, 2) + CityData(vCityNo, 3) * 256&
.Name = Cities(vCityNo)
For Wa = 0 To 7
.CRate(Wa) = CityData(vCityNo, Wa * 2 + &H1E) + CityData(vCityNo, Wa * 2 + &H1F) * 256&
If .CRate(Wa) > 32768 Then .CRate(Wa) = .CRate(Wa) - 65536
If (CityData(vCityNo, &H2E) And 2 ^ Wa) > 0 Then .CRateE(Wa) = True Else .CRateE(Wa) = False
Next Wa
'
End With
End Function
Public Sub TTDXCityPut(wDta As TTDXCitInfo)
Dim wWork(&H5D) As Byte, Wa As Integer, Wb As Long
With wDta
For Wa = 0 To &H5D: wWork(Wa) = CityData(.Number, Wa): Next Wa
wWork(&H2E) = 0
For Wa = 0 To 7
Wb = .CRate(Wa)
If Wb < 0 Then Wb = Wb + 65536
wWork(&H1E + Wa * 2) = CByte(Wb Mod 256)
wWork(&H1F + Wa * 2) = CByte(Fix(Wb / 256))
If .CRateE(Wa) Then wWork(&H2E) = wWork(&H2E) Or 2 ^ Wa
Next Wa
For Wa = 0 To &H5D
If wWork(Wa) <> wData(&H264 + &H5E * .Number + Wa) Then
FileChanged = True: wData(&H264 + &H5E * .Number + Wa) = wWork(Wa)
'Debug.Print Hex(Wa)
End If
Next Wa
End With
End Sub
Public Function CityName(vCityNo As Integer) As String
Dim Wa As Long, Wb As Long
Wb = CityData(vCityNo, 4)
If CityData(vCityNo, 5) = &H7D Then
CityName = GetString(Wb)
ElseIf CityData(vCityNo, 5) = &H20 Then
If Wb = &HC1 Then
CityName = TTDXMakeC1CityName(CityData(vCityNo, 6), CityData(vCityNo, 7), CityData(vCityNo, 8), CityData(vCityNo, 9))
ElseIf Wb = &HC2 Then
CityName = TTDXMakeC2CityName(CityData(vCityNo, 6), CityData(vCityNo, 7), CityData(vCityNo, 8), CityData(vCityNo, 9))
End If
End If
'If CityName = "" Then: CityName = "<Unknown Name " + Right("00" + Hex(vCityNo), 2) + ">"
End Function
'****************************************************************************************************
'**** Industry procedures ****
'****************************************************************************************************
Public Function TTDXIndustryData(vIndNo As Integer, vDataoff As Integer) As Byte
Dim Wa As Long
TTDXIndustryData = wData(333670 + &H36 * vIndNo + vDataoff)
End Function
Public Function TTDXIndustryInfo(vIndNo As Integer) As TTDXIndInfo
Dim Wa As Integer
' 1 2 3
With TTDXIndustryInfo
.Number = vIndNo
.X = TTDXIndustryData(vIndNo, 0)
.Y = TTDXIndustryData(vIndNo, 1)
.W = TTDXIndustryData(vIndNo, 6)
.H = TTDXIndustryData(vIndNo, 7)
.HomeTown = CByte((((TTDXIndustryData(vIndNo, 2) + TTDXIndustryData(vIndNo, 3) * 256) - &H264) / &H5E) And &HFF)
.Prod(0) = TTDXIndustryData(vIndNo, 8)
.Prod(1) = TTDXIndustryData(vIndNo, 9)
.ProR(0) = TTDXIndustryData(vIndNo, &HE)
.ProR(1) = TTDXIndustryData(vIndNo, &HF)
.del(0) = TTDXIndustryData(vIndNo, &H10)
.del(1) = TTDXIndustryData(vIndNo, &H11)
.del(2) = TTDXIndustryData(vIndNo, &H12)
.Type = TTDXIndustryData(vIndNo, &H26)
.Name = IndustryTypes(.Type)
.Offset = 333670 + &H36 * vIndNo
End With
End Function
Public Sub TTDXIndustryPut(wDta As TTDXIndInfo)
Dim wWork(&H35) As Byte, Wa As Integer
With wDta
For Wa = 0 To &H35: wWork(Wa) = TTDXIndustryData(.Number, Wa): Next Wa
wWork(8) = .Prod(0): wWork(9) = .Prod(1)
wWork(&HE) = .ProR(0): wWork(&HF) = .ProR(1)
wWork(&H10) = .del(0): wWork(&H11) = .del(1): wWork(&H12) = .del(2)
wWork(2) = CByte(((.HomeTown * &H5E) + &H264) Mod 256)
wWork(3) = CByte(Fix(((.HomeTown * &H5E) + &H264) / 256))
For Wa = 0 To &H35
If wWork(Wa) <> wData(333670 + &H36 * .Number + Wa) Then
FileChanged = True: wData(333670 + &H36 * .Number + Wa) = wWork(Wa)
End If
Next Wa
End With
End Sub
'****************************************************************************************************
'**** Station procedures ****
'****************************************************************************************************
Public Function TTDXStationInfo(wNo As Integer) As TTDXStation
Dim sOff As Long, Wa As Integer, Wx As Byte, Wy As Byte
sOff = &H48CBA + wNo * &H8E&
With TTDXStationInfo
.BaseX = wData(sOff)
.BaseY = wData(sOff + 1)
'.AreaX1 = 255: .AreaX2 = 0: .AreaY1 = 255: .AreaY2 = 0
.Number = wNo
'
.Parts = wData(sOff + &H80)
.TruckStatus = wData(sOff + &H82)
.BusStatus = wData(sOff + &H83)
.BusX = wData(sOff + 6): .BusY = wData(sOff + 7)
.TruckX = wData(sOff + 8): .TruckY = wData(sOff + 9)
.RailX = wData(sOff + 10): .RailY = wData(sOff + 11)
.AirX = wData(sOff + 12): .AirY = wData(sOff + 13)
.DockX = wData(sOff + 14): .DockY = wData(sOff + 15)
.RailTracks = CByte(wData(sOff + 16) And 7)
.RailTrackLen = CByte((wData(sOff + 16) And 248) / 8)
If TTDXgetLandscape(CInt(Wx), CInt(Wy)).L5 And 1& Then: .RailDir = False: Else: .RailDir = True
.AirportType = wData(sOff + 81)
For Wa = 0 To 11
.Cargo(Wa) = CInt((wData(sOff + &H1C + Wa * 8) + wData(sOff + &H1D + Wa * 8) * 256&) And &HFFF)
.CAcc(Wa) = (wData(sOff + &H1D + Wa * 8) And &HF0) / 16
.Cgood2(Wa) = wData(sOff + &H1C + Wa * 8 + 2)
.CRate(Wa) = wData(sOff + &H1C + Wa * 8 + 3)
.CEnrout(Wa) = wData(sOff + &H1C + Wa * 8 + 4): .CEnroutOrg(Wa) = .CEnrout(Wa)
.Cgood5(Wa) = wData(sOff + &H1C + Wa * 8 + 5)
.Cgood6(Wa) = wData(sOff + &H1C + Wa * 8 + 6)
.Cgood7(Wa) = wData(sOff + &H1C + Wa * 8 + 7)
Next Wa
.Owner = wData(sOff + &H7F)
.HomeTown = CByte((((wData(sOff + 2) + wData(sOff + 3) * 256) - &H264) / &H5E) And &HFF)
.Name = Stations(.Number)
.Offset = sOff
End With
End Function
Public Sub TTDXStationPut(wDta As TTDXStation)
Dim wWork(&H8D) As Byte, Wa As Integer, sOff As Long
With wDta
sOff = &H48CBA + wDta.Number * &H8E&
'
' Copy Original Data
'
For Wa = 0 To &H8D: wWork(Wa) = wData(sOff + Wa): Next Wa
'
wWork(&H80) = .Parts
wWork(&H82) = .TruckStatus
wWork(&H83) = .BusStatus
wWork(6) = .BusX: wWork(7) = .BusY
wWork(8) = .TruckX: wWork(9) = .TruckY
'
For Wa = 0 To 11
If .CEnrout(Wa) = 255 Then
.Cargo(Wa) = 0: .Cgood2(Wa) = 0: .CRate(Wa) = &HAF
.Cgood5(Wa) = 0: .Cgood6(Wa) = 0: .Cgood7(Wa) = 255
End If
wWork(&H1C + Wa * 8) = CByte(.Cargo(Wa) Mod 256)
wWork(&H1D + Wa * 8) = CByte(Fix(.Cargo(Wa) / 256) + .CAcc(Wa) * 16)
wWork(&H1E + Wa * 8) = .Cgood2(Wa)
wWork(&H1F + Wa * 8) = .CRate(Wa)
wWork(&H20 + Wa * 8) = .CEnrout(Wa)
wWork(&H1C + Wa * 8 + 5) = .Cgood5(Wa)
wWork(&H1C + Wa * 8 + 6) = .Cgood6(Wa)
wWork(&H1C + Wa * 8 + 7) = .Cgood7(Wa)
Next Wa
'
' Check to see if Data has changed
'
For Wa = 0 To &H8D
If wWork(Wa) <> wData(sOff + Wa) Then
FileChanged = True: wData(sOff + Wa) = wWork(Wa)
End If
Next Wa
End With
End Sub
'****************************************************************************************************
'**** Vehicle procedures ****
'****************************************************************************************************
Public Function TTDXGetVeh(wNo As Long) As TTDXVehicle
Dim sOff As Long
sOff = &H547F2 + wNo * 128
With TTDXGetVeh
.Number = wNo
.Class = wData(sOff)
'If .Class = 19 Then
'MsgBox "aircraft"
'End If
.SubClass = wData(sOff + 1)
.SpeedMax = wData(sOff + &H18) + wData(sOff + &H19) * 256&
.Owner = wData(sOff + &H25)
.CargoT = wData(sOff + &H39)
.CargoMax = wData(sOff + &H3A) + wData(sOff + &H3B) * 256&
.CargoCur = wData(sOff + &H3C) + wData(sOff + &H3D) * 256&
.Age = wData(sOff + &H40) + wData(sOff + &H41) * 256&
.AgeMax = wData(sOff + &H42) + wData(sOff + &H43) * 256&
.Rel = wData(sOff + &H4F)
.RelDropRate = wData(sOff + &H50) + wData(sOff + &H51) * 256&
.Next = wData(sOff + &H5A) + wData(sOff + &H5B) * 256&
.Value = GetLong(sOff + &H5C)
.Offset = sOff
.Name = "Unknown"
Select Case .Class
Case &H10
Select Case .SubClass
Case 0
.Name = "Train " + Format(wData(sOff + &H45), "000")
End Select
Case &H11
.Name = "Road Vehicle " + Format(wData(sOff + &H45), "000")
Case &H12
.Name = "Ship " + Format(wData(sOff + &H45), "000")
Case &H13
Select Case .SubClass
Case 0 ' copter
.Name = "Aircraft " + Format(wData(sOff + &H45), "000")
Case 2 ' plane
.Name = "Aircraft " + Format(wData(sOff + &H45), "000")
Case 4
.Name = "Aircraft Second " + Format(wData(sOff + &H45), "000")
Case 6
.Name = "Chopper Rotor " + Format(wData(sOff + &H45), "000")
End Select
End Select
' Select Case .SubClass
' Case 0
' Select Case wData(sOff + &H60) + wData(sOff + &H61) * 256&
' Case &H8864&: .Name = "Train " + Format(wData(sOff + &H45), "000")
' Case &H902B&: .Name = "Road Vehicle " + Format(wData(sOff + &H45), "000")
' Case &H9830&: .Name = "Ship " + Format(wData(sOff + &H45), "000")
' Case &HA02F&: .Name = "Aircraft " + Format(wData(sOff + &H45), "000")
' End Select
' Case 2: .Name = "Car"
' Case 4: .Name = "Aircraft Second"
' Case 6: .Name = "Chopper Rotor"
' End Select
End With
End Function
Public Sub TTDXPutVeh(wDta As TTDXVehicle)
Dim wWork(&H7F) As Byte, Wa As Integer, sOff As Long
With wDta
sOff = &H547F2 + .Number * &H80
'
' Copy Original Data
'
For Wa = 0 To &H7F: wWork(Wa) = wData(sOff + Wa): Next Wa
'
' Store values
'
wWork(&H39) = .CargoT
wWork(&H3A) = CByte(.CargoMax Mod 256)
wWork(&H3B) = CByte(Fix(.CargoMax / 256))
wWork(&H40) = CByte(.Age Mod 256)
wWork(&H41) = CByte(Fix(.Age / 256))
wWork(&H42) = CByte(.AgeMax Mod 256)
wWork(&H43) = CByte(Fix(.AgeMax / 256))
wWork(&H4F) = CByte(.Rel)
wWork(&H50) = CByte(.RelDropRate Mod 256)
wWork(&H51) = CByte(Fix(.RelDropRate / 256))
PutLong wWork(), &H5C, .Value
'
' Check to see if Data has changed
'
For Wa = 0 To &H7F
If wWork(Wa) <> wData(sOff + Wa) Then
FileChanged = True: wData(sOff + Wa) = wWork(Wa)
End If
Next Wa
End With
End Sub
'****************************************************************************************************
'**** General & Player ****
'****************************************************************************************************
Public Function TTDXGetByte(wPos) As Byte
TTDXGetByte = wData(wPos)
End Function
Public Function TTDXGeneralInfo() As TTDXgeneral
Dim Wa As Integer
With TTDXGeneralInfo
.VehSize = wTTDPext
.ClimType = wClimate
Select Case wClimate
Case 0: .ClimName = "Temperate"
Case 1: .ClimName = "Sub-Arctic"
Case 2: .ClimName = "Sub-Tropic"
Case 3: .ClimName = "Toyland"
Case Else: .ClimName = "<Undefined " + Format(wClimate) + ">"
End Select
.CityNameSet = wData(&H5C80D + &H1A900 * .VehSize)
Select Case .CityNameSet
Case 0: .CityNames = "English"
Case 1: .CityNames = "French"
Case 2: .CityNames = "German"
Case 3: .CityNames = "American"
Case 4: .CityNames = "Latin-American"
Case 5: .CityNames = "Silly"
End Select
.GameName = ""
For Wa = 0 To 46
If wHeadData(Wa) < 32 Then Exit For
.GameName = .GameName + Chr(wHeadData(Wa))
Next Wa
End With
End Function
Public Function TTDXPlayerInfo(vPlayer As Integer) As TTDXplayer
Dim Wa As Integer
' 1 2 3 4 5
' 0 8 0 8 0 8 0 8 0 8 0 8 E
Const BytKnow As String = " xxxxxxxx "
With TTDXPlayerInfo
.Number = vPlayer
.Offset = Poff + vPlayer * 946