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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
|
/***************************************************************************
* kgrfigure.cpp - description *
* ------------------- *
* Copyright (C) 2003 by Ian Wadham and Marco Krüger *
* email : See menu "Help, About KGoldrunner" *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "kgrconsts.h"
#include "kgrobject.h"
#include "kgrcanvas.h"
#include "kgrfigure.h"
#include <stdio.h>
KGrFigure :: KGrFigure (int px, int py)
{
x = mem_x = px;
y = mem_y = py;
relx = mem_relx = 0;
rely = mem_rely = 0;
absx = px*16;
absy = py*16;
nuggets = 0;
status = STANDING;
walkTimer = new TQTimer (this);
fallTimer = new TQTimer (this);
}
// Initialise the global settings flags.
bool KGrFigure::variableTiming = TRUE;
bool KGrFigure::alwaysCollectNugget = TRUE;
bool KGrFigure::runThruHole = TRUE;
bool KGrFigure::reappearAtTop = TRUE;
SearchStrategy KGrFigure::searchStrategy = LOW;
int KGrFigure::herox = 0;
int KGrFigure::heroy = 0;
// Initialise the global game-speed factors.
int KGrFigure::speed = NSPEED;
int KGrBrick::speed = NSPEED;
// Initialise constants for fixed (KGoldrunner) and variable (Traditional)
// timing. Each row contains timings for hero walk and fall, enemy walk and
// fall, enemy captured in hole and dug brick.
Timing KGrFigure::fixedTiming = {45, 50, 55, 100, 500, 40}; // KGr original.
Timing KGrFigure::varTiming[6] = { // Traditional.
{40, 58, 78, 88, 170, 23}, // No enemies.
{50, 68, 78, 88, 170, 32}, // 1 enemy.
{57, 67, 114, 128, 270, 37}, // 2 enemies.
{60, 70, 134, 136, 330, 40}, // 3 enemies.
{63, 76, 165, 150, 400, 46}, // 4 enemies.
{70, 80, 189, 165, 460, 51} // >4 enemies.
};
int KGrBrick::HOLETIME = 0;
int KGrFigure::getx()
{
return absx;
}
int KGrFigure::gety()
{
return absy;
}
Status KGrFigure::getStatus()
{
return status;
}
void KGrFigure::init(int a,int b)
{
walkTimer->stop();
fallTimer->stop();
x = mem_x = a;
y = mem_y = b;
relx = mem_relx = 0;
rely = mem_rely = 0;
nuggets = 0;
status = STANDING;
}
void KGrFigure:: setNuggets(int n)
{
nuggets = n;
}
bool KGrFigure::canWalkRight()
{
return (((*playfield)[x+1][y]->whatIam() != BRICK) &&
((*playfield)[x+1][y]->whatIam() != BETON) &&
((*playfield)[x+1][y]->whatIam() != FBRICK));
}
bool KGrFigure::canWalkLeft()
{
return (((*playfield)[x-1][y]->whatIam() != BRICK) &&
((*playfield)[x-1][y]->whatIam() != BETON) &&
((*playfield)[x-1][y]->whatIam() != FBRICK));
}
bool KGrFigure::canWalkUp()
{
return (((*playfield)[x][y-1]->whatIam() != BRICK) &&
((*playfield)[x][y-1]->whatIam() != BETON) &&
((*playfield)[x][y-1]->whatIam() != FBRICK) &&
((*playfield)[x][y]->whatIam() == LADDER));
}
bool KGrFigure::canWalkDown()
{
return (((*playfield)[x][y+1]->whatIam() != BRICK) &&
((*playfield)[x][y+1]->whatIam() != BETON) &&
// v0.3 FIX - Let figure step down into FBRICK from a ladder.
// ((*playfield)[x][y+1]->whatIam() != FBRICK)&&
(((*playfield)[x][y+1]->whatIam() == LADDER)||
((*playfield)[x][y]->whatIam() == LADDER)));
}
bool KGrFigure::canStand()
{
return (((*playfield)[x][y+1]->whatIam() == BRICK) ||
((*playfield)[x][y+1]->whatIam() == BETON) ||
((*playfield)[x][y+1]->whatIam() == USEDHOLE)||
((*playfield)[x][y+1]->whatIam() == LADDER)||
((*playfield)[x][y]->whatIam() == LADDER)||
standOnEnemy());
}
bool KGrFigure::hangAtPole()
{
return ((*playfield)[x][y]->whatIam() == POLE);
}
void KGrFigure::walkUp(int WALKDELAY)
{
actualPixmap = (actualPixmap == CLIMB1) ? CLIMB2 : CLIMB1;
if (walkCounter++ < 4) {
// Not end of 4-step cycle: move one step up, if possible.
if (canWalkUp()) {
rely -= STEP;
absy -= STEP;
}
walkTimer->start ((WALKDELAY * NSPEED) / speed, TRUE);
}
else {
// End of 4-step cycle: move up to next cell, if possible.
if (canWalkUp()) {
y--;
}
// Always reset position, in case we are stuck partly into a brick.
rely = 0;
absy = y*16;
// Wait for caller to set next direction.
status = STANDING;
}
}
void KGrFigure::walkDown(int WALKDELAY, int FALLDELAY)
{
if (hangAtPole() || (! canStand())) {
// On bar or no firm ground underneath: so must fall.
initFall (FALL2, FALLDELAY);
}
else {
actualPixmap = (actualPixmap == CLIMB1) ? CLIMB2 : CLIMB1;
if (walkCounter++ < 4) {
// Not end of 4-step cycle: move one step down, if possible.
if (canWalkDown()) {
rely += STEP;
absy += STEP;
}
walkTimer->start ((WALKDELAY * NSPEED) / speed, TRUE);
}
else {
// End of 4-step cycle: move down to next cell, if possible.
if (canWalkDown()) {
y++;
}
// Always reset position, in case we are stuck partly into a brick.
rely = 0;
absy = y*16;
// Must be able to halt at a pole when going down.
if (! (canStand() || hangAtPole()))
initFall(FALL2, FALLDELAY); // kein Halt....urgs
else
// Wait for caller to set next direction.
status = STANDING;
}
}
}
void KGrFigure::walkLeft (int WALKDELAY, int FALLDELAY)
{
// If counter != 0, the figure is walking, otherwise he is turning around.
if (walkCounter++ != 0) {
// Change to the next pixmap in the animation.
if ((++actualPixmap%4) != 0) {
// Not end of 4-pixmap cycle: move one step left, if possible.
if (canWalkLeft()) {
relx -= STEP;
absx -=STEP;
}
walkTimer->start ((WALKDELAY * NSPEED) / speed, TRUE);
}
else {
// End of 4-pixmap cycle: start again, in next cell if possible.
actualPixmap -= 4;
if (canWalkLeft()) {
x--;
}
// Always reset position, in case we are stuck partly into a brick.
relx = 0;
absx = x*16;
// If cannot stand or hang, start fall, else await next assignment.
if (! (canStand() || hangAtPole()))
initFall (FALL1, FALLDELAY);
else
status = STANDING; // Caller should set next direction.
}
}
else {
status = STANDING; // The figure is turning around.
}
}
void KGrFigure::walkRight(int WALKDELAY, int FALLDELAY)
{
if (walkCounter++) { // wenn 0, soll sich Figur nur umdrehen
if (++actualPixmap % 4) { // wenn true, dann ist kein vollständiger Schritt gemacht
if (canWalkRight()) {
relx += STEP;
absx += STEP; // nur vorwärts gehen, wenn es auch möglich ist
}
walkTimer->start ((WALKDELAY * NSPEED) / speed, TRUE);
}
else {
actualPixmap -= 4; // Schritt war vollendet
if (canWalkRight()) {
x++;
} //gehe entgültig nach rechts
// Always reset position, in case we are stuck partly into a brick.
relx = 0;
absx = x*16;
if (!(canStand()||hangAtPole())) // kein Halt mehr...arrrgghhh
initFall (FALL2, FALLDELAY);
else
status = STANDING; // Figur hat Halt
}
}
else {
status = STANDING; // Figur sollte sich nur Umdrehen.
}
}
void KGrFigure::initFall(int apm, int FALLDELAY)
{
status = FALLING;
actualPixmap = apm;
walkCounter=1;
walkTimer->stop();
fallTimer->start((FALLDELAY * NSPEED) / speed, TRUE);
}
void KGrFigure::showFigure ()
{
}
void KGrFigure::setPlayfield (KGrObject * (*p)[30][22])
{
playfield = p;
}
KGrFigure :: ~KGrFigure ()
{
}
KGrHero :: KGrHero (KGrCanvas * view, int x, int y)
:KGrFigure (x, y)
{
heroView = view;
status = STANDING;
actualPixmap = FALL1;
herox = x;
heroy = y;
started = FALSE;
mouseMode = TRUE;
walkCounter = 1;
walkFrozen = FALSE;
fallFrozen = FALSE;
connect (walkTimer, TQT_SIGNAL (timeout ()), TQT_SLOT (walkTimeDone ()));
connect (fallTimer, TQT_SIGNAL (timeout ()), TQT_SLOT (fallTimeDone ()));
}
int KGrHero::WALKDELAY = 0;
int KGrHero::FALLDELAY = 0;
/* Es ist Notwendig der eigentlichen Timerfunktion diese
Startwalk vorzuschalten, um bei einem evtl. notwendigem
Richtungswechsel der Figur diese Bewegung mit einzufügen */
void KGrHero::startWalk ()
{
switch (nextDir) {
case UP:
if ((*playfield)[x][y]->whatIam () == LADDER)
{walkCounter = 1;
direction = UP;}
break;
case RIGHT:
if (hangAtPole())
actualPixmap = RIGHTCLIMB1;
else
actualPixmap = RIGHTWALK1;
if (direction != RIGHT)
walkCounter = 0;
else
walkCounter = 1;
direction = RIGHT;
break;
case DOWN:
if (((*playfield)[x][y]->whatIam () == LADDER)||
((*playfield)[x][y+1]->whatIam () == LADDER))
{walkCounter = 1;
direction = DOWN;}
else // wenn figur an Stange haengt und nichts unter ihr,
if (hangAtPole() && (!canStand())) // dann soll sie fallen
{ status = STANDING;
actualPixmap = (direction==RIGHT)?19:18;
walkCounter=1;
direction=STAND;
walkTimer->stop();
}
break;
case LEFT:
if (hangAtPole())
actualPixmap = LEFTCLIMB1;
else
actualPixmap = LEFTWALK1;
if (direction != LEFT)
walkCounter = 0;
else
walkCounter = 1;
direction = LEFT;
break;
default :
direction = STAND;
status = FALLING;
break;
}
nextDir = STAND;
if (status != FALLING)//immer ausführen, ausser beim fallen
{ status = WALKING; // da sonst der FALLINGstatus wieder
showFigure (); // geaendert wird und der falsche Timer anspringt.
}
} // END KGrHero::startWalk
void KGrHero::setKey(Direction key)
{
// Keyboard control of hero: direction is fixed until next key is pressed.
// Sets a simulated mouse-pointer above, below, left, right or on the hero.
mouseMode = FALSE;
stopped = FALSE;
switch (key) {
case UP: mousex = x; mousey = 0; break;
case DOWN: mousex = x; mousey = FIELDHEIGHT + 1; break;
case LEFT: mousex = 0; mousey = y; break;
case RIGHT: mousex = FIELDWIDTH + 1; mousey = y; break;
case STAND: stopped = TRUE; mousex = x; mousey = y; break;
}
}
void KGrHero::setDirection(int i, int j)
{
// Mouse control of hero: direction is updated continually on a timer.
mouseMode = TRUE;
stopped = FALSE;
mousex = i;
mousey = j;
}
void KGrHero::setNextDir()
{
int dx, dy;
if (! mouseMode) {
// Keyboard control of hero: adjust simulated mouse-pointer.
if (stopped) {
mousex = x;
mousey = y;
}
if ((mousey < 1) || (mousey > FIELDHEIGHT)) {
mousex = x; // Stay directly above/below the hero.
}
else if ((mousex < 1) || (mousex > FIELDWIDTH)) {
mousey = y; // Stay directly left/right of the hero.
}
}
dx = mousex - x; dy = mousey - y;
if ((dy == 0) && (y == 1) && (nuggets <= 0)) {
nextDir = UP;
}
else if ((dy > 0) &&
(canWalkDown() ||
standOnEnemy() ||
(hangAtPole() && ((*playfield)[x][y+1]->whatIam() != BRICK) &&
((*playfield)[x][y+1]->whatIam() != BETON)))) {
nextDir = DOWN;
}
else if ((dy < 0) && canWalkUp ()) {
nextDir = UP;
}
else if (dx > 0) {
nextDir = RIGHT;
}
else if (dx < 0) {
nextDir = LEFT;
}
else if (dx == 0) {
nextDir = STAND;
}
}
void KGrHero::doStep() {
if (walkFrozen) {
walkFrozen = FALSE;
walkTimeDone();
}
if (fallFrozen) {
fallFrozen = FALSE;
fallTimeDone();
}
}
void KGrHero::showState(char option)
{
printf("(%02d,%02d) - Hero ", x, y);
switch (option) {
case 'p': printf ("\n"); break;
case 's': printf (" nuggets %02d status %d walk-ctr %d ",
nuggets, status, walkCounter);
printf ("dirn %d next dirn %d\n", direction, nextDir);
printf (" rel (%02d,%02d) abs (%03d,%03d)",
relx, rely, absx, absy);
printf (" pix %02d", actualPixmap);
printf (" mem %d %d %d %d", mem_x, mem_y, mem_relx, mem_rely);
if (walkFrozen) printf (" wBlock");
if (fallFrozen) printf (" fBlock");
printf ("\n");
break;
}
}
void KGrHero::init(int a,int b)
{
walkTimer->stop();
fallTimer->stop();
walkCounter = 1;
started = FALSE;
x = mem_x = a;
y = mem_y = b;
relx = mem_relx = 0;
rely = mem_rely = 0;
absx = 16*x;
absy = 16*y;
nuggets = 0;
if (herox < 1) { // If first call to init, ...
heroView->makeHeroSprite (x, y, actualPixmap);
}
herox = x;
heroy = y;
actualPixmap = FALL2;
heroView->moveHero (absx, absy, actualPixmap);
}
void KGrHero::start()
{
started = TRUE;
walkFrozen = FALSE;
fallFrozen = FALSE;
if (!(canStand()||hangAtPole())) { // Held muss wohl fallen...
status = FALLING;
fallTimeDone();
}
else {
status = STANDING;
walkTimeDone();
}
}
void KGrHero::setSpeed (int gamespeed)
{
if (gamespeed >= 0) {
if (gamespeed < MINSPEED)
speed++; // Increase speed.
else
speed = gamespeed; // Set selected speed.
if (speed > MAXSPEED)
speed = MAXSPEED; // Set upper limit.
}
else {
speed--; // Reduce speed.
if (speed < MINSPEED)
speed = MINSPEED; // Set lower limit.
}
KGrBrick::speed = speed; // Make a copy for bricks.
}
void KGrHero::walkTimeDone ()
{
if (! started) return; // Ignore signals from earlier play.
if (KGrObject::frozen) {walkFrozen = TRUE; return; }
if ((*playfield)[x][y]->whatIam() == BRICK) {
emit caughtHero(); // Brick closed over hero.
return;
}
if ((y==1)&&(nuggets<=0)) { // If on top row and all nuggets collected,
emit leaveLevel(); // the hero has won and can go to next level.
return;
}
if (status == STANDING)
setNextDir();
if ((status == STANDING) && (nextDir != STAND)) {
if ((standOnEnemy()) && (nextDir == DOWN)) {
emit caughtHero(); // Hero is going to step down into an enemy.
return;
}
startWalk();
}
if (status != STANDING) {
switch (direction) {
case UP: walkUp (WALKDELAY); break;
case DOWN: walkDown (WALKDELAY, FALLDELAY); break;
case RIGHT: walkRight (WALKDELAY, FALLDELAY); break;
case LEFT: walkLeft (WALKDELAY, FALLDELAY); break;
default :
// The following code is strange. It makes the hero fall off a pole.
// It works because of other strange code in "startWalk(), case DOWN:".
if (!canStand()||hangAtPole()) // falling
initFall(FALL1, FALLDELAY);
else status = STANDING;
break;
}
herox=x;heroy=y; // Koordinatenvariablen neu
// wenn Held genau ein Feld weitergelaufen ist,
if ((relx==0)&&(rely==0)) // dann setzte statische
{
collectNugget(); // und nehme evtl. Nugget
}
showFigure(); // Is this REDUNDANT now? See showFigure() below.
//////////////////////////////////////////////////
}
if (status == STANDING)
if (!canStand()&&!hangAtPole())
initFall(FALL1, FALLDELAY);
else
walkTimer->start ((WALKDELAY * NSPEED) / speed, TRUE);
// This additional showFigure() is to update the hero position after it is
// altered by the hero-enemy deadlock fix in standOnEnemy(). Messy, but ...
////////////////////////////////////////////////////////////////////////////
showFigure();
if(isInEnemy()) {
walkTimer->stop();
emit caughtHero();
}
}
void KGrHero::fallTimeDone()
{
if (! started) return; // Ignore signals from earlier play.
if (KGrObject::frozen) {fallFrozen = TRUE; return; }
if (!standOnEnemy()) {
if (walkCounter++ < 4) { // Held fällt vier Positionen
fallTimer->start((FALLDELAY * NSPEED) / speed, TRUE);
rely+=STEP;
absy+=STEP;
}
else { //Held ist ein Feld weitergefallen
// Verschiebung der Figur zum 0-Punkt des Objekts (Brick etc...)
heroy = ++y;
rely = 0;
absy = y*16; // wird Null und Figur eins runter
collectNugget(); // gesetzt. Zeit evtl. Nugget zu nehmen
if (! (canStand()||hangAtPole())) { // Held muss wohl weiterfallen.
fallTimer->start((FALLDELAY * NSPEED) / speed, TRUE);
walkCounter = 1;
}
else { // Held hat Boden (oder Feind) unter den
status = STANDING; // Füssen oder hängt an Stange -> steh!
walkTimer->start((WALKDELAY * NSPEED) / speed, TRUE);
direction = (actualPixmap == 19) ? RIGHT : LEFT;
if ((*playfield)[x][y]->whatIam() == POLE)
actualPixmap = (direction == RIGHT)? RIGHTCLIMB1:LEFTCLIMB1;
// else
// Reduce jerkiness when descending over a falling enemy.
// actualPixmap = (direction == RIGHT)? RIGHTWALK1:LEFTWALK1;
}
}
showFigure();
}
else {
if (rely == 0) {
// If at the bottom of a cell, try to walk or just stand still.
status = STANDING;
direction = (actualPixmap == 19) ? RIGHT : LEFT;
if ((*playfield)[x][y]->whatIam() == POLE)
actualPixmap = (direction == RIGHT)? RIGHTCLIMB1:LEFTCLIMB1;
// else
// Reduce jerkiness when descending over a falling enemy.
// actualPixmap = (direction == RIGHT)? RIGHTWALK1:LEFTWALK1;
walkTimer->start((WALKDELAY * NSPEED) / speed, TRUE);
}
else {
// Else, freeze hero until enemy moves out of the way.
fallTimer->start((FALLDELAY * NSPEED) / speed, TRUE);
}
}
if (isInEnemy() && (! standOnEnemy()))
emit caughtHero();
}
void KGrHero::showFigure () {
heroView->moveHero (absx, absy, actualPixmap);
// Merke alte Werte zum löschen der Figur
mem_x = x;
mem_y = y;
mem_relx = relx;
mem_rely = rely;
}
void KGrHero::dig(){
if (direction == LEFT)
digLeft();
else
if (direction == RIGHT)
digRight();
}
void KGrHero::digLeft(){
int i = 1; // If stationary or moving up/down, dig at x-1.
if (status == STANDING)
setNextDir();
if ((status == WALKING) ||
((status == STANDING) && ((nextDir == LEFT) || (nextDir == RIGHT)))) {
if ((direction == LEFT) && canWalkLeft())
i = 2; // If walking left, dig at x-2 and stop at x-1.
else if ((direction == RIGHT) && canWalkRight())
i = 0; // If walking right, dig at x and stop at x+1.
}
if (((*playfield)[x-i][y+1]->whatIam() == BRICK)&&
(((*playfield)[x-i][y]->whatIam() == HLADDER)||
((*playfield)[x-i][y]->whatIam() == FREE)||
((*playfield)[x-i][y]->whatIam() == HOLE)))
((KGrBrick*)(*playfield)[x-i][y+1])->dig();
}
void KGrHero::digRight(){
int i = 1; // If stationary or moving up/down, dig at x+1.
if (status == STANDING)
setNextDir();
if ((status == WALKING) ||
((status == STANDING) && ((nextDir == LEFT) || (nextDir == RIGHT)))) {
if ((direction == LEFT) && canWalkLeft())
i = 0; // If walking left, dig at x and stop at x-1.
else if ((direction == RIGHT) && canWalkRight())
i = 2; // If walking right, dig at x+2 and stop at x+1.
}
if (((*playfield)[x+i][y+1]->whatIam() == BRICK)&&
(((*playfield)[x+i][y]->whatIam() == HLADDER)||
((*playfield)[x+i][y]->whatIam() == FREE)||
((*playfield)[x+i][y]->whatIam() == HOLE)))
((KGrBrick*)(*playfield)[x+i][y+1])->dig();
}
#ifdef QT3
void KGrHero::setEnemyList(TQPtrList<KGrEnemy> *e)
#else
void KGrHero::setEnemyList(TQPtrList<KGrEnemy> *e)
#endif
{
enemies = e;
}
bool KGrHero::standOnEnemy()
{
int c = 0;
int range = enemies->count();
if (range > 0) {
for (KGrEnemy * enemy = enemies->at (c); c < range; ) {
enemy = enemies->at(c++);
// Test if hero's foot is at or just below enemy's head (tolerance
// of 4 pixels) and the two figures overlap in the X direction.
if ((((absy + 16) == enemy->gety()) ||
((absy + 12) == enemy->gety())) &&
(((absx - 16) < enemy->getx()) &&
((absx + 16) > enemy->getx()))) {
if (((absy + 12) == enemy->gety()) &&
(enemy->getStatus() != FALLING)) {
absy = absy - rely; // Bounce back from overlap, to avoid
rely = 0; // hero-enemy mid-cycle deadlock.
walkCounter = 1;
}
return true;
}
}
}
return false;
}
void KGrHero::collectNugget(){
if ((*playfield)[x][y]->whatIam() == NUGGET)
{
((KGrFree *)(*playfield)[x][y])->setNugget(false);
if (!(--nuggets))
emit haveAllNuggets(); // sendet der Application dass alle Nuggets
// gesammelt sind, um versteckte Leitern zu zeigen
emit gotNugget(250); // sendet der Application ein Nugget um Score zu erhöhen
}
}
void KGrHero::loseNugget() {
// Enemy trapped or dead and could not drop nugget (NO SCORE for this).
if (! (--nuggets))
emit haveAllNuggets(); // Sendet der Application dass alle Nuggets
// gesammelt sind, um versteckte Leitern zu zeigen.
}
bool KGrHero::isInEnemy(){
int c=0;
int range=enemies->count();
if (range)
for (KGrEnemy *enemy=enemies->at(c);c<range; )
{enemy = enemies->at(c++);
if (isInside(enemy->getx(),enemy->gety())||
isInside(enemy->getx()-15,enemy->gety())||
isInside(enemy->getx(),enemy->gety()-15))
return true;}
return false;
}
bool KGrHero::isInside(int enemyx, int enemyy){
return ((absx >= enemyx)&&
(absx <= enemyx+15)&&
(absy >= enemyy)&&
(absy <= enemyy+15));
}
KGrHero :: ~KGrHero (){
delete walkTimer;
delete fallTimer;
}
KGrEnemy :: KGrEnemy (KGrCanvas * view, int x, int y)
: KGrFigure (x, y)
{
enemyView = view;
actualPixmap = FALL1;
nuggets = 0;
enemyView->makeEnemySprite (x, y, actualPixmap);
walkCounter = 1;
captiveCounter = 0;
searchStatus = HORIZONTAL;
birthX=x;
birthY=y;
walkFrozen = FALSE;
fallFrozen = FALSE;
captiveFrozen = FALSE;
captiveTimer = new TQTimer (this);
connect (captiveTimer,TQT_SIGNAL(timeout()),TQT_SLOT(captiveTimeDone()));
connect (walkTimer, TQT_SIGNAL (timeout ()), TQT_SLOT (walkTimeDone ()));
connect (fallTimer, TQT_SIGNAL (timeout ()), TQT_SLOT (fallTimeDone ()));
}
int KGrEnemy::WALKDELAY = 0;
int KGrEnemy::FALLDELAY = 0;
int KGrEnemy::CAPTIVEDELAY = 0;
void KGrEnemy::doStep() {
if (walkFrozen) {
walkFrozen = FALSE;
walkTimeDone();
}
if (fallFrozen) {
fallFrozen = FALSE;
fallTimeDone();
}
if (captiveFrozen) {
captiveFrozen = FALSE;
captiveTimeDone();
}
}
void KGrEnemy::showState(char option)
{
printf("(%02d,%02d) - Enemy [%d]", x, y, enemyId);
switch (option) {
case 'p': printf ("\n"); break;
case 's': printf (" nuggets %02d status %d walk-ctr %d ",
nuggets, status, walkCounter);
printf ("dirn %d search %d capt-ctr %d\n",
direction, searchStatus, captiveCounter);
printf (" rel (%02d,%02d) abs (%03d,%03d)",
relx, rely, absx, absy);
printf (" pix %02d", actualPixmap);
printf (" mem %d %d %d %d", mem_x, mem_y, mem_relx, mem_rely);
if (walkFrozen) printf (" wBlock");
if (fallFrozen) printf (" fBlock");
if (captiveFrozen) printf (" cBlock");
printf ("\n");
break;
}
}
void KGrEnemy::init(int a,int b)
{
walkTimer->stop(); // alles stoppen bevor die Werte neu gesetzt
fallTimer->stop(); // werden, da es sonst zu ungewollten Effekten
captiveTimer->stop(); // kommen kann
walkCounter = 1;
captiveCounter = 0;
x = mem_x = a;
y = mem_y = b;
relx = mem_relx = 0;
rely = mem_rely = 0;
absx=16*x;
absy=16*y;
actualPixmap = 19;
status = STANDING;
}
void KGrEnemy::walkTimeDone ()
{
if (KGrObject::frozen) {walkFrozen = TRUE; return; }
// Check we are alive BEFORE checking for friends being in the way.
// Maybe a friend overhead is blocking our escape from a brick.
if ((*playfield)[x][y]->whatIam()==BRICK) { // sollte er aber in einem Brick
dieAndReappear(); // sein, dann stirbt er wohl
return; // Must leave "walkTimeDone" when an enemy dies.
}
if (! bumpingFriend()) {
switch (direction) {
case UP: walkUp (WALKDELAY);
if ((rely == 0) &&
((*playfield)[x][y+1]->whatIam() == USEDHOLE))
// Enemy kletterte grad aus einem Loch hinaus
// -> gib es frei!
((KGrBrick *)(*playfield)[x][y+1])->unUseHole();
break;
case DOWN: walkDown (WALKDELAY, FALLDELAY); break;
case RIGHT: walkRight (WALKDELAY, FALLDELAY); break;
case LEFT: walkLeft (WALKDELAY, FALLDELAY); break;
default: // Switch search direction in KGoldrunner search (only).
searchStatus = (searchStatus==VERTIKAL) ?
HORIZONTAL : VERTIKAL;
// In KGoldrunner rules, if a hole opens under an enemy
// who is standing and waiting to move, he should fall.
if (!(canStand()||hangAtPole())) {
initFall (actualPixmap, FALLDELAY);
}
else {
status = STANDING;
}
break;
}
// wenn die Figur genau ein Feld gelaufen ist
if (status == STANDING) { // dann suche den Helden
direction = searchbestway(x,y,herox,heroy); // und
if (walkCounter >= 4) {
if (! nuggets)
collectNugget();
else
dropNugget();
}
status = WALKING; // initialisiere die Zählervariablen und
walkCounter = 1; // den Timer um den Held weiter
walkTimer->start ((WALKDELAY * NSPEED) / speed, TRUE); // zu jagen
startWalk ();
}
}
else {
// A friend is in the way. Try a new direction, but not if leaving a hole.
Direction dirn;
// In KGoldrunner rules, change the search strategy,
// to avoid enemy-enemy deadlock.
searchStatus = (searchStatus==VERTIKAL) ? HORIZONTAL : VERTIKAL;
dirn = searchbestway (x, y, herox, heroy);
if ((dirn != direction) && ((*playfield)[x][y]->whatIam() != USEDHOLE)) {
direction = dirn;
status = WALKING;
walkCounter = 1;
relx = 0; absx = 16 * x;
rely = 0; absy = 16 * y;
startWalk ();
}
walkTimer->start ((WALKDELAY * NSPEED) / speed, TRUE);
}
showFigure();
}
void KGrEnemy::fallTimeDone ()
{
if (KGrObject::frozen) {fallFrozen = TRUE; return; }
if ((*playfield)[x][y+1]->whatIam() == HOLE) { // wenn Enemy ins Loch fällt
((KGrBrick*)(*playfield)[x][y+1])->useHole(); // reserviere das Loch, damit
// kein anderer es benutzt und
if (nuggets) { // er muss Gold vorher fallen lassen
nuggets=0;
switch ((*playfield)[x][y]->whatIam()) {
case FREE:
case HLADDER:
((KGrFree *)(*playfield)[x][y])->setNugget(true); break;
default:
emit lostNugget(); break; // Cannot drop the nugget here.
}
}
emit trapped (75); // Enemy trapped: score 75.
}
else if (walkCounter <= 1) {
// Enemies collect nuggets when falling.
if (!nuggets) {
collectNugget();
}
}
if ((*playfield)[x][y]->whatIam()==BRICK) { // sollte er aber in einem Brick
dieAndReappear(); // sein, dann stirbt er wohl
return; // Must leave "fallTimeDone" when an enemy dies.
}
if (standOnEnemy()) { // Don't fall into a friend.
fallTimer->start((FALLDELAY * NSPEED) / speed, TRUE);
return;
}
if (walkCounter++ < 4){
fallTimer->start((FALLDELAY * NSPEED) / speed, TRUE);
{ rely+=STEP; absy+=STEP;}
}
else {
rely = 0; y ++; absy=16*y;
if ((*playfield)[x][y]->whatIam() == USEDHOLE) {
captiveCounter = 0;
status = CAPTIVE;
captiveTimer->start((CAPTIVEDELAY * NSPEED) / speed, TRUE);
}
else if (!(canStand()||hangAtPole())) {
fallTimer->start((FALLDELAY * NSPEED) / speed, TRUE);
walkCounter=1;
}
else {
status = STANDING;
if (hangAtPole())
actualPixmap=(direction ==RIGHT)?8:12;
}
}
if (status == STANDING) {
status = WALKING;
walkCounter = 1;
direction = searchbestway(x,y,herox,heroy);
walkTimer->start ((WALKDELAY * NSPEED) / speed, TRUE);
startWalk ();
if (!nuggets)
collectNugget();
else
dropNugget();
}
showFigure();
}
void KGrEnemy::captiveTimeDone()
{
if (KGrObject::frozen) {captiveFrozen = TRUE; return; }
if ((*playfield)[x][y]->whatIam()==BRICK)
dieAndReappear();
else
if (captiveCounter > 6){
status = WALKING;
walkCounter = 1;
direction = UP;
walkTimer->start ((WALKDELAY * NSPEED) / speed, TRUE);
captiveCounter = 0;
} else {
captiveCounter ++;
captiveTimer->start((CAPTIVEDELAY * NSPEED) / speed, TRUE);
showFigure();
}
}
void KGrEnemy::startSearching()
{
// Called from "KGoldrunner::startPlaying" and "KGrEnemy::dieAndReappear".
init(x,y);
if (canStand()||((*playfield)[x][y+1]->whatIam()==USEDHOLE)) {
status = WALKING;
walkTimer->start ((WALKDELAY * NSPEED) / speed, TRUE);
}
else {
status = FALLING;
fallTimer->start ((FALLDELAY * NSPEED) / speed, TRUE);
}
walkCounter = 1;
direction = searchbestway(x,y,herox,heroy);
startWalk();
}
void KGrEnemy::collectNugget()
{
if (((*playfield)[x][y]->whatIam() == NUGGET) && (nuggets == 0) &&
(alwaysCollectNugget || ((int)(5.0*rand()/RAND_MAX+1.0) > 4))){
((KGrFree *)(*playfield)[x][y])->setNugget(false);
nuggets=1;
}
}
void KGrEnemy::dropNugget()
{
if (((int)(DROPNUGGETDELAY*rand()/RAND_MAX+1.0) > DROPNUGGETDELAY-5) &&
((*playfield)[x][y]->whatIam() == FREE)) {
((KGrFree *)(*playfield)[x][y])->setNugget(true);
nuggets=0;
}
}
void KGrEnemy::showFigure ()
{
enemyView->moveEnemy (enemyId, absx, absy, actualPixmap, nuggets);
// Merke alte Werte zum löschen der Figur
mem_x = x;
mem_y = y;
mem_relx = relx;
mem_rely = rely;
}
bool KGrEnemy::canWalkUp()
{
return (((*playfield)[x][y-1]->whatIam() != BRICK) &&
((*playfield)[x][y-1]->whatIam() != BETON) &&
((*playfield)[x][y-1]->whatIam() != FBRICK) &&
(((*playfield)[x][y]->whatIam() == USEDHOLE) ||
((*playfield)[x][y]->whatIam() == LADDER)));
}
void KGrEnemy::startWalk ()
{
switch (direction) {
case UP: break;
case RIGHT: if (hangAtPole())
actualPixmap = RIGHTCLIMB1;
else
actualPixmap = RIGHTWALK1;
break;
case DOWN: break;
case LEFT: if (hangAtPole())
actualPixmap = LEFTCLIMB1;
else
actualPixmap = LEFTWALK1;
break;
default: break;
}
}
void KGrEnemy::dieAndReappear()
{
bool looking;
int i;
if (nuggets > 0) {
nuggets = 0; // Enemy died and could not drop nugget.
emit lostNugget(); // NO SCORE for lost nugget.
}
emit killed (75); // Killed an enemy: score 75.
if (reappearAtTop) {
// Follow Traditional rules: enemies reappear at top.
looking = TRUE;
y = 2;
// Randomly look for a free spot in row 2. Limit the number of tries.
for (i = 1; ((i <= 3) && looking); i++) {
x = (int)((FIELDWIDTH * (float) rand()) / RAND_MAX) + 1;
switch ((*playfield)[x][2]->whatIam()) {
case FREE:
case HLADDER:
looking = FALSE;
break;
default:
break;
}
}
// If unsuccessful, choose the first free spot in row 3 or below.
while ((y<FIELDHEIGHT) && looking) {
y++;
x = 0;
while ((x<FIELDWIDTH) && looking) {
x++;
switch ((*playfield)[x][y]->whatIam()) {
case FREE:
case HLADDER:
looking = FALSE;
break;
default:
break;
}
}
}
}
else {
// Follow KGoldrunner rules: enemies reappear where they started.
x = birthX;
y = birthY;
}
// Enemy reappears and starts searching for the hero.
startSearching();
}
Direction KGrEnemy::searchbestway(int ew,int eh,int hw,int hh)
{
Direction dirn;
if ((*playfield)[x][y]->whatIam() == USEDHOLE) // Could not get out of
return UP; // hole (eg. brick above
// closed): keep trying.
if (!canStand() && // Cannot stand,
!hangAtPole() && // not on pole, not
!standOnEnemy() && // walking on friend and
!((*playfield)[x][y+1]->whatIam() == HOLE)) // not just out of hole,
return DOWN; // so must fall.
switch (searchStrategy) {
// Traditional search strategy.
case LOW:
dirn = STAND;
if (eh == hh) { // Hero on same row.
dirn = lowGetHero (ew, eh, hw);
}
if (dirn != STAND) return (dirn); // Can go towards him.
if (eh >= hh) { // Hero above enemy.
dirn = lowSearchUp (ew, eh, hh); // Find a way up.
}
else { // Hero below enemy.
dirn = lowSearchDown (ew, eh, hh); // Find way down to him.
if (dirn == STAND) {
dirn = lowSearchUp (ew, eh, hh); // No go: try up.
}
}
if (dirn == STAND) { // When all else fails,
dirn = lowSearchDown (ew, eh, eh - 1); // find way below hero.
}
return dirn;
break;
// KGoldrunner search strategy.
case MEDIUM:
case HIGH:
if(searchStatus==VERTIKAL){
if (eh > hh)
return searchupway(ew,eh);
if (eh < hh)
return searchdownway(ew,eh);
return STAND;
} else {
if (ew > hw)
return searchleftway(ew,eh);
if (ew < hw)
return searchrightway(ew,eh);
return STAND;
}
break;
}
return STAND;
}
///////////////////////////////////////////////
// Methods for medium-level search strategy. //
///////////////////////////////////////////////
Direction KGrEnemy :: searchdownway(int ew,int eh){
int i,j;
i=j=ew;
if ( (*playfield)[ew][eh]->searchValue & CANWALKDOWN)
return DOWN;
else while((i>=0)||(j<=FIELDWIDTH)){
if (i>=0)
if ( (*playfield)[i][eh]->searchValue & CANWALKDOWN)
return LEFT;
else
if (!(( (*playfield)[i--][eh]->searchValue & CANWALKLEFT) ||
(runThruHole && ( (*playfield)[i][eh]->whatIam() == HOLE))))
i=-1;
if (j<=FIELDWIDTH)
if ( (*playfield)[j][eh]->searchValue & CANWALKDOWN)
return RIGHT;
else
if (!(( (*playfield)[j++][eh]->searchValue & CANWALKRIGHT) ||
(runThruHole && ( (*playfield)[j][eh]->whatIam() == HOLE))))
j=FIELDWIDTH+1;
}
return STAND;
}
Direction KGrEnemy :: searchupway(int ew,int eh){
int i,j;
i=j=ew;
if ( (*playfield)[ew][eh]->searchValue & CANWALKUP)
return UP;
else while((i>=0)||(j<=FIELDWIDTH)){// search for the first way up
if (i>=0)
if ( (*playfield)[i][eh]->searchValue & CANWALKUP)
return LEFT;
else
if (!(( (*playfield)[i--][eh]->searchValue & CANWALKLEFT) ||
(runThruHole && ( (*playfield)[i][eh]->whatIam() == HOLE))))
i=-1;
if (j<=FIELDWIDTH)
if ( (*playfield)[j][eh]->searchValue & CANWALKUP)
return RIGHT;
else
if (!(( (*playfield)[j++][eh]->searchValue & CANWALKRIGHT) ||
(runThruHole && ( (*playfield)[j][eh]->whatIam() == HOLE))))
j=FIELDWIDTH+1;
}
// BUG FIX - Ian W., 30/4/01 - Don't leave an enemy standing in mid air.
if (!canStand()) return DOWN; else return STAND;
}
Direction KGrEnemy :: searchleftway(int ew,int eh){
int i,j;
i=j=eh;
if ( ((*playfield)[ew][eh]->searchValue & CANWALKLEFT) || /* kann figur nach links laufen ?*/
(runThruHole && ( (*playfield)[ew-1][eh]->whatIam() == HOLE)))
return LEFT;
else while((i>=0)||(j<=FIELDHEIGHT)){ /* in den grenzen ?*/
if (i>=0)
if ( ((*playfield)[ew][i]->searchValue & CANWALKLEFT) || /* ein weg nach links- oben gefunden ?*/
(runThruHole && ( (*playfield)[ew-1][i]->whatIam() == HOLE)))
return UP; /* geh nach oben */
else
if (!( (*playfield)[ew][i--]->searchValue & CANWALKUP)) /* sonst ...*/
i=-1;
if (j<=FIELDHEIGHT)
if ( ((*playfield)[ew][j]->searchValue & CANWALKLEFT) || /* ein weg nach links- unten gefunden ?*/
(runThruHole && ( (*playfield)[ew-1][j]->whatIam() == HOLE)))
return DOWN; /* geh nach unten */
else
if (!( (*playfield)[ew][j++]->searchValue&CANWALKDOWN)) /* sonst ... */
j=FIELDHEIGHT+1;
}
return STAND; /* default */
}
Direction KGrEnemy :: searchrightway(int ew,int eh)
{
int i,j;
i=j=eh;
if ( ((*playfield)[ew][eh]->searchValue & CANWALKRIGHT) ||
(runThruHole && ( (*playfield)[ew+1][eh]->whatIam() == HOLE)))
return RIGHT;
else while((i>=0)||(j<=FIELDHEIGHT)){
if (i>=0)
if ( ((*playfield)[ew][i]->searchValue & CANWALKRIGHT) ||
(runThruHole && ( (*playfield)[ew+1][i]->whatIam() == HOLE)))
return UP;
else
if (!( (*playfield)[ew][i--]->searchValue & CANWALKUP))
i=-1;
if (j<=FIELDHEIGHT)
if ( ((*playfield)[ew][j]->searchValue & CANWALKRIGHT) ||
(runThruHole && ( (*playfield)[ew+1][j]->whatIam() == HOLE)))
return DOWN;
else
if (!( (*playfield)[ew][j++]->searchValue & CANWALKDOWN))
j=FIELDHEIGHT+1;
}
return STAND;
}
////////////////////////////////////////////
// Methods for low-level search strategy. //
////////////////////////////////////////////
Direction KGrEnemy::lowSearchUp (int ew, int eh, int hh)
{
int i, ilen, ipos, j, jlen, jpos, deltah, rungs;
deltah = eh - hh; // Get distance up to hero's level.
// Search for the best ladder right here or on the left.
i = ew; ilen = 0; ipos = -1;
while (i >= 1) {
rungs = distanceUp (i, eh, deltah);
if (rungs > ilen) {
ilen = rungs; // This the best yet.
ipos = i;
}
if (searchOK (-1, i, eh))
i--; // Look further to the left.
else
i = -1; // Cannot go any further to the left.
}
// Search for the best ladder on the right.
j = ew; jlen = 0; jpos = -1;
while (j < FIELDWIDTH) {
if (searchOK (+1, j, eh)) {
j++; // Look further to the right.
rungs = distanceUp (j, eh, deltah);
if (rungs > jlen) {
jlen = rungs; // This the best yet.
jpos = j;
}
}
else
j = FIELDWIDTH+1; // Cannot go any further to the right.
}
if ((ilen == 0) && (jlen == 0)) // No ladder found.
return STAND;
// Choose a ladder to go to.
if (ilen != jlen) { // If the ladders are not the same
// length, choose the longer one.
if (ilen > jlen) {
if (ipos == ew) // If already on the best ladder, go up.
return UP;
else
return LEFT;
}
else
return RIGHT;
}
else { // Both ladders are the same length.
if (ipos == ew) // If already on the best ladder, go up.
return UP;
else if (ilen == deltah) { // If both reach the hero's level,
if ((ew - ipos) <= (jpos - ew)) // choose the closest.
return LEFT;
else
return RIGHT;
}
else return LEFT; // Else choose the left ladder.
}
}
Direction KGrEnemy::lowSearchDown (int ew, int eh, int hh)
{
int i, ilen, ipos, j, jlen, jpos, deltah, rungs, path;
deltah = hh - eh; // Get distance down to hero's level.
// Search for the best way down, right here or on the left.
ilen = 0; ipos = -1;
i = (willNotFall (ew, eh)) ? ew : -1;
rungs = distanceDown (ew, eh, deltah);
if (rungs > 0) {
ilen = rungs; ipos = ew;
}
while (i >= 1) {
rungs = distanceDown (i - 1, eh, deltah);
if (((rungs > 0) && (ilen == 0)) ||
((deltah > 0) && (rungs > ilen)) ||
((deltah <= 0) && (rungs < ilen) && (rungs != 0))) {
ilen = rungs; // This the best way yet.
ipos = i - 1;
}
if (searchOK (-1, i, eh))
i--; // Look further to the left.
else
i = -1; // Cannot go any further to the left.
}
// Search for the best way down, on the right.
j = ew; jlen = 0; jpos = -1;
while (j < FIELDWIDTH) {
rungs = distanceDown (j + 1, eh, deltah);
if (((rungs > 0) && (jlen == 0)) ||
((deltah > 0) && (rungs > jlen)) ||
((deltah <= 0) && (rungs < jlen) && (rungs != 0))) {
jlen = rungs; // This the best way yet.
jpos = j + 1;
}
if (searchOK (+1, j, eh)) {
j++; // Look further to the right.
}
else
j = FIELDWIDTH+1; // Cannot go any further to the right.
}
if ((ilen == 0) && (jlen == 0)) // Found no way down.
return STAND;
// Choose a way down to follow.
if (ilen == 0)
path = jpos;
else if (jlen == 0)
path = ipos;
else if (ilen != jlen) { // If the ways down are not same length,
// choose closest to hero's level.
if (deltah > 0) {
if (jlen > ilen)
path = jpos;
else
path = ipos;
}
else {
if (jlen > ilen)
path = ipos;
else
path = jpos;
}
}
else { // Both ways down are the same length.
if ((deltah > 0) && // If both reach the hero's level,
(ilen == deltah)) { // choose the closest.
if ((ew - ipos) <= (jpos - ew))
path = ipos;
else
path = jpos;
}
else
path = ipos; // Else, go left or down.
}
if (path == ew)
return DOWN;
else if (path < ew)
return LEFT;
else
return RIGHT;
}
Direction KGrEnemy::lowGetHero (int ew, int eh, int hw)
{
int i, inc, returnValue;
inc = (ew > hw) ? -1 : +1;
i = ew;
while (i != hw) {
returnValue = canWalkLR (inc, i, eh);
if (returnValue > 0)
i = i + inc; // Can run further towards the hero.
else if (returnValue < 0)
break; // Will run into a wall regardless.
else
return STAND; // Won't run over a hole.
}
if (i < ew) return LEFT;
else if (i > ew) return RIGHT;
else return STAND;
}
int KGrEnemy::distanceUp (int x, int y, int deltah)
{
int rungs = 0;
// If there is a ladder at (x.y), return its length, else return zero.
while ( (*playfield)[x][y - rungs]->whatIam() == LADDER) {
rungs++;
if (rungs >= deltah) // To hero's level is enough.
break;
}
return rungs;
}
int KGrEnemy::distanceDown (int x, int y, int deltah)
{
// When deltah > 0, we want an exit sideways at the hero's level or above.
// When deltah <= 0, we can go down any distance (as a last resort).
int rungs = -1;
int exitRung = 0;
bool canGoThru = TRUE;
char objType;
// If there is a way down at (x,y), return its length, else return zero.
// Because rungs == -1, we first check that level y is not blocked here.
while (canGoThru) {
objType = (*playfield)[x][y + rungs + 1]->whatIam();
switch (objType) {
case BRICK:
case BETON:
case HOLE: // Enemy cannot go DOWN through a hole.
case USEDHOLE:
if ((deltah > 0) && (rungs <= deltah))
exitRung = rungs;
if ((objType == HOLE) && (rungs < 0))
rungs = 0; // Enemy can go SIDEWAYS through a hole.
else
canGoThru = FALSE; // Cannot go through here.
break;
case LADDER:
case POLE: // Can go through or stop.
rungs++; // Add to path length.
if ((deltah > 0) && (rungs >= 0)) {
// If at or above hero's level, check for an exit from ladder.
if ((rungs - 1) <= deltah) {
// Maybe can stand on top of ladder and exit L or R.
if ((objType == LADDER) && (searchOK (-1, x, y+rungs-1) ||
searchOK (+1, x, y+rungs-1)))
exitRung = rungs - 1;
// Maybe can exit L or R from ladder body or pole.
if ((rungs <= deltah) && (searchOK (-1, x, y+rungs) ||
searchOK (+1, x, y+rungs)))
exitRung = rungs;
}
else
canGoThru = FALSE; // Should stop at hero's level.
}
break;
default:
rungs++; // Can go through. Add to path length.
break;
}
}
if (rungs == 1) {
for (KGrEnemy *enemy=enemies->first();enemy!=0;enemy=enemies->next()) {
if((x*16==enemy->getx()) && (y*16+16==enemy->gety()))
rungs = 0; // Pit is blocked. Find another way.
}
}
if (rungs <= 0)
return 0; // There is no way down.
else if (deltah > 0)
return exitRung; // We want to take an exit, if any.
else
return rungs; // We can go down all the way.
}
bool KGrEnemy::searchOK (int direction, int x, int y)
{
// Check whether it is OK to search left or right.
if (canWalkLR (direction, x, y) > 0) {
// Can go into that cell, but check for a fall.
if (willNotFall (x+direction, y))
return TRUE;
}
return FALSE; // Cannot go into and through that cell.
}
int KGrEnemy::canWalkLR (int direction, int x, int y)
{
if (willNotFall (x, y)) {
switch ((*playfield)[x+direction][y]->whatIam()) {
case BETON:
case BRICK:
case USEDHOLE:
return -1; break; // Will be halted in current cell.
default:
// NB. FREE, LADDER, HLADDER, NUGGET and POLE are OK of course,
// but enemies can also walk left/right through a HOLE and
// THINK they can walk left/right through a FBRICK.
return +1; break; // Can walk into next cell.
}
}
else
return 0; // Will fall before getting there.
}
bool KGrEnemy::willNotFall (int x, int y)
{
int c, cmax;
KGrEnemy *enemy;
// Check the ceiling.
switch ( (*playfield)[x][y]->whatIam()) {
case LADDER:
case POLE:
return TRUE; break; // OK, can hang on ladder or pole.
default:
break;
}
// Check the floor.
switch ( (*playfield)[x][y+1]->whatIam()) {
// Cases where the enemy knows he will fall.
case FREE:
case HLADDER:
case FBRICK:
// N.B. The enemy THINKS he can run over a NUGGET, a buried POLE or a HOLE.
// The last of these cases allows the hero to trap the enemy, of course.
// Note that there are several Traditional levels that require an enemy to
// be trapped permanently in a pit containing a nugget, as he runs towards
// you. It is also possible to use a buried POLE in the same way.
cmax = enemies->count();
for (c = 0; c < cmax; c++) {
enemy = enemies->at(c);
if ((enemy->getx()==16*x) && (enemy->gety()==16*(y+1)))
return TRUE; // Standing on a friend.
}
return FALSE; break; // Will fall: there is no floor.
default:
return TRUE; break; // OK, will not fall.
}
}
#ifdef QT3
void KGrEnemy::setEnemyList(TQPtrList<KGrEnemy> *e)
#else
void KGrEnemy::setEnemyList(TQPtrList<KGrEnemy> *e)
#endif
{
enemies = e;
}
bool KGrEnemy::standOnEnemy()
{
int c = 0;
int range = enemies->count();
if (range > 1) {
for (KGrEnemy * enemy = enemies->at (c); c < range; ) {
enemy = enemies->at(c++);
// Test if enemy's foot is at or just below enemy's head (tolerance
// of 4 pixels) and the two figures overlap in the X direction.
if ((((absy + 16) == enemy->gety()) ||
((absy + 12) == enemy->gety())) &&
(((absx - 16) < enemy->getx()) &&
((absx + 16) > enemy->getx()))) {
return true;
}
}
}
return false;
}
bool KGrEnemy::bumpingFriend()
{
// Enemies that are falling are treated as being invisible (i.e. a walking
// enemy will walk through them or they will stop on that enemy's head).
//
// If two enemies are moving in opposite directions, they are allowed to come
// within two cell widths of each other (8 steps). Then one must stop before
// entering the next cell and let the other one go into it. If both are about
// to enter a new cell, the one on the right or above gives way to the one on
// the left or below (implemented by letting the latter approach to 7 steps
// apart before detecting an impending collision, by which time the first
// enemy should have stopped and given way).
//
// In all other cases, enemies are allowed to approach to 4 steps apart (i.e.
// bumping a friend), before being forced to stop and wait. If they somehow
// get closer than 4 steps (i.e. overlapping), the lower ID enemy is allowed
// to move out while the higher ID enemy waits. This can happen if one enemy
// falls into another or is reborn where another enemy is located.
int c, cmax;
KGrEnemy *enemy;
int dx, dy;
cmax = enemies->count();
for (c = 0; c < cmax; c++) {
enemy = enemies->at(c);
if ((enemy->enemyId != enemyId) && (enemy->status != FALLING)) {
dx = enemy->getx() - absx;
dy = enemy->gety() - absy;
switch (direction) {
case LEFT:
if ((dx >= -32) && (dx < 16) && (dy > -16) && (dy < 16)) {
if ((enemy->direction == RIGHT) &&
(enemy->status == WALKING) && (absx%16==0)) {
return TRUE;
}
else if (dx >= -16) {
if ((dx > -16) && (enemyId < enemy->enemyId))
return FALSE;
else
return TRUE; // Wait for the one in front.
}
}
break;
case RIGHT:
if ((dx > -16) && (dx < 32) && (dy > -16) && (dy < 16)) {
if ((enemy->direction == LEFT) &&
(enemy->status == WALKING) && (absx%16==0)) {
return TRUE;
}
else if (dx <= 16) {
if ((dx < 16) && (enemyId < enemy->enemyId))
return FALSE;
else
return TRUE; // Wait for the one in front.
}
}
break;
case UP:
if ((dy >= -32) && (dy < 16) && (dx > -16) && (dx < 16)) {
if ((enemy->direction == DOWN) &&
(enemy->status == WALKING) && (absy%16==0)) {
return TRUE;
}
else if (dy >= -16) {
if ((dy > -16) && (enemyId < enemy->enemyId))
return FALSE;
else
return TRUE; // Wait for the one above.
}
}
break;
case DOWN:
if ((dy > -16) && (dy < 32) && (dx > -16) && (dx < 16)) {
if ((enemy->direction == UP) &&
(enemy->status == WALKING) && (absy%16==0)) {
return TRUE;
}
else if (dy <= 16) {
if ((dy < 16) && (enemyId < enemy->enemyId))
return FALSE;
else
return TRUE; // Wait for the one below.
}
}
break;
default:
break;
}
}
}
return FALSE;
}
KGrEnemy :: ~KGrEnemy ()
{
delete captiveTimer;
delete walkTimer;
delete fallTimer;
}
#include "kgrfigure.moc"
|