-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainTaipei.cpp
More file actions
1974 lines (1706 loc) · 69.2 KB
/
Copy pathMainTaipei.cpp
File metadata and controls
1974 lines (1706 loc) · 69.2 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 <vcl.h>
#include <mmsystem.h>
#include <algorithm>
#include <exception>
#include <registry.hpp>
#pragma hdrstop
#include "MainTaipei.h"
#include "SelGame.h"
#include "EditorCode.cpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfTaipei *fTaipei;
//Sizes at zoom factor 100
#define TILESIZE_100 36 // Horizontal pixel width of a tile
#define HALFTILESIZE_100 18 // Since a tile covers 2 by 2 units of game grid, an half value is sometime required
#define TILEXOFFSET_100 5 // Pixel horizontal offset for the high-angle isometric perspective
#define TILEYOFFSET_100 3 // Pixel vertical offset for the high-angle isometric perspective
//Sizes at zoom factor 125
#define TILESIZE_125 45 // Horizontal pixel width of a tile
#define HALFTILESIZE_125 22 // Since a tile covers 2 by 2 units of game grid, an half value is sometime required
#define TILEXOFFSET_125 6 // Pixel horizontal offset for the high-angle isometric perspective
#define TILEYOFFSET_125 4 // Pixel vertical offset for the high-angle isometric perspective
//Sizes at zoom factor 150
#define TILESIZE_150 54 // Horizontal pixel width of a tile
#define HALFTILESIZE_150 27 // Since a tile covers 2 by 2 units of game grid, an half value is sometime required
#define TILEXOFFSET_150 7 // Pixel horizontal offset for the high-angle isometric perspective
#define TILEYOFFSET_150 4 // Pixel vertical offset for the high-angle isometric perspective
//Sizes at zoom factor 200
#define TILESIZE_200 72 // Horizontal pixel width of a tile
#define HALFTILESIZE_200 36 // Since a tile covers 2 by 2 units of game grid, an half value is sometime required
#define TILEXOFFSET_200 10 // Pixel horizontal offset for the high-angle isometric perspective
#define TILEYOFFSET_200 6 // Pixel vertical offset for the high-angle isometric perspective
//Those don't scale (check Redefined in EditorCode.cpp)
#define MAXNUMBERLAYER 7 // Theres only 7 layers
#define GAMEWIDTH 31 // Horizontal width of the game grid
#define GAMEHEIGHT 17 // Vertical height of the game grid
#define YEAR "2026"
//---------------------------------------------------------------------------
//Constructor for a Tile
TTile::TTile(int pId, int pX, int pY, int pZ)
{
this->Id = pId; // Unique ID for each tiles, 0 to number of tiles
// used by the AutoPlay algo and the Hint functionality as well as Save/Load game functionality
this->Next = NULL;// Pointer to the next entry in the Linked List used to store tiles
// X, Y and Z coordinates in the game grid (not Canvas X/Y) for tiles draw and cursor interactions
// NOTE: a tile is a square with 2 units of width and height !!!
this->X = pX; // X starts on the left side and increases horizontaly toward the right
this->Y = pY; // Y starts on the upper portion and increases verticaly toward the lower portion
this->Z = pZ; // Z starts on the bottom layer and increases verticaly toward the top layer
// drawing is done win and isometric perspective with a "high-angle shot" with a lateral offset
//
// |\¯¯¯¯\ //
// \ \____\ //
// \|____| //
this->Type = -1; // Value used to match tiles of the same type
this->Graph = -1; // Image list index for graph used to draw tile
this->Step = -1; // Indicate the order tiles were removed: starts at 0 and increases by 1 for each pair of tiles removed
this->Hint = -1; // Indicate the order tiles were laid, starts at number of pairs and decreases by 1 to 0
// used by the AutoPlay algo and the Hint functionality
this->Debug = 0; // Debug indicator to toggle a tile visiable as a solid color: 1=blue 2=black
this->Selected = false; // Selected tiles are drawn with an inverted color graph
this->Visible = false; // Visible tiles are drawn with their corresponding Graph
this->WireFrame = false; // WireFrame tiles are drawn in ..well, wireframe :-)
}
//---------------------------------------------------------------------------
__fastcall TfTaipei::TfTaipei(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
//Called to close the application
void __fastcall TfTaipei::mExitClick(TObject *Sender)
{
this->Close();
}
//---------------------------------------------------------------------------
//Initialises form's default values and read command line call parameters
void __fastcall TfTaipei::FormCreate(TObject *Sender)
{
this->ilTiles->SetSize(36,36);
this->ilTiles->ResourceLoad(rtBitmap, "TILE_100_ALL", clWhite);
this->ZoomFactor = 100;
this->TILESIZE = TILESIZE_100;
this->HALFTILESIZE = HALFTILESIZE_100;
this->TILEXOFFSET = TILEXOFFSET_100;
this->TILEYOFFSET = TILEYOFFSET_100;
bool OpenResult;
TRegistry* Reg = new TRegistry(KEY_READ);
this->DoubleBuffered = true;
Application->Icon = this->Icon;
this->lMainTitle->Caption = "Oriental Game of Skill and Chance\nVersion 6.00\nClone by David Morrissette\n2020-2024";
this->lMainTitleShadow->Caption = this->lMainTitle->Caption;
this->iMainLogo->Picture->Bitmap->LoadFromResourceName((int)HInstance, "TAIPEI_LOGO");
this->iMainLogo->Left = (this->ClientWidth - this->iMainLogo->Width) / 2;
this->Mode = 1;
this->DebugDraw = false;
this->Shade = 4;
this->Radius = 0;
this->Peek = false;
this->EditLayer = 0;
this->AdvancedUserMode = 0;
this->MsgNo = 0;
this->TileList = NULL;
this->SelectedTile = NULL;
this->ClientWidth = 570;
this->ClientHeight = 310;
OutputDebugString("Taipei.exe\nParams: \n -d debug mode\n -m no messages\n");
OutputDebugString("Press T in main window for Dragon layout and WatchBuild option");
OutputDebugString(" Ctrl+T for Difficulty and Peak options");
OutputDebugString(" Shift+T for Save Game and Load Game functionalities");
OutputDebugString(" Shift+Alt+T for experimental custom layout editor");
Reg->RootKey = HKEY_CURRENT_USER;
OpenResult = Reg->OpenKey("Software\\tDavidM\\Taipei\\", true);
if(OpenResult) {
this->mMessages->Checked = Reg->ReadString("MessagesVisible") == "True";
String ZoomFact = Reg->ReadString("ZoomFactor");
if (Reg->ReadString("ZoomFactor")=="")
ZoomFact = "0";
switch ( StrToInt(ZoomFact) ) {
case 125:
this->m125p->Click();
break;
case 150:
this->m150p->Click();
break;
case 200:
this->m200p->Click();
break;
}
}
Reg->CloseKey();
Reg->Free();
for (int i=1;i<=ParamCount();i++) {
//DEBUG mode
if (LowerCase(ParamStr(i)) == "-d") {
Beep();
this->DebugDraw = true;
this->Mode = 0;
lDebug->Visible = true;
//No Messages
} else if (LowerCase(ParamStr(i)) == "-m") {
this->mMessages->Checked = false;
}
}
}
//---------------------------------------------------------------------------
//Toggles contextual help/reminder messages about gameplay
void __fastcall TfTaipei::mMessagesClick(TObject *Sender)
{
this->mMessages->Checked = !this->mMessages->Checked;
//If Ctrl+T was pressed
if (this->AdvancedUserMode > 0) {
bool OpenResult;
TRegistry* Reg = new TRegistry(KEY_WRITE);
Reg->RootKey = HKEY_CURRENT_USER;
OpenResult = Reg->OpenKey("Software\\tDavidM\\Taipei\\", true);
if(OpenResult)
Reg->WriteString("MessagesVisible", this->mMessages->Checked ? "True" : "False");
Reg->CloseKey();
Reg->Free();
}
}
//---------------------------------------------------------------------------
//Shows a system MessageBox with infos about Taipei
void __fastcall TfTaipei::mAboutClick(TObject *Sender)
{
String DialogText = "Taipei !\n\nOriginal by Dave Norris\n"
"Clone by David Morrissette\n\n2020-2024\n\n\nPress T in main window for more options";
String DialogCap = "About " + this->Caption;
if (this->AdvancedUserMode > 0) {
DialogText = DialogText + "\n or Ctrl+T";
if (this->AdvancedUserMode > 1)
DialogText = DialogText + "\n or Shift+T";
DialogText = DialogText + "\n these options were available in lesser known versions of Taipei.";
}
Application->MessageBox(DialogText.w_str(), DialogCap.w_str(), MB_OK);
}
//--------------------------------------------------------------------------
//Shows a system MessageBox with help to play the game
void __fastcall TfTaipei::mHowtoPlayClick(TObject *Sender)
{
String DialogText = "Taipei is a modern, solitaire version of the ancient oriental game, Mah-Jongg.\n\n"
"Playing Taipei is simple.\n"
"The object of Taipei is to remove all of the tiles from the board.\n\n"
"Tiles are removed from the board in matching pairs.\n"
"Tiles can only be removed if they are \"free\".\n"
"A tile is \"free\" if:\n"
" 1) it has no tiles on top of it, and\n"
" 2) you can \"slide\" the tile out to the right or left.";
Application->MessageBox(DialogText.w_str(), L"What is Taipei?", MB_OK);
}
//---------------------------------------------------------------------------
//Shows a system MessageBox with strategies to play the game
void __fastcall TfTaipei::mStrategyClick(TObject *Sender)
{
String DialogText = "Winning a game is much like solving an intricate puzzle.\n"
"One false move at the beginning could ruin your chances at victory.\n"
"And, while every game has a solution, finding it often takes hours of diligent play.\n\n"
"Here are some hints if you are having difficulty solving the Taipei puzzles:\n"
"1. Remove the end tiles as soon as you can, "
"especially those tiles that block more than one tile.\n"
"2. If all four tiles in a matching set are free, you can remove all four safely.\n"
"3. Work from the outside in.";
Application->MessageBox(DialogText.w_str(), L"Strategy", MB_OK); // MB_HELP
}
//---------------------------------------------------------------------------
//Change zoom to 100%
void __fastcall TfTaipei::m100pClick(TObject *Sender)
{
this->m100p->Checked = true;
this->m125p->Checked = false;
this->m150p->Checked = false;
this->m200p->Checked = false;
this->ilTiles->Clear();
this->ilTiles->SetSize(36,36);
if (this->mColor->Checked)
this->ilTiles->ResourceLoad(rtBitmap, "TILE_100_ALL", clWhite);
else
this->ilTiles->ResourceLoad(rtBitmap, "TILE_100_ALL_B", clWhite);
this->ZoomFactor = 100;
this->ClientWidth = 570;
this->ClientHeight = 310;
this->TILESIZE = TILESIZE_100;
this->HALFTILESIZE = HALFTILESIZE_100;
this->TILEXOFFSET = TILEXOFFSET_100;
this->TILEYOFFSET = TILEYOFFSET_100;
if (this->iMainLogo->Visible) {
this->iMainLogo->Left = (this->ClientWidth - this->iMainLogo->Width) / 2;
this->iMainLogo->Top = 60;
}
this->Invalidate();
bool OpenResult;
TRegistry* Reg = new TRegistry(KEY_WRITE);
Reg->RootKey = HKEY_CURRENT_USER;
OpenResult = Reg->OpenKey("Software\\tDavidM\\Taipei\\", true);
if(OpenResult)
Reg->WriteString("ZoomFactor", this->ZoomFactor);
Reg->CloseKey();
Reg->Free();
}
//---------------------------------------------------------------------------
//Change zoom to 125%
void __fastcall TfTaipei::m125pClick(TObject *Sender)
{
Graphics::TBitmap *TileGraphSrc, *TileGraphDest;
this->m100p->Checked = false;
this->m125p->Checked = true;
this->m150p->Checked = false;
this->m200p->Checked = false;
this->ilTiles->Clear();
this->ilTiles->SetSize(45,45);
TileGraphSrc = new Graphics::TBitmap;
TileGraphDest = new Graphics::TBitmap;
if (this->mColor->Checked)
TileGraphSrc->LoadFromResourceName((int)HInstance, "TILE_100_ALL");
else
TileGraphSrc->LoadFromResourceName((int)HInstance, "TILE_100_ALL_B");
ResizeImage(TileGraphSrc, TileGraphDest, 1.25);
this->ilTiles->Add(TileGraphDest,TileGraphDest);
TileGraphSrc->Free();
TileGraphDest->Free();
this->ZoomFactor = 125;
this->ClientWidth = 712;
this->ClientHeight = 387;
this->TILESIZE = TILESIZE_125;
this->HALFTILESIZE = HALFTILESIZE_125;
this->TILEXOFFSET = TILEXOFFSET_125;
this->TILEYOFFSET = TILEYOFFSET_125;
if (this->iMainLogo->Visible) {
this->iMainLogo->Left = (this->ClientWidth - this->iMainLogo->Width) / 2;
this->iMainLogo->Top = 90;
}
this->Invalidate();
bool OpenResult;
TRegistry* Reg = new TRegistry(KEY_WRITE);
Reg->RootKey = HKEY_CURRENT_USER;
OpenResult = Reg->OpenKey("Software\\tDavidM\\Taipei\\", true);
if(OpenResult)
Reg->WriteString("ZoomFactor", this->ZoomFactor);
Reg->CloseKey();
Reg->Free();
}
//---------------------------------------------------------------------------
//Change zoom to 150%
void __fastcall TfTaipei::m150pClick(TObject *Sender)
{
Graphics::TBitmap *TileGraphSrc, *TileGraphDest;
this->m100p->Checked = false;
this->m125p->Checked = false;
this->m150p->Checked = true;
this->m200p->Checked = false;
this->ilTiles->Clear();
this->ilTiles->SetSize(54,54);
TileGraphSrc = new Graphics::TBitmap;
TileGraphDest = new Graphics::TBitmap;
if (this->mColor->Checked)
TileGraphSrc->LoadFromResourceName((int)HInstance, "TILE_100_ALL");
else
TileGraphSrc->LoadFromResourceName((int)HInstance, "TILE_100_ALL_B");
ResizeImage(TileGraphSrc, TileGraphDest, 1.5);
this->ilTiles->Add(TileGraphDest,TileGraphDest);
TileGraphSrc->Free();
TileGraphDest->Free();
this->ZoomFactor = 150;
this->ClientWidth = 855;
this->ClientHeight = 465;
this->TILESIZE = TILESIZE_150;
this->HALFTILESIZE = HALFTILESIZE_150;
this->TILEXOFFSET = TILEXOFFSET_150;
this->TILEYOFFSET = TILEYOFFSET_150;
if (this->iMainLogo->Visible) {
this->iMainLogo->Left = (this->ClientWidth - this->iMainLogo->Width) / 2;
this->iMainLogo->Top = 120;
}
this->Invalidate();
bool OpenResult;
TRegistry* Reg = new TRegistry(KEY_WRITE);
Reg->RootKey = HKEY_CURRENT_USER;
OpenResult = Reg->OpenKey("Software\\tDavidM\\Taipei\\", true);
if(OpenResult)
Reg->WriteString("ZoomFactor", this->ZoomFactor);
Reg->CloseKey();
Reg->Free();
}
//---------------------------------------------------------------------------
//Change zoom to 200%
void __fastcall TfTaipei::m200pClick(TObject *Sender)
{
Graphics::TBitmap *TileGraphSrc, *TileGraphDest;
this->m100p->Checked = false;
this->m125p->Checked = false;
this->m150p->Checked = false;
this->m200p->Checked = true;
this->ilTiles->Clear();
this->ilTiles->SetSize(72,72);
TileGraphSrc = new Graphics::TBitmap;
TileGraphDest = new Graphics::TBitmap;
if (this->mColor->Checked)
TileGraphSrc->LoadFromResourceName((int)HInstance, "TILE_100_ALL");
else
TileGraphSrc->LoadFromResourceName((int)HInstance, "TILE_100_ALL_B");
ResizeImage(TileGraphSrc, TileGraphDest, 2);
this->ilTiles->Add(TileGraphDest,TileGraphDest);
this->ZoomFactor = 200;
this->ClientWidth = 1140;
this->ClientHeight = 620;
this->TILESIZE = TILESIZE_200;
this->HALFTILESIZE = HALFTILESIZE_200;
this->TILEXOFFSET = TILEXOFFSET_200;
this->TILEYOFFSET = TILEYOFFSET_200;
TileGraphSrc->Free();
TileGraphDest->Free();
if (this->iMainLogo->Visible) {
this->iMainLogo->Left = (this->ClientWidth - this->iMainLogo->Width) / 2;
this->iMainLogo->Top = 180;
}
this->Invalidate();
bool OpenResult;
TRegistry* Reg = new TRegistry(KEY_WRITE);
Reg->RootKey = HKEY_CURRENT_USER;
OpenResult = Reg->OpenKey("Software\\tDavidM\\Taipei\\", true);
if(OpenResult)
Reg->WriteString("ZoomFactor", this->ZoomFactor);
Reg->CloseKey();
Reg->Free();
}
//---------------------------------------------------------------------------
//Toggles between color and black&white tile graphs
void __fastcall TfTaipei::mColorClick(TObject *Sender)
{
this->mColor->Checked = !this->mColor->Checked;
if (this->m100p->Checked)
this->m100p->Click();
if (this->m125p->Checked)
this->m125p->Click();
if (this->m150p->Checked)
this->m150p->Click();
if (this->m200p->Checked)
this->m200p->Click();
this->Repaint();
}
//---------------------------------------------------------------------------
//Toggles tile heap build animation
void __fastcall TfTaipei::mWatchBuildsClick(TObject *Sender)
{
this->mWatchBuilds->Checked = !this->mWatchBuilds->Checked;
}
//---------------------------------------------------------------------------
//Lowers the tile's eges brightness
void __fastcall TfTaipei::mDarkenClick(TObject *Sender)
{
if (this->Shade > 1)
this->Shade--;
this->Repaint();
}
//---------------------------------------------------------------------------
//Increases the tile's eges brightness
void __fastcall TfTaipei::mLightenClick(TObject *Sender)
{
if (this->Shade < 8)
this->Shade++;
this->Repaint();
}
//---------------------------------------------------------------------------
//Toggles the Peek Mode used to temporarily remove any tile to have a look under
void __fastcall TfTaipei::mPeekClick(TObject *Sender)
{
this->Peek = !this->Peek;
this->mPeek->Checked = !this->mPeek->Checked;
}
//---------------------------------------------------------------------------
//Activates Beginner Mode where a pair of tiles will preferencialy be located within a limited distance when possible
void __fastcall TfTaipei::mBeginnerClick(TObject *Sender)
{
this->Radius = 3;
this->mExpert->Checked = false;
this->mBeginner->Checked = true;
}
//---------------------------------------------------------------------------
//Activates Expert Mode where a pair of tiles have no bias on their relative random location
void __fastcall TfTaipei::mExpertClick(TObject *Sender)
{
this->Radius = 0;
this->mExpert->Checked = true;
this->mBeginner->Checked = false;
}
//---------------------------------------------------------------------------
//Called by every entries in the Layour menu, sets the game Mode using the caller's Tag
void __fastcall TfTaipei::mLayoutMenuClick(TObject *Sender)
{
this->mStandard->Checked = false;
this->mBridge->Checked = false;
this->mCastle->Checked = false;
this->mCube->Checked = false;
this->mGlyph->Checked = false;
this->mPyramid->Checked = false;
this->mSpiral->Checked = false;
this->mDragon->Checked = false;
this->mCustom->Checked = false;
TMenuItem *Current = (TMenuItem *)Sender;
Current->Checked = true;
this->Mode = Current->Tag;
this->mNewClick(NULL);
}
//---------------------------------------------------------------------------
//Resets the current game back to start
void __fastcall TfTaipei::mStartOverClick(TObject *Sender)
{
TTile* CurrentTile;
if (this->StepBack > 0) {
this->tAutoPlay->Enabled = false;
this->mAutoPlay->Checked = false;
CurrentTile = this->TileList;
while(CurrentTile != NULL) {
if (CurrentTile->Step >= 0) {
CurrentTile->Step = -1;
CurrentTile->Visible = true;
}
CurrentTile = CurrentTile->Next;
}
this->StepBack = 0;
this->HintLoopMain = NULL;
this->GamedDone = 0;
this->Repaint();
}
}
//---------------------------------------------------------------------------
//Revers the last move, can be called sequentially to cancel all moves back to start, cannot be undo
void __fastcall TfTaipei::mBackupClick(TObject *Sender)
{
TTile* CurrentTile;
if (this->StepBack > 0) {
this->tAutoPlay->Enabled = false;
this->mAutoPlay->Checked = false;
this->StepBack--;
CurrentTile = this->TileList;
while(CurrentTile != NULL) {
if (CurrentTile->Step == this->StepBack) {
CurrentTile->Step = -1;
CurrentTile->Visible = true;
if (this->GamedDone > 0)
this->GamedDone--;
}
CurrentTile = CurrentTile->Next;
}
this->HintLoopMain = NULL;
this->Repaint();
}
}
//---------------------------------------------------------------------------
//Calls to Initialize a new game (by calling InitGame() ) with a random seed
void __fastcall TfTaipei::mNewClick(TObject *Sender)
{
int GameNo;
Randomize();
GameNo = Random(32767);
this->InitGame(GameNo);
}
//---------------------------------------------------------------------------
//Calls to Initialize a new game (by calling InitGame() ) with a seed provided by the user
void __fastcall TfTaipei::mSelectClick(TObject *Sender)
{
int GameNo;
TfSelectGame *fSelectGame = new TfSelectGame( this );
if (fSelectGame->ShowModal() == mrOk) {
try {
GameNo = StrToInt(fSelectGame->eGameNo->Text);
} catch(Exception& e) {
GameNo = 0;
}
//The original had a stange "bug" replicated here,
// I belive the seed was stored in a short unsigned int while the prompt used a short signed int
// out of bounds values where set to zero instead of wrapping around
if (GameNo > 32767)
GameNo = 0;
if (GameNo < -32767)
GameNo = 0;
if (GameNo < 0)
GameNo = (65536 + GameNo);
this->InitGame(GameNo);
}
delete fSelectGame;
}
//---------------------------------------------------------------------------
//Initializes a new game using a seed given in parameter and the currently selected game mode (layout)
void TfTaipei::InitGame(int pGameNo)
{
this->tAutoPlay->Enabled = false;
this->mAutoPlay->Checked = false;
this->iMainLogo->Visible = false;
this->lMainTitleShadow->Visible = false;
this->lMainTitle->Visible = false;
this->StepBack = 0;
this->GamedDone = 0;
this->HintLoopMain = NULL;
this->HintLoopSecond = NULL;
this->SelectedTile = NULL;
this->GameNumber = pGameNo;
this->Caption = "Taipei Game #" + IntToStr(pGameNo);
this->BuildStructure(this->Mode);
this->FillStructure(pGameNo);
this->Repaint();
}
//---------------------------------------------------------------------------
//Get next available tile for hint
TTile* TfTaipei::GetNextHintTile(TTile* pStartingTile, int pType)
{
bool ItsFound = false;
while(pStartingTile != NULL && !ItsFound) {
if (pStartingTile->Visible &&
(pType == -1 || pStartingTile->Type == pType) &&
this->IsTileFree(pStartingTile))
ItsFound = true;
else
pStartingTile = pStartingTile->Next;
}
return pStartingTile;
}
//---------------------------------------------------------------------------
//suggests a pair of matching tiles from the available tiles at the current game state, can be called sequentially to show all matchs
void __fastcall TfTaipei::mHintClick(TObject *Sender)
{
TTile* StartingTile = NULL;
TTile* FollowingTile = NULL;
bool TryNext;
int LoopCmp = 0;
this->tAutoPlay->Enabled = false;
this->mAutoPlay->Checked = false;
//Cancel any selection
if (this->SelectedTile != NULL) {
this->SelectedTile->Selected = false;
this->SelectedTile = NULL;
}
//If HintLoopMain not yet initialized (at the beginning of the game and after every step)
if (this->HintLoopMain == NULL) {
//Find the first available tile
this->HintLoopMain = GetNextHintTile(this->TileList);
if (this->HintLoopMain != NULL)
this->HintLoopSecond = GetNextHintTile(this->HintLoopMain->Next, this->HintLoopMain->Type);
}
StartingTile = this->HintLoopMain;
FollowingTile = this->HintLoopSecond;
//Just a quick test in case
if (StartingTile != NULL) {
do {
//FailSafe counter
LoopCmp++;
//If match exists
if (FollowingTile != NULL) {
//Show hint
StartingTile->Selected = true;
FollowingTile->Selected = true;
this->Repaint();
Sleep(100);
StartingTile->Selected = false;
FollowingTile->Selected = false;
this->Repaint();
//Select the next secondary matching available tile of the same type
FollowingTile = GetNextHintTile(FollowingTile->Next, StartingTile->Type);
TryNext = false;
} else {
//Select the next available tile and the next matching available tile of the same type
StartingTile = GetNextHintTile(StartingTile->Next);
TryNext = true;
//If end of main loop is reach, skip one round
if (StartingTile == NULL)
TryNext = false;
else
FollowingTile = GetNextHintTile(StartingTile->Next, StartingTile->Type);
}
} while (TryNext && LoopCmp <= 72);
this->HintLoopMain = StartingTile;
this->HintLoopSecond = FollowingTile;
}
}
//---------------------------------------------------------------------------
//Toggles the AutoPlay mode where the games plays a move every few seconds (see tAutoPlayTimer)
void __fastcall TfTaipei::mAutoPlayClick(TObject *Sender)
{
this->mAutoPlay->Checked = !this->mAutoPlay->Checked;
this->tAutoPlay->Enabled = this->mAutoPlay->Checked;
if (this->mAutoPlay->Checked) {
Sleep(200);
this->tAutoPlayTimer(Sender);
}
}
//---------------------------------------------------------------------------
//Plays automaticly the best move from a given game state
void __fastcall TfTaipei::tAutoPlayTimer(TObject *Sender)
{
TTile* CurrentTile = this->TileList;
TTile* SelectTile = NULL;
int MinHint = 72;
int FloorHint = -1;
int MaxLoop = 72;
if (this->SelectedTile != NULL) {
this->SelectedTile->Selected = false;
this->SelectedTile = NULL;
}
//May take multiple tries
do {
//Find the lowest Hint value from all free and visible tiles and select it
while(CurrentTile != NULL) {
if (CurrentTile->Visible && CurrentTile->Hint <= MinHint && CurrentTile->Hint > FloorHint &&
this->IsTileFree(CurrentTile)) {
MinHint = CurrentTile->Hint;
this->SelectedTile = CurrentTile;
}
CurrentTile = CurrentTile->Next;
}
if (this->SelectedTile != NULL)
this->SelectedTile->Selected = true;
//Select the matching tile with lowest Hint value if possible
CurrentTile = this->TileList;
while(CurrentTile != NULL && SelectTile == NULL) {
if (CurrentTile->Visible && CurrentTile->Hint == MinHint &&
CurrentTile->Id != this->SelectedTile->Id && this->IsTileFree(CurrentTile)) {
SelectTile = CurrentTile;
SelectTile->Selected = true;
}
CurrentTile = CurrentTile->Next;
}
//Select the other tile available of the same type if possible
if (SelectTile == NULL) {
CurrentTile = this->TileList;
while(CurrentTile != NULL && SelectTile == NULL) {
if (CurrentTile->Visible && CurrentTile->Hint != MinHint &&
CurrentTile->Type == this->SelectedTile->Type && this->IsTileFree(CurrentTile)) {
SelectTile = CurrentTile;
SelectTile->Selected = true;
}
CurrentTile = CurrentTile->Next;
}
}
//If no match found, try again with the the next "lowest" that is greater than this one
if (SelectTile == NULL) {
FloorHint = MinHint;
MinHint = 72;
MaxLoop--;
this->SelectedTile->Selected = false;
this->SelectedTile = NULL;
CurrentTile = this->TileList;
}
} while(MaxLoop > 0 && SelectTile == NULL) ;
if (SelectTile != NULL) {
this->Repaint();
Sleep(100);
this->HideTileStep(SelectTile, true);
} else {
//AutoPlay is stuck and cannot finish the game as it was
if (this->SelectedTile != NULL) {
this->SelectedTile->Selected = false;
this->SelectedTile = NULL;
this->tAutoPlay->Enabled = false;
this->mAutoPlay->Checked = false;
PlaySound("SystemAsterisk", 0, SND_ALIAS || SND_ASYNC);
}
}
}
//---------------------------------------------------------------------------
//Senses keystrokes used as shortcuts
void __fastcall TfTaipei::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
//If not in Edit mode
if (this->EditLayer == 0) {
if(Key == 0x8 /*BackSpace*/)
this->mBackup->Click();
if (Key == 0x48 /*VK_KEY_H*/)
this->mHint->Click();
if (Key == 0x54 /*VK_KEY_T*/) {
this->mDragon->Visible = true;
this->mZoom->Visible = true;
this->mWatchBuilds->Visible = true;
this->AdvancedUserMode = this->AdvancedUserMode | 1;
if (Shift.Contains(ssCtrl)) {
this->mLighten->Visible = true;
this->mDarken->Visible = true;
this->mDifficulty->Visible = true;
this->mPeek->Visible = true;
this->AdvancedUserMode = this->AdvancedUserMode | 2;
}
if (Shift.Contains(ssShift)) {
this->mFile->Visible = true;
this->AdvancedUserMode = this->AdvancedUserMode | 4;
}
if (Shift.Contains(ssAlt)) {
this->N5->Visible = true;
this->mCreate->Visible = true;
this->mPlay->Visible = true;
this->AdvancedUserMode = this->AdvancedUserMode | 8;
}
}
}
}
//---------------------------------------------------------------------------
//Senses cursor mouvements in the game area, primarily cursor over tile events
void __fastcall TfTaipei::FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
TTile* CurrentTile;
TPoint Pos;
Pos.x = X;
Pos.y = Y;
CurrentTile = this->GetTile(Pos);
if (CurrentTile != NULL) {
this->lDebug->Caption = IntToStr(CurrentTile->Id) + ":" + IntToStr(CurrentTile->Hint);
if (this->Peek && CurrentTile->Z > 0 ) {
Screen->Cursor = crHelp; //crHandPoint; crDrag;
} else {
if (this->IsTileFree(CurrentTile))
Screen->Cursor = crCross;
else
Screen->Cursor = crDefault;
}
} else
Screen->Cursor = crDefault;
}
//---------------------------------------------------------------------------
//Senses cursor clicks in the game area, primarily tile selection events
void __fastcall TfTaipei::FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
TTile* CurrentTile;
TPoint Pos;
int TileCmp = 0;
bool Found = false;
Pos.x = X;
Pos.y = Y;
CurrentTile = this->GetTile(Pos);
if (CurrentTile != NULL ) {
this->tAutoPlay->Enabled = false;
this->mAutoPlay->Checked = false;
//Peek Mode
if (this->Peek) {
//Tile clicked on toggles visibility for 750ms
CurrentTile->Visible = false;
this->Repaint();
this->Peek = false;
this->mPeek->Checked = false;
Sleep(750);
CurrentTile->Visible = true;
this->Repaint();
} else { //Normal Mode
//Tile clicked on get selected or removed
if (!this->IsTileFree(CurrentTile)) {
if (this->mMessages->Checked)
Application->MessageBox(L"Tiles isn't free", L"Taipei", MB_OK | MB_ICONINFORMATION);
} else {
//Select 1 tile
if (this->SelectedTile == NULL) {
CurrentTile->Selected = true;
this->SelectedTile = CurrentTile;
this->Repaint();
} else {
//Same tile = Deselect
if(CurrentTile == this->SelectedTile) {
this->SelectedTile->Selected = false;
this->SelectedTile = NULL;
this->Repaint();
} else {
//Match tile with selection
if (CurrentTile->Type == this->SelectedTile->Type){
this->HideTileStep(CurrentTile, false);
} else {
CurrentTile->Selected = true;
this->Repaint();
Sleep(75);
CurrentTile->Selected = false;
this->Repaint();
if (this->mMessages->Checked)
Application->MessageBox(L"Tiles don't match", L"Taipei", MB_OK | MB_ICONINFORMATION);
//There's a strange rare bug where the SelectedTile is no longer visible (seen once)
// or not free yet (uncertain) and that jams the game logics, the player can't recover
if (!this->SelectedTile->Visible || !this->IsTileFree(this->SelectedTile)) {
//So when this situation is detected, simply deselect the invalid selection
this->SelectedTile->Selected = false;
this->SelectedTile = NULL;
this->Repaint();
//Testers did not repport using Autoplay, just regular click when the bug occurs
}
}
}
}
}
}
}
}
//---------------------------------------------------------------------------
//Calls tiles drawing procedures
void __fastcall TfTaipei::FormPaint(TObject *Sender)
{
this->DrawAllTiles();
}
//---------------------------------------------------------------------------
//Called when a tile is selected, if valid, it will select a tile or match with a selected tile and test if game is done
void TfTaipei::HideTileStep(TTile* pTile, bool pAutoPlay)
{
TTile* CurrentTile;
TTile* LookupTile;
bool Found = false;
pTile->Visible = false;
pTile->Selected = false;
pTile->Step = this->StepBack;
this->SelectedTile->Visible = false;
this->SelectedTile->Selected = false;
this->SelectedTile->Step = this->StepBack;